1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:47:34 +00:00

LibGfx: Propagate errors from serializing bitmaps

We essentially just end up moving `release_value_but_fixme_...` one
layer down, but it makes adding more fallible operations in the
serialization function more comfortable.
This commit is contained in:
Tim Schumacher 2023-02-08 18:21:41 +01:00 committed by Linus Groh
parent 81863eaf57
commit 6ea3c1659a
4 changed files with 8 additions and 9 deletions

View file

@ -84,7 +84,7 @@ void DragOperation::set_bitmap(Gfx::Bitmap const* bitmap)
if (!m_mime_data)
m_mime_data = Core::MimeData::construct();
if (bitmap)
m_mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer());
m_mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer().release_value_but_fixme_should_propagate_errors());
}
void DragOperation::set_data(DeprecatedString const& data_type, DeprecatedString const& data)
{

View file

@ -128,7 +128,7 @@ RefPtr<Core::MimeData> Model::mime_data(ModelSelection const& selection) const
mime_data->set_data(drag_data_type(), data_builder.to_byte_buffer());
mime_data->set_text(text_builder.to_deprecated_string());
if (bitmap)
mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer());
mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer().release_value_but_fixme_should_propagate_errors());
return mime_data;
}

View file

@ -256,10 +256,9 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::create_from_serialized_bytes(ReadonlyByte
return bitmap;
}
ByteBuffer Bitmap::serialize_to_byte_buffer() const
ErrorOr<ByteBuffer> Bitmap::serialize_to_byte_buffer() const
{
// FIXME: Somehow handle possible OOM situation here.
auto buffer = ByteBuffer::create_uninitialized(sizeof(size_t) + 4 * sizeof(unsigned) + sizeof(BitmapFormat) + sizeof(ARGB32) * palette_size(m_format) + size_in_bytes()).release_value_but_fixme_should_propagate_errors();
auto buffer = TRY(ByteBuffer::create_uninitialized(sizeof(size_t) + 4 * sizeof(unsigned) + sizeof(BitmapFormat) + sizeof(ARGB32) * palette_size(m_format) + size_in_bytes()));
DeprecatedOutputMemoryStream stream { buffer };
auto write = [&]<typename T>(T value) {
@ -271,17 +270,17 @@ ByteBuffer Bitmap::serialize_to_byte_buffer() const
auto palette = palette_to_vector();
if (!write(size_in_bytes()) || !write((unsigned)size().width()) || !write((unsigned)size().height()) || !write((unsigned)scale()) || !write(m_format) || !write((unsigned)palette.size()))
return {};
return Error::from_string_literal("Failed to write serialized image metadata to the buffer");
for (auto& p : palette) {
if (!write(p))
return {};
return Error::from_string_literal("Failed to write serialized palette to the buffer");
}
auto size = size_in_bytes();
VERIFY(stream.remaining() == size);
if (stream.write({ scanline(0), size }) != size)
return {};
return Error::from_string_literal("Failed to write serialized image data to the buffer");
return buffer;
}

View file

@ -121,7 +121,7 @@ public:
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scaled(float sx, float sy) const;
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> cropped(Gfx::IntRect, Optional<BitmapFormat> new_bitmap_format = {}) const;
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap_backed_by_anonymous_buffer() const;
[[nodiscard]] ByteBuffer serialize_to_byte_buffer() const;
[[nodiscard]] ErrorOr<ByteBuffer> serialize_to_byte_buffer() const;
[[nodiscard]] ShareableBitmap to_shareable_bitmap() const;