get Alpha Image - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

get Alpha Image

Demo Code

/*// w w  w  .  j a v a  2  s  .co  m
 * VectorGraphics2D: Vector export for Java(R) Graphics2D
 *
 * (C) Copyright 2010-2015 Erich Seifert <dev[at]erichseifert.de>
 *
 * This file is part of VectorGraphics2D.
 *
 * VectorGraphics2D is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VectorGraphics2D is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with VectorGraphics2D.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

import java.awt.Transparency;
import java.awt.color.ColorSpace;

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;

import java.awt.image.WritableRaster;

public class Main {
    public static BufferedImage getAlphaImage(BufferedImage image) {
        WritableRaster alphaRaster = image.getAlphaRaster();
        int width = image.getWidth();
        int height = image.getHeight();

        ColorModel cm;
        WritableRaster raster;
        // TODO Handle bitmap masks (work on ImageDataStream is necessary)
        /*
        if (image.getTransparency() == BufferedImage.BITMASK) {
              byte[] arr = {(byte) 0, (byte) 255};

              cm = new IndexColorModel(1, 2, arr, arr, arr);
              raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE,
                    width, height, 1, 1, null);
        } else {*/
        ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        int[] bits = { 8 };
        cm = new ComponentColorModel(colorSpace, bits, false, true,
                Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        raster = cm.createCompatibleWritableRaster(width, height);
        //}

        BufferedImage alphaImage = new BufferedImage(cm, raster, false,
                null);

        int[] alphaValues = new int[image.getWidth()
                * alphaRaster.getNumBands()];
        for (int y = 0; y < image.getHeight(); y++) {
            alphaRaster.getPixels(0, y, image.getWidth(), 1, alphaValues);
            // FIXME Don't force 8-bit alpha channel (see TODO above)
            if (image.getTransparency() == BufferedImage.BITMASK) {
                for (int i = 0; i < alphaValues.length; i++) {
                    if (alphaValues[i] > 0) {
                        alphaValues[i] = 255;
                    }
                }
            }
            alphaImage.getRaster().setPixels(0, y, image.getWidth(), 1,
                    alphaValues);
        }

        return alphaImage;
    }
}

Related Tutorials