Fix race condition in gyp/ninja builds.
[chromium-blink-merge.git] / third_party / qcms / src / iccread.c
blob5876f96b124f0117d09f21fd06b30529b98c7c40
1 /* vim: set ts=8 sw=8 noexpandtab: */
2 // qcms
3 // Copyright (C) 2009 Mozilla Foundation
4 // Copyright (C) 1998-2007 Marti Maria
5 //
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.
24 #include <math.h>
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <string.h> //memset
28 #include "qcmsint.h"
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;
37 #if 0
38 not used yet
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);
46 return v;
47 #endif
49 #endif
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);
56 #else
57 return v;
58 #endif
61 static uint16_t be16_to_cpu(be16 v)
63 #ifdef IS_LITTLE_ENDIAN
64 return ((v & 0xff) << 8) | ((v & 0xff00) >> 8);
65 #else
66 return v;
67 #endif
70 /* a wrapper around the memory that we are going to parse
71 * into a qcms_profile */
72 struct mem_source
74 const unsigned char *buf;
75 size_t size;
76 qcms_bool valid;
77 const char *invalid_reason;
80 static void invalid_source(struct mem_source *mem, const char *reason)
82 mem->valid = false;
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");
93 return 0;
94 } else {
95 be32 k;
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");
105 return 0;
106 } else {
107 be16 k;
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");
117 return 0;
118 } else {
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:
190 break;
191 default:
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) {
200 case RGB_SIGNATURE:
201 case GRAY_SIGNATURE:
202 break;
203 default:
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) {
212 case XYZ_SIGNATURE:
213 case LAB_SIGNATURE:
214 break;
215 default:
216 invalid_source(mem, "Unsupported pcs");
220 struct tag
222 uint32_t signature;
223 uint32_t offset;
224 uint32_t size;
227 struct tag_index {
228 uint32_t count;
229 struct tag *tags;
232 static struct tag_index read_tag_table(qcms_profile *profile, struct mem_source *mem)
234 struct tag_index index = {0, NULL};
235 unsigned int i;
237 index.count = read_u32(mem, 128);
238 if (index.count > MAX_TAG_COUNT) {
239 invalid_source(mem, "max number of tags exceeded");
240 return index;
243 index.tags = malloc(sizeof(struct tag)*index.count);
244 if (index.tags) {
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);
252 return index;
255 // Checks a profile for obvious inconsistencies and returns
256 // true if the profile looks bogus and should probably be
257 // ignored.
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;
262 bool negative;
263 unsigned i;
265 // We currently only check the bogosity of RGB profiles
266 if (profile->color_space != RGB_SIGNATURE)
267 return false;
269 if (qcms_supports_iccv4 && (profile->A2B0 || profile->B2A0))
270 return false;
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.
286 negative =
287 (rX < 0) || (rY < 0) || (rZ < 0) ||
288 (gX < 0) || (gY < 0) || (gZ < 0) ||
289 (bX < 0) || (bY < 0) || (bZ < 0);
291 if (negative)
292 return true;
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 #if defined (_MSC_VER)
301 #pragma warning(push)
302 /* Disable double to float truncation warning 4305 */
303 #pragma warning(disable:4305)
304 #endif
305 // Build our target vector (see mozilla bug 460629)
306 target[0] = 0.96420;
307 target[1] = 1.00000;
308 target[2] = 0.82491;
310 // Our tolerance vector - Recommended by Chris Murphy based on
311 // conversion from the LAB space criterion of no more than 3 in any one
312 // channel. This is similar to, but slightly more tolerant than Adobe's
313 // criterion.
314 tolerance[0] = 0.02;
315 tolerance[1] = 0.02;
316 tolerance[2] = 0.04;
318 #if defined (_MSC_VER)
319 /* Restore warnings */
320 #pragma warning(pop)
321 #endif
322 // Compare with our tolerance
323 for (i = 0; i < 3; ++i) {
324 if (!(((sum[i] - tolerance[i]) <= target[i]) &&
325 ((sum[i] + tolerance[i]) >= target[i])))
326 return true;
329 // All Good
330 return false;
333 #define TAG_bXYZ 0x6258595a
334 #define TAG_gXYZ 0x6758595a
335 #define TAG_rXYZ 0x7258595a
336 #define TAG_rTRC 0x72545243
337 #define TAG_bTRC 0x62545243
338 #define TAG_gTRC 0x67545243
339 #define TAG_kTRC 0x6b545243
340 #define TAG_A2B0 0x41324230
341 #define TAG_B2A0 0x42324130
342 #define TAG_CHAD 0x63686164
344 static struct tag *find_tag(struct tag_index index, uint32_t tag_id)
346 unsigned int i;
347 struct tag *tag = NULL;
348 for (i = 0; i < index.count; i++) {
349 if (index.tags[i].signature == tag_id) {
350 return &index.tags[i];
353 return tag;
356 #define XYZ_TYPE 0x58595a20 // 'XYZ '
357 #define CURVE_TYPE 0x63757276 // 'curv'
358 #define PARAMETRIC_CURVE_TYPE 0x70617261 // 'para'
359 #define LUT16_TYPE 0x6d667432 // 'mft2'
360 #define LUT8_TYPE 0x6d667431 // 'mft1'
361 #define LUT_MAB_TYPE 0x6d414220 // 'mAB '
362 #define LUT_MBA_TYPE 0x6d424120 // 'mBA '
363 #define CHROMATIC_TYPE 0x73663332 // 'sf32'
365 static struct matrix read_tag_s15Fixed16ArrayType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
367 struct tag *tag = find_tag(index, tag_id);
368 struct matrix matrix;
369 if (tag) {
370 uint8_t i;
371 uint32_t offset = tag->offset;
372 uint32_t type = read_u32(src, offset);
374 // Check mandatory type signature for s16Fixed16ArrayType
375 if (type != CHROMATIC_TYPE) {
376 invalid_source(src, "unexpected type, expected 'sf32'");
379 for (i = 0; i < 9; i++) {
380 matrix.m[i/3][i%3] = s15Fixed16Number_to_float(read_s15Fixed16Number(src, offset+8+i*4));
382 matrix.invalid = false;
383 } else {
384 matrix.invalid = true;
385 invalid_source(src, "missing sf32tag");
387 return matrix;
390 static struct XYZNumber read_tag_XYZType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
392 struct XYZNumber num = {0, 0, 0};
393 struct tag *tag = find_tag(index, tag_id);
394 if (tag) {
395 uint32_t offset = tag->offset;
397 uint32_t type = read_u32(src, offset);
398 if (type != XYZ_TYPE)
399 invalid_source(src, "unexpected type, expected XYZ");
400 num.X = read_s15Fixed16Number(src, offset+8);
401 num.Y = read_s15Fixed16Number(src, offset+12);
402 num.Z = read_s15Fixed16Number(src, offset+16);
403 } else {
404 invalid_source(src, "missing xyztag");
406 return num;
409 // Read the tag at a given offset rather then the tag_index.
410 // This method is used when reading mAB tags where nested curveType are
411 // present that are not part of the tag_index.
412 static struct curveType *read_curveType(struct mem_source *src, uint32_t offset, uint32_t *len)
414 static const uint32_t COUNT_TO_LENGTH[5] = {1, 3, 4, 5, 7};
415 struct curveType *curve = NULL;
416 uint32_t type = read_u32(src, offset);
417 uint32_t count;
418 int i;
420 if (type != CURVE_TYPE && type != PARAMETRIC_CURVE_TYPE) {
421 invalid_source(src, "unexpected type, expected CURV or PARA");
422 return NULL;
425 if (type == CURVE_TYPE) {
426 count = read_u32(src, offset+8);
428 #define MAX_CURVE_ENTRIES 40000 //arbitrary
429 if (count > MAX_CURVE_ENTRIES) {
430 invalid_source(src, "curve size too large");
431 return NULL;
433 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*count);
434 if (!curve)
435 return NULL;
437 curve->count = count;
438 curve->type = type;
440 for (i=0; i<count; i++) {
441 curve->data[i] = read_u16(src, offset + 12 + i*2);
443 *len = 12 + count * 2;
444 } else { //PARAMETRIC_CURVE_TYPE
445 count = read_u16(src, offset+8);
447 if (count > 4) {
448 invalid_source(src, "parametric function type not supported.");
449 return NULL;
452 curve = malloc(sizeof(struct curveType));
453 if (!curve)
454 return NULL;
456 curve->count = count;
457 curve->type = type;
459 for (i=0; i < COUNT_TO_LENGTH[count]; i++) {
460 curve->parameter[i] = s15Fixed16Number_to_float(read_s15Fixed16Number(src, offset + 12 + i*4));
462 *len = 12 + COUNT_TO_LENGTH[count] * 4;
464 if ((count == 1 || count == 2)) {
465 /* we have a type 1 or type 2 function that has a division by 'a' */
466 float a = curve->parameter[1];
467 if (a == 0.f)
468 invalid_source(src, "parametricCurve definition causes division by zero.");
472 return curve;
475 static struct curveType *read_tag_curveType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
477 struct tag *tag = find_tag(index, tag_id);
478 struct curveType *curve = NULL;
479 if (tag) {
480 uint32_t len;
481 return read_curveType(src, tag->offset, &len);
482 } else {
483 invalid_source(src, "missing curvetag");
486 return curve;
489 #define MAX_CLUT_SIZE 500000 // arbitrary
490 #define MAX_CHANNELS 10 // arbitrary
491 static void read_nested_curveType(struct mem_source *src, struct curveType *(*curveArray)[MAX_CHANNELS], uint8_t num_channels, uint32_t curve_offset)
493 uint32_t channel_offset = 0;
494 int i;
495 for (i = 0; i < num_channels; i++) {
496 uint32_t tag_len = ~0;
498 (*curveArray)[i] = read_curveType(src, curve_offset + channel_offset, &tag_len);
499 if (!(*curveArray)[i]) {
500 invalid_source(src, "invalid nested curveType curve");
503 if (tag_len == ~0) {
504 invalid_source(src, "invalid nested curveType tag length");
505 return;
508 channel_offset += tag_len;
509 // 4 byte aligned
510 if ((tag_len % 4) != 0)
511 channel_offset += 4 - (tag_len % 4);
515 static void mAB_release(struct lutmABType *lut)
517 uint8_t i;
519 for (i = 0; i < lut->num_in_channels; i++){
520 free(lut->a_curves[i]);
522 for (i = 0; i < lut->num_out_channels; i++){
523 free(lut->b_curves[i]);
524 free(lut->m_curves[i]);
526 free(lut);
529 /* See section 10.10 for specs */
530 static struct lutmABType *read_tag_lutmABType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
532 struct tag *tag = find_tag(index, tag_id);
533 uint32_t offset = tag->offset;
534 uint32_t a_curve_offset, b_curve_offset, m_curve_offset;
535 uint32_t matrix_offset;
536 uint32_t clut_offset;
537 uint32_t clut_size = 1;
538 uint8_t clut_precision;
539 uint32_t type = read_u32(src, offset);
540 uint8_t num_in_channels, num_out_channels;
541 struct lutmABType *lut;
542 int i;
544 if (type != LUT_MAB_TYPE && type != LUT_MBA_TYPE) {
545 return NULL;
548 num_in_channels = read_u8(src, offset + 8);
549 num_out_channels = read_u8(src, offset + 8);
550 if (num_in_channels > MAX_CHANNELS || num_out_channels > MAX_CHANNELS)
551 return NULL;
553 // We require 3in/out channels since we only support RGB->XYZ (or RGB->LAB)
554 // XXX: If we remove this restriction make sure that the number of channels
555 // is less or equal to the maximum number of mAB curves in qcmsint.h
556 // also check for clut_size overflow.
557 if (num_in_channels != 3 || num_out_channels != 3)
558 return NULL;
560 // some of this data is optional and is denoted by a zero offset
561 // we also use this to track their existance
562 a_curve_offset = read_u32(src, offset + 28);
563 clut_offset = read_u32(src, offset + 24);
564 m_curve_offset = read_u32(src, offset + 20);
565 matrix_offset = read_u32(src, offset + 16);
566 b_curve_offset = read_u32(src, offset + 12);
568 // Convert offsets relative to the tag to relative to the profile
569 // preserve zero for optional fields
570 if (a_curve_offset)
571 a_curve_offset += offset;
572 if (clut_offset)
573 clut_offset += offset;
574 if (m_curve_offset)
575 m_curve_offset += offset;
576 if (matrix_offset)
577 matrix_offset += offset;
578 if (b_curve_offset)
579 b_curve_offset += offset;
581 if (clut_offset) {
582 assert (num_in_channels == 3);
583 // clut_size can not overflow since lg(256^num_in_channels) = 24 bits.
584 for (i = 0; i < num_in_channels; i++) {
585 clut_size *= read_u8(src, clut_offset + i);
587 } else {
588 clut_size = 0;
591 // 24bits * 3 won't overflow either
592 clut_size = clut_size * num_out_channels;
594 if (clut_size > MAX_CLUT_SIZE)
595 return NULL;
597 lut = malloc(sizeof(struct lutmABType) + (clut_size) * sizeof(float));
598 if (!lut)
599 return NULL;
600 // we'll fill in the rest below
601 memset(lut, 0, sizeof(struct lutmABType));
602 lut->clut_table = &lut->clut_table_data[0];
604 for (i = 0; i < num_in_channels; i++) {
605 lut->num_grid_points[i] = read_u8(src, clut_offset + i);
608 // Reverse the processing of transformation elements for mBA type.
609 lut->reversed = (type == LUT_MBA_TYPE);
611 lut->num_in_channels = num_in_channels;
612 lut->num_out_channels = num_out_channels;
614 if (matrix_offset) {
615 // read the matrix if we have it
616 lut->e00 = read_s15Fixed16Number(src, matrix_offset+4*0);
617 lut->e01 = read_s15Fixed16Number(src, matrix_offset+4*1);
618 lut->e02 = read_s15Fixed16Number(src, matrix_offset+4*2);
619 lut->e10 = read_s15Fixed16Number(src, matrix_offset+4*3);
620 lut->e11 = read_s15Fixed16Number(src, matrix_offset+4*4);
621 lut->e12 = read_s15Fixed16Number(src, matrix_offset+4*5);
622 lut->e20 = read_s15Fixed16Number(src, matrix_offset+4*6);
623 lut->e21 = read_s15Fixed16Number(src, matrix_offset+4*7);
624 lut->e22 = read_s15Fixed16Number(src, matrix_offset+4*8);
625 lut->e03 = read_s15Fixed16Number(src, matrix_offset+4*9);
626 lut->e13 = read_s15Fixed16Number(src, matrix_offset+4*10);
627 lut->e23 = read_s15Fixed16Number(src, matrix_offset+4*11);
630 if (a_curve_offset) {
631 read_nested_curveType(src, &lut->a_curves, num_in_channels, a_curve_offset);
633 if (m_curve_offset) {
634 read_nested_curveType(src, &lut->m_curves, num_out_channels, m_curve_offset);
636 if (b_curve_offset) {
637 read_nested_curveType(src, &lut->b_curves, num_out_channels, b_curve_offset);
638 } else {
639 invalid_source(src, "B curves required");
642 if (clut_offset) {
643 clut_precision = read_u8(src, clut_offset + 16);
644 if (clut_precision == 1) {
645 for (i = 0; i < clut_size; i++) {
646 lut->clut_table[i] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + 20 + i*1));
648 } else if (clut_precision == 2) {
649 for (i = 0; i < clut_size; i++) {
650 lut->clut_table[i] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + 20 + i*2));
652 } else {
653 invalid_source(src, "Invalid clut precision");
657 if (!src->valid) {
658 mAB_release(lut);
659 return NULL;
662 return lut;
665 static struct lutType *read_tag_lutType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
667 struct tag *tag = find_tag(index, tag_id);
668 uint32_t offset = tag->offset;
669 uint32_t type = read_u32(src, offset);
670 uint16_t num_input_table_entries;
671 uint16_t num_output_table_entries;
672 uint8_t in_chan, grid_points, out_chan;
673 size_t clut_offset, output_offset;
674 uint32_t clut_size;
675 size_t entry_size;
676 struct lutType *lut;
677 int i;
679 /* I'm not sure why the spec specifies a fixed number of entries for LUT8 tables even though
680 * they have room for the num_entries fields */
681 if (type == LUT8_TYPE) {
682 num_input_table_entries = 256;
683 num_output_table_entries = 256;
684 entry_size = 1;
685 } else if (type == LUT16_TYPE) {
686 num_input_table_entries = read_u16(src, offset + 48);
687 num_output_table_entries = read_u16(src, offset + 50);
688 entry_size = 2;
689 } else {
690 assert(0); // the caller checks that this doesn't happen
691 invalid_source(src, "Unexpected lut type");
692 return NULL;
695 in_chan = read_u8(src, offset + 8);
696 out_chan = read_u8(src, offset + 9);
697 grid_points = read_u8(src, offset + 10);
699 clut_size = pow(grid_points, in_chan);
700 if (clut_size > MAX_CLUT_SIZE) {
701 return NULL;
704 if (in_chan != 3 || out_chan != 3) {
705 return NULL;
708 lut = malloc(sizeof(struct lutType) + (num_input_table_entries * in_chan + clut_size*out_chan + num_output_table_entries * out_chan)*sizeof(float));
709 if (!lut) {
710 return NULL;
713 /* compute the offsets of tables */
714 lut->input_table = &lut->table_data[0];
715 lut->clut_table = &lut->table_data[in_chan*num_input_table_entries];
716 lut->output_table = &lut->table_data[in_chan*num_input_table_entries + clut_size*out_chan];
718 lut->num_input_table_entries = num_input_table_entries;
719 lut->num_output_table_entries = num_output_table_entries;
720 lut->num_input_channels = read_u8(src, offset + 8);
721 lut->num_output_channels = read_u8(src, offset + 9);
722 lut->num_clut_grid_points = read_u8(src, offset + 10);
723 lut->e00 = read_s15Fixed16Number(src, offset+12);
724 lut->e01 = read_s15Fixed16Number(src, offset+16);
725 lut->e02 = read_s15Fixed16Number(src, offset+20);
726 lut->e10 = read_s15Fixed16Number(src, offset+24);
727 lut->e11 = read_s15Fixed16Number(src, offset+28);
728 lut->e12 = read_s15Fixed16Number(src, offset+32);
729 lut->e20 = read_s15Fixed16Number(src, offset+36);
730 lut->e21 = read_s15Fixed16Number(src, offset+40);
731 lut->e22 = read_s15Fixed16Number(src, offset+44);
733 for (i = 0; i < lut->num_input_table_entries * in_chan; i++) {
734 if (type == LUT8_TYPE) {
735 lut->input_table[i] = uInt8Number_to_float(read_uInt8Number(src, offset + 52 + i * entry_size));
736 } else {
737 lut->input_table[i] = uInt16Number_to_float(read_uInt16Number(src, offset + 52 + i * entry_size));
741 clut_offset = offset + 52 + lut->num_input_table_entries * in_chan * entry_size;
742 for (i = 0; i < clut_size * out_chan; i+=3) {
743 if (type == LUT8_TYPE) {
744 lut->clut_table[i+0] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 0));
745 lut->clut_table[i+1] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 1));
746 lut->clut_table[i+2] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 2));
747 } else {
748 lut->clut_table[i+0] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 0));
749 lut->clut_table[i+1] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 2));
750 lut->clut_table[i+2] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 4));
754 output_offset = clut_offset + clut_size * out_chan * entry_size;
755 for (i = 0; i < lut->num_output_table_entries * out_chan; i++) {
756 if (type == LUT8_TYPE) {
757 lut->output_table[i] = uInt8Number_to_float(read_uInt8Number(src, output_offset + i*entry_size));
758 } else {
759 lut->output_table[i] = uInt16Number_to_float(read_uInt16Number(src, output_offset + i*entry_size));
763 return lut;
766 static void read_rendering_intent(qcms_profile *profile, struct mem_source *src)
768 profile->rendering_intent = read_u32(src, 64);
769 switch (profile->rendering_intent) {
770 case QCMS_INTENT_PERCEPTUAL:
771 case QCMS_INTENT_SATURATION:
772 case QCMS_INTENT_RELATIVE_COLORIMETRIC:
773 case QCMS_INTENT_ABSOLUTE_COLORIMETRIC:
774 break;
775 default:
776 invalid_source(src, "unknown rendering intent");
780 qcms_profile *qcms_profile_create(void)
782 return calloc(sizeof(qcms_profile), 1);
787 /* build sRGB gamma table */
788 /* based on cmsBuildParametricGamma() */
789 static uint16_t *build_sRGB_gamma_table(int num_entries)
791 int i;
792 /* taken from lcms: Build_sRGBGamma() */
793 double gamma = 2.4;
794 double a = 1./1.055;
795 double b = 0.055/1.055;
796 double c = 1./12.92;
797 double d = 0.04045;
799 uint16_t *table = malloc(sizeof(uint16_t) * num_entries);
800 if (!table)
801 return NULL;
803 for (i=0; i<num_entries; i++) {
804 double x = (double)i / (num_entries-1);
805 double y, output;
806 // IEC 61966-2.1 (sRGB)
807 // Y = (aX + b)^Gamma | X >= d
808 // Y = cX | X < d
809 if (x >= d) {
810 double e = (a*x + b);
811 if (e > 0)
812 y = pow(e, gamma);
813 else
814 y = 0;
815 } else {
816 y = c*x;
819 // Saturate -- this could likely move to a separate function
820 output = y * 65535. + .5;
821 if (output > 65535.)
822 output = 65535;
823 if (output < 0)
824 output = 0;
825 table[i] = (uint16_t)floor(output);
827 return table;
830 static struct curveType *curve_from_table(uint16_t *table, int num_entries)
832 struct curveType *curve;
833 int i;
834 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entries);
835 if (!curve)
836 return NULL;
837 curve->type = CURVE_TYPE;
838 curve->count = num_entries;
839 for (i = 0; i < num_entries; i++) {
840 curve->data[i] = table[i];
842 return curve;
845 static uint16_t float_to_u8Fixed8Number(float a)
847 if (a > (255.f + 255.f/256))
848 return 0xffff;
849 else if (a < 0.f)
850 return 0;
851 else
852 return floor(a*256.f + .5f);
855 static struct curveType *curve_from_gamma(float gamma)
857 struct curveType *curve;
858 int num_entries = 1;
859 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entries);
860 if (!curve)
861 return NULL;
862 curve->count = num_entries;
863 curve->data[0] = float_to_u8Fixed8Number(gamma);
864 return curve;
868 //XXX: it would be nice if we had a way of ensuring
869 // everything in a profile was initialized regardless of how it was created
871 //XXX: should this also be taking a black_point?
872 /* similar to CGColorSpaceCreateCalibratedRGB */
873 qcms_profile* qcms_profile_create_rgb_with_gamma(
874 qcms_CIE_xyY white_point,
875 qcms_CIE_xyYTRIPLE primaries,
876 float gamma)
878 qcms_profile* profile = qcms_profile_create();
879 if (!profile)
880 return NO_MEM_PROFILE;
882 //XXX: should store the whitepoint
883 if (!set_rgb_colorants(profile, white_point, primaries)) {
884 qcms_profile_release(profile);
885 return INVALID_PROFILE;
888 profile->redTRC = curve_from_gamma(gamma);
889 profile->blueTRC = curve_from_gamma(gamma);
890 profile->greenTRC = curve_from_gamma(gamma);
892 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
893 qcms_profile_release(profile);
894 return NO_MEM_PROFILE;
896 profile->class = DISPLAY_DEVICE_PROFILE;
897 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
898 profile->color_space = RGB_SIGNATURE;
899 return profile;
902 qcms_profile* qcms_profile_create_rgb_with_table(
903 qcms_CIE_xyY white_point,
904 qcms_CIE_xyYTRIPLE primaries,
905 uint16_t *table, int num_entries)
907 qcms_profile* profile = qcms_profile_create();
908 if (!profile)
909 return NO_MEM_PROFILE;
911 //XXX: should store the whitepoint
912 if (!set_rgb_colorants(profile, white_point, primaries)) {
913 qcms_profile_release(profile);
914 return INVALID_PROFILE;
917 profile->redTRC = curve_from_table(table, num_entries);
918 profile->blueTRC = curve_from_table(table, num_entries);
919 profile->greenTRC = curve_from_table(table, num_entries);
921 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
922 qcms_profile_release(profile);
923 return NO_MEM_PROFILE;
925 profile->class = DISPLAY_DEVICE_PROFILE;
926 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
927 profile->color_space = RGB_SIGNATURE;
928 return profile;
931 /* from lcms: cmsWhitePointFromTemp */
932 /* tempK must be >= 4000. and <= 25000.
933 * similar to argyll: icx_DTEMP2XYZ() */
934 static qcms_CIE_xyY white_point_from_temp(int temp_K)
936 qcms_CIE_xyY white_point;
937 double x, y;
938 double T, T2, T3;
939 // double M1, M2;
941 // No optimization provided.
942 T = temp_K;
943 T2 = T*T; // Square
944 T3 = T2*T; // Cube
946 // For correlated color temperature (T) between 4000K and 7000K:
947 if (T >= 4000. && T <= 7000.) {
948 x = -4.6070*(1E9/T3) + 2.9678*(1E6/T2) + 0.09911*(1E3/T) + 0.244063;
949 } else {
950 // or for correlated color temperature (T) between 7000K and 25000K:
951 if (T > 7000.0 && T <= 25000.0) {
952 x = -2.0064*(1E9/T3) + 1.9018*(1E6/T2) + 0.24748*(1E3/T) + 0.237040;
953 } else {
954 assert(0 && "invalid temp");
958 // Obtain y(x)
960 y = -3.000*(x*x) + 2.870*x - 0.275;
962 // wave factors (not used, but here for futures extensions)
964 // M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y);
965 // M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y);
967 // Fill white_point struct
968 white_point.x = x;
969 white_point.y = y;
970 white_point.Y = 1.0;
972 return white_point;
975 qcms_profile* qcms_profile_sRGB(void)
977 qcms_profile *profile;
978 uint16_t *table;
980 qcms_CIE_xyYTRIPLE Rec709Primaries = {
981 {0.6400, 0.3300, 1.0},
982 {0.3000, 0.6000, 1.0},
983 {0.1500, 0.0600, 1.0}
985 qcms_CIE_xyY D65;
987 D65 = white_point_from_temp(6504);
989 table = build_sRGB_gamma_table(1024);
991 if (!table)
992 return NO_MEM_PROFILE;
994 profile = qcms_profile_create_rgb_with_table(D65, Rec709Primaries, table, 1024);
995 free(table);
996 return profile;
1000 /* qcms_profile_from_memory does not hold a reference to the memory passed in */
1001 qcms_profile* qcms_profile_from_memory(const void *mem, size_t size)
1003 uint32_t length;
1004 struct mem_source source;
1005 struct mem_source *src = &source;
1006 struct tag_index index;
1007 qcms_profile *profile;
1009 source.buf = mem;
1010 source.size = size;
1011 source.valid = true;
1013 if (size < 4)
1014 return INVALID_PROFILE;
1016 length = read_u32(src, 0);
1017 if (length <= size) {
1018 // shrink the area that we can read if appropriate
1019 source.size = length;
1020 } else {
1021 return INVALID_PROFILE;
1024 /* ensure that the profile size is sane so it's easier to reason about */
1025 if (source.size <= 64 || source.size >= MAX_PROFILE_SIZE)
1026 return INVALID_PROFILE;
1028 profile = qcms_profile_create();
1029 if (!profile)
1030 return NO_MEM_PROFILE;
1032 check_CMM_type_signature(src);
1033 check_profile_version(src);
1034 read_class_signature(profile, src);
1035 read_rendering_intent(profile, src);
1036 read_color_space(profile, src);
1037 read_pcs(profile, src);
1038 //TODO read rest of profile stuff
1040 if (!src->valid)
1041 goto invalid_profile;
1043 index = read_tag_table(profile, src);
1044 if (!src->valid || !index.tags)
1045 goto invalid_tag_table;
1047 if (find_tag(index, TAG_CHAD)) {
1048 profile->chromaticAdaption = read_tag_s15Fixed16ArrayType(src, index, TAG_CHAD);
1049 } else {
1050 profile->chromaticAdaption.invalid = true; //Signal the data is not present
1053 if (profile->class == DISPLAY_DEVICE_PROFILE || profile->class == INPUT_DEVICE_PROFILE ||
1054 profile->class == OUTPUT_DEVICE_PROFILE || profile->class == COLOR_SPACE_PROFILE) {
1055 if (profile->color_space == RGB_SIGNATURE) {
1056 if (find_tag(index, TAG_A2B0)) {
1057 if (read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT8_TYPE ||
1058 read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT16_TYPE) {
1059 profile->A2B0 = read_tag_lutType(src, index, TAG_A2B0);
1060 } else if (read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT_MAB_TYPE) {
1061 profile->mAB = read_tag_lutmABType(src, index, TAG_A2B0);
1064 if (find_tag(index, TAG_B2A0)) {
1065 if (read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT8_TYPE ||
1066 read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT16_TYPE) {
1067 profile->B2A0 = read_tag_lutType(src, index, TAG_B2A0);
1068 } else if (read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT_MBA_TYPE) {
1069 profile->mBA = read_tag_lutmABType(src, index, TAG_B2A0);
1072 if (find_tag(index, TAG_rXYZ) || !qcms_supports_iccv4) {
1073 profile->redColorant = read_tag_XYZType(src, index, TAG_rXYZ);
1074 profile->greenColorant = read_tag_XYZType(src, index, TAG_gXYZ);
1075 profile->blueColorant = read_tag_XYZType(src, index, TAG_bXYZ);
1078 if (!src->valid)
1079 goto invalid_tag_table;
1081 if (find_tag(index, TAG_rTRC) || !qcms_supports_iccv4) {
1082 profile->redTRC = read_tag_curveType(src, index, TAG_rTRC);
1083 profile->greenTRC = read_tag_curveType(src, index, TAG_gTRC);
1084 profile->blueTRC = read_tag_curveType(src, index, TAG_bTRC);
1086 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC)
1087 goto invalid_tag_table;
1089 } else if (profile->color_space == GRAY_SIGNATURE) {
1091 profile->grayTRC = read_tag_curveType(src, index, TAG_kTRC);
1092 if (!profile->grayTRC)
1093 goto invalid_tag_table;
1095 } else {
1096 assert(0 && "read_color_space protects against entering here");
1097 goto invalid_tag_table;
1099 } else {
1100 goto invalid_tag_table;
1103 if (!src->valid)
1104 goto invalid_tag_table;
1106 free(index.tags);
1108 return profile;
1110 invalid_tag_table:
1111 free(index.tags);
1112 invalid_profile:
1113 qcms_profile_release(profile);
1114 return INVALID_PROFILE;
1117 qcms_intent qcms_profile_get_rendering_intent(qcms_profile *profile)
1119 return profile->rendering_intent;
1122 icColorSpaceSignature
1123 qcms_profile_get_color_space(qcms_profile *profile)
1125 return profile->color_space;
1128 static void lut_release(struct lutType *lut)
1130 free(lut);
1133 void qcms_profile_release(qcms_profile *profile)
1135 if (profile->output_table_r)
1136 precache_release(profile->output_table_r);
1137 if (profile->output_table_g)
1138 precache_release(profile->output_table_g);
1139 if (profile->output_table_b)
1140 precache_release(profile->output_table_b);
1142 if (profile->A2B0)
1143 lut_release(profile->A2B0);
1144 if (profile->B2A0)
1145 lut_release(profile->B2A0);
1147 if (profile->mAB)
1148 mAB_release(profile->mAB);
1149 if (profile->mBA)
1150 mAB_release(profile->mBA);
1152 free(profile->redTRC);
1153 free(profile->blueTRC);
1154 free(profile->greenTRC);
1155 free(profile->grayTRC);
1156 free(profile);
1160 #include <stdio.h>
1161 qcms_profile* qcms_profile_from_file(FILE *file)
1163 uint32_t length, remaining_length;
1164 qcms_profile *profile;
1165 size_t read_length;
1166 be32 length_be;
1167 void *data;
1169 if (fread(&length_be, 1, sizeof(length_be), file) != sizeof(length_be))
1170 return BAD_VALUE_PROFILE;
1172 length = be32_to_cpu(length_be);
1173 if (length > MAX_PROFILE_SIZE || length < sizeof(length_be))
1174 return BAD_VALUE_PROFILE;
1176 /* allocate room for the entire profile */
1177 data = malloc(length);
1178 if (!data)
1179 return NO_MEM_PROFILE;
1181 /* copy in length to the front so that the buffer will contain the entire profile */
1182 *((be32*)data) = length_be;
1183 remaining_length = length - sizeof(length_be);
1185 /* read the rest profile */
1186 read_length = fread((unsigned char*)data + sizeof(length_be), 1, remaining_length, file);
1187 if (read_length != remaining_length) {
1188 free(data);
1189 return INVALID_PROFILE;
1192 profile = qcms_profile_from_memory(data, length);
1193 free(data);
1194 return profile;
1197 qcms_profile* qcms_profile_from_path(const char *path)
1199 qcms_profile *profile = NULL;
1200 FILE *file = fopen(path, "rb");
1201 if (file) {
1202 profile = qcms_profile_from_file(file);
1203 fclose(file);
1205 return profile;
1208 #ifdef _WIN32
1209 /* Unicode path version */
1210 qcms_profile* qcms_profile_from_unicode_path(const wchar_t *path)
1212 qcms_profile *profile = NULL;
1213 FILE *file = _wfopen(path, L"rb");
1214 if (file) {
1215 profile = qcms_profile_from_file(file);
1216 fclose(file);
1218 return profile;
1220 #endif