2023-03-12 00:33:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <LibCore/MappedFile.h>
|
|
|
|
#include <LibGfx/BMPWriter.h>
|
|
|
|
#include <LibGfx/ImageDecoder.h>
|
|
|
|
#include <LibGfx/PNGWriter.h>
|
2023-03-13 02:22:43 +00:00
|
|
|
#include <LibGfx/PortableFormatWriter.h>
|
2023-03-12 00:33:17 +00:00
|
|
|
#include <LibGfx/QOIWriter.h>
|
|
|
|
|
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|
|
|
{
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
|
|
|
|
StringView in_path;
|
|
|
|
args_parser.add_positional_argument(in_path, "Path to input image file", "FILE");
|
|
|
|
|
|
|
|
StringView out_path;
|
|
|
|
args_parser.add_option(out_path, "Path to output image file", "output", 'o', "FILE");
|
|
|
|
|
2023-03-13 02:30:40 +00:00
|
|
|
bool ppm_ascii;
|
|
|
|
args_parser.add_option(ppm_ascii, "Convert to a PPM in ASCII", "ppm-ascii", {});
|
|
|
|
|
2023-03-16 08:10:48 +00:00
|
|
|
StringView assign_color_profile_path;
|
|
|
|
args_parser.add_option(assign_color_profile_path, "Load color profile from file and assign it to output image", "assign-color-profile", {}, "FILE");
|
|
|
|
|
2023-03-16 08:02:31 +00:00
|
|
|
bool strip_color_profile;
|
|
|
|
args_parser.add_option(strip_color_profile, "Do not write color profile to output", "strip-color-profile", {});
|
|
|
|
|
2023-03-12 00:33:17 +00:00
|
|
|
args_parser.parse(arguments);
|
|
|
|
|
|
|
|
if (out_path.is_empty()) {
|
|
|
|
warnln("-o is required");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto file = TRY(Core::MappedFile::map(in_path));
|
|
|
|
auto decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(file->bytes());
|
2023-03-16 08:21:01 +00:00
|
|
|
if (!decoder) {
|
|
|
|
warnln("Failed to decode input file '{}'", in_path);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-12 00:33:17 +00:00
|
|
|
|
|
|
|
// This uses ImageDecoder instead of Bitmap::load_from_file() to have more control
|
|
|
|
// over selecting a frame, access color profile data, and so on in the future.
|
|
|
|
auto frame = TRY(decoder->frame(0)).image;
|
2023-03-15 10:13:34 +00:00
|
|
|
Optional<ReadonlyBytes> icc_data = TRY(decoder->icc_data());
|
2023-03-12 00:33:17 +00:00
|
|
|
|
2023-03-16 08:10:48 +00:00
|
|
|
RefPtr<Core::MappedFile> icc_file;
|
|
|
|
if (!assign_color_profile_path.is_empty()) {
|
|
|
|
icc_file = TRY(Core::MappedFile::map(assign_color_profile_path));
|
|
|
|
icc_data = icc_file->bytes();
|
|
|
|
}
|
|
|
|
|
2023-03-16 08:02:31 +00:00
|
|
|
if (strip_color_profile)
|
|
|
|
icc_data.clear();
|
|
|
|
|
2023-03-12 00:33:17 +00:00
|
|
|
ByteBuffer bytes;
|
|
|
|
if (out_path.ends_with(".bmp"sv, CaseSensitivity::CaseInsensitive)) {
|
2023-03-15 11:09:32 +00:00
|
|
|
bytes = TRY(Gfx::BMPWriter::encode(*frame, { .icc_data = icc_data }));
|
2023-03-12 00:33:17 +00:00
|
|
|
} else if (out_path.ends_with(".png"sv, CaseSensitivity::CaseInsensitive)) {
|
2023-03-15 10:13:34 +00:00
|
|
|
bytes = TRY(Gfx::PNGWriter::encode(*frame, { .icc_data = icc_data }));
|
2023-03-13 02:22:43 +00:00
|
|
|
} else if (out_path.ends_with(".ppm"sv, CaseSensitivity::CaseInsensitive)) {
|
2023-03-13 02:30:40 +00:00
|
|
|
auto const format = ppm_ascii ? Gfx::PortableFormatWriter::Options::Format::ASCII : Gfx::PortableFormatWriter::Options::Format::Raw;
|
2023-03-15 10:16:48 +00:00
|
|
|
bytes = TRY(Gfx::PortableFormatWriter::encode(*frame, { .format = format }));
|
2023-03-12 00:33:17 +00:00
|
|
|
} else if (out_path.ends_with(".qoi"sv, CaseSensitivity::CaseInsensitive)) {
|
2023-03-12 13:05:25 +00:00
|
|
|
bytes = TRY(Gfx::QOIWriter::encode(*frame));
|
2023-03-12 00:33:17 +00:00
|
|
|
} else {
|
2023-03-13 02:22:43 +00:00
|
|
|
warnln("can only write .bmp, .png, .ppm, and .qoi");
|
2023-03-12 00:33:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto output_stream = TRY(Core::File::open(out_path, Core::File::OpenMode::Write));
|
2023-03-01 14:37:45 +00:00
|
|
|
TRY(output_stream->write_until_depleted(bytes));
|
2023-03-12 00:33:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|