Example usage for java.awt.image IndexColorModel getRGBs

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

Introduction

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

Prototype

public final void getRGBs(int[] rgb) 

Source Link

Document

Converts data for each index from the color and alpha component arrays to an int in the default RGB ColorModel format and copies the resulting 32-bit ARGB values into the specified array.

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/*from   ww  w  .java2  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 void prepareColorSpace(PSGenerator gen, ColorModel cm) throws IOException {
    //Prepare color space
    if ((cm instanceof IndexColorModel)) {
        ColorSpace cs = cm.getColorSpace();
        IndexColorModel im = (IndexColorModel) cm;
        gen.write("[/Indexed " + getColorSpaceName(cs));
        int c = im.getMapSize();
        int hival = c - 1;
        if (hival > 4095) {
            throw new UnsupportedOperationException("hival must not go beyond 4095");
        }/*w w w  .j  a  v a2 s.  co m*/
        gen.writeln(" " + Integer.toString(hival));
        gen.write("  <");
        int[] palette = new int[c];
        im.getRGBs(palette);
        for (int i = 0; i < c; i++) {
            if (i > 0) {
                if ((i % 8) == 0) {
                    gen.newLine();
                    gen.write("   ");
                } else {
                    gen.write(" ");
                }
            }
            gen.write(rgb2Hex(palette[i]));
        }
        gen.writeln(">");
        gen.writeln("] setcolorspace");
    } else {
        gen.writeln(getColorSpaceName(cm.getColorSpace()) + " setcolorspace");
    }
}