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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
/*
* Copyright (c) 2022, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Endian.h>
#include <LibGfx/ICCProfile.h>
#include <time.h>
// V2 spec: https://color.org/specification/ICC.1-2001-04.pdf
// V4 spec: https://color.org/specification/ICC.1-2022-05.pdf
namespace Gfx::ICC {
namespace {
// ICC V4, 4.2 dateTimeNumber
// "All the dateTimeNumber values in a profile shall be in Coordinated Universal Time [...]."
struct DateTimeNumber {
BigEndian<u16> year;
BigEndian<u16> month;
BigEndian<u16> day;
BigEndian<u16> hour;
BigEndian<u16> minutes;
BigEndian<u16> seconds;
};
ErrorOr<time_t> parse_date_time_number(DateTimeNumber const& date_time)
{
// ICC V4, 4.2 dateTimeNumber
// "Number of the month (1 to 12)"
if (date_time.month < 1 || date_time.month > 12)
return Error::from_string_literal("ICC::Profile: dateTimeNumber month out of bounds");
// "Number of the day of the month (1 to 31)"
if (date_time.day < 1 || date_time.day > 31)
return Error::from_string_literal("ICC::Profile: dateTimeNumber day out of bounds");
// "Number of hours (0 to 23)"
if (date_time.hour > 23)
return Error::from_string_literal("ICC::Profile: dateTimeNumber hour out of bounds");
// "Number of minutes (0 to 59)"
if (date_time.minutes > 59)
return Error::from_string_literal("ICC::Profile: dateTimeNumber minutes out of bounds");
// "Number of seconds (0 to 59)"
// ICC profiles apparently can't be created during leap seconds (seconds would be 60 there, but the spec doesn't allow that).
if (date_time.seconds > 59)
return Error::from_string_literal("ICC::Profile: dateTimeNumber seconds out of bounds");
struct tm tm = {};
tm.tm_year = date_time.year - 1900;
tm.tm_mon = date_time.month - 1;
tm.tm_mday = date_time.day;
tm.tm_hour = date_time.hour;
tm.tm_min = date_time.minutes;
tm.tm_sec = date_time.seconds;
// timegm() doesn't read tm.tm_isdst, tm.tm_wday, and tm.tm_yday, no need to fill them in.
time_t timestamp = timegm(&tm);
if (timestamp == -1)
return Error::from_string_literal("ICC::Profile: dateTimeNumber not representable as timestamp");
return timestamp;
}
// ICC V4, 7.2 Profile header
struct ICCHeader {
BigEndian<u32> profile_size;
BigEndian<u32> preferred_cmm_type;
u8 profile_version_major;
u8 profile_version_minor_bugfix;
BigEndian<u16> profile_version_zero;
BigEndian<DeviceClass> profile_device_class;
BigEndian<u32> data_color_space;
BigEndian<u32> pcs; // "Profile Connection Space"
DateTimeNumber profile_creation_time;
BigEndian<u32> profile_file_signature;
BigEndian<u32> primary_platform;
BigEndian<u32> profile_flags;
BigEndian<u32> device_manufacturer;
BigEndian<u32> device_model;
BigEndian<u64> device_attributes;
BigEndian<u32> rendering_intent;
BigEndian<i32> pcs_illuminant_x;
BigEndian<i32> pcs_illuminant_y;
BigEndian<i32> pcs_illuminant_z;
BigEndian<u32> profile_creator;
u8 profile_md5[16];
u8 reserved[28];
};
static_assert(sizeof(ICCHeader) == 128);
ErrorOr<Version> parse_version(ICCHeader const& header)
{
// ICC v4, 7.2.4 Profile version field
if (header.profile_version_zero != 0)
return Error::from_string_literal("ICC::Profile: Reserved version bytes not zero");
return Version(header.profile_version_major, header.profile_version_minor_bugfix);
}
ErrorOr<DeviceClass> parse_device_class(ICCHeader const& header)
{
// ICC v4, 7.2.5 Profile/device class field
switch (header.profile_device_class) {
case DeviceClass::InputDevce:
case DeviceClass::DisplayDevice:
case DeviceClass::OutputDevice:
case DeviceClass::DeviceLink:
case DeviceClass::ColorSpace:
case DeviceClass::Abstract:
case DeviceClass::NamedColor:
return header.profile_device_class;
}
return Error::from_string_literal("ICC::Profile: Invalid device class");
}
ErrorOr<time_t> parse_creation_date_time(ICCHeader const& header)
{
// iCC v4, 7.2.8 Date and time field
return parse_date_time_number(header.profile_creation_time);
}
ErrorOr<void> parse_file_signature(ICCHeader const& header)
{
// iCC v4, 7.2.9 Profile file signature field
if (header.profile_file_signature != 0x61637370)
return Error::from_string_literal("ICC::Profile: profile file signature not 'acsp'");
return {};
}
}
char const* device_class_name(DeviceClass device_class)
{
switch (device_class) {
case DeviceClass::InputDevce:
return "InputDevce";
case DeviceClass::DisplayDevice:
return "DisplayDevice";
case DeviceClass::OutputDevice:
return "OutputDevice";
case DeviceClass::DeviceLink:
return "DeviceLink";
case DeviceClass::ColorSpace:
return "ColorSpace";
case DeviceClass::Abstract:
return "Abstract";
case DeviceClass::NamedColor:
return "NamedColor";
default:
return "(unknown device class)";
}
}
ErrorOr<NonnullRefPtr<Profile>> Profile::try_load_from_externally_owned_memory(ReadonlyBytes bytes)
{
auto profile = adopt_ref(*new Profile());
if (bytes.size() < sizeof(ICCHeader))
return Error::from_string_literal("ICC::Profile: Not enough data for header");
auto header = *bit_cast<ICCHeader const*>(bytes.data());
TRY(parse_file_signature(header));
profile->m_version = TRY(parse_version(header));
profile->m_device_class = TRY(parse_device_class(header));
profile->m_creation_timestamp = TRY(parse_creation_date_time(header));
return profile;
}
}
|