Example usage for java.awt.image IndexColorModel IndexColorModel

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

Introduction

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

Prototype

public IndexColorModel(int bits, int size, int[] cmap, int start, int transferType, BigInteger validBits) 

Source Link

Document

Constructs an IndexColorModel from an int array where each int is comprised of red, green, blue, and alpha components in the default RGB color model format.

Usage

From source file:org.eclipse.swt.snippets.Snippet156.java

static BufferedImage convertToAWT(ImageData data) {
    ColorModel colorModel = null;
    PaletteData palette = data.palette;//from  www  . j  av  a  2 s  . co  m
    if (palette.isDirect) {
        colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask);
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                RGB rgb = palette.getRGB(pixel);
                bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue);
            }
        }
        return bufferedImage;
    } else {
        RGB[] rgbs = palette.getRGBs();
        byte[] red = new byte[rgbs.length];
        byte[] green = new byte[rgbs.length];
        byte[] blue = new byte[rgbs.length];
        for (int i = 0; i < rgbs.length; i++) {
            RGB rgb = rgbs[i];
            red[i] = (byte) rgb.red;
            green[i] = (byte) rgb.green;
            blue[i] = (byte) rgb.blue;
        }
        if (data.transparentPixel != -1) {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel);
        } else {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue);
        }
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        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++) {
                int pixel = data.getPixel(x, y);
                pixelArray[0] = pixel;
                raster.setPixel(x, y, pixelArray);
            }
        }
        return bufferedImage;
    }
}

From source file:Snippet156.java

static BufferedImage convertToAWT(ImageData data) {
    ColorModel colorModel = null;
    PaletteData palette = data.palette;//from  w  w  w  .  ja  v  a2s  . c om
    if (palette.isDirect) {
        colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask);
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        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++) {
                int pixel = data.getPixel(x, y);
                RGB rgb = palette.getRGB(pixel);
                pixelArray[0] = rgb.red;
                pixelArray[1] = rgb.green;
                pixelArray[2] = rgb.blue;
                raster.setPixels(x, y, 1, 1, pixelArray);
            }
        }
        return bufferedImage;
    } else {
        RGB[] rgbs = palette.getRGBs();
        byte[] red = new byte[rgbs.length];
        byte[] green = new byte[rgbs.length];
        byte[] blue = new byte[rgbs.length];
        for (int i = 0; i < rgbs.length; i++) {
            RGB rgb = rgbs[i];
            red[i] = (byte) rgb.red;
            green[i] = (byte) rgb.green;
            blue[i] = (byte) rgb.blue;
        }
        if (data.transparentPixel != -1) {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel);
        } else {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue);
        }
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        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++) {
                int pixel = data.getPixel(x, y);
                pixelArray[0] = pixel;
                raster.setPixel(x, y, pixelArray);
            }
        }
        return bufferedImage;
    }
}

From source file:net.d53dev.dslfy.web.service.GifService.java

