LibGfx: Remove ICC::Profile and friends

This is not really used anymore since the fork.
This commit is contained in:
Lucas CHOLLET 2024-12-15 20:37:28 -05:00 committed by Jelle Raaijmakers
parent 57cc248883
commit 2174e5dfcc
Notes: github-actions[bot] 2024-12-16 06:40:43 +00:00
30 changed files with 0 additions and 7490 deletions

View file

@ -1,18 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
namespace Gfx {
// https://en.wikipedia.org/wiki/CIELAB_color_space
struct CIELAB {
float L; // L*
float a; // a*
float b; // b*
};
}

View file

@ -8,7 +8,6 @@ set(SOURCES
CMYKBitmap.cpp
Color.cpp
ColorSpace.cpp
DeltaE.cpp
FontCascadeList.cpp
Font/Font.cpp
Font/FontData.cpp
@ -21,12 +20,6 @@ set(SOURCES
Font/WOFF/Loader.cpp
Font/WOFF2/Loader.cpp
GradientPainting.cpp
ICC/BinaryWriter.cpp
ICC/Enums.cpp
ICC/Profile.cpp
ICC/Tags.cpp
ICC/TagTypes.cpp
ICC/WellKnownProfiles.cpp
ImageFormats/AnimationWriter.cpp
ImageFormats/BMPLoader.cpp
ImageFormats/BMPWriter.cpp

View file

@ -1,89 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Math.h>
#include <LibGfx/DeltaE.h>
#include <math.h>
namespace Gfx {
float DeltaE(CIELAB const& c1, CIELAB const& c2)
{
// https://en.wikipedia.org/wiki/Color_difference#CIEDE2000
// http://zschuessler.github.io/DeltaE/learn/
// https://www.hajim.rochester.edu/ece/sites/gsharma/ciede2000/ciede2000noteCRNA.pdf
float delta_L_prime = c2.L - c1.L;
float L_bar = (c1.L + c2.L) / 2;
float C1 = hypotf(c1.a, c1.b);
float C2 = hypotf(c2.a, c2.b);
float C_bar = (C1 + C2) / 2;
float G = 0.5f * (1 - sqrtf(powf(C_bar, 7) / (powf(C_bar, 7) + powf(25, 7))));
float a1_prime = (1 + G) * c1.a;
float a2_prime = (1 + G) * c2.a;
float C1_prime = hypotf(a1_prime, c1.b);
float C2_prime = hypotf(a2_prime, c2.b);
float C_prime_bar = (C1_prime + C2_prime) / 2;
float delta_C_prime = C2_prime - C1_prime;
auto h_prime = [](float b, float a_prime) {
if (b == 0 && a_prime == 0)
return 0.f;
float h_prime = atan2(b, a_prime);
if (h_prime < 0)
h_prime += 2 * static_cast<float>(M_PI);
return AK::to_degrees(h_prime);
};
float h1_prime = h_prime(c1.b, a1_prime);
float h2_prime = h_prime(c2.b, a2_prime);
float delta_h_prime;
if (C1_prime == 0 || C2_prime == 0)
delta_h_prime = 0;
else if (fabsf(h1_prime - h2_prime) <= 180.f)
delta_h_prime = h2_prime - h1_prime;
else if (h2_prime <= h1_prime)
delta_h_prime = h2_prime - h1_prime + 360;
else
delta_h_prime = h2_prime - h1_prime - 360;
auto sin_degrees = [](float x) { return sinf(AK::to_radians(x)); };
auto cos_degrees = [](float x) { return cosf(AK::to_radians(x)); };
float delta_H_prime = 2 * sqrtf(C1_prime * C2_prime) * sin_degrees(delta_h_prime / 2);
float h_prime_bar;
if (C1_prime == 0 || C2_prime == 0)
h_prime_bar = h1_prime + h2_prime;
else if (fabsf(h1_prime - h2_prime) <= 180.f)
h_prime_bar = (h1_prime + h2_prime) / 2;
else if (h1_prime + h2_prime < 360)
h_prime_bar = (h1_prime + h2_prime + 360) / 2;
else
h_prime_bar = (h1_prime + h2_prime - 360) / 2;
float T = 1 - 0.17f * cos_degrees(h_prime_bar - 30) + 0.24f * cos_degrees(2 * h_prime_bar) + 0.32f * cos_degrees(3 * h_prime_bar + 6) - 0.2f * cos_degrees(4 * h_prime_bar - 63);
float S_L = 1 + 0.015f * powf(L_bar - 50, 2) / sqrtf(20 + powf(L_bar - 50, 2));
float S_C = 1 + 0.045f * C_prime_bar;
float S_H = 1 + 0.015f * C_prime_bar * T;
float R_T = -2 * sqrtf(powf(C_prime_bar, 7) / (powf(C_prime_bar, 7) + powf(25, 7))) * sin_degrees(60 * exp(-powf((h_prime_bar - 275) / 25, 2)));
// "kL, kC, and kH are usually unity."
float k_L = 1, k_C = 1, k_H = 1;
float L = delta_L_prime / (k_L * S_L);
float C = delta_C_prime / (k_C * S_C);
float H = delta_H_prime / (k_H * S_H);
return sqrtf(powf(L, 2) + powf(C, 2) + powf(H, 2) + R_T * C * H);
}
}

View file

@ -1,21 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGfx/CIELAB.h>
namespace Gfx {
// Returns a number between 0 and 100 that describes how far apart two colors are in human perception.
// A return value < 1 means that the two colors are not noticeably different.
// The larger the return value, the easier it is to tell the two colors apart.
// Works better for colors that are somewhat "close".
//
// You can use ICC::sRGB()->to_lab() to convert sRGB colors to CIELAB.
float DeltaE(CIELAB const&, CIELAB const&);
}

View file

@ -1,170 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Endian.h>
#include <LibGfx/ICC/DistinctFourCC.h>
#include <LibGfx/ICC/Profile.h>
#include <LibGfx/ICC/TagTypes.h>
#include <math.h>
namespace Gfx::ICC {
// 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> hours;
BigEndian<u16> minutes;
BigEndian<u16> seconds;
};
// ICC V4, 4.6 s15Fixed16Number
using s15Fixed16Number = i32;
// ICC V4, 4.7 u16Fixed16Number
using u16Fixed16Number = u32;
// ICC V4, 4.14 XYZNumber
struct XYZNumber {
BigEndian<s15Fixed16Number> X;
BigEndian<s15Fixed16Number> Y;
BigEndian<s15Fixed16Number> Z;
XYZNumber() = default;
XYZNumber(XYZ const& xyz)
: X(round(xyz.X * 0x1'0000))
, Y(round(xyz.Y * 0x1'0000))
, Z(round(xyz.Z * 0x1'0000))
{
}
operator XYZ() const
{
return XYZ { X / (float)0x1'0000, Y / (float)0x1'0000, Z / (float)0x1'0000 };
}
};
// ICC V4, 7.2 Profile header
struct ICCHeader {
BigEndian<u32> profile_size;
BigEndian<PreferredCMMType> preferred_cmm_type;
u8 profile_version_major;
u8 profile_version_minor_bugfix;
BigEndian<u16> profile_version_zero;
BigEndian<DeviceClass> profile_device_class;
BigEndian<ColorSpace> data_color_space;
BigEndian<ColorSpace> profile_connection_space; // "PCS" in the spec.
DateTimeNumber profile_creation_time;
BigEndian<u32> profile_file_signature;
BigEndian<PrimaryPlatform> primary_platform;
BigEndian<u32> profile_flags;
BigEndian<DeviceManufacturer> device_manufacturer;
BigEndian<DeviceModel> device_model;
BigEndian<u64> device_attributes;
BigEndian<RenderingIntent> rendering_intent;
XYZNumber pcs_illuminant;
BigEndian<Creator> profile_creator;
u8 profile_id[16];
u8 reserved[28];
};
static_assert(AssertSize<ICCHeader, 128>());
// ICC v4, 7.2.9 Profile file signature field
// "The profile file signature field shall contain the value “acsp” (61637370h) as a profile file signature."
constexpr u32 ProfileFileSignature = 0x61637370;
// ICC V4, 7.3 Tag table, Table 24 - Tag table structure
struct TagTableEntry {
BigEndian<TagSignature> tag_signature;
BigEndian<u32> offset_to_beginning_of_tag_data_element;
BigEndian<u32> size_of_tag_data_element;
};
static_assert(AssertSize<TagTableEntry, 12>());
// Common bits of ICC v4, Table 40 — lut16Type encoding and Table 44 — lut8Type encoding
struct LUTHeader {
u8 number_of_input_channels;
u8 number_of_output_channels;
u8 number_of_clut_grid_points;
u8 reserved_for_padding;
BigEndian<s15Fixed16Number> e_parameters[9];
};
static_assert(AssertSize<LUTHeader, 40>());
// Common bits of ICC v4, Table 45 — lutAToBType encoding and Table 47 — lutBToAType encoding
struct AdvancedLUTHeader {
u8 number_of_input_channels;
u8 number_of_output_channels;
BigEndian<u16> reserved_for_padding;
BigEndian<u32> offset_to_b_curves;
BigEndian<u32> offset_to_matrix;
BigEndian<u32> offset_to_m_curves;
BigEndian<u32> offset_to_clut;
BigEndian<u32> offset_to_a_curves;
};
static_assert(AssertSize<AdvancedLUTHeader, 24>());
// ICC v4, Table 46 — lutAToBType CLUT encoding
// ICC v4, Table 48 — lutBToAType CLUT encoding
// (They're identical.)
struct CLUTHeader {
u8 number_of_grid_points_in_dimension[16];
u8 precision_of_data_elements; // 1 for u8 entries, 2 for u16 entries.
u8 reserved_for_padding[3];
};
static_assert(AssertSize<CLUTHeader, 20>());
// Table 49 — measurementType structure
struct MeasurementHeader {
BigEndian<MeasurementTagData::StandardObserver> standard_observer;
XYZNumber tristimulus_value_for_measurement_backing;
BigEndian<MeasurementTagData::MeasurementGeometry> measurement_geometry;
BigEndian<u16Fixed16Number> measurement_flare;
BigEndian<MeasurementTagData::StandardIlluminant> standard_illuminant;
};
static_assert(AssertSize<MeasurementHeader, 28>());
// ICC v4, 10.15 multiLocalizedUnicodeType
struct MultiLocalizedUnicodeRawRecord {
BigEndian<u16> language_code;
BigEndian<u16> country_code;
BigEndian<u32> string_length_in_bytes;
BigEndian<u32> string_offset_in_bytes;
};
static_assert(AssertSize<MultiLocalizedUnicodeRawRecord, 12>());
// Table 66 — namedColor2Type encoding
struct NamedColorHeader {
BigEndian<u32> vendor_specific_flag;
BigEndian<u32> count_of_named_colors;
BigEndian<u32> number_of_device_coordinates_of_each_named_color;
u8 prefix_for_each_color_name[32]; // null-terminated
u8 suffix_for_each_color_name[32]; // null-terminated
};
static_assert(AssertSize<NamedColorHeader, 76>());
// Table 84 — viewingConditionsType encoding
struct ViewingConditionsHeader {
XYZNumber unnormalized_ciexyz_values_for_illuminant; // "(in which Y is in cd/m2)"
XYZNumber unnormalized_ciexyz_values_for_surround; // "(in which Y is in cd/m2)"
BigEndian<MeasurementTagData::StandardIlluminant> illuminant_type;
};
static_assert(AssertSize<ViewingConditionsHeader, 28>());
}

View file

@ -1,748 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Utf16View.h>
#include <LibGfx/ICC/BinaryFormat.h>
#include <LibGfx/ICC/BinaryWriter.h>
#include <LibGfx/ICC/Profile.h>
#include <time.h>
#pragma GCC diagnostic ignored "-Warray-bounds"
namespace Gfx::ICC {
static ErrorOr<ByteBuffer> encode_chromaticity(ChromaticityTagData const& tag_data)
{
// ICC v4, 10.2 chromaticityType
auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + 2 * sizeof(u16) + tag_data.xy_coordinates().size() * 2 * sizeof(u16Fixed16Number)));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(ChromaticityTagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
*bit_cast<BigEndian<u16>*>(bytes.data() + 8) = tag_data.xy_coordinates().size();
*bit_cast<BigEndian<u16>*>(bytes.data() + 10) = static_cast<u16>(tag_data.phosphor_or_colorant_type());
auto* coordinates = bit_cast<BigEndian<u16Fixed16Number>*>(bytes.data() + 12);
for (size_t i = 0; i < tag_data.xy_coordinates().size(); ++i) {
coordinates[2 * i] = tag_data.xy_coordinates()[i].x.raw();
coordinates[2 * i + 1] = tag_data.xy_coordinates()[i].y.raw();
}
return bytes;
}
static ErrorOr<ByteBuffer> encode_cipc(CicpTagData const& tag_data)
{
// ICC v4, 10.3 cicpType
auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + 4));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(CicpTagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
bytes.data()[8] = tag_data.color_primaries();
bytes.data()[9] = tag_data.transfer_characteristics();
bytes.data()[10] = tag_data.matrix_coefficients();
bytes.data()[11] = tag_data.video_full_range_flag();
return bytes;
}
static u32 curve_encoded_size(CurveTagData const& tag_data)
{
return 3 * sizeof(u32) + tag_data.values().size() * sizeof(u16);
}
static void encode_curve_to(CurveTagData const& tag_data, Bytes bytes)
{
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(CurveTagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
*bit_cast<BigEndian<u32>*>(bytes.data() + 8) = tag_data.values().size();
auto* values = bit_cast<BigEndian<u16>*>(bytes.data() + 12);
for (size_t i = 0; i < tag_data.values().size(); ++i)
values[i] = tag_data.values()[i];
}
static ErrorOr<ByteBuffer> encode_curve(CurveTagData const& tag_data)
{
// ICC v4, 10.6 curveType
auto bytes = TRY(ByteBuffer::create_uninitialized(curve_encoded_size(tag_data)));
encode_curve_to(tag_data, bytes.bytes());
return bytes;
}
static ErrorOr<ByteBuffer> encode_lut_16(Lut16TagData const& tag_data)
{
// ICC v4, 10.10 lut16Type
u32 input_tables_size = tag_data.input_tables().size();
u32 clut_values_size = tag_data.clut_values().size();
u32 output_tables_size = tag_data.output_tables().size();
auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(LUTHeader) + 2 * sizeof(u16) + sizeof(u16) * (input_tables_size + clut_values_size + output_tables_size)));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(Lut16TagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
auto& header = *bit_cast<LUTHeader*>(bytes.data() + 8);
header.number_of_input_channels = tag_data.number_of_input_channels();
header.number_of_output_channels = tag_data.number_of_output_channels();
header.number_of_clut_grid_points = tag_data.number_of_clut_grid_points();
header.reserved_for_padding = 0;
for (int i = 0; i < 9; ++i)
header.e_parameters[i] = tag_data.e_matrix().e[i].raw();
*bit_cast<BigEndian<u16>*>(bytes.data() + 8 + sizeof(LUTHeader)) = tag_data.number_of_input_table_entries();
*bit_cast<BigEndian<u16>*>(bytes.data() + 8 + sizeof(LUTHeader) + 2) = tag_data.number_of_output_table_entries();
auto* values = bit_cast<BigEndian<u16>*>(bytes.data() + 8 + sizeof(LUTHeader) + 4);
for (u16 input_value : tag_data.input_tables())
*values++ = input_value;
for (u16 clut_value : tag_data.clut_values())
*values++ = clut_value;
for (u16 output_value : tag_data.output_tables())
*values++ = output_value;
return bytes;
}
static ErrorOr<ByteBuffer> encode_lut_8(Lut8TagData const& tag_data)
{
// ICC v4, 10.11 lut8Type
u32 input_tables_size = tag_data.input_tables().size();
u32 clut_values_size = tag_data.clut_values().size();
u32 output_tables_size = tag_data.output_tables().size();
auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(LUTHeader) + input_tables_size + clut_values_size + output_tables_size));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(Lut8TagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
auto& header = *bit_cast<LUTHeader*>(bytes.data() + 8);
header.number_of_input_channels = tag_data.number_of_input_channels();
header.number_of_output_channels = tag_data.number_of_output_channels();
header.number_of_clut_grid_points = tag_data.number_of_clut_grid_points();
header.reserved_for_padding = 0;
for (int i = 0; i < 9; ++i)
header.e_parameters[i] = tag_data.e_matrix().e[i].raw();
u8* values = bytes.data() + 8 + sizeof(LUTHeader);
memcpy(values, tag_data.input_tables().data(), input_tables_size);
values += input_tables_size;
memcpy(values, tag_data.clut_values().data(), clut_values_size);
values += clut_values_size;
memcpy(values, tag_data.output_tables().data(), output_tables_size);
return bytes;
}
static u32 curve_encoded_size(CurveTagData const&);
static void encode_curve_to(CurveTagData const&, Bytes);
static u32 parametric_curve_encoded_size(ParametricCurveTagData const&);
static void encode_parametric_curve_to(ParametricCurveTagData const&, Bytes);
static u32 byte_size_of_curve(LutCurveType const& curve)
{
VERIFY(curve->type() == Gfx::ICC::CurveTagData::Type || curve->type() == Gfx::ICC::ParametricCurveTagData::Type);
if (curve->type() == Gfx::ICC::CurveTagData::Type)
return curve_encoded_size(static_cast<CurveTagData const&>(*curve));
return parametric_curve_encoded_size(static_cast<ParametricCurveTagData const&>(*curve));
}
static u32 byte_size_of_curves(Vector<LutCurveType> const& curves)
{
u32 size = 0;
for (auto const& curve : curves)
size += align_up_to(byte_size_of_curve(curve), 4);
return size;
}
static void write_curve(Bytes bytes, LutCurveType const& curve)
{
VERIFY(curve->type() == Gfx::ICC::CurveTagData::Type || curve->type() == Gfx::ICC::ParametricCurveTagData::Type);
if (curve->type() == Gfx::ICC::CurveTagData::Type)
encode_curve_to(static_cast<CurveTagData const&>(*curve), bytes);
if (curve->type() == Gfx::ICC::ParametricCurveTagData::Type)
encode_parametric_curve_to(static_cast<ParametricCurveTagData const&>(*curve), bytes);
}
static void write_curves(Bytes bytes, Vector<LutCurveType> const& curves)
{
u32 offset = 0;
for (auto const& curve : curves) {
u32 size = byte_size_of_curve(curve);
write_curve(bytes.slice(offset, size), curve);
offset += align_up_to(size, 4);
}
}
static u32 byte_size_of_clut(CLUTData const& clut)
{
u32 data_size = clut.values.visit(
[](Vector<u8> const& v) { return v.size(); },
[](Vector<u16> const& v) { return 2 * v.size(); });
return align_up_to(sizeof(CLUTHeader) + data_size, 4);
}
static void write_clut(Bytes bytes, CLUTData const& clut)
{
auto& clut_header = *bit_cast<CLUTHeader*>(bytes.data());
memset(clut_header.number_of_grid_points_in_dimension, 0, sizeof(clut_header.number_of_grid_points_in_dimension));
VERIFY(clut.number_of_grid_points_in_dimension.size() <= sizeof(clut_header.number_of_grid_points_in_dimension));
for (size_t i = 0; i < clut.number_of_grid_points_in_dimension.size(); ++i)
clut_header.number_of_grid_points_in_dimension[i] = clut.number_of_grid_points_in_dimension[i];
clut_header.precision_of_data_elements = clut.values.visit(
[](Vector<u8> const&) { return 1; },
[](Vector<u16> const&) { return 2; });
memset(clut_header.reserved_for_padding, 0, sizeof(clut_header.reserved_for_padding));
clut.values.visit(
[&bytes](Vector<u8> const& v) {
memcpy(bytes.data() + sizeof(CLUTHeader), v.data(), v.size());
},
[&bytes](Vector<u16> const& v) {
auto* raw_clut = bit_cast<BigEndian<u16>*>(bytes.data() + sizeof(CLUTHeader));
for (size_t i = 0; i < v.size(); ++i)
raw_clut[i] = v[i];
});
}
static void write_matrix(Bytes bytes, EMatrix3x4 const& e_matrix)
{
auto* raw_e = bit_cast<BigEndian<s15Fixed16Number>*>(bytes.data());
for (int i = 0; i < 12; ++i)
raw_e[i] = e_matrix.e[i].raw();
}
static ErrorOr<ByteBuffer> encode_lut_a_to_b(LutAToBTagData const& tag_data)
{
// ICC v4, 10.12 lutAToBType
u32 a_curves_size = tag_data.a_curves().map(byte_size_of_curves).value_or(0);
u32 clut_size = tag_data.clut().map(byte_size_of_clut).value_or(0);
u32 m_curves_size = tag_data.m_curves().map(byte_size_of_curves).value_or(0);
u32 e_matrix_size = tag_data.e_matrix().has_value() ? 12 * sizeof(s15Fixed16Number) : 0;
u32 b_curves_size = byte_size_of_curves(tag_data.b_curves());
auto bytes = TRY(ByteBuffer::create_zeroed(2 * sizeof(u32) + sizeof(AdvancedLUTHeader) + a_curves_size + clut_size + m_curves_size + e_matrix_size + b_curves_size));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(LutAToBTagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
auto& header = *bit_cast<AdvancedLUTHeader*>(bytes.data() + 8);
header.number_of_input_channels = tag_data.number_of_input_channels();
header.number_of_output_channels = tag_data.number_of_output_channels();
header.reserved_for_padding = 0;
header.offset_to_b_curves = 0;
header.offset_to_matrix = 0;
header.offset_to_m_curves = 0;
header.offset_to_clut = 0;
header.offset_to_a_curves = 0;
u32 offset = 2 * sizeof(u32) + sizeof(AdvancedLUTHeader);
auto advance = [&offset](BigEndian<u32>& header_slot, u32 size) {
header_slot = offset;
VERIFY(size % 4 == 0);
offset += size;
};
if (auto const& a_curves = tag_data.a_curves(); a_curves.has_value()) {
write_curves(bytes.bytes().slice(offset, a_curves_size), a_curves.value());
advance(header.offset_to_a_curves, a_curves_size);
}
if (auto const& clut = tag_data.clut(); clut.has_value()) {
write_clut(bytes.bytes().slice(offset, clut_size), clut.value());
advance(header.offset_to_clut, clut_size);
}
if (auto const& m_curves = tag_data.m_curves(); m_curves.has_value()) {
write_curves(bytes.bytes().slice(offset, m_curves_size), m_curves.value());
advance(header.offset_to_m_curves, m_curves_size);
}
if (auto const& e_matrix = tag_data.e_matrix(); e_matrix.has_value()) {
write_matrix(bytes.bytes().slice(offset, e_matrix_size), e_matrix.value());
advance(header.offset_to_matrix, e_matrix_size);
}
write_curves(bytes.bytes().slice(offset, b_curves_size), tag_data.b_curves());
advance(header.offset_to_b_curves, b_curves_size);
return bytes;
}
static ErrorOr<ByteBuffer> encode_lut_b_to_a(LutBToATagData const& tag_data)
{
// ICC v4, 10.13 lutBToAType
u32 b_curves_size = byte_size_of_curves(tag_data.b_curves());
u32 e_matrix_size = tag_data.e_matrix().has_value() ? 12 * sizeof(s15Fixed16Number) : 0;
u32 m_curves_size = tag_data.m_curves().map(byte_size_of_curves).value_or(0);
u32 clut_size = tag_data.clut().map(byte_size_of_clut).value_or(0);
u32 a_curves_size = tag_data.a_curves().map(byte_size_of_curves).value_or(0);
auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(AdvancedLUTHeader) + b_curves_size + e_matrix_size + m_curves_size + clut_size + a_curves_size));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(LutBToATagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
auto& header = *bit_cast<AdvancedLUTHeader*>(bytes.data() + 8);
header.number_of_input_channels = tag_data.number_of_input_channels();
header.number_of_output_channels = tag_data.number_of_output_channels();
header.reserved_for_padding = 0;
header.offset_to_b_curves = 0;
header.offset_to_matrix = 0;
header.offset_to_m_curves = 0;
header.offset_to_clut = 0;
header.offset_to_a_curves = 0;
u32 offset = 2 * sizeof(u32) + sizeof(AdvancedLUTHeader);
auto advance = [&offset](BigEndian<u32>& header_slot, u32 size) {
header_slot = offset;
VERIFY(size % 4 == 0);
offset += size;
};
write_curves(bytes.bytes().slice(offset, b_curves_size), tag_data.b_curves());
advance(header.offset_to_b_curves, b_curves_size);
if (auto const& e_matrix = tag_data.e_matrix(); e_matrix.has_value()) {
write_matrix(bytes.bytes().slice(offset, e_matrix_size), e_matrix.value());
advance(header.offset_to_matrix, e_matrix_size);
}
if (auto const& m_curves = tag_data.m_curves(); m_curves.has_value()) {
write_curves(bytes.bytes().slice(offset, m_curves_size), m_curves.value());
advance(header.offset_to_m_curves, m_curves_size);
}
if (auto const& clut = tag_data.clut(); clut.has_value()) {
write_clut(bytes.bytes().slice(offset, clut_size), clut.value());
advance(header.offset_to_clut, clut_size);
}
if (auto const& a_curves = tag_data.a_curves(); a_curves.has_value()) {
write_curves(bytes.bytes().slice(offset, a_curves_size), a_curves.value());
advance(header.offset_to_a_curves, a_curves_size);
}
return bytes;
}
static ErrorOr<ByteBuffer> encode_measurement(MeasurementTagData const& tag_data)
{
// ICC v4, 10.14 measurementType
auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(MeasurementHeader)));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(MeasurementTagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
auto& header = *bit_cast<MeasurementHeader*>(bytes.data() + 8);
header.standard_observer = tag_data.standard_observer();
header.tristimulus_value_for_measurement_backing = tag_data.tristimulus_value_for_measurement_backing();
header.measurement_geometry = tag_data.measurement_geometry();
header.measurement_flare = tag_data.measurement_flare().raw();
header.standard_illuminant = tag_data.standard_illuminant();
return bytes;
}
static ErrorOr<ByteBuffer> encode_multi_localized_unicode(MultiLocalizedUnicodeTagData const& tag_data)
{
// ICC v4, 10.15 multiLocalizedUnicodeType
// "The Unicode strings in storage should be encoded as 16-bit big-endian, UTF-16BE,
// and should not be NULL terminated."
size_t number_of_records = tag_data.records().size();
size_t header_and_record_size = 4 * sizeof(u32) + number_of_records * sizeof(MultiLocalizedUnicodeRawRecord);
size_t number_of_codepoints = 0;
Vector<Utf16Data> utf16_strings;
TRY(utf16_strings.try_ensure_capacity(number_of_records));
for (auto const& record : tag_data.records()) {
TRY(utf16_strings.try_append(TRY(utf8_to_utf16(record.text))));
number_of_codepoints += utf16_strings.last().size();
}
size_t string_table_size = number_of_codepoints * sizeof(u16);
auto bytes = TRY(ByteBuffer::create_uninitialized(header_and_record_size + string_table_size));
auto* header = bit_cast<BigEndian<u32>*>(bytes.data());
header[0] = static_cast<u32>(MultiLocalizedUnicodeTagData::Type);
header[1] = 0;
header[2] = number_of_records;
header[3] = sizeof(MultiLocalizedUnicodeRawRecord);
size_t offset = header_and_record_size;
auto* records = bit_cast<MultiLocalizedUnicodeRawRecord*>(bytes.data() + 16);
for (size_t i = 0; i < number_of_records; ++i) {
records[i].language_code = tag_data.records()[i].iso_639_1_language_code;
records[i].country_code = tag_data.records()[i].iso_3166_1_country_code;
records[i].string_length_in_bytes = utf16_strings[i].size() * sizeof(u16);
records[i].string_offset_in_bytes = offset;
offset += records[i].string_length_in_bytes;
}
auto* string_table = bit_cast<BigEndian<u16>*>(bytes.data() + header_and_record_size);
for (auto const& utf16_string : utf16_strings) {
for (size_t i = 0; i < utf16_string.size(); ++i)
string_table[i] = utf16_string[i];
string_table += utf16_string.size();
}
return bytes;
}
static ErrorOr<ByteBuffer> encode_named_color_2(NamedColor2TagData const& tag_data)
{
// ICC v4, 10.17 namedColor2Type
unsigned const record_byte_size = 32 + sizeof(u16) * (3 + tag_data.number_of_device_coordinates());
auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(NamedColorHeader) + tag_data.size() * record_byte_size));
*bit_cast<BigEndian<u32>*>(bytes.data()) = (u32)NamedColor2TagData::Type;
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
auto& header = *bit_cast<NamedColorHeader*>(bytes.data() + 8);
header.vendor_specific_flag = tag_data.vendor_specific_flag();
header.count_of_named_colors = tag_data.size();
header.number_of_device_coordinates_of_each_named_color = tag_data.number_of_device_coordinates();
memset(header.prefix_for_each_color_name, 0, 32);
memcpy(header.prefix_for_each_color_name, tag_data.prefix().bytes().data(), tag_data.prefix().bytes().size());
memset(header.suffix_for_each_color_name, 0, 32);
memcpy(header.suffix_for_each_color_name, tag_data.suffix().bytes().data(), tag_data.suffix().bytes().size());
u8* record = bytes.data() + 8 + sizeof(NamedColorHeader);
for (size_t i = 0; i < tag_data.size(); ++i) {
memset(record, 0, 32);
memcpy(record, tag_data.root_name(i).bytes().data(), tag_data.root_name(i).bytes().size());
auto* components = bit_cast<BigEndian<u16>*>(record + 32);
components[0] = tag_data.pcs_coordinates(i).xyz.x;
components[1] = tag_data.pcs_coordinates(i).xyz.y;
components[2] = tag_data.pcs_coordinates(i).xyz.z;
for (size_t j = 0; j < tag_data.number_of_device_coordinates(); ++j)
components[3 + j] = tag_data.device_coordinates(i)[j];
record += record_byte_size;
}
return bytes;
}
static u32 parametric_curve_encoded_size(ParametricCurveTagData const& tag_data)
{
return 2 * sizeof(u32) + 2 * sizeof(u16) + tag_data.parameter_count() * sizeof(s15Fixed16Number);
}
static void encode_parametric_curve_to(ParametricCurveTagData const& tag_data, Bytes bytes)
{
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(ParametricCurveTagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
*bit_cast<BigEndian<u16>*>(bytes.data() + 8) = static_cast<u16>(tag_data.function_type());
*bit_cast<BigEndian<u16>*>(bytes.data() + 10) = 0;
auto* parameters = bit_cast<BigEndian<s15Fixed16Number>*>(bytes.data() + 12);
for (size_t i = 0; i < tag_data.parameter_count(); ++i)
parameters[i] = tag_data.parameter(i).raw();
}
static ErrorOr<ByteBuffer> encode_parametric_curve(ParametricCurveTagData const& tag_data)
{
// ICC v4, 10.18 parametricCurveType
auto bytes = TRY(ByteBuffer::create_uninitialized(parametric_curve_encoded_size(tag_data)));
encode_parametric_curve_to(tag_data, bytes.bytes());
return bytes;
}
static ErrorOr<ByteBuffer> encode_s15_fixed_array(S15Fixed16ArrayTagData const& tag_data)
{
// ICC v4, 10.22 s15Fixed16ArrayType
auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + tag_data.values().size() * sizeof(s15Fixed16Number)));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(S15Fixed16ArrayTagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
auto* values = bit_cast<BigEndian<s15Fixed16Number>*>(bytes.data() + 8);
for (size_t i = 0; i < tag_data.values().size(); ++i)
values[i] = tag_data.values()[i].raw();
return bytes;
}
static ErrorOr<ByteBuffer> encode_signature(SignatureTagData const& tag_data)
{
// ICC v4, 10.23 signatureType
auto bytes = TRY(ByteBuffer::create_uninitialized(3 * sizeof(u32)));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(SignatureTagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
*bit_cast<BigEndian<u32>*>(bytes.data() + 8) = tag_data.signature();
return bytes;
}
static ErrorOr<ByteBuffer> encode_text_description(TextDescriptionTagData const& tag_data)
{
// ICC v2, 6.5.17 textDescriptionType
// All lengths include room for a trailing nul character.
// See also the many comments in TextDescriptionTagData::from_bytes().
u32 ascii_size = sizeof(u32) + tag_data.ascii_description().bytes().size() + 1;
// FIXME: Include tag_data.unicode_description() if it's set.
u32 unicode_size = 2 * sizeof(u32);
// FIXME: Include tag_data.macintosh_description() if it's set.
u32 macintosh_size = sizeof(u16) + sizeof(u8) + 67;
auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + ascii_size + unicode_size + macintosh_size));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(TextDescriptionTagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
// ASCII
*bit_cast<BigEndian<u32>*>(bytes.data() + 8) = tag_data.ascii_description().bytes().size() + 1;
memcpy(bytes.data() + 12, tag_data.ascii_description().bytes().data(), tag_data.ascii_description().bytes().size());
bytes.data()[12 + tag_data.ascii_description().bytes().size()] = '\0';
// Unicode
// "Because the Unicode language code and Unicode count immediately follow the ASCII description,
// their alignment is not correct when the ASCII count is not a multiple of four"
// So we can't use BigEndian<u32> here.
u8* cursor = bytes.data() + 8 + ascii_size;
u32 unicode_language_code = 0; // FIXME: Set to tag_data.unicode_language_code() once this writes unicode data.
cursor[0] = unicode_language_code >> 24;
cursor[1] = (unicode_language_code >> 16) & 0xff;
cursor[2] = (unicode_language_code >> 8) & 0xff;
cursor[3] = unicode_language_code & 0xff;
cursor += 4;
// FIXME: Include tag_data.unicode_description() if it's set.
u32 ucs2_count = 0; // FIXME: If tag_data.unicode_description() is set, set this to its length plus room for one nul character.
cursor[0] = ucs2_count >> 24;
cursor[1] = (ucs2_count >> 16) & 0xff;
cursor[2] = (ucs2_count >> 8) & 0xff;
cursor[3] = ucs2_count & 0xff;
cursor += 4;
// Macintosh scriptcode
u16 scriptcode_code = 0; // MacRoman
cursor[0] = (scriptcode_code >> 8) & 0xff;
cursor[1] = scriptcode_code & 0xff;
cursor += 2;
u8 macintosh_description_length = 0; // FIXME: If tag_data.macintosh_description() is set, set this to tis length plus room for one nul character.
cursor[0] = macintosh_description_length;
cursor += 1;
memset(cursor, 0, 67);
return bytes;
}
static ErrorOr<ByteBuffer> encode_text(TextTagData const& tag_data)
{
// ICC v4, 10.24 textType
// "The textType is a simple text structure that contains a 7-bit ASCII text string. The length of the string is obtained
// by subtracting 8 from the element size portion of the tag itself. This string shall be terminated with a 00h byte."
auto text_bytes = tag_data.text().bytes();
auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + text_bytes.size() + 1));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(TextTagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
memcpy(bytes.data() + 8, text_bytes.data(), text_bytes.size());
*(bytes.data() + 8 + text_bytes.size()) = '\0';
return bytes;
}
static ErrorOr<ByteBuffer> encode_viewing_conditions(ViewingConditionsTagData const& tag_data)
{
// ICC v4, 10.30 viewingConditionsType
auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(ViewingConditionsHeader)));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(ViewingConditionsTagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
auto& header = *bit_cast<ViewingConditionsHeader*>(bytes.data() + 8);
header.unnormalized_ciexyz_values_for_illuminant = tag_data.unnormalized_ciexyz_values_for_illuminant();
header.unnormalized_ciexyz_values_for_surround = tag_data.unnormalized_ciexyz_values_for_surround();
header.illuminant_type = tag_data.illuminant_type();
return bytes;
}
static ErrorOr<ByteBuffer> encode_xyz(XYZTagData const& tag_data)
{
// ICC v4, 10.31 XYZType
auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + tag_data.xyzs().size() * sizeof(XYZNumber)));
*bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(XYZTagData::Type);
*bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
auto* xyzs = bit_cast<XYZNumber*>(bytes.data() + 8);
for (size_t i = 0; i < tag_data.xyzs().size(); ++i)
xyzs[i] = tag_data.xyzs()[i];
return bytes;
}
static ErrorOr<Optional<ByteBuffer>> encode_tag_data(TagData const& tag_data)
{
switch (tag_data.type()) {
case ChromaticityTagData::Type:
return encode_chromaticity(static_cast<ChromaticityTagData const&>(tag_data));
case CicpTagData::Type:
return encode_cipc(static_cast<CicpTagData const&>(tag_data));
case CurveTagData::Type:
return encode_curve(static_cast<CurveTagData const&>(tag_data));
case Lut16TagData::Type:
return encode_lut_16(static_cast<Lut16TagData const&>(tag_data));
case Lut8TagData::Type:
return encode_lut_8(static_cast<Lut8TagData const&>(tag_data));
case LutAToBTagData::Type:
return encode_lut_a_to_b(static_cast<LutAToBTagData const&>(tag_data));
case LutBToATagData::Type:
return encode_lut_b_to_a(static_cast<LutBToATagData const&>(tag_data));
case MeasurementTagData::Type:
return encode_measurement(static_cast<MeasurementTagData const&>(tag_data));
case MultiLocalizedUnicodeTagData::Type:
return encode_multi_localized_unicode(static_cast<MultiLocalizedUnicodeTagData const&>(tag_data));
case NamedColor2TagData::Type:
return encode_named_color_2(static_cast<NamedColor2TagData const&>(tag_data));
case ParametricCurveTagData::Type:
return encode_parametric_curve(static_cast<ParametricCurveTagData const&>(tag_data));
case S15Fixed16ArrayTagData::Type:
return encode_s15_fixed_array(static_cast<S15Fixed16ArrayTagData const&>(tag_data));
case SignatureTagData::Type:
return encode_signature(static_cast<SignatureTagData const&>(tag_data));
case TextDescriptionTagData::Type:
return encode_text_description(static_cast<TextDescriptionTagData const&>(tag_data));
case TextTagData::Type:
return encode_text(static_cast<TextTagData const&>(tag_data));
case ViewingConditionsTagData::Type:
return encode_viewing_conditions(static_cast<ViewingConditionsTagData const&>(tag_data));
case XYZTagData::Type:
return encode_xyz(static_cast<XYZTagData const&>(tag_data));
}
return OptionalNone {};
}
static ErrorOr<Vector<ByteBuffer>> encode_tag_datas(Profile const& profile, HashMap<TagData*, size_t>& tag_data_map)
{
Vector<ByteBuffer> tag_data_bytes;
TRY(tag_data_bytes.try_ensure_capacity(profile.tag_count()));
TRY(profile.try_for_each_tag([&](auto, auto tag_data) -> ErrorOr<void> {
if (tag_data_map.contains(tag_data.ptr()))
return {};
auto encoded_tag_data = TRY(encode_tag_data(tag_data));
if (!encoded_tag_data.has_value())
return {};
tag_data_bytes.append(encoded_tag_data.release_value());
TRY(tag_data_map.try_set(tag_data.ptr(), tag_data_bytes.size() - 1));
return {};
}));
return tag_data_bytes;
}
static ErrorOr<void> encode_tag_table(ByteBuffer& bytes, Profile const& profile, u32 number_of_serialized_tags, Vector<size_t> const& offsets,
Vector<ByteBuffer> const& tag_data_bytes, HashMap<TagData*, size_t> const& tag_data_map)
{
// ICC v4, 7.3 Tag table
// ICC v4, 7.3.1 Overview
VERIFY(bytes.size() >= sizeof(ICCHeader) + sizeof(u32) + number_of_serialized_tags * sizeof(TagTableEntry));
*bit_cast<BigEndian<u32>*>(bytes.data() + sizeof(ICCHeader)) = number_of_serialized_tags;
TagTableEntry* tag_table_entries = bit_cast<TagTableEntry*>(bytes.data() + sizeof(ICCHeader) + sizeof(u32));
int i = 0;
profile.for_each_tag([&](auto tag_signature, auto tag_data) {
auto index = tag_data_map.get(tag_data.ptr());
if (!index.has_value())
return;
tag_table_entries[i].tag_signature = tag_signature;
tag_table_entries[i].offset_to_beginning_of_tag_data_element = offsets[index.value()];
tag_table_entries[i].size_of_tag_data_element = tag_data_bytes[index.value()].size();
++i;
});
return {};
}
static ErrorOr<void> encode_header(ByteBuffer& bytes, Profile const& profile)
{
VERIFY(bytes.size() >= sizeof(ICCHeader));
auto& raw_header = *bit_cast<ICCHeader*>(bytes.data());
raw_header.profile_size = bytes.size();
raw_header.preferred_cmm_type = profile.preferred_cmm_type().value_or(PreferredCMMType { 0 });
raw_header.profile_version_major = profile.version().major_version();
raw_header.profile_version_minor_bugfix = profile.version().minor_and_bugfix_version();
raw_header.profile_version_zero = 0;
raw_header.profile_device_class = profile.device_class();
raw_header.data_color_space = profile.data_color_space();
raw_header.profile_connection_space = profile.connection_space();
DateTime profile_timestamp = profile.creation_timestamp();
raw_header.profile_creation_time.year = profile_timestamp.year;
raw_header.profile_creation_time.month = profile_timestamp.month;
raw_header.profile_creation_time.day = profile_timestamp.day;
raw_header.profile_creation_time.hours = profile_timestamp.hours;
raw_header.profile_creation_time.minutes = profile_timestamp.minutes;
raw_header.profile_creation_time.seconds = profile_timestamp.seconds;
raw_header.profile_file_signature = ProfileFileSignature;
raw_header.primary_platform = profile.primary_platform().value_or(PrimaryPlatform { 0 });
raw_header.profile_flags = profile.flags().bits();
raw_header.device_manufacturer = profile.device_manufacturer().value_or(DeviceManufacturer { 0 });
raw_header.device_model = profile.device_model().value_or(DeviceModel { 0 });
raw_header.device_attributes = profile.device_attributes().bits();
raw_header.rendering_intent = profile.rendering_intent();
raw_header.pcs_illuminant = profile.pcs_illuminant();
raw_header.profile_creator = profile.creator().value_or(Creator { 0 });
memset(raw_header.reserved, 0, sizeof(raw_header.reserved));
auto id = Profile::compute_id(bytes);
static_assert(sizeof(id.data) == sizeof(raw_header.profile_id));
memcpy(raw_header.profile_id, id.data, sizeof(id.data));
return {};
}
ErrorOr<ByteBuffer> encode(Profile const& profile)
{
// Valid profiles always have tags. Profile only represents valid profiles.
VERIFY(profile.tag_count() > 0);
HashMap<TagData*, size_t> tag_data_map;
Vector<ByteBuffer> tag_data_bytes = TRY(encode_tag_datas(profile, tag_data_map));
u32 number_of_serialized_tags = 0;
profile.for_each_tag([&](auto tag_signature, auto tag_data) {
if (!tag_data_map.contains(tag_data.ptr())) {
dbgln("ICC serialization: dropping tag {} because it has unknown type {}", tag_signature, tag_data->type());
return;
}
number_of_serialized_tags++;
});
size_t tag_table_size = sizeof(u32) + number_of_serialized_tags * sizeof(TagTableEntry);
size_t offset = sizeof(ICCHeader) + tag_table_size;
Vector<size_t> offsets;
for (auto const& bytes : tag_data_bytes) {
TRY(offsets.try_append(offset));
offset += align_up_to(bytes.size(), 4);
}
// Include padding after last element. Use create_zeroed() to fill padding bytes with null bytes.
// ICC v4, 7.1.2:
// "c) all tagged element data, including the last, shall be padded by no more than three following pad bytes to
// reach a 4-byte boundary;
// d) all pad bytes shall be NULL (as defined in ISO/IEC 646, character 0/0).
// NOTE 1 This implies that the length is required to be a multiple of four."
auto bytes = TRY(ByteBuffer::create_zeroed(offset));
for (size_t i = 0; i < tag_data_bytes.size(); ++i)
memcpy(bytes.data() + offsets[i], tag_data_bytes[i].data(), tag_data_bytes[i].size());
TRY(encode_tag_table(bytes, profile, number_of_serialized_tags, offsets, tag_data_bytes, tag_data_map));
TRY(encode_header(bytes, profile));
return bytes;
}
}

