Java BufferedImage Transparent pickBestTransparency(BufferedImage image)

Here you can find the source of pickBestTransparency(BufferedImage image)

Description

pick Best Transparency

License

Open Source License

Declaration

public static int pickBestTransparency(BufferedImage image) 

Method Source Code

//package com.java2s;
/*/*from   ww w . j a v  a 2 s  .  c  o  m*/
 * Copyright (c) 2014 tabletoptool.com team.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     rptools.com team - initial implementation
 *     tabletoptool.com team - further development
 */

import java.awt.Image;

import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;

public class Main {
    /**
     * Look at the image and determine which Transparency is most appropriate.
     * If it finds any translucent pixels it returns Transparency.TRANSLUCENT, if 
     * it finds at least one purely transparent pixel and no translucent pixels
     * it will return Transparency.BITMASK, in all other cases it returns 
     * Transparency.OPAQUE, including errors
     * 
     * @param image
     * @return one of Transparency constants
     */
    public static int pickBestTransparency(Image image) {

        // Take a shortcut if possible
        if (image instanceof BufferedImage) {
            return pickBestTransparency((BufferedImage) image);
        }

        // Legacy method
        // NOTE: This is a horrible memory hog
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        int[] pixelArray = new int[width * height];
        PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixelArray, 0, width);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
            System.err.println("interrupted waiting for pixels!");
            return Transparency.OPAQUE;
        }

        if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
            System.err.println("image fetch aborted or errored");
            return Transparency.OPAQUE;
        }

        // Look for specific pixels
        boolean foundTransparent = false;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // Get the next pixel
                int pixel = pixelArray[y * width + x];
                int alpha = (pixel >> 24) & 0xff;

                // Is there translucency or just pure transparency ?
                if (alpha > 0 && alpha < 255) {
                    return Transparency.TRANSLUCENT;
                }

                if (alpha == 0 && !foundTransparent) {
                    foundTransparent = true;
                }
            }
        }

        return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE;
    }

    public static int pickBestTransparency(BufferedImage image) {

        // See if we can short circuit
        ColorModel colorModel = image.getColorModel();
        if (colorModel.getTransparency() == Transparency.OPAQUE) {
            return Transparency.OPAQUE;
        }

        // Get the pixels
        int width = image.getWidth();
        int height = image.getHeight();

        // Look for specific pixels
        boolean foundTransparent = false;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // Get the next pixel
                int pixel = image.getRGB(x, y);
                int alpha = (pixel >> 24) & 0xff;

                // Is there translucency or just pure transparency ?
                if (alpha > 0 && alpha < 255) {
                    return Transparency.TRANSLUCENT;
                }

                if (alpha == 0 && !foundTransparent) {
                    foundTransparent = true;
                }
            }
        }

        return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE;
    }
}

Related

  1. cutTransparentBorder(BufferedImage src)
  2. fixTransparency(String var0, BufferedImage var1)
  3. makeTransparency(BufferedImage image, int color)
  4. makeTranspBufferedImage(Image image)
  5. nonTransparentPixels(BufferedImage image)
  6. setTranformImagePixels(BufferedImage origImg, Color paramColor, int colorRangePerc, Vector transPixel, Shape cutoutShape)
  7. transparentBG(BufferedImage image, Boolean transparent)
  8. transparentColor(BufferedImage src, Color trColor)