Truly independent web browser
LibGfx: Decode a cut-off progressive JPEG from the scans that arrived
sideshowbarker11e02b263b93
diff --git a/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp b/Libraries/LibGfx/ImageFormats/JPEGLoader.cppindex d275de31c28..256f656c335 100644--- a/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp+++ b/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp@@ -91,7 +91,29 @@ ErrorOr<void> JPEGLoadingContext::decode()cinfo.out_color_space = JCS_EXT_BGRX;}+ // NB: A JPEG with several scans gives no rows until all of them have been read, unless it's decoded as a buffered+ // image, which can output its rows from whatever input has been read. So decode it that way, and a file that+ // was cut off short still shows the scans that did arrive.+ bool const decode_as_buffered_image = jpeg_has_multiple_scans(&cinfo);+ cinfo.buffered_image = decode_as_buffered_image;+jpeg_start_decompress(&cinfo);+ if (decode_as_buffered_image) {+ int status;+ do {+ status = jpeg_consume_input(&cinfo);+ } while (status != JPEG_SUSPENDED && status != JPEG_REACHED_EOI);+ auto scan = cinfo.input_scan_number;+ if (status != JPEG_REACHED_EOI) {+ // NB: An output pass for the scan being read waits for the rest of that scan's data, which isn't coming,+ // so output from the scan before it, which only waits for data already read. Block smoothing would+ // wait on rows of the scan after the one output, too, so leave it off.+ if (scan > 1)+ --scan;+ cinfo.do_block_smoothing = false;+ }+ jpeg_start_output(&cinfo, scan);+ }bool could_read_all_scanlines = true;if (cinfo.out_color_space == JCS_EXT_BGRX) {@@ -178,7 +200,10 @@ ErrorOr<void> JPEGLoadingContext::decode()free(icc_data_ptr);}- if (could_read_all_scanlines)+ if (decode_as_buffered_image)+ jpeg_finish_output(&cinfo);++ if (could_read_all_scanlines && (!decode_as_buffered_image || jpeg_input_complete(&cinfo)))jpeg_finish_decompress(&cinfo);elsejpeg_abort_decompress(&cinfo);diff --git a/Tests/LibGfx/TestImageDecoder.cpp b/Tests/LibGfx/TestImageDecoder.cppindex c22c798fe94..40ccb8f1c8f 100644--- a/Tests/LibGfx/TestImageDecoder.cpp+++ b/Tests/LibGfx/TestImageDecoder.cpp@@ -856,6 +856,58 @@ TEST_CASE(test_jpeg_sof2_successive_approximation)TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 600, 800 }));}+TEST_CASE(test_jpeg_sof2_truncated)+{+ auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/successive_approximation.jpg"sv)));+ auto full_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));+ auto full_image = TRY_OR_FAIL(full_decoder->frame(0)).image;++ // Cut off within the later scans, which only refine the image the earlier ones already cover.+ auto truncated_file = file->bytes().slice(0, file->bytes().size() * 6 / 10);+ auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(truncated_file));+ TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 600, 800 }));++ auto image = plugin_decoder->frame(0).value().image;+ for (auto point : { Gfx::IntPoint { 100, 100 }, Gfx::IntPoint { 300, 400 }, Gfx::IntPoint { 500, 700 } }) {+ auto pixel = image->get_pixel(point.x(), point.y());+ auto full_pixel = full_image->get_pixel(point.x(), point.y());+ EXPECT(abs(pixel.red() - full_pixel.red()) < 48);+ EXPECT(abs(pixel.green() - full_pixel.green()) < 48);+ EXPECT(abs(pixel.blue() - full_pixel.blue()) < 48);+ }+}++TEST_CASE(test_jpeg_sof2_truncated_after_first_scan)+{+ auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/successive_approximation.jpg"sv)));+ auto bytes = file->bytes();+ auto full_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(bytes));+ auto full_image = TRY_OR_FAIL(full_decoder->frame(0)).image;++ // Cut off where the second scan's SOS marker starts, so only the first scan arrives, but all of it.+ size_t start_of_scan_markers = 0;+ size_t cut = 0;+ for (size_t i = 0; i + 1 < bytes.size(); ++i) {+ if (bytes[i] == 0xFF && bytes[i + 1] == 0xDA && ++start_of_scan_markers == 2) {+ cut = i;+ break;+ }+ }+ EXPECT(cut > 0);+ auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(bytes.slice(0, cut)));+ TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 600, 800 }));++ // Every row comes out, the bottom ones too, as a coarser version of the full image.+ auto image = plugin_decoder->frame(0).value().image;+ for (int y = 790; y < 800; ++y) {+ auto pixel = image->get_pixel(300, y);+ auto full_pixel = full_image->get_pixel(300, y);+ EXPECT(abs(pixel.red() - full_pixel.red()) < 64);+ EXPECT(abs(pixel.green() - full_pixel.green()) < 64);+ EXPECT(abs(pixel.blue() - full_pixel.blue()) < 64);+ }+}+TEST_CASE(test_jpeg_empty_icc){auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpg/gradient_empty_icc.jpg"sv)));