View file

@ -1,21 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
namespace Gfx::ICC {
class Profile;
// Serializes a Profile object.
// Ignores the Profile's on_disk_size() and id() and recomputes them instead.
// Also ignores and the offsets and sizes in tag data.
// But if the profile has its tag data in tag order and has a computed id,
// it's a goal that encode(Profile::try_load_from_externally_owned_memory(bytes) returns `bytes`.
// Unconditionally computes a Profile ID (which is an MD5 hash of most of the contents, see Profile::compute_id()) and writes it to the output.
ErrorOr<ByteBuffer> encode(Profile const&);
}

View file

@ -1,78 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Format.h>
#include <AK/Types.h>
namespace Gfx::ICC {
// The ICC spec uses FourCCs for many different things.
// This is used to give FourCCs for different roles distinct types, so that they can only be compared to the correct constants.
// (FourCCs that have only a small and fixed set of values should use an enum class instead, see e.g. DeviceClass and ColorSpace in Enums.h.)
enum class FourCCType {
PreferredCMMType,
DeviceManufacturer,
DeviceModel,
Creator,
TagSignature,
TagTypeSignature,
};
template<FourCCType type>
struct [[gnu::packed]] DistinctFourCC {
constexpr explicit DistinctFourCC(u32 value)
: value(value)
{
}
constexpr operator u32() const { return value; }
char c0() const { return value >> 24; }
char c1() const { return (value >> 16) & 0xff; }
char c2() const { return (value >> 8) & 0xff; }
char c3() const { return value & 0xff; }
bool operator==(DistinctFourCC b) const { return value == b.value; }
u32 value { 0 };
};
using PreferredCMMType = DistinctFourCC<FourCCType::PreferredCMMType>; // ICC v4, "7.2.3 Preferred CMM type field"
using DeviceManufacturer = DistinctFourCC<FourCCType::DeviceManufacturer>; // ICC v4, "7.2.12 Device manufacturer field"
using DeviceModel = DistinctFourCC<FourCCType::DeviceModel>; // ICC v4, "7.2.13 Device model field"
using Creator = DistinctFourCC<FourCCType::Creator>; // ICC v4, "7.2.17 Profile creator field"
using TagSignature = DistinctFourCC<FourCCType::TagSignature>; // ICC v4, "9.2 Tag listing"
using TagTypeSignature = DistinctFourCC<FourCCType::TagTypeSignature>; // ICC v4, "10 Tag type definitions"
}
template<Gfx::ICC::FourCCType Type>
struct AK::Formatter<Gfx::ICC::DistinctFourCC<Type>> : StandardFormatter {
ErrorOr<void> format(FormatBuilder& builder, Gfx::ICC::DistinctFourCC<Type> const& four_cc)
{
TRY(builder.put_padding('\'', 1));
TRY(builder.put_padding(four_cc.c0(), 1));
TRY(builder.put_padding(four_cc.c1(), 1));
TRY(builder.put_padding(four_cc.c2(), 1));
TRY(builder.put_padding(four_cc.c3(), 1));
TRY(builder.put_padding('\'', 1));
return {};
}
};
template<Gfx::ICC::FourCCType Type>
struct AK::Traits<Gfx::ICC::DistinctFourCC<Type>> : public DefaultTraits<Gfx::ICC::DistinctFourCC<Type>> {
static unsigned hash(Gfx::ICC::DistinctFourCC<Type> const& key)
{
return int_hash(key.value);
}
static bool equals(Gfx::ICC::DistinctFourCC<Type> const& a, Gfx::ICC::DistinctFourCC<Type> const& b)
{
return a == b;
}
};

