private BufferedImage getBufferedImage()

in src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java [249:332]


    private BufferedImage getBufferedImage(final GifImageData imageData, final byte[] globalColorTable)
            throws ImagingException {
        final ImageDescriptor id = imageData.descriptor;
        final GraphicControlExtension gce = imageData.gce;

        final int width = id.imageWidth;
        final int height = id.imageHeight;

        boolean hasAlpha = false;
        if (gce != null && gce.transparency) {
            hasAlpha = true;
        }

        final ImageBuilder imageBuilder = new ImageBuilder(width, height, hasAlpha);

        final int[] colorTable;
        if (id.localColorTable != null) {
            colorTable = getColorTable(id.localColorTable);
        } else if (globalColorTable != null) {
            colorTable = getColorTable(globalColorTable);
        } else {
            throw new ImagingException("Gif: No Color Table");
        }

        int transparentIndex = -1;
        if (gce != null && hasAlpha) {
            transparentIndex = gce.transparentColorIndex;
        }

        int counter = 0;

        final int rowsInPass1 = (height + 7) / 8;
        final int rowsInPass2 = (height + 3) / 8;
        final int rowsInPass3 = (height + 1) / 4;
        final int rowsInPass4 = height / 2;

        for (int row = 0; row < height; row++) {
            final int y;
            if (id.interlaceFlag) {
                int theRow = row;
                if (theRow < rowsInPass1) {
                    y = theRow * 8;
                } else {
                    theRow -= rowsInPass1;
                    if (theRow < rowsInPass2) {
                        y = 4 + theRow * 8;
                    } else {
                        theRow -= rowsInPass2;
                        if (theRow < rowsInPass3) {
                            y = 2 + theRow * 4;
                        } else {
                            theRow -= rowsInPass3;
                            if (theRow >= rowsInPass4) {
                                throw new ImagingException("Gif: Strange Row");
                            }
                            y = 1 + theRow * 2;
                        }
                    }
                }
            } else {
                y = row;
            }

            for (int x = 0; x < width; x++) {
                if (counter >= id.imageData.length) {
                    throw new ImagingException(
                            String.format("Invalid GIF image data length [%d], greater than the image data length [%d]", id.imageData.length, width));
                }
                final int index = 0xff & id.imageData[counter++];
                if (index >= colorTable.length) {
                    throw new ImagingException(
                            String.format("Invalid GIF color table index [%d], greater than the color table length [%d]", index, colorTable.length));
                }
                int rgb = colorTable[index];

                if (transparentIndex == index) {
                    rgb = 0x00;
                }
                imageBuilder.setRgb(x, y, rgb);
            }
        }

        return imageBuilder.getBufferedImage();
    }