Skip to content

Commit

Permalink
Be more lenient
Browse files Browse the repository at this point in the history
  • Loading branch information
CGDogan authored Jul 31, 2023
1 parent f1bed8a commit 5a9e62a
Showing 1 changed file with 8 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -424,43 +424,17 @@ private void parseSOF() throws IOException {
throw new IOException("Only exactly 3 channels supported by this reader");
}

boolean gotY = false, gotCb = false, gotCr = false;
// Default to 1*1 (8*8 MCU)
int samplingFactor = 0x22;
// 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;

switch(componentId) {
case 1:
if (gotY) {
throw new IOException("Got multiple Y channels");
}
gotY = true;

// Sampling factors https://groups.google.com/g/comp.compression/c/Q8FUSocL7nA
// https://stackoverflow.com/questions/27918757/interpreting-jpeg-chroma-subsampling-read-from-file
samplingFactor = in.readByte() & 0xff;
int quantTableNumberY = in.readByte() & 0xff;
break;

case 2:
case 3:
if (componentId == 2) {
if (gotCb) throw new IOException("Got multiple Cb channels");
gotCb = true;
} else {
if (gotCr) throw new IOException("Got multiple Cr channels");
gotCr = true;
}

int samplingFactorByteCbOrCr = in.readByte() & 0xff;
int quantTableNumberCbOrCr = in.readByte() & 0xff;
break;

default:
throw new IOException("Only YCbCr supported by this reader");
}
// 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;
Expand Down

0 comments on commit 5a9e62a

Please sign in to comment.