} else if(Arrays.equals(head, new byte[]{8, 0, 0, 0, 0})) {
mode = PNGSaver.GREYSCALE_MODE;
} else if(Arrays.equals(head, new byte[]{8, 2, 0, 0, 0})) {
mode = PNGSaver.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[] colors = new int[3];
int[] black = new int[] {0, 0, 0};
int[] white = new int[] {1, 1, 1};
try {
switch (mode) {
case PNGSaver.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) {
setColors(result, x * 8 + sh, y, white);
} else {
setColors(result, x * 8 + sh, y, black);
}
colorset <<= 1;
}
}
}
}
break;
case PNGSaver.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++) {
colors[0] = row[x];
colors[1] = colors[0];
colors[2] = colors[0];
setColors(result, x, y, colors);
}
}
}
break;
case PNGSaver.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++) {
colors[0] = (row[x * 3 + 0]&0xff);
colors[1] = (row[x * 3 + 1]&0xff);
colors[2] = (row[x * 3 + 2]&0xff);
setColors(result, x, y, colors);
}
}
}
}
} catch(DataFormatException e) {
throw(new JemmyException("ZIP error", e));
}
readInt();//!!crc
readInt();//0