Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fail on non-4:4:4 JPEG #4061

Merged
merged 8 commits into from
Jan 31, 2024
Merged
Changes from 3 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
CGDogan marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public class JPEGTurboServiceImpl implements JPEGTurboService {
private long sos;
private long imageDimensions;

private int mcuWidth;
private int mcuHeight;
private int tileWidth;
private int tileHeight;
private int xTiles;
Expand Down Expand Up @@ -156,6 +158,10 @@ public void initialize(RandomAccessInputStream jpeg, int width, int height)
}
else if (marker == SOF0) {
imageDimensions = in.getFilePointer() + 1;
parseSOF();
}
else if (marker > 0xFFC0 && marker < 0xFFD0 && marker % 4 != 0) {
throw new IOException("Unsupported JPEG SOF marker: " + marker);
}
else if (marker == SOS) {
sos = end;
Expand All @@ -180,6 +186,12 @@ else if (marker == SOS) {
}
}

if (mcuWidth == 0) {
LOGGER.warn("assume MCU width and height 8");
mcuWidth = 8;
mcuHeight = 8;
}

if (restartMarkers.size() == 1) {
in.seek(restartMarkers.get(0));

Expand All @@ -206,7 +218,7 @@ else if (marker == SOS) {
}
}

tileWidth = restartInterval * 8;
tileWidth = restartInterval * mcuWidth;
tileHeight = (int) Math.min(tileWidth, 512);

xTiles = imageWidth / tileWidth;
Expand Down Expand Up @@ -289,9 +301,9 @@ public byte[] getTile(int tileX, int tileY) throws IOException {

long dataLength = header.length + 2;

int mult = tileHeight / 8; // was restartInterval
int mult = tileHeight / mcuHeight; // was restartInterval
int start = tileX + (tileY * xTiles * mult);
for (int row=0; row<tileHeight/8; row++) {
for (int row=0; row<tileHeight/mcuHeight; row++) {
int end = start + 1;

long startOffset = restartMarkers.get(start);
Expand All @@ -314,7 +326,7 @@ public byte[] getTile(int tileX, int tileY) throws IOException {
offset += header.length;

start = tileX + (tileY * xTiles * mult);
for (int row=0; row<tileHeight/8; row++) {
for (int row=0; row<tileHeight/mcuHeight; row++) {
int end = start + 1;

long endOffset = in.length();
Expand Down Expand Up @@ -374,6 +386,8 @@ public void close() throws IOException {
restartInterval = 1;
sos = 0;
imageDimensions = 0;
mcuWidth = 0;
mcuHeight = 0;
tileWidth = 0;
tileHeight = 0;
xTiles = 0;
Expand All @@ -396,4 +410,49 @@ private byte[] getFixedHeader() throws IOException {
return header;
}

private void parseSOF() throws IOException {
// https://mykb.cipindanci.com/archive/SuperKB/1294/JPEG%20File%20Layout%20and%20Format.htm
// example: FFC00011 08001100 11030122 00021101 031101
int bpc = in.readByte() & 0xff;
if (bpc != 8) {
throw new IOException("Only 8-bit channels supported by this reader");
}
in.skipBytes(4);
int channels = in.readByte() & 0xff;
if (channels != 3) {
// https://stackoverflow.com/questions/51008883/is-there-a-grayscale-jpg-format
throw new IOException("Only exactly 3 channels supported by this reader");
}

// Sampling factors: https://groups.google.com/g/comp.compression/c/Q8FUSocL7nA
// https://stackoverflow.com/questions/27918757/interpreting-jpeg-chroma-subsampling-read-from-file
// In our code, assume that Y is the largest, and others are 0x11.
// Default to 1*1 (8*8 MCU), where Y=0x11
int samplingFactor = 0x11;

for (int i = 0; i < channels; i++) {
int componentId = in.readByte() & 0xff;
// The first one is Y, then the others, but use a simpler logic here
samplingFactor = Math.max(samplingFactor, in.readByte() & 0xff);
int quantTableNumber = in.readByte() & 0xff;
}

int samplingVertical = samplingFactor & 0x0f;
int samplingHorizontal = (samplingFactor & 0xf0) >> 4;
if (samplingVertical == 0x01) {
mcuHeight = 8;
} else if (samplingVertical == 0x02) {
mcuHeight = 16;
} else {
throw new IOException("Unsupported vertical sampling: " + samplingVertical);
}
if (samplingHorizontal == 0x01) {
mcuWidth = 8;
} else if (samplingHorizontal == 0x02) {
mcuWidth = 16;
} else {
throw new IOException("Unsupported horizontal sampling: " + samplingHorizontal);
}
}

}