mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
PixelPaint: Use ErrorOr<T> for Image::try_compose_bitmap()
This commit is contained in:
parent
77fd4625b5
commit
9268ed9605
2 changed files with 8 additions and 14 deletions
|
@ -163,12 +163,9 @@ ErrorOr<void> Image::write_to_file(const String& file_path) const
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<Gfx::Bitmap> Image::try_compose_bitmap(Gfx::BitmapFormat format) const
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::try_compose_bitmap(Gfx::BitmapFormat format) const
|
||||||
{
|
{
|
||||||
auto bitmap_or_error = Gfx::Bitmap::try_create(format, m_size);
|
auto bitmap = TRY(Gfx::Bitmap::try_create(format, m_size));
|
||||||
if (bitmap_or_error.is_error())
|
|
||||||
return nullptr;
|
|
||||||
auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
|
|
||||||
GUI::Painter painter(bitmap);
|
GUI::Painter painter(bitmap);
|
||||||
paint_into(painter, { 0, 0, m_size.width(), m_size.height() });
|
paint_into(painter, { 0, 0, m_size.width(), m_size.height() });
|
||||||
return bitmap;
|
return bitmap;
|
||||||
|
@ -181,9 +178,10 @@ RefPtr<Gfx::Bitmap> Image::try_copy_bitmap(Selection const& selection) const
|
||||||
auto selection_rect = selection.bounding_rect();
|
auto selection_rect = selection.bounding_rect();
|
||||||
|
|
||||||
// FIXME: Add a way to only compose a certain part of the image
|
// FIXME: Add a way to only compose a certain part of the image
|
||||||
auto full_bitmap = try_compose_bitmap(Gfx::BitmapFormat::BGRA8888);
|
auto bitmap_or_error = try_compose_bitmap(Gfx::BitmapFormat::BGRA8888);
|
||||||
if (!full_bitmap)
|
if (bitmap_or_error.is_error())
|
||||||
return {};
|
return {};
|
||||||
|
auto full_bitmap = bitmap_or_error.release_value();
|
||||||
|
|
||||||
auto cropped_bitmap_or_error = full_bitmap->cropped(selection_rect);
|
auto cropped_bitmap_or_error = full_bitmap->cropped(selection_rect);
|
||||||
if (cropped_bitmap_or_error.is_error())
|
if (cropped_bitmap_or_error.is_error())
|
||||||
|
@ -199,9 +197,7 @@ ErrorOr<void> Image::export_bmp_to_fd_and_close(int fd, bool preserve_alpha_chan
|
||||||
return Error::from_errno(file->error());
|
return Error::from_errno(file->error());
|
||||||
|
|
||||||
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
|
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
|
||||||
auto bitmap = try_compose_bitmap(bitmap_format);
|
auto bitmap = TRY(try_compose_bitmap(bitmap_format));
|
||||||
if (!bitmap)
|
|
||||||
return Error::from_string_literal("Failed to allocate bitmap for encoding"sv);
|
|
||||||
|
|
||||||
Gfx::BMPWriter dumper;
|
Gfx::BMPWriter dumper;
|
||||||
auto encoded_data = dumper.dump(bitmap);
|
auto encoded_data = dumper.dump(bitmap);
|
||||||
|
@ -220,9 +216,7 @@ ErrorOr<void> Image::export_png_to_fd_and_close(int fd, bool preserve_alpha_chan
|
||||||
return Error::from_errno(file->error());
|
return Error::from_errno(file->error());
|
||||||
|
|
||||||
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
|
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
|
||||||
auto bitmap = try_compose_bitmap(bitmap_format);
|
auto bitmap = TRY(try_compose_bitmap(bitmap_format));
|
||||||
if (!bitmap)
|
|
||||||
return Error::from_string_literal("Failed to allocate bitmap for encoding"sv);
|
|
||||||
|
|
||||||
auto encoded_data = Gfx::PNGWriter::encode(*bitmap);
|
auto encoded_data = Gfx::PNGWriter::encode(*bitmap);
|
||||||
if (!file->write(encoded_data.data(), encoded_data.size()))
|
if (!file->write(encoded_data.data(), encoded_data.size()))
|
||||||
|
|
|
@ -53,7 +53,7 @@ public:
|
||||||
static RefPtr<Gfx::Bitmap> try_decode_bitmap(const ReadonlyBytes& bitmap_data);
|
static RefPtr<Gfx::Bitmap> try_decode_bitmap(const ReadonlyBytes& bitmap_data);
|
||||||
|
|
||||||
// This generates a new Bitmap with the final image (all layers composed according to their attributes.)
|
// 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;
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> try_compose_bitmap(Gfx::BitmapFormat format) const;
|
||||||
RefPtr<Gfx::Bitmap> try_copy_bitmap(Selection const&) const;
|
RefPtr<Gfx::Bitmap> try_copy_bitmap(Selection const&) const;
|
||||||
|
|
||||||
size_t layer_count() const { return m_layers.size(); }
|
size_t layer_count() const { return m_layers.size(); }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue