1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 19:57:45 +00:00

LibGfx: Make BMPWriter::dump() return ErrorOr

...and use TRY() consistently in BMPWriter.cpp
This commit is contained in:
Nico Weber 2023-03-12 13:22:19 -04:00 committed by Linus Groh
parent a2ccf38f11
commit c587ada084
2 changed files with 7 additions and 16 deletions

View file

@ -42,14 +42,10 @@ private:
u8* m_data; u8* m_data;
}; };
static ByteBuffer write_pixel_data(Bitmap const& bitmap, int pixel_row_data_size, int bytes_per_pixel, bool include_alpha_channel) static ErrorOr<ByteBuffer> write_pixel_data(Bitmap const& bitmap, int pixel_row_data_size, int bytes_per_pixel, bool include_alpha_channel)
{ {
int image_size = pixel_row_data_size * bitmap.height(); int image_size = pixel_row_data_size * bitmap.height();
auto buffer_result = ByteBuffer::create_uninitialized(image_size); auto buffer = TRY(ByteBuffer::create_uninitialized(image_size));
if (buffer_result.is_error())
return {};
auto buffer = buffer_result.release_value();
int current_row = 0; int current_row = 0;
for (int y = bitmap.physical_height() - 1; y >= 0; --y) { for (int y = bitmap.physical_height() - 1; y >= 0; --y) {
@ -83,7 +79,7 @@ ByteBuffer BMPWriter::compress_pixel_data(ByteBuffer pixel_data, BMPWriter::Comp
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
ByteBuffer BMPWriter::dump(Bitmap const& bitmap, Options options) ErrorOr<ByteBuffer> BMPWriter::dump(Bitmap const& bitmap, Options options)
{ {
Options::DibHeader dib_header = options.dib_header; Options::DibHeader dib_header = options.dib_header;
@ -105,13 +101,9 @@ ByteBuffer BMPWriter::dump(Bitmap const& bitmap, Options options)
int pixel_row_data_size = (m_bytes_per_pixel * 8 * bitmap.width() + 31) / 32 * 4; int pixel_row_data_size = (m_bytes_per_pixel * 8 * bitmap.width() + 31) / 32 * 4;
int image_size = pixel_row_data_size * bitmap.height(); int image_size = pixel_row_data_size * bitmap.height();
auto buffer_result = ByteBuffer::create_uninitialized(pixel_data_offset); auto buffer = TRY(ByteBuffer::create_uninitialized(pixel_data_offset));
if (buffer_result.is_error())
return {};
auto buffer = buffer_result.release_value(); auto pixel_data = TRY(write_pixel_data(bitmap, pixel_row_data_size, m_bytes_per_pixel, m_include_alpha_channel));
auto pixel_data = write_pixel_data(bitmap, pixel_row_data_size, m_bytes_per_pixel, m_include_alpha_channel);
pixel_data = compress_pixel_data(move(pixel_data), m_compression); pixel_data = compress_pixel_data(move(pixel_data), m_compression);
size_t file_size = pixel_data_offset + pixel_data.size(); size_t file_size = pixel_data_offset + pixel_data.size();
@ -149,8 +141,7 @@ ByteBuffer BMPWriter::dump(Bitmap const& bitmap, Options options)
} }
} }
if (auto result = buffer.try_append(pixel_data.data(), pixel_data.size()); result.is_error()) TRY(buffer.try_append(pixel_data.data(), pixel_data.size()));
dbgln("Failed to write {} bytes of pixel data to buffer: {}", pixel_data.size(), result.error());
return buffer; return buffer;
} }

View file

@ -30,7 +30,7 @@ public:
private: private:
BMPWriter() = default; BMPWriter() = default;
ByteBuffer dump(Bitmap const&, Options options); ErrorOr<ByteBuffer> dump(Bitmap const&, Options options);
enum class Compression : u32 { enum class Compression : u32 {
BI_RGB = 0, BI_RGB = 0,