boolean hasAlpha = false;
if (gce != null && gce.transparency) {
hasAlpha = true;
}
final ImageBuilder imageBuilder = new ImageBuilder(width, height, hasAlpha);
int[] colorTable;
if (id.localColorTable != null) {
colorTable = getColorTable(id.localColorTable);
} else if (imageContents.globalColorTable != null) {
colorTable = getColorTable(imageContents.globalColorTable);
} else {
throw new ImageReadException("Gif: No Color Table");
}
int transparentIndex = -1;
if (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++) {
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)) {
y = 1 + (theRow * 2);
} else {
throw new ImageReadException("Gif: Strange Row");
}
}
}
}
} else {
y = row;
}
for (int x = 0; x < width; x++) {
final int index = 0xff & id.imageData[counter++];
int rgb = colorTable[index];
if (transparentIndex == index) {
rgb = 0x00;
}
imageBuilder.setRGB(x, y, rgb);
}
}
return imageBuilder.getBufferedImage();
}