summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorMarcus Nilsson <brainbomb@gmail.com>2021-07-04 12:34:10 +0200
committerAndreas Kling <kling@serenityos.org>2021-07-05 00:43:00 +0200
commit2183d01eb0c2ee9fd5db9af33b4f201586e5cd30 (patch)
treebfbe10e6cc9afc0ae530d3489d8e8ff6087e0c3d /Userland
parent8324ffefe707c83ce8a952d779d33edcc38d24fe (diff)
downloadserenity-2183d01eb0c2ee9fd5db9af33b4f201586e5cd30.zip
PixelPaint: Ask to preserve transparency when exporting
Previously the alpha channel was thrown away when exporting to BMP or PNG in PixelPaint, instead let the user decide.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Applications/PixelPaint/Image.cpp14
-rw-r--r--Userland/Applications/PixelPaint/Image.h6
-rw-r--r--Userland/Applications/PixelPaint/main.cpp6
3 files changed, 15 insertions, 11 deletions
diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp
index f9f93c8c43..33ed9ff84a 100644
--- a/Userland/Applications/PixelPaint/Image.cpp
+++ b/Userland/Applications/PixelPaint/Image.cpp
@@ -192,9 +192,9 @@ Result<void, String> Image::write_to_file(const String& file_path) const
return {};
}
-RefPtr<Gfx::Bitmap> Image::try_compose_bitmap() const
+RefPtr<Gfx::Bitmap> Image::try_compose_bitmap(Gfx::BitmapFormat format) const
{
- auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, m_size);
+ auto bitmap = Gfx::Bitmap::create(format, m_size);
if (!bitmap)
return nullptr;
GUI::Painter painter(*bitmap);
@@ -202,13 +202,14 @@ RefPtr<Gfx::Bitmap> Image::try_compose_bitmap() const
return bitmap;
}
-Result<void, String> Image::export_bmp_to_file(String const& file_path)
+Result<void, String> Image::export_bmp_to_file(String const& file_path, bool preserve_alpha_channel)
{
auto file_or_error = Core::File::open(file_path, (Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate));
if (file_or_error.is_error())
return file_or_error.error();
- auto bitmap = try_compose_bitmap();
+ auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
+ auto bitmap = try_compose_bitmap(bitmap_format);
if (!bitmap)
return String { "Failed to allocate bitmap for encoding"sv };
@@ -222,13 +223,14 @@ Result<void, String> Image::export_bmp_to_file(String const& file_path)
return {};
}
-Result<void, String> Image::export_png_to_file(String const& file_path)
+Result<void, String> Image::export_png_to_file(String const& file_path, bool preserve_alpha_channel)
{
auto file_or_error = Core::File::open(file_path, (Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate));
if (file_or_error.is_error())
return file_or_error.error();
- auto bitmap = try_compose_bitmap();
+ auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
+ auto bitmap = try_compose_bitmap(bitmap_format);
if (!bitmap)
return String { "Failed to allocate bitmap for encoding"sv };
diff --git a/Userland/Applications/PixelPaint/Image.h b/Userland/Applications/PixelPaint/Image.h
index a5474a6958..65aad4726b 100644
--- a/Userland/Applications/PixelPaint/Image.h
+++ b/Userland/Applications/PixelPaint/Image.h
@@ -43,7 +43,7 @@ public:
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() const;
+ 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); }
@@ -58,8 +58,8 @@ public:
void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const;
Result<void, String> write_to_file(String const& file_path) const;
- Result<void, String> export_bmp_to_file(String const& file_path);
- Result<void, String> export_png_to_file(String const& file_path);
+ 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);
void move_layer_to_front(Layer&);
void move_layer_to_back(Layer&);
diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp
index bd2fc9d9a6..27167d347c 100644
--- a/Userland/Applications/PixelPaint/main.cpp
+++ b/Userland/Applications/PixelPaint/main.cpp
@@ -157,7 +157,8 @@ int main(int argc, char** argv)
auto save_path = GUI::FilePicker::get_save_filepath(window, "untitled", "bmp");
if (!save_path.has_value())
return;
- auto result = editor->image().export_bmp_to_file(save_path.value());
+ auto preserve_alpha_channel = GUI::MessageBox::show(window, "Do you wish to preserve transparency?", "Preserve transparency?", GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
+ auto result = editor->image().export_bmp_to_file(save_path.value(), preserve_alpha_channel == GUI::MessageBox::ExecYes);
if (result.is_error())
GUI::MessageBox::show_error(window, String::formatted("Export to BMP failed: {}", result.error()));
},
@@ -169,7 +170,8 @@ int main(int argc, char** argv)
auto save_path = GUI::FilePicker::get_save_filepath(window, "untitled", "png");
if (!save_path.has_value())
return;
- auto result = editor->image().export_bmp_to_file(save_path.value());
+ auto preserve_alpha_channel = GUI::MessageBox::show(window, "Do you wish to preserve transparency?", "Preserve transparency?", GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
+ auto result = editor->image().export_png_to_file(save_path.value(), preserve_alpha_channel == GUI::MessageBox::ExecYes);
if (result.is_error())
GUI::MessageBox::show_error(window, String::formatted("Export to PNG failed: {}", result.error()));
},