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

LibGfx/PortableFormat+image: Make encode take a Stream

As we directly write to the stream, we don't need to store a copy of the
entire image in memory. However, writing to a stream is heavier on the
CPU than to a ByteBuffer. This commit unfortunately makes `add_pixels`
two times slower.
This commit is contained in:
Lucas CHOLLET 2023-04-30 21:13:50 -04:00 committed by Andreas Kling
parent af6dc267d3
commit d4d3c3f262
3 changed files with 29 additions and 28 deletions

View file

@ -5,47 +5,45 @@
*/ */
#include "PortableFormatWriter.h" #include "PortableFormatWriter.h"
#include <AK/String.h> #include <AK/Stream.h>
namespace Gfx { namespace Gfx {
ErrorOr<ByteBuffer> PortableFormatWriter::encode(Bitmap const& bitmap, Options options) ErrorOr<void> PortableFormatWriter::encode(Stream& output, Bitmap const& bitmap, Options options)
{ {
ByteBuffer buffer;
// FIXME: Add support for PBM and PGM // FIXME: Add support for PBM and PGM
TRY(add_header(buffer, options, bitmap.width(), bitmap.height(), 255)); TRY(add_header(output, options, bitmap.width(), bitmap.height(), 255));
TRY(add_pixels(buffer, options, bitmap)); TRY(add_pixels(output, options, bitmap));
return buffer;
}
ErrorOr<void> PortableFormatWriter::add_header(ByteBuffer& buffer, Options const& options, u32 width, u32 height, u32 maximal_value)
{
TRY(buffer.try_append(TRY(String::formatted("P{}\n", options.format == Options::Format::ASCII ? "3"sv : "6"sv)).bytes()));
TRY(buffer.try_append(TRY(String::formatted("# {}\n", options.comment)).bytes()));
TRY(buffer.try_append(TRY(String::formatted("{} {}\n", width, height)).bytes()));
TRY(buffer.try_append(TRY(String::formatted("{}\n", maximal_value)).bytes()));
return {}; return {};
} }
ErrorOr<void> PortableFormatWriter::add_pixels(ByteBuffer& buffer, Options const& options, Bitmap const& bitmap) ErrorOr<void> PortableFormatWriter::add_header(Stream& output, Options const& options, u32 width, u32 height, u32 maximal_value)
{
TRY(output.write_formatted("P{}\n", options.format == Options::Format::ASCII ? "3"sv : "6"sv));
TRY(output.write_formatted("# {}\n", options.comment));
TRY(output.write_formatted("{} {}\n", width, height));
TRY(output.write_formatted("{}\n", maximal_value));
return {};
}
ErrorOr<void> PortableFormatWriter::add_pixels(Stream& output, Options const& options, Bitmap const& bitmap)
{ {
for (int i = 0; i < bitmap.height(); ++i) { for (int i = 0; i < bitmap.height(); ++i) {
for (int j = 0; j < bitmap.width(); ++j) { for (int j = 0; j < bitmap.width(); ++j) {
auto color = bitmap.get_pixel(j, i); auto color = bitmap.get_pixel(j, i);
if (options.format == Options::Format::ASCII) { if (options.format == Options::Format::ASCII) {
TRY(buffer.try_append(TRY(String::formatted("{} {} {}\t", color.red(), color.green(), color.blue())).bytes())); TRY(output.write_formatted("{} {} {}\t", color.red(), color.green(), color.blue()));
} else { } else {
TRY(buffer.try_append(color.red())); TRY(output.write_value(color.red()));
TRY(buffer.try_append(color.green())); TRY(output.write_value(color.green()));
TRY(buffer.try_append(color.blue())); TRY(output.write_value(color.blue()));
} }
} }
if (options.format == Options::Format::ASCII) if (options.format == Options::Format::ASCII)
TRY(buffer.try_append('\n')); TRY(output.write_value('\n'));
} }
return {}; return {};

View file

@ -26,13 +26,13 @@ class PortableFormatWriter {
public: public:
using Options = PortableFormatWriterOptions; using Options = PortableFormatWriterOptions;
static ErrorOr<ByteBuffer> encode(Bitmap const&, Options options = Options {}); static ErrorOr<void> encode(Stream&, Bitmap const&, Options options = Options {});
private: private:
PortableFormatWriter() = delete; PortableFormatWriter() = delete;
static ErrorOr<void> add_header(ByteBuffer&, Options const& options, u32 width, u32 height, u32 max_value); static ErrorOr<void> add_header(Stream&, Options const& options, u32 width, u32 height, u32 max_value);
static ErrorOr<void> add_pixels(ByteBuffer&, Options const& options, Bitmap const&); static ErrorOr<void> add_pixels(Stream&, Options const& options, Bitmap const&);
}; };
} }

View file

@ -83,6 +83,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (strip_color_profile) if (strip_color_profile)
icc_data.clear(); icc_data.clear();
auto output_stream = TRY(Core::File::open(out_path, Core::File::OpenMode::Write));
auto buffered_stream = TRY(Core::OutputBufferedFile::create(move(output_stream)));
ByteBuffer bytes; ByteBuffer bytes;
if (out_path.ends_with(".bmp"sv, CaseSensitivity::CaseInsensitive)) { if (out_path.ends_with(".bmp"sv, CaseSensitivity::CaseInsensitive)) {
bytes = TRY(Gfx::BMPWriter::encode(*frame, { .icc_data = icc_data })); bytes = TRY(Gfx::BMPWriter::encode(*frame, { .icc_data = icc_data }));
@ -90,7 +93,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
bytes = TRY(Gfx::PNGWriter::encode(*frame, { .icc_data = icc_data })); bytes = TRY(Gfx::PNGWriter::encode(*frame, { .icc_data = icc_data }));
} else if (out_path.ends_with(".ppm"sv, CaseSensitivity::CaseInsensitive)) { } else if (out_path.ends_with(".ppm"sv, CaseSensitivity::CaseInsensitive)) {
auto const format = ppm_ascii ? Gfx::PortableFormatWriter::Options::Format::ASCII : Gfx::PortableFormatWriter::Options::Format::Raw; auto const format = ppm_ascii ? Gfx::PortableFormatWriter::Options::Format::ASCII : Gfx::PortableFormatWriter::Options::Format::Raw;
bytes = TRY(Gfx::PortableFormatWriter::encode(*frame, { .format = format })); TRY(Gfx::PortableFormatWriter::encode(*buffered_stream, *frame, { .format = format }));
return 0;
} else if (out_path.ends_with(".qoi"sv, CaseSensitivity::CaseInsensitive)) { } else if (out_path.ends_with(".qoi"sv, CaseSensitivity::CaseInsensitive)) {
bytes = TRY(Gfx::QOIWriter::encode(*frame)); bytes = TRY(Gfx::QOIWriter::encode(*frame));
} else { } else {
@ -98,8 +102,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
return 1; return 1;
} }
auto output_stream = TRY(Core::File::open(out_path, Core::File::OpenMode::Write)); TRY(buffered_stream->write_until_depleted(bytes));
TRY(output_stream->write_until_depleted(bytes));
return 0; return 0;
} }