1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:04:59 +00:00

LibGfx: Change BMPWriter API to be consistent with other image writers

This commit is contained in:
Nico Weber 2023-03-12 12:09:29 -04:00 committed by Jelle Raaijmakers
parent 01387eb72b
commit f1a3028ef1
6 changed files with 15 additions and 12 deletions

View file

@ -27,8 +27,7 @@
static ErrorOr<ByteBuffer> dump_bitmap(RefPtr<Gfx::Bitmap> bitmap, AK::StringView extension)
{
if (extension == "bmp") {
Gfx::BMPWriter dumper;
return dumper.dump(bitmap);
return Gfx::BMPWriter::encode(*bitmap);
} else if (extension == "png") {
return Gfx::PNGWriter::encode(*bitmap);
} else if (extension == "qoi") {

View file

@ -176,8 +176,7 @@ ErrorOr<void> Image::export_bmp_to_file(NonnullOwnPtr<Stream> stream, bool prese
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
auto bitmap = TRY(compose_bitmap(bitmap_format));
Gfx::BMPWriter dumper;
auto encoded_data = dumper.dump(bitmap);
auto encoded_data = TRY(Gfx::BMPWriter::encode(*bitmap));
TRY(stream->write_entire_buffer(encoded_data));
return {};
}

View file

@ -371,11 +371,9 @@ ErrorOr<void> Mandelbrot::export_image(DeprecatedString const& export_path, Imag
m_set.resize(Gfx::IntSize { 1920, 1080 });
ByteBuffer encoded_data;
switch (image_type) {
case ImageType::BMP: {
Gfx::BMPWriter dumper;
encoded_data = dumper.dump(m_set.bitmap());
case ImageType::BMP:
encoded_data = TRY(Gfx::BMPWriter::encode(m_set.bitmap()));
break;
}
case ImageType::PNG:
encoded_data = TRY(Gfx::PNGWriter::encode(m_set.bitmap()));
break;

View file

@ -67,6 +67,11 @@ static ByteBuffer write_pixel_data(RefPtr<Bitmap const> bitmap, int pixel_row_da
return buffer;
}
ErrorOr<ByteBuffer> BMPWriter::encode(Bitmap const& bitmap, Options options)
{
return BMPWriter().dump(bitmap, options);
}
ByteBuffer BMPWriter::compress_pixel_data(ByteBuffer const& pixel_data, BMPWriter::Compression compression)
{
switch (compression) {

View file

@ -25,11 +25,13 @@ struct BMPWriterOptions {
class BMPWriter {
public:
using Options = BMPWriterOptions;
BMPWriter() = default;
ByteBuffer dump(RefPtr<Bitmap const>, Options options = Options {});
static ErrorOr<ByteBuffer> encode(Bitmap const&, Options options = Options {});
private:
BMPWriter() = default;
ByteBuffer dump(RefPtr<Bitmap const>, Options options);
enum class Compression : u32 {
BI_RGB = 0,
BI_BITFIELDS = 3,

View file

@ -38,7 +38,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
ByteBuffer bytes;
if (out_path.ends_with(".bmp"sv, CaseSensitivity::CaseInsensitive)) {
bytes = Gfx::BMPWriter().dump(frame);
bytes = TRY(Gfx::BMPWriter::encode(*frame));
} else if (out_path.ends_with(".png"sv, CaseSensitivity::CaseInsensitive)) {
bytes = TRY(Gfx::PNGWriter::encode(*frame));
} else if (out_path.ends_with(".qoi"sv, CaseSensitivity::CaseInsensitive)) {