Example usage for java.awt.image BufferedImage getColorModel

List of usage examples for java.awt.image BufferedImage getColorModel

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getColorModel.

Prototype

public ColorModel getColorModel() 

Source Link

Document

Returns the ColorModel .

Usage

From source file:at.gv.egiz.pdfas.common.utils.ImageUtils.java

public static BufferedImage makeTransparent(BufferedImage image, int x, int y) {
    ColorModel cm = image.getColorModel();
    if (!(cm instanceof IndexColorModel))
        return image; // sorry...
    IndexColorModel icm = (IndexColorModel) cm;
    WritableRaster raster = image.getRaster();
    int pixel = raster.getSample(x, y, 0); // pixel is offset in ICM's
    // palette//ww w  .  j a  va2  s  .  co m
    int size = icm.getMapSize();
    byte[] reds = new byte[size];
    byte[] greens = new byte[size];
    byte[] blues = new byte[size];
    icm.getReds(reds);
    icm.getGreens(greens);
    icm.getBlues(blues);
    IndexColorModel icm2 = new IndexColorModel(8, size, reds, greens, blues, pixel);
    return new BufferedImage(icm2, raster, image.isAlphaPremultiplied(), null);
}

From source file:ImageUtil.java

/**
 * create new image from source image// w w  w  . jav  a  2s. com
 * @param srcImg source image
 * @param targetWidth target image width
 * @param targetHeight target image height
 * @return new image with specify width and height
 */
public static BufferedImage createNewImage(BufferedImage srcImg, int targetWidth, int targetHeight) {
    BufferedImage targetImg = null;
    int type = srcImg.getType();
    if (type == BufferedImage.TYPE_CUSTOM) {
        ColorModel cm = srcImg.getColorModel();
        WritableRaster raster = cm.createCompatibleWritableRaster(targetWidth, targetHeight);
        boolean alphaPremultiplied = cm.isAlphaPremultiplied();
        targetImg = new BufferedImage(cm, raster, alphaPremultiplied, null);
    } else {
        targetImg = new BufferedImage(targetWidth, targetHeight, type);
    }
    return targetImg;
}

From source file:SWTUtils.java

/**
 * Converts a buffered image to SWT <code>ImageData</code>.
 *
 * @param bufferedImage  the buffered image (<code>null</code> not
 *         permitted)./*  w w w .  jav a2  s.  co  m*/
 *
 * @return The image data.
 */
public static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
                colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[3];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
                data.setPixel(x, y, pixel);
            }
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}

From source file:org.openossad.util.ImageUtil.java

public static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
                colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[3];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
                data.setPixel(x, y, pixel);
            }/*  w  w w.  ja  v a 2s .  c om*/
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}

From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactory.java

private static BufferedImage getAlphaImage(BufferedImage image) {
    if (!image.getColorModel().hasAlpha()) {
        return null;
    }//from w w  w.  j  a  v  a2s.co m
    if (image.getTransparency() == Transparency.BITMASK) {
        throw new UnsupportedOperationException(
                "BITMASK Transparency JPEG compression is not" + " useful, use LosslessImageFactory instead");
    }
    WritableRaster alphaRaster = image.getAlphaRaster();
    if (alphaRaster == null) {
        // happens sometimes (PDFBOX-2654) despite colormodel claiming to have alpha
        return null;
    }
    BufferedImage alphaImage = new BufferedImage(image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_BYTE_GRAY);
    alphaImage.setData(alphaRaster);
    return alphaImage;
}

From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactory.java

private static BufferedImage getColorImage(BufferedImage image) {
    if (!image.getColorModel().hasAlpha()) {
        return image;
    }//  w  w w  . j  a  v a 2  s. c o m

    if (image.getColorModel().getColorSpace().getType() != ColorSpace.TYPE_RGB) {
        throw new UnsupportedOperationException("only RGB color spaces are implemented");
    }

    // create an RGB image without alpha
    // BEWARE: the previous solution in the history
    // g.setComposite(AlphaComposite.Src) and g.drawImage()
    // didn't work properly for TYPE_4BYTE_ABGR.
    // alpha values of 0 result in a black dest pixel!!!
    BufferedImage rgbImage = new BufferedImage(image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_3BYTE_BGR);
    return new ColorConvertOp(null).filter(image, rgbImage);
}

From source file:ConvertUtil.java

/**
 * Converts the source image to 24-bit colour (RGB). No transparency.
 * //w ww  .  ja va2  s. c o  m
 * @param src
 *            the source image to convert
 * @return a copy of the source image with a 24-bit colour depth
 */
public static BufferedImage convert24(BufferedImage src) {
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);
    return dest;
}

From source file:ConvertUtil.java

/**
 * Converts the source image to 32-bit colour with transparency (ARGB).
 * /*  w w w  .  ja  v  a 2s  . c om*/
 * @param src
 *            the source image to convert
 * @return a copy of the source image with a 32-bit colour depth.
 */
public static BufferedImage convert32(BufferedImage src) {
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);
    return dest;
}

From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactory.java

public static PDColorSpace getColorSpaceFromAWT(BufferedImage awtImage) {
    if (awtImage.getColorModel().getNumComponents() == 1) {
        // 256 color (gray) JPEG
        return PDDeviceGray.INSTANCE;
    }//from  ww w . j  a  v  a2  s  .  c  om

    ColorSpace awtColorSpace = awtImage.getColorModel().getColorSpace();
    if (awtColorSpace instanceof ICC_ColorSpace && !awtColorSpace.isCS_sRGB()) {
        throw new UnsupportedOperationException("ICC color spaces not implemented");
    }

    switch (awtColorSpace.getType()) {
    case ColorSpace.TYPE_RGB:
        return PDDeviceRGB.INSTANCE;
    case ColorSpace.TYPE_GRAY:
        return PDDeviceGray.INSTANCE;
    case ColorSpace.TYPE_CMYK:
        return PDDeviceCMYK.INSTANCE;
    default:
        throw new UnsupportedOperationException("color space not implemented: " + awtColorSpace.getType());
    }
}

From source file:ConvertUtil.java

/**
 * Converts the source image to 8-bit colour using the default 256-colour
 * palette. No transparency./*from  w w w .j  a va 2s  . c o  m*/
 * 
 * @param src
 *            the source image to convert
 * @return a copy of the source image with an 8-bit colour depth
 */
public static BufferedImage convert8(BufferedImage src) {
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);
    return dest;
}