summaryrefslogtreecommitdiff
path: root/Userland/Applications/PixelPaint/Image.h
blob: b681c10c13abfa3825d2a453ab205c5d6a84d829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/HashTable.h>
#include <AK/NonnullRefPtrVector.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/Result.h>
#include <AK/Vector.h>
#include <LibCore/File.h>
#include <LibGUI/Command.h>
#include <LibGUI/Forward.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Rect.h>
#include <LibGfx/Size.h>

namespace PixelPaint {

class Layer;

class ImageClient {
public:
    virtual void image_did_add_layer(size_t) { }
    virtual void image_did_remove_layer(size_t) { }
    virtual void image_did_modify_layer_properties(size_t) { }
    virtual void image_did_modify_layer_bitmap(size_t) { }
    virtual void image_did_modify_layer_stack() { }
    virtual void image_did_change(Gfx::IntRect const&) { }
    virtual void image_did_change_rect(Gfx::IntRect const&) { }
    virtual void image_select_layer(Layer*) { }
    virtual void image_did_change_title(String const&) { }

protected:
    virtual ~ImageClient() = default;
};

class Image : public RefCounted<Image> {
public:
    static RefPtr<Image> try_create_with_size(Gfx::IntSize const&);
    static Result<NonnullRefPtr<Image>, String> try_create_from_fd_and_close(int fd, String const& file_path);
    static Result<NonnullRefPtr<Image>, String> try_create_from_path(String const& file_path);
    static RefPtr<Image> try_create_from_bitmap(NonnullRefPtr<Gfx::Bitmap>);

    // This generates a new Bitmap with the final image (all layers composed according to their attributes.)
    RefPtr<Gfx::Bitmap> try_compose_bitmap(Gfx::BitmapFormat format) const;

    size_t layer_count() const { return m_layers.size(); }
    Layer const& layer(size_t index) const { return m_layers.at(index); }
    Layer& layer(size_t index) { return m_layers.at(index); }

    Gfx::IntSize const& size() const { return m_size; }
    Gfx::IntRect rect() const { return { {}, m_size }; }

    void add_layer(NonnullRefPtr<Layer>);
    RefPtr<Image> take_snapshot() const;
    void restore_snapshot(Image const&);

    void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const;
    Result<void, String> write_to_fd_and_close(int fd) const;
    Result<void, String> write_to_file(String const& file_path) const;
    Result<void, String> export_bmp_to_fd_and_close(int fd, bool preserve_alpha_channel);
    Result<void, String> export_png_to_fd_and_close(int fd, bool preserve_alpha_channel);

    void move_layer_to_front(Layer&);
    void move_layer_to_back(Layer&);
    void move_layer_up(Layer&);
    void move_layer_down(Layer&);
    void change_layer_index(size_t old_index, size_t new_index);
    void remove_layer(Layer&);
    void select_layer(Layer*);
    void flatten_all_layers();
    void merge_visible_layers();
    void merge_active_layer_down(Layer& layer);

    void add_client(ImageClient&);
    void remove_client(ImageClient&);

    void layer_did_modify_bitmap(Badge<Layer>, Layer const&, Gfx::IntRect const& modified_layer_rect);
    void layer_did_modify_properties(Badge<Layer>, Layer const&);

    size_t index_of(Layer const&) const;

    String const& path() const { return m_path; }
    void set_path(String);

    String const& title() const { return m_title; }
    void set_title(String);

    void flip(Gfx::Orientation orientation);
    void rotate(Gfx::RotationDirection direction);

private:
    explicit Image(Gfx::IntSize const&);

    static Result<NonnullRefPtr<Image>, String> try_create_from_pixel_paint_fd(int fd, String const& file_path);
    static Result<NonnullRefPtr<Image>, String> try_create_from_pixel_paint_path(String const& file_path);
    static Result<NonnullRefPtr<Image>, String> try_create_from_pixel_paint_file(Core::File& file, String const& file_path);

    void did_change(Gfx::IntRect const& modified_rect = {});
    void did_change_rect();
    void did_modify_layer_stack();

    String m_path;
    String m_title;

    Gfx::IntSize m_size;
    NonnullRefPtrVector<Layer> m_layers;

    HashTable<ImageClient*> m_clients;
};

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;
};

}