} else if(compare(head, new byte[]{8, 0, 0, 0, 0})) {
mode = PNGEncoder.GREYSCALE_MODE;
} else if(compare(head, new byte[]{8, 2, 0, 0, 0})) {
mode = PNGEncoder.COLOR_MODE;
} else {
throw(new JemmyException("Format error"));
}
readInt();//!!crc
int size = readInt();
byte[] idat = read(4);
checkEquality(idat, "IDAT".getBytes());
byte[] data = read(size);
Inflater inflater = new Inflater();
inflater.setInput(data, 0, size);
int color;
try {
switch (mode) {
case PNGEncoder.BW_MODE:
{
int bytes = (int)(width / 8);
if((width % 8) != 0) {
bytes++;
}
byte colorset;
byte[] row = new byte[bytes];
for (int y = 0; y < height; y++) {
inflater.inflate(new byte[1]);
inflater.inflate(row);
for (int x = 0; x < bytes; x++) {
colorset = row[x];
for (int sh = 0; sh < 8; sh++) {
if(x * 8 + sh >= width) {
break;
}
if((colorset & 0x80) == 0x80) {
result.setRGB(x * 8 + sh, y, Color.white.getRGB());
} else {
result.setRGB(x * 8 + sh, y, Color.black.getRGB());
}
colorset <<= 1;
}
}
}
}
break;
case PNGEncoder.GREYSCALE_MODE:
{
byte[] row = new byte[width];
for (int y = 0; y < height; y++) {
inflater.inflate(new byte[1]);
inflater.inflate(row);
for (int x = 0; x < width; x++) {
color = row[x];
result.setRGB(x, y, (color << 16) + (color << 8) + color);
}
}
}
break;
case PNGEncoder.COLOR_MODE:
{
byte[] row = new byte[width * 3];
for (int y = 0; y < height; y++) {
inflater.inflate(new byte[1]);
inflater.inflate(row);
for (int x = 0; x < width; x++) {
result.setRGB(x, y,
((row[x * 3 + 0]&0xff) << 16) +
((row[x * 3 + 1]&0xff) << 8) +
((row[x * 3 + 2]&0xff)));
}
}
}
}
} catch(DataFormatException e) {
throw(new JemmyException("ZIP error", e));
}
readInt();//!!crc
readInt();//0