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

LibGfx: Change second argument of BMPWriter::dump() to Options struct

This makes it more economical to add more options here, and makes it
possible to use similar API surface for the other image writers.

(It looks like nothing currently uses this optional parameter, but
having a way to pass options to image writers seems like something
we generally want.)
This commit is contained in:
Nico Weber 2023-03-12 10:24:30 -04:00 committed by Jelle Raaijmakers
parent 74891ab656
commit 01387eb72b
2 changed files with 17 additions and 11 deletions

View file

@ -78,17 +78,18 @@ ByteBuffer BMPWriter::compress_pixel_data(ByteBuffer const& pixel_data, BMPWrite
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
ByteBuffer BMPWriter::dump(RefPtr<Bitmap const> bitmap, DibHeader dib_header) ByteBuffer BMPWriter::dump(RefPtr<Bitmap const> bitmap, Options options)
{ {
Options::DibHeader dib_header = options.dib_header;
switch (dib_header) { switch (dib_header) {
case DibHeader::Info: case Options::DibHeader::Info:
m_compression = Compression::BI_RGB; m_compression = Compression::BI_RGB;
m_bytes_per_pixel = 3; m_bytes_per_pixel = 3;
m_include_alpha_channel = false; m_include_alpha_channel = false;
break; break;
case DibHeader::V3: case Options::DibHeader::V3:
case DibHeader::V4: case Options::DibHeader::V4:
m_compression = Compression::BI_BITFIELDS; m_compression = Compression::BI_BITFIELDS;
m_bytes_per_pixel = 4; m_bytes_per_pixel = 4;
m_include_alpha_channel = true; m_include_alpha_channel = true;
@ -128,14 +129,14 @@ ByteBuffer BMPWriter::dump(RefPtr<Bitmap const> bitmap, DibHeader dib_header)
streamer.write_u32(0); // TotalColors streamer.write_u32(0); // TotalColors
streamer.write_u32(0); // ImportantColors streamer.write_u32(0); // ImportantColors
if (dib_header == DibHeader::V3 || dib_header == DibHeader::V4) { if (dib_header == Options::DibHeader::V3 || dib_header == Options::DibHeader::V4) {
streamer.write_u32(0x00ff0000); // Red bitmask streamer.write_u32(0x00ff0000); // Red bitmask
streamer.write_u32(0x0000ff00); // Green bitmask streamer.write_u32(0x0000ff00); // Green bitmask
streamer.write_u32(0x000000ff); // Blue bitmask streamer.write_u32(0x000000ff); // Blue bitmask
streamer.write_u32(0xff000000); // Alpha bitmask streamer.write_u32(0xff000000); // Alpha bitmask
} }
if (dib_header == DibHeader::V4) { if (dib_header == Options::DibHeader::V4) {
streamer.write_u32(0); // Colorspace streamer.write_u32(0); // Colorspace
for (int i = 0; i < 12; i++) { for (int i = 0; i < 12; i++) {

View file

@ -12,17 +12,22 @@ namespace Gfx {
class Bitmap; class Bitmap;
class BMPWriter { // This is not a nested struct to work around https://llvm.org/PR36684
public: struct BMPWriterOptions {
BMPWriter() = default;
enum class DibHeader : u32 { enum class DibHeader : u32 {
Info = 40, Info = 40,
V3 = 56, V3 = 56,
V4 = 108, V4 = 108,
}; };
DibHeader dib_header = DibHeader::V4;
};
ByteBuffer dump(RefPtr<Bitmap const>, DibHeader dib_header = DibHeader::V4); class BMPWriter {
public:
using Options = BMPWriterOptions;
BMPWriter() = default;
ByteBuffer dump(RefPtr<Bitmap const>, Options options = Options {});
private: private:
enum class Compression : u32 { enum class Compression : u32 {