@Override public void close() throws IOException { assertImageDataAtEnd(); checkAdler(); }
void readNextBlockHeader() throws IOException { isLastBlock = stream.getNextIdatBit() != 0; compressionType = (byte) stream.getNextIdatBits(2); if (compressionType > 2) stream.error(); if (compressionType == UNCOMPRESSED) { byte b1 = stream.getNextIdatByte(); byte b2 = stream.getNextIdatByte(); byte b3 = stream.getNextIdatByte(); byte b4 = stream.getNextIdatByte(); if (b1 != ~b3 || b2 != ~b4) stream.error(); uncompressedBytesRemaining = (b1 & 0xFF) | ((b2 & 0xFF) << 8); } else if (compressionType == COMPRESSED_DYNAMIC) { huffmanTables = PngHuffmanTables.getDynamicTables(stream); } else { huffmanTables = PngHuffmanTables.getFixedTables(); } }
@Override public int read(byte[] buffer, int off, int len) throws IOException { for (int i = 0; i < len; i++) { int b = read(); if (b == -1) return i; buffer[off + i] = (byte)b; } return len; }
void checkAdler() throws IOException { int storedAdler = ((getNextIdatByte() & 0xFF) << 24) | ((getNextIdatByte() & 0xFF) << 16) | ((getNextIdatByte() & 0xFF) << 8) | (getNextIdatByte() & 0xFF); if (storedAdler != adlerValue) error(); }
int getNextValue(PngDecodingDataStream stream) throws IOException { int code = stream.getNextIdatBit(); int codelength = 0; // Here we are taking advantage of the fact that 1 bits are used as // a prefix to the longer codeValues. while (codelength < MAX_CODE_LENGTH && code > codeLengthInfo[codelength].max) { code = ((code << 1) | stream.getNextIdatBit()); codelength++; } if (codelength >= MAX_CODE_LENGTH) stream.error(); // Now we have a Huffman code of length (codelength + 1) that // is somewhere in the range // minCodesByLength[codelength]..maxCodesByLength[codelength]. // This code is the (offset + 1)'th code of (codelength + 1); int offset = code - codeLengthInfo[codelength].min; // indexesByLength[codelength] is the first code of length (codelength + 1) // so now we can look up the value for the Huffman code in the table. int index = codeLengthInfo[codelength].baseIndex + offset; return codeValues[index]; }
} else if (value == 16) { int count = stream.getNextIdatBits(2) + 3; for (int i = 0; i < count; i++) { lengths[index] = lengths [index - 1]; int count = stream.getNextIdatBits(3) + 3; for (int i = 0; i < count; i++) { lengths[index] = 0; int count = stream.getNextIdatBits(7) + 11; for (int i = 0; i < count; i++) { lengths[index] = 0; stream.error();
int getNextIdatBit() throws IOException { if (nextBitIndex > MAX_BIT) { currentByte = getNextIdatByte(); nextBitIndex = 0; } return (currentByte & (1 << nextBitIndex)) >> nextBitIndex++; }
PngDecodingDataStream(InputStream stream) throws IOException { super(); this.stream = stream; nextBitIndex = MAX_BIT + 1; adlerValue = 1; lzBlockReader = new PngLzBlockReader(this); readCompressedDataHeader(); lzBlockReader.readNextBlockHeader(); }
private void assertBlockAtEnd() throws IOException { if (compressionType == UNCOMPRESSED) { if (uncompressedBytesRemaining > 0) stream.error(); } else if (copyBytesRemaining > 0 || (huffmanTables.getNextLiteralValue(stream) != END_OF_COMPRESSED_BLOCK)) { stream.error(); } } void assertCompressedDataAtEnd() throws IOException {
int getNextIdatBits(int length) throws IOException { int value = 0; for (int i = 0; i < length; i++) { value |= (getNextIdatBit() << i); } return value; }
@Override public int read() throws IOException { byte nextDecodedByte = lzBlockReader.getNextByte(); updateAdler(nextDecodedByte); return nextDecodedByte & 0xFF; }
/** * Read the image data from the data stream. This must handle * decoding the data, filtering, and interlacing. */ @SuppressWarnings("resource") void readPixelData(PngIdatChunk chunk, PngChunkReader chunkReader) throws IOException { InputStream stream = new PngInputStream(chunk, chunkReader); //TEMPORARY CODE boolean use3_2 = System.getProperty("org.eclipse.swt.internal.image.PNGFileFormat_3.2") != null; InputStream inflaterStream = use3_2 ? null : new BufferedInputStream(new InflaterInputStream(stream)); if (inflaterStream != null) { stream = inflaterStream; } else { stream = new PngDecodingDataStream(stream); } int interlaceMethod = headerChunk.getInterlaceMethod(); if (interlaceMethod == PngIhdrChunk.INTERLACE_METHOD_NONE) { readNonInterlacedImage(stream); } else { readInterlacedImage(stream); } /* * InflaterInputStream does not consume all bytes in the stream * when it is closed. This may leave unread IDAT chunks. The fix * is to read all available bytes before closing it. */ while (stream.available() > 0) stream.read(); stream.close(); } /**
} else if (value == 16) { int count = stream.getNextIdatBits(2) + 3; for (int i = 0; i < count; i++) { lengths[index] = lengths [index - 1]; int count = stream.getNextIdatBits(3) + 3; for (int i = 0; i < count; i++) { lengths[index] = 0; int count = stream.getNextIdatBits(7) + 11; for (int i = 0; i < count; i++) { lengths[index] = 0; stream.error();
void checkAdler() throws IOException { int storedAdler = ((getNextIdatByte() & 0xFF) << 24) | ((getNextIdatByte() & 0xFF) << 16) | ((getNextIdatByte() & 0xFF) << 8) | (getNextIdatByte() & 0xFF); if (storedAdler != adlerValue) error(); }
int getNextValue(PngDecodingDataStream stream) throws IOException { int code = stream.getNextIdatBit(); int codelength = 0; // Here we are taking advantage of the fact that 1 bits are used as // a prefix to the longer codeValues. while (codelength < MAX_CODE_LENGTH && code > codeLengthInfo[codelength].max) { code = ((code << 1) | stream.getNextIdatBit()); codelength++; } if (codelength >= MAX_CODE_LENGTH) stream.error(); // Now we have a Huffman code of length (codelength + 1) that // is somewhere in the range // minCodesByLength[codelength]..maxCodesByLength[codelength]. // This code is the (offset + 1)'th code of (codelength + 1); int offset = code - codeLengthInfo[codelength].min; // indexesByLength[codelength] is the first code of length (codelength + 1) // so now we can look up the value for the Huffman code in the table. int index = codeLengthInfo[codelength].baseIndex + offset; return codeValues[index]; }
int getNextIdatBit() throws IOException { if (nextBitIndex > MAX_BIT) { currentByte = getNextIdatByte(); nextBitIndex = 0; } return (currentByte & (1 << nextBitIndex)) >> nextBitIndex++; }
PngDecodingDataStream(InputStream stream) throws IOException { super(); this.stream = stream; nextBitIndex = MAX_BIT + 1; adlerValue = 1; lzBlockReader = new PngLzBlockReader(this); readCompressedDataHeader(); lzBlockReader.readNextBlockHeader(); }
private void assertBlockAtEnd() throws IOException { if (compressionType == UNCOMPRESSED) { if (uncompressedBytesRemaining > 0) stream.error(); } else if (copyBytesRemaining > 0 || (huffmanTables.getNextLiteralValue(stream) != END_OF_COMPRESSED_BLOCK)) { stream.error(); } } void assertCompressedDataAtEnd() throws IOException {
int getNextIdatBits(int length) throws IOException { int value = 0; for (int i = 0; i < length; i++) { value |= (getNextIdatBit() << i); } return value; }
@Override public int read() throws IOException { byte nextDecodedByte = lzBlockReader.getNextByte(); updateAdler(nextDecodedByte); return nextDecodedByte & 0xFF; }