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>
|
2022-01-05 05:42:26 +03:00
|
|
|
* Copyright (c) 2021-2022, Mustafa Quraish <mustafa@serenityos.org>
|
2021-08-31 21:24:46 +03:00
|
|
|
* Copyright (c) 2021, Tobias Christiansen <tobyase@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
|
|
|
|
|
2022-08-25 21:50:15 +03:00
|
|
|
#include "Selection.h"
|
2020-05-25 23:38:27 +03:00
|
|
|
#include <AK/HashTable.h>
|
2021-08-31 21:24:46 +03:00
|
|
|
#include <AK/JsonObjectSerializer.h>
|
2023-08-08 21:22:54 +03:00
|
|
|
#include <AK/Optional.h>
|
2020-05-12 23:22:45 +03:00
|
|
|
#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-11-13 16:19:24 +03:00
|
|
|
#include <LibGUI/Command.h>
|
2020-05-12 23:22:45 +03:00
|
|
|
#include <LibGUI/Forward.h>
|
2021-09-03 02:29:03 +03:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2020-05-12 23:22:45 +03:00
|
|
|
#include <LibGfx/Forward.h>
|
2022-03-08 15:00:04 +03:00
|
|
|
#include <LibGfx/Painter.h>
|
2020-05-12 23:22:45 +03:00
|
|
|
#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;
|
2021-09-03 14:38:45 +03:00
|
|
|
class Selection;
|
2020-05-12 23:22:45 +03:00
|
|
|
|
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) { }
|
2021-07-09 22:33:30 +03:00
|
|
|
virtual void image_did_modify_layer_properties(size_t) { }
|
|
|
|
virtual void image_did_modify_layer_bitmap(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&) { }
|
2021-09-03 02:29:03 +03:00
|
|
|
virtual void image_did_change_rect(Gfx::IntRect const&) { }
|
2020-10-17 19:47:34 +03:00
|
|
|
virtual void image_select_layer(Layer*) { }
|
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:
|
2023-01-28 23:12:17 +03:00
|
|
|
static ErrorOr<NonnullRefPtr<Image>> create_with_size(Gfx::IntSize);
|
|
|
|
static ErrorOr<NonnullRefPtr<Image>> create_from_pixel_paint_json(JsonObject const&);
|
|
|
|
static ErrorOr<NonnullRefPtr<Image>> create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> const&);
|
2021-09-03 19:46:11 +03:00
|
|
|
|
2023-08-08 21:22:54 +03:00
|
|
|
static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> decode_bitmap(ReadonlyBytes, Optional<StringView> guessed_mime_type);
|
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.)
|
2023-01-28 23:12:17 +03:00
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> compose_bitmap(Gfx::BitmapFormat format) const;
|
|
|
|
RefPtr<Gfx::Bitmap> copy_bitmap(Selection const&) const;
|
2021-06-15 12:30:34 +03:00
|
|
|
|
2022-08-25 21:50:15 +03:00
|
|
|
Selection& selection() { return m_selection; }
|
|
|
|
Selection const& selection() const { return m_selection; }
|
|
|
|
|
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
|
|
|
|
2022-12-07 00:35:32 +03:00
|
|
|
Gfx::IntSize 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>);
|
2023-03-24 20:51:24 +03:00
|
|
|
void insert_layer(NonnullRefPtr<Layer>, size_t index);
|
2021-11-07 13:22:35 +03:00
|
|
|
ErrorOr<NonnullRefPtr<Image>> take_snapshot() const;
|
|
|
|
ErrorOr<void> restore_snapshot(Image const&);
|
2020-05-12 23:22:45 +03:00
|
|
|
|
2023-01-05 15:26:13 +03:00
|
|
|
void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect, float scale) const;
|
2021-08-31 21:24:46 +03:00
|
|
|
|
2022-12-17 17:38:01 +03:00
|
|
|
ErrorOr<void> serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const;
|
2023-02-10 03:00:18 +03:00
|
|
|
ErrorOr<void> export_bmp_to_file(NonnullOwnPtr<Stream>, bool preserve_alpha_channel) const;
|
|
|
|
ErrorOr<void> export_png_to_file(NonnullOwnPtr<Stream>, bool preserve_alpha_channel) const;
|
|
|
|
ErrorOr<void> export_qoi_to_file(NonnullOwnPtr<Stream>) const;
|
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*);
|
2023-02-11 18:03:42 +03:00
|
|
|
ErrorOr<void> flatten_all_layers();
|
|
|
|
ErrorOr<void> merge_visible_layers();
|
2023-02-11 18:04:39 +03:00
|
|
|
ErrorOr<void> merge_active_layer_up(Layer& layer);
|
|
|
|
ErrorOr<void> merge_active_layer_down(Layer& layer);
|
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
|
|
|
|
2022-12-20 19:17:51 +03:00
|
|
|
ErrorOr<void> flip(Gfx::Orientation orientation);
|
|
|
|
ErrorOr<void> rotate(Gfx::RotationDirection direction);
|
|
|
|
ErrorOr<void> crop(Gfx::IntRect const& rect);
|
|
|
|
ErrorOr<void> resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode scaling_mode);
|
2021-09-03 02:25:41 +03:00
|
|
|
|
2022-08-23 22:33:52 +03:00
|
|
|
Optional<Gfx::IntRect> nonempty_content_bounding_rect() const;
|
|
|
|
|
2022-12-06 23:27:44 +03:00
|
|
|
Color color_at(Gfx::IntPoint point) const;
|
2021-09-01 07:12:15 +03:00
|
|
|
|
2020-05-12 23:22:45 +03:00
|
|
|
private:
|
2023-02-11 18:03:42 +03:00
|
|
|
enum class LayerMergeMode {
|
|
|
|
All,
|
|
|
|
VisibleOnly
|
|
|
|
};
|
|
|
|
|
2023-02-11 18:04:39 +03:00
|
|
|
enum class LayerMergeDirection {
|
|
|
|
Up,
|
|
|
|
Down
|
|
|
|
};
|
|
|
|
|
2022-12-07 00:35:32 +03:00
|
|
|
explicit Image(Gfx::IntSize);
|
2020-05-12 23:22:45 +03:00
|
|
|
|
2021-07-06 21:35:19 +03:00
|
|
|
void did_change(Gfx::IntRect const& modified_rect = {});
|
2021-09-03 04:09:04 +03:00
|
|
|
void did_change_rect(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
|
|
|
|
2023-02-11 18:03:42 +03:00
|
|
|
ErrorOr<void> merge_layers(LayerMergeMode);
|
2023-02-11 18:04:39 +03:00
|
|
|
ErrorOr<void> merge_active_layer(NonnullRefPtr<Layer> const&, LayerMergeDirection);
|
2023-02-11 18:03:42 +03:00
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntSize m_size;
|
2023-03-06 16:17:01 +03:00
|
|
|
Vector<NonnullRefPtr<Layer>> m_layers;
|
2020-05-25 23:38:27 +03:00
|
|
|
|
|
|
|
HashTable<ImageClient*> m_clients;
|
2022-08-25 21:50:15 +03:00
|
|
|
|
|
|
|
Selection m_selection;
|
2020-05-12 23:22:45 +03:00
|
|
|
};
|
|
|
|
|
2020-11-13 16:19:24 +03:00
|
|
|
class ImageUndoCommand : public GUI::Command {
|
|
|
|
public:
|
2023-12-16 17:19:34 +03:00
|
|
|
ImageUndoCommand(Image&, ByteString action_text);
|
2020-11-13 16:19:24 +03:00
|
|
|
|
|
|
|
virtual void undo() override;
|
|
|
|
virtual void redo() override;
|
2023-12-16 17:19:34 +03:00
|
|
|
virtual ByteString action_text() const override { return m_action_text; }
|
2020-11-13 16:19:24 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<Image> m_snapshot;
|
|
|
|
Image& m_image;
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString m_action_text;
|
2020-11-13 16:19:24 +03:00
|
|
|
};
|
|
|
|
|
2020-05-12 23:22:45 +03:00
|
|
|
}
|