123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- /*
- * Copyright (c) 2021, the SerenityOS developers.
- * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <AK/DeprecatedString.h>
- #include <LibCore/MappedFile.h>
- #include <LibGfx/ImageFormats/BMPLoader.h>
- #include <LibGfx/ImageFormats/GIFLoader.h>
- #include <LibGfx/ImageFormats/ICOLoader.h>
- #include <LibGfx/ImageFormats/ImageDecoder.h>
- #include <LibGfx/ImageFormats/JPEGLoader.h>
- #include <LibGfx/ImageFormats/PBMLoader.h>
- #include <LibGfx/ImageFormats/PGMLoader.h>
- #include <LibGfx/ImageFormats/PNGLoader.h>
- #include <LibGfx/ImageFormats/PPMLoader.h>
- #include <LibGfx/ImageFormats/TGALoader.h>
- #include <LibGfx/ImageFormats/WebPLoader.h>
- #include <LibTest/TestCase.h>
- #include <stdio.h>
- #include <string.h>
- #ifdef AK_OS_SERENITY
- # define TEST_INPUT(x) ("/usr/Tests/LibGfx/test-inputs/" x)
- #else
- # define TEST_INPUT(x) ("test-inputs/" x)
- #endif
- TEST_CASE(test_bmp)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgba32-1.bmp"sv)));
- EXPECT(Gfx::BMPImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::BMPImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT(plugin_decoder->frame_count());
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT(frame.duration == 0);
- }
- TEST_CASE(test_gif)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("download-animation.gif"sv)));
- EXPECT(Gfx::GIFImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::GIFImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT(plugin_decoder->frame_count());
- EXPECT(plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- auto frame = MUST(plugin_decoder->frame(1));
- EXPECT(frame.duration == 400);
- }
- TEST_CASE(test_not_ico)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie.png"sv)));
- EXPECT(!Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::ICOImageDecoderPlugin::create(file->bytes()));
- EXPECT(!plugin_decoder->initialize());
- EXPECT(plugin_decoder->frame_count());
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- EXPECT(plugin_decoder->frame(0).is_error());
- }
- TEST_CASE(test_bmp_embedded_in_ico)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("serenity.ico"sv)));
- EXPECT(Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::ICOImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT(plugin_decoder->frame_count());
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- EXPECT(!plugin_decoder->frame(0).is_error());
- }
- TEST_CASE(test_jpeg_sof0_one_scan)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgb24.jpg"sv)));
- EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT(plugin_decoder->frame_count());
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT(frame.duration == 0);
- }
- TEST_CASE(test_jpeg_sof0_several_scans)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("several_scans.jpg"sv)));
- EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT_EQ(frame.image->size(), Gfx::IntSize(592, 800));
- }
- TEST_CASE(test_jpeg_rgb_components)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("rgb_components.jpg"sv)));
- EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT_EQ(frame.image->size(), Gfx::IntSize(592, 800));
- }
- TEST_CASE(test_jpeg_sof2_spectral_selection)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("spectral_selection.jpg"sv)));
- EXPECT(Gfx::JPEGImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT_EQ(frame.image->size(), Gfx::IntSize(592, 800));
- }
- TEST_CASE(test_pbm)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.pbm"sv)));
- EXPECT(Gfx::PBMImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::PBMImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT(plugin_decoder->frame_count());
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT(frame.duration == 0);
- }
- TEST_CASE(test_pgm)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.pgm"sv)));
- EXPECT(Gfx::PGMImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::PGMImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT(plugin_decoder->frame_count());
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT(frame.duration == 0);
- }
- TEST_CASE(test_png)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie.png"sv)));
- EXPECT(Gfx::PNGImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT(plugin_decoder->frame_count());
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT(frame.duration == 0);
- }
- TEST_CASE(test_ppm)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-raw.ppm"sv)));
- EXPECT(Gfx::PPMImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::PPMImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT(plugin_decoder->frame_count());
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT(frame.duration == 0);
- }
- TEST_CASE(test_targa_bottom_left)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-bottom-left-uncompressed.tga"sv)));
- EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
- auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT_EQ(plugin_decoder->frame_count(), 1u);
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT(frame.duration == 0);
- }
- TEST_CASE(test_targa_top_left)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-top-left-uncompressed.tga"sv)));
- EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
- auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT_EQ(plugin_decoder->frame_count(), 1u);
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT(frame.duration == 0);
- }
- TEST_CASE(test_targa_bottom_left_compressed)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-bottom-left-compressed.tga"sv)));
- EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
- auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT_EQ(plugin_decoder->frame_count(), 1u);
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT(frame.duration == 0);
- }
- TEST_CASE(test_targa_top_left_compressed)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie-top-left-compressed.tga"sv)));
- EXPECT(MUST(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
- auto plugin_decoder = MUST(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT_EQ(plugin_decoder->frame_count(), 1u);
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- auto frame = MUST(plugin_decoder->frame(0));
- EXPECT(frame.duration == 0);
- }
- TEST_CASE(test_webp_simple_lossy)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("simple-vp8.webp"sv)));
- EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT_EQ(plugin_decoder->frame_count(), 1u);
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(240, 240));
- }
- TEST_CASE(test_webp_simple_lossless)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("simple-vp8l.webp"sv)));
- EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT_EQ(plugin_decoder->frame_count(), 1u);
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(386, 395));
- }
- TEST_CASE(test_webp_extended_lossy)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossy.webp"sv)));
- EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT_EQ(plugin_decoder->frame_count(), 1u);
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(417, 223));
- }
- TEST_CASE(test_webp_extended_lossless)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossless.webp"sv)));
- EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT_EQ(plugin_decoder->frame_count(), 1u);
- EXPECT(!plugin_decoder->is_animated());
- EXPECT(!plugin_decoder->loop_count());
- EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(417, 223));
- }
- TEST_CASE(test_webp_extended_lossless_animated)
- {
- auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossless-animated.webp"sv)));
- EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes()));
- auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
- EXPECT(plugin_decoder->initialize());
- EXPECT_EQ(plugin_decoder->frame_count(), 8u);
- EXPECT(plugin_decoder->is_animated());
- EXPECT_EQ(plugin_decoder->loop_count(), 42u);
- EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(990, 1050));
- }
|