Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ final class RgbeInfo implements Closeable {
private static final Pattern RESOLUTION_STRING = Pattern.compile("-Y (\\d+) \\+X (\\d+)");
private static final byte[] TWO_TWO = { 0x2, 0x2 };

private static int readByte(final InputStream in) throws IOException {
final int b = in.read();
if (b < 0) {
throw new ImagingException("Error decompressing RGBE file: unexpected end of the scan line data");
}
return b;
}

private static void decompress(final InputStream in, final byte[] out) throws IOException, ImagingException {
int position = 0;
final int total = out.length;
Expand All @@ -47,14 +55,18 @@ private static void decompress(final InputStream in, final byte[] out) throws IO
if (n < 0) {
throw new ImagingException("Error decompressing RGBE file");
}
final int count = n > 128 ? n & 0x7f : n;
if (count > total - position) {
throw new ImagingException("Error decompressing RGBE file: run of " + count + " exceeds the scan line");
}
if (n > 128) {
final int value = in.read();
for (int i = 0; i < (n & 0x7f); i++) {
final int value = readByte(in);
for (int i = 0; i < count; i++) {
out[position++] = (byte) value;
}
} else {
for (int i = 0; i < n; i++) {
out[position++] = (byte) in.read();
for (int i = 0; i < count; i++) {
out[position++] = (byte) readByte(in);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.awt.Dimension;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.apache.commons.imaging.ImageFormat;
import org.apache.commons.imaging.ImageFormats;
Expand Down Expand Up @@ -109,4 +110,26 @@ public void testRgbeImagingParameters() {
final RgbeImagingParameters params = new RgbeImagingParameters();
assertNotNull(params);
}

private static byte[] hdr(final byte[] scanLine) {
final byte[] header = "#?RADIANCE\nFORMAT=32-bit_rle_rgbe\n\n-Y 1 +X 4\n".getBytes(StandardCharsets.US_ASCII);
final byte[] bytes = Arrays.copyOf(header, header.length + scanLine.length);
System.arraycopy(scanLine, 0, bytes, header.length, scanLine.length);
return bytes;
}

/**
* Tests that a scan line cut off inside a literal run, or with a run longer than the scan line, is reported.
*/
@Test
void testTruncatedAndOverlongRuns() throws IOException {
final RgbeImageParser parser = new RgbeImageParser();
// 4 pixels, 4 channels, each channel one literal run of 4 bytes
final byte[] scanLine = { 2, 2, 0, 4, 4, 1, 2, 3, 4, 4, 1, 2, 3, 4, 4, 1, 2, 3, 4, 4, (byte) 0x81, (byte) 0x81, (byte) 0x81, (byte) 0x81 };
assertNotNull(parser.getBufferedImage(ByteSource.array(hdr(scanLine)), new RgbeImagingParameters()));
final byte[] truncated = hdr(Arrays.copyOf(scanLine, scanLine.length - 2));
assertThrows(ImagingException.class, () -> parser.getBufferedImage(ByteSource.array(truncated), new RgbeImagingParameters()));
final byte[] overlong = hdr(new byte[] { 2, 2, 0, 4, (byte) (0x80 | 20), 1 });
assertThrows(ImagingException.class, () -> parser.getBufferedImage(ByteSource.array(overlong), new RgbeImagingParameters()));
}
}