From 76adac103ec370d6a02eacd6e602b65f2a6d70fe Mon Sep 17 00:00:00 2001 From: Marco Cutecchia Date: Tue, 1 Jun 2021 08:34:40 +0200 Subject: [PATCH] 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 --- Userland/Applications/PixelPaint/Image.cpp | 30 +++++++++++++++++++++- Userland/Applications/PixelPaint/Image.h | 5 +++- Userland/Applications/PixelPaint/main.cpp | 2 +- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp index 62db2fac82..affb256070 100644 --- a/Userland/Applications/PixelPaint/Image.cpp +++ b/Userland/Applications/PixelPaint/Image.cpp @@ -12,8 +12,11 @@ #include #include #include +#include #include +#include #include +#include #include #include @@ -49,7 +52,22 @@ void Image::paint_into(GUI::Painter& painter, const Gfx::IntRect& dest_rect) } } -RefPtr Image::create_from_file(const String& file_path) +RefPtr Image::create_from_bitmap(RefPtr 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::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::create_from_file(const String& file_path) return image; } +RefPtr 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 diff --git a/Userland/Applications/PixelPaint/Image.h b/Userland/Applications/PixelPaint/Image.h index 9f7765c16a..fbce98efd6 100644 --- a/Userland/Applications/PixelPaint/Image.h +++ b/Userland/Applications/PixelPaint/Image.h @@ -37,7 +37,8 @@ protected: class Image : public RefCounted { public: static RefPtr create_with_size(const Gfx::IntSize&); - static RefPtr create_from_file(const String& file_path); + static RefPtr create_from_file(String const& file_path); + static RefPtr create_from_bitmap(RefPtr bitmap); size_t layer_count() const { return m_layers.size(); } const Layer& layer(size_t index) const { return m_layers.at(index); } @@ -74,6 +75,8 @@ public: private: explicit Image(const Gfx::IntSize&); + static RefPtr create_from_pixel_paint_file(String const& file_path); + void did_change(); void did_modify_layer_stack(); diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp index a181bb7bfc..24eb295f72 100644 --- a/Userland/Applications/PixelPaint/main.cpp +++ b/Userland/Applications/PixelPaint/main.cpp @@ -49,7 +49,7 @@ int main(int argc, char** argv) const char* image_file = nullptr; Core::ArgsParser args_parser; - args_parser.add_positional_argument(image_file, "Pixel Paint image file (*.pp) to open", "path", Core::ArgsParser::Required::No); + args_parser.add_positional_argument(image_file, "Image file to open", "path", Core::ArgsParser::Required::No); args_parser.parse(argc, argv); auto app_icon = GUI::Icon::default_icon("app-pixel-paint");