#704 Fix LSBBitReader to avoid back/forth seeking that invalidates buffer

This commit is contained in:
Harald Kuhr 2022-10-20 13:57:11 +02:00
parent 61424f33b6
commit 8a240aac68
3 changed files with 8 additions and 11 deletions

View File

@ -47,8 +47,8 @@ public final class LSBBitReader {
// TODO: Consider creating an ImageInputStream wrapper with the WebP implementation of readBit(s)?
private final ImageInputStream imageInput;
int bitOffset = 64;
long streamPosition = -1;
private int bitOffset = 64;
private long streamPosition = -1;
/**
* Pre-buffers up to the next 8 Bytes in input.
@ -124,13 +124,13 @@ public final class LSBBitReader {
private void refillBuffer() throws IOException {
// Set to stream position consistent with buffered bytes
imageInput.seek(streamPosition + 8);
imageInput.readLong(); // Don't replace with skipBytes(8) or seek(+8), this will invalidate stream buffer... TODO: Fix streams to cope...
for (; bitOffset >= 8; bitOffset -= 8) {
try {
byte b = imageInput.readByte();
buffer >>>= 8;
buffer = ((long) b << 56) | buffer >>> 8;
streamPosition++;
buffer |= ((long) b << 56);
}
catch (EOFException e) {
imageInput.seek(streamPosition);

View File

@ -48,7 +48,6 @@ final class ColorIndexingTransform implements Transform {
@Override
public void applyInverse(WritableRaster raster) {
int width = raster.getWidth();
int height = raster.getHeight();
@ -57,7 +56,6 @@ final class ColorIndexingTransform implements Transform {
for (int y = 0; y < height; y++) {
// Reversed so no used elements are overridden (in case of packing)
for (int x = width - 1; x >= 0; x--) {
int componentSize = 8 >> bits;
int packed = 1 << bits;
int xC = x / packed;
@ -70,7 +68,6 @@ final class ColorIndexingTransform implements Transform {
// Arraycopy for 4 elements might not be beneficial
System.arraycopy(colorTable, index * 4, rgba, 0, 4);
raster.setDataElements(x, y, rgba);
}
}
}

View File

@ -37,11 +37,11 @@ import java.awt.image.*;
* @author Simon Kammermeier
*/
final class HuffmanInfo {
public Raster huffmanMetaCodes; // Raster allows intuitive lookup by x and y
public final Raster huffmanMetaCodes; // Raster allows intuitive lookup by x and y
public int metaCodeBits;
public final int metaCodeBits;
public HuffmanCodeGroup[] huffmanGroups;
public final HuffmanCodeGroup[] huffmanGroups;
public HuffmanInfo(Raster huffmanMetaCodes, int metaCodeBits, HuffmanCodeGroup[] huffmanGroups) {
this.huffmanMetaCodes = huffmanMetaCodes;