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

(cherry picked from commit 8a240aac684b16444a98b67f67070dfc30b3d621)
This commit is contained in:
Harald Kuhr 2022-10-20 13:57:11 +02:00
parent 4cc53d822f
commit 36c91f67e4
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)? // TODO: Consider creating an ImageInputStream wrapper with the WebP implementation of readBit(s)?
private final ImageInputStream imageInput; private final ImageInputStream imageInput;
int bitOffset = 64; private int bitOffset = 64;
long streamPosition = -1; private long streamPosition = -1;
/** /**
* Pre-buffers up to the next 8 Bytes in input. * Pre-buffers up to the next 8 Bytes in input.
@ -124,13 +124,13 @@ public final class LSBBitReader {
private void refillBuffer() throws IOException { private void refillBuffer() throws IOException {
// Set to stream position consistent with buffered bytes // 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) { for (; bitOffset >= 8; bitOffset -= 8) {
try { try {
byte b = imageInput.readByte(); byte b = imageInput.readByte();
buffer >>>= 8; buffer = ((long) b << 56) | buffer >>> 8;
streamPosition++; streamPosition++;
buffer |= ((long) b << 56);
} }
catch (EOFException e) { catch (EOFException e) {
imageInput.seek(streamPosition); imageInput.seek(streamPosition);

View File

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

View File

@ -37,11 +37,11 @@ import java.awt.image.*;
* @author Simon Kammermeier * @author Simon Kammermeier
*/ */
final class HuffmanInfo { 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) { public HuffmanInfo(Raster huffmanMetaCodes, int metaCodeBits, HuffmanCodeGroup[] huffmanGroups) {
this.huffmanMetaCodes = huffmanMetaCodes; this.huffmanMetaCodes = huffmanMetaCodes;