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

icc: Introduce --name flag to load a built-in profile by name

...instead of from a file.

For now, `--name=sRGB` is the only valid value, but more will
probably follow in the future.

Just `icc --name=sRGB` prints the built-in sRGB profile.

`icc --name=sRGB --reencode-to=file.icc` writes it to file.icc.
This commit is contained in:
Nico Weber 2023-02-27 08:51:36 -05:00 committed by Linus Groh
parent 0ba532e14e
commit 85507b34d7

View file

@ -13,6 +13,7 @@
#include <LibGfx/ICC/BinaryWriter.h> #include <LibGfx/ICC/BinaryWriter.h>
#include <LibGfx/ICC/Profile.h> #include <LibGfx/ICC/Profile.h>
#include <LibGfx/ICC/Tags.h> #include <LibGfx/ICC/Tags.h>
#include <LibGfx/ICC/WellKnownProfiles.h>
#include <LibGfx/ImageDecoder.h> #include <LibGfx/ImageDecoder.h>
#include <LibVideo/Color/CodingIndependentCodePoints.h> #include <LibVideo/Color/CodingIndependentCodePoints.h>
@ -92,7 +93,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
StringView path; StringView path;
args_parser.add_positional_argument(path, "Path to ICC profile or to image containing ICC profile", "FILE"); args_parser.add_positional_argument(path, "Path to ICC profile or to image containing ICC profile", "FILE", Core::ArgsParser::Required::No);
StringView name;
args_parser.add_option(name, "Name of a built-in profile, such as 'sRGB'", "name", 'n', "NAME");
StringView dump_out_path; StringView dump_out_path;
args_parser.add_option(dump_out_path, "Dump unmodified ICC profile bytes to this path", "dump-to", 0, "FILE"); args_parser.add_option(dump_out_path, "Dump unmodified ICC profile bytes to this path", "dump-to", 0, "FILE");
@ -105,27 +109,46 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.parse(arguments); args_parser.parse(arguments);
auto file = TRY(Core::MappedFile::map(path)); if (path.is_empty() && name.is_empty()) {
warnln("need either a path or a profile name");
return 1;
}
if (!path.is_empty() && !name.is_empty()) {
warnln("can't have both a path and a profile name");
return 1;
}
if (path.is_empty() && !dump_out_path.is_empty()) {
warnln("--dump-to only valid with path, not with profile name; use --reencode-to instead");
return 1;
}
ReadonlyBytes icc_bytes; ReadonlyBytes icc_bytes;
NonnullRefPtr<Gfx::ICC::Profile> profile = TRY([&]() -> ErrorOr<NonnullRefPtr<Gfx::ICC::Profile>> {
auto decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(file->bytes()); if (!name.is_empty()) {
if (decoder) { if (name == "sRGB")
if (auto embedded_icc_bytes = TRY(decoder->icc_data()); embedded_icc_bytes.has_value()) { return Gfx::ICC::sRGB();
icc_bytes = *embedded_icc_bytes; return Error::from_string_literal("unknown profile name");
} else {
outln("image contains no embedded ICC profile");
return 1;
} }
} else { auto file = TRY(Core::MappedFile::map(path));
icc_bytes = file->bytes();
}
if (!dump_out_path.is_empty()) { auto decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(file->bytes());
auto output_stream = TRY(Core::File::open(dump_out_path, Core::File::OpenMode::Write)); if (decoder) {
TRY(output_stream->write_entire_buffer(icc_bytes)); if (auto embedded_icc_bytes = TRY(decoder->icc_data()); embedded_icc_bytes.has_value()) {
} icc_bytes = *embedded_icc_bytes;
} else {
outln("image contains no embedded ICC profile");
exit(1);
}
} else {
icc_bytes = file->bytes();
}
auto profile = TRY(Gfx::ICC::Profile::try_load_from_externally_owned_memory(icc_bytes)); if (!dump_out_path.is_empty()) {
auto output_stream = TRY(Core::File::open(dump_out_path, Core::File::OpenMode::Write));
TRY(output_stream->write_entire_buffer(icc_bytes));
}
return Gfx::ICC::Profile::try_load_from_externally_owned_memory(icc_bytes);
}());
if (!reencode_out_path.is_empty()) { if (!reencode_out_path.is_empty()) {
auto reencoded_bytes = TRY(Gfx::ICC::encode(profile)); auto reencoded_bytes = TRY(Gfx::ICC::encode(profile));