View file

@ -1,178 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/ICC/Enums.h>
namespace Gfx::ICC {
StringView device_class_name(DeviceClass device_class)
{
switch (device_class) {
case DeviceClass::InputDevice:
return "InputDevice"sv;
case DeviceClass::DisplayDevice:
return "DisplayDevice"sv;
case DeviceClass::OutputDevice:
return "OutputDevice"sv;
case DeviceClass::DeviceLink:
return "DeviceLink"sv;
case DeviceClass::ColorSpace:
return "ColorSpace"sv;
case DeviceClass::Abstract:
return "Abstract"sv;
case DeviceClass::NamedColor:
return "NamedColor"sv;
}
VERIFY_NOT_REACHED();
}
StringView data_color_space_name(ColorSpace color_space)
{
switch (color_space) {
case ColorSpace::nCIEXYZ:
return "nCIEXYZ"sv;
case ColorSpace::CIELAB:
return "CIELAB"sv;
case ColorSpace::CIELUV:
return "CIELUV"sv;
case ColorSpace::YCbCr:
return "YCbCr"sv;
case ColorSpace::CIEYxy:
return "CIEYxy"sv;
case ColorSpace::RGB:
return "RGB"sv;
case ColorSpace::Gray:
return "Gray"sv;
case ColorSpace::HSV:
return "HSV"sv;
case ColorSpace::HLS:
return "HLS"sv;
case ColorSpace::CMYK:
return "CMYK"sv;
case ColorSpace::CMY:
return "CMY"sv;
case ColorSpace::TwoColor:
return "2 color"sv;
case ColorSpace::ThreeColor:
return "3 color (other than XYZ, Lab, Luv, YCbCr, CIEYxy, RGB, HSV, HLS, CMY)"sv;
case ColorSpace::FourColor:
return "4 color (other than CMYK)"sv;
case ColorSpace::FiveColor:
return "5 color"sv;
case ColorSpace::SixColor:
return "6 color"sv;
case ColorSpace::SevenColor:
return "7 color"sv;
case ColorSpace::EightColor:
return "8 color"sv;
case ColorSpace::NineColor:
return "9 color"sv;
case ColorSpace::TenColor:
return "10 color"sv;
case ColorSpace::ElevenColor:
return "11 color"sv;
case ColorSpace::TwelveColor:
return "12 color"sv;
case ColorSpace::ThirteenColor:
return "13 color"sv;
case ColorSpace::FourteenColor:
return "14 color"sv;
case ColorSpace::FifteenColor:
return "15 color"sv;
}
VERIFY_NOT_REACHED();
}
StringView profile_connection_space_name(ColorSpace color_space)
{
switch (color_space) {
case ColorSpace::PCSXYZ:
return "PCSXYZ"sv;
case ColorSpace::PCSLAB:
return "PCSLAB"sv;
default:
return data_color_space_name(color_space);
}
}
unsigned number_of_components_in_color_space(ColorSpace color_space)
{
switch (color_space) {
case ColorSpace::Gray:
return 1;
case ColorSpace::TwoColor:
return 2;
case ColorSpace::nCIEXYZ:
case ColorSpace::CIELAB:
case ColorSpace::CIELUV:
case ColorSpace::YCbCr:
case ColorSpace::CIEYxy:
case ColorSpace::RGB:
case ColorSpace::HSV:
case ColorSpace::HLS:
case ColorSpace::CMY:
case ColorSpace::ThreeColor:
return 3;
case ColorSpace::CMYK:
case ColorSpace::FourColor:
return 4;
case ColorSpace::FiveColor:
return 5;
case ColorSpace::SixColor:
return 6;
case ColorSpace::SevenColor:
return 7;
case ColorSpace::EightColor:
return 8;
case ColorSpace::NineColor:
return 9;
case ColorSpace::TenColor:
return 10;
case ColorSpace::ElevenColor:
return 11;
case ColorSpace::TwelveColor:
return 12;
case ColorSpace::ThirteenColor:
return 13;
case ColorSpace::FourteenColor:
return 14;
case ColorSpace::FifteenColor:
return 15;
}
VERIFY_NOT_REACHED();
}
StringView primary_platform_name(PrimaryPlatform primary_platform)
{
switch (primary_platform) {
case PrimaryPlatform::Apple:
return "Apple"sv;
case PrimaryPlatform::Microsoft:
return "Microsoft"sv;
case PrimaryPlatform::SiliconGraphics:
return "Silicon Graphics"sv;
case PrimaryPlatform::Sun:
return "Sun"sv;
}
VERIFY_NOT_REACHED();
}
StringView rendering_intent_name(RenderingIntent rendering_intent)
{
switch (rendering_intent) {
case RenderingIntent::Perceptual:
return "Perceptual"sv;
case RenderingIntent::MediaRelativeColorimetric:
return "Media-relative colorimetric"sv;
case RenderingIntent::Saturation:
return "Saturation"sv;
case RenderingIntent::ICCAbsoluteColorimetric:
return "ICC-absolute colorimetric"sv;
}
VERIFY_NOT_REACHED();
}
}

View file

