mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:07:45 +00:00
image: Move image saving code into helper function
No behavior change.
This commit is contained in:
parent
0681df7f3b
commit
653b614611
1 changed files with 28 additions and 22 deletions
|
@ -77,6 +77,33 @@ static ErrorOr<OwnPtr<Core::MappedFile>> convert_image_profile(LoadedImage& imag
|
||||||
return icc_file;
|
return icc_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ErrorOr<void> save_image(LoadedImage& image, StringView out_path, bool ppm_ascii, u8 jpeg_quality)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
if (out_path.ends_with(".bmp"sv, CaseSensitivity::CaseInsensitive)) {
|
||||||
|
bytes = TRY(Gfx::BMPWriter::encode(*image.bitmap, { .icc_data = image.icc_data }));
|
||||||
|
} else if (out_path.ends_with(".png"sv, CaseSensitivity::CaseInsensitive)) {
|
||||||
|
bytes = TRY(Gfx::PNGWriter::encode(*image.bitmap, { .icc_data = image.icc_data }));
|
||||||
|
} 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;
|
||||||
|
TRY(Gfx::PortableFormatWriter::encode(*buffered_stream, *image.bitmap, { .format = format }));
|
||||||
|
return {};
|
||||||
|
} else if (out_path.ends_with(".jpg"sv, CaseSensitivity::CaseInsensitive) || out_path.ends_with(".jpeg"sv, CaseSensitivity::CaseInsensitive)) {
|
||||||
|
TRY(Gfx::JPEGWriter::encode(*buffered_stream, *image.bitmap, { .quality = jpeg_quality }));
|
||||||
|
return {};
|
||||||
|
} else if (out_path.ends_with(".qoi"sv, CaseSensitivity::CaseInsensitive)) {
|
||||||
|
bytes = TRY(Gfx::QOIWriter::encode(*image.bitmap));
|
||||||
|
} else {
|
||||||
|
return Error::from_string_view("can only write .bmp, .png, .ppm, and .qoi"sv);
|
||||||
|
}
|
||||||
|
|
||||||
|
TRY(buffered_stream->write_until_depleted(bytes));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
|
@ -147,28 +174,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
if (no_output)
|
if (no_output)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
auto output_stream = TRY(Core::File::open(out_path, Core::File::OpenMode::Write));
|
TRY(save_image(image, out_path, ppm_ascii, quality));
|
||||||
auto buffered_stream = TRY(Core::OutputBufferedFile::create(move(output_stream)));
|
|
||||||
|
|
||||||
ByteBuffer bytes;
|
|
||||||
if (out_path.ends_with(".bmp"sv, CaseSensitivity::CaseInsensitive)) {
|
|
||||||
bytes = TRY(Gfx::BMPWriter::encode(*image.bitmap, { .icc_data = image.icc_data }));
|
|
||||||
} else if (out_path.ends_with(".png"sv, CaseSensitivity::CaseInsensitive)) {
|
|
||||||
bytes = TRY(Gfx::PNGWriter::encode(*image.bitmap, { .icc_data = image.icc_data }));
|
|
||||||
} 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;
|
|
||||||
TRY(Gfx::PortableFormatWriter::encode(*buffered_stream, *image.bitmap, { .format = format }));
|
|
||||||
return 0;
|
|
||||||
} else if (out_path.ends_with(".jpg"sv, CaseSensitivity::CaseInsensitive) || out_path.ends_with(".jpeg"sv, CaseSensitivity::CaseInsensitive)) {
|
|
||||||
TRY(Gfx::JPEGWriter::encode(*buffered_stream, *image.bitmap, { .quality = quality }));
|
|
||||||
return 0;
|
|
||||||
} else if (out_path.ends_with(".qoi"sv, CaseSensitivity::CaseInsensitive)) {
|
|
||||||
bytes = TRY(Gfx::QOIWriter::encode(*image.bitmap));
|
|
||||||
} else {
|
|
||||||
return Error::from_string_view("can only write .bmp, .png, .ppm, and .qoi"sv);
|
|
||||||
}
|
|
||||||
|
|
||||||
TRY(buffered_stream->write_until_depleted(bytes));
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue