Java BufferedImage Transparent fixTransparency(String var0, BufferedImage var1)

Here you can find the source of fixTransparency(String var0, BufferedImage var1)

Description

fix Transparency

License

LGPL

Declaration

static BufferedImage fixTransparency(String var0, BufferedImage var1) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferInt;
import java.awt.image.ImageObserver;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import java.util.Arrays;
import java.util.HashMap;

import java.util.Map;

public class Main {
    private static final Map imagePool = new HashMap();
    private static final Map bufferPool = new HashMap();

    static BufferedImage fixTransparency(String var0, BufferedImage var1) {
        if (var1 == null) {
            return var1;
        } else {/*from  www . j  ava 2 s  . co m*/
            long var2 = System.currentTimeMillis();
            var1 = convertToARGB(var1);
            int var4 = var1.getWidth();
            int var5 = var1.getHeight();
            IntBuffer var6 = getARGBAsIntBuffer(var1);
            IntBuffer var7 = var6;
            label33:

            while (var4 % 2 == 0 && var5 % 2 == 0) {
                int var8 = 0;

                while (true) {
                    if (var8 >= var7.limit()) {
                        break label33;
                    }

                    if (var7.get(var8) >>> 24 == 0) {
                        IntBuffer var9 = getPooledBuffer(var4 * var5).asIntBuffer();
                        scaleHalf(var7, var4, var5, var9, 8);
                        var7 = var9;
                        var4 >>= 1;
                        var5 >>= 1;
                        break;
                    }

                    ++var8;
                }
            }

            long var12 = System.currentTimeMillis();

            if (var7 != var6) {
                setBackgroundColor(var6, var1.getWidth(), var1.getHeight(), var7, var1.getWidth() / var4);
            }

            long var10 = System.currentTimeMillis();
            return var1;
        }
    }

    private static BufferedImage convertToARGB(BufferedImage var0) {
        if (var0 == null) {
            return null;
        } else if (var0.getType() == 2) {
            return var0;
        } else {
            int var1 = var0.getWidth();
            int var2 = var0.getHeight();
            BufferedImage var3 = getPooledImage(var1, var2, 0);
            Graphics2D var4 = var3.createGraphics();
            Arrays.fill(getARGBAsIntBuffer(var3).array(), 0);
            var4.drawImage(var0, 0, 0, (ImageObserver) null);
            return var3;
        }
    }

    private static IntBuffer getARGBAsIntBuffer(BufferedImage var0) {
        DataBuffer var1 = var0.getRaster().getDataBuffer();

        if (var1 instanceof DataBufferInt) {
            return IntBuffer.wrap(((DataBufferInt) var1).getData());
        } else if (var1 instanceof DataBufferByte) {
            return ByteBuffer.wrap(((DataBufferByte) var1).getData()).order(ByteOrder.BIG_ENDIAN).asIntBuffer();
        } else {
            int var2 = var0.getWidth();
            int var3 = var0.getHeight();
            int[] var4 = new int[var2 * var3];
            var0.getRGB(0, 0, var2, var3, var4, 0, var2);
            return IntBuffer.wrap(var4);
        }
    }

    private static ByteBuffer getPooledBuffer(int var0) {
        Reference var1 = (Reference) bufferPool.get(Integer.valueOf(var0));
        ByteBuffer var2 = var1 == null ? null : (ByteBuffer) var1.get();

        if (var2 == null) {
            var2 = ByteBuffer.allocateDirect(var0);
            bufferPool.put(Integer.valueOf(var0), new SoftReference(var2));
        }

        var2.order(ByteOrder.BIG_ENDIAN);
        var2.position(0);
        return var2;
    }

    static void scaleHalf(IntBuffer var0, int var1, int var2, IntBuffer var3, int var4) {
        for (int var5 = 0; var5 < var1 / 2; ++var5) {
            for (int var6 = 0; var6 < var2 / 2; ++var6) {
                int var7 = var1 * 2 * var6 + 2 * var5;
                int var8 = var0.get(var7);
                int var9 = var0.get(var7 + 1);
                int var10 = var0.get(var7 + var1);
                int var11 = var0.get(var7 + var1 + 1);

                if (var4 != 0) {
                    var8 = Integer.rotateLeft(var8, var4);
                    var9 = Integer.rotateLeft(var9, var4);
                    var10 = Integer.rotateLeft(var10, var4);
                    var11 = Integer.rotateLeft(var11, var4);
                }

                int var12 = average4RGBA(var8, var9, var10, var11);

                if (var4 != 0) {
                    var12 = Integer.rotateRight(var12, var4);
                }

                var3.put(var1 / 2 * var6 + var5, var12);
            }
        }
    }

    private static BufferedImage scaleHalf(BufferedImage var0) {
        int var1 = var0.getWidth();
        int var2 = var0.getHeight();
        BufferedImage var3 = getPooledImage(var1 / 2, var2 / 2, 0);
        scaleHalf(getARGBAsIntBuffer(var0), var1, var2, getARGBAsIntBuffer(var3), 8);
        return var3;
    }

    private static void setBackgroundColor(BufferedImage var0, BufferedImage var1) {
        int var2 = var0.getWidth();
        int var3 = var0.getHeight();
        int var4 = var2 / var1.getWidth();
        IntBuffer var5 = getARGBAsIntBuffer(var0);
        IntBuffer var6 = getARGBAsIntBuffer(var1);
        setBackgroundColor(var5, var2, var3, var6, var4);
    }

    private static void setBackgroundColor(IntBuffer var0, int var1, int var2, IntBuffer var3, int var4) {
        for (int var5 = 0; var5 < var1; ++var5) {
            for (int var6 = 0; var6 < var2; ++var6) {
                int var7 = var1 * var6 + var5;
                int var8 = var0.get(var7);

                if ((var8 & -16777216) == 0) {
                    var8 = var3.get(var6 / var4 * (var1 / var4) + var5 / var4);
                    var0.put(var7, var8 & 16777215);
                }
            }
        }
    }

    private static BufferedImage getPooledImage(int var0, int var1, int var2) {
        String var3 = String.format("%dx%d#%d",
                new Object[] { Integer.valueOf(var0), Integer.valueOf(var1), Integer.valueOf(var2) });
        Reference var4 = (Reference) imagePool.get(var3);
        BufferedImage var5 = var4 == null ? null : (BufferedImage) var4.get();

        if (var5 == null) {
            var5 = new BufferedImage(var0, var1, 2);
            imagePool.put(var3, new SoftReference(var5));
        }

        return var5;
    }

    private static int average4RGBA(int var0, int var1, int var2, int var3) {
        int var4 = var0 & 255;
        int var5 = var1 & 255;
        int var6 = var2 & 255;
        int var7 = var3 & 255;

        switch (var4 << 24 | var5 << 16 | var6 << 8 | var7) {
        case -16777216:
            return var0;

        case -16776961:
            return average2RGBA(var0, var3);

        case -16711936:
            return average2RGBA(var0, var2);

        case -65536:
            return average2RGBA(var0, var1);

        case -1:
        case 0:
            return average2RGBA(average2RGBA(var0, var3), average2RGBA(var1, var2));

        case 255:
            return var3;

        case 65280:
            return var2;

        case 65535:
            return average2RGBA(var2, var3);

        case 16711680:
            return var1;

        case 16711935:
            return average2RGBA(var1, var3);

        case 16776960:
            return average2RGBA(var1, var2);

        default:
            int var8 = var4 + var5 + var6 + var7;
            int var9 = var8 >> 2;

            for (int var10 = 8; var10 < 32; var10 += 8) {
                int var11 = (var4 * (var0 >> var10 & 255) + var5 * (var1 >> var10 & 255)
                        + var6 * (var2 >> var10 & 255) + var7 * (var3 >> var10 & 255)) / var8;
                var9 |= var11 << var10;
            }

            return var9;
        }
    }

    private static int average2RGBA(int var0, int var1) {
        return ((var0 & -16843010) >>> 1) + ((var1 & -16843010) >>> 1) | var0 & var1 & 16843009;
    }
}

Related

  1. ApplyTransparency(BufferedImage image, Image mask)
  2. applyTransparency(BufferedImage src, float alpha)
  3. bordersNonTransparentPixel(BufferedImage data, int wid, int hei, boolean[] traced, int x, int y)
  4. ConvToTransparentImage(BufferedImage src, float alpha)
  5. cutTransparentBorder(BufferedImage src)
  6. makeTransparency(BufferedImage image, int color)
  7. makeTranspBufferedImage(Image image)
  8. nonTransparentPixels(BufferedImage image)
  9. pickBestTransparency(BufferedImage image)