Example usage for java.awt.image IndexColorModel getPixelSize

List of usage examples for java.awt.image IndexColorModel getPixelSize

Introduction

In this page you can find the example usage for java.awt.image IndexColorModel getPixelSize.

Prototype

public int getPixelSize() 

Source Link

Document

Returns the number of bits per pixel described by this ColorModel .

Usage

From source file:org.apache.fop.render.pdf.AbstractImageAdapter.java

/**
 * This is to be used by populateXObjectDictionary() when the image is palette based.
 * @param dict the dictionary to fill in
 * @param icm the image color model//  w  ww  . ja v  a  2  s. c  o m
 */
protected void populateXObjectDictionaryForIndexColorModel(PDFDictionary dict, IndexColorModel icm) {
    PDFArray indexed = new PDFArray(dict);
    indexed.add(new PDFName("Indexed"));
    if (icm.getColorSpace().getType() != ColorSpace.TYPE_RGB) {
        log.warn("Indexed color space is not using RGB as base color space."
                + " The image may not be handled correctly." + " Base color space: " + icm.getColorSpace()
                + " Image: " + image.getInfo());
    }
    indexed.add(new PDFName(toPDFColorSpace(icm.getColorSpace()).getName()));
    int c = icm.getMapSize();
    int hival = c - 1;
    if (hival > MAX_HIVAL) {
        throw new UnsupportedOperationException("hival must not go beyond " + MAX_HIVAL);
    }
    indexed.add(Integer.valueOf(hival));
    int[] palette = new int[c];
    icm.getRGBs(palette);
    ByteArrayOutputStream baout = new ByteArrayOutputStream();
    for (int i = 0; i < c; i++) {
        // TODO Probably doesn't work for non RGB based color spaces
        // See log warning above
        int entry = palette[i];
        baout.write((entry & 0xFF0000) >> 16);
        baout.write((entry & 0xFF00) >> 8);
        baout.write(entry & 0xFF);
    }
    indexed.add(baout.toByteArray());

    dict.put("ColorSpace", indexed);
    dict.put("BitsPerComponent", icm.getPixelSize());

    Integer index = getIndexOfFirstTransparentColorInPalette(icm);
    if (index != null) {
        PDFArray mask = new PDFArray(dict);
        mask.add(index);
        mask.add(index);
        dict.put("Mask", mask);
    }
}

From source file:org.apache.xmlgraphics.ps.PSImageUtils.java

private static ColorModel populateImageDictionary(ImageEncodingHelper helper, PSDictionary imageDict) {
    RenderedImage img = helper.getImage();
    String w = Integer.toString(img.getWidth());
    String h = Integer.toString(img.getHeight());
    imageDict.put("/ImageType", "1");
    imageDict.put("/Width", w);
    imageDict.put("/Height", h);

    ColorModel cm = helper.getEncodedColorModel();

    boolean invertColors = false;
    String decodeArray = getDecodeArray(cm.getNumComponents(), invertColors);
    int bitsPerComp = cm.getComponentSize(0);

    // Setup scanning for left-to-right and top-to-bottom
    imageDict.put("/ImageMatrix", "[" + w + " 0 0 " + h + " 0 0]");

    if ((cm instanceof IndexColorModel)) {
        IndexColorModel im = (IndexColorModel) cm;
        int c = im.getMapSize();
        int hival = c - 1;
        if (hival > 4095) {
            throw new UnsupportedOperationException("hival must not go beyond 4095");
        }/*from  w  w w .  j  a va  2 s .c  om*/
        bitsPerComp = im.getPixelSize();
        int ceiling = ((int) Math.pow(2, bitsPerComp)) - 1;
        decodeArray = "[0 " + ceiling + "]";
    }
    imageDict.put("/BitsPerComponent", Integer.toString(bitsPerComp));
    imageDict.put("/Decode", decodeArray);
    return cm;
}