summaryrefslogtreecommitdiff
path: root/Userland/Applications/PixelPaint/main.cpp
blob: 1aae299a7290be0294e7aee4f60e3f41f56ed9ad (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "CreateNewImageDialog.h"
#include "CreateNewLayerDialog.h"
#include "FilterParams.h"
#include "Image.h"
#include "ImageEditor.h"
#include "Layer.h"
#include "LayerListWidget.h"
#include "LayerPropertiesWidget.h"
#include "PaletteWidget.h"
#include "Tool.h"
#include "ToolPropertiesWidget.h"
#include "ToolboxWidget.h"
#include <Applications/PixelPaint/PixelPaintWindowGML.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/FilePicker.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Statusbar.h>
#include <LibGUI/Toolbar.h>
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char** argv)
{
    if (pledge("stdio thread recvfd sendfd rpath unix wpath cpath", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    auto app = GUI::Application::construct(argc, argv);

    const char* image_file = nullptr;
    Core::ArgsParser args_parser;
    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");

    auto window = GUI::Window::construct();
    window->set_title("Pixel Paint");
    window->resize(800, 510);
    window->set_icon(app_icon.bitmap_for_size(16));

    auto& main_widget = window->set_main_widget<GUI::Widget>();
    main_widget.load_from_gml(pixel_paint_window_gml);

    auto& toolbox = *main_widget.find_descendant_of_type_named<PixelPaint::ToolboxWidget>("toolbox");
    auto& image_editor = *main_widget.find_descendant_of_type_named<PixelPaint::ImageEditor>("image_editor");
    image_editor.set_focus(true);

    auto& palette_widget = *main_widget.find_descendant_of_type_named<PixelPaint::PaletteWidget>("palette_widget");
    palette_widget.set_image_editor(image_editor);

    auto& layer_list_widget = *main_widget.find_descendant_of_type_named<PixelPaint::LayerListWidget>("layer_list_widget");
    layer_list_widget.on_layer_select = [&](auto* layer) {
        image_editor.set_active_layer(layer);
    };

    auto& layer_properties_widget = *main_widget.find_descendant_of_type_named<PixelPaint::LayerPropertiesWidget>("layer_properties_widget");

    auto& tool_properties_widget = *main_widget.find_descendant_of_type_named<PixelPaint::ToolPropertiesWidget>("tool_properties_widget");

    toolbox.on_tool_selection = [&](auto* tool) {
        image_editor.set_active_tool(tool);
        tool_properties_widget.set_active_tool(tool);
    };

    auto new_image_action = GUI::Action::create(
        "&New Image...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](auto&) {
            auto dialog = PixelPaint::CreateNewImageDialog::construct(window);
            if (dialog->exec() == GUI::Dialog::ExecOK) {
                auto image = PixelPaint::Image::try_create_with_size(dialog->image_size());
                auto bg_layer = PixelPaint::Layer::try_create_with_size(*image, image->size(), "Background");
                VERIFY(bg_layer);
                image->add_layer(*bg_layer);
                bg_layer->bitmap().fill(Color::White);

                image_editor.set_image(image);
                layer_list_widget.set_image(image);
                image_editor.set_active_layer(bg_layer);
            }
        },
        window);

    auto open_image_file = [&](auto& path) {
        auto image_or_error = PixelPaint::Image::try_create_from_file(path);
        if (image_or_error.is_error()) {
            GUI::MessageBox::show_error(window, String::formatted("Unable to open file: {}", path));
            return;
        }
        auto& image = *image_or_error.value();
        image_editor.set_image(image);
        layer_list_widget.set_image(&image);
    };

    auto open_image_action = GUI::CommonActions::make_open_action([&](auto&) {
        Optional<String> open_path = GUI::FilePicker::get_open_filepath(window);
        if (!open_path.has_value())
            return;
        open_image_file(open_path.value());
    });

    auto save_image_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
        if (!image_editor.image())
            return;
        auto save_path = GUI::FilePicker::get_save_filepath(window, "untitled", "pp");
        if (!save_path.has_value())
            return;
        auto result = image_editor.image()->write_to_file(save_path.value());
        if (result.is_error()) {
            GUI::MessageBox::show_error(window, String::formatted("Could not save {}: {}", save_path.value(), result.error()));
            return;
        }
    });

    auto menubar = GUI::Menubar::construct();
    auto& file_menu = menubar->add_menu("&File");

    file_menu.add_action(new_image_action);
    file_menu.add_action(open_image_action);
    file_menu.add_action(save_image_as_action);
    auto& export_submenu = file_menu.add_submenu("&Export");
    export_submenu.add_action(
        GUI::Action::create(
            "As &BMP", [&](auto&) {
                if (!image_editor.image())
                    return;
                auto save_path = GUI::FilePicker::get_save_filepath(window, "untitled", "bmp");
                if (!save_path.has_value())
                    return;
                auto result = image_editor.image()->export_bmp_to_file(save_path.value());
                if (result.is_error())
                    GUI::MessageBox::show_error(window, String::formatted("Export to BMP failed: {}", result.error()));
            },
            window));
    export_submenu.add_action(
        GUI::Action::create(
            "As &PNG", [&](auto&) {
                if (!image_editor.image())
                    return;
                auto save_path = GUI::FilePicker::get_save_filepath(window, "untitled", "png");
                if (!save_path.has_value())
                    return;
                auto result = image_editor.image()->export_bmp_to_file(save_path.value());
                if (result.is_error())
                    GUI::MessageBox::show_error(window, String::formatted("Export to PNG failed: {}", result.error()));
            },
            window));

    file_menu.add_separator();
    file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
        GUI::Application::the()->quit();
    }));

    auto& edit_menu = menubar->add_menu("&Edit");

    auto copy_action = GUI::CommonActions::make_copy_action([&](auto&) {
        VERIFY(image_editor.image());
        if (!image_editor.active_layer()) {
            dbgln("Cannot copy with no active layer selected");
            return;
        }
        auto bitmap = image_editor.active_layer()->try_copy_bitmap(image_editor.selection());
        if (!bitmap) {
            dbgln("try_copy() from Layer failed");
            return;
        }
        GUI::Clipboard::the().set_bitmap(*bitmap);
    });

    auto paste_action = GUI::CommonActions::make_paste_action([&](auto&) {
        VERIFY(image_editor.image());
        auto bitmap = GUI::Clipboard::the().bitmap();
        if (!bitmap)
            return;

        auto layer = PixelPaint::Layer::try_create_with_bitmap(*image_editor.image(), *bitmap, "Pasted layer");
        VERIFY(layer);
        image_editor.image()->add_layer(*layer);
        image_editor.set_active_layer(layer);
        image_editor.selection().clear();
    });
    GUI::Clipboard::the().on_change = [&](auto& mime_type) {
        paste_action->set_enabled(mime_type == "image/x-serenityos");
    };
    paste_action->set_enabled(GUI::Clipboard::the().mime_type() == "image/x-serenityos");

    edit_menu.add_action(copy_action);
    edit_menu.add_action(paste_action);

    auto undo_action = GUI::CommonActions::make_undo_action([&](auto&) {
        VERIFY(image_editor.image());
        image_editor.undo();
    });
    edit_menu.add_action(undo_action);

    auto redo_action = GUI::CommonActions::make_redo_action([&](auto&) {
        VERIFY(image_editor.image());
        image_editor.redo();
    });
    edit_menu.add_action(redo_action);

    edit_menu.add_separator();
    edit_menu.add_action(GUI::CommonActions::make_select_all_action([&](auto&) {
        VERIFY(image_editor.image());
        if (!image_editor.active_layer())
            return;
        image_editor.selection().set(image_editor.active_layer()->relative_rect());
    }));
    edit_menu.add_action(GUI::Action::create(
        "Clear &Selection", { Mod_Ctrl | Mod_Shift, Key_A }, [&](auto&) {
            image_editor.selection().clear();
        },
        window));

    edit_menu.add_separator();
    edit_menu.add_action(GUI::Action::create(
        "&Swap Colors", { Mod_None, Key_X }, [&](auto&) {
            auto old_primary_color = image_editor.primary_color();
            image_editor.set_primary_color(image_editor.secondary_color());
            image_editor.set_secondary_color(old_primary_color);
        },
        window));
    edit_menu.add_action(GUI::Action::create(
        "&Default Colors", { Mod_None, Key_D }, [&](auto&) {
            image_editor.set_primary_color(Color::Black);
            image_editor.set_secondary_color(Color::White);
        },
        window));

    auto& view_menu = menubar->add_menu("&View");

    auto zoom_in_action = GUI::CommonActions::make_zoom_in_action(
        [&](auto&) {
            image_editor.scale_by(0.1f);
        },
        window);

    auto zoom_out_action = GUI::CommonActions::make_zoom_out_action(
        [&](auto&) {
            image_editor.scale_by(-0.1f);
        },
        window);

    auto reset_zoom_action = GUI::CommonActions::make_reset_zoom_action(
        [&](auto&) {
            image_editor.reset_scale_and_position();
        },
        window);

    view_menu.add_action(zoom_in_action);
    view_menu.add_action(zoom_out_action);
    view_menu.add_action(reset_zoom_action);

    auto& tool_menu = menubar->add_menu("&Tool");
    toolbox.for_each_tool([&](auto& tool) {
        if (tool.action())
            tool_menu.add_action(*tool.action());
        return IterationDecision::Continue;
    });

    auto& layer_menu = menubar->add_menu("&Layer");
    layer_menu.add_action(GUI::Action::create(
        "New &Layer...", { Mod_Ctrl | Mod_Shift, Key_N }, [&](auto&) {
            auto dialog = PixelPaint::CreateNewLayerDialog::construct(image_editor.image()->size(), window);
            if (dialog->exec() == GUI::Dialog::ExecOK) {
                auto layer = PixelPaint::Layer::try_create_with_size(*image_editor.image(), dialog->layer_size(), dialog->layer_name());
                if (!layer) {
                    GUI::MessageBox::show_error(window, String::formatted("Unable to create layer with size {}", dialog->size().to_string()));
                    return;
                }
                image_editor.image()->add_layer(layer.release_nonnull());
                image_editor.layers_did_change();
            }
        },
        window));

    layer_menu.add_separator();
    layer_menu.add_action(GUI::Action::create(
        "Select &Previous Layer", { 0, Key_PageUp }, [&](auto&) {
            layer_list_widget.move_selection(1);
        },
        window));
    layer_menu.add_action(GUI::Action::create(
        "Select &Next Layer", { 0, Key_PageDown }, [&](auto&) {
            layer_list_widget.move_selection(-1);
        },
        window));
    layer_menu.add_action(GUI::Action::create(
        "Select &Top Layer", { 0, Key_Home }, [&](auto&) {
            layer_list_widget.select_top_layer();
        },
        window));
    layer_menu.add_action(GUI::Action::create(
        "Select &Bottom Layer", { 0, Key_End }, [&](auto&) {
            layer_list_widget.select_bottom_layer();
        },
        window));
    layer_menu.add_separator();
    layer_menu.add_action(GUI::Action::create(
        "Move Active Layer &Up", { Mod_Ctrl, Key_PageUp }, [&](auto&) {
            auto active_layer = image_editor.active_layer();
            if (!active_layer)
                return;
            image_editor.image()->move_layer_up(*active_layer);
        },
        window));
    layer_menu.add_action(GUI::Action::create(
        "Move Active Layer &Down", { Mod_Ctrl, Key_PageDown }, [&](auto&) {
            auto active_layer = image_editor.active_layer();
            if (!active_layer)
                return;
            image_editor.image()->move_layer_down(*active_layer);
        },
        window));
    layer_menu.add_separator();
    layer_menu.add_action(GUI::Action::create(
        "&Remove Active Layer", { Mod_Ctrl, Key_D }, [&](auto&) {
            auto active_layer = image_editor.active_layer();
            if (!active_layer)
                return;
            image_editor.image()->remove_layer(*active_layer);
            image_editor.set_active_layer(nullptr);
        },
        window));

    auto& filter_menu = menubar->add_menu("&Filter");
    auto& spatial_filters_menu = filter_menu.add_submenu("&Spatial");

    auto& edge_detect_submenu = spatial_filters_menu.add_submenu("&Edge Detect");
    edge_detect_submenu.add_action(GUI::Action::create("Laplacian (&Cardinal)", [&](auto&) {
        if (auto* layer = image_editor.active_layer()) {
            Gfx::LaplacianFilter filter;
            if (auto parameters = PixelPaint::FilterParameters<Gfx::LaplacianFilter>::get(false)) {
                filter.apply(layer->bitmap(), layer->rect(), layer->bitmap(), layer->rect(), *parameters);
                image_editor.did_complete_action();
            }
        }
    }));
    edge_detect_submenu.add_action(GUI::Action::create("Laplacian (&Diagonal)", [&](auto&) {
        if (auto* layer = image_editor.active_layer()) {
            Gfx::LaplacianFilter filter;
            if (auto parameters = PixelPaint::FilterParameters<Gfx::LaplacianFilter>::get(true)) {
                filter.apply(layer->bitmap(), layer->rect(), layer->bitmap(), layer->rect(), *parameters);
                image_editor.did_complete_action();
            }
        }
    }));
    auto& blur_submenu = spatial_filters_menu.add_submenu("&Blur and Sharpen");
    blur_submenu.add_action(GUI::Action::create("&Gaussian Blur (3x3)", [&](auto&) {
        if (auto* layer = image_editor.active_layer()) {
            Gfx::SpatialGaussianBlurFilter<3> filter;
            if (auto parameters = PixelPaint::FilterParameters<Gfx::SpatialGaussianBlurFilter<3>>::get()) {
                filter.apply(layer->bitmap(), layer->rect(), layer->bitmap(), layer->rect(), *parameters);
                image_editor.did_complete_action();
            }
        }
    }));
    blur_submenu.add_action(GUI::Action::create("G&aussian Blur (5x5)", [&](auto&) {
        if (auto* layer = image_editor.active_layer()) {
            Gfx::SpatialGaussianBlurFilter<5> filter;
            if (auto parameters = PixelPaint::FilterParameters<Gfx::SpatialGaussianBlurFilter<5>>::get()) {
                filter.apply(layer->bitmap(), layer->rect(), layer->bitmap(), layer->rect(), *parameters);
                image_editor.did_complete_action();
            }
        }
    }));
    blur_submenu.add_action(GUI::Action::create("&Box Blur (3x3)", [&](auto&) {
        if (auto* layer = image_editor.active_layer()) {
            Gfx::BoxBlurFilter<3> filter;
            if (auto parameters = PixelPaint::FilterParameters<Gfx::BoxBlurFilter<3>>::get()) {
                filter.apply(layer->bitmap(), layer->rect(), layer->bitmap(), layer->rect(), *parameters);
                image_editor.did_complete_action();
            }
        }
    }));
    blur_submenu.add_action(GUI::Action::create("B&ox Blur (5x5)", [&](auto&) {
        if (auto* layer = image_editor.active_layer()) {
            Gfx::BoxBlurFilter<5> filter;
            if (auto parameters = PixelPaint::FilterParameters<Gfx::BoxBlurFilter<5>>::get()) {
                filter.apply(layer->bitmap(), layer->rect(), layer->bitmap(), layer->rect(), *parameters);
                image_editor.did_complete_action();
            }
        }
    }));
    blur_submenu.add_action(GUI::Action::create("&Sharpen", [&](auto&) {
        if (auto* layer = image_editor.active_layer()) {
            Gfx::SharpenFilter filter;
            if (auto parameters = PixelPaint::FilterParameters<Gfx::SharpenFilter>::get()) {
                filter.apply(layer->bitmap(), layer->rect(), layer->bitmap(), layer->rect(), *parameters);
                image_editor.did_complete_action();
            }
        }
    }));

    spatial_filters_menu.add_separator();
    spatial_filters_menu.add_action(GUI::Action::create("Generic 5x5 &Convolution", [&](auto&) {
        if (auto* layer = image_editor.active_layer()) {
            Gfx::GenericConvolutionFilter<5> filter;
            if (auto parameters = PixelPaint::FilterParameters<Gfx::GenericConvolutionFilter<5>>::get(window)) {
                filter.apply(layer->bitmap(), layer->rect(), layer->bitmap(), layer->rect(), *parameters);
                image_editor.did_complete_action();
            }
        }
    }));

    auto& help_menu = menubar->add_menu("&Help");
    help_menu.add_action(GUI::CommonActions::make_about_action("Pixel Paint", app_icon, window));

    window->set_menubar(move(menubar));

    auto& toolbar = *main_widget.find_descendant_of_type_named<GUI::Toolbar>("toolbar");
    toolbar.add_action(new_image_action);
    toolbar.add_action(open_image_action);
    toolbar.add_action(save_image_as_action);
    toolbar.add_separator();
    toolbar.add_action(copy_action);
    toolbar.add_action(paste_action);
    toolbar.add_action(undo_action);
    toolbar.add_action(redo_action);
    toolbar.add_separator();
    toolbar.add_action(zoom_in_action);
    toolbar.add_action(zoom_out_action);
    toolbar.add_action(reset_zoom_action);

    image_editor.on_active_layer_change = [&](auto* layer) {
        layer_list_widget.set_selected_layer(layer);
        layer_properties_widget.set_layer(layer);
    };

    auto image_file_real_path = Core::File::real_path_for(image_file);
    if (Core::File::exists(image_file_real_path)) {
        open_image_file(image_file_real_path);
    } else {
        auto image = PixelPaint::Image::try_create_with_size({ 480, 360 });

        auto bg_layer = PixelPaint::Layer::try_create_with_size(*image, image->size(), "Background");
        VERIFY(bg_layer);
        image->add_layer(*bg_layer);
        bg_layer->bitmap().fill(Color::White);

        layer_list_widget.set_image(image);

        image_editor.set_image(image);
        image_editor.set_active_layer(bg_layer);
    }

    auto& statusbar = *main_widget.find_descendant_of_type_named<GUI::Statusbar>("statusbar");

    app->on_action_enter = [&statusbar](GUI::Action& action) {
        auto text = action.status_tip();
        if (text.is_empty())
            text = Gfx::parse_ampersand_string(action.text());
        statusbar.set_override_text(move(text));
    };

    app->on_action_leave = [&statusbar](GUI::Action&) {
        statusbar.set_override_text({});
    };

    window->show();
    return app->exec();
}