@ -1,77 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/StringView.h>
namespace Gfx::ICC {
// ICC v4, 7.2.5 Profile/device class field
enum class DeviceClass : u32 {
InputDevice = 0x73636E72, // 'scnr'
DisplayDevice = 0x6D6E7472, // 'mntr'
OutputDevice = 0x70727472, // 'prtr'
DeviceLink = 0x6C696E6B, // 'link'
ColorSpace = 0x73706163, // 'spac'
Abstract = 0x61627374, // 'abst'
NamedColor = 0x6E6D636C, // 'nmcl'
};
StringView device_class_name(DeviceClass);
// ICC v4, 7.2.6 Data colour space field, Table 19 — Data colour space signatures
enum class ColorSpace : u32 {
nCIEXYZ = 0x58595A20, // 'XYZ ', used in data color spaces.
PCSXYZ = nCIEXYZ, // Used in profile connection space instead.
CIELAB = 0x4C616220, // 'Lab ', used in data color spaces.
PCSLAB = CIELAB, // Used in profile connection space instead.
CIELUV = 0x4C757620, // 'Luv '
YCbCr = 0x59436272, // 'YCbr'
CIEYxy = 0x59787920, // 'Yxy '
RGB = 0x52474220, // 'RGB '
Gray = 0x47524159, // 'GRAY'
HSV = 0x48535620, // 'HSV '
HLS = 0x484C5320, // 'HLS '
CMYK = 0x434D594B, // 'CMYK'
CMY = 0x434D5920, // 'CMY '
TwoColor = 0x32434C52, // '2CLR'
ThreeColor = 0x33434C52, // '3CLR'
FourColor = 0x34434C52, // '4CLR'
FiveColor = 0x35434C52, // '5CLR'
SixColor = 0x36434C52, // '6CLR'
SevenColor = 0x37434C52, // '7CLR'
EightColor = 0x38434C52, // '8CLR'
NineColor = 0x39434C52, // '9CLR'
TenColor = 0x41434C52, // 'ACLR'
ElevenColor = 0x42434C52, // 'BCLR'
TwelveColor = 0x43434C52, // 'CCLR'
ThirteenColor = 0x44434C52, // 'DCLR'
FourteenColor = 0x45434C52, // 'ECLR'
FifteenColor = 0x46434C52, // 'FCLR'
};
StringView data_color_space_name(ColorSpace);
StringView profile_connection_space_name(ColorSpace);
unsigned number_of_components_in_color_space(ColorSpace);
// ICC v4, 7.2.10 Primary platform field, Table 20 — Primary platforms
enum class PrimaryPlatform : u32 {
Apple = 0x4150504C, // 'APPL'
Microsoft = 0x4D534654, // 'MSFT'
SiliconGraphics = 0x53474920, // 'SGI '
Sun = 0x53554E57, // 'SUNW'
};
StringView primary_platform_name(PrimaryPlatform);
// ICC v4, 7.2.15 Rendering intent field
enum class RenderingIntent {
Perceptual,
MediaRelativeColorimetric,
Saturation,
ICCAbsoluteColorimetric,
};
StringView rendering_intent_name(RenderingIntent);
}

File diff suppressed because it is too large Load diff

View file

@ -1,356 +0,0 @@
/*
* Copyright (c) 2022-2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/Format.h>
#include <AK/HashMap.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h>
#include <AK/Span.h>
#include <LibCrypto/Hash/MD5.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/CIELAB.h>
#include <LibGfx/ICC/DistinctFourCC.h>
#include <LibGfx/ICC/TagTypes.h>
#include <LibGfx/Matrix3x3.h>
#include <LibGfx/Vector3.h>
#include <LibURL/URL.h>
namespace Gfx::ICC {
URL::URL device_manufacturer_url(DeviceManufacturer);
URL::URL device_model_url(DeviceModel);
// ICC v4, 7.2.4 Profile version field
class Version {
public:
Version() = default;
Version(u8 major, u8 minor_and_bugfix)
: m_major_version(major)
, m_minor_and_bugfix_version(minor_and_bugfix)
{
}
u8 major_version() const { return m_major_version; }
u8 minor_version() const { return m_minor_and_bugfix_version >> 4; }
u8 bugfix_version() const { return m_minor_and_bugfix_version & 0xf; }
u8 minor_and_bugfix_version() const { return m_minor_and_bugfix_version; }
private:
u8 m_major_version = 0;
u8 m_minor_and_bugfix_version = 0;
};
// ICC v4, 7.2.11 Profile flags field
class Flags {
public:
Flags();
// "The profile flags field contains flags."
Flags(u32);
u32 bits() const { return m_bits; }
// "These can indicate various hints for the CMM such as distributed processing and caching options."
// "The least-significant 16 bits are reserved for the ICC."
u16 color_management_module_bits() const { return bits() >> 16; }
u16 icc_bits() const { return bits() & 0xffff; }
// "Bit position 0: Embedded profile (0 if not embedded, 1 if embedded in file)"
bool is_embedded_in_file() const { return (icc_bits() & 1) != 0; }
// "Bit position 1: Profile cannot be used independently of the embedded colour data (set to 1 if true, 0 if false)"
// Double negation isn't unconfusing, so this function uses the inverted, positive sense.
bool can_be_used_independently_of_embedded_color_data() const { return (icc_bits() & 2) == 0; }
static constexpr u32 KnownBitsMask = 3;
private:
u32 m_bits = 0;
};
// ICC v4, 7.2.14 Device attributes field
class DeviceAttributes {
public:
DeviceAttributes();
// "The device attributes field shall contain flags used to identify attributes
// unique to the particular device setup for which the profile is applicable."
DeviceAttributes(u64);
u64 bits() const { return m_bits; }
// "The least-significant 32 bits of this 64-bit value are defined by the ICC. "
u32 icc_bits() const { return bits() & 0xffff'ffff; }
// "Notice that bits 0, 1, 2, and 3 describe the media, not the device."
// "0": "Reflective (0) or transparency (1)"
enum class MediaReflectivity {
Reflective,
Transparent,
};
MediaReflectivity media_reflectivity() const { return MediaReflectivity(icc_bits() & 1); }
// "1": "Glossy (0) or matte (1)"
enum class MediaGlossiness {
Glossy,
Matte,
};
MediaGlossiness media_glossiness() const { return MediaGlossiness((icc_bits() >> 1) & 1); }
// "2": "Media polarity, positive (0) or negative (1)"
enum class MediaPolarity {
Positive,
Negative,
};
MediaPolarity media_polarity() const { return MediaPolarity((icc_bits() >> 2) & 1); }
// "3": "Colour media (0), black & white media (1)"
enum class MediaColor {
Colored,
BlackAndWhite,
};
MediaColor media_color() const { return MediaColor((icc_bits() >> 3) & 1); }
// "4 to 31": Reserved (set to binary zero)"
// "32 to 63": "Use not defined by ICC (vendor specific"
u32 vendor_bits() const { return bits() >> 32; }
static constexpr u64 KnownBitsMask = 0xf;
private:
u64 m_bits = 0;
};
// Time is in UTC.
// Per spec, month is 1-12, day is 1-31, hours is 0-23, minutes 0-59, seconds 0-59 (i.e. no leap seconds).
// But in practice, some profiles have invalid dates, like 0-0-0 0:0:0.
// For valid profiles, the conversion to time_t will succeed.
struct DateTime {
u16 year = 1970;
u16 month = 1; // One-based.
u16 day = 1; // One-based.
u16 hours = 0;
u16 minutes = 0;
u16 seconds = 0;
ErrorOr<time_t> to_time_t() const;
static ErrorOr<DateTime> from_time_t(time_t);
};
struct ProfileHeader {
u32 on_disk_size { 0 };
Optional<PreferredCMMType> preferred_cmm_type;
Version version;
DeviceClass device_class {};
ColorSpace data_color_space {};
ColorSpace connection_space {};
DateTime creation_timestamp;
Optional<PrimaryPlatform> primary_platform {};
Flags flags;
Optional<DeviceManufacturer> device_manufacturer;
Optional<DeviceModel> device_model;
DeviceAttributes device_attributes;
RenderingIntent rendering_intent {};
XYZ pcs_illuminant;
Optional<Creator> creator;
Optional<Crypto::Hash::MD5::DigestType> id;
};
// FIXME: This doesn't belong here.
class MatrixMatrixConversion {
public:
MatrixMatrixConversion(LutCurveType source_red_TRC,
LutCurveType source_green_TRC,
LutCurveType source_blue_TRC,
FloatMatrix3x3 matrix,
LutCurveType destination_red_TRC,
LutCurveType destination_green_TRC,
LutCurveType destination_blue_TRC);
Color map(FloatVector3) const;
private:
LutCurveType m_source_red_TRC;
LutCurveType m_source_green_TRC;
LutCurveType m_source_blue_TRC;
FloatMatrix3x3 m_matrix;
LutCurveType m_destination_red_TRC;
LutCurveType m_destination_green_TRC;
LutCurveType m_destination_blue_TRC;
};
inline Color MatrixMatrixConversion::map(FloatVector3 in_rgb) const
{
auto evaluate_curve = [](TagData const& trc, float f) {
if (trc.type() == CurveTagData::Type)
return static_cast<CurveTagData const&>(trc).evaluate(f);
return static_cast<ParametricCurveTagData const&>(trc).evaluate(f);
};
auto evaluate_curve_inverse = [](TagData const& trc, float f) {
if (trc.type() == CurveTagData::Type)
return static_cast<CurveTagData const&>(trc).evaluate_inverse(f);
return static_cast<ParametricCurveTagData const&>(trc).evaluate_inverse(f);
};
FloatVector3 linear_rgb = {
evaluate_curve(m_source_red_TRC, in_rgb[0]),
evaluate_curve(m_source_green_TRC, in_rgb[1]),
evaluate_curve(m_source_blue_TRC, in_rgb[2]),
};
linear_rgb = m_matrix * linear_rgb;
linear_rgb.clamp(0.f, 1.f);
float device_r = evaluate_curve_inverse(m_destination_red_TRC, linear_rgb[0]);
float device_g = evaluate_curve_inverse(m_destination_green_TRC, linear_rgb[1]);
float device_b = evaluate_curve_inverse(m_destination_blue_TRC, linear_rgb[2]);
u8 out_r = round(255 * device_r);
u8 out_g = round(255 * device_g);
u8 out_b = round(255 * device_b);
return Color(out_r, out_g, out_b);
}
class Profile : public RefCounted<Profile> {
public:
static ErrorOr<NonnullRefPtr<Profile>> try_load_from_externally_owned_memory(ReadonlyBytes);
static ErrorOr<NonnullRefPtr<Profile>> create(ProfileHeader const& header, OrderedHashMap<TagSignature, NonnullRefPtr<TagData>> tag_table);
Optional<PreferredCMMType> preferred_cmm_type() const { return m_header.preferred_cmm_type; }
Version version() const { return m_header.version; }
DeviceClass device_class() const { return m_header.device_class; }
ColorSpace data_color_space() const { return m_header.data_color_space; }
// For non-DeviceLink profiles, always PCSXYZ or PCSLAB.
ColorSpace connection_space() const { return m_header.connection_space; }
u32 on_disk_size() const { return m_header.on_disk_size; }
DateTime creation_timestamp() const { return m_header.creation_timestamp; }
Optional<PrimaryPlatform> primary_platform() const { return m_header.primary_platform; }
Flags flags() const { return m_header.flags; }
Optional<DeviceManufacturer> device_manufacturer() const { return m_header.device_manufacturer; }
Optional<DeviceModel> device_model() const { return m_header.device_model; }
DeviceAttributes device_attributes() const { return m_header.device_attributes; }
RenderingIntent rendering_intent() const { return m_header.rendering_intent; }
XYZ const& pcs_illuminant() const { return m_header.pcs_illuminant; }
Optional<Creator> creator() const { return m_header.creator; }
Optional<Crypto::Hash::MD5::DigestType> const& id() const { return m_header.id; }
static Crypto::Hash::MD5::DigestType compute_id(ReadonlyBytes);
template<typename Callback>
void for_each_tag(Callback callback) const
{
for (auto const& tag : m_tag_table)
callback(tag.key, tag.value);
}
template<FallibleFunction<TagSignature, NonnullRefPtr<TagData>> Callback>
ErrorOr<void> try_for_each_tag(Callback&& callback) const
{
for (auto const& tag : m_tag_table)
TRY(callback(tag.key, tag.value));
return {};
}
Optional<TagData const&> tag_data(TagSignature signature) const
{
return m_tag_table.get(signature).map([](auto it) -> TagData const& { return *it; });
}
Optional<String> tag_string_data(TagSignature signature) const;
size_t tag_count() const { return m_tag_table.size(); }
// Only versions 2 and 4 are in use.
bool is_v2() const { return version().major_version() == 2; }
bool is_v4() const { return version().major_version() == 4; }
// FIXME: The color conversion stuff should be in some other class.
// Converts an 8-bits-per-channel color to the profile connection space.
// The color's number of channels must match number_of_components_in_color_space(data_color_space()).
// Do not call for DeviceLink or NamedColor profiles. (XXX others?)
// Call connection_space() to find out the space the result is in.
ErrorOr<FloatVector3> to_pcs(ReadonlyBytes) const;
// Converts from the profile connection space to an 8-bits-per-channel color.
// The notes on `to_pcs()` apply to this too.
ErrorOr<void> from_pcs(Profile const& source_profile, FloatVector3, Bytes) const;
ErrorOr<CIELAB> to_lab(ReadonlyBytes) const;
ErrorOr<void> convert_image(Bitmap&, Profile const& source_profile) const;
ErrorOr<void> convert_cmyk_image(Bitmap&, CMYKBitmap const&, Profile const& source_profile) const;
// Only call these if you know that this is an RGB matrix-based profile.
XYZ const& red_matrix_column() const;
XYZ const& green_matrix_column() const;
XYZ const& blue_matrix_column() const;
Optional<MatrixMatrixConversion> matrix_matrix_conversion(Profile const& source_profile) const;
private:
Profile(ProfileHeader const& header, OrderedHashMap<TagSignature, NonnullRefPtr<TagData>> tag_table)
: m_header(header)
, m_tag_table(move(tag_table))
{
}
XYZ const& xyz_data(TagSignature tag) const
{
auto const& data = *m_tag_table.get(tag).value();
VERIFY(data.type() == XYZTagData::Type);
return static_cast<XYZTagData const&>(data).xyz();
}
ErrorOr<void> check_required_tags();
ErrorOr<void> check_tag_types();
ProfileHeader m_header;
OrderedHashMap<TagSignature, NonnullRefPtr<TagData>> m_tag_table;
// FIXME: The color conversion stuff should be in some other class.
ErrorOr<FloatVector3> to_pcs_a_to_b(TagData const& tag_data, ReadonlyBytes) const;
ErrorOr<void> from_pcs_b_to_a(TagData const& tag_data, FloatVector3 const&, Bytes) const;
ErrorOr<void> convert_image_matrix_matrix(Gfx::Bitmap&, MatrixMatrixConversion const&) const;
// Cached values.
bool m_cached_has_any_a_to_b_tag { false };
bool m_cached_has_a_to_b0_tag { false };
bool m_cached_has_any_b_to_a_tag { false };
bool m_cached_has_b_to_a0_tag { false };
bool m_cached_has_all_rgb_matrix_tags { false };
// Only valid for RGB matrix-based profiles.
ErrorOr<FloatMatrix3x3> xyz_to_rgb_matrix() const;
FloatMatrix3x3 rgb_to_xyz_matrix() const;
mutable Optional<FloatMatrix3x3> m_cached_xyz_to_rgb_matrix;
struct OneElementCLUTCache {
Vector<u8, 4> key;
FloatVector3 value;
};
mutable Optional<OneElementCLUTCache> m_to_pcs_clut_cache;
};
}
template<>
struct AK::Formatter<Gfx::ICC::Version> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, Gfx::ICC::Version const& version)
{
return Formatter<FormatString>::format(builder, "{}.{}.{}"sv, version.major_version(), version.minor_version(), version.bugfix_version());
}
};

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,23 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/ICC/Tags.h>
namespace Gfx::ICC {
Optional<StringView> tag_signature_spec_name(TagSignature tag_signature)
{
switch (tag_signature) {
#define TAG(name, id) \
case name: \
return #name##sv;
ENUMERATE_TAG_SIGNATURES(TAG)
#undef TAG
}
return {};
}
}

View file

@ -1,89 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Optional.h>
#include <AK/StringView.h>
#include <LibGfx/ICC/DistinctFourCC.h>
namespace Gfx::ICC {
// ICC v4, 9.2 Tag listing
#define ENUMERATE_TAG_SIGNATURES(TAG) \
TAG(AToB0Tag, 0x41324230 /* 'A2B0' */) \
TAG(AToB1Tag, 0x41324231 /* 'A2B1' */) \
TAG(AToB2Tag, 0x41324232 /* 'A2B2' */) \
TAG(blueMatrixColumnTag, 0x6258595A /* 'bXYZ' */) \
TAG(blueTRCTag, 0x62545243 /* 'bTRC' */) \
TAG(BToA0Tag, 0x42324130 /* 'B2A0' */) \
TAG(BToA1Tag, 0x42324131 /* 'B2A1' */) \
TAG(BToA2Tag, 0x42324132 /* 'B2A2' */) \
TAG(BToD0Tag, 0x42324430 /* 'B2D0' */) \
TAG(BToD1Tag, 0x42324431 /* 'B2D1' */) \
TAG(BToD2Tag, 0x42324432 /* 'B2D2' */) \
TAG(BToD3Tag, 0x42324433 /* 'B2D3' */) \
TAG(calibrationDateTimeTag, 0x63616C74 /* 'calt' */) \
TAG(charTargetTag, 0x74617267 /* 'targ' */) \
TAG(chromaticAdaptationTag, 0x63686164 /* 'chad' */) \
TAG(chromaticityTag, 0x6368726D /* 'chrm' */) \
TAG(cicpTag, 0x63696370 /* 'cicp' */) \
TAG(colorantOrderTag, 0x636C726F /* 'clro' */) \
TAG(colorantTableTag, 0x636C7274 /* 'clrt' */) \
TAG(colorantTableOutTag, 0x636C6F74 /* 'clot' */) \
TAG(colorimetricIntentImageStateTag, 0x63696973 /* 'ciis' */) \
TAG(copyrightTag, 0x63707274 /* 'cprt' */) \
TAG(deviceMfgDescTag, 0x646D6E64 /* 'dmnd' */) \
TAG(deviceModelDescTag, 0x646D6464 /* 'dmdd' */) \
TAG(DToB0Tag, 0x44324230 /* 'D2B0' */) \
TAG(DToB1Tag, 0x44324231 /* 'D2B1' */) \
TAG(DToB2Tag, 0x44324232 /* 'D2B2' */) \
TAG(DToB3Tag, 0x44324233 /* 'D2B3' */) \
TAG(gamutTag, 0x67616D74 /* 'gamt' */) \
TAG(grayTRCTag, 0x6B545243 /* 'kTRC' */) \
TAG(greenMatrixColumnTag, 0x6758595A /* 'gXYZ' */) \
TAG(greenTRCTag, 0x67545243 /* 'gTRC' */) \
TAG(luminanceTag, 0x6C756D69 /* 'lumi' */) \
TAG(measurementTag, 0x6D656173 /* 'meas' */) \
TAG(metadataTag, 0x6D657461 /* 'meta' */) \
TAG(mediaWhitePointTag, 0x77747074 /* 'wtpt' */) \
TAG(namedColor2Tag, 0x6E636C32 /* 'ncl2' */) \
TAG(outputResponseTag, 0x72657370 /* 'resp' */) \
TAG(perceptualRenderingIntentGamutTag, 0x72696730 /* 'rig0' */) \
TAG(preview0Tag, 0x70726530 /* 'pre0' */) \
TAG(preview1Tag, 0x70726531 /* 'pre1' */) \
TAG(preview2Tag, 0x70726532 /* 'pre2' */) \
TAG(profileDescriptionTag, 0x64657363 /* 'desc' */) \
TAG(profileSequenceDescTag, 0x70736571 /* 'pseq' */) \
TAG(profileSequenceIdentifierTag, 0x70736964 /* 'psid' */) \
TAG(redMatrixColumnTag, 0x7258595A /* 'rXYZ' */) \
TAG(redTRCTag, 0x72545243 /* 'rTRC' */) \
TAG(saturationRenderingIntentGamutTag, 0x72696732 /* 'rig2' */) \
TAG(technologyTag, 0x74656368 /* 'tech' */) \
TAG(viewingCondDescTag, 0x76756564 /* 'vued' */) \
TAG(viewingConditionsTag, 0x76696577 /* 'view' */) \
/* The following tags are v2-only */ \
TAG(crdInfoTag, 0x63726469 /* 'crdi' */) \
TAG(deviceSettingsTag, 0x64657673 /* 'devs' */) \
TAG(mediaBlackPointTag, 0x626B7074 /* 'bkpt' */) \
TAG(namedColorTag, 0x6E636F6C /* 'ncol' */) \
TAG(ps2CRD0Tag, 0x70736430 /* 'psd0' */) \
TAG(ps2CRD1Tag, 0x70736431 /* 'psd1' */) \
TAG(ps2CRD2Tag, 0x70736432 /* 'psd2' */) \
TAG(ps2CRD3Tag, 0x70736433 /* 'psd3' */) \
TAG(ps2CSATag, 0x70733273 /* 'ps2s' */) \
TAG(ps2RenderingIntentTag, 0x70733269 /* 'ps2i' */) \
TAG(screeningDescTag, 0x73637264 /* 'scrd' */) \
TAG(screeningTag, 0x7363726E /* 'scrn' */) \
TAG(ucrbgTag, 0x62666420 /* 'bfd ' */)
#define TAG(name, id) constexpr inline TagSignature name { id };
ENUMERATE_TAG_SIGNATURES(TAG)
#undef TAG
Optional<StringView> tag_signature_spec_name(TagSignature);
}

