1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:47:45 +00:00

LibGfx: Re-structure the whole initialization pattern for image decoders

When trying to figure out the correct implementation, we now have a very
strong distinction on plugins that are well suited for sniffing, and
plugins that need a MIME type to be chosen.

Instead of having multiple calls to non-static virtual sniff methods for
each Image decoding plugin, we have 2 static methods for each
implementation:
1. The sniff method, which in contrast to the old method, gets a
    ReadonlyBytes parameter and ensures we can figure out the result
    with zero heap allocations for most implementations.
2. The create method, which just creates a new instance so we don't
    expose the constructor to everyone anymore.

In addition to that, we have a new virtual method called initialize,
which has a per-implementation initialization pattern to actually ensure
each implementation can construct a decoder object, and then have a
correct context being applied to it for the actual decoding.
This commit is contained in:
Liav A 2023-01-20 10:13:14 +02:00 committed by Linus Groh
parent 6e6999ce57
commit 57e19a7e56
33 changed files with 493 additions and 206 deletions

View file

@ -24,138 +24,180 @@
TEST_CASE(test_bmp)
{
auto file = Core::MappedFile::map("/res/html/misc/bmpsuite_files/rgba32-1.bmp"sv).release_value();
auto bmp = Gfx::BMPImageDecoderPlugin((u8 const*)file->data(), file->size());
EXPECT(bmp.frame_count());
EXPECT_EQ(MUST(Gfx::BMPImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::BMPImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(bmp.sniff());
EXPECT(!bmp.is_animated());
EXPECT(!bmp.loop_count());
EXPECT(plugin_decoder->frame_count());
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
auto frame = bmp.frame(0).release_value_but_fixme_should_propagate_errors();
auto frame = plugin_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
EXPECT(frame.duration == 0);
}
TEST_CASE(test_gif)
{
auto file = Core::MappedFile::map("/res/graphics/download-animation.gif"sv).release_value();
auto gif = Gfx::GIFImageDecoderPlugin((u8 const*)file->data(), file->size());
EXPECT(gif.frame_count());
EXPECT_EQ(MUST(Gfx::GIFImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::GIFImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(gif.sniff());
EXPECT(gif.is_animated());
EXPECT(!gif.loop_count());
EXPECT(plugin_decoder->frame_count());
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
auto frame = gif.frame(1).release_value_but_fixme_should_propagate_errors();
auto frame = plugin_decoder->frame(1).release_value_but_fixme_should_propagate_errors();
EXPECT(frame.duration == 400);
}
TEST_CASE(test_not_ico)
{
auto file = Core::MappedFile::map("/res/graphics/buggie.png"sv).release_value();
auto ico = Gfx::ICOImageDecoderPlugin((u8 const*)file->data(), file->size());
EXPECT(ico.frame_count());
EXPECT_EQ(MUST(Gfx::ICOImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::ICOImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(!ico.sniff());
EXPECT(!ico.is_animated());
EXPECT(!ico.loop_count());
EXPECT(plugin_decoder->frame_count());
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
EXPECT(ico.frame(0).is_error());
EXPECT(plugin_decoder->frame(0).is_error());
}
TEST_CASE(test_bmp_embedded_in_ico)
{
auto file = Core::MappedFile::map("/res/icons/16x16/serenity.ico"sv).release_value();
auto ico = Gfx::ICOImageDecoderPlugin((u8 const*)file->data(), file->size());
EXPECT(ico.frame_count());
EXPECT_EQ(MUST(Gfx::ICOImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::ICOImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(ico.sniff());
EXPECT(!ico.is_animated());
EXPECT(!ico.loop_count());
EXPECT(plugin_decoder->frame_count());
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
EXPECT(!ico.frame(0).is_error());
EXPECT(!plugin_decoder->frame(0).is_error());
}
TEST_CASE(test_jpg)
{
auto file = Core::MappedFile::map("/res/html/misc/bmpsuite_files/rgb24.jpg"sv).release_value();
auto jpg = Gfx::JPGImageDecoderPlugin((u8 const*)file->data(), file->size());
EXPECT(jpg.frame_count());
EXPECT_EQ(MUST(Gfx::JPGImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::JPGImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(jpg.sniff());
EXPECT(!jpg.is_animated());
EXPECT(!jpg.loop_count());
EXPECT(plugin_decoder->frame_count());
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
auto frame = jpg.frame(0).release_value_but_fixme_should_propagate_errors();
EXPECT(!plugin_decoder->frame(0).is_error());
auto frame = plugin_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
EXPECT(frame.duration == 0);
}
TEST_CASE(test_pbm)
{
auto file = Core::MappedFile::map("/res/html/misc/pbmsuite_files/buggie-raw.pbm"sv).release_value();
auto pbm = Gfx::PBMImageDecoderPlugin((u8 const*)file->data(), file->size());
EXPECT(pbm.frame_count());
EXPECT_EQ(MUST(Gfx::PBMImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::PBMImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(pbm.sniff());
EXPECT(!pbm.is_animated());
EXPECT(!pbm.loop_count());
EXPECT(plugin_decoder->frame_count());
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
auto frame = pbm.frame(0).release_value_but_fixme_should_propagate_errors();
EXPECT(!plugin_decoder->frame(0).is_error());
auto frame = plugin_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
EXPECT(frame.duration == 0);
}
TEST_CASE(test_pgm)
{
auto file = Core::MappedFile::map("/res/html/misc/pgmsuite_files/buggie-raw.pgm"sv).release_value();
auto pgm = Gfx::PGMImageDecoderPlugin((u8 const*)file->data(), file->size());
EXPECT(pgm.frame_count());
EXPECT_EQ(MUST(Gfx::PGMImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::PGMImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(pgm.sniff());
EXPECT(!pgm.is_animated());
EXPECT(!pgm.loop_count());
EXPECT(plugin_decoder->frame_count());
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
auto frame = pgm.frame(0).release_value_but_fixme_should_propagate_errors();
EXPECT(!plugin_decoder->frame(0).is_error());
auto frame = plugin_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
EXPECT(frame.duration == 0);
}
TEST_CASE(test_png)
{
auto file = Core::MappedFile::map("/res/graphics/buggie.png"sv).release_value();
auto png = Gfx::PNGImageDecoderPlugin((u8 const*)file->data(), file->size());
EXPECT(png.frame_count());
EXPECT_EQ(MUST(Gfx::PNGImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::PNGImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(png.sniff());
EXPECT(!png.is_animated());
EXPECT(!png.loop_count());
EXPECT(plugin_decoder->frame_count());
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
auto frame = png.frame(0).release_value_but_fixme_should_propagate_errors();
EXPECT(!plugin_decoder->frame(0).is_error());
auto frame = plugin_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
EXPECT(frame.duration == 0);
}
TEST_CASE(test_ppm)
{
auto file = Core::MappedFile::map("/res/html/misc/ppmsuite_files/buggie-raw.ppm"sv).release_value();
auto ppm = Gfx::PPMImageDecoderPlugin((u8 const*)file->data(), file->size());
EXPECT(ppm.frame_count());
EXPECT_EQ(MUST(Gfx::PPMImageDecoderPlugin::sniff({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::PPMImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(ppm.sniff());
EXPECT(!ppm.is_animated());
EXPECT(!ppm.loop_count());
EXPECT(plugin_decoder->frame_count());
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
auto frame = ppm.frame(0).release_value_but_fixme_should_propagate_errors();
EXPECT(!plugin_decoder->frame(0).is_error());
auto frame = plugin_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
EXPECT(frame.duration == 0);
}
TEST_CASE(test_targa_bottom_left)
{
auto file = Core::MappedFile::map("/res/html/misc/targasuite_files/buggie-bottom-left-uncompressed.tga"sv).release_value();
auto tga = Gfx::TGAImageDecoderPlugin(reinterpret_cast<u8 const*>(file->data()), file->size());
EXPECT_EQ(tga.frame_count(), 1u);
EXPECT_EQ(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::TGAImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(tga.sniff());
EXPECT(!tga.is_animated());
EXPECT(!tga.loop_count());
EXPECT_EQ(plugin_decoder->frame_count(), 1u);
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
auto frame_or_error = tga.frame(0);
EXPECT(!plugin_decoder->frame(0).is_error());
auto frame_or_error = plugin_decoder->frame(0);
EXPECT(!frame_or_error.is_error());
auto frame = frame_or_error.release_value();
EXPECT(frame.duration == 0);
@ -164,14 +206,19 @@ TEST_CASE(test_targa_bottom_left)
TEST_CASE(test_targa_top_left)
{
auto file = Core::MappedFile::map("/res/html/misc/targasuite_files/buggie-top-left-uncompressed.tga"sv).release_value();
auto tga = Gfx::TGAImageDecoderPlugin(reinterpret_cast<u8 const*>(file->data()), file->size());
EXPECT_EQ(tga.frame_count(), 1u);
EXPECT_EQ(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::TGAImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(tga.sniff());
EXPECT(!tga.is_animated());
EXPECT(!tga.loop_count());
EXPECT_EQ(plugin_decoder->frame_count(), 1u);
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
auto frame_or_error = tga.frame(0);
EXPECT(!plugin_decoder->frame(0).is_error());
auto frame_or_error = plugin_decoder->frame(0);
EXPECT(!frame_or_error.is_error());
auto frame = frame_or_error.release_value();
EXPECT(frame.duration == 0);
@ -180,14 +227,19 @@ TEST_CASE(test_targa_top_left)
TEST_CASE(test_targa_bottom_left_compressed)
{
auto file = Core::MappedFile::map("/res/html/misc/targasuite_files/buggie-bottom-left-compressed.tga"sv).release_value();
auto tga = Gfx::TGAImageDecoderPlugin(reinterpret_cast<u8 const*>(file->data()), file->size());
EXPECT_EQ(tga.frame_count(), 1u);
EXPECT_EQ(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::TGAImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(tga.sniff());
EXPECT(!tga.is_animated());
EXPECT(!tga.loop_count());
EXPECT_EQ(plugin_decoder->frame_count(), 1u);
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
auto frame_or_error = tga.frame(0);
EXPECT(!plugin_decoder->frame(0).is_error());
auto frame_or_error = plugin_decoder->frame(0);
EXPECT(!frame_or_error.is_error());
auto frame = frame_or_error.release_value();
EXPECT(frame.duration == 0);
@ -196,14 +248,19 @@ TEST_CASE(test_targa_bottom_left_compressed)
TEST_CASE(test_targa_top_left_compressed)
{
auto file = Core::MappedFile::map("/res/html/misc/targasuite_files/buggie-top-left-compressed.tga"sv).release_value();
auto tga = Gfx::TGAImageDecoderPlugin(reinterpret_cast<u8 const*>(file->data()), file->size());
EXPECT_EQ(tga.frame_count(), 1u);
EXPECT_EQ(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create({ (u8 const*)file->data(), file->size() })), true);
auto plugin_decoder_or_error = Gfx::TGAImageDecoderPlugin::create({ (u8 const*)file->data(), file->size() });
EXPECT(!plugin_decoder_or_error.is_error());
auto plugin_decoder = plugin_decoder_or_error.release_value();
EXPECT_EQ(plugin_decoder->initialize(), true);
EXPECT(tga.sniff());
EXPECT(!tga.is_animated());
EXPECT(!tga.loop_count());
EXPECT_EQ(plugin_decoder->frame_count(), 1u);
EXPECT(!plugin_decoder->is_animated());
EXPECT(!plugin_decoder->loop_count());
auto frame_or_error = tga.frame(0);
EXPECT(!plugin_decoder->frame(0).is_error());
auto frame_or_error = plugin_decoder->frame(0);
EXPECT(!frame_or_error.is_error());
auto frame = frame_or_error.release_value();
EXPECT(frame.duration == 0);