public BufferedImage convertRGBAToGIF(BufferedImage src, int transColor) {
    BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
    Graphics g = dst.getGraphics();
    g.setColor(new Color(transColor));
    g.fillRect(0, 0, dst.getWidth(), dst.getHeight());
    {//from  w w w  . j  a v  a  2s  .c o  m
        IndexColorModel indexedModel = (IndexColorModel) dst.getColorModel();
        WritableRaster raster = dst.getRaster();
        int sample = raster.getSample(0, 0, 0);
        int size = indexedModel.getMapSize();
        byte[] rr = new byte[size];
        byte[] gg = new byte[size];
        byte[] bb = new byte[size];
        indexedModel.getReds(rr);
        indexedModel.getGreens(gg);
        indexedModel.getBlues(bb);
        IndexColorModel newModel = new IndexColorModel(8, size, rr, gg, bb, sample);
        dst = new BufferedImage(newModel, raster, dst.isAlphaPremultiplied(), null);
    }
    dst.createGraphics().drawImage(src, 0, 0, null);
    return dst;
}

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/*w  ww.  j av  a  2 s . c  o  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:de.fhg.igd.swingrcp.SwingRCPUtilities.java

/**
 * Convert a SWT Image to a {@link BufferedImage}
 * /* w  w w  .java  2  s .  c om*/
 * {@link "http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java?view=co"}
 * 
 * @param data the SWT {@link ImageData}
 * @param applyAlphaMask true if the image data's alpha mask should be
 *            applied to the result image (if there is any). This method
 *            calls {@link #applyTransparencyMask(BufferedImage, ImageData)}
 *            for that purpose.
 * @return the AWT {@link BufferedImage}
 */
public static BufferedImage convertToAWT(ImageData data, boolean applyAlphaMask) {
    ColorModel colorModel = null;
    PaletteData palette = data.palette;

    BufferedImage result;
    if (palette.isDirect) {
        colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask);
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                RGB rgb = palette.getRGB(pixel);
                bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue);
            }
        }
        result = bufferedImage;
    } else {
        RGB[] rgbs = palette.getRGBs();
        byte[] red = new byte[rgbs.length];
        byte[] green = new byte[rgbs.length];
        byte[] blue = new byte[rgbs.length];
        for (int i = 0; i < rgbs.length; i++) {
            RGB rgb = rgbs[i];
            red[i] = (byte) rgb.red;
            green[i] = (byte) rgb.green;
            blue[i] = (byte) rgb.blue;
        }
        if (data.transparentPixel != -1) {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel);
        } else {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue);
        }
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        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++) {
                int pixel = data.getPixel(x, y);
                pixelArray[0] = pixel;
                raster.setPixel(x, y, pixelArray);
            }
        }
        result = bufferedImage;
    }

    if (data.getTransparencyType() == SWT.TRANSPARENCY_MASK && applyAlphaMask) {
        result = applyTransparencyMask(result, data.getTransparencyMask());
    }
    return result;
}

From source file:org.csa.rstb.dat.toolviews.HaAlphaPlotPanel.java

private void initColorModels() {
    for (int j = 0; j <= 1; j++) {
        final int palSize = 256;
        final byte[] r = new byte[palSize];
        final byte[] g = new byte[palSize];
        final byte[] b = new byte[palSize];
        final byte[] a = new byte[palSize];
        r[0] = (byte) backgroundColor.getRed();
        g[0] = (byte) backgroundColor.getGreen();
        b[0] = (byte) backgroundColor.getBlue();
        a[0] = (byte) backgroundColor.getAlpha();

        final Palette pal = new Palette("Rainbow", new Color[] { Color.black, Color.blue, Color.cyan,
                Color.green, Color.yellow, Color.orange, Color.red });
        for (int i = 1; i < 256; ++i) {
            float value = i / 255f;
            if (j == 0)
                value = (255 - i) / 255f;
            Color c = pal.lookupColor(value);
            r[i] = (byte) c.getRed();
            g[i] = (byte) c.getGreen();
            b[i] = (byte) c.getBlue();
            a[i] = (byte) 255;
        }//from   www .ja va 2  s . c om

        if (j == 0) {
            toggledColorModel = new IndexColorModel(8, palSize, r, g, b, a);
        } else {
            untoggledColorModel = new IndexColorModel(8, palSize, r, g, b, a);
        }
    }
}

From source file:org.esa.beam.visat.toolviews.stat.DensityPlotPanel.java

private void initColorModels() {
    for (int j = 0; j <= 1; j++) {
        final int palSize = 256;
        final byte[] r = new byte[palSize];
        final byte[] g = new byte[palSize];
        final byte[] b = new byte[palSize];
        final byte[] a = new byte[palSize];
        r[0] = (byte) backgroundColor.getRed();
        g[0] = (byte) backgroundColor.getGreen();
        b[0] = (byte) backgroundColor.getBlue();
        a[0] = (byte) backgroundColor.getAlpha();
        for (int i = 1; i < 128; i++) {
            if (j == 0) {
                r[i] = (byte) (2 * i);
                g[i] = (byte) 0;
            } else {
                r[i] = (byte) 255;
                g[i] = (byte) (255 - (2 * (i - 128)));
            }//from   w  w w.j ava  2  s. com
            b[i] = (byte) 0;
            a[i] = (byte) 255;
        }
        for (int i = 128; i < 256; i++) {
            if (j == 0) {
                r[i] = (byte) 255;
                g[i] = (byte) (2 * (i - 128));
            } else {
                r[i] = (byte) (255 - (2 * i));
                g[i] = (byte) 0;
            }
            b[i] = (byte) 0;
            a[i] = (byte) 255;
        }
        if (j == 0) {
            toggledColorModel = new IndexColorModel(8, palSize, r, g, b, a);
        } else {
            untoggledColorModel = new IndexColorModel(8, palSize, r, g, b, a);
        }

    }
}

