1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 11:27:34 +00:00

PixelPaint: Support opening more image file formats

Previously we could only open .pp files, now we can open all formats
supported by Gfx::Bitmap::load_from_file
This commit is contained in:
Marco Cutecchia 2021-06-01 08:34:40 +02:00 committed by Andreas Kling
parent 566d3cb393
commit 76adac103e
3 changed files with 34 additions and 3 deletions

View file

@ -12,8 +12,11 @@
#include <AK/JsonValue.h>
#include <AK/StringBuilder.h>
#include <LibGUI/Painter.h>
#include <LibGfx/BMPLoader.h>
#include <LibGfx/BMPWriter.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
#include <LibGfx/PNGLoader.h>
#include <LibGfx/PNGWriter.h>
#include <stdio.h>
@ -49,7 +52,22 @@ void Image::paint_into(GUI::Painter& painter, const Gfx::IntRect& dest_rect)
}
}
RefPtr<Image> Image::create_from_file(const String& file_path)
RefPtr<Image> Image::create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap)
{
auto image = create_with_size({ bitmap->width(), bitmap->height() });
if (image.is_null())
return nullptr;
auto layer = Layer::create_with_bitmap(*image, *bitmap, "Background");
if (layer.is_null())
return nullptr;
image->add_layer(layer.release_nonnull());
return image;
}
RefPtr<Image> Image::create_from_pixel_paint_file(String const& file_path)
{
auto file = fopen(file_path.characters(), "r");
fseek(file, 0L, SEEK_END);
@ -87,6 +105,16 @@ RefPtr<Image> Image::create_from_file(const String& file_path)
return image;
}
RefPtr<Image> Image::create_from_file(String const& file_path)
{
auto bitmap = Gfx::Bitmap::load_from_file(file_path);
if (bitmap) {
return create_from_bitmap(bitmap);
}
return create_from_pixel_paint_file(file_path);
}
void Image::save(const String& file_path) const
{
// Build json file