Repository: SerenityOS/serenity
File: Tests/LibGfx/TestImageWriter.cpp (C++)

Code before:
    }

    {
        Gfx::MQArithmeticCoderContext context { 0, 0 };
        auto encoder = TRY_OR_FAIL(Gfx::MQArithmeticEncoder::initialize(0x00));
        FixedMemoryStream input_stream { input };
        BigEndianInputBitStream input_bit_stream { MaybeOwned { input_stream } };
        while (!input_bit_stream.is_eof())
            encoder.encode_bit(TRY_OR_FAIL(input_bit_stream.read_bit()), context);
        auto encoded = TRY_OR_FAIL(encoder.finalize(Gfx::MQArithmeticEncoder::Trailing7FFFHandling::Remove));
        EXPECT_EQ(encoded.span(), output_remove_7FFF.span());
    }
}

TEST_CASE(test_tiff)
{
    TRY_OR_FAIL((test_roundtrip<Gfx::TIFFWriter, Gfx::TIFFImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgb_bitmap()))));

    // FIXME: Implement saving alpha channel in TIFFWriter.
    // TRY_OR_FAIL((test_roundtrip<Gfx::TIFFWriter, Gfx::TIFFImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgba_bitmap()))));
}

TEST_CASE(test_tiff_icc)
{
    auto sRGB_icc_profile = MUST(Gfx::ICC::sRGB());
    auto sRGB_icc_data = MUST(Gfx::ICC::encode(sRGB_icc_profile));

    auto rgb_bitmap = TRY_OR_FAIL(create_test_rgb_bitmap());
    Gfx::TIFFEncoderOptions options;
    options.icc_data = sRGB_icc_data;
    auto encoded_rgb_bitmap = TRY_OR_FAIL((encode_bitmap<Gfx::TIFFWriter>(rgb_bitmap, options)));

    auto decoded_rgb_plugin = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(encoded_rgb_bitmap));
    expect_bitmaps_equal(*TRY_OR_FAIL(expect_single_frame_of_size(*decoded_rgb_plugin, rgb_bitmap->size())), rgb_bitmap);
    auto decoded_rgb_profile = TRY_OR_FAIL(Gfx::ICC::Profile::try_load_from_externally_owned_memory(TRY_OR_FAIL(decoded_rgb_plugin->icc_data()).value()));
    auto reencoded_icc_data = TRY_OR_FAIL(Gfx::ICC::encode(decoded_rgb_profile));
    EXPECT_EQ(sRGB_icc_data, reencoded_icc_data);
}

TEST_CASE(test_webp)
{
    TRY_OR_FAIL((test_roundtrip<Gfx::WebPWriter, Gfx::WebPImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgb_bitmap()))));
    TRY_OR_FAIL((test_roundtrip<Gfx::WebPWriter, Gfx::WebPImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgba_bitmap()))));
}

TEST_CASE(test_webp_color_indexing_transform)
{
    Array<Color, 256> colors;
    for (size_t i = 0; i < colors.size(); ++i) {
        colors[i].set_red(i);
        colors[i].set_green(255 - i);

Review comment (bug):
I see no reasons to use `MUST` here. I know this shouldn't fail, but a crashing test is always worse than a failing one, IMO. Especially as the crash occurs right here, rather than where it started to go wrong.

Code after:
    }

    {
        Gfx::MQArithmeticCoderContext context { 0, 0 };
        auto encoder = TRY_OR_FAIL(Gfx::MQArithmeticEncoder::initialize(0x00));
        FixedMemoryStream input_stream { input };
        BigEndianInputBitStream input_bit_stream { MaybeOwned { input_stream } };
        while (!input_bit_stream.is_eof())
            encoder.encode_bit(TRY_OR_FAIL(input_bit_stream.read_bit()), context);
        auto encoded = TRY_OR_FAIL(encoder.finalize(Gfx::MQArithmeticEncoder::Trailing7FFFHandling::Remove));
        EXPECT_EQ(encoded.span(), output_remove_7FFF.span());
    }
}

TEST_CASE(test_tiff)
{
    TRY_OR_FAIL((test_roundtrip<Gfx::TIFFWriter, Gfx::TIFFImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgb_bitmap()))));
    TRY_OR_FAIL((test_roundtrip<Gfx::TIFFWriter, Gfx::TIFFImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgba_bitmap()))));
}

TEST_CASE(test_tiff_icc)
{
    auto sRGB_icc_profile = TRY_OR_FAIL(Gfx::ICC::sRGB());
    auto sRGB_icc_data = TRY_OR_FAIL(Gfx::ICC::encode(sRGB_icc_profile));

    auto rgb_bitmap = TRY_OR_FAIL(create_test_rgb_bitmap());
    Gfx::TIFFEncoderOptions options;
    options.icc_data = sRGB_icc_data;
    auto encoded_rgb_bitmap = TRY_OR_FAIL((encode_bitmap<Gfx::TIFFWriter>(rgb_bitmap, options)));

    auto decoded_rgb_plugin = TRY_OR_FAIL(Gfx::TIFFImageDecoderPlugin::create(encoded_rgb_bitmap));
    expect_bitmaps_equal(*TRY_OR_FAIL(expect_single_frame_of_size(*decoded_rgb_plugin, rgb_bitmap->size())), rgb_bitmap);
    auto decoded_rgb_profile = TRY_OR_FAIL(Gfx::ICC::Profile::try_load_from_externally_owned_memory(TRY_OR_FAIL(decoded_rgb_plugin->icc_data()).value()));
    auto reencoded_icc_data = TRY_OR_FAIL(Gfx::ICC::encode(decoded_rgb_profile));
    EXPECT_EQ(sRGB_icc_data, reencoded_icc_data);
}

TEST_CASE(test_webp)
{
    TRY_OR_FAIL((test_roundtrip<Gfx::WebPWriter, Gfx::WebPImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgb_bitmap()))));
    TRY_OR_FAIL((test_roundtrip<Gfx::WebPWriter, Gfx::WebPImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgba_bitmap()))));
}

TEST_CASE(test_webp_color_indexing_transform)
{
    Array<Color, 256> colors;
    for (size_t i = 0; i < colors.size(); ++i) {
        colors[i].set_red(i);
        colors[i].set_green(255 - i);
        colors[i].set_blue(128);
        colors[i].set_alpha(255 - i / 16);