View file

@ -1,89 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/ICC/Profile.h>
#include <LibGfx/ICC/Tags.h>
#include <LibGfx/ICC/WellKnownProfiles.h>
namespace Gfx::ICC {
static ProfileHeader rgb_header()
{
ProfileHeader header;
header.version = Version(4, 0x40);
header.device_class = DeviceClass::DisplayDevice;
header.data_color_space = ColorSpace::RGB;
header.connection_space = ColorSpace::PCSXYZ;
header.creation_timestamp = MUST(DateTime::from_time_t(0));
header.rendering_intent = RenderingIntent::Perceptual;
header.pcs_illuminant = XYZ { 0.9642, 1.0, 0.8249 };
return header;
}
static ErrorOr<NonnullRefPtr<MultiLocalizedUnicodeTagData>> en_US(StringView text)
{
Vector<MultiLocalizedUnicodeTagData::Record> records;
TRY(records.try_append({ ('e' << 8) | 'n', ('U' << 8) | 'S', TRY(String::from_utf8(text)) }));
return try_make_ref_counted<MultiLocalizedUnicodeTagData>(0, 0, records);
}
static ErrorOr<NonnullRefPtr<XYZTagData>> XYZ_data(XYZ xyz)
{
Vector<XYZ> xyzs;
TRY(xyzs.try_append(xyz));
return try_make_ref_counted<XYZTagData>(0, 0, move(xyzs));
}
ErrorOr<NonnullRefPtr<TagData>> sRGB_curve()
{
// Numbers from https://en.wikipedia.org/wiki/SRGB#From_sRGB_to_CIE_XYZ
Array<S15Fixed16, 7> curve_parameters = { 2.4, 1 / 1.055, 0.055 / 1.055, 1 / 12.92, 0.04045 };
return try_make_ref_counted<ParametricCurveTagData>(0, 0, ParametricCurveTagData::FunctionType::sRGB, curve_parameters);
}
ErrorOr<NonnullRefPtr<Profile>> sRGB()
{
// Returns an sRGB profile.
// https://en.wikipedia.org/wiki/SRGB
// FIXME: There are many different sRGB ICC profiles in the wild.
// Explain why, and why this picks the numbers it does.
// In the meantime, https://github.com/SerenityOS/serenity/pull/17714 has a few notes.
auto header = rgb_header();
OrderedHashMap<TagSignature, NonnullRefPtr<TagData>> tag_table;
TRY(tag_table.try_set(profileDescriptionTag, TRY(en_US("SerenityOS sRGB"sv))));
TRY(tag_table.try_set(copyrightTag, TRY(en_US("Public Domain"sv))));
// Transfer function.
auto curve = TRY(sRGB_curve());
TRY(tag_table.try_set(redTRCTag, curve));
TRY(tag_table.try_set(greenTRCTag, curve));
TRY(tag_table.try_set(blueTRCTag, curve));
// White point.
// ICC v4, 9.2.36 mediaWhitePointTag: "For displays, the values specified shall be those of the PCS illuminant as defined in 7.2.16."
TRY(tag_table.try_set(mediaWhitePointTag, TRY(XYZ_data(header.pcs_illuminant))));
// The chromatic_adaptation_matrix values are from https://www.color.org/chadtag.xalter
// That leads to exactly the S15Fixed16 values in the sRGB profiles in GIMP, Android, RawTherapee (but not in Compact-ICC-Profiles's v4 sRGB profile).
Vector<S15Fixed16, 9> chromatic_adaptation_matrix = { 1.047882, 0.022918, -0.050217, 0.029586, 0.990478, -0.017075, -0.009247, 0.015075, 0.751678 };
TRY(tag_table.try_set(chromaticAdaptationTag, TRY(try_make_ref_counted<S15Fixed16ArrayTagData>(0, 0, move(chromatic_adaptation_matrix)))));
// The chromaticity values are from https://www.color.org/srgb.pdf
// The chromatic adaptation matrix in that document is slightly different from the one on https://www.color.org/chadtag.xalter,
// so the values in our sRGB profile are currently not fully self-consistent.
// FIXME: Make values self-consistent (probably by using slightly different chromaticities).
TRY(tag_table.try_set(redMatrixColumnTag, TRY(XYZ_data(XYZ { 0.436030342570117, 0.222438466210245, 0.013897440074263 }))));
TRY(tag_table.try_set(greenMatrixColumnTag, TRY(XYZ_data(XYZ { 0.385101860087134, 0.716942745571917, 0.097076381494207 }))));
TRY(tag_table.try_set(blueMatrixColumnTag, TRY(XYZ_data(XYZ { 0.143067806654203, 0.060618777416563, 0.713926257896652 }))));
return Profile::create(header, move(tag_table));
}
}

View file

@ -1,21 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/NonnullRefPtr.h>
namespace Gfx::ICC {
class Profile;
class TagData;
ErrorOr<NonnullRefPtr<Profile>> sRGB();
ErrorOr<NonnullRefPtr<TagData>> sRGB_curve();
}

View file

