1 /* vim: set ts=8 sw=8 noexpandtab: */
3 // Copyright (C) 2009 Mozilla Foundation
4 // Copyright (C) 1998-2007 Marti Maria
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the Software
11 // is furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
18 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <string.h> //memset
30 /* It might be worth having a unified limit on content controlled
31 * allocation per profile. This would remove the need for many
32 * of the arbitrary limits that we used */
34 typedef uint32_t be32
;
35 typedef uint16_t be16
;
39 /* __builtin_bswap isn't available in older gccs
40 * so open code it for now */
41 static be32
cpu_to_be32(int32_t v
)
43 #ifdef IS_LITTLE_ENDIAN
44 return ((v
& 0xff) << 24) | ((v
& 0xff00) << 8) | ((v
& 0xff0000) >> 8) | ((v
& 0xff000000) >> 24);
45 //return __builtin_bswap32(v);
51 static uint32_t be32_to_cpu(be32 v
)
53 #ifdef IS_LITTLE_ENDIAN
54 return ((v
& 0xff) << 24) | ((v
& 0xff00) << 8) | ((v
& 0xff0000) >> 8) | ((v
& 0xff000000) >> 24);
55 //return __builtin_bswap32(v);
61 static uint16_t be16_to_cpu(be16 v
)
63 #ifdef IS_LITTLE_ENDIAN
64 return ((v
& 0xff) << 8) | ((v
& 0xff00) >> 8);
70 /* a wrapper around the memory that we are going to parse
71 * into a qcms_profile */
74 const unsigned char *buf
;
77 const char *invalid_reason
;
80 static void invalid_source(struct mem_source
*mem
, const char *reason
)
83 mem
->invalid_reason
= reason
;
86 static uint32_t read_u32(struct mem_source
*mem
, size_t offset
)
88 /* Subtract from mem->size instead of the more intuitive adding to offset.
89 * This avoids overflowing offset. The subtraction is safe because
90 * mem->size is guaranteed to be > 4 */
91 if (offset
> mem
->size
- 4) {
92 invalid_source(mem
, "Invalid offset");
96 memcpy(&k
, mem
->buf
+ offset
, sizeof(k
));
97 return be32_to_cpu(k
);
101 static uint16_t read_u16(struct mem_source
*mem
, size_t offset
)
103 if (offset
> mem
->size
- 2) {
104 invalid_source(mem
, "Invalid offset");
108 memcpy(&k
, mem
->buf
+ offset
, sizeof(k
));
109 return be16_to_cpu(k
);
113 static uint8_t read_u8(struct mem_source
*mem
, size_t offset
)
115 if (offset
> mem
->size
- 1) {
116 invalid_source(mem
, "Invalid offset");
119 return *(uint8_t*)(mem
->buf
+ offset
);
123 static s15Fixed16Number
read_s15Fixed16Number(struct mem_source
*mem
, size_t offset
)
125 return read_u32(mem
, offset
);
128 static uInt8Number
read_uInt8Number(struct mem_source
*mem
, size_t offset
)
130 return read_u8(mem
, offset
);
133 static uInt16Number
read_uInt16Number(struct mem_source
*mem
, size_t offset
)
135 return read_u16(mem
, offset
);
138 #define BAD_VALUE_PROFILE NULL
139 #define INVALID_PROFILE NULL
140 #define NO_MEM_PROFILE NULL
142 /* An arbitrary 4MB limit on profile size */
143 #define MAX_PROFILE_SIZE 1024*1024*4
144 #define MAX_TAG_COUNT 1024
146 static void check_CMM_type_signature(struct mem_source
*src
)
148 //uint32_t CMM_type_signature = read_u32(src, 4);
149 //TODO: do the check?
153 static void check_profile_version(struct mem_source
*src
)
157 uint8_t major_revision = read_u8(src, 8 + 0);
158 uint8_t minor_revision = read_u8(src, 8 + 1);
160 uint8_t reserved1
= read_u8(src
, 8 + 2);
161 uint8_t reserved2
= read_u8(src
, 8 + 3);
162 /* Checking the version doesn't buy us anything
163 if (major_revision != 0x4) {
164 if (major_revision > 0x2)
165 invalid_source(src, "Unsupported major revision");
166 if (minor_revision > 0x40)
167 invalid_source(src, "Unsupported minor revision");
170 if (reserved1
!= 0 || reserved2
!= 0)
171 invalid_source(src
, "Invalid reserved bytes");
174 #define INPUT_DEVICE_PROFILE 0x73636e72 // 'scnr'
175 #define DISPLAY_DEVICE_PROFILE 0x6d6e7472 // 'mntr'
176 #define OUTPUT_DEVICE_PROFILE 0x70727472 // 'prtr'
177 #define DEVICE_LINK_PROFILE 0x6c696e6b // 'link'
178 #define COLOR_SPACE_PROFILE 0x73706163 // 'spac'
179 #define ABSTRACT_PROFILE 0x61627374 // 'abst'
180 #define NAMED_COLOR_PROFILE 0x6e6d636c // 'nmcl'
182 static void read_class_signature(qcms_profile
*profile
, struct mem_source
*mem
)
184 profile
->class = read_u32(mem
, 12);
185 switch (profile
->class) {
186 case DISPLAY_DEVICE_PROFILE
:
187 case INPUT_DEVICE_PROFILE
:
188 case OUTPUT_DEVICE_PROFILE
:
189 case COLOR_SPACE_PROFILE
:
192 invalid_source(mem
, "Invalid Profile/Device Class signature");
196 static void read_color_space(qcms_profile
*profile
, struct mem_source
*mem
)
198 profile
->color_space
= read_u32(mem
, 16);
199 switch (profile
->color_space
) {
204 invalid_source(mem
, "Unsupported colorspace");
208 static void read_pcs(qcms_profile
*profile
, struct mem_source
*mem
)
210 profile
->pcs
= read_u32(mem
, 20);
211 switch (profile
->pcs
) {
216 invalid_source(mem
, "Unsupported pcs");
232 static struct tag_index
read_tag_table(qcms_profile
*profile
, struct mem_source
*mem
)
234 struct tag_index index
= {0, NULL
};
237 index
.count
= read_u32(mem
, 128);
238 if (index
.count
> MAX_TAG_COUNT
) {
239 invalid_source(mem
, "max number of tags exceeded");
243 index
.tags
= malloc(sizeof(struct tag
)*index
.count
);
245 for (i
= 0; i
< index
.count
; i
++) {
246 index
.tags
[i
].signature
= read_u32(mem
, 128 + 4 + 4*i
*3);
247 index
.tags
[i
].offset
= read_u32(mem
, 128 + 4 + 4*i
*3 + 4);
248 index
.tags
[i
].size
= read_u32(mem
, 128 + 4 + 4*i
*3 + 8);
255 // Checks a profile for obvious inconsistencies and returns
256 // true if the profile looks bogus and should probably be
258 qcms_bool
qcms_profile_is_bogus(qcms_profile
*profile
)
260 float sum
[3], target
[3], tolerance
[3];
261 float rX
, rY
, rZ
, gX
, gY
, gZ
, bX
, bY
, bZ
;
265 // We currently only check the bogosity of RGB profiles
266 if (profile
->color_space
!= RGB_SIGNATURE
)
269 if (profile
->A2B0
|| profile
->B2A0
)
272 rX
= s15Fixed16Number_to_float(profile
->redColorant
.X
);
273 rY
= s15Fixed16Number_to_float(profile
->redColorant
.Y
);
274 rZ
= s15Fixed16Number_to_float(profile
->redColorant
.Z
);
276 gX
= s15Fixed16Number_to_float(profile
->greenColorant
.X
);
277 gY
= s15Fixed16Number_to_float(profile
->greenColorant
.Y
);
278 gZ
= s15Fixed16Number_to_float(profile
->greenColorant
.Z
);
280 bX
= s15Fixed16Number_to_float(profile
->blueColorant
.X
);
281 bY
= s15Fixed16Number_to_float(profile
->blueColorant
.Y
);
282 bZ
= s15Fixed16Number_to_float(profile
->blueColorant
.Z
);
284 // Check if any of the XYZ values are negative (see mozilla bug 498245)
285 // CIEXYZ tristimulus values cannot be negative according to the spec.
287 (rX
< 0) || (rY
< 0) || (rZ
< 0) ||
288 (gX
< 0) || (gY
< 0) || (gZ
< 0) ||
289 (bX
< 0) || (bY
< 0) || (bZ
< 0);
295 // Sum the values; they should add up to something close to white
296 sum
[0] = rX
+ gX
+ bX
;
297 sum
[1] = rY
+ gY
+ bY
;
298 sum
[2] = rZ
+ gZ
+ bZ
;
300 // Build our target vector (see mozilla bug 460629)
305 // Our tolerance vector - Recommended by Chris Murphy based on
306 // conversion from the LAB space criterion of no more than 3 in any one
307 // channel. This is similar to, but slightly more tolerant than Adobe's
313 // Compare with our tolerance
314 for (i
= 0; i
< 3; ++i
) {
315 if (!(((sum
[i
] - tolerance
[i
]) <= target
[i
]) &&
316 ((sum
[i
] + tolerance
[i
]) >= target
[i
])))
324 #define TAG_bXYZ 0x6258595a
325 #define TAG_gXYZ 0x6758595a
326 #define TAG_rXYZ 0x7258595a
327 #define TAG_rTRC 0x72545243
328 #define TAG_bTRC 0x62545243
329 #define TAG_gTRC 0x67545243
330 #define TAG_kTRC 0x6b545243
331 #define TAG_A2B0 0x41324230
332 #define TAG_B2A0 0x42324130
333 #define TAG_CHAD 0x63686164
335 static struct tag
*find_tag(struct tag_index index
, uint32_t tag_id
)
338 struct tag
*tag
= NULL
;
339 for (i
= 0; i
< index
.count
; i
++) {
340 if (index
.tags
[i
].signature
== tag_id
) {
341 return &index
.tags
[i
];
347 #define XYZ_TYPE 0x58595a20 // 'XYZ '
348 #define CURVE_TYPE 0x63757276 // 'curv'
349 #define PARAMETRIC_CURVE_TYPE 0x70617261 // 'para'
350 #define LUT16_TYPE 0x6d667432 // 'mft2'
351 #define LUT8_TYPE 0x6d667431 // 'mft1'
352 #define LUT_MAB_TYPE 0x6d414220 // 'mAB '
353 #define LUT_MBA_TYPE 0x6d424120 // 'mBA '
354 #define CHROMATIC_TYPE 0x73663332 // 'sf32'
356 static struct matrix
read_tag_s15Fixed16ArrayType(struct mem_source
*src
, struct tag_index index
, uint32_t tag_id
)
358 struct tag
*tag
= find_tag(index
, tag_id
);
359 struct matrix matrix
;
362 uint32_t offset
= tag
->offset
;
363 uint32_t type
= read_u32(src
, offset
);
365 // Check mandatory type signature for s16Fixed16ArrayType
366 if (type
!= CHROMATIC_TYPE
) {
367 invalid_source(src
, "unexpected type, expected 'sf32'");
370 for (i
= 0; i
< 9; i
++) {
371 matrix
.m
[i
/3][i
%3] = s15Fixed16Number_to_float(read_s15Fixed16Number(src
, offset
+8+i
*4));
373 matrix
.invalid
= false;
375 matrix
.invalid
= true;
376 invalid_source(src
, "missing sf32tag");
381 static struct XYZNumber
read_tag_XYZType(struct mem_source
*src
, struct tag_index index
, uint32_t tag_id
)
383 struct XYZNumber num
= {0, 0, 0};
384 struct tag
*tag
= find_tag(index
, tag_id
);
386 uint32_t offset
= tag
->offset
;
388 uint32_t type
= read_u32(src
, offset
);
389 if (type
!= XYZ_TYPE
)
390 invalid_source(src
, "unexpected type, expected XYZ");
391 num
.X
= read_s15Fixed16Number(src
, offset
+8);
392 num
.Y
= read_s15Fixed16Number(src
, offset
+12);
393 num
.Z
= read_s15Fixed16Number(src
, offset
+16);
395 invalid_source(src
, "missing xyztag");
400 // Read the tag at a given offset rather then the tag_index.
401 // This method is used when reading mAB tags where nested curveType are
402 // present that are not part of the tag_index.
403 static struct curveType
*read_curveType(struct mem_source
*src
, uint32_t offset
, uint32_t *len
)
405 static const uint32_t COUNT_TO_LENGTH
[5] = {1, 3, 4, 5, 7};
406 struct curveType
*curve
= NULL
;
407 uint32_t type
= read_u32(src
, offset
);
411 if (type
!= CURVE_TYPE
&& type
!= PARAMETRIC_CURVE_TYPE
) {
412 invalid_source(src
, "unexpected type, expected CURV or PARA");
416 if (type
== CURVE_TYPE
) {
417 count
= read_u32(src
, offset
+8);
419 #define MAX_CURVE_ENTRIES 40000 //arbitrary
420 if (count
> MAX_CURVE_ENTRIES
) {
421 invalid_source(src
, "curve size too large");
424 curve
= malloc(sizeof(struct curveType
) + sizeof(uInt16Number
)*count
);
428 curve
->count
= count
;
429 curve
->type
= CURVE_TYPE
;
431 for (i
=0; i
<count
; i
++) {
432 curve
->data
[i
] = read_u16(src
, offset
+ 12 + i
*2);
434 *len
= 12 + count
* 2;
435 } else { //PARAMETRIC_CURVE_TYPE
436 count
= read_u16(src
, offset
+8);
439 invalid_source(src
, "parametric function type not supported.");
443 curve
= malloc(sizeof(struct curveType
));
447 curve
->count
= count
;
448 curve
->type
= PARAMETRIC_CURVE_TYPE
;
450 for (i
=0; i
< COUNT_TO_LENGTH
[count
]; i
++) {
451 curve
->parameter
[i
] = s15Fixed16Number_to_float(read_s15Fixed16Number(src
, offset
+ 12 + i
*4));
453 *len
= 12 + COUNT_TO_LENGTH
[count
] * 4;
455 if ((count
== 1 || count
== 2)) {
456 /* we have a type 1 or type 2 function that has a division by 'a' */
457 float a
= curve
->parameter
[1];
459 invalid_source(src
, "parametricCurve definition causes division by zero.");
466 static struct curveType
*read_tag_curveType(struct mem_source
*src
, struct tag_index index
, uint32_t tag_id
)
468 struct tag
*tag
= find_tag(index
, tag_id
);
469 struct curveType
*curve
= NULL
;
472 return read_curveType(src
, tag
->offset
, &len
);
474 invalid_source(src
, "missing curvetag");
480 #define MAX_CLUT_SIZE 500000 // arbitrary
481 #define MAX_CHANNELS 10 // arbitrary
482 static void read_nested_curveType(struct mem_source
*src
, struct curveType
*(*curveArray
)[MAX_CHANNELS
], uint8_t num_channels
, uint32_t curve_offset
)
484 uint32_t channel_offset
= 0;
486 for (i
= 0; i
< num_channels
; i
++) {
489 (*curveArray
)[i
] = read_curveType(src
, curve_offset
+ channel_offset
, &tag_len
);
490 if (!(*curveArray
)[i
]) {
491 invalid_source(src
, "invalid nested curveType curve");
494 channel_offset
+= tag_len
;
496 if ((tag_len
% 4) != 0)
497 channel_offset
+= 4 - (tag_len
% 4);
502 static void mAB_release(struct lutmABType
*lut
)
506 for (i
= 0; i
< lut
->num_in_channels
; i
++){
507 free(lut
->a_curves
[i
]);
509 for (i
= 0; i
< lut
->num_out_channels
; i
++){
510 free(lut
->b_curves
[i
]);
511 free(lut
->m_curves
[i
]);
516 /* See section 10.10 for specs */
517 static struct lutmABType
*read_tag_lutmABType(struct mem_source
*src
, struct tag_index index
, uint32_t tag_id
)
519 struct tag
*tag
= find_tag(index
, tag_id
);
520 uint32_t offset
= tag
->offset
;
521 uint32_t a_curve_offset
, b_curve_offset
, m_curve_offset
;
522 uint32_t matrix_offset
;
523 uint32_t clut_offset
;
524 uint32_t clut_size
= 1;
525 uint8_t clut_precision
;
526 uint32_t type
= read_u32(src
, offset
);
527 uint8_t num_in_channels
, num_out_channels
;
528 struct lutmABType
*lut
;
531 if (type
!= LUT_MAB_TYPE
&& type
!= LUT_MBA_TYPE
) {
535 num_in_channels
= read_u8(src
, offset
+ 8);
536 num_out_channels
= read_u8(src
, offset
+ 8);
537 if (num_in_channels
> MAX_CHANNELS
|| num_out_channels
> MAX_CHANNELS
)
540 // We require 3in/out channels since we only support RGB->XYZ (or RGB->LAB)
541 // XXX: If we remove this restriction make sure that the number of channels
542 // is less or equal to the maximum number of mAB curves in qcmsint.h
543 // also check for clut_size overflow.
544 if (num_in_channels
!= 3 || num_out_channels
!= 3)
547 // some of this data is optional and is denoted by a zero offset
548 // we also use this to track their existance
549 a_curve_offset
= read_u32(src
, offset
+ 28);
550 clut_offset
= read_u32(src
, offset
+ 24);
551 m_curve_offset
= read_u32(src
, offset
+ 20);
552 matrix_offset
= read_u32(src
, offset
+ 16);
553 b_curve_offset
= read_u32(src
, offset
+ 12);
555 // Convert offsets relative to the tag to relative to the profile
556 // preserve zero for optional fields
558 a_curve_offset
+= offset
;
560 clut_offset
+= offset
;
562 m_curve_offset
+= offset
;
564 matrix_offset
+= offset
;
566 b_curve_offset
+= offset
;
569 assert (num_in_channels
== 3);
570 // clut_size can not overflow since lg(256^num_in_channels) = 24 bits.
571 for (i
= 0; i
< num_in_channels
; i
++) {
572 clut_size
*= read_u8(src
, clut_offset
+ i
);
578 // 24bits * 3 won't overflow either
579 clut_size
= clut_size
* num_out_channels
;
581 if (clut_size
> MAX_CLUT_SIZE
)
584 lut
= malloc(sizeof(struct lutmABType
) + (clut_size
) * sizeof(float));
587 // we'll fill in the rest below
588 memset(lut
, 0, sizeof(struct lutmABType
));
589 lut
->clut_table
= &lut
->clut_table_data
[0];
591 for (i
= 0; i
< num_in_channels
; i
++) {
592 lut
->num_grid_points
[i
] = read_u8(src
, clut_offset
+ i
);
595 // Reverse the processing of transformation elements for mBA type.
596 lut
->reversed
= (type
== LUT_MBA_TYPE
);
598 lut
->num_in_channels
= num_in_channels
;
599 lut
->num_out_channels
= num_out_channels
;
602 // read the matrix if we have it
603 lut
->e00
= read_s15Fixed16Number(src
, matrix_offset
+4*0);
604 lut
->e01
= read_s15Fixed16Number(src
, matrix_offset
+4*1);
605 lut
->e02
= read_s15Fixed16Number(src
, matrix_offset
+4*2);
606 lut
->e10
= read_s15Fixed16Number(src
, matrix_offset
+4*3);
607 lut
->e11
= read_s15Fixed16Number(src
, matrix_offset
+4*4);
608 lut
->e12
= read_s15Fixed16Number(src
, matrix_offset
+4*5);
609 lut
->e20
= read_s15Fixed16Number(src
, matrix_offset
+4*6);
610 lut
->e21
= read_s15Fixed16Number(src
, matrix_offset
+4*7);
611 lut
->e22
= read_s15Fixed16Number(src
, matrix_offset
+4*8);
612 lut
->e03
= read_s15Fixed16Number(src
, matrix_offset
+4*9);
613 lut
->e13
= read_s15Fixed16Number(src
, matrix_offset
+4*10);
614 lut
->e23
= read_s15Fixed16Number(src
, matrix_offset
+4*11);
617 if (a_curve_offset
) {
618 read_nested_curveType(src
, &lut
->a_curves
, num_in_channels
, a_curve_offset
);
620 if (m_curve_offset
) {
621 read_nested_curveType(src
, &lut
->m_curves
, num_out_channels
, m_curve_offset
);
623 if (b_curve_offset
) {
624 read_nested_curveType(src
, &lut
->b_curves
, num_out_channels
, b_curve_offset
);
626 invalid_source(src
, "B curves required");
630 clut_precision
= read_u8(src
, clut_offset
+ 16);
631 if (clut_precision
== 1) {
632 for (i
= 0; i
< clut_size
; i
++) {
633 lut
->clut_table
[i
] = uInt8Number_to_float(read_uInt8Number(src
, clut_offset
+ 20 + i
*1));
635 } else if (clut_precision
== 2) {
636 for (i
= 0; i
< clut_size
; i
++) {
637 lut
->clut_table
[i
] = uInt16Number_to_float(read_uInt16Number(src
, clut_offset
+ 20 + i
*2));
640 invalid_source(src
, "Invalid clut precision");
652 static struct lutType
*read_tag_lutType(struct mem_source
*src
, struct tag_index index
, uint32_t tag_id
)
654 struct tag
*tag
= find_tag(index
, tag_id
);
655 uint32_t offset
= tag
->offset
;
656 uint32_t type
= read_u32(src
, offset
);
657 uint16_t num_input_table_entries
;
658 uint16_t num_output_table_entries
;
659 uint8_t in_chan
, grid_points
, out_chan
;
660 uint32_t clut_offset
, output_offset
;
666 /* I'm not sure why the spec specifies a fixed number of entries for LUT8 tables even though
667 * they have room for the num_entries fields */
668 if (type
== LUT8_TYPE
) {
669 num_input_table_entries
= 256;
670 num_output_table_entries
= 256;
672 } else if (type
== LUT16_TYPE
) {
673 num_input_table_entries
= read_u16(src
, offset
+ 48);
674 num_output_table_entries
= read_u16(src
, offset
+ 50);
677 assert(0); // the caller checks that this doesn't happen
678 invalid_source(src
, "Unexpected lut type");
682 in_chan
= read_u8(src
, offset
+ 8);
683 out_chan
= read_u8(src
, offset
+ 9);
684 grid_points
= read_u8(src
, offset
+ 10);
686 clut_size
= pow(grid_points
, in_chan
);
687 if (clut_size
> MAX_CLUT_SIZE
) {
691 if (in_chan
!= 3 || out_chan
!= 3) {
695 lut
= malloc(sizeof(struct lutType
) + (num_input_table_entries
* in_chan
+ clut_size
*out_chan
+ num_output_table_entries
* out_chan
)*sizeof(float));
700 /* compute the offsets of tables */
701 lut
->input_table
= &lut
->table_data
[0];
702 lut
->clut_table
= &lut
->table_data
[in_chan
*num_input_table_entries
];
703 lut
->output_table
= &lut
->table_data
[in_chan
*num_input_table_entries
+ clut_size
*out_chan
];
705 lut
->num_input_table_entries
= num_input_table_entries
;
706 lut
->num_output_table_entries
= num_output_table_entries
;
707 lut
->num_input_channels
= read_u8(src
, offset
+ 8);
708 lut
->num_output_channels
= read_u8(src
, offset
+ 9);
709 lut
->num_clut_grid_points
= read_u8(src
, offset
+ 10);
710 lut
->e00
= read_s15Fixed16Number(src
, offset
+12);
711 lut
->e01
= read_s15Fixed16Number(src
, offset
+16);
712 lut
->e02
= read_s15Fixed16Number(src
, offset
+20);
713 lut
->e10
= read_s15Fixed16Number(src
, offset
+24);
714 lut
->e11
= read_s15Fixed16Number(src
, offset
+28);
715 lut
->e12
= read_s15Fixed16Number(src
, offset
+32);
716 lut
->e20
= read_s15Fixed16Number(src
, offset
+36);
717 lut
->e21
= read_s15Fixed16Number(src
, offset
+40);
718 lut
->e22
= read_s15Fixed16Number(src
, offset
+44);
720 for (i
= 0; i
< lut
->num_input_table_entries
* in_chan
; i
++) {
721 if (type
== LUT8_TYPE
) {
722 lut
->input_table
[i
] = uInt8Number_to_float(read_uInt8Number(src
, offset
+ 52 + i
* entry_size
));
724 lut
->input_table
[i
] = uInt16Number_to_float(read_uInt16Number(src
, offset
+ 52 + i
* entry_size
));
728 clut_offset
= offset
+ 52 + lut
->num_input_table_entries
* in_chan
* entry_size
;
729 for (i
= 0; i
< clut_size
* out_chan
; i
+=3) {
730 if (type
== LUT8_TYPE
) {
731 lut
->clut_table
[i
+0] = uInt8Number_to_float(read_uInt8Number(src
, clut_offset
+ i
*entry_size
+ 0));
732 lut
->clut_table
[i
+1] = uInt8Number_to_float(read_uInt8Number(src
, clut_offset
+ i
*entry_size
+ 1));
733 lut
->clut_table
[i
+2] = uInt8Number_to_float(read_uInt8Number(src
, clut_offset
+ i
*entry_size
+ 2));
735 lut
->clut_table
[i
+0] = uInt16Number_to_float(read_uInt16Number(src
, clut_offset
+ i
*entry_size
+ 0));
736 lut
->clut_table
[i
+1] = uInt16Number_to_float(read_uInt16Number(src
, clut_offset
+ i
*entry_size
+ 2));
737 lut
->clut_table
[i
+2] = uInt16Number_to_float(read_uInt16Number(src
, clut_offset
+ i
*entry_size
+ 4));
741 output_offset
= clut_offset
+ clut_size
* out_chan
* entry_size
;
742 for (i
= 0; i
< lut
->num_output_table_entries
* out_chan
; i
++) {
743 if (type
== LUT8_TYPE
) {
744 lut
->output_table
[i
] = uInt8Number_to_float(read_uInt8Number(src
, output_offset
+ i
*entry_size
));
746 lut
->output_table
[i
] = uInt16Number_to_float(read_uInt16Number(src
, output_offset
+ i
*entry_size
));
753 static void read_rendering_intent(qcms_profile
*profile
, struct mem_source
*src
)
755 profile
->rendering_intent
= read_u32(src
, 64);
756 switch (profile
->rendering_intent
) {
757 case QCMS_INTENT_PERCEPTUAL
:
758 case QCMS_INTENT_SATURATION
:
759 case QCMS_INTENT_RELATIVE_COLORIMETRIC
:
760 case QCMS_INTENT_ABSOLUTE_COLORIMETRIC
:
763 invalid_source(src
, "unknown rendering intent");
767 qcms_profile
*qcms_profile_create(void)
769 return calloc(sizeof(qcms_profile
), 1);
774 /* build sRGB gamma table */
775 /* based on cmsBuildParametricGamma() */
776 static uint16_t *build_sRGB_gamma_table(int num_entries
)
779 /* taken from lcms: Build_sRGBGamma() */
782 double b
= 0.055/1.055;
786 uint16_t *table
= malloc(sizeof(uint16_t) * num_entries
);
790 for (i
=0; i
<num_entries
; i
++) {
791 double x
= (double)i
/ (num_entries
-1);
793 // IEC 61966-2.1 (sRGB)
794 // Y = (aX + b)^Gamma | X >= d
797 double e
= (a
*x
+ b
);
806 // Saturate -- this could likely move to a separate function
807 output
= y
* 65535. + .5;
812 table
[i
] = (uint16_t)floor(output
);
817 static struct curveType
*curve_from_table(uint16_t *table
, int num_entries
)
819 struct curveType
*curve
;
821 curve
= malloc(sizeof(struct curveType
) + sizeof(uInt16Number
)*num_entries
);
824 curve
->type
= CURVE_TYPE
;
825 curve
->count
= num_entries
;
826 for (i
= 0; i
< num_entries
; i
++) {
827 curve
->data
[i
] = table
[i
];
832 static uint16_t float_to_u8Fixed8Number(float a
)
834 if (a
> (255.f
+ 255.f
/256))
839 return floorf(a
*256.f
+ .5f
);
842 static struct curveType
*curve_from_gamma(float gamma
)
844 struct curveType
*curve
;
846 curve
= malloc(sizeof(struct curveType
) + sizeof(uInt16Number
)*num_entries
);
849 curve
->count
= num_entries
;
850 curve
->data
[0] = float_to_u8Fixed8Number(gamma
);
851 curve
->type
= CURVE_TYPE
;
856 //XXX: it would be nice if we had a way of ensuring
857 // everything in a profile was initialized regardless of how it was created
859 //XXX: should this also be taking a black_point?
860 /* similar to CGColorSpaceCreateCalibratedRGB */
861 qcms_profile
* qcms_profile_create_rgb_with_gamma(
862 qcms_CIE_xyY white_point
,
863 qcms_CIE_xyYTRIPLE primaries
,
866 qcms_profile
* profile
= qcms_profile_create();
868 return NO_MEM_PROFILE
;
870 //XXX: should store the whitepoint
871 if (!set_rgb_colorants(profile
, white_point
, primaries
)) {
872 qcms_profile_release(profile
);
873 return INVALID_PROFILE
;
876 profile
->redTRC
= curve_from_gamma(gamma
);
877 profile
->blueTRC
= curve_from_gamma(gamma
);
878 profile
->greenTRC
= curve_from_gamma(gamma
);
880 if (!profile
->redTRC
|| !profile
->blueTRC
|| !profile
->greenTRC
) {
881 qcms_profile_release(profile
);
882 return NO_MEM_PROFILE
;
884 profile
->class = DISPLAY_DEVICE_PROFILE
;
885 profile
->rendering_intent
= QCMS_INTENT_PERCEPTUAL
;
886 profile
->color_space
= RGB_SIGNATURE
;
890 qcms_profile
* qcms_profile_create_rgb_with_table(
891 qcms_CIE_xyY white_point
,
892 qcms_CIE_xyYTRIPLE primaries
,
893 uint16_t *table
, int num_entries
)
895 qcms_profile
* profile
= qcms_profile_create();
897 return NO_MEM_PROFILE
;
899 //XXX: should store the whitepoint
900 if (!set_rgb_colorants(profile
, white_point
, primaries
)) {
901 qcms_profile_release(profile
);
902 return INVALID_PROFILE
;
905 profile
->redTRC
= curve_from_table(table
, num_entries
);
906 profile
->blueTRC
= curve_from_table(table
, num_entries
);
907 profile
->greenTRC
= curve_from_table(table
, num_entries
);
909 if (!profile
->redTRC
|| !profile
->blueTRC
|| !profile
->greenTRC
) {
910 qcms_profile_release(profile
);
911 return NO_MEM_PROFILE
;
913 profile
->class = DISPLAY_DEVICE_PROFILE
;
914 profile
->rendering_intent
= QCMS_INTENT_PERCEPTUAL
;
915 profile
->color_space
= RGB_SIGNATURE
;
919 /* from lcms: cmsWhitePointFromTemp */
920 /* tempK must be >= 4000. and <= 25000.
921 * Invalid values of tempK will return
922 * (x,y,Y) = (-1.0, -1.0, -1.0)
923 * similar to argyll: icx_DTEMP2XYZ() */
924 static qcms_CIE_xyY
white_point_from_temp(int temp_K
)
926 qcms_CIE_xyY white_point
;
931 // No optimization provided.
936 // For correlated color temperature (T) between 4000K and 7000K:
937 if (T
>= 4000. && T
<= 7000.) {
938 x
= -4.6070*(1E9
/T3
) + 2.9678*(1E6
/T2
) + 0.09911*(1E3
/T
) + 0.244063;
940 // or for correlated color temperature (T) between 7000K and 25000K:
941 if (T
> 7000.0 && T
<= 25000.0) {
942 x
= -2.0064*(1E9
/T3
) + 1.9018*(1E6
/T2
) + 0.24748*(1E3
/T
) + 0.237040;
945 white_point
.x
= -1.0;
946 white_point
.y
= -1.0;
947 white_point
.Y
= -1.0;
949 assert(0 && "invalid temp");
957 y
= -3.000*(x
*x
) + 2.870*x
- 0.275;
959 // wave factors (not used, but here for futures extensions)
961 // M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y);
962 // M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y);
964 // Fill white_point struct
972 qcms_profile
* qcms_profile_sRGB(void)
974 qcms_profile
*profile
;
977 qcms_CIE_xyYTRIPLE Rec709Primaries
= {
978 {0.6400, 0.3300, 1.0},
979 {0.3000, 0.6000, 1.0},
980 {0.1500, 0.0600, 1.0}
984 D65
= white_point_from_temp(6504);
986 table
= build_sRGB_gamma_table(1024);
989 return NO_MEM_PROFILE
;
991 profile
= qcms_profile_create_rgb_with_table(D65
, Rec709Primaries
, table
, 1024);
997 /* qcms_profile_from_memory does not hold a reference to the memory passed in */
998 qcms_profile
* qcms_profile_from_memory(const void *mem
, size_t size
)
1001 struct mem_source source
;
1002 struct mem_source
*src
= &source
;
1003 struct tag_index index
;
1004 qcms_profile
*profile
;
1008 source
.valid
= true;
1010 length
= read_u32(src
, 0);
1011 if (length
<= size
) {
1012 // shrink the area that we can read if appropriate
1013 source
.size
= length
;
1015 return INVALID_PROFILE
;
1018 /* ensure that the profile size is sane so it's easier to reason about */
1019 if (source
.size
<= 64 || source
.size
>= MAX_PROFILE_SIZE
)
1020 return INVALID_PROFILE
;
1022 profile
= qcms_profile_create();
1024 return NO_MEM_PROFILE
;
1026 check_CMM_type_signature(src
);
1027 check_profile_version(src
);
1028 read_class_signature(profile
, src
);
1029 read_rendering_intent(profile
, src
);
1030 read_color_space(profile
, src
);
1031 read_pcs(profile
, src
);
1032 //TODO read rest of profile stuff
1035 goto invalid_profile
;
1037 index
= read_tag_table(profile
, src
);
1038 if (!src
->valid
|| !index
.tags
)
1039 goto invalid_tag_table
;
1041 if (find_tag(index
, TAG_CHAD
)) {
1042 profile
->chromaticAdaption
= read_tag_s15Fixed16ArrayType(src
, index
, TAG_CHAD
);
1044 profile
->chromaticAdaption
.invalid
= true; //Signal the data is not present
1047 if (profile
->class == DISPLAY_DEVICE_PROFILE
|| profile
->class == INPUT_DEVICE_PROFILE
||
1048 profile
->class == OUTPUT_DEVICE_PROFILE
|| profile
->class == COLOR_SPACE_PROFILE
) {
1049 if (profile
->color_space
== RGB_SIGNATURE
) {
1050 if (find_tag(index
, TAG_A2B0
)) {
1051 if (read_u32(src
, find_tag(index
, TAG_A2B0
)->offset
) == LUT8_TYPE
||
1052 read_u32(src
, find_tag(index
, TAG_A2B0
)->offset
) == LUT16_TYPE
) {
1053 profile
->A2B0
= read_tag_lutType(src
, index
, TAG_A2B0
);
1054 } else if (read_u32(src
, find_tag(index
, TAG_A2B0
)->offset
) == LUT_MAB_TYPE
) {
1055 profile
->mAB
= read_tag_lutmABType(src
, index
, TAG_A2B0
);
1058 if (find_tag(index
, TAG_B2A0
)) {
1059 if (read_u32(src
, find_tag(index
, TAG_B2A0
)->offset
) == LUT8_TYPE
||
1060 read_u32(src
, find_tag(index
, TAG_B2A0
)->offset
) == LUT16_TYPE
) {
1061 profile
->B2A0
= read_tag_lutType(src
, index
, TAG_B2A0
);
1062 } else if (read_u32(src
, find_tag(index
, TAG_B2A0
)->offset
) == LUT_MBA_TYPE
) {
1063 profile
->mBA
= read_tag_lutmABType(src
, index
, TAG_B2A0
);
1066 if (find_tag(index
, TAG_rXYZ
) || !qcms_supports_iccv4
) {
1067 profile
->redColorant
= read_tag_XYZType(src
, index
, TAG_rXYZ
);
1068 profile
->greenColorant
= read_tag_XYZType(src
, index
, TAG_gXYZ
);
1069 profile
->blueColorant
= read_tag_XYZType(src
, index
, TAG_bXYZ
);
1073 goto invalid_tag_table
;
1075 if (find_tag(index
, TAG_rTRC
) || !qcms_supports_iccv4
) {
1076 profile
->redTRC
= read_tag_curveType(src
, index
, TAG_rTRC
);
1077 profile
->greenTRC
= read_tag_curveType(src
, index
, TAG_gTRC
);
1078 profile
->blueTRC
= read_tag_curveType(src
, index
, TAG_bTRC
);
1080 if (!profile
->redTRC
|| !profile
->blueTRC
|| !profile
->greenTRC
)
1081 goto invalid_tag_table
;
1083 } else if (profile
->color_space
== GRAY_SIGNATURE
) {
1085 profile
->grayTRC
= read_tag_curveType(src
, index
, TAG_kTRC
);
1086 if (!profile
->grayTRC
)
1087 goto invalid_tag_table
;
1090 assert(0 && "read_color_space protects against entering here");
1091 goto invalid_tag_table
;
1094 goto invalid_tag_table
;
1098 goto invalid_tag_table
;
1107 qcms_profile_release(profile
);
1108 return INVALID_PROFILE
;
1111 qcms_intent
qcms_profile_get_rendering_intent(qcms_profile
*profile
)
1113 return profile
->rendering_intent
;
1116 icColorSpaceSignature
1117 qcms_profile_get_color_space(qcms_profile
*profile
)
1119 return profile
->color_space
;
1122 static void lut_release(struct lutType
*lut
)
1127 void qcms_profile_release(qcms_profile
*profile
)
1129 if (profile
->output_table_r
)
1130 precache_release(profile
->output_table_r
);
1131 if (profile
->output_table_g
)
1132 precache_release(profile
->output_table_g
);
1133 if (profile
->output_table_b
)
1134 precache_release(profile
->output_table_b
);
1137 lut_release(profile
->A2B0
);
1139 lut_release(profile
->B2A0
);
1142 mAB_release(profile
->mAB
);
1144 mAB_release(profile
->mBA
);
1146 free(profile
->redTRC
);
1147 free(profile
->blueTRC
);
1148 free(profile
->greenTRC
);
1149 free(profile
->grayTRC
);
1155 qcms_profile
* qcms_profile_from_file(FILE *file
)
1157 uint32_t length
, remaining_length
;
1158 qcms_profile
*profile
;
1163 if (fread(&length_be
, 1, sizeof(length_be
), file
) != sizeof(length_be
))
1164 return BAD_VALUE_PROFILE
;
1166 length
= be32_to_cpu(length_be
);
1167 if (length
> MAX_PROFILE_SIZE
|| length
< sizeof(length_be
))
1168 return BAD_VALUE_PROFILE
;
1170 /* allocate room for the entire profile */
1171 data
= malloc(length
);
1173 return NO_MEM_PROFILE
;
1175 /* copy in length to the front so that the buffer will contain the entire profile */
1176 *((be32
*)data
) = length_be
;
1177 remaining_length
= length
- sizeof(length_be
);
1179 /* read the rest profile */
1180 read_length
= fread((unsigned char*)data
+ sizeof(length_be
), 1, remaining_length
, file
);
1181 if (read_length
!= remaining_length
) {
1183 return INVALID_PROFILE
;
1186 profile
= qcms_profile_from_memory(data
, length
);
1191 qcms_profile
* qcms_profile_from_path(const char *path
)
1193 qcms_profile
*profile
= NULL
;
1194 FILE *file
= fopen(path
, "rb");
1196 profile
= qcms_profile_from_file(file
);
1203 /* Unicode path version */
1204 qcms_profile
* qcms_profile_from_unicode_path(const wchar_t *path
)
1206 qcms_profile
*profile
= NULL
;
1207 FILE *file
= _wfopen(path
, L
"rb");
1209 profile
= qcms_profile_from_file(file
);