Fix expensive decompression in JPEG GetImageInfo #44066
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The current implementation of GetImageInfo starts a libjpeg decompression pass over input images. Certain images (e.g., progressive JPEG) trigger a full image decompression, resulting in a performance degradation. This commit switches to an equivalent but cheaper libjpeg call for evaluating image dimensions.
The end result is performance is currently slowed down on these images by over 50x when using ExtractJpegShape.
To reproduce, download input image as 'test_img.jpg':

Convert it to progressive jpeg:
jpegtran -progressive test_img.jpg > test_img_progressive.jpgAnd benchmark using test_img_progressive.jpg with
use_shape=Falseanduse_shape=True. On my machine, the first finishes in 0.12 seconds and the second in 8.85 seconds (roughly 70x performance degradation).Inspecting profiling results, the data decoding part of the decompression pass is being performed on the JPEG. In other words, the JPEG is being decompressed at near the full cost of decompression rather than simply extracting the shape (a metadata operation).

This PR replaces the
jpeg_start_decompresscall with a call tojpeg_calc_output_dimensions, which fills out the requiredcinfofields without decompressing the data.