@ -19,7 +19,6 @@ serenity_option(UNDEFINED_BEHAVIOR_IS_FATAL OFF CACHE BOOL "Make undefined behav
serenity_option(ENABLE_ALL_THE_DEBUG_MACROS OFF CACHE BOOL "Enable all debug macros to validate they still compile")
serenity_option(ENABLE_ALL_DEBUG_FACILITIES OFF CACHE BOOL "Enable all noisy debug symbols and options. Not recommended for normal developer use")
serenity_option(ENABLE_ADOBE_ICC_PROFILES_DOWNLOAD ON CACHE BOOL "Enable download of Adobe's ICC profiles")
serenity_option(ENABLE_COMPILETIME_HEADER_CHECK OFF CACHE BOOL "Enable compiletime check that each library header compiles stand-alone")
serenity_option(ENABLE_PUBLIC_SUFFIX_DOWNLOAD ON CACHE BOOL "Enable download of the Public Suffix List at build time")

View file

@ -1,25 +0,0 @@
include(${CMAKE_CURRENT_LIST_DIR}/utils.cmake)
if (ENABLE_ADOBE_ICC_PROFILES_DOWNLOAD)
set(ADOBE_ICC_PROFILES_PATH "${SERENITY_CACHE_DIR}/AdobeICCProfiles" CACHE PATH "Download location for Adobe ICC profiles")
set(ADOBE_ICC_PROFILES_DATA_URL "https://download.adobe.com/pub/adobe/iccprofiles/win/AdobeICCProfilesCS4Win_end-user.zip")
set(ADOBE_ICC_PROFILES_ZIP_PATH "${ADOBE_ICC_PROFILES_PATH}/adobe-icc-profiles.zip")
if (ENABLE_NETWORK_DOWNLOADS)
download_file("${ADOBE_ICC_PROFILES_DATA_URL}" "${ADOBE_ICC_PROFILES_ZIP_PATH}")
else()
message(STATUS "Skipping download of ${ADOBE_ICC_PROFILES_DATA_URL}, expecting the archive to have been donwloaded to ${ADOBE_ICC_PROFILES_ZIP_PATH}")
endif()
function(extract_adobe_icc_profiles source path)
if(EXISTS "${ADOBE_ICC_PROFILES_ZIP_PATH}" AND NOT EXISTS "${path}")
file(ARCHIVE_EXTRACT INPUT "${ADOBE_ICC_PROFILES_ZIP_PATH}" DESTINATION "${ADOBE_ICC_PROFILES_PATH}" PATTERNS "${source}")
endif()
endfunction()
set(ADOBE_ICC_CMYK_SWOP "CMYK/USWebCoatedSWOP.icc")
set(ADOBE_ICC_CMYK_SWOP_PATH "${ADOBE_ICC_PROFILES_PATH}/Adobe ICC Profiles (end-user)/${ADOBE_ICC_CMYK_SWOP}")
extract_adobe_icc_profiles("Adobe ICC Profiles (end-user)/${ADOBE_ICC_CMYK_SWOP}" "${ADOBE_ICC_CMYK_SWOP_PATH}")
set(ADOBE_ICC_CMYK_SWOP_INSTALL_PATH "${CMAKE_BINARY_DIR}/Root/res/icc/Adobe/${ADOBE_ICC_CMYK_SWOP}")
configure_file("${ADOBE_ICC_CMYK_SWOP_PATH}" "${ADOBE_ICC_CMYK_SWOP_INSTALL_PATH}" COPYONLY)
endif()

View file

@ -452,7 +452,6 @@ lagom_utility(dns SOURCES ../../Utilities/dns.cpp LIBS LibDNS LibMain LibTLS Lib
if (ENABLE_GUI_TARGETS)
lagom_utility(animation SOURCES ../../Utilities/animation.cpp LIBS LibGfx LibMain)
lagom_utility(icc SOURCES ../../Utilities/icc.cpp LIBS LibGfx LibMain LibURL)
lagom_utility(image SOURCES ../../Utilities/image.cpp LIBS LibGfx LibMain)
endif()

View file

@ -1,16 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/ICC/Profile.h>
#include <stddef.h>
#include <stdint.h>
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
{
AK::set_debug_enabled(false);
(void)Gfx::ICC::Profile::try_load_from_externally_owned_memory({ data, size });
return 0;
}

View file

@ -9,7 +9,6 @@ set(FUZZER_TARGETS
GIFLoader
GzipDecompression
GzipRoundtrip
ICCProfile
ICOLoader
JPEGLoader
Js
@ -56,7 +55,6 @@ set(FUZZER_DEPENDENCIES_ELF LibELF)
set(FUZZER_DEPENDENCIES_GIFLoader LibGfx)
set(FUZZER_DEPENDENCIES_GzipDecompression LibCompress)
set(FUZZER_DEPENDENCIES_GzipRoundtrip LibCompress)
set(FUZZER_DEPENDENCIES_ICCProfile LibGfx)
set(FUZZER_DEPENDENCIES_ICOLoader LibGfx)
set(FUZZER_DEPENDENCIES_JPEGLoader LibGfx)
set(FUZZER_DEPENDENCIES_Js LibJS LibGC)

View file

@ -29,7 +29,6 @@ shared_library("LibGfx") {
"BitmapSequence.cpp",
"CMYKBitmap.cpp",
"Color.cpp",
"DeltaE.cpp",
"Font/Font.cpp",
"Font/FontData.cpp",
"Font/FontDatabase.cpp",
@ -41,12 +40,6 @@ shared_library("LibGfx") {
"Font/WOFF2/Loader.cpp",
"FontCascadeList.cpp",
"GradientPainting.cpp",
"ICC/BinaryWriter.cpp",
"ICC/Enums.cpp",
"ICC/Profile.cpp",
"ICC/TagTypes.cpp",
"ICC/Tags.cpp",
"ICC/WellKnownProfiles.cpp",
"ImageFormats/AVIFLoader.cpp",
"ImageFormats/AnimationWriter.cpp",
"ImageFormats/BMPLoader.cpp",

View file

@ -1,8 +1,6 @@
set(TEST_SOURCES
BenchmarkJPEGLoader.cpp
TestColor.cpp
TestDeltaE.cpp
TestICCProfile.cpp
TestImageDecoder.cpp
TestImageWriter.cpp
TestQuad.cpp

View file

@ -1,61 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/DeltaE.h>
#include <LibTest/TestCase.h>
TEST_CASE(delta_e)
{
// Test data is from https://hajim.rochester.edu/ece/sites/gsharma/ciede2000/dataNprograms/ciede2000testdata.txt,
// linked to from https://hajim.rochester.edu/ece/sites/gsharma/ciede2000/, which is source [5] in
// https://www.hajim.rochester.edu/ece/sites/gsharma/ciede2000/ciede2000noteCRNA.pdf
struct {
Gfx::CIELAB a;
Gfx::CIELAB b;
float expected_delta;
} test_cases[] = {
{ { 50.0000, 2.6772, -79.7751 }, { 50.0000, 0.0000, -82.7485 }, 2.0425 },
{ { 50.0000, 3.1571, -77.2803 }, { 50.0000, 0.0000, -82.7485 }, 2.8615 },
{ { 50.0000, 2.8361, -74.0200 }, { 50.0000, 0.0000, -82.7485 }, 3.4412 },
{ { 50.0000, -1.3802, -84.2814 }, { 50.0000, 0.0000, -82.7485 }, 1.0000 },
{ { 50.0000, -1.1848, -84.8006 }, { 50.0000, 0.0000, -82.7485 }, 1.0000 },
{ { 50.0000, -0.9009, -85.5211 }, { 50.0000, 0.0000, -82.7485 }, 1.0000 },
{ { 50.0000, 0.0000, 0.0000 }, { 50.0000, -1.0000, 2.0000 }, 2.3669 },
{ { 50.0000, -1.0000, 2.0000 }, { 50.0000, 0.0000, 0.0000 }, 2.3669 },
{ { 50.0000, 2.4900, -0.0010 }, { 50.0000, -2.4900, 0.0009 }, 7.1792 },
{ { 50.0000, 2.4900, -0.0010 }, { 50.0000, -2.4900, 0.0010 }, 7.1792 },
{ { 50.0000, 2.4900, -0.0010 }, { 50.0000, -2.4900, 0.0011 }, 7.2195 },
{ { 50.0000, 2.4900, -0.0010 }, { 50.0000, -2.4900, 0.0012 }, 7.2195 },
{ { 50.0000, -0.0010, 2.4900 }, { 50.0000, 0.0009, -2.4900 }, 4.8045 },
{ { 50.0000, -0.0010, 2.4900 }, { 50.0000, 0.0010, -2.4900 }, 4.8045 },
{ { 50.0000, -0.0010, 2.4900 }, { 50.0000, 0.0011, -2.4900 }, 4.7461 },
{ { 50.0000, 2.5000, 0.0000 }, { 50.0000, 0.0000, -2.5000 }, 4.3065 },
{ { 50.0000, 2.5000, 0.0000 }, { 73.0000, 25.0000, -18.0000 }, 27.1492 },
{ { 50.0000, 2.5000, 0.0000 }, { 61.0000, -5.0000, 29.0000 }, 22.8977 },
{ { 50.0000, 2.5000, 0.0000 }, { 56.0000, -27.0000, -3.0000 }, 31.9030 },
{ { 50.0000, 2.5000, 0.0000 }, { 58.0000, 24.0000, 15.0000 }, 19.4535 },
{ { 50.0000, 2.5000, 0.0000 }, { 50.0000, 3.1736, 0.5854 }, 1.0000 },
{ { 50.0000, 2.5000, 0.0000 }, { 50.0000, 3.2972, 0.0000 }, 1.0000 },
{ { 50.0000, 2.5000, 0.0000 }, { 50.0000, 1.8634, 0.5757 }, 1.0000 },
{ { 50.0000, 2.5000, 0.0000 }, { 50.0000, 3.2592, 0.3350 }, 1.0000 },
{ { 60.2574, -34.0099, 36.2677 }, { 60.4626, -34.1751, 39.4387 }, 1.2644 },
{ { 63.0109, -31.0961, -5.8663 }, { 62.8187, -29.7946, -4.0864 }, 1.2630 },
{ { 61.2901, 3.7196, -5.3901 }, { 61.4292, 2.2480, -4.9620 }, 1.8731 },
{ { 35.0831, -44.1164, 3.7933 }, { 35.0232, -40.0716, 1.5901 }, 1.8645 },
{ { 22.7233, 20.0904, -46.6940 }, { 23.0331, 14.9730, -42.5619 }, 2.0373 },
{ { 36.4612, 47.8580, 18.3852 }, { 36.2715, 50.5065, 21.2231 }, 1.4146 },
{ { 90.8027, -2.0831, 1.4410 }, { 91.1528, -1.6435, 0.0447 }, 1.4441 },
{ { 90.9257, -0.5406, -0.9208 }, { 88.6381, -0.8985, -0.7239 }, 1.5381 },
{ { 6.7747, -0.2908, -2.4247 }, { 5.8714, -0.0985, -2.2286 }, 0.6377 },
{ { 2.0776, 0.0795, -1.1350 }, { 0.9033, -0.0636, -0.5514 }, 0.9082 },
};
for (auto const& test_case : test_cases) {
// The inputs are given with 4 decimal digits, so we can be up to 0.00005 away just to rounding to 4 decimal digits.
EXPECT_APPROXIMATE_WITH_ERROR(Gfx::DeltaE(test_case.a, test_case.b), test_case.expected_delta, 0.00005);
EXPECT_APPROXIMATE_WITH_ERROR(Gfx::DeltaE(test_case.b, test_case.a), test_case.expected_delta, 0.00005);
}
}

View file

@ -1,286 +0,0 @@
/*
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Endian.h>
#include <LibCore/MappedFile.h>
#include <LibGfx/ICC/BinaryWriter.h>
#include <LibGfx/ICC/Profile.h>
#include <LibGfx/ICC/Tags.h>
#include <LibGfx/ICC/WellKnownProfiles.h>
#include <LibGfx/ImageFormats/JPEGLoader.h>
#include <LibGfx/ImageFormats/PNGLoader.h>
#include <LibGfx/ImageFormats/TIFFLoader.h>
#include <LibGfx/ImageFormats/WebPLoader.h>
#include <LibTest/TestCase.h>
#define TEST_INPUT(x) ("test-inputs/" x)
TEST_CASE(png)
{
auto file = MUST(Core::MappedFile::map(TEST_INPUT("icc/icc-v2.png"sv)));
auto png = MUST(Gfx::PNGImageDecoderPlugin::create(file->bytes()));
auto icc_bytes = MUST(png->icc_data());
EXPECT(icc_bytes.has_value());
auto icc_profile = MUST(Gfx::ICC::Profile::try_load_from_externally_owned_memory(icc_bytes.value()));
EXPECT(icc_profile->is_v2());
}
TEST_CASE(jpg)
{
auto file = MUST(Core::MappedFile::map(TEST_INPUT("icc/icc-v4.jpg"sv)));
auto jpg = MUST(Gfx::JPEGImageDecoderPlugin::create(file->bytes()));
auto icc_bytes = MUST(jpg->icc_data());
EXPECT(icc_bytes.has_value());
auto icc_profile = MUST(Gfx::ICC::Profile::try_load_from_externally_owned_memory(icc_bytes.value()));
EXPECT(icc_profile->is_v4());
icc_profile->for_each_tag([](auto tag_signature, auto tag_data) {
if (tag_signature == Gfx::ICC::profileDescriptionTag) {
// Required per v4 spec, but in practice even v4 files sometimes have TextDescriptionTagData descriptions. Not icc-v4.jpg, though.
EXPECT_EQ(tag_data->type(), Gfx::ICC::MultiLocalizedUnicodeTagData::Type);
auto& multi_localized_unicode = static_cast<Gfx::ICC::MultiLocalizedUnicodeTagData&>(*tag_data);
EXPECT_EQ(multi_localized_unicode.records().size(), 1u);
auto& record = multi_localized_unicode.records()[0];
EXPECT_EQ(record.text, "sRGB built-in"sv);
}
});
}
TEST_CASE(webp_extended_lossless)
{
auto file = MUST(Core::MappedFile::map(TEST_INPUT("icc/extended-lossless.webp"sv)));
auto webp = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
auto icc_bytes = MUST(webp->icc_data());
EXPECT(icc_bytes.has_value());
auto icc_profile = MUST(Gfx::ICC::Profile::try_load_from_externally_owned_memory(icc_bytes.value()));
EXPECT(icc_profile->is_v2());
}
TEST_CASE(webp_extended_lossy)
{
auto file = MUST(Core::MappedFile::map(TEST_INPUT("icc/extended-lossy.webp"sv)));
auto webp = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes()));
auto icc_bytes = MUST(webp->icc_data());
EXPECT(icc_bytes.has_value());
auto icc_profile = MUST(Gfx::ICC::Profile::try_load_from_externally_owned_memory(icc_bytes.value()));
EXPECT(icc_profile->is_v2());
}
TEST_CASE(tiff)
{
auto file = MUST(Core::MappedFile::map(TEST_INPUT("icc/icc.tiff"sv)));
auto tiff = MUST(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
auto icc_bytes = MUST(tiff->icc_data());
EXPECT(icc_bytes.has_value());
auto icc_profile = MUST(Gfx::ICC::Profile::try_load_from_externally_owned_memory(icc_bytes.value()));
EXPECT(icc_profile->is_v4());
}
TEST_CASE(serialize_icc)
{
auto file = MUST(Core::MappedFile::map(TEST_INPUT("icc/p3-v4.icc"sv)));
auto icc_profile = MUST(Gfx::ICC::Profile::try_load_from_externally_owned_memory(file->bytes()));
EXPECT(icc_profile->is_v4());
auto serialized_bytes = MUST(Gfx::ICC::encode(*icc_profile));
EXPECT_EQ(serialized_bytes, file->bytes());
}
TEST_CASE(built_in_sRGB)
{
auto sRGB = MUST(Gfx::ICC::sRGB());
auto serialized_bytes = MUST(Gfx::ICC::encode(sRGB));
// We currently exactly match the curve in GIMP's built-in sRGB profile. It's a type 3 'para' curve with 5 parameters.
u32 para[] = { 0x70617261, 0x00000000, 0x00030000, 0x00026666, 0x0000F2A7, 0x00000D59, 0x000013D0, 0x00000A5B };
for (u32& i : para)
i = AK::convert_between_host_and_big_endian(i);
EXPECT(memmem(serialized_bytes.data(), serialized_bytes.size(), para, sizeof(para)) != nullptr);
// We currently exactly match the chromatic adaptation matrix in GIMP's (and other's) built-in sRGB profile.
u32 sf32[] = { 0x73663332, 0x00000000, 0x00010C42, 0x000005DE, 0xFFFFF325, 0x00000793, 0x0000FD90, 0xFFFFFBA1, 0xFFFFFDA2, 0x000003DC, 0x0000C06E };
for (u32& i : sf32)
i = AK::convert_between_host_and_big_endian(i);
EXPECT(memmem(serialized_bytes.data(), serialized_bytes.size(), sf32, sizeof(sf32)) != nullptr);
}
TEST_CASE(to_pcs)
{
auto sRGB = MUST(Gfx::ICC::sRGB());
EXPECT(sRGB->data_color_space() == Gfx::ICC::ColorSpace::RGB);
EXPECT(sRGB->connection_space() == Gfx::ICC::ColorSpace::PCSXYZ);
auto sRGB_curve_pointer = MUST(Gfx::ICC::sRGB_curve());
VERIFY(sRGB_curve_pointer->type() == Gfx::ICC::ParametricCurveTagData::Type);
auto const& sRGB_curve = static_cast<Gfx::ICC::ParametricCurveTagData const&>(*sRGB_curve_pointer);
EXPECT_EQ(sRGB_curve.evaluate(0.f), 0.f);
EXPECT_EQ(sRGB_curve.evaluate(1.f), 1.f);
auto xyz_from_sRGB = [&sRGB](u8 r, u8 g, u8 b) {
u8 rgb[3] = { r, g, b };
return MUST(sRGB->to_pcs(rgb));
};
auto vec3_from_xyz = [](Gfx::ICC::XYZ const& xyz) {
return FloatVector3 { xyz.X, xyz.Y, xyz.Z };
};
#define EXPECT_APPROXIMATE_VECTOR3(v1, v2) \
EXPECT_APPROXIMATE((v1)[0], (v2)[0]); \
EXPECT_APPROXIMATE((v1)[1], (v2)[1]); \
EXPECT_APPROXIMATE((v1)[2], (v2)[2]);
// At 0 and 255, the gamma curve is (exactly) 0 and 1, so these just test the matrix part.
EXPECT_APPROXIMATE_VECTOR3(xyz_from_sRGB(0, 0, 0), FloatVector3(0, 0, 0));
auto r_xyz = vec3_from_xyz(sRGB->red_matrix_column());
EXPECT_APPROXIMATE_VECTOR3(xyz_from_sRGB(255, 0, 0), r_xyz);
auto g_xyz = vec3_from_xyz(sRGB->green_matrix_column());
EXPECT_APPROXIMATE_VECTOR3(xyz_from_sRGB(0, 255, 0), g_xyz);
auto b_xyz = vec3_from_xyz(sRGB->blue_matrix_column());
EXPECT_APPROXIMATE_VECTOR3(xyz_from_sRGB(0, 0, 255), b_xyz);
EXPECT_APPROXIMATE_VECTOR3(xyz_from_sRGB(255, 255, 0), r_xyz + g_xyz);
EXPECT_APPROXIMATE_VECTOR3(xyz_from_sRGB(255, 0, 255), r_xyz + b_xyz);
EXPECT_APPROXIMATE_VECTOR3(xyz_from_sRGB(0, 255, 255), g_xyz + b_xyz);
// FIXME: This should also be equal to sRGB->pcs_illuminant() and to the profiles mediaWhitePointTag,
// but at the moment it's off by a bit too much. See also FIXME in WellKnownProfiles.cpp.
EXPECT_APPROXIMATE_VECTOR3(xyz_from_sRGB(255, 255, 255), r_xyz + g_xyz + b_xyz);
// These test the curve part.
float f64 = sRGB_curve.evaluate(64 / 255.f);
EXPECT_APPROXIMATE_VECTOR3(xyz_from_sRGB(64, 64, 64), (r_xyz + g_xyz + b_xyz) * f64);
float f128 = sRGB_curve.evaluate(128 / 255.f);
EXPECT_APPROXIMATE_VECTOR3(xyz_from_sRGB(128, 128, 128), (r_xyz + g_xyz + b_xyz) * f128);
// Test for curve and matrix combined.
float f192 = sRGB_curve.evaluate(192 / 255.f);
EXPECT_APPROXIMATE_VECTOR3(xyz_from_sRGB(64, 128, 192), r_xyz * f64 + g_xyz * f128 + b_xyz * f192);
}
TEST_CASE(from_pcs)
{
auto sRGB = MUST(Gfx::ICC::sRGB());
auto sRGB_curve_pointer = MUST(Gfx::ICC::sRGB_curve());
VERIFY(sRGB_curve_pointer->type() == Gfx::ICC::ParametricCurveTagData::Type);
auto const& sRGB_curve = static_cast<Gfx::ICC::ParametricCurveTagData const&>(*sRGB_curve_pointer);
auto sRGB_from_xyz = [&sRGB](FloatVector3 const& XYZ) {
u8 rgb[3];
// The first parameter, the source profile, is used to check if the PCS data is XYZ or LAB,
// and what the source whitepoint is. We just need any profile with an XYZ PCS space,
// so passing sRGB as source profile too is fine.
MUST(sRGB->from_pcs(sRGB, XYZ, rgb));
return Color(rgb[0], rgb[1], rgb[2]);
};
auto vec3_from_xyz = [](Gfx::ICC::XYZ const& xyz) {
return FloatVector3 { xyz.X, xyz.Y, xyz.Z };
};
// At 0 and 255, the gamma curve is (exactly) 0 and 1, so these just test the matrix part.
EXPECT_EQ(sRGB_from_xyz(FloatVector3 { 0, 0, 0 }), Color(0, 0, 0));
auto r_xyz = vec3_from_xyz(sRGB->red_matrix_column());
EXPECT_EQ(sRGB_from_xyz(r_xyz), Color(255, 0, 0));
auto g_xyz = vec3_from_xyz(sRGB->green_matrix_column());
EXPECT_EQ(sRGB_from_xyz(g_xyz), Color(0, 255, 0));
auto b_xyz = vec3_from_xyz(sRGB->blue_matrix_column());
EXPECT_EQ(sRGB_from_xyz(b_xyz), Color(0, 0, 255));
EXPECT_EQ(sRGB_from_xyz(r_xyz + g_xyz), Color(255, 255, 0));
EXPECT_EQ(sRGB_from_xyz(r_xyz + b_xyz), Color(255, 0, 255));
EXPECT_EQ(sRGB_from_xyz(g_xyz + b_xyz), Color(0, 255, 255));
EXPECT_EQ(sRGB_from_xyz(r_xyz + g_xyz + b_xyz), Color(255, 255, 255));
// Test the inverse curve transform.
float f64 = sRGB_curve.evaluate(64 / 255.f);
EXPECT_EQ(sRGB_from_xyz((r_xyz + g_xyz + b_xyz) * f64), Color(64, 64, 64));
float f128 = sRGB_curve.evaluate(128 / 255.f);
EXPECT_EQ(sRGB_from_xyz((r_xyz + g_xyz + b_xyz) * f128), Color(128, 128, 128));
// Test for curve and matrix combined.
float f192 = sRGB_curve.evaluate(192 / 255.f);
EXPECT_EQ(sRGB_from_xyz(r_xyz * f64 + g_xyz * f128 + b_xyz * f192), Color(64, 128, 192));
}
TEST_CASE(to_lab)
{
auto sRGB = MUST(Gfx::ICC::sRGB());
auto lab_from_sRGB = [&sRGB](u8 r, u8 g, u8 b) {
u8 rgb[3] = { r, g, b };
return MUST(sRGB->to_lab(rgb));
};
// The `expected` numbers are from https://colorjs.io/notebook/ for this snippet of code:
// new Color("srgb", [0, 0, 0]).lab.toString();
//
// new Color("srgb", [1, 0, 0]).lab.toString();
// new Color("srgb", [0, 1, 0]).lab.toString();
// new Color("srgb", [0, 0, 1]).lab.toString();
//
// new Color("srgb", [1, 1, 0]).lab.toString();
// new Color("srgb", [1, 0, 1]).lab.toString();
// new Color("srgb", [0, 1, 1]).lab.toString();
//
// new Color("srgb", [1, 1, 1]).lab.toString();
Gfx::CIELAB expected[] = {
{ 0, 0, 0 },
{ 54.29054294696968, 80.80492033462421, 69.89098825896275 },
{ 87.81853633115202, -79.27108223854806, 80.99459785152247 },
{ 29.56829715344471, 68.28740665215547, -112.02971798617645 },
{ 97.60701009682253, -15.749846639252663, 93.39361164266089 },
{ 60.16894098715946, 93.53959546199253, -60.50080231921204 },
{ 90.66601315791455, -50.65651077286893, -14.961666625736525 },
{ 100.00000139649632, -0.000007807961277528364, 0.000006766250648659877 },
};
// We're off by more than the default EXPECT_APPROXIMATE() error, so use EXPECT_APPROXIMATE_WITH_ERROR().
// The difference is not too bad: ranges for L*, a*, b* are [0, 100], [-125, 125], [-125, 125],
// so this is an error of considerably less than 0.1 for u8 channels.
#define EXPECT_APPROXIMATE_LAB(l1, l2) \
EXPECT_APPROXIMATE_WITH_ERROR((l1).L, (l2).L, 0.01); \
EXPECT_APPROXIMATE_WITH_ERROR((l1).a, (l2).a, 0.03); \
EXPECT_APPROXIMATE_WITH_ERROR((l1).b, (l2).b, 0.02);
EXPECT_APPROXIMATE_LAB(lab_from_sRGB(0, 0, 0), expected[0]);
EXPECT_APPROXIMATE_LAB(lab_from_sRGB(255, 0, 0), expected[1]);
EXPECT_APPROXIMATE_LAB(lab_from_sRGB(0, 255, 0), expected[2]);
EXPECT_APPROXIMATE_LAB(lab_from_sRGB(0, 0, 255), expected[3]);
EXPECT_APPROXIMATE_LAB(lab_from_sRGB(255, 255, 0), expected[4]);
EXPECT_APPROXIMATE_LAB(lab_from_sRGB(255, 0, 255), expected[5]);
EXPECT_APPROXIMATE_LAB(lab_from_sRGB(0, 255, 255), expected[6]);
EXPECT_APPROXIMATE_LAB(lab_from_sRGB(255, 255, 255), expected[7]);
}
TEST_CASE(malformed_profile)
{
Array test_inputs = {
TEST_INPUT("icc/oss-fuzz-testcase-57426.icc"sv),
TEST_INPUT("icc/oss-fuzz-testcase-59551.icc"sv),
TEST_INPUT("icc/oss-fuzz-testcase-60281.icc"sv)
};
for (auto test_input : test_inputs) {
auto file = MUST(Core::MappedFile::map(test_input));
auto profile_or_error = Gfx::ICC::Profile::try_load_from_externally_owned_memory(file->bytes());
EXPECT(profile_or_error.is_error());
}
}

View file

@ -6,9 +6,6 @@
#include <AK/MemoryStream.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ICC/BinaryWriter.h>
#include <LibGfx/ICC/Profile.h>
#include <LibGfx/ICC/WellKnownProfiles.h>
#include <LibGfx/ImageFormats/AnimationWriter.h>
#include <LibGfx/ImageFormats/BMPLoader.h>
#include <LibGfx/ImageFormats/BMPWriter.h>
@ -188,23 +185,6 @@ TEST_CASE(test_webp_color_indexing_transform_single_channel)
}
}
TEST_CASE(test_webp_icc)
{
auto sRGB_icc_profile = MUST(Gfx::ICC::sRGB());
auto sRGB_icc_data = MUST(Gfx::ICC::encode(sRGB_icc_profile));
auto rgba_bitmap = TRY_OR_FAIL(create_test_rgba_bitmap());
Gfx::WebPEncoderOptions options;
options.icc_data = sRGB_icc_data;
auto encoded_rgba_bitmap = TRY_OR_FAIL((encode_bitmap<Gfx::WebPWriter>(rgba_bitmap, options)));
auto decoded_rgba_plugin = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(encoded_rgba_bitmap));
expect_bitmaps_equal(*TRY_OR_FAIL(expect_single_frame_of_size(*decoded_rgba_plugin, rgba_bitmap->size())), rgba_bitmap);
auto decoded_rgba_profile = TRY_OR_FAIL(Gfx::ICC::Profile::try_load_from_externally_owned_memory(TRY_OR_FAIL(decoded_rgba_plugin->icc_data()).value()));
auto reencoded_icc_data = TRY_OR_FAIL(Gfx::ICC::encode(decoded_rgba_profile));
EXPECT_EQ(sRGB_icc_data, reencoded_icc_data);
}
TEST_CASE(test_webp_animation)
{
auto rgb_bitmap = TRY_OR_FAIL(create_test_rgb_bitmap());

View file

@ -1,605 +0,0 @@
/*
* Copyright (c) 2022-2023, Nico Weber <thakis@chromium.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Random.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DateTime.h>
#include <LibCore/File.h>
#include <LibCore/MappedFile.h>
#include <LibGfx/DeltaE.h>
#include <LibGfx/ICC/BinaryWriter.h>
#include <LibGfx/ICC/Profile.h>
#include <LibGfx/ICC/Tags.h>
#include <LibGfx/ICC/WellKnownProfiles.h>
#include <LibGfx/ImageFormats/ImageDecoder.h>
#include <LibMedia/Color/CodingIndependentCodePoints.h>
template<class T>
static ErrorOr<String> hyperlink(URL::URL const& target, T const& label)
{
return String::formatted("\033]8;;{}\033\\{}\033]8;;\033\\", target, label);
}
template<class T>
static void out_optional(char const* label, Optional<T> const& optional)
{
out("{}: ", label);
if (optional.has_value())
outln("{}", *optional);
else
outln("(not set)");
}
static void out_curve(Gfx::ICC::CurveTagData const& curve, int indent_amount)
{
if (curve.values().is_empty()) {
outln("{: >{}}identity curve", "", indent_amount);
} else if (curve.values().size() == 1) {
outln("{: >{}}gamma: {}", "", indent_amount, AK::FixedPoint<8, u16>::create_raw(curve.values()[0]));
} else {
// FIXME: Maybe print the actual points if -v is passed?
outln("{: >{}}curve with {} points", "", indent_amount, curve.values().size());
}
}
static void out_parametric_curve(Gfx::ICC::ParametricCurveTagData const& parametric_curve, int indent_amount)
{
switch (parametric_curve.function_type()) {
case Gfx::ICC::ParametricCurveTagData::FunctionType::Type0:
outln("{: >{}}Y = X**{}", "", indent_amount, parametric_curve.g());
break;
case Gfx::ICC::ParametricCurveTagData::FunctionType::Type1:
outln("{: >{}}Y = ({}*X + {})**{} if X >= -{}/{}", "", indent_amount,
parametric_curve.a(), parametric_curve.b(), parametric_curve.g(), parametric_curve.b(), parametric_curve.a());
outln("{: >{}}Y = 0 else", "", indent_amount);
break;
case Gfx::ICC::ParametricCurveTagData::FunctionType::Type2:
outln("{: >{}}Y = ({}*X + {})**{} + {} if X >= -{}/{}", "", indent_amount,
parametric_curve.a(), parametric_curve.b(), parametric_curve.g(), parametric_curve.c(), parametric_curve.b(), parametric_curve.a());
outln("{: >{}}Y = {} else", "", indent_amount, parametric_curve.c());
break;
case Gfx::ICC::ParametricCurveTagData::FunctionType::Type3:
outln("{: >{}}Y = ({}*X + {})**{} if X >= {}", "", indent_amount,
parametric_curve.a(), parametric_curve.b(), parametric_curve.g(), parametric_curve.d());
outln("{: >{}}Y = {}*X else", "", indent_amount, parametric_curve.c());
break;
case Gfx::ICC::ParametricCurveTagData::FunctionType::Type4:
outln("{: >{}}Y = ({}*X + {})**{} + {} if X >= {}", "", indent_amount,
parametric_curve.a(), parametric_curve.b(), parametric_curve.g(), parametric_curve.e(), parametric_curve.d());
outln("{: >{}}Y = {}*X + {} else", "", indent_amount, parametric_curve.c(), parametric_curve.f());
break;
}
}
static float curve_distance_u8(Gfx::ICC::TagData const& tag1, Gfx::ICC::TagData const& tag2)
{
VERIFY(tag1.type() == Gfx::ICC::CurveTagData::Type || tag1.type() == Gfx::ICC::ParametricCurveTagData::Type);
VERIFY(tag2.type() == Gfx::ICC::CurveTagData::Type || tag2.type() == Gfx::ICC::ParametricCurveTagData::Type);
float curve1_data[256];
if (tag1.type() == Gfx::ICC::CurveTagData::Type) {
auto& curve1 = static_cast<Gfx::ICC::CurveTagData const&>(tag1);
for (int i = 0; i < 256; ++i)
curve1_data[i] = curve1.evaluate(i / 255.f);
} else {
auto& parametric_curve1 = static_cast<Gfx::ICC::ParametricCurveTagData const&>(tag1);
for (int i = 0; i < 256; ++i)
curve1_data[i] = parametric_curve1.evaluate(i / 255.f);
}
float curve2_data[256];
if (tag2.type() == Gfx::ICC::CurveTagData::Type) {
auto& curve2 = static_cast<Gfx::ICC::CurveTagData const&>(tag2);
for (int i = 0; i < 256; ++i)
curve2_data[i] = curve2.evaluate(i / 255.f);
} else {
auto& parametric_curve2 = static_cast<Gfx::ICC::ParametricCurveTagData const&>(tag2);
for (int i = 0; i < 256; ++i)
curve2_data[i] = parametric_curve2.evaluate(i / 255.f);
}
float distance = 0;
for (int i = 0; i < 256; ++i)
distance += fabsf(curve1_data[i] - curve2_data[i]);
return distance;
}
static ErrorOr<void> out_curve_tag(Gfx::ICC::TagData const& tag, int indent_amount)
{
VERIFY(tag.type() == Gfx::ICC::CurveTagData::Type || tag.type() == Gfx::ICC::ParametricCurveTagData::Type);
if (tag.type() == Gfx::ICC::CurveTagData::Type)
out_curve(static_cast<Gfx::ICC::CurveTagData const&>(tag), indent_amount);
if (tag.type() == Gfx::ICC::ParametricCurveTagData::Type)
out_parametric_curve(static_cast<Gfx::ICC::ParametricCurveTagData const&>(tag), indent_amount);
auto sRGB_curve = TRY(Gfx::ICC::sRGB_curve());
// Some example values (for abs distance summed over the 256 values of an u8):
// In Compact-ICC-Profiles/profiles:
// AdobeCompat-v2.icc: 1.14 (this is a gamma 2.2 curve, so not really sRGB but close)
// AdobeCompat-v4.icc: 1.13
// AppleCompat-v2.icc: 11.94 (gamma 1.8 curve)
// DCI-P3-v4.icc: 8.29 (gamma 2.6 curve)
// DisplayP3-v2-magic.icc: 0.000912 (looks sRGB-ish)
// DisplayP3-v2-micro.icc: 0.010819
// DisplayP3-v4.icc: 0.001062 (yes, definitely sRGB)
// Rec2020-g24-v4.icc: 4.119216 (gamma 2.4 curve)
// Rec2020-v4.icc: 7.805417 (custom non-sRGB curve)
// Rec709-v4.icc: 7.783267 (same custom non-sRGB curve as Rec2020)
// sRGB-v2-magic.icc: 0.000912
// sRGB-v2-micro.icc: 0.010819
// sRGB-v2-nano.icc: 0.052516
// sRGB-v4.icc: 0.001062
// scRGB-v2.icc: 48.379859 (linear identity curve)
// Google sRGB IEC61966-2.1 (from a Pixel jpeg, parametric): 0
// Google sRGB IEC61966-2.1 (from a Pixel jpeg, LUT curve): 0.00096
// Apple 2015 Display P3 (from iPhone 7, parametric): 0.011427 (has the old, left intersection for switching from linear to exponent)
// HP sRGB: 0.00096
// color.org sRGB2014.icc: 0.00096
// color.org sRGB_ICC_v4_Appearance.icc, AToB1Tag, a curves: 0.441926 -- but this is not _really_ sRGB
// color.org sRGB_v4_ICC_preference.icc, AToB1Tag, a curves: 2.205453 -- not really sRGB either
// So `< 0.06` identifies sRGB in practice (for u8 values).
float u8_distance_to_sRGB = curve_distance_u8(*sRGB_curve, tag);
if (u8_distance_to_sRGB < 0.06f)
outln("{: >{}}Looks like sRGB's curve (distance {})", "", indent_amount, u8_distance_to_sRGB);
else
outln("{: >{}}Does not look like sRGB's curve (distance: {})", "", indent_amount, u8_distance_to_sRGB);
return {};
}
static ErrorOr<void> out_curves(Vector<Gfx::ICC::LutCurveType> const& curves)
{
for (auto const& curve : curves) {
VERIFY(curve->type() == Gfx::ICC::CurveTagData::Type || curve->type() == Gfx::ICC::ParametricCurveTagData::Type);
outln(" type {}, relative offset {}, size {}", curve->type(), curve->offset(), curve->size());
TRY(out_curve_tag(*curve, /*indent=*/12));
}
return {};
}
static ErrorOr<void> perform_debug_roundtrip(Gfx::ICC::Profile const& profile)
{
size_t num_channels = Gfx::ICC::number_of_components_in_color_space(profile.data_color_space());
Vector<u8, 4> input, output;
input.resize(num_channels);
output.resize(num_channels);
size_t const num_total_roundtrips = 500;
size_t num_lossless_roundtrips = 0;
for (size_t i = 0; i < num_total_roundtrips; ++i) {
for (size_t j = 0; j < num_channels; ++j)
input[j] = get_random<u8>();
auto color_in_profile_connection_space = TRY(profile.to_pcs(input));
TRY(profile.from_pcs(profile, color_in_profile_connection_space, output));
if (input != output) {
outln("roundtrip failed for {} -> {}", input, output);
} else {
++num_lossless_roundtrips;
}
}
outln("lossless roundtrips: {} / {}", num_lossless_roundtrips, num_total_roundtrips);
return {};
}
static ErrorOr<void> print_profile_measurement(Gfx::ICC::Profile const& profile)
{
auto lab_from_rgb = [&profile](u8 r, u8 g, u8 b) {
u8 rgb[3] = { r, g, b };
return profile.to_lab(rgb);
};
float largest = -1, smallest = 1000;
Color largest_color1, largest_color2, smallest_color1, smallest_color2;
for (u8 r = 0; r < 254; ++r) {
out("\r{}/254", r + 1);
fflush(stdout);
for (u8 g = 0; g < 254; ++g) {
for (u8 b = 0; b < 254; ++b) {
auto lab = TRY(lab_from_rgb(r, g, b));
u8 delta_r[] = { 1, 0, 0 };
u8 delta_g[] = { 0, 1, 0 };
u8 delta_b[] = { 0, 0, 1 };
for (unsigned i = 0; i < sizeof(delta_r); ++i) {
auto lab2 = TRY(lab_from_rgb(r + delta_r[i], g + delta_g[i], b + delta_b[i]));
float delta = Gfx::DeltaE(lab, lab2);
if (delta > largest) {
largest = delta;
largest_color1 = Color(r, g, b);
largest_color2 = Color(r + delta_r[i], g + delta_g[i], b + delta_b[i]);
}
if (delta < smallest) {
smallest = delta;
smallest_color1 = Color(r, g, b);
smallest_color2 = Color(r + delta_r[i], g + delta_g[i], b + delta_b[i]);
}
}
}
}
}
outln("\rlargest difference between neighboring colors: {}, between {} and {}", largest, largest_color1, largest_color2);
outln("smallest difference between neighboring colors: {}, between {} and {}", smallest, smallest_color1, smallest_color2);
return {};
}
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
Core::ArgsParser args_parser;
StringView path;
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;
args_parser.add_option(dump_out_path, "Dump unmodified ICC profile bytes to this path", "dump-to", 0, "FILE");
StringView reencode_out_path;
args_parser.add_option(reencode_out_path, "Reencode ICC profile to this path", "reencode-to", 0, "FILE");
bool debug_roundtrip = false;
args_parser.add_option(debug_roundtrip, "Check how many u8 colors roundtrip losslessly through the profile. For debugging.", "debug-roundtrip");
bool measure = false;
args_parser.add_option(measure, "For RGB ICC profiles, print perceptually smallest and largest color step", "measure");
bool force_print = false;
args_parser.add_option(force_print, "Print profile even when writing ICC files", "print");
args_parser.parse(arguments);
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;
NonnullRefPtr<Gfx::ICC::Profile> profile = TRY([&]() -> ErrorOr<NonnullRefPtr<Gfx::ICC::Profile>> {
if (!name.is_empty()) {
if (name == "sRGB")
return Gfx::ICC::sRGB();
return Error::from_string_literal("unknown profile name");
}
auto file = TRY(Core::MappedFile::map(path));
auto decoder = TRY(Gfx::ImageDecoder::try_create_for_raw_bytes(file->bytes()));
if (decoder) {
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();
}
if (!dump_out_path.is_empty()) {
auto output_stream = TRY(Core::File::open(dump_out_path, Core::File::OpenMode::Write));
TRY(output_stream->write_until_depleted(icc_bytes));
}
return Gfx::ICC::Profile::try_load_from_externally_owned_memory(icc_bytes);
}());
if (!reencode_out_path.is_empty()) {
auto reencoded_bytes = TRY(Gfx::ICC::encode(profile));
auto output_stream = TRY(Core::File::open(reencode_out_path, Core::File::OpenMode::Write));
TRY(output_stream->write_until_depleted(reencoded_bytes));
}
if (debug_roundtrip) {
TRY(perform_debug_roundtrip(*profile));
return 0;
}
if (measure) {
if (profile->data_color_space() != Gfx::ICC::ColorSpace::RGB) {
warnln("--measure only works for RGB ICC profiles");
return 1;
}
TRY(print_profile_measurement(*profile));
}
bool do_print = (dump_out_path.is_empty() && reencode_out_path.is_empty() && !measure) || force_print;
if (!do_print)
return 0;
outln(" size: {} bytes", profile->on_disk_size());
out_optional(" preferred CMM type", profile->preferred_cmm_type());
outln(" version: {}", profile->version());
outln(" device class: {}", Gfx::ICC::device_class_name(profile->device_class()));
outln(" data color space: {}", Gfx::ICC::data_color_space_name(profile->data_color_space()));
outln(" connection space: {}", Gfx::ICC::profile_connection_space_name(profile->connection_space()));
if (auto time = profile->creation_timestamp().to_time_t(); !time.is_error()) {
// Print in friendly localtime for valid profiles.
outln("creation date and time: {}", Core::DateTime::from_timestamp(time.release_value()));
} else {
outln("creation date and time: {:04}-{:02}-{:02} {:02}:{:02}:{:02} UTC (invalid)",
profile->creation_timestamp().year, profile->creation_timestamp().month, profile->creation_timestamp().day,
profile->creation_timestamp().hours, profile->creation_timestamp().minutes, profile->creation_timestamp().seconds);
}
out_optional(" primary platform", profile->primary_platform().map([](auto platform) { return primary_platform_name(platform); }));
auto flags = profile->flags();
outln(" flags: {:#08x}", flags.bits());
outln(" - {}embedded in file", flags.is_embedded_in_file() ? "" : "not ");
outln(" - can{} be used independently of embedded color data", flags.can_be_used_independently_of_embedded_color_data() ? "" : "not");
if (auto unknown_icc_bits = flags.icc_bits() & ~Gfx::ICC::Flags::KnownBitsMask)
outln(" other unknown ICC bits: {:#04x}", unknown_icc_bits);
if (auto color_management_module_bits = flags.color_management_module_bits())
outln(" CMM bits: {:#04x}", color_management_module_bits);
out_optional(" device manufacturer", TRY(profile->device_manufacturer().map([](auto device_manufacturer) {
return hyperlink(device_manufacturer_url(device_manufacturer), device_manufacturer);
})));
out_optional(" device model", TRY(profile->device_model().map([](auto device_model) {
return hyperlink(device_model_url(device_model), device_model);
})));
auto device_attributes = profile->device_attributes();
outln(" device attributes: {:#016x}", device_attributes.bits());
outln(" media is:");
outln(" - {}",
device_attributes.media_reflectivity() == Gfx::ICC::DeviceAttributes::MediaReflectivity::Reflective ? "reflective" : "transparent");
outln(" - {}",
device_attributes.media_glossiness() == Gfx::ICC::DeviceAttributes::MediaGlossiness::Glossy ? "glossy" : "matte");
outln(" - {}",
device_attributes.media_polarity() == Gfx::ICC::DeviceAttributes::MediaPolarity::Positive ? "of positive polarity" : "of negative polarity");
outln(" - {}",
device_attributes.media_color() == Gfx::ICC::DeviceAttributes::MediaColor::Colored ? "colored" : "black and white");
VERIFY((flags.icc_bits() & ~Gfx::ICC::DeviceAttributes::KnownBitsMask) == 0);
if (auto vendor_bits = device_attributes.vendor_bits())
outln(" vendor bits: {:#08x}", vendor_bits);
outln(" rendering intent: {}", Gfx::ICC::rendering_intent_name(profile->rendering_intent()));
outln(" pcs illuminant: {}", profile->pcs_illuminant());
out_optional(" creator", profile->creator());
out_optional(" id", profile->id());
size_t profile_disk_size = icc_bytes.size();
if (profile_disk_size != profile->on_disk_size()) {
VERIFY(profile_disk_size > profile->on_disk_size());
outln("{} trailing bytes after profile data", profile_disk_size - profile->on_disk_size());
}
outln("");
outln("tags:");
HashMap<Gfx::ICC::TagData*, Gfx::ICC::TagSignature> tag_data_to_first_signature;
TRY(profile->try_for_each_tag([&tag_data_to_first_signature](auto tag_signature, auto tag_data) -> ErrorOr<void> {
if (auto name = tag_signature_spec_name(tag_signature); name.has_value())
out("{} ({}): ", *name, tag_signature);
else
out("Unknown tag ({}): ", tag_signature);
outln("type {}, offset {}, size {}", tag_data->type(), tag_data->offset(), tag_data->size());
// Print tag data only the first time it's seen.
// (Different sigatures can refer to the same data.)
auto it = tag_data_to_first_signature.find(tag_data);
if (it != tag_data_to_first_signature.end()) {
outln(" (see {} above)", it->value);
return {};
}
tag_data_to_first_signature.set(tag_data, tag_signature);
if (tag_data->type() == Gfx::ICC::ChromaticityTagData::Type) {
auto& chromaticity = static_cast<Gfx::ICC::ChromaticityTagData&>(*tag_data);
outln(" phosphor or colorant type: {}", Gfx::ICC::ChromaticityTagData::phosphor_or_colorant_type_name(chromaticity.phosphor_or_colorant_type()));
for (auto const& xy : chromaticity.xy_coordinates())
outln(" x, y: {}, {}", xy.x, xy.y);
} else if (tag_data->type() == Gfx::ICC::CicpTagData::Type) {
auto& cicp = static_cast<Gfx::ICC::CicpTagData&>(*tag_data);
outln(" color primaries: {} - {}", cicp.color_primaries(),
Media::color_primaries_to_string((Media::ColorPrimaries)cicp.color_primaries()));
outln(" transfer characteristics: {} - {}", cicp.transfer_characteristics(),
Media::transfer_characteristics_to_string((Media::TransferCharacteristics)cicp.transfer_characteristics()));
outln(" matrix coefficients: {} - {}", cicp.matrix_coefficients(),
Media::matrix_coefficients_to_string((Media::MatrixCoefficients)cicp.matrix_coefficients()));
outln(" video full range flag: {} - {}", cicp.video_full_range_flag(),
Media::video_full_range_flag_to_string((Media::VideoFullRangeFlag)cicp.video_full_range_flag()));
} else if (tag_data->type() == Gfx::ICC::CurveTagData::Type) {
TRY(out_curve_tag(*tag_data, /*indent=*/4));
} else if (tag_data->type() == Gfx::ICC::Lut16TagData::Type) {
auto& lut16 = static_cast<Gfx::ICC::Lut16TagData&>(*tag_data);
outln(" input table: {} channels x {} entries", lut16.number_of_input_channels(), lut16.number_of_input_table_entries());
outln(" output table: {} channels x {} entries", lut16.number_of_output_channels(), lut16.number_of_output_table_entries());
outln(" color lookup table: {} grid points, {} total entries", lut16.number_of_clut_grid_points(), lut16.clut_values().size());
auto const& e = lut16.e_matrix();
outln(" e = [ {}, {}, {},", e[0], e[1], e[2]);
outln(" {}, {}, {},", e[3], e[4], e[5]);
outln(" {}, {}, {} ]", e[6], e[7], e[8]);
} else if (tag_data->type() == Gfx::ICC::Lut8TagData::Type) {
auto& lut8 = static_cast<Gfx::ICC::Lut8TagData&>(*tag_data);
outln(" input table: {} channels x {} entries", lut8.number_of_input_channels(), lut8.number_of_input_table_entries());
outln(" output table: {} channels x {} entries", lut8.number_of_output_channels(), lut8.number_of_output_table_entries());
outln(" color lookup table: {} grid points, {} total entries", lut8.number_of_clut_grid_points(), lut8.clut_values().size());
auto const& e = lut8.e_matrix();
outln(" e = [ {}, {}, {},", e[0], e[1], e[2]);
outln(" {}, {}, {},", e[3], e[4], e[5]);
outln(" {}, {}, {} ]", e[6], e[7], e[8]);
} else if (tag_data->type() == Gfx::ICC::LutAToBTagData::Type) {
auto& a_to_b = static_cast<Gfx::ICC::LutAToBTagData&>(*tag_data);
outln(" {} input channels, {} output channels", a_to_b.number_of_input_channels(), a_to_b.number_of_output_channels());
if (auto const& optional_a_curves = a_to_b.a_curves(); optional_a_curves.has_value()) {
outln(" a curves: {} curves", optional_a_curves->size());
TRY(out_curves(optional_a_curves.value()));
} else {
outln(" a curves: (not set)");
}
if (auto const& optional_clut = a_to_b.clut(); optional_clut.has_value()) {
auto const& clut = optional_clut.value();
outln(" color lookup table: {} grid points, {}",
TRY(String::join(" x "sv, clut.number_of_grid_points_in_dimension)),
TRY(clut.values.visit(
[](Vector<u8> const& v) { return String::formatted("{} u8 entries", v.size()); },
[](Vector<u16> const& v) { return String::formatted("{} u16 entries", v.size()); })));
} else {
outln(" color lookup table: (not set)");
}
if (auto const& optional_m_curves = a_to_b.m_curves(); optional_m_curves.has_value()) {
outln(" m curves: {} curves", optional_m_curves->size());
TRY(out_curves(optional_m_curves.value()));
} else {
outln(" m curves: (not set)");
}
if (auto const& optional_e = a_to_b.e_matrix(); optional_e.has_value()) {
auto const& e = optional_e.value();
outln(" e = [ {}, {}, {}, {},", e[0], e[1], e[2], e[9]);
outln(" {}, {}, {}, {},", e[3], e[4], e[5], e[10]);
outln(" {}, {}, {}, {} ]", e[6], e[7], e[8], e[11]);
} else {
outln(" e = (not set)");
}
outln(" b curves: {} curves", a_to_b.b_curves().size());
TRY(out_curves(a_to_b.b_curves()));
} else if (tag_data->type() == Gfx::ICC::LutBToATagData::Type) {
auto& b_to_a = static_cast<Gfx::ICC::LutBToATagData&>(*tag_data);
outln(" {} input channels, {} output channels", b_to_a.number_of_input_channels(), b_to_a.number_of_output_channels());
outln(" b curves: {} curves", b_to_a.b_curves().size());
TRY(out_curves(b_to_a.b_curves()));
if (auto const& optional_e = b_to_a.e_matrix(); optional_e.has_value()) {
auto const& e = optional_e.value();
outln(" e = [ {}, {}, {}, {},", e[0], e[1], e[2], e[9]);
outln(" {}, {}, {}, {},", e[3], e[4], e[5], e[10]);
outln(" {}, {}, {}, {} ]", e[6], e[7], e[8], e[11]);
} else {
outln(" e = (not set)");
}
if (auto const& optional_m_curves = b_to_a.m_curves(); optional_m_curves.has_value()) {
outln(" m curves: {} curves", optional_m_curves->size());
TRY(out_curves(optional_m_curves.value()));
} else {
outln(" m curves: (not set)");
}
if (auto const& optional_clut = b_to_a.clut(); optional_clut.has_value()) {
auto const& clut = optional_clut.value();
outln(" color lookup table: {} grid points, {}",
TRY(String::join(" x "sv, clut.number_of_grid_points_in_dimension)),
TRY(clut.values.visit(
[](Vector<u8> const& v) { return String::formatted("{} u8 entries", v.size()); },
[](Vector<u16> const& v) { return String::formatted("{} u16 entries", v.size()); })));
} else {
outln(" color lookup table: (not set)");
}
if (auto const& optional_a_curves = b_to_a.a_curves(); optional_a_curves.has_value()) {
outln(" a curves: {} curves", optional_a_curves->size());
TRY(out_curves(optional_a_curves.value()));
} else {
outln(" a curves: (not set)");
}
} else if (tag_data->type() == Gfx::ICC::MeasurementTagData::Type) {
auto& measurement = static_cast<Gfx::ICC::MeasurementTagData&>(*tag_data);
outln(" standard observer: {}", Gfx::ICC::MeasurementTagData::standard_observer_name(measurement.standard_observer()));
outln(" tristimulus value for measurement backing: {}", measurement.tristimulus_value_for_measurement_backing());
outln(" measurement geometry: {}", Gfx::ICC::MeasurementTagData::measurement_geometry_name(measurement.measurement_geometry()));
outln(" measurement flare: {} %", measurement.measurement_flare() * 100);
outln(" standard illuminant: {}", Gfx::ICC::MeasurementTagData::standard_illuminant_name(measurement.standard_illuminant()));
} else if (tag_data->type() == Gfx::ICC::MultiLocalizedUnicodeTagData::Type) {
auto& multi_localized_unicode = static_cast<Gfx::ICC::MultiLocalizedUnicodeTagData&>(*tag_data);
for (auto& record : multi_localized_unicode.records()) {
outln(" {:c}{:c}/{:c}{:c}: \"{}\"",
record.iso_639_1_language_code >> 8, record.iso_639_1_language_code & 0xff,
record.iso_3166_1_country_code >> 8, record.iso_3166_1_country_code & 0xff,
record.text);
}
} else if (tag_data->type() == Gfx::ICC::NamedColor2TagData::Type) {
auto& named_colors = static_cast<Gfx::ICC::NamedColor2TagData&>(*tag_data);
outln(" vendor specific flag: {:#08x}", named_colors.vendor_specific_flag());
outln(" common name prefix: \"{}\"", named_colors.prefix());
outln(" common name suffix: \"{}\"", named_colors.suffix());
outln(" {} colors:", named_colors.size());
for (size_t i = 0; i < min(named_colors.size(), 5u); ++i) {
auto const& pcs = named_colors.pcs_coordinates(i);
// FIXME: Display decoded values? (See ICC v4 6.3.4.2 and 10.8.)
out(" \"{}\", PCS coordinates: {:#04x} {:#04x} {:#04x}", TRY(named_colors.color_name(i)), pcs.xyz.x, pcs.xyz.y, pcs.xyz.z);
if (auto number_of_device_coordinates = named_colors.number_of_device_coordinates(); number_of_device_coordinates > 0) {
out(", device coordinates:");
for (size_t j = 0; j < number_of_device_coordinates; ++j)
out(" {:#04x}", named_colors.device_coordinates(i)[j]);
}
outln();
}
if (named_colors.size() > 5u)
outln(" ...");
} else if (tag_data->type() == Gfx::ICC::ParametricCurveTagData::Type) {
TRY(out_curve_tag(*tag_data, /*indent=*/4));
} else if (tag_data->type() == Gfx::ICC::S15Fixed16ArrayTagData::Type) {
// This tag can contain arbitrarily many fixed-point numbers, but in practice it's
// exclusively used for the 'chad' tag, where it always contains 9 values that
// represent a 3x3 matrix. So print the values in groups of 3.
auto& fixed_array = static_cast<Gfx::ICC::S15Fixed16ArrayTagData&>(*tag_data);
out(" [");
int i = 0;
for (auto value : fixed_array.values()) {
if (i > 0) {
out(",");
if (i % 3 == 0) {
outln();
out(" ");
}
}
out(" {}", value);
i++;
}
outln(" ]");
} else if (tag_data->type() == Gfx::ICC::SignatureTagData::Type) {
auto& signature = static_cast<Gfx::ICC::SignatureTagData&>(*tag_data);
if (auto name = signature.name_for_tag(tag_signature); name.has_value()) {
outln(" signature: {}", name.value());
} else {
outln(" signature: Unknown ('{:c}{:c}{:c}{:c}' / {:#08x})",
signature.signature() >> 24, (signature.signature() >> 16) & 0xff, (signature.signature() >> 8) & 0xff, signature.signature() & 0xff,
signature.signature());
}
} else if (tag_data->type() == Gfx::ICC::TextDescriptionTagData::Type) {
auto& text_description = static_cast<Gfx::ICC::TextDescriptionTagData&>(*tag_data);
outln(" ascii: \"{}\"", text_description.ascii_description());
out_optional(" unicode", TRY(text_description.unicode_description().map([](auto description) { return String::formatted("\"{}\"", description); })));
outln(" unicode language code: 0x{}", text_description.unicode_language_code());
out_optional(" macintosh", TRY(text_description.macintosh_description().map([](auto description) { return String::formatted("\"{}\"", description); })));
} else if (tag_data->type() == Gfx::ICC::TextTagData::Type) {
outln(" text: \"{}\"", static_cast<Gfx::ICC::TextTagData&>(*tag_data).text());
} else if (tag_data->type() == Gfx::ICC::ViewingConditionsTagData::Type) {
auto& viewing_conditions = static_cast<Gfx::ICC::ViewingConditionsTagData&>(*tag_data);
outln(" unnormalized CIEXYZ values for illuminant (in which Y is in cd/m²): {}", viewing_conditions.unnormalized_ciexyz_values_for_illuminant());
outln(" unnormalized CIEXYZ values for surround (in which Y is in cd/m²): {}", viewing_conditions.unnormalized_ciexyz_values_for_surround());
outln(" illuminant type: {}", Gfx::ICC::MeasurementTagData::standard_illuminant_name(viewing_conditions.illuminant_type()));
} else if (tag_data->type() == Gfx::ICC::XYZTagData::Type) {
for (auto& xyz : static_cast<Gfx::ICC::XYZTagData&>(*tag_data).xyzs())
outln(" {}", xyz);
}
return {};
}));
return 0;
}

View file

@ -7,7 +7,6 @@
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/MappedFile.h>
#include <LibGfx/ICC/Profile.h>
#include <LibGfx/ImageFormats/BMPWriter.h>
#include <LibGfx/ImageFormats/ImageDecoder.h>
#include <LibGfx/ImageFormats/JPEGWriter.h>
@ -122,41 +121,6 @@ static ErrorOr<void> strip_alpha(LoadedImage& image)
return {};
}
static ErrorOr<OwnPtr<Core::MappedFile>> convert_image_profile(LoadedImage& image, StringView convert_color_profile_path, OwnPtr<Core::MappedFile> maybe_source_icc_file)
{
if (!image.icc_data.has_value())
return Error::from_string_literal("No source color space embedded in image. Pass one with --assign-color-profile.");
auto source_icc_file = move(maybe_source_icc_file);
auto source_icc_data = image.icc_data.value();
auto icc_file = TRY(Core::MappedFile::map(convert_color_profile_path));
image.icc_data = icc_file->bytes();
auto source_profile = TRY(Gfx::ICC::Profile::try_load_from_externally_owned_memory(source_icc_data));
auto destination_profile = TRY(Gfx::ICC::Profile::try_load_from_externally_owned_memory(icc_file->bytes()));
if (destination_profile->data_color_space() != Gfx::ICC::ColorSpace::RGB)
return Error::from_string_literal("Can only convert to RGB at the moment, but destination color space is not RGB");
if (image.bitmap.has<RefPtr<Gfx::CMYKBitmap>>()) {
if (source_profile->data_color_space() != Gfx::ICC::ColorSpace::CMYK)
return Error::from_string_literal("Source image data is CMYK but source color space is not CMYK");
auto& cmyk_frame = image.bitmap.get<RefPtr<Gfx::CMYKBitmap>>();
auto rgb_frame = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, cmyk_frame->size()));
TRY(destination_profile->convert_cmyk_image(*rgb_frame, *cmyk_frame, *source_profile));
image.bitmap = RefPtr(move(rgb_frame));
image.internal_format = Gfx::NaturalFrameFormat::RGB;
} else {
// FIXME: This likely wrong for grayscale images because they've been converted to
// RGB at this point, but their embedded color profile is still for grayscale.
auto& frame = image.bitmap.get<RefPtr<Gfx::Bitmap>>();
TRY(destination_profile->convert_image(*frame, *source_profile));
}
return icc_file;
}
static ErrorOr<void> save_image(LoadedImage& image, StringView out_path, u8 jpeg_quality, Optional<unsigned> webp_allowed_transforms)
{
auto stream = [out_path]() -> ErrorOr<NonnullOwnPtr<Core::OutputBufferedFile>> {
@ -262,8 +226,6 @@ static ErrorOr<Options> parse_options(Main::Arguments arguments)
args_parser.add_option(options.move_alpha_to_rgb, "Copy alpha channel to rgb, clear alpha", "move-alpha-to-rgb", {});
args_parser.add_option(options.strip_alpha, "Remove alpha channel", "strip-alpha", {});
args_parser.add_option(options.assign_color_profile_path, "Load color profile from file and assign it to output image", "assign-color-profile", {}, "FILE");
args_parser.add_option(options.convert_color_profile_path, "Load color profile from file and convert output image from current profile to loaded profile", "convert-to-color-profile", {}, "FILE");
args_parser.add_option(options.strip_color_profile, "Do not write color profile to output", "strip-color-profile", {});
args_parser.add_option(options.quality, "Quality used for the JPEG encoder, the default value is 75 on a scale from 0 to 100", "quality", {}, {});
StringView webp_allowed_transforms = "default"sv;
args_parser.add_option(webp_allowed_transforms, "Comma-separated list of allowed transforms (predictor,p,color,c,subtract-green,sg,color-indexing,ci) for WebP output (default: all allowed)", "webp-allowed-transforms", {}, {});
@ -309,9 +271,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
image.icc_data = icc_file->bytes();
}
if (!options.convert_color_profile_path.is_empty())
icc_file = TRY(convert_image_profile(image, options.convert_color_profile_path, move(icc_file)));
if (options.strip_color_profile)
image.icc_data.clear();