Skip to content

Commit

Permalink
TestImageWriter: Add a JPEG roundtrip test
Browse files Browse the repository at this point in the history
  • Loading branch information
nico committed May 6, 2024
1 parent 80acd54 commit b71b8ad
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Tests/LibGfx/TestImageWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageFormats/BMPLoader.h>
#include <LibGfx/ImageFormats/BMPWriter.h>
#include <LibGfx/ImageFormats/JPEGLoader.h>
#include <LibGfx/ImageFormats/JPEGWriter.h>
#include <LibGfx/ImageFormats/PNGLoader.h>
#include <LibGfx/ImageFormats/PNGWriter.h>
#include <LibGfx/ImageFormats/QOILoader.h>
Expand Down Expand Up @@ -36,7 +38,7 @@ static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> expect_single_frame_of_size(Gfx::Imag
}

template<class Writer, class Loader>
static ErrorOr<void> test_roundtrip(Gfx::Bitmap const& bitmap)
static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> get_roundtrip_bitmap(Gfx::Bitmap const& bitmap)
{
ByteBuffer encoded_data;
if constexpr (requires(AllocatingMemoryStream stream) { Writer::encode(stream, bitmap); }) {
Expand All @@ -48,7 +50,13 @@ static ErrorOr<void> test_roundtrip(Gfx::Bitmap const& bitmap)
}

auto plugin = TRY(Loader::create(encoded_data));
auto decoded = TRY(expect_single_frame_of_size(*plugin, bitmap.size()));
return expect_single_frame_of_size(*plugin, bitmap.size());
}

template<class Writer, class Loader>
static ErrorOr<void> test_roundtrip(Gfx::Bitmap const& bitmap)
{
auto decoded = TRY((get_roundtrip_bitmap<Writer, Loader>(bitmap)));

for (int y = 0; y < bitmap.height(); ++y)
for (int x = 0; x < bitmap.width(); ++x)
Expand Down Expand Up @@ -89,6 +97,12 @@ TEST_CASE(test_bmp)
TRY_OR_FAIL((test_roundtrip<Gfx::BMPWriter, Gfx::BMPImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgba_bitmap()))));
}

TEST_CASE(test_jpeg)
{
// JPEG is lossy, so the roundtripped bitmap won't match the original bitmap. But it should still have the same size.
(void)TRY_OR_FAIL((get_roundtrip_bitmap<Gfx::JPEGWriter, Gfx::JPEGImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgb_bitmap()))));
}

TEST_CASE(test_png)
{
TRY_OR_FAIL((test_roundtrip<Gfx::PNGWriter, Gfx::PNGImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgb_bitmap()))));
Expand Down

0 comments on commit b71b8ad

Please sign in to comment.