From source file:org.apache.pdfbox.pdmodel.graphics.xobject.PDInlinedImage.java

/**
 * This will take the inlined image information and create a java.awt.Image from
 * it./*from   w  ww . ja  v  a2 s  .  co  m*/
 * 
 * @param colorSpaces The ColorSpace dictionary from the current resources, if any.
 *
 * @return The image that this object represents.
 *
 * @throws IOException If there is an error creating the image.
 */
public BufferedImage createImage(Map colorSpaces) throws IOException {
    /*
     * This was the previous implementation, not sure which is better right now.
     *         byte[] transparentColors = new byte[]{(byte)0xFF,(byte)0xFF};
    byte[] colors=new byte[]{0, (byte)0xFF};
    IndexColorModel colorModel = new IndexColorModel( 1, 2, colors, colors, colors, transparentColors );
    BufferedImage image = new BufferedImage(
    params.getWidth(),
    params.getHeight(),
    BufferedImage.TYPE_BYTE_BINARY,
    colorModel );
    DataBufferByte buffer = new DataBufferByte( getImageData(), 1 );
    WritableRaster raster =
    Raster.createPackedRaster(
        buffer,
        params.getWidth(),
        params.getHeight(),
        params.getBitsPerComponent(),
        new Point(0,0) );
    image.setData( raster );
    return image;
     */

    //verify again pci32.pdf before changing below
    PDColorSpace pcs = params.getColorSpace(colorSpaces);
    ColorModel colorModel;
    if (pcs != null) {
        colorModel = pcs.createColorModel(params.getBitsPerComponent());
    } else {
        byte[] transparentColors = new byte[] { (byte) 0xFF, (byte) 0xFF };
        byte[] colors = new byte[] { 0, (byte) 0xFF };
        colorModel = new IndexColorModel(1, 2, colors, colors, colors, transparentColors);
    }

    boolean invert = false;
    // maybe a decode array is defined
    COSBase dictObj = params.getDictionary().getDictionaryObject(COSName.DECODE, COSName.D);
    if (dictObj != null && dictObj instanceof COSArray) {
        COSArray decode = (COSArray) dictObj;
        if (decode.getInt(0) == 1) {
            if (params.getBitsPerComponent() == 1) {
                // [1.0, 0.0] -> invert the "color" values
                invert = true;
            } else {
                //TODO implement decode array for BPC > 1
                LOG.warn("decode array is not implemented for BPC > 1");
            }
        }
    }

    List filters = params.getFilters();
    byte[] finalData;
    if (filters == null || filters.isEmpty()) {
        finalData = getImageData();
    } else {
        ByteArrayInputStream in = new ByteArrayInputStream(getImageData());
        ByteArrayOutputStream out = new ByteArrayOutputStream(getImageData().length);
        FilterManager filterManager = new FilterManager();
        for (int i = 0; i < filters.size(); i++) {
            out.reset();
            Filter filter = filterManager.getFilter((String) filters.get(i));
            filter.decode(in, out, params.getDictionary(), i);
            in = new ByteArrayInputStream(out.toByteArray());
        }
        finalData = out.toByteArray();
    }

    WritableRaster raster = colorModel.createCompatibleWritableRaster(params.getWidth(), params.getHeight());
    /*    Raster.createPackedRaster(
        buffer,
        params.getWidth(),
        params.getHeight(),
        params.getBitsPerComponent(),
        new Point(0,0) );
        */
    DataBuffer rasterBuffer = raster.getDataBuffer();
    if (rasterBuffer instanceof DataBufferByte) {
        DataBufferByte byteBuffer = (DataBufferByte) rasterBuffer;
        byte[] data = byteBuffer.getData();
        System.arraycopy(finalData, 0, data, 0, data.length);
        if (invert) {
            invertBitmap(data);
        }
    } else if (rasterBuffer instanceof DataBufferInt) {
        DataBufferInt byteBuffer = (DataBufferInt) rasterBuffer;
        int[] data = byteBuffer.getData();
        for (int i = 0; i < finalData.length; i++) {
            data[i] = (finalData[i] + 256) % 256;
            if (invert) {
                data[i] = (~data[i] & 0xFF);
            }
        }
    }
    BufferedImage image = new BufferedImage(colorModel, raster, false, null);
    image.setData(raster);
    return image;
}

