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 5 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 Down Expand Up @@ -206,7 +212,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 +295,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 +320,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 +380,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 +404,53 @@ 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 images with 3 channels are supported by this reader");
}

// Sampling factors: https://groups.google.com/g/comp.compression/c/Q8FUSocL7nA
// https://stackoverflow.com/q/27918757 https://stackoverflow.com/q/43225439
// mcu_tool.sh shows MCU size for some images
// JpegSnoop or identify -verbose show subsampling rates
// convert -sampling rate 2x2 or ffmpeg -i .. -vf format=yuv420p for making images

// MCU size in X direction divided by 8 is the ratio of largest X to smallest X
// likewise for Y. The first 4 bits is X, the next 4 bits make up Y.
// some examples: 2x1,2x1,2x1 (0x21,0x21,0x21) is 8x8. because 2/2 = 1, 1/1 = 1
// 2x1,1x1,1x1 is 16x8. 3x2,1x1,1x1 is 24x16

// No need to shift now
int maxX = 0x00;
int minX = 0xf0;
int maxY = 0x00;
int minY = 0x0f;

for (int i = 0; i < channels; i++) {
int componentId = in.readByte() & 0xff;

int rates = in.readByte();
int X = rates & 0xf0;
int Y = rates & 0x0f;
maxX = Math.max(maxX, X);
minX = Math.min(minX, X);
maxY = Math.max(maxY, Y);
minY = Math.min(minY, Y);

int quantTableNumber = in.readByte() & 0xff;
}

mcuHeight = maxX / minX * 8;
mcuWidth = maxY / minY * 8;
}

}