Ver Fonte

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

Nico Weber há 2 anos atrás
pai
commit
f1a3028ef1

+ 1 - 2
Userland/Applications/Magnifier/main.cpp

@@ -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") {

+ 1 - 2
Userland/Applications/PixelPaint/Image.cpp

@@ -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 {};
 }

+ 2 - 4
Userland/Demos/Mandelbrot/Mandelbrot.cpp

@@ -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;

+ 5 - 0
Userland/Libraries/LibGfx/BMPWriter.cpp

@@ -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) {

+ 4 - 2
Userland/Libraries/LibGfx/BMPWriter.h

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

+ 1 - 1
Userland/Utilities/image.cpp

@@ -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)) {