From 44905ed3959e43f03f9c7345b6799f5805bad7f0 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sun, 30 Apr 2023 19:17:08 -0400 Subject: [PATCH] LibGfx/PortableFormat: Write to the output stream row by row This is done by adding an intermediate buffer and flush it at the end of every row. This makes the `add_pixels` method to drop from 50% to 7% in profiles. --- .../ImageFormats/PortableFormatWriter.cpp | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) 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 {};