Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibGfx: Apply Exif orientation for PNG images #1821

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Tests/LibGfx/TestImageDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,13 @@ TEST_CASE(test_exif)
EXPECT(Gfx::PNGImageDecoderPlugin::sniff(file->bytes()));
auto plugin_decoder = TRY_OR_FAIL(Gfx::PNGImageDecoderPlugin::create(file->bytes()));

TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 100, 200 }));
auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 200, 100 }));
EXPECT(plugin_decoder->metadata().has_value());
auto const& exif_metadata = static_cast<Gfx::ExifMetadata const&>(plugin_decoder->metadata().value());
EXPECT_EQ(*exif_metadata.orientation(), Gfx::TIFF::Orientation::Rotate90Clockwise);

EXPECT_EQ(frame.image->get_pixel(65, 70), Gfx::Color(0, 255, 0));
EXPECT_EQ(frame.image->get_pixel(190, 10), Gfx::Color(255, 0, 0));
}

TEST_CASE(test_png_malformed_frame)
Expand Down
32 changes: 32 additions & 0 deletions Userland/Libraries/LibGfx/ImageFormats/PNGLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <AK/Vector.h>
#include <LibGfx/DeprecatedPainter.h>
#include <LibGfx/ImageFormats/ExifOrientedBitmap.h>
#include <LibGfx/ImageFormats/PNGLoader.h>
#include <LibGfx/ImageFormats/TIFFLoader.h>
#include <LibGfx/ImageFormats/TIFFMetadata.h>
Expand Down Expand Up @@ -67,6 +68,7 @@ struct PNGLoadingContext {
RefPtr<Gfx::Bitmap> decoded_frame_bitmap;

ErrorOr<size_t> read_frames(png_structp, png_infop);
ErrorOr<void> apply_exif_orientation();
};

ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> PNGImageDecoderPlugin::create(ReadonlyBytes bytes)
Expand Down Expand Up @@ -196,10 +198,40 @@ ErrorOr<bool> PNGImageDecoderPlugin::initialize()
m_context->exif_metadata = TRY(TIFFImageDecoderPlugin::read_exif_metadata({ exif_data, exif_length }));
}

if (m_context->exif_metadata) {
if (auto result = m_context->apply_exif_orientation(); result.is_error())
dbgln("Could not apply eXIf chunk orientation for PNG: {}", result.error());
}

png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
return true;
}

ErrorOr<void> PNGLoadingContext::apply_exif_orientation()
{
auto orientation = exif_metadata->orientation().value_or(TIFF::Orientation::Default);
if (orientation == TIFF::Orientation::Default)
return {};

for (auto& img_frame_descriptor : frame_descriptors) {
auto& img = img_frame_descriptor.image;
auto oriented_bmp = TRY(ExifOrientedBitmap::create(orientation, img->size(), img->format()));

for (int y = 0; y < img->size().height(); ++y) {
for (int x = 0; x < img->size().width(); ++x) {
auto pixel = img->get_pixel(x, y);
oriented_bmp.set_pixel(x, y, pixel.value());
}
}

img_frame_descriptor.image = oriented_bmp.bitmap();
}

size = ExifOrientedBitmap::oriented_size(size, orientation);

return {};
}

static ErrorOr<NonnullRefPtr<Bitmap>> render_animation_frame(AnimationFrame const& prev_animation_frame, AnimationFrame const& animation_frame, Bitmap const& decoded_frame_bitmap)
{
auto rendered_bitmap = TRY(prev_animation_frame.bitmap->clone());
Expand Down
Loading