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
Notes: sideshowbarker 2024-07-18 17:00:41 +09:00
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

View File

@ -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();

View File

@ -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");