2020-05-12 23:22:45 +03:00
|
|
|
/*
|
2021-06-11 18:49:04 +03:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-05-12 23:22:45 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-12 23:22:45 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-05-25 23:38:27 +03:00
|
|
|
#include <AK/HashTable.h>
|
2020-05-12 23:22:45 +03:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
|
|
|
#include <AK/RefCounted.h>
|
2020-05-13 00:00:23 +03:00
|
|
|
#include <AK/RefPtr.h>
|
2021-06-14 22:46:29 +03:00
|
|
|
#include <AK/Result.h>
|
2020-05-12 23:22:45 +03:00
|
|
|
#include <AK/Vector.h>
|
2020-11-13 16:19:24 +03:00
|
|
|
#include <LibGUI/Command.h>
|
2020-05-12 23:22:45 +03:00
|
|
|
#include <LibGUI/Forward.h>
|
|
|
|
#include <LibGfx/Forward.h>
|
|
|
|
#include <LibGfx/Rect.h>
|
|
|
|
#include <LibGfx/Size.h>
|
|
|
|
|
2020-05-20 21:35:35 +03:00
|
|
|
namespace PixelPaint {
|
2020-05-12 23:22:45 +03:00
|
|
|
|
|
|
|
class Layer;
|
|
|
|
|
2020-05-25 23:38:27 +03:00
|
|
|
class ImageClient {
|
|
|
|
public:
|
|
|
|
virtual void image_did_add_layer(size_t) { }
|
|
|
|
virtual void image_did_remove_layer(size_t) { }
|
2020-05-25 23:49:50 +03:00
|
|
|
virtual void image_did_modify_layer(size_t) { }
|
2020-09-18 10:49:51 +03:00
|
|
|
virtual void image_did_modify_layer_stack() { }
|
2021-07-06 21:35:19 +03:00
|
|
|
virtual void image_did_change(Gfx::IntRect const&) { }
|
2020-10-17 19:47:34 +03:00
|
|
|
virtual void image_select_layer(Layer*) { }
|
2021-06-16 13:08:05 +03:00
|
|
|
virtual void image_did_change_title(String const&) { }
|
2021-04-15 20:43:29 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~ImageClient() = default;
|
2020-05-25 23:38:27 +03:00
|
|
|
};
|
|
|
|
|
2020-05-12 23:22:45 +03:00
|
|
|
class Image : public RefCounted<Image> {
|
|
|
|
public:
|
2021-06-11 18:49:04 +03:00
|
|
|
static RefPtr<Image> try_create_with_size(Gfx::IntSize const&);
|
2021-06-15 02:10:50 +03:00
|
|
|
static Result<NonnullRefPtr<Image>, String> try_create_from_file(String const& file_path);
|
2021-06-12 11:26:46 +03:00
|
|
|
static RefPtr<Image> try_create_from_bitmap(NonnullRefPtr<Gfx::Bitmap>);
|
2020-05-12 23:22:45 +03:00
|
|
|
|
2021-06-15 12:30:34 +03:00
|
|
|
// This generates a new Bitmap with the final image (all layers composed according to their attributes.)
|
2021-07-04 13:34:10 +03:00
|
|
|
RefPtr<Gfx::Bitmap> try_compose_bitmap(Gfx::BitmapFormat format) const;
|
2021-06-15 12:30:34 +03:00
|
|
|
|
2020-05-12 23:22:45 +03:00
|
|
|
size_t layer_count() const { return m_layers.size(); }
|
2021-06-11 14:27:47 +03:00
|
|
|
Layer const& layer(size_t index) const { return m_layers.at(index); }
|
2020-05-26 10:51:28 +03:00
|
|
|
Layer& layer(size_t index) { return m_layers.at(index); }
|
2020-05-12 23:22:45 +03:00
|
|
|
|
2021-06-11 14:27:47 +03:00
|
|
|
Gfx::IntSize const& size() const { return m_size; }
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect rect() const { return { {}, m_size }; }
|
2020-05-12 23:22:45 +03:00
|
|
|
|
|
|
|
void add_layer(NonnullRefPtr<Layer>);
|
2020-10-17 19:47:34 +03:00
|
|
|
RefPtr<Image> take_snapshot() const;
|
2021-06-02 12:50:19 +03:00
|
|
|
void restore_snapshot(Image const&);
|
2020-05-12 23:22:45 +03:00
|
|
|
|
2021-06-15 12:30:34 +03:00
|
|
|
void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const;
|
2021-06-14 22:46:29 +03:00
|
|
|
Result<void, String> write_to_file(String const& file_path) const;
|
2021-07-04 13:34:10 +03:00
|
|
|
Result<void, String> export_bmp_to_file(String const& file_path, bool preserve_alpha_channel);
|
|
|
|
Result<void, String> export_png_to_file(String const& file_path, bool preserve_alpha_channel);
|
2020-05-12 23:22:45 +03:00
|
|
|
|
2020-05-13 23:03:29 +03:00
|
|
|
void move_layer_to_front(Layer&);
|
|
|
|
void move_layer_to_back(Layer&);
|
2020-05-14 15:22:30 +03:00
|
|
|
void move_layer_up(Layer&);
|
|
|
|
void move_layer_down(Layer&);
|
2020-05-26 00:48:09 +03:00
|
|
|
void change_layer_index(size_t old_index, size_t new_index);
|
2020-05-13 23:12:14 +03:00
|
|
|
void remove_layer(Layer&);
|
2020-10-17 19:47:34 +03:00
|
|
|
void select_layer(Layer*);
|
2021-07-05 23:42:43 +03:00
|
|
|
void flatten_all_layers();
|
2021-07-06 13:48:30 +03:00
|
|
|
void merge_visible_layers();
|
2020-05-13 23:03:29 +03:00
|
|
|
|
2020-05-25 23:38:27 +03:00
|
|
|
void add_client(ImageClient&);
|
|
|
|
void remove_client(ImageClient&);
|
|
|
|
|
2021-07-06 21:35:19 +03:00
|
|
|
void layer_did_modify_bitmap(Badge<Layer>, Layer const&, Gfx::IntRect const& modified_layer_rect);
|
2021-06-02 12:50:19 +03:00
|
|
|
void layer_did_modify_properties(Badge<Layer>, Layer const&);
|
2020-05-25 23:49:50 +03:00
|
|
|
|
2021-06-11 14:27:47 +03:00
|
|
|
size_t index_of(Layer const&) const;
|
2020-05-26 10:51:28 +03:00
|
|
|
|
2021-06-16 13:08:05 +03:00
|
|
|
String const& path() const { return m_path; }
|
|
|
|
void set_path(String);
|
|
|
|
|
|
|
|
String const& title() const { return m_title; }
|
|
|
|
void set_title(String);
|
|
|
|
|
2020-05-12 23:22:45 +03:00
|
|
|
private:
|
2021-06-11 14:27:47 +03:00
|
|
|
explicit Image(Gfx::IntSize const&);
|
2020-05-12 23:22:45 +03:00
|
|
|
|
2021-06-15 02:10:50 +03:00
|
|
|
static Result<NonnullRefPtr<Image>, String> try_create_from_pixel_paint_file(String const& file_path);
|
2021-06-01 09:34:40 +03:00
|
|
|
|
2021-07-06 21:35:19 +03:00
|
|
|
void did_change(Gfx::IntRect const& modified_rect = {});
|
2020-05-26 00:48:09 +03:00
|
|
|
void did_modify_layer_stack();
|
2020-05-25 23:49:50 +03:00
|
|
|
|
2021-06-16 13:08:05 +03:00
|
|
|
String m_path;
|
|
|
|
String m_title;
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntSize m_size;
|
2020-05-12 23:22:45 +03:00
|
|
|
NonnullRefPtrVector<Layer> m_layers;
|
2020-05-25 23:38:27 +03:00
|
|
|
|
|
|
|
HashTable<ImageClient*> m_clients;
|
2020-05-12 23:22:45 +03:00
|
|
|
};
|
|
|
|
|
2020-11-13 16:19:24 +03:00
|
|
|
class ImageUndoCommand : public GUI::Command {
|
|
|
|
public:
|
|
|
|
ImageUndoCommand(Image& image);
|
|
|
|
|
|
|
|
virtual void undo() override;
|
|
|
|
virtual void redo() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<Image> m_snapshot;
|
|
|
|
Image& m_image;
|
|
|
|
};
|
|
|
|
|
2020-05-12 23:22:45 +03:00
|
|
|
}
|