diff options
author | EWouters <6179932+EWouters@users.noreply.github.com> | 2022-04-13 19:04:22 +0200 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2022-04-20 08:19:25 -0400 |
commit | d3bc3da8732a1a03abda8cf2fc5b8067db2a7ced (patch) | |
tree | 973bfac8eaa66ab6e16b36a4b798e77c4232b854 /Userland/Demos/Mandelbrot | |
parent | 65d4fb7649db44ec362e353738f1e7d8ea5f2cdc (diff) | |
download | serenity-d3bc3da8732a1a03abda8cf2fc5b8067db2a7ced.zip |
Mandelbrot: Add BMP and QOI image formats as options for export
Inspired by #13558 I added the option to export as QOI to the
Mandelbrot demo. I added BMP as well for completeness.
Diffstat (limited to 'Userland/Demos/Mandelbrot')
-rw-r--r-- | Userland/Demos/Mandelbrot/Mandelbrot.cpp | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/Userland/Demos/Mandelbrot/Mandelbrot.cpp b/Userland/Demos/Mandelbrot/Mandelbrot.cpp index 7243314b47..9f2ed3099e 100644 --- a/Userland/Demos/Mandelbrot/Mandelbrot.cpp +++ b/Userland/Demos/Mandelbrot/Mandelbrot.cpp @@ -16,8 +16,10 @@ #include <LibGUI/Painter.h> #include <LibGUI/Widget.h> #include <LibGUI/Window.h> +#include <LibGfx/BMPWriter.h> #include <LibGfx/Bitmap.h> #include <LibGfx/PNGWriter.h> +#include <LibGfx/QOIWriter.h> #include <LibMain/Main.h> #include <unistd.h> @@ -206,10 +208,16 @@ private: } }; +enum class ImageType { + BMP, + PNG, + QOI +}; + class Mandelbrot : public GUI::Frame { C_OBJECT(Mandelbrot) - void export_image(String const& export_path); + void export_image(String const& export_path, ImageType image_type); enum class Zoom { In, @@ -358,17 +366,32 @@ void Mandelbrot::resize_event(GUI::ResizeEvent& event) m_set.resize(event.size()); } -void Mandelbrot::export_image(String const& export_path) +void Mandelbrot::export_image(String const& export_path, ImageType image_type) { m_set.resize(Gfx::IntSize { 1920, 1080 }); - auto png = Gfx::PNGWriter::encode(m_set.bitmap()); + ByteBuffer encoded_data; + switch (image_type) { + case ImageType::BMP: { + Gfx::BMPWriter dumper; + encoded_data = dumper.dump(m_set.bitmap()); + break; + } + case ImageType::PNG: + encoded_data = Gfx::PNGWriter::encode(m_set.bitmap()); + break; + case ImageType::QOI: + encoded_data = Gfx::QOIWriter::encode(m_set.bitmap()); + break; + default: + VERIFY_NOT_REACHED(); + } m_set.resize(size()); auto file = fopen(export_path.characters(), "wb"); if (!file) { GUI::MessageBox::show(window(), String::formatted("Could not open '{}' for writing.", export_path), "Mandelbrot", GUI::MessageBox::Type::Error); return; } - fwrite(png.data(), 1, png.size(), file); + fwrite(encoded_data.data(), 1, encoded_data.size(), file); fclose(file); GUI::MessageBox::show(window(), "Image was successfully exported.", "Mandelbrot", GUI::MessageBox::Type::Information); } @@ -392,13 +415,33 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) auto mandelbrot = TRY(window->try_set_main_widget<Mandelbrot>()); auto file_menu = TRY(window->try_add_menu("&File")); - TRY(file_menu->try_add_action(GUI::Action::create("&Export...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/save.png").release_value_but_fixme_should_propagate_errors(), + + auto& export_submenu = file_menu->add_submenu("&Export"); + + TRY(export_submenu.try_add_action(GUI::Action::create("As &BMP", + [&](GUI::Action&) { + Optional<String> export_path = GUI::FilePicker::get_save_filepath(window, "untitled", "bmp"); + if (!export_path.has_value()) + return; + mandelbrot->export_image(export_path.value(), ImageType::BMP); + }))); + TRY(export_submenu.try_add_action(GUI::Action::create("As &PNG", { Mod_Ctrl | Mod_Shift, Key_S }, [&](GUI::Action&) { Optional<String> export_path = GUI::FilePicker::get_save_filepath(window, "untitled", "png"); if (!export_path.has_value()) return; - mandelbrot->export_image(export_path.value()); + mandelbrot->export_image(export_path.value(), ImageType::PNG); + }))); + TRY(export_submenu.try_add_action(GUI::Action::create("As &QOI", + [&](GUI::Action&) { + Optional<String> export_path = GUI::FilePicker::get_save_filepath(window, "untitled", "qoi"); + if (!export_path.has_value()) + return; + mandelbrot->export_image(export_path.value(), ImageType::QOI); }))); + + export_submenu.set_icon(TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/save.png"))); + TRY(file_menu->try_add_separator()); TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }))); |