mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:17:45 +00:00
LibGfx: Make BMPWriter::dump() return ErrorOr
...and use TRY() consistently in BMPWriter.cpp
This commit is contained in:
parent
a2ccf38f11
commit
c587ada084
2 changed files with 7 additions and 16 deletions
|
@ -42,14 +42,10 @@ private:
|
|||
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();
|
||||
auto buffer_result = ByteBuffer::create_uninitialized(image_size);
|
||||
if (buffer_result.is_error())
|
||||
return {};
|
||||
|
||||
auto buffer = buffer_result.release_value();
|
||||
auto buffer = TRY(ByteBuffer::create_uninitialized(image_size));
|
||||
|
||||
int current_row = 0;
|
||||
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();
|
||||
}
|
||||
|
||||
ByteBuffer BMPWriter::dump(Bitmap const& bitmap, Options options)
|
||||
ErrorOr<ByteBuffer> BMPWriter::dump(Bitmap const& bitmap, Options options)
|
||||
{
|
||||
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 image_size = pixel_row_data_size * bitmap.height();
|
||||
auto buffer_result = ByteBuffer::create_uninitialized(pixel_data_offset);
|
||||
if (buffer_result.is_error())
|
||||
return {};
|
||||
auto buffer = TRY(ByteBuffer::create_uninitialized(pixel_data_offset));
|
||||
|
||||
auto buffer = buffer_result.release_value();
|
||||
|
||||
auto pixel_data = write_pixel_data(bitmap, pixel_row_data_size, m_bytes_per_pixel, m_include_alpha_channel);
|
||||
auto pixel_data = TRY(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);
|
||||
|
||||
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())
|
||||
dbgln("Failed to write {} bytes of pixel data to buffer: {}", pixel_data.size(), result.error());
|
||||
TRY(buffer.try_append(pixel_data.data(), pixel_data.size()));
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ public:
|
|||
private:
|
||||
BMPWriter() = default;
|
||||
|
||||
ByteBuffer dump(Bitmap const&, Options options);
|
||||
ErrorOr<ByteBuffer> dump(Bitmap const&, Options options);
|
||||
|
||||
enum class Compression : u32 {
|
||||
BI_RGB = 0,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue