1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
/*
* 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/ImageFormats/BMPWriter.h>
#include <LibGfx/ImageFormats/ImageDecoder.h>
#include <LibGfx/ImageFormats/PNGWriter.h>
#include <LibGfx/ImageFormats/PortableFormatWriter.h>
#include <LibGfx/ImageFormats/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");
bool ppm_ascii;
args_parser.add_option(ppm_ascii, "Convert to a PPM in ASCII", "ppm-ascii", {});
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");
bool strip_color_profile;
args_parser.add_option(strip_color_profile, "Do not write color profile to output", "strip-color-profile", {});
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());
if (!decoder) {
warnln("Failed to decode input file '{}'", in_path);
return 1;
}
// 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;
Optional<ReadonlyBytes> icc_data = TRY(decoder->icc_data());
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();
}
if (strip_color_profile)
icc_data.clear();
ByteBuffer bytes;
if (out_path.ends_with(".bmp"sv, CaseSensitivity::CaseInsensitive)) {
bytes = TRY(Gfx::BMPWriter::encode(*frame, { .icc_data = icc_data }));
} else if (out_path.ends_with(".png"sv, CaseSensitivity::CaseInsensitive)) {
bytes = TRY(Gfx::PNGWriter::encode(*frame, { .icc_data = 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;
bytes = TRY(Gfx::PortableFormatWriter::encode(*frame, { .format = format }));
} else if (out_path.ends_with(".qoi"sv, CaseSensitivity::CaseInsensitive)) {
bytes = TRY(Gfx::QOIWriter::encode(*frame));
} else {
warnln("can only write .bmp, .png, .ppm, and .qoi");
return 1;
}
auto output_stream = TRY(Core::File::open(out_path, Core::File::OpenMode::Write));
TRY(output_stream->write_until_depleted(bytes));
return 0;
}
|