Truly independent web browser
LibGfx: Draw the decoded rows of a truncated PNG
sideshowbarker20f0da23b720
diff --git a/Libraries/LibGfx/ImageFormats/PNGLoader.cpp b/Libraries/LibGfx/ImageFormats/PNGLoader.cppindex 9af0f34adae..990ea0dd9b3 100644--- a/Libraries/LibGfx/ImageFormats/PNGLoader.cpp+++ b/Libraries/LibGfx/ImageFormats/PNGLoader.cpp@@ -55,16 +55,18 @@ struct PNGLoadingContext {{// NOTE: We need to setjmp() here because libpng uses longjmp() for error handling.if (auto error_value = setjmp(png_jmpbuf(png_ptr)); error_value) {- // longjmp() bypassed the C++ destructors for any stack-locals in read_frames(); release the working-state- // members explicitly here — so their heap storage doesn't sit around until ~PNGLoadingContext().+ if (error_value != EIO || !in_flight_bitmap || output_buffer) {+ clear_read_frames_working_state();+ return Error::from_errno(error_value);+ }+ frame_descriptors.append({ in_flight_bitmap.release_nonnull(), 0 });+ frame_count = 1;clear_read_frames_working_state();- return Error::from_errno(error_value);+ } else {+ png_read_update_info(png_ptr, info_ptr);+ frame_count = TRY(read_frames(png_ptr, info_ptr));}- png_read_update_info(png_ptr, info_ptr);-- frame_count = TRY(read_frames(png_ptr, info_ptr));-if (exif_metadata)TRY(apply_exif_orientation());return {};@@ -176,12 +178,13 @@ ErrorOr<void> PNGImageDecoderPlugin::initialize()png_set_read_fn(m_context->png_ptr, &m_context->data, [](png_structp png_ptr, png_bytep data, png_size_t length) {auto* read_data = reinterpret_cast<ReadonlyBytes*>(png_get_io_ptr(png_ptr));- if (read_data->size() < length) {- png_error(png_ptr, "Read error");- return;- }+ if (read_data->size() < length)+ png_longjmp(png_ptr, EIO);memcpy(data, read_data->data(), length);*read_data = read_data->slice(length);+ // Let libpng inflate the available bytes even when the file ends inside an IDAT chunk.+ if (!read_data->is_empty() && read_data->size() < png_get_compression_buffer_size(png_ptr))+ png_set_compression_buffer_size(png_ptr, read_data->size());});png_set_error_fn(m_context->png_ptr, nullptr, log_png_error, log_png_warning);@@ -348,6 +351,10 @@ ErrorOr<size_t> PNGLoadingContext::read_frames(png_structp png_ptr, png_infop inelsein_flight_bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, AlphaType::Unpremultiplied, frame_size));+ // A partial static frame keeps the pixels that libpng never wrote transparent, including between interlace passes.+ if (!output_buffer)+ __builtin_memset(in_flight_bitmap->begin(), 0, in_flight_bitmap->size_in_bytes());+row_pointers.resize_and_keep_capacity(frame_size.height());for (auto i = 0; i < frame_size.height(); ++i)row_pointers[i] = in_flight_bitmap->scanline_u8(i);diff --git a/Tests/LibGfx/TestImageDecoder.cpp b/Tests/LibGfx/TestImageDecoder.cppindex 4f5121749dd..23c0427bafd 100644--- a/Tests/LibGfx/TestImageDecoder.cpp+++ b/Tests/LibGfx/TestImageDecoder.cpp@@ -973,6 +973,17 @@ TEST_CASE(test_png)TRY_OR_FAIL(expect_single_frame(*plugin_decoder));}+TEST_CASE(test_png_truncated)+{+ auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("png/1x65535.png"sv)));+ auto bytes = file->bytes().slice(0, file->bytes().size() / 2);+ auto decoder = TRY_OR_FAIL(Gfx::PNGImageDecoderPlugin::create(bytes));+ auto frame = TRY_OR_FAIL(decoder->frame(0));+ EXPECT_EQ(frame.image->size(), Gfx::IntSize(1, 65535));+ EXPECT_EQ(frame.image->get_pixel(0, 16384), Gfx::Color::NamedColor::Red);+ EXPECT_EQ(frame.image->get_pixel(0, 65534), Gfx::Color::NamedColor::Transparent);+}+TEST_CASE(test_apng){auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("png/apng-1-frame.png"sv)));diff --git a/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/RGB_64x64-progressive-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/RGB_64x64-progressive-ref.htmlnew file mode 100644index 00000000000..d88c4e81941--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/RGB_64x64-progressive-ref.html@@ -0,0 +1,19 @@+<!DOCTYPE html>+<meta charset="utf-8">+<title>PNG: progressive display of interlaced image RGB_64x64 (reference)</title>+<link rel="author" title="Chris Lilley" href="mailto:[email protected]">++<style>+ .pass { position: relative; display: inline-block; margin: 0 8px 8px 0; line-height: 0; }+ .pass img { display: block; image-rendering: pixelated; }+ .pass img.mask { position: absolute; left: 0; top: 0; }+</style>+<p>Test passes if the images below match the reference: the grey masks+leave visible only the pixels received by the end of each pass, which+must show their final values.</p>+<div class="pass"><img src="support/RGB_64x64-pass7.png" width="64" height="64" alt=""><img class="mask" src="support/RGB_64x64-mask1.png" width="64" height="64" alt=""></div>+<div class="pass"><img src="support/RGB_64x64-pass7.png" width="64" height="64" alt=""><img class="mask" src="support/RGB_64x64-mask2.png" width="64" height="64" alt=""></div>+<div class="pass"><img src="support/RGB_64x64-pass7.png" width="64" height="64" alt=""><img class="mask" src="support/RGB_64x64-mask3.png" width="64" height="64" alt=""></div>+<div class="pass"><img src="support/RGB_64x64-pass7.png" width="64" height="64" alt=""><img class="mask" src="support/RGB_64x64-mask4.png" width="64" height="64" alt=""></div>+<div class="pass"><img src="support/RGB_64x64-pass7.png" width="64" height="64" alt=""><img class="mask" src="support/RGB_64x64-mask5.png" width="64" height="64" alt=""></div>+<div class="pass"><img src="support/RGB_64x64-pass7.png" width="64" height="64" alt=""><img class="mask" src="support/RGB_64x64-mask6.png" width="64" height="64" alt=""></div>diff --git a/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask1.png b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask1.pngnew file mode 100644index 00000000000..b9198621d24Binary files /dev/null and b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask1.png differdiff --git a/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask2.png b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask2.pngnew file mode 100644index 00000000000..2b889e1f509Binary files /dev/null and b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask2.png differdiff --git a/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask3.png b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask3.pngnew file mode 100644index 00000000000..e67abf3eb34Binary files /dev/null and b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask3.png differdiff --git a/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask4.png b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask4.pngnew file mode 100644index 00000000000..56a48d80f0dBinary files /dev/null and b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask4.png differdiff --git a/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask5.png b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask5.pngnew file mode 100644index 00000000000..a204aa8a84fBinary files /dev/null and b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask5.png differdiff --git a/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask6.png b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask6.pngnew file mode 100644index 00000000000..f8a85748befBinary files /dev/null and b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-mask6.png differdiff --git a/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-pass7.png b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-pass7.pngnew file mode 100644index 00000000000..35867ea10e6Binary files /dev/null and b/Tests/LibWeb/Ref/expected/wpt-import/png/progressive/support/RGB_64x64-pass7.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/RGB_64x64-progressive.html b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/RGB_64x64-progressive.htmlnew file mode 100644index 00000000000..8be1d451881--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/RGB_64x64-progressive.html@@ -0,0 +1,21 @@+<!DOCTYPE html>+<meta charset="utf-8">+<title>PNG: progressive display of interlaced image RGB_64x64</title>+<link rel="author" title="Chris Lilley" href="mailto:[email protected]">+<link rel="help" href="https://w3c.github.io/png/#13Progressive-display">+<link rel="match" href="../../../../expected/wpt-import/png/progressive/RGB_64x64-progressive-ref.html">+<meta name="assert" content="When an Adam7-interlaced PNG has been received up to the end of pass N, for N from 1 to 6, the pixels received so far are displayed with their final values. Positions not yet received, which a viewer may fill in any way, are hidden by a mask.">+<style>+ .pass { position: relative; display: inline-block; margin: 0 8px 8px 0; line-height: 0; }+ .pass img { display: block; image-rendering: pixelated; }+ .pass img.mask { position: absolute; left: 0; top: 0; }+</style>+<p>Test passes if the images below match the reference: the grey masks+leave visible only the pixels received by the end of each pass, which+must show their final values.</p>+<div class="pass"><img src="support/RGB_64x64-trunc1.png" width="64" height="64" alt=""><img class="mask" src="support/RGB_64x64-mask1.png" width="64" height="64" alt=""></div>+<div class="pass"><img src="support/RGB_64x64-trunc2.png" width="64" height="64" alt=""><img class="mask" src="support/RGB_64x64-mask2.png" width="64" height="64" alt=""></div>+<div class="pass"><img src="support/RGB_64x64-trunc3.png" width="64" height="64" alt=""><img class="mask" src="support/RGB_64x64-mask3.png" width="64" height="64" alt=""></div>+<div class="pass"><img src="support/RGB_64x64-trunc4.png" width="64" height="64" alt=""><img class="mask" src="support/RGB_64x64-mask4.png" width="64" height="64" alt=""></div>+<div class="pass"><img src="support/RGB_64x64-trunc5.png" width="64" height="64" alt=""><img class="mask" src="support/RGB_64x64-mask5.png" width="64" height="64" alt=""></div>+<div class="pass"><img src="support/RGB_64x64-trunc6.png" width="64" height="64" alt=""><img class="mask" src="support/RGB_64x64-mask6.png" width="64" height="64" alt=""></div>diff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask1.png b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask1.pngnew file mode 100644index 00000000000..b9198621d24Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask1.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask2.png b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask2.pngnew file mode 100644index 00000000000..2b889e1f509Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask2.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask3.png b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask3.pngnew file mode 100644index 00000000000..e67abf3eb34Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask3.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask4.png b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask4.pngnew file mode 100644index 00000000000..56a48d80f0dBinary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask4.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask5.png b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask5.pngnew file mode 100644index 00000000000..a204aa8a84fBinary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask5.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask6.png b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask6.pngnew file mode 100644index 00000000000..f8a85748befBinary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-mask6.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc1.png b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc1.pngnew file mode 100644index 00000000000..6fda98c1c49Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc1.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc2.png b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc2.pngnew file mode 100644index 00000000000..b0e12decf19Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc2.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc3.png b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc3.pngnew file mode 100644index 00000000000..5ce25a76a81Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc3.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc4.png b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc4.pngnew file mode 100644index 00000000000..0b48b84f74fBinary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc4.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc5.png b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc5.pngnew file mode 100644index 00000000000..dbe821a0982Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc5.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc6.png b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc6.pngnew file mode 100644index 00000000000..405c1950262Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/png/progressive/support/RGB_64x64-trunc6.png differ