1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:57:44 +00:00

PixelPaint: Guess image type based on its filename

We do this in ImageViewer so it understands TGA images; let's do it in
PixelPaint as well :^)
This commit is contained in:
Jelle Raaijmakers 2023-08-08 20:22:54 +02:00 committed by Daniel Bertalan
parent e272f796ad
commit 1c19c2bc92
5 changed files with 13 additions and 9 deletions

View file

@ -50,13 +50,14 @@ void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect, flo
}
}
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::decode_bitmap(ReadonlyBytes bitmap_data)
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::decode_bitmap(ReadonlyBytes bitmap_data, Optional<StringView> guessed_mime_type)
{
// Spawn a new ImageDecoder service process and connect to it.
auto client = TRY(ImageDecoderClient::Client::try_create());
auto optional_mime_type = guessed_mime_type.map([](auto mime_type) { return mime_type.to_deprecated_string(); });
// FIXME: Find a way to avoid the memory copying here.
auto maybe_decoded_image = client->decode_image(bitmap_data);
auto maybe_decoded_image = client->decode_image(bitmap_data, optional_mime_type);
if (!maybe_decoded_image.has_value())
return Error::from_string_literal("Image decode failed");
@ -91,13 +92,13 @@ ErrorOr<NonnullRefPtr<Image>> Image::create_from_pixel_paint_json(JsonObject con
auto bitmap_base64_encoded = layer_object.get_deprecated_string("bitmap"sv).value();
auto bitmap_data = TRY(decode_base64(bitmap_base64_encoded));
auto bitmap = TRY(decode_bitmap(bitmap_data));
auto bitmap = TRY(decode_bitmap(bitmap_data, {}));
auto layer = TRY(Layer::create_with_bitmap(*image, move(bitmap), name));
if (auto const& mask_object = layer_object.get_deprecated_string("mask"sv); mask_object.has_value()) {
auto mask_base64_encoded = mask_object.value();
auto mask_data = TRY(decode_base64(mask_base64_encoded));
auto mask = TRY(decode_bitmap(mask_data));
auto mask = TRY(decode_bitmap(mask_data, {}));
TRY(layer->set_bitmaps(layer->content_bitmap(), mask));
}