diff --git a/Userland/Libraries/LibGfx/ImageFormats/PortableFormatWriter.cpp b/Userland/Libraries/LibGfx/ImageFormats/PortableFormatWriter.cpp index 80b6218bed..018b2e45d1 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PortableFormatWriter.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PortableFormatWriter.cpp @@ -5,6 +5,7 @@ */ #include "PortableFormatWriter.h" +#include #include namespace Gfx { @@ -31,19 +32,30 @@ ErrorOr PortableFormatWriter::add_header(Stream& output, Options const& op ErrorOr PortableFormatWriter::add_pixels(Stream& output, Options const& options, Bitmap const& bitmap) { + if (options.format == Options::Format::Raw) { + auto row = TRY(FixedArray::create(bitmap.width() * 3ul)); + + for (int i = 0; i < bitmap.height(); ++i) { + for (int j = 0; j < bitmap.width(); ++j) { + auto const color = bitmap.get_pixel(j, i); + + row[j * 3 + 0] = color.red(); + row[j * 3 + 1] = color.green(); + row[j * 3 + 2] = color.blue(); + } + + TRY(output.write_until_depleted(row.span())); + } + + return {}; + } + for (int i = 0; i < bitmap.height(); ++i) { for (int j = 0; j < bitmap.width(); ++j) { auto color = bitmap.get_pixel(j, i); - if (options.format == Options::Format::ASCII) { - TRY(output.write_formatted("{} {} {}\t", color.red(), color.green(), color.blue())); - } else { - TRY(output.write_value(color.red())); - TRY(output.write_value(color.green())); - TRY(output.write_value(color.blue())); - } + TRY(output.write_formatted("{} {} {}\t", color.red(), color.green(), color.blue())); } - if (options.format == Options::Format::ASCII) - TRY(output.write_value('\n')); + TRY(output.write_value('\n')); } return {};