From source file:org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.java

/**
 * Returns a {@link java.awt.image.BufferedImage} of the COSStream
 * set in the constructor or null if the COSStream could not be encoded.
 *
 * @return {@inheritDoc}//from  w  ww  .j av  a 2 s  .  co  m
 *
 * @throws IOException {@inheritDoc}
 */
public BufferedImage getRGBImage() throws IOException {
    if (image != null) {
        return image;
    }

    try {
        int width = getWidth();
        int height = getHeight();
        int bpc = getBitsPerComponent();

        byte[] array = getPDStream().getByteArray();
        if (array.length == 0) {
            LOG.error("Something went wrong ... the pixelmap doesn't contain any data.");
            return null;
        }
        // Get the ColorModel right
        PDColorSpace colorspace = getColorSpace();
        if (colorspace == null) {
            LOG.error("getColorSpace() returned NULL.  Predictor = " + getPredictor());
            return null;
        }

        ColorModel cm = null;
        if (colorspace instanceof PDIndexed) {
            PDIndexed csIndexed = (PDIndexed) colorspace;
            // the base color space uses 8 bit per component, as the indexed color values
            // of an indexed color space are always in a range from 0 to 255
            ColorModel baseColorModel = csIndexed.getBaseColorSpace().createColorModel(8);
            // number of possible color values in the target color space
            int numberOfColorValues = 1 << bpc;
            // number of indexed color values
            int highValue = csIndexed.getHighValue();
            // choose the correct size, sometimes there are more indexed values than needed
            // and sometimes there are fewer indexed value than possible
            int size = Math.min(numberOfColorValues - 1, highValue);
            byte[] index = csIndexed.getLookupData();
            boolean hasAlpha = baseColorModel.hasAlpha();
            COSBase maskArray = getMask();
            if (baseColorModel.getTransferType() != DataBuffer.TYPE_BYTE) {
                throw new IOException("Not implemented");
            }
            // the IndexColorModel uses RGB-based color values
            // which leads to 3 color components and a optional alpha channel
            int numberOfComponents = 3 + (hasAlpha ? 1 : 0);
            int buffersize = (size + 1) * numberOfComponents;
            byte[] colorValues = new byte[buffersize];
            byte[] inData = new byte[baseColorModel.getNumComponents()];
            int bufferIndex = 0;
            for (int i = 0; i <= size; i++) {
                System.arraycopy(index, i * inData.length, inData, 0, inData.length);
                // convert the indexed color values to RGB 
                colorValues[bufferIndex] = (byte) baseColorModel.getRed(inData);
                colorValues[bufferIndex + 1] = (byte) baseColorModel.getGreen(inData);
                colorValues[bufferIndex + 2] = (byte) baseColorModel.getBlue(inData);
                if (hasAlpha) {
                    colorValues[bufferIndex + 3] = (byte) baseColorModel.getAlpha(inData);
                }
                bufferIndex += numberOfComponents;
            }
            if (maskArray != null && maskArray instanceof COSArray) {
                cm = new IndexColorModel(bpc, size + 1, colorValues, 0, hasAlpha,
                        ((COSArray) maskArray).getInt(0));
            } else {
                cm = new IndexColorModel(bpc, size + 1, colorValues, 0, hasAlpha);
            }
        } else if (colorspace instanceof PDSeparation) {
            PDSeparation csSeparation = (PDSeparation) colorspace;
            int numberOfComponents = csSeparation.getAlternateColorSpace().getNumberOfComponents();
            PDFunction tintTransformFunc = csSeparation.getTintTransform();
            COSArray decode = getDecode();
            // we have to invert the tint-values,
            // if the Decode array exists and consists of (1,0)
            boolean invert = decode != null && decode.getInt(0) == 1;
            // TODO add interpolation for other decode values then 1,0
            int maxValue = (int) Math.pow(2, bpc) - 1;
            // destination array
            byte[] mappedData = new byte[width * height * numberOfComponents];
            int rowLength = width * numberOfComponents;
            float[] input = new float[1];
            for (int i = 0; i < height; i++) {
                int rowOffset = i * rowLength;
                for (int j = 0; j < width; j++) {
                    // scale tint values to a range of 0...1
                    int value = (array[i * width + j] + 256) % 256;
                    if (invert) {
                        input[0] = 1 - (value / maxValue);
                    } else {
                        input[0] = value / maxValue;
                    }
                    float[] mappedColor = tintTransformFunc.eval(input);
                    int columnOffset = j * numberOfComponents;
                    for (int k = 0; k < numberOfComponents; k++) {
                        // redo scaling for every single color value 
                        float mappedValue = mappedColor[k];
                        mappedData[rowOffset + columnOffset + k] = (byte) (mappedValue * maxValue);
                    }
                }
            }
            array = mappedData;
            cm = colorspace.createColorModel(bpc);
        } else if (bpc == 1) {
            byte[] map = null;
            if (colorspace instanceof PDDeviceGray) {
                COSArray decode = getDecode();
                // we have to invert the b/w-values,
                // if the Decode array exists and consists of (1,0)
                if (decode != null && decode.getInt(0) == 1) {
                    map = new byte[] { (byte) 0xff };
                } else {
                    map = new byte[] { (byte) 0x00, (byte) 0xff };
                }
            } else if (colorspace instanceof PDICCBased) {
                if (((PDICCBased) colorspace).getNumberOfComponents() == 1) {
                    map = new byte[] { (byte) 0xff };
                } else {
                    map = new byte[] { (byte) 0x00, (byte) 0xff };
                }
            } else {
                map = new byte[] { (byte) 0x00, (byte) 0xff };
            }
            cm = new IndexColorModel(bpc, map.length, map, map, map, Transparency.OPAQUE);
        } else {
            if (colorspace instanceof PDICCBased) {
                if (((PDICCBased) colorspace).getNumberOfComponents() == 1) {
                    byte[] map = new byte[] { (byte) 0xff };
                    cm = new IndexColorModel(bpc, 1, map, map, map, Transparency.OPAQUE);
                } else {
                    cm = colorspace.createColorModel(bpc);
                }
            } else {
                cm = colorspace.createColorModel(bpc);
            }
        }

        LOG.debug("ColorModel: " + cm.toString());
        WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
        DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
        byte[] bufferData = buffer.getData();

        System.arraycopy(array, 0, bufferData, 0,
                (array.length < bufferData.length ? array.length : bufferData.length));
        image = new BufferedImage(cm, raster, false, null);

        // If there is a 'soft mask' image then we use that as a transparency mask.
        PDXObjectImage smask = getSMaskImage();
        if (smask != null) {
            BufferedImage smaskBI = smask.getRGBImage();

            COSArray decodeArray = smask.getDecode();

            CompositeImage compositeImage = new CompositeImage(image, smaskBI);
            BufferedImage rgbImage = compositeImage.createMaskedImage(decodeArray);

            return rgbImage;
        } else if (getImageMask()) {
            BufferedImage stencilMask = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D graphics = (Graphics2D) stencilMask.getGraphics();
            if (getStencilColor() != null) {
                graphics.setColor(getStencilColor().getJavaColor());
            } else {
                // this might happen when using ExractImages, see PDFBOX-1145
                LOG.debug("no stencil color for PixelMap found, using Color.BLACK instead.");
                graphics.setColor(Color.BLACK);
            }

            graphics.fillRect(0, 0, width, height);
            // assume default values ([0,1]) for the DecodeArray
            // TODO DecodeArray == [1,0]
            graphics.setComposite(AlphaComposite.DstIn);
            graphics.drawImage(image, null, 0, 0);
            return stencilMask;
        } else {
            // if there is no mask, use the unaltered image.
            return image;
        }
    } catch (Exception exception) {
        LOG.error(exception, exception);
        //A NULL return is caught in pagedrawer.Invoke.process() so don't re-throw.
        //Returning the NULL falls through to Phillip Koch's TODO section.
        return null;
    }
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGImageDecoder.java

private void parse_IEND_chunk(final PNGChunk chunk) {
    // Store text strings
    final int textLen = this.textKeys.size();
    final String[] textArray = new String[2 * textLen];
    for (int i = 0; i < textLen; ++i) {
        final String key = this.textKeys.get(i);
        final String val = this.textStrings.get(i);
        textArray[2 * i] = key;//from   ww w . jav a2s  . c  o m
        textArray[2 * i + 1] = val;
        if (this.emitProperties) {
            final String uniqueKey = "text_" + i + ':' + key;
            this.properties.put(uniqueKey.toLowerCase(), val);
        }
    }
    if (this.encodeParam != null) {
        this.encodeParam.setText(textArray);
    }

    // Store compressed text strings
    final int ztextLen = this.ztextKeys.size();
    final String[] ztextArray = new String[2 * ztextLen];
    for (int i = 0; i < ztextLen; ++i) {
        final String key = this.ztextKeys.get(i);
        final String val = this.ztextStrings.get(i);
        ztextArray[2 * i] = key;
        ztextArray[2 * i + 1] = val;
        if (this.emitProperties) {
            final String uniqueKey = "ztext_" + i + ':' + key;
            this.properties.put(uniqueKey.toLowerCase(), val);
        }
    }
    if (this.encodeParam != null) {
        this.encodeParam.setCompressedText(ztextArray);
    }

    // Parse prior IDAT chunks
    final InputStream seqStream = new SequenceInputStream(Collections.enumeration(this.streamVec));
    final InputStream infStream = new InflaterInputStream(seqStream, new Inflater());
    this.dataStream = new DataInputStream(infStream);

    // Create an empty WritableRaster
    int depth = this.bitDepth;
    if (this.colorType == PNG_COLOR_GRAY && this.bitDepth < 8 && this.output8BitGray) {
        depth = 8;
    }
    if (this.colorType == PNG_COLOR_PALETTE && this.expandPalette) {
        depth = 8;
    }
    final int bytesPerRow = (this.outputBands * this.width * depth + 7) / 8;
    final int scanlineStride = depth == 16 ? bytesPerRow / 2 : bytesPerRow;

    this.theTile = createRaster(this.width, this.height, this.outputBands, scanlineStride, depth);

    if (this.performGammaCorrection && this.gammaLut == null) {
        initGammaLut(this.bitDepth);
    }
    if (this.postProcess == POST_GRAY_LUT || this.postProcess == POST_GRAY_LUT_ADD_TRANS
            || this.postProcess == POST_GRAY_LUT_ADD_TRANS_EXP) {
        initGrayLut(this.bitDepth);
    }

    decodeImage(this.interlaceMethod == 1);
    this.sampleModel = this.theTile.getSampleModel();

    if (this.colorType == PNG_COLOR_PALETTE && !this.expandPalette) {
        if (this.outputHasAlphaPalette) {
            this.colorModel = new IndexColorModel(this.bitDepth, this.paletteEntries, this.redPalette,
                    this.greenPalette, this.bluePalette, this.alphaPalette);
        } else {
            this.colorModel = new IndexColorModel(this.bitDepth, this.paletteEntries, this.redPalette,
                    this.greenPalette, this.bluePalette);
        }
    } else if (this.colorType == PNG_COLOR_GRAY && this.bitDepth < 8 && !this.output8BitGray) {
        final byte[] palette = this.expandBits[this.bitDepth];
        this.colorModel = new IndexColorModel(this.bitDepth, palette.length, palette, palette, palette);
    } else {
        this.colorModel = createComponentColorModel(this.sampleModel);
    }
}