private void refillInflater(Inflater inflater) { while (chunkRemaining == 0) { closeChunk(); openChunk(IDAT); } int read = readChunk(buffer, 0, buffer.Length); inflater.SetInput(buffer, 0, read); }
private void readChunkUnzip(Inflater inflater, byte[] buffer, int offset, int length) { try { do { int read = inflater.Inflate(buffer, offset, length); if (read <= 0) { if (inflater.Finished()) { throw new EOFException(); } if (inflater.NeedsInput()) { refillInflater(inflater); } else { throw new IOException("Can't inflate " + length + " bytes"); } } else { offset += read; length -= read; } } while (length > 0); } catch (DataFormatException ex) { throw (IOException)(new IOException("inflate error").InitCause(ex)); } }
public void decode(ByteBuffer buffer, int stride, Format fmt) { int offset = buffer.Position(); int lineSize = ((width * bitdepth + 7) / 8) * bytesPerPixel; byte[] curLine = new byte[lineSize + 1]; byte[] prevLine = new byte[lineSize + 1]; byte[] palLine = (bitdepth < 8) ? new byte[width + 1] : null; Inflater inflater = new Inflater(); try { for (int y = 0; y < height; y++) { readChunkUnzip(inflater, curLine, 0, curLine.Length); unfilter(curLine, prevLine); buffer.Position(offset + y * stride); switch (colorType) { case COLOR_TRUECOLOR: switch (fmt) { case Format.ABGR: copyRGBtoABGR(buffer, curLine); break; case Format.RGBA: copyRGBtoRGBA(buffer, curLine); break; case Format.BGRA: copyRGBtoBGRA(buffer, curLine); break; case Format.RGB: copy(buffer, curLine); break; default: throw new UnsupportedOperationException("Unsupported format for this image"); } break; case COLOR_TRUEALPHA: switch (fmt) { case Format.ABGR: copyRGBAtoABGR(buffer, curLine); break; case Format.RGBA: copy(buffer, curLine); break; case Format.BGRA: copyRGBAtoBGRA(buffer, curLine); break; case Format.RGB: copyRGBAtoRGB(buffer, curLine); break; default: throw new UnsupportedOperationException("Unsupported format for this image"); } break; case COLOR_GREYSCALE: switch (fmt) { case Format.LUMINANCE: case Format.ALPHA: copy(buffer, curLine); break; default: throw new UnsupportedOperationException("Unsupported format for this image"); } break; case COLOR_GREYALPHA: switch (fmt) { case Format.LUMINANCE_ALPHA: copy(buffer, curLine); break; default: throw new UnsupportedOperationException("Unsupported format for this image"); } break; case COLOR_INDEXED: switch (bitdepth) { case 8: palLine = curLine; break; case 4: expand4(curLine, palLine); break; case 2: expand2(curLine, palLine); break; case 1: expand1(curLine, palLine); break; default: throw new UnsupportedOperationException("Unsupported bitdepth for this image"); } switch (fmt) { case Format.ABGR: copyPALtoABGR(buffer, palLine); break; case Format.RGBA: copyPALtoRGBA(buffer, palLine); break; case Format.BGRA: copyPALtoBGRA(buffer, palLine); break; default: throw new UnsupportedOperationException("Unsupported format for this image"); } break; default: throw new UnsupportedOperationException("Not yet implemented"); } byte[] tmp = curLine; curLine = prevLine; prevLine = tmp; } } finally { inflater.End(); } }