1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:58:11 +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) static ErrorOr<ByteBuffer> dump_bitmap(RefPtr<Gfx::Bitmap> bitmap, AK::StringView extension)
{ {
if (extension == "bmp") { if (extension == "bmp") {
Gfx::BMPWriter dumper; return Gfx::BMPWriter::encode(*bitmap);
return dumper.dump(bitmap);
} else if (extension == "png") { } else if (extension == "png") {
return Gfx::PNGWriter::encode(*bitmap); return Gfx::PNGWriter::encode(*bitmap);
} else if (extension == "qoi") { } 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_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
auto bitmap = TRY(compose_bitmap(bitmap_format)); auto bitmap = TRY(compose_bitmap(bitmap_format));
Gfx::BMPWriter dumper; auto encoded_data = TRY(Gfx::BMPWriter::encode(*bitmap));
auto encoded_data = dumper.dump(bitmap);
TRY(stream->write_entire_buffer(encoded_data)); TRY(stream->write_entire_buffer(encoded_data));
return {}; return {};
} }

View file

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

View file

@ -67,6 +67,11 @@ static ByteBuffer write_pixel_data(RefPtr<Bitmap const> bitmap, int pixel_row_da
return buffer; 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) ByteBuffer BMPWriter::compress_pixel_data(ByteBuffer const& pixel_data, BMPWriter::Compression compression)
{ {
switch (compression) { switch (compression) {

View file

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

View file

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