Преглед на файлове

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
Marco Cutecchia преди 4 години
родител
ревизия
76adac103e
променени са 3 файла, в които са добавени 34 реда и са изтрити 3 реда
  1. 29 1
      Userland/Applications/PixelPaint/Image.cpp
  2. 4 1
      Userland/Applications/PixelPaint/Image.h
  3. 1 1
      Userland/Applications/PixelPaint/main.cpp

+ 29 - 1
Userland/Applications/PixelPaint/Image.cpp

@@ -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

+ 4 - 1
Userland/Applications/PixelPaint/Image.h

@@ -37,7 +37,8 @@ protected:
 class Image : public RefCounted<Image> {
 public:
     static RefPtr<Image> create_with_size(const Gfx::IntSize&);
-    static RefPtr<Image> create_from_file(const String& file_path);
+    static RefPtr<Image> create_from_file(String const& file_path);
+    static RefPtr<Image> create_from_bitmap(RefPtr<Gfx::Bitmap> 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<Image> create_from_pixel_paint_file(String const& file_path);
+
     void did_change();
     void did_modify_layer_stack();
 

+ 1 - 1
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");