2022-12-26 08:21:26 -05:00
/*
* Copyright ( c ) 2022 , Nico Weber < thakis @ chromium . org >
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# include <AK/Endian.h>
# include <LibGfx/ICCProfile.h>
2023-01-21 21:32:22 -05:00
# include <LibTextCodec/Decoder.h>
2023-01-01 07:09:38 -05:00
# include <math.h>
2022-12-27 13:37:37 -05:00
# include <time.h>
2022-12-26 08:21:26 -05:00
// V2 spec: https://color.org/specification/ICC.1-2001-04.pdf
// V4 spec: https://color.org/specification/ICC.1-2022-05.pdf
namespace Gfx : : ICC {
namespace {
2022-12-27 13:37:37 -05:00
// 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 ;
2023-01-08 06:06:20 -05:00
BigEndian < u16 > hours ;
2022-12-27 13:37:37 -05:00
BigEndian < u16 > minutes ;
BigEndian < u16 > seconds ;
} ;
2023-01-01 07:09:38 -05:00
// ICC V4, 4.6 s15Fixed16Number
using s15Fixed16Number = i32 ;
// ICC V4, 4.14 XYZNumber
struct XYZNumber {
BigEndian < s15Fixed16Number > x ;
BigEndian < s15Fixed16Number > y ;
BigEndian < s15Fixed16Number > z ;
operator XYZ ( ) const
{
return XYZ { x / ( double ) 0x1'0000 , y / ( double ) 0x1'0000 , z / ( double ) 0x1'0000 } ;
}
} ;
2022-12-27 13:37:37 -05:00
ErrorOr < time_t > parse_date_time_number ( DateTimeNumber const & date_time )
{
// ICC V4, 4.2 dateTimeNumber
// "Number of the month (1 to 12)"
if ( date_time . month < 1 | | date_time . month > 12 )
return Error : : from_string_literal ( " ICC::Profile: dateTimeNumber month out of bounds " ) ;
// "Number of the day of the month (1 to 31)"
if ( date_time . day < 1 | | date_time . day > 31 )
return Error : : from_string_literal ( " ICC::Profile: dateTimeNumber day out of bounds " ) ;
// "Number of hours (0 to 23)"
2023-01-08 06:06:20 -05:00
if ( date_time . hours > 23 )
return Error : : from_string_literal ( " ICC::Profile: dateTimeNumber hours out of bounds " ) ;
2022-12-27 13:37:37 -05:00
// "Number of minutes (0 to 59)"
if ( date_time . minutes > 59 )
return Error : : from_string_literal ( " ICC::Profile: dateTimeNumber minutes out of bounds " ) ;
// "Number of seconds (0 to 59)"
// ICC profiles apparently can't be created during leap seconds (seconds would be 60 there, but the spec doesn't allow that).
if ( date_time . seconds > 59 )
return Error : : from_string_literal ( " ICC::Profile: dateTimeNumber seconds out of bounds " ) ;
struct tm tm = { } ;
tm . tm_year = date_time . year - 1900 ;
tm . tm_mon = date_time . month - 1 ;
tm . tm_mday = date_time . day ;
2023-01-08 06:06:20 -05:00
tm . tm_hour = date_time . hours ;
2022-12-27 13:37:37 -05:00
tm . tm_min = date_time . minutes ;
tm . tm_sec = date_time . seconds ;
// timegm() doesn't read tm.tm_isdst, tm.tm_wday, and tm.tm_yday, no need to fill them in.
time_t timestamp = timegm ( & tm ) ;
if ( timestamp = = - 1 )
return Error : : from_string_literal ( " ICC::Profile: dateTimeNumber not representable as timestamp " ) ;
return timestamp ;
}
2022-12-26 08:21:26 -05:00
// ICC V4, 7.2 Profile header
struct ICCHeader {
BigEndian < u32 > profile_size ;
2023-01-18 22:16:10 -05:00
BigEndian < PreferredCMMType > preferred_cmm_type ;
2022-12-26 08:21:26 -05:00
u8 profile_version_major ;
u8 profile_version_minor_bugfix ;
BigEndian < u16 > profile_version_zero ;
2022-12-28 12:58:45 -05:00
BigEndian < DeviceClass > profile_device_class ;
2022-12-30 07:54:58 -05:00
BigEndian < ColorSpace > data_color_space ;
2022-12-30 11:07:38 -05:00
BigEndian < ColorSpace > profile_connection_space ; // "PCS" in the spec.
2022-12-26 08:21:26 -05:00
2022-12-27 13:37:37 -05:00
DateTimeNumber profile_creation_time ;
2022-12-26 08:21:26 -05:00
BigEndian < u32 > profile_file_signature ;
2023-01-07 15:22:15 -05:00
BigEndian < PrimaryPlatform > primary_platform ;
2022-12-26 08:21:26 -05:00
BigEndian < u32 > profile_flags ;
2023-01-18 22:16:10 -05:00
BigEndian < DeviceManufacturer > device_manufacturer ;
BigEndian < DeviceModel > device_model ;
2022-12-26 08:21:26 -05:00
BigEndian < u64 > device_attributes ;
BigEndian < u32 > rendering_intent ;
2023-01-01 07:09:38 -05:00
XYZNumber pcs_illuminant ;
2022-12-26 08:21:26 -05:00
2023-01-18 22:16:10 -05:00
BigEndian < Creator > profile_creator ;
2022-12-26 08:21:26 -05:00
2023-01-05 15:09:31 -05:00
u8 profile_id [ 16 ] ;
2022-12-26 08:21:26 -05:00
u8 reserved [ 28 ] ;
} ;
static_assert ( sizeof ( ICCHeader ) = = 128 ) ;
2023-01-08 07:07:21 -05:00
ErrorOr < u32 > parse_size ( ICCHeader const & header , ReadonlyBytes icc_bytes )
{
// ICC v4, 7.2.2 Profile size field
// "The value in the profile size field shall be the exact size obtained by combining the profile header,
// the tag table, and the tagged element data, including the pad bytes for the last tag."
// Valid files have enough data for profile header and tag table entry count.
if ( header . profile_size < sizeof ( ICCHeader ) + sizeof ( u32 ) )
return Error : : from_string_literal ( " ICC::Profile: Profile size too small " ) ;
if ( header . profile_size > icc_bytes . size ( ) )
return Error : : from_string_literal ( " ICC::Profile: Profile size larger than input data " ) ;
return header . profile_size ;
}
2023-01-06 15:57:45 -05:00
Optional < PreferredCMMType > parse_preferred_cmm_type ( ICCHeader const & header )
{
// ICC v4, 7.2.3 Preferred CMM type field
// "This field may be used to identify the preferred CMM to be used.
// If used, it shall match a CMM type signature registered in the ICC Tag Registry"
// https://www.color.org/signatures2.xalter currently links to
// https://www.color.org/registry/signature/TagRegistry-2021-03.pdf, which contains
// some CMM signatures.
// This requirement is often honored in practice, but not always. For example,
// JPEGs exported in Adobe Lightroom contain profiles that set this to 'Lino',
// which is not present in the "CMM Signatures" table in that PDF.
// "If no preferred CMM is identified, this field shall be set to zero (00000000h)."
2023-01-18 22:16:10 -05:00
if ( header . preferred_cmm_type = = PreferredCMMType { 0 } )
2023-01-06 15:57:45 -05:00
return { } ;
2023-01-18 22:16:10 -05:00
return header . preferred_cmm_type ;
2023-01-06 15:57:45 -05:00
}
2022-12-26 08:21:26 -05:00
ErrorOr < Version > parse_version ( ICCHeader const & header )
{
// ICC v4, 7.2.4 Profile version field
if ( header . profile_version_zero ! = 0 )
return Error : : from_string_literal ( " ICC::Profile: Reserved version bytes not zero " ) ;
return Version ( header . profile_version_major , header . profile_version_minor_bugfix ) ;
}
ErrorOr < DeviceClass > parse_device_class ( ICCHeader const & header )
{
// ICC v4, 7.2.5 Profile/device class field
switch ( header . profile_device_class ) {
2022-12-28 12:58:45 -05:00
case DeviceClass : : InputDevce :
case DeviceClass : : DisplayDevice :
case DeviceClass : : OutputDevice :
case DeviceClass : : DeviceLink :
case DeviceClass : : ColorSpace :
case DeviceClass : : Abstract :
case DeviceClass : : NamedColor :
return header . profile_device_class ;
2022-12-26 08:21:26 -05:00
}
return Error : : from_string_literal ( " ICC::Profile: Invalid device class " ) ;
}
2022-12-30 11:07:38 -05:00
ErrorOr < ColorSpace > parse_color_space ( ColorSpace color_space )
2022-12-30 07:54:58 -05:00
{
2022-12-30 11:07:38 -05:00
// ICC v4, Table 19 — Data colour space signatures
switch ( color_space ) {
2022-12-30 07:54:58 -05:00
case ColorSpace : : nCIEXYZ :
case ColorSpace : : CIELAB :
case ColorSpace : : CIELUV :
case ColorSpace : : YCbCr :
case ColorSpace : : CIEYxy :
case ColorSpace : : RGB :
case ColorSpace : : Gray :
case ColorSpace : : HSV :
case ColorSpace : : HLS :
case ColorSpace : : CMYK :
case ColorSpace : : CMY :
case ColorSpace : : TwoColor :
case ColorSpace : : ThreeColor :
case ColorSpace : : FourColor :
case ColorSpace : : FiveColor :
case ColorSpace : : SixColor :
case ColorSpace : : SevenColor :
case ColorSpace : : EightColor :
case ColorSpace : : NineColor :
case ColorSpace : : TenColor :
case ColorSpace : : ElevenColor :
case ColorSpace : : TwelveColor :
case ColorSpace : : ThirteenColor :
case ColorSpace : : FourteenColor :
case ColorSpace : : FifteenColor :
2022-12-30 11:07:38 -05:00
return color_space ;
2022-12-30 07:54:58 -05:00
}
2022-12-30 11:07:38 -05:00
return Error : : from_string_literal ( " ICC::Profile: Invalid color space " ) ;
}
ErrorOr < ColorSpace > parse_data_color_space ( ICCHeader const & header )
{
// ICC v4, 7.2.6 Data colour space field
return parse_color_space ( header . data_color_space ) ;
}
ErrorOr < ColorSpace > parse_connection_space ( ICCHeader const & header )
{
// ICC v4, 7.2.7 PCS field
// and Annex D
auto space = TRY ( parse_color_space ( header . profile_connection_space ) ) ;
if ( header . profile_device_class ! = DeviceClass : : DeviceLink & & ( space ! = ColorSpace : : PCSXYZ & & space ! = ColorSpace : : PCSLAB ) )
return Error : : from_string_literal ( " ICC::Profile: Invalid profile connection space: Non-PCS space on non-DeviceLink profile " ) ;
return space ;
2022-12-30 07:54:58 -05:00
}
2023-01-04 13:42:37 -05:00
ErrorOr < time_t > parse_creation_date_time ( ICCHeader const & header )
{
// ICC v4, 7.2.8 Date and time field
return parse_date_time_number ( header . profile_creation_time ) ;
}
ErrorOr < void > parse_file_signature ( ICCHeader const & header )
{
// ICC v4, 7.2.9 Profile file signature field
2023-01-06 19:59:03 -05:00
// "The profile file signature field shall contain the value “acsp” (61637370h) as a profile file signature."
2023-01-04 13:42:37 -05:00
if ( header . profile_file_signature ! = 0x61637370 )
return Error : : from_string_literal ( " ICC::Profile: profile file signature not 'acsp' " ) ;
return { } ;
}
2023-01-07 15:22:15 -05:00
ErrorOr < PrimaryPlatform > parse_primary_platform ( ICCHeader const & header )
{
// ICC v4, 7.2.10 Primary platform field
switch ( header . primary_platform ) {
case PrimaryPlatform : : Apple :
case PrimaryPlatform : : Microsoft :
case PrimaryPlatform : : SiliconGraphics :
case PrimaryPlatform : : Sun :
return header . primary_platform ;
}
return Error : : from_string_literal ( " ICC::Profile: Invalid primary platform " ) ;
}
2023-01-06 15:57:45 -05:00
Optional < DeviceManufacturer > parse_device_manufacturer ( ICCHeader const & header )
{
// ICC v4, 7.2.12 Device manufacturer field
// "This field may be used to identify a device manufacturer.
// If used the signature shall match the signature contained in the appropriate section of the ICC signature registry found at www.color.org"
// Device manufacturers can be looked up at https://www.color.org/signatureRegistry/index.xalter
// For example: https://www.color.org/signatureRegistry/?entityEntry=APPL-4150504C
// Some icc files use codes not in that registry. For example. D50_XYZ.icc from https://www.color.org/XYZprofiles.xalter
// has its device manufacturer set to 'none', but https://www.color.org/signatureRegistry/?entityEntry=none-6E6F6E65 does not exist.
// "If not used this field shall be set to zero (00000000h)."
2023-01-18 22:16:10 -05:00
if ( header . device_manufacturer = = DeviceManufacturer { 0 } )
2023-01-06 15:57:45 -05:00
return { } ;
2023-01-18 22:16:10 -05:00
return header . device_manufacturer ;
2023-01-06 15:57:45 -05:00
}
Optional < DeviceModel > parse_device_model ( ICCHeader const & header )
{
// ICC v4, 7.2.13 Device model field
// "This field may be used to identify a device model.
// If used the signature shall match the signature contained in the appropriate section of the ICC signature registry found at www.color.org"
// Device models can be looked up at https://www.color.org/signatureRegistry/deviceRegistry/index.xalter
// For example: https://www.color.org/signatureRegistry/deviceRegistry/?entityEntry=7FD8-37464438
// Some icc files use codes not in that registry. For example. D50_XYZ.icc from https://www.color.org/XYZprofiles.xalter
// has its device model set to 'none', but https://www.color.org/signatureRegistry/deviceRegistry?entityEntry=none-6E6F6E65 does not exist.
// "If not used this field shall be set to zero (00000000h)."
2023-01-18 22:16:10 -05:00
if ( header . device_model = = DeviceModel { 0 } )
2023-01-06 15:57:45 -05:00
return { } ;
2023-01-18 22:16:10 -05:00
return header . device_model ;
2023-01-06 15:57:45 -05:00
}
2023-01-07 11:04:36 -05:00
ErrorOr < DeviceAttributes > parse_device_attributes ( ICCHeader const & header )
{
// ICC v4, 7.2.14 Device attributes field
// "4 to 31": "Reserved (set to binary zero)"
if ( header . device_attributes & 0xffff'fff0 )
return Error : : from_string_literal ( " ICC::Profile: Device attributes reserved bits not set to 0 " ) ;
return DeviceAttributes { header . device_attributes } ;
}
2022-12-30 08:15:49 -05:00
ErrorOr < RenderingIntent > parse_rendering_intent ( ICCHeader const & header )
{
// ICC v4, 7.2.15 Rendering intent field
switch ( header . rendering_intent ) {
case 0 :
return RenderingIntent : : Perceptual ;
case 1 :
return RenderingIntent : : MediaRelativeColorimetric ;
case 2 :
return RenderingIntent : : Saturation ;
case 3 :
return RenderingIntent : : ICCAbsoluteColorimetric ;
}
return Error : : from_string_literal ( " ICC::Profile: Invalid rendering intent " ) ;
}
2023-01-01 07:09:38 -05:00
ErrorOr < XYZ > parse_pcs_illuminant ( ICCHeader const & header )
{
// ICC v4, 7.2.16 PCS illuminant field
XYZ xyz = ( XYZ ) header . pcs_illuminant ;
/// "The value, when rounded to four decimals, shall be X = 0,9642, Y = 1,0 and Z = 0,8249."
if ( round ( xyz . x * 10'000 ) ! = 9'642 | | round ( xyz . y * 10'000 ) ! = 10'000 | | round ( xyz . z * 10'000 ) ! = 8'249 )
return Error : : from_string_literal ( " ICC::Profile: Invalid pcs illuminant " ) ;
return xyz ;
}
2023-01-04 14:55:16 -05:00
2023-01-06 15:57:45 -05:00
Optional < Creator > parse_profile_creator ( ICCHeader const & header )
{
// ICC v4, 7.2.17 Profile creator field
// "This field may be used to identify the creator of the profile.
// If used the signature should match the signature contained in the device manufacturer section of the ICC signature registry found at www.color.org."
// This is not always true in practice.
// For example, .icc files in /System/ColorSync/Profiles on macOS 12.6 set this to 'appl', which is a CMM signature, not a device signature (that one would be 'APPL').
// "If not used this field shall be set to zero (00000000h)."
2023-01-18 22:16:10 -05:00
if ( header . profile_creator = = Creator { 0 } )
2023-01-06 15:57:45 -05:00
return { } ;
2023-01-18 22:16:10 -05:00
return header . profile_creator ;
2023-01-06 15:57:45 -05:00
}
2023-01-06 12:05:53 -05:00
template < size_t N >
bool all_bytes_are_zero ( const u8 ( & bytes ) [ N ] )
{
for ( u8 byte : bytes ) {
if ( byte ! = 0 )
return false ;
}
return true ;
}
2023-01-06 13:46:18 -05:00
ErrorOr < Optional < Crypto : : Hash : : MD5 : : DigestType > > parse_profile_id ( ICCHeader const & header , ReadonlyBytes icc_bytes )
2023-01-04 14:55:16 -05:00
{
// ICC v4, 7.2.18 Profile ID field
// "A profile ID field value of zero (00h) shall indicate that a profile ID has not been calculated."
2023-01-06 12:05:53 -05:00
if ( all_bytes_are_zero ( header . profile_id ) )
2023-01-21 18:30:01 -05:00
return OptionalNone { } ;
2023-01-04 14:55:16 -05:00
2023-01-06 13:40:10 -05:00
Crypto : : Hash : : MD5 : : DigestType id ;
static_assert ( sizeof ( id . data ) = = sizeof ( header . profile_id ) ) ;
memcpy ( id . data , header . profile_id , sizeof ( id . data ) ) ;
2023-01-04 15:34:25 -05:00
2023-01-06 13:46:18 -05:00
auto computed_id = Profile : : compute_id ( icc_bytes ) ;
if ( id ! = computed_id )
return Error : : from_string_literal ( " ICC::Profile: Invalid profile id " ) ;
2023-01-04 15:34:25 -05:00
2023-01-06 13:40:10 -05:00
return id ;
2023-01-04 14:55:16 -05:00
}
2023-01-06 12:06:12 -05:00
ErrorOr < void > parse_reserved ( ICCHeader const & header )
{
// ICC v4, 7.2.19 Reserved field
// "This field of the profile header is reserved for future ICC definition and shall be set to zero."
if ( ! all_bytes_are_zero ( header . reserved ) )
return Error : : from_string_literal ( " ICC::Profile: Reserved header bytes are not zero " ) ;
return { } ;
}
2022-12-26 08:21:26 -05:00
}
2023-01-21 16:14:39 -05:00
URL device_manufacturer_url ( DeviceManufacturer device_manufacturer )
{
return URL ( DeprecatedString : : formatted ( " https://www.color.org/signatureRegistry/?entityEntry={:c}{:c}{:c}{:c}-{:08X} " ,
device_manufacturer . c0 ( ) , device_manufacturer . c1 ( ) , device_manufacturer . c2 ( ) , device_manufacturer . c3 ( ) , device_manufacturer . value ) ) ;
}
URL device_model_url ( DeviceModel device_model )
{
return URL ( DeprecatedString : : formatted ( " https://www.color.org/signatureRegistry/deviceRegistry/?entityEntry={:c}{:c}{:c}{:c}-{:08X} " ,
device_model . c0 ( ) , device_model . c1 ( ) , device_model . c2 ( ) , device_model . c3 ( ) , device_model . value ) ) ;
}
2023-01-24 15:21:20 -05:00
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 { } ;
}
2022-12-31 11:45:36 -05:00
StringView device_class_name ( DeviceClass device_class )
2022-12-26 08:21:26 -05:00
{
switch ( device_class ) {
case DeviceClass : : InputDevce :
2022-12-31 11:45:36 -05:00
return " InputDevce " sv ;
2022-12-26 08:21:26 -05:00
case DeviceClass : : DisplayDevice :
2022-12-31 11:45:36 -05:00
return " DisplayDevice " sv ;
2022-12-26 08:21:26 -05:00
case DeviceClass : : OutputDevice :
2022-12-31 11:45:36 -05:00
return " OutputDevice " sv ;
2022-12-26 08:21:26 -05:00
case DeviceClass : : DeviceLink :
2022-12-31 11:45:36 -05:00
return " DeviceLink " sv ;
2022-12-26 08:21:26 -05:00
case DeviceClass : : ColorSpace :
2022-12-31 11:45:36 -05:00
return " ColorSpace " sv ;
2022-12-26 08:21:26 -05:00
case DeviceClass : : Abstract :
2022-12-31 11:45:36 -05:00
return " Abstract " sv ;
2022-12-26 08:21:26 -05:00
case DeviceClass : : NamedColor :
2022-12-31 11:45:36 -05:00
return " NamedColor " sv ;
2022-12-26 08:21:26 -05:00
}
2022-12-31 11:49:57 -05:00
VERIFY_NOT_REACHED ( ) ;
2022-12-26 08:21:26 -05:00
}
2022-12-30 11:07:38 -05:00
StringView data_color_space_name ( ColorSpace color_space )
2022-12-30 07:54:58 -05:00
{
switch ( color_space ) {
case ColorSpace : : nCIEXYZ :
2022-12-31 11:45:36 -05:00
return " nCIEXYZ " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : CIELAB :
2022-12-31 11:45:36 -05:00
return " CIELAB " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : CIELUV :
2022-12-31 11:45:36 -05:00
return " CIELUV " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : YCbCr :
2022-12-31 11:45:36 -05:00
return " YCbCr " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : CIEYxy :
2022-12-31 11:45:36 -05:00
return " CIEYxy " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : RGB :
2022-12-31 11:45:36 -05:00
return " RGB " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : Gray :
2022-12-31 11:45:36 -05:00
return " Gray " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : HSV :
2022-12-31 11:45:36 -05:00
return " HSV " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : HLS :
2022-12-31 11:45:36 -05:00
return " HLS " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : CMYK :
2022-12-31 11:45:36 -05:00
return " CMYK " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : CMY :
2022-12-31 11:45:36 -05:00
return " CMY " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : TwoColor :
2022-12-31 11:45:36 -05:00
return " 2 color " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : ThreeColor :
2022-12-31 11:45:36 -05:00
return " 3 color (other than XYZ, Lab, Luv, YCbCr, CIEYxy, RGB, HSV, HLS, CMY) " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : FourColor :
2022-12-31 11:45:36 -05:00
return " 4 color (other than CMYK) " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : FiveColor :
2022-12-31 11:45:36 -05:00
return " 5 color " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : SixColor :
2022-12-31 11:45:36 -05:00
return " 6 color " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : SevenColor :
2022-12-31 11:45:36 -05:00
return " 7 color " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : EightColor :
2022-12-31 11:45:36 -05:00
return " 8 color " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : NineColor :
2022-12-31 11:45:36 -05:00
return " 9 color " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : TenColor :
2022-12-31 11:45:36 -05:00
return " 10 color " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : ElevenColor :
2022-12-31 11:45:36 -05:00
return " 11 color " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : TwelveColor :
2022-12-31 11:45:36 -05:00
return " 12 color " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : ThirteenColor :
2022-12-31 11:45:36 -05:00
return " 13 color " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : FourteenColor :
2022-12-31 11:45:36 -05:00
return " 14 color " sv ;
2022-12-30 07:54:58 -05:00
case ColorSpace : : FifteenColor :
2022-12-31 11:45:36 -05:00
return " 15 color " sv ;
2022-12-30 07:54:58 -05:00
}
2022-12-31 11:49:57 -05:00
VERIFY_NOT_REACHED ( ) ;
2022-12-30 07:54:58 -05:00
}
2022-12-30 11:07:38 -05:00
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 ) ;
}
}
2023-01-07 15:22:15 -05:00
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 ( ) ;
}
2022-12-31 11:45:36 -05:00
StringView rendering_intent_name ( RenderingIntent rendering_intent )
2022-12-30 08:15:49 -05:00
{
switch ( rendering_intent ) {
case RenderingIntent : : Perceptual :
2022-12-31 11:45:36 -05:00
return " Perceptual " sv ;
2022-12-30 08:15:49 -05:00
case RenderingIntent : : MediaRelativeColorimetric :
2022-12-31 11:45:36 -05:00
return " Media-relative colorimetric " sv ;
2022-12-30 08:15:49 -05:00
case RenderingIntent : : Saturation :
2022-12-31 11:45:36 -05:00
return " Saturation " sv ;
2022-12-30 08:15:49 -05:00
case RenderingIntent : : ICCAbsoluteColorimetric :
2022-12-31 11:45:36 -05:00
return " ICC-absolute colorimetric " sv ;
2022-12-30 08:15:49 -05:00
}
2022-12-31 11:49:57 -05:00
VERIFY_NOT_REACHED ( ) ;
2022-12-30 08:15:49 -05:00
}
2022-12-31 13:50:52 -05:00
Flags : : Flags ( ) = default ;
Flags : : Flags ( u32 bits )
: m_bits ( bits )
{
}
2023-01-06 15:06:30 -05:00
DeviceAttributes : : DeviceAttributes ( ) = default ;
DeviceAttributes : : DeviceAttributes ( u64 bits )
: m_bits ( bits )
{
}
2023-01-21 20:50:20 -05:00
static TagTypeSignature tag_type ( ReadonlyBytes tag_bytes )
{
VERIFY ( tag_bytes . size ( ) > = sizeof ( u32 ) ) ;
return * bit_cast < BigEndian < TagTypeSignature > const * > ( tag_bytes . data ( ) ) ;
}
static ErrorOr < void > check_reserved ( ReadonlyBytes tag_bytes )
{
if ( tag_bytes . size ( ) < 2 * sizeof ( u32 ) )
return Error : : from_string_literal ( " ICC::Profile: Not enough data for tag reserved field " ) ;
if ( * bit_cast < BigEndian < u32 > const * > ( tag_bytes . data ( ) + sizeof ( u32 ) ) ! = 0 )
return Error : : from_string_literal ( " ICC::Profile: tag reserved field not 0 " ) ;
return { } ;
}
2023-01-23 13:14:20 -05:00
ErrorOr < NonnullRefPtr < CurveTagData > > CurveTagData : : from_bytes ( ReadonlyBytes bytes , u32 offset , u32 size )
{
// ICC v4, 10.6 curveType
VERIFY ( tag_type ( bytes ) = = Type ) ;
TRY ( check_reserved ( bytes ) ) ;
if ( bytes . size ( ) < 3 * sizeof ( u32 ) )
return Error : : from_string_literal ( " ICC::Profile: curveType has not enough data for count " ) ;
u32 count = * bit_cast < BigEndian < u32 > const * > ( bytes . data ( ) + 8 ) ;
if ( bytes . size ( ) < 3 * sizeof ( u32 ) + count * sizeof ( u16 ) )
return Error : : from_string_literal ( " ICC::Profile: curveType has not enough data for curve points " ) ;
BigEndian < u16 > const * raw_values = bit_cast < BigEndian < u16 > const * > ( bytes . data ( ) + 12 ) ;
Vector < u16 > values ;
TRY ( values . try_resize ( count ) ) ;
for ( u32 i = 0 ; i < count ; + + i )
values [ i ] = raw_values [ i ] ;
return adopt_ref ( * new CurveTagData ( offset , size , move ( values ) ) ) ;
}
2023-01-21 21:32:22 -05:00
ErrorOr < NonnullRefPtr < MultiLocalizedUnicodeTagData > > MultiLocalizedUnicodeTagData : : from_bytes ( ReadonlyBytes bytes , u32 offset , u32 size )
{
// ICC v4, 10.15 multiLocalizedUnicodeType
2023-01-23 08:58:17 -05:00
VERIFY ( tag_type ( bytes ) = = Type ) ;
2023-01-21 21:32:22 -05:00
TRY ( check_reserved ( bytes ) ) ;
// "Multiple strings within this tag may share storage locations. For example, en/US and en/UK can refer to the
// same string data."
// This implementation makes redudant string copies in that case.
// Most of the time, this costs just a few bytes, so that seems ok.
if ( bytes . size ( ) < 4 * sizeof ( u32 ) )
return Error : : from_string_literal ( " ICC::Profile: multiLocalizedUnicodeType has not enough data " ) ;
// Table 54 — multiLocalizedUnicodeType
u32 number_of_records = * bit_cast < BigEndian < u32 > const * > ( bytes . data ( ) + 8 ) ;
u32 record_size = * bit_cast < BigEndian < u32 > const * > ( bytes . data ( ) + 12 ) ;
// "The fourth field of this tag, the record size, should contain the value 12, which corresponds to the size in bytes
// of each record. Any code that needs to access the nth record should determine the record’ s offset by multiplying
// n by the contents of this size field and adding 16. This minor extra effort allows for future expansion of the record
// encoding, should the need arise, without having to define a new tag type."
if ( record_size < 12 )
return Error : : from_string_literal ( " ICC::Profile: multiLocalizedUnicodeType record size too small " ) ;
if ( bytes . size ( ) < 16 + number_of_records * record_size )
return Error : : from_string_literal ( " ICC::Profile: multiLocalizedUnicodeType not enough data for records " ) ;
Vector < Record > records ;
TRY ( records . try_resize ( number_of_records ) ) ;
// "For the definition of language codes and country codes, see respectively
// ISO 639-1 and ISO 3166-1. The Unicode strings in storage should be encoded as 16-bit big-endian, UTF-16BE,
// and should not be NULL terminated."
auto & utf_16be_decoder = * TextCodec : : decoder_for ( " utf-16be " ) ;
struct RawRecord {
BigEndian < u16 > language_code ;
BigEndian < u16 > country_code ;
BigEndian < u32 > string_length_in_bytes ;
BigEndian < u32 > string_offset_in_bytes ;
} ;
for ( u32 i = 0 ; i < number_of_records ; + + i ) {
size_t offset = 16 + i * record_size ;
RawRecord record = * bit_cast < RawRecord const * > ( bytes . data ( ) + offset ) ;
records [ i ] . iso_639_1_language_code = record . language_code ;
records [ i ] . iso_3166_1_country_code = record . country_code ;
if ( record . string_length_in_bytes % 2 ! = 0 )
return Error : : from_string_literal ( " ICC::Profile: multiLocalizedUnicodeType odd UTF-16 byte length " ) ;
if ( record . string_offset_in_bytes + record . string_length_in_bytes > bytes . size ( ) )
return Error : : from_string_literal ( " ICC::Profile: multiLocalizedUnicodeType string offset out of bounds " ) ;
StringView utf_16be_data { bytes . data ( ) + record . string_offset_in_bytes , record . string_length_in_bytes } ;
records [ i ] . text = TRY ( String : : from_deprecated_string ( utf_16be_decoder . to_utf8 ( utf_16be_data ) ) ) ;
}
return adopt_ref ( * new MultiLocalizedUnicodeTagData ( offset , size , move ( records ) ) ) ;
}
2023-01-23 13:55:57 -05:00
unsigned ParametricCurveTagData : : parameter_count ( FunctionType function_type )
{
switch ( function_type ) {
case FunctionType : : Type0 :
return 1 ;
case FunctionType : : Type1 :
return 3 ;
case FunctionType : : Type2 :
return 4 ;
case FunctionType : : Type3 :
return 5 ;
case FunctionType : : Type4 :
return 7 ;
}
VERIFY_NOT_REACHED ( ) ;
}
ErrorOr < NonnullRefPtr < ParametricCurveTagData > > ParametricCurveTagData : : from_bytes ( ReadonlyBytes bytes , u32 offset , u32 size )
{
// ICC v4, 10.18 parametricCurveType
VERIFY ( tag_type ( bytes ) = = Type ) ;
TRY ( check_reserved ( bytes ) ) ;
// "The parametricCurveType describes a one-dimensional curve by specifying one of a predefined set of functions
// using the parameters."
if ( bytes . size ( ) < 2 * sizeof ( u32 ) + 2 * sizeof ( u16 ) )
return Error : : from_string_literal ( " ICC::Profile: parametricCurveType has not enough data " ) ;
u16 raw_function_type = * bit_cast < BigEndian < u16 > const * > ( bytes . data ( ) + 8 ) ;
u16 reserved = * bit_cast < BigEndian < u16 > const * > ( bytes . data ( ) + 10 ) ;
if ( reserved ! = 0 )
return Error : : from_string_literal ( " ICC::Profile: parametricCurveType reserved u16 after function type not 0 " ) ;
if ( raw_function_type > 4 )
return Error : : from_string_literal ( " ICC::Profile: parametricCurveType unknown function type " ) ;
FunctionType function_type = ( FunctionType ) raw_function_type ;
unsigned count = parameter_count ( function_type ) ;
if ( bytes . size ( ) < 2 * sizeof ( u32 ) + 2 * sizeof ( u16 ) + count * sizeof ( s15Fixed16Number ) )
return Error : : from_string_literal ( " ICC::Profile: parametricCurveType has not enough data for parameters " ) ;
BigEndian < s15Fixed16Number > const * raw_parameters = bit_cast < BigEndian < s15Fixed16Number > const * > ( bytes . data ( ) + 12 ) ;
Array < S15Fixed16 , 7 > parameters ;
parameters . fill ( 0 ) ;
for ( unsigned i = 0 ; i < count ; + + i )
parameters [ i ] = S15Fixed16 : : create_raw ( raw_parameters [ i ] ) ;
return adopt_ref ( * new ParametricCurveTagData ( offset , size , function_type , move ( parameters ) ) ) ;
}
2023-01-23 11:00:22 -05:00
ErrorOr < NonnullRefPtr < S15Fixed16ArrayTagData > > S15Fixed16ArrayTagData : : from_bytes ( ReadonlyBytes bytes , u32 offset , u32 size )
{
// ICC v4, 10.22 s15Fixed16ArrayType
VERIFY ( tag_type ( bytes ) = = Type ) ;
TRY ( check_reserved ( bytes ) ) ;
// "This type represents an array of generic 4-byte (32-bit) fixed point quantity. The number of values is determined
// from the size of the tag."
size_t byte_size = bytes . size ( ) - 8 ;
if ( byte_size % sizeof ( s15Fixed16Number ) ! = 0 )
return Error : : from_string_literal ( " ICC::Profile: s15Fixed16ArrayType has wrong size " ) ;
size_t count = byte_size / sizeof ( s15Fixed16Number ) ;
BigEndian < s15Fixed16Number > const * raw_values = bit_cast < BigEndian < s15Fixed16Number > const * > ( bytes . data ( ) + 8 ) ;
Vector < S15Fixed16 , 9 > values ;
TRY ( values . try_resize ( count ) ) ;
for ( size_t i = 0 ; i < count ; + + i )
values [ i ] = S15Fixed16 : : create_raw ( raw_values [ i ] ) ;
return adopt_ref ( * new S15Fixed16ArrayTagData ( offset , size , move ( values ) ) ) ;
}
2023-01-22 21:49:05 -05:00
ErrorOr < NonnullRefPtr < TextDescriptionTagData > > TextDescriptionTagData : : from_bytes ( ReadonlyBytes bytes , u32 offset , u32 size )
{
// ICC v2, 6.5.17 textDescriptionType
// textDescriptionType is no longer in the V4 spec.
// In both the V2 and V4 specs, 'desc' is a required tag. In V4, it has type multiLocalizedUnicodeType,
// but in V2 it has type textDescriptionType. Since 'desc' is required, this type is present in every
// V2 icc file, and there are still many V2 files in use. So textDescriptionType is here to stay for now.
// It's a very 90s type, preceding universal adoption of Unicode.
// "The textDescriptionType is a complex structure that contains three types of text description structures:
// 7-bit ASCII, Unicode and ScriptCode. Since no single standard method for specifying localizable character
// sets exists across the major platform vendors, including all three provides access for the major operating
// systems. The 7-bit ASCII description is to be an invariant, nonlocalizable name for consistent reference.
// It is preferred that both the Unicode and ScriptCode structures be properly localized."
2023-01-23 08:58:17 -05:00
VERIFY ( tag_type ( bytes ) = = Type ) ;
2023-01-22 21:49:05 -05:00
TRY ( check_reserved ( bytes ) ) ;
// 7-bit ASCII
// "ASCII: The count is the length of the string in bytes including the null terminator."
if ( bytes . size ( ) < 3 * sizeof ( u32 ) )
return Error : : from_string_literal ( " ICC::Profile: textDescriptionType has not enough data for ASCII size " ) ;
u32 ascii_description_length = * bit_cast < BigEndian < u32 > const * > ( bytes . data ( ) + 8 ) ;
if ( bytes . size ( ) < 3 * sizeof ( u32 ) + ascii_description_length )
return Error : : from_string_literal ( " ICC::Profile: textDescriptionType has not enough data for ASCII description " ) ;
u8 const * ascii_description_data = bytes . data ( ) + 3 * sizeof ( u32 ) ;
for ( u32 i = 0 ; i < ascii_description_length ; + + i ) {
if ( ascii_description_data [ i ] > = 128 )
return Error : : from_string_literal ( " ICC::Profile: textDescriptionType ASCII description not 7-bit ASCII " ) ;
}
if ( ascii_description_length = = 0 )
return Error : : from_string_literal ( " ICC::Profile: textDescriptionType ASCII description length does not include trailing \\ 0 " ) ;
if ( ascii_description_data [ ascii_description_length - 1 ] ! = ' \0 ' )
return Error : : from_string_literal ( " ICC::Profile: textDescriptionType ASCII description not \\ 0-terminated " ) ;
StringView ascii_description { ascii_description_data , ascii_description_length - 1 } ;
// Unicode
if ( bytes . size ( ) < 3 * sizeof ( u32 ) + ascii_description_length + 2 * sizeof ( u32 ) )
return Error : : from_string_literal ( " ICC::Profile: textDescriptionType has not enough data for Unicode metadata " ) ;
// "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 const * cursor = ascii_description_data + ascii_description_length ;
u32 unicode_language_code = ( u32 ) ( cursor [ 0 ] < < 24 ) | ( u32 ) ( cursor [ 1 ] < < 16 ) | ( u32 ) ( cursor [ 2 ] < < 8 ) | ( u32 ) cursor [ 3 ] ;
cursor + = 4 ;
// "Unicode: The count is the number of characters including a Unicode null where a character is always two bytes."
// This implies UCS-2.
u32 unicode_description_length = ( u32 ) ( cursor [ 0 ] < < 24 ) | ( u32 ) ( cursor [ 1 ] < < 16 ) | ( u32 ) ( cursor [ 2 ] < < 8 ) | ( u32 ) cursor [ 3 ] ;
cursor + = 4 ;
if ( bytes . size ( ) < 3 * sizeof ( u32 ) + ascii_description_length + 2 * sizeof ( u32 ) + 2 * unicode_description_length )
return Error : : from_string_literal ( " ICC::Profile: textDescriptionType has not enough data for Unicode description " ) ;
u8 const * unicode_description_data = cursor ;
cursor + = 2 * unicode_description_length ;
for ( u32 i = 0 ; i < unicode_description_length ; + + i ) {
u16 code_point = ( u16 ) ( unicode_description_data [ 2 * i ] < < 8 ) | ( u16 ) unicode_description_data [ 2 * i + 1 ] ;
if ( is_unicode_surrogate ( code_point ) )
return Error : : from_string_literal ( " ICC::Profile: textDescriptionType Unicode description is not valid UCS-2 " ) ;
}
// If Unicode is not native on the platform, then the Unicode language code and Unicode count should be
// filled in as 0, with no data placed in the Unicode localizable profile description area.
Optional < String > unicode_description ;
if ( unicode_description_length > 0 ) {
u16 last_code_point = ( u16 ) ( unicode_description_data [ 2 * ( unicode_description_length - 1 ) ] < < 8 ) | ( u16 ) unicode_description_data [ 2 * ( unicode_description_length - 1 ) + 1 ] ;
if ( last_code_point ! = 0 )
return Error : : from_string_literal ( " ICC::Profile: textDescriptionType Unicode description not \\ 0-terminated " ) ;
StringView utf_16be_data { unicode_description_data , 2 * ( unicode_description_length - 1 ) } ;
unicode_description = TRY ( String : : from_deprecated_string ( TextCodec : : decoder_for ( " utf-16be " ) - > to_utf8 ( utf_16be_data ) ) ) ;
}
// ScriptCode
// What is a script code? It's an old, obsolete mac thing. It looks like it's documented in
// https://developer.apple.com/library/archive/documentation/mac/pdf/Text.pdf
// "Script Codes, Language Codes, and Region Codes 1", PDF page 82.
// I haven't found a complete explanation though. PDF page 84 suggests that:
// - There are 16 script codes
// - 0 is Roman, 1 is Japanese, 2 is Chinese, 3 is Korean, 9 is Devanagari
// Roman uses https://en.wikipedia.org/wiki/Mac_OS_Roman as encoding (also on page 89),
// and "All non-Roman script systems include Roman as a subscript" (page 87).
// Aha, "Script Codes 6" on page 676 has the complete list! There are 32 of them.
// The document mentions that each script code possibly has its own encoding, but I haven't found
// details on the encodings for script codes other than 0 (which uses Mac OS Roman).
// http://www.kreativekorp.com/charset/encoding/ has an unofficial list of old Mac OS encodings,
// but it's not clear to me which script codes map to which encoding.
// From here on, quotes are from the ICC spec on textDescriptionType again.
// "The ScriptCode code is misaligned when the ASCII count is odd."
// So don't use BigEndian<u16> here.
u16 scriptcode_code = ( u16 ) ( cursor [ 0 ] < < 8 ) | ( u32 ) cursor [ 1 ] ;
cursor + = 2 ;
// "ScriptCode: The count is the length of the string in bytes including the terminating null."
u8 macintosh_description_length = * cursor ;
cursor + = 1 ;
if ( macintosh_description_length > 67 )
return Error : : from_string_literal ( " ICC::Profile: textDescriptionType ScriptCode description too long " ) ;
u8 const * macintosh_description_data = cursor ;
// "If Scriptcode is not native on the platform, then the ScriptCode code and ScriptCode count should be filled
// in as 0. The 67-byte localizable Macintosh profile description should be filled with 0’ s."
Optional < String > macintosh_description ;
if ( macintosh_description_length > 0 ) {
// ScriptCode is old-timey and a complicated to fully support. Lightroom Classic does write the ScriptCode section of textDescriptionType.
// But supporting only ASCII MacRoman is good enough for those files, and easy to implement, so let's do only that for now.
if ( scriptcode_code = = 0 ) { // MacRoman
if ( macintosh_description_data [ macintosh_description_length - 1 ] ! = ' \0 ' )
return Error : : from_string_literal ( " ICC::Profile: textDescriptionType ScriptCode not \\ 0-terminated " ) ;
2023-01-24 08:55:51 -05:00
macintosh_description = TRY ( String : : from_deprecated_string ( TextCodec : : decoder_for ( " x-mac-roman " ) - > to_utf8 ( { macintosh_description_data , ( size_t ) macintosh_description_length - 1 } ) ) ) ;
2023-01-22 21:49:05 -05:00
} else {
dbgln ( " TODO: ICCProfile textDescriptionType ScriptCode {}, length {} " , scriptcode_code , macintosh_description_length ) ;
}
}
return adopt_ref ( * new TextDescriptionTagData ( offset , size , TRY ( String : : from_utf8 ( ascii_description ) ) , unicode_language_code , move ( unicode_description ) , move ( macintosh_description ) ) ) ;
}
2023-01-21 20:50:20 -05:00
ErrorOr < NonnullRefPtr < TextTagData > > TextTagData : : from_bytes ( ReadonlyBytes bytes , u32 offset , u32 size )
{
// ICC v4, 10.24 textType
2023-01-23 08:58:17 -05:00
VERIFY ( tag_type ( bytes ) = = Type ) ;
2023-01-21 20:50:20 -05:00
TRY ( check_reserved ( bytes ) ) ;
// "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."
u32 length = bytes . size ( ) - 8 ;
u8 const * text_data = bytes . data ( ) + 8 ;
for ( u32 i = 0 ; i < length ; + + i ) {
if ( text_data [ i ] > = 128 )
2023-01-22 21:56:49 -05:00
return Error : : from_string_literal ( " ICC::Profile: textType data not 7-bit ASCII " ) ;
2023-01-21 20:50:20 -05:00
}
if ( length = = 0 )
return Error : : from_string_literal ( " ICC::Profile: textType too short for \\ 0 byte " ) ;
if ( text_data [ length - 1 ] ! = ' \0 ' )
return Error : : from_string_literal ( " ICC::Profile: textType data not \\ 0-terminated " ) ;
return adopt_ref ( * new TextTagData ( offset , size , TRY ( String : : from_utf8 ( StringView ( text_data , length - 1 ) ) ) ) ) ;
}
2023-01-22 22:25:44 -05:00
ErrorOr < NonnullRefPtr < XYZTagData > > XYZTagData : : from_bytes ( ReadonlyBytes bytes , u32 offset , u32 size )
{
// ICC v4, 10.31 XYZType
2023-01-23 08:58:17 -05:00
VERIFY ( tag_type ( bytes ) = = Type ) ;
2023-01-22 22:25:44 -05:00
TRY ( check_reserved ( bytes ) ) ;
// "The XYZType contains an array of three encoded values for PCSXYZ, CIEXYZ, or nCIEXYZ values. The
// number of sets of values is determined from the size of the tag."
size_t byte_size = bytes . size ( ) - 8 ;
if ( byte_size % sizeof ( XYZNumber ) ! = 0 )
2023-01-23 13:04:27 -05:00
return Error : : from_string_literal ( " ICC::Profile: XYZType has wrong size " ) ;
2023-01-22 22:25:44 -05:00
size_t xyz_count = byte_size / sizeof ( XYZNumber ) ;
XYZNumber const * raw_xyzs = bit_cast < XYZNumber const * > ( bytes . data ( ) + 8 ) ;
Vector < XYZ , 1 > xyzs ;
TRY ( xyzs . try_resize ( xyz_count ) ) ;
for ( size_t i = 0 ; i < xyz_count ; + + i )
xyzs [ i ] = ( XYZ ) raw_xyzs [ i ] ;
return adopt_ref ( * new XYZTagData ( offset , size , move ( xyzs ) ) ) ;
}
2023-01-18 22:28:10 -05:00
ErrorOr < void > Profile : : read_header ( ReadonlyBytes bytes )
2022-12-26 08:21:26 -05:00
{
if ( bytes . size ( ) < sizeof ( ICCHeader ) )
return Error : : from_string_literal ( " ICC::Profile: Not enough data for header " ) ;
auto header = * bit_cast < ICCHeader const * > ( bytes . data ( ) ) ;
TRY ( parse_file_signature ( header ) ) ;
2023-01-18 22:28:10 -05:00
m_on_disk_size = TRY ( parse_size ( header , bytes ) ) ;
m_preferred_cmm_type = parse_preferred_cmm_type ( header ) ;
m_version = TRY ( parse_version ( header ) ) ;
m_device_class = TRY ( parse_device_class ( header ) ) ;
m_data_color_space = TRY ( parse_data_color_space ( header ) ) ;
m_connection_space = TRY ( parse_connection_space ( header ) ) ;
m_creation_timestamp = TRY ( parse_creation_date_time ( header ) ) ;
m_primary_platform = TRY ( parse_primary_platform ( header ) ) ;
m_flags = Flags { header . profile_flags } ;
m_device_manufacturer = parse_device_manufacturer ( header ) ;
m_device_model = parse_device_model ( header ) ;
m_device_attributes = TRY ( parse_device_attributes ( header ) ) ;
m_rendering_intent = TRY ( parse_rendering_intent ( header ) ) ;
m_pcs_illuminant = TRY ( parse_pcs_illuminant ( header ) ) ;
m_creator = parse_profile_creator ( header ) ;
m_id = TRY ( parse_profile_id ( header , bytes ) ) ;
2023-01-06 12:06:12 -05:00
TRY ( parse_reserved ( header ) ) ;
2022-12-26 08:21:26 -05:00
2023-01-18 22:28:10 -05:00
return { } ;
}
2023-01-23 18:21:36 -05:00
ErrorOr < NonnullRefPtr < TagData > > Profile : : read_tag ( ReadonlyBytes bytes , u32 offset_to_beginning_of_tag_data_element , u32 size_of_tag_data_element )
2023-01-20 14:37:25 -05:00
{
2023-01-23 18:21:36 -05:00
if ( offset_to_beginning_of_tag_data_element + size_of_tag_data_element > bytes . size ( ) )
2023-01-20 14:37:25 -05:00
return Error : : from_string_literal ( " ICC::Profile: Tag data out of bounds " ) ;
2023-01-23 18:21:36 -05:00
auto tag_bytes = bytes . slice ( offset_to_beginning_of_tag_data_element , size_of_tag_data_element ) ;
2023-01-20 14:37:25 -05:00
// ICC v4, 9 Tag definitions
// ICC v4, 9.1 General
// "All tags, including private tags, have as their first four bytes a tag signature to identify to profile readers
// what kind of data is contained within a tag."
if ( tag_bytes . size ( ) < sizeof ( u32 ) )
return Error : : from_string_literal ( " ICC::Profile: Not enough data for tag type " ) ;
2023-01-21 20:50:20 -05:00
auto type = tag_type ( tag_bytes ) ;
switch ( type ) {
2023-01-23 13:14:20 -05:00
case CurveTagData : : Type :
2023-01-23 18:21:36 -05:00
return CurveTagData : : from_bytes ( tag_bytes , offset_to_beginning_of_tag_data_element , size_of_tag_data_element ) ;
2023-01-21 21:32:22 -05:00
case MultiLocalizedUnicodeTagData : : Type :
2023-01-23 18:21:36 -05:00
return MultiLocalizedUnicodeTagData : : from_bytes ( tag_bytes , offset_to_beginning_of_tag_data_element , size_of_tag_data_element ) ;
2023-01-23 13:55:57 -05:00
case ParametricCurveTagData : : Type :
2023-01-23 18:21:36 -05:00
return ParametricCurveTagData : : from_bytes ( tag_bytes , offset_to_beginning_of_tag_data_element , size_of_tag_data_element ) ;
2023-01-23 11:00:22 -05:00
case S15Fixed16ArrayTagData : : Type :
2023-01-23 18:21:36 -05:00
return S15Fixed16ArrayTagData : : from_bytes ( tag_bytes , offset_to_beginning_of_tag_data_element , size_of_tag_data_element ) ;
2023-01-22 21:49:05 -05:00
case TextDescriptionTagData : : Type :
2023-01-23 18:21:36 -05:00
return TextDescriptionTagData : : from_bytes ( tag_bytes , offset_to_beginning_of_tag_data_element , size_of_tag_data_element ) ;
2023-01-21 20:50:20 -05:00
case TextTagData : : Type :
2023-01-23 18:21:36 -05:00
return TextTagData : : from_bytes ( tag_bytes , offset_to_beginning_of_tag_data_element , size_of_tag_data_element ) ;
2023-01-22 22:25:44 -05:00
case XYZTagData : : Type :
2023-01-23 18:21:36 -05:00
return XYZTagData : : from_bytes ( tag_bytes , offset_to_beginning_of_tag_data_element , size_of_tag_data_element ) ;
2023-01-20 14:37:25 -05:00
default :
// FIXME: optionally ignore tags of unknown type
2023-01-23 18:21:36 -05:00
return adopt_ref ( * new UnknownTagData ( offset_to_beginning_of_tag_data_element , size_of_tag_data_element , type ) ) ;
2023-01-20 14:37:25 -05:00
}
}
ErrorOr < void > Profile : : read_tag_table ( ReadonlyBytes bytes )
{
// ICC v4, 7.3 Tag table
// ICC v4, 7.3.1 Overview
// "The tag table acts as a table of contents for the tags and an index into the tag data element in the profiles. It
// shall consist of a 4-byte entry that contains a count of the number of tags in the table followed by a series of 12-
// byte entries with one entry for each tag. The tag table therefore contains 4+12n bytes where n is the number of
// tags contained in the profile. The entries for the tags within the table are not required to be in any particular
// order nor are they required to match the sequence of tag data element within the profile.
// Each 12-byte tag entry following the tag count shall consist of a 4-byte tag signature, a 4-byte offset to define
// the beginning of the tag data element, and a 4-byte entry identifying the length of the tag data element in bytes.
// [...]
// The tag table shall define a contiguous sequence of unique tag elements, with no gaps between the last byte
// of any tag data element referenced from the tag table (inclusive of any necessary additional pad bytes required
// to reach a four-byte boundary) and the byte offset of the following tag element, or the end of the file.
// Duplicate tag signatures shall not be included in the tag table.
// Tag data elements shall not partially overlap, so there shall be no part of any tag data element that falls within
2023-01-23 19:32:06 -05:00
// the range defined for another tag in the tag table."
2023-01-20 14:37:25 -05:00
ReadonlyBytes tag_table_bytes = bytes . slice ( sizeof ( ICCHeader ) ) ;
if ( tag_table_bytes . size ( ) < sizeof ( u32 ) )
return Error : : from_string_literal ( " ICC::Profile: Not enough data for tag count " ) ;
auto tag_count = * bit_cast < BigEndian < u32 > const * > ( tag_table_bytes . data ( ) ) ;
2023-01-23 18:23:04 -05:00
// 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 ( sizeof ( TagTableEntry ) = = 12 ) ;
2023-01-20 14:37:25 -05:00
tag_table_bytes = tag_table_bytes . slice ( sizeof ( u32 ) ) ;
2023-01-23 18:23:04 -05:00
if ( tag_table_bytes . size ( ) < tag_count * sizeof ( TagTableEntry ) )
2023-01-20 14:37:25 -05:00
return Error : : from_string_literal ( " ICC::Profile: Not enough data for tag table entries " ) ;
2023-01-23 18:23:04 -05:00
auto tag_table_entries = bit_cast < TagTableEntry const * > ( tag_table_bytes . data ( ) ) ;
2023-01-20 14:37:25 -05:00
2023-01-23 19:32:06 -05:00
// "The tag table may contain multiple tags signatures that all reference the same tag data element offset, allowing
// efficient reuse of tag data elements."
HashMap < u32 , NonnullRefPtr < TagData > > offset_to_tag_data ;
2023-01-20 14:37:25 -05:00
for ( u32 i = 0 ; i < tag_count ; + + i ) {
// FIXME: optionally ignore tags with unknown signature
2023-01-23 19:32:06 -05:00
// Dedupe identical offset/sizes.
NonnullRefPtr < TagData > tag_data = TRY ( offset_to_tag_data . try_ensure ( tag_table_entries [ i ] . offset_to_beginning_of_tag_data_element , [ = , this ] ( ) {
return read_tag ( bytes , tag_table_entries [ i ] . offset_to_beginning_of_tag_data_element , tag_table_entries [ i ] . size_of_tag_data_element ) ;
} ) ) ;
// "In such cases, both the offset and size of the tag data elements in the tag table shall be the same."
if ( tag_data - > size ( ) ! = tag_table_entries [ i ] . size_of_tag_data_element )
return Error : : from_string_literal ( " ICC::Profile: two tags have same offset but different sizes " ) ;
2023-01-20 14:37:25 -05:00
// "Duplicate tag signatures shall not be included in the tag table."
if ( TRY ( m_tag_table . try_set ( tag_table_entries [ i ] . tag_signature , move ( tag_data ) ) ) ! = AK : : HashSetResult : : InsertedNewEntry )
return Error : : from_string_literal ( " ICC::Profile: duplicate tag signature " ) ;
}
return { } ;
}
2023-01-25 07:15:24 -05:00
static bool is_xCLR ( ColorSpace color_space )
{
switch ( color_space ) {
case ColorSpace : : TwoColor :
case ColorSpace : : ThreeColor :
case ColorSpace : : FourColor :
case ColorSpace : : FiveColor :
case ColorSpace : : SixColor :
case ColorSpace : : SevenColor :
case ColorSpace : : EightColor :
case ColorSpace : : NineColor :
case ColorSpace : : TenColor :
case ColorSpace : : ElevenColor :
case ColorSpace : : TwelveColor :
case ColorSpace : : ThirteenColor :
case ColorSpace : : FourteenColor :
case ColorSpace : : FifteenColor :
return true ;
default :
return false ;
}
}
ErrorOr < void > Profile : : check_required_tags ( )
{
// ICC v4, 8 Required tags
// ICC v4, 8.2 Common requirements
// "With the exception of DeviceLink profiles, all profiles shall contain the following tags:
// - profileDescriptionTag (see 9.2.41);
// - copyrightTag (see 9.2.21);
// - mediaWhitePointTag (see 9.2.34);
// - chromaticAdaptationTag, when the measurement data used to calculate the profile was specified for an
// adopted white with a chromaticity different from that of the PCS adopted white (see 9.2.15).
// NOTE A DeviceLink profile is not required to have either a mediaWhitePointTag or a chromaticAdaptationTag."
// profileDescriptionTag, copyrightTag are required for DeviceLink too (see ICC v4, 8.6 DeviceLink profile).
// profileDescriptionTag, copyrightTag, mediaWhitePointTag are required in ICC v2 as well.
// chromaticAdaptationTag isn't required in v2 profiles as far as I can tell.
if ( ! m_tag_table . contains ( profileDescriptionTag ) )
return Error : : from_string_literal ( " ICC::Profile: required profileDescriptionTag is missing " ) ;
if ( ! m_tag_table . contains ( copyrightTag ) )
return Error : : from_string_literal ( " ICC::Profile: required copyrightTag is missing " ) ;
if ( device_class ( ) ! = DeviceClass : : DeviceLink ) {
if ( ! m_tag_table . contains ( mediaWhitePointTag ) )
return Error : : from_string_literal ( " ICC::Profile: required mediaWhitePointTag is missing " ) ;
// FIXME: Check for chromaticAdaptationTag after figuring out when exactly it needs to be present.
}
auto has_all_tags = [ & ] < class T > ( T tags ) { return all_of ( tags , [ & ] ( auto & key ) { return m_tag_table . contains ( key ) ; } ) ; } ;
switch ( device_class ( ) ) {
case DeviceClass : : InputDevce : {
// ICC v4, 8.3 Input profiles
// "8.3.1 General
// Input profiles are generally used with devices such as scanners and digital cameras. The types of profiles
// available for use as Input profiles are N-component LUT-based, Three-component matrix-based, and
// monochrome.
// 8.3.2 N-component LUT-based Input profiles
// In addition to the tags listed in 8.2 an N-component LUT-based Input profile shall contain the following tag:
// - AToB0Tag (see 9.2.1).
// 8.3.3 Three-component matrix-based Input profiles
// In addition to the tags listed in 8.2, a three-component matrix-based Input profile shall contain the following tags:
// - redMatrixColumnTag (see 9.2.44);
// - greenMatrixColumnTag (see 9.2.30);
// - blueMatrixColumnTag (see 9.2.4);
// - redTRCTag (see 9.2.45);
// - greenTRCTag (see 9.2.31);
// - blueTRCTag (see 9.2.5).
// [...] Only the PCSXYZ encoding can be used with matrix/TRC models.
// 8.3.4 Monochrome Input profiles
// In addition to the tags listed in 8.2, a monochrome Input profile shall contain the following tag:
// - grayTRCTag (see 9.2.29).
bool has_n_component_lut_based_tags = m_tag_table . contains ( AToB0Tag ) ;
bool has_three_component_matrix_based_tags = has_all_tags ( Array { redMatrixColumnTag , greenMatrixColumnTag , blueMatrixColumnTag , redTRCTag , greenTRCTag , blueTRCTag } ) ;
bool has_monochrome_tags = m_tag_table . contains ( grayTRCTag ) ;
if ( ! has_n_component_lut_based_tags & & ! has_three_component_matrix_based_tags & & ! has_monochrome_tags )
return Error : : from_string_literal ( " ICC::Profile: InputDevce required tags are missing " ) ;
if ( ! has_n_component_lut_based_tags & & has_three_component_matrix_based_tags & & connection_space ( ) ! = ColorSpace : : PCSXYZ )
return Error : : from_string_literal ( " ICC::Profile: InputDevce three-component matrix-based profile must use PCSXYZ " ) ;
break ;
}
case DeviceClass : : DisplayDevice : {
// ICC v4, 8.4 Display profiles
// "8.4.1 General
// This class of profiles represents display devices such as monitors. The types of profiles available for use as
// Display profiles are N-component LUT-based, Three-component matrix-based, and monochrome.
// 8.4.2 N-Component LUT-based Display profiles
// In addition to the tags listed in 8.2 an N-component LUT-based Input profile shall contain the following tags:
// - AToB0Tag (see 9.2.1);
// - BToA0Tag (see 9.2.6).
// 8.4.3 Three-component matrix-based Display profiles
// In addition to the tags listed in 8.2, a three-component matrix-based Display profile shall contain the following
// tags:
// - redMatrixColumnTag (see 9.2.44);
// - greenMatrixColumnTag (see 9.2.30);
// - blueMatrixColumnTag (see 9.2.4);
// - redTRCTag (see 9.2.45);
// - greenTRCTag (see 9.2.31);
// - blueTRCTag (see 9.2.5).
// [...] Only the PCSXYZ encoding can be used with matrix/TRC models.
// 8.4.4 Monochrome Display profiles
// In addition to the tags listed in 8.2 a monochrome Display profile shall contain the following tag:
// - grayTRCTag (see 9.2.29)."
bool has_n_component_lut_based_tags = has_all_tags ( Array { AToB0Tag , BToA0Tag } ) ;
bool has_three_component_matrix_based_tags = has_all_tags ( Array { redMatrixColumnTag , greenMatrixColumnTag , blueMatrixColumnTag , redTRCTag , greenTRCTag , blueTRCTag } ) ;
bool has_monochrome_tags = m_tag_table . contains ( grayTRCTag ) ;
if ( ! has_n_component_lut_based_tags & & ! has_three_component_matrix_based_tags & & ! has_monochrome_tags )
return Error : : from_string_literal ( " ICC::Profile: DisplayDevice required tags are missing " ) ;
if ( ! has_n_component_lut_based_tags & & has_three_component_matrix_based_tags & & connection_space ( ) ! = ColorSpace : : PCSXYZ )
return Error : : from_string_literal ( " ICC::Profile: DisplayDevice three-component matrix-based profile must use PCSXYZ " ) ;
break ;
}
case DeviceClass : : OutputDevice : {
// ICC v4, 8.5 Output profiles
// "8.5.1 General
// Output profiles are used to support devices such as printers and film recorders. The types of profiles available
// for use as Output profiles are N-component LUT-based and Monochrome.
// 8.5.2 N-component LUT-based Output profiles
// In addition to the tags listed in 8.2 an N-component LUT-based Output profile shall contain the following tags:
// - AToB0Tag (see 9.2.1);
// - AToB1Tag (see 9.2.2);
// - AToB2Tag (see 9.2.3);
// - BToA0Tag (see 9.2.6);
// - BToA1Tag (see 9.2.7);
// - BToA2Tag (see 9.2.8);
// - gamutTag (see 9.2.28);
// - colorantTableTag (see 9.2.18), for the xCLR colour spaces (see 7.2.6)
// 8.5.3 Monochrome Output profiles
// In addition to the tags listed in 8.2 a monochrome Output profile shall contain the following tag:
// - grayTRCTag (see 9.2.29)."
// The colorantTableTag requirement is new in v4.
Vector < TagSignature , 8 > required_n_component_lut_based_tags = { AToB0Tag , AToB1Tag , AToB2Tag , BToA0Tag , BToA1Tag , BToA2Tag , gamutTag } ;
if ( is_v4 ( ) & & is_xCLR ( connection_space ( ) ) )
required_n_component_lut_based_tags . append ( colorantTableTag ) ;
bool has_n_component_lut_based_tags = has_all_tags ( required_n_component_lut_based_tags ) ;
bool has_monochrome_tags = m_tag_table . contains ( grayTRCTag ) ;
if ( ! has_n_component_lut_based_tags & & ! has_monochrome_tags )
return Error : : from_string_literal ( " ICC::Profile: OutputDevice required tags are missing " ) ;
break ;
}
case DeviceClass : : DeviceLink : {
// ICC v4, 8.6 DeviceLink profile
// "A DeviceLink profile shall contain the following tags:
// - profileDescriptionTag (see 9.2.41);
// - copyrightTag (see 9.2.21);
// - profileSequenceDescTag (see 9.2.42);
// - AToB0Tag (see 9.2.1);
// - colorantTableTag (see 9.2.18) which is required only if the data colour space field is xCLR, where x is
// hexadecimal 2 to F (see 7.2.6);
// - colorantTableOutTag (see 9.2.19), required only if the PCS field is xCLR, where x is hexadecimal 2 to F
// (see 7.2.6)"
// profileDescriptionTag and copyrightTag are already checked above, in the code for section 8.2.
Vector < TagSignature , 4 > required_tags = { profileSequenceDescTag , AToB0Tag } ;
if ( is_v4 ( ) & & is_xCLR ( connection_space ( ) ) ) { // This requirement is new in v4.
required_tags . append ( colorantTableTag ) ;
required_tags . append ( colorantTableOutTag ) ;
}
if ( ! has_all_tags ( required_tags ) )
return Error : : from_string_literal ( " ICC::Profile: DeviceLink required tags are missing " ) ;
// "The data colour space field (see 7.2.6) in the DeviceLink profile will be the same as the data colour space field
// of the first profile in the sequence used to construct the device link. The PCS field (see 7.2.7) will be the same
// as the data colour space field of the last profile in the sequence."
// FIXME: Check that if profileSequenceDescType parsing is implemented.
break ;
}
case DeviceClass : : ColorSpace :
// ICC v4, 8.7 ColorSpace profile
// "In addition to the tags listed in 8.2, a ColorSpace profile shall contain the following tags:
// - BToA0Tag (see 9.2.6);
// - AToB0Tag (see 9.2.1).
// [...] ColorSpace profiles may be embedded in images."
if ( ! has_all_tags ( Array { AToB0Tag , BToA0Tag } ) )
return Error : : from_string_literal ( " ICC::Profile: ColorSpace required tags are missing " ) ;
break ;
case DeviceClass : : Abstract :
// ICC v4, 8.8 Abstract profile
// "In addition to the tags listed in 8.2, an Abstract profile shall contain the following tag:
// - AToB0Tag (see 9.2.1).
// [...] Abstract profiles cannot be embedded in images."
if ( ! m_tag_table . contains ( AToB0Tag ) )
return Error : : from_string_literal ( " ICC::Profile: Abstract required AToB0Tag is missing " ) ;
break ;
case DeviceClass : : NamedColor :
// ICC v4, 8.9 NamedColor profile
// "In addition to the tags listed in 8.2, a NamedColor profile shall contain the following tag:
// - namedColor2Tag (see 9.2.35)."
if ( ! m_tag_table . contains ( namedColor2Tag ) )
return Error : : from_string_literal ( " ICC::Profile: NamedColor required namedColor2Tag is missing " ) ;
break ;
}
return { } ;
}
2023-01-18 22:28:10 -05:00
ErrorOr < NonnullRefPtr < Profile > > Profile : : try_load_from_externally_owned_memory ( ReadonlyBytes bytes )
{
auto profile = adopt_ref ( * new Profile ( ) ) ;
TRY ( profile - > read_header ( bytes ) ) ;
bytes = bytes . trim ( profile - > on_disk_size ( ) ) ;
2023-01-20 14:37:25 -05:00
TRY ( profile - > read_tag_table ( bytes ) ) ;
2023-01-08 07:07:21 -05:00
2023-01-25 07:15:24 -05:00
TRY ( profile - > check_required_tags ( ) ) ;
2022-12-26 08:21:26 -05:00
return profile ;
}
2023-01-04 15:34:25 -05:00
Crypto : : Hash : : MD5 : : DigestType Profile : : compute_id ( ReadonlyBytes bytes )
{
// ICC v4, 7.2.18 Profile ID field
2023-01-05 15:10:30 -05:00
// "The Profile ID shall be calculated using the MD5 fingerprinting method as defined in Internet RFC 1321.
// The entire profile, whose length is given by the size field in the header, with the
// profile flags field (bytes 44 to 47, see 7.2.11),
// rendering intent field (bytes 64 to 67, see 7.2.15),
// and profile ID field (bytes 84 to 99)
// in the profile header temporarily set to zeros (00h),
// shall be used to calculate the ID."
2023-01-04 15:34:25 -05:00
const u8 zero [ 16 ] = { } ;
Crypto : : Hash : : MD5 md5 ;
md5 . update ( bytes . slice ( 0 , 44 ) ) ;
md5 . update ( ReadonlyBytes { zero , 4 } ) ; // profile flags field
md5 . update ( bytes . slice ( 48 , 64 - 48 ) ) ;
md5 . update ( ReadonlyBytes { zero , 4 } ) ; // rendering intent field
md5 . update ( bytes . slice ( 68 , 84 - 68 ) ) ;
md5 . update ( ReadonlyBytes { zero , 16 } ) ; // profile ID field
md5 . update ( bytes . slice ( 100 ) ) ;
return md5 . digest ( ) ;
}
2022-12-26 08:21:26 -05:00
}