Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / qcms / src / iccread.c
blob4ed24a8ce1f750d6c30969b064d84dad7d673e69
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);
151 static void check_profile_version(struct mem_source *src)
154 uint8_t major_revision = read_u8(src, 8 + 0);
155 uint8_t minor_revision = read_u8(src, 8 + 1);
157 uint8_t reserved1 = read_u8(src, 8 + 2);
158 uint8_t reserved2 = read_u8(src, 8 + 3);
159 /* Checking the version doesn't buy us anything
160 if (major_revision != 0x4) {
161 if (major_revision > 0x2)
162 invalid_source(src, "Unsupported major revision");
163 if (minor_revision > 0x40)
164 invalid_source(src, "Unsupported minor revision");
167 if (reserved1 != 0 || reserved2 != 0)
168 invalid_source(src, "Invalid reserved bytes");
171 #define INPUT_DEVICE_PROFILE 0x73636e72 // 'scnr'
172 #define DISPLAY_DEVICE_PROFILE 0x6d6e7472 // 'mntr'
173 #define OUTPUT_DEVICE_PROFILE 0x70727472 // 'prtr'
174 #define DEVICE_LINK_PROFILE 0x6c696e6b // 'link'
175 #define COLOR_SPACE_PROFILE 0x73706163 // 'spac'
176 #define ABSTRACT_PROFILE 0x61627374 // 'abst'
177 #define NAMED_COLOR_PROFILE 0x6e6d636c // 'nmcl'
179 static void read_class_signature(qcms_profile *profile, struct mem_source *mem)
181 profile->class = read_u32(mem, 12);
182 switch (profile->class) {
183 case DISPLAY_DEVICE_PROFILE:
184 case INPUT_DEVICE_PROFILE:
185 case OUTPUT_DEVICE_PROFILE:
186 case COLOR_SPACE_PROFILE:
187 break;
188 default:
189 invalid_source(mem, "Invalid Profile/Device Class signature");
193 static void read_color_space(qcms_profile *profile, struct mem_source *mem)
195 profile->color_space = read_u32(mem, 16);
196 switch (profile->color_space) {
197 case RGB_SIGNATURE:
198 case GRAY_SIGNATURE:
199 break;
200 default:
201 invalid_source(mem, "Unsupported colorspace");
205 static void read_pcs(qcms_profile *profile, struct mem_source *mem)
207 profile->pcs = read_u32(mem, 20);
208 switch (profile->pcs) {
209 case XYZ_SIGNATURE:
210 case LAB_SIGNATURE:
211 break;
212 default:
213 invalid_source(mem, "Unsupported pcs");
217 struct tag {
218 uint32_t signature;
219 uint32_t offset;
220 uint32_t size;
223 struct tag_index {
224 uint32_t count;
225 struct tag *tags;
228 static struct tag_index read_tag_table(qcms_profile *profile, struct mem_source *mem)
230 struct tag_index index = {0, NULL};
231 unsigned int i;
233 index.count = read_u32(mem, 128);
234 if (index.count > MAX_TAG_COUNT) {
235 invalid_source(mem, "max number of tags exceeded");
236 return index;
239 index.tags = malloc(sizeof(struct tag)*index.count);
240 if (index.tags) {
241 for (i = 0; i < index.count; i++) {
242 index.tags[i].signature = read_u32(mem, 128 + 4 + 4*i*3);
243 index.tags[i].offset = read_u32(mem, 128 + 4 + 4*i*3 + 4);
244 index.tags[i].size = read_u32(mem, 128 + 4 + 4*i*3 + 8);
248 return index;
251 // Checks a profile for obvious inconsistencies and returns
252 // true if the profile looks bogus and should probably be
253 // ignored.
254 qcms_bool qcms_profile_is_bogus(qcms_profile *profile)
256 float sum[3], target[3], tolerance[3];
257 float rX, rY, rZ, gX, gY, gZ, bX, bY, bZ;
258 bool negative;
259 unsigned i;
261 // We currently only check the bogosity of RGB profiles
262 if (profile->color_space != RGB_SIGNATURE)
263 return false;
265 if (qcms_supports_iccv4 && (profile->A2B0 || profile->B2A0))
266 return false;
268 rX = s15Fixed16Number_to_float(profile->redColorant.X);
269 rY = s15Fixed16Number_to_float(profile->redColorant.Y);
270 rZ = s15Fixed16Number_to_float(profile->redColorant.Z);
272 gX = s15Fixed16Number_to_float(profile->greenColorant.X);
273 gY = s15Fixed16Number_to_float(profile->greenColorant.Y);
274 gZ = s15Fixed16Number_to_float(profile->greenColorant.Z);
276 bX = s15Fixed16Number_to_float(profile->blueColorant.X);
277 bY = s15Fixed16Number_to_float(profile->blueColorant.Y);
278 bZ = s15Fixed16Number_to_float(profile->blueColorant.Z);
280 // Check if any of the XYZ values are negative (see mozilla bug 498245)
281 // CIEXYZ tristimulus values cannot be negative according to the spec.
282 negative =
283 (rX < 0) || (rY < 0) || (rZ < 0) ||
284 (gX < 0) || (gY < 0) || (gZ < 0) ||
285 (bX < 0) || (bY < 0) || (bZ < 0);
287 if (negative)
288 return true;
291 // Sum the values; they should add up to something close to white
292 sum[0] = rX + gX + bX;
293 sum[1] = rY + gY + bY;
294 sum[2] = rZ + gZ + bZ;
296 #if defined (_MSC_VER)
297 #pragma warning(push)
298 /* Disable double to float truncation warning 4305 */
299 #pragma warning(disable:4305)
300 #endif
301 // Build our target vector (see mozilla bug 460629)
302 target[0] = 0.96420;
303 target[1] = 1.00000;
304 target[2] = 0.82491;
306 // Our tolerance vector - Recommended by Chris Murphy based on
307 // conversion from the LAB space criterion of no more than 3 in any one
308 // channel. This is similar to, but slightly more tolerant than Adobe's
309 // criterion.
310 tolerance[0] = 0.02;
311 tolerance[1] = 0.02;
312 tolerance[2] = 0.04;
314 #if defined (_MSC_VER)
315 /* Restore warnings */
316 #pragma warning(pop)
317 #endif
318 // Compare with our tolerance
319 for (i = 0; i < 3; ++i) {
320 if (!(((sum[i] - tolerance[i]) <= target[i]) &&
321 ((sum[i] + tolerance[i]) >= target[i])))
322 return true;
325 // All Good
326 return false;
329 #define TAG_bXYZ 0x6258595a
330 #define TAG_gXYZ 0x6758595a
331 #define TAG_rXYZ 0x7258595a
332 #define TAG_rTRC 0x72545243
333 #define TAG_bTRC 0x62545243
334 #define TAG_gTRC 0x67545243
335 #define TAG_kTRC 0x6b545243
336 #define TAG_A2B0 0x41324230
337 #define TAG_B2A0 0x42324130
338 #define TAG_CHAD 0x63686164
339 #define TAG_desc 0x64657363
340 #define TAG_vcgt 0x76636774
342 static struct tag *find_tag(struct tag_index index, uint32_t tag_id)
344 unsigned int i;
346 for (i = 0; i < index.count; i++) {
347 if (index.tags[i].signature == tag_id)
348 return &index.tags[i];
351 return NULL;
354 #define DESC_TYPE 0x64657363 // 'desc'
355 #define MLUC_TYPE 0x6d6c7563 // 'mluc'
356 #define MMOD_TYPE 0x6D6D6F64 // 'mmod'
357 #define VCGT_TYPE 0x76636774 // 'vcgt'
359 static qcms_bool read_tag_vcgtType(qcms_profile *profile, struct mem_source *src, struct tag_index index)
361 size_t tag_offset = find_tag(index, TAG_vcgt)->offset;
362 uint32_t tag_type = read_u32(src, tag_offset);
363 uint32_t vcgt_type = read_u32(src, tag_offset + 8);
364 uint16_t channels = read_u16(src, tag_offset + 12);
365 uint16_t elements = read_u16(src, tag_offset + 14);
366 uint16_t byte_depth = read_u16(src, tag_offset + 16);
367 size_t table_offset = tag_offset + 18;
368 uint32_t i;
369 uint16_t *dest;
371 if (!src->valid || tag_type != VCGT_TYPE)
372 goto invalid_vcgt_tag;
374 // Only support 3 channels.
375 if (channels != 3)
376 return true;
377 // Only support single or double byte values.
378 if (byte_depth != 1 && byte_depth != 2)
379 return true;
380 // Only support table data, not equation.
381 if (vcgt_type != 0)
382 return true;
383 // Limit the table to a sensible size; 10-bit gamma is a reasonable
384 // maximum for hardware correction.
385 if (elements > 1024)
386 return true;
388 // Empty table is invalid.
389 if (!elements)
390 goto invalid_vcgt_tag;
392 profile->vcgt.length = elements;
393 profile->vcgt.data = malloc(3 * elements * sizeof(uint16_t));
394 if (!profile->vcgt.data)
395 return false;
397 dest = profile->vcgt.data;
399 for (i = 0; i < 3 * elements; ++i) {
400 if (byte_depth == 1) {
401 *dest++ = read_u8(src, table_offset) * 256;
402 } else {
403 *dest++ = read_u16(src, table_offset);
406 table_offset += byte_depth;
408 if (!src->valid)
409 goto invalid_vcgt_tag;
412 return true;
414 invalid_vcgt_tag:
415 invalid_source(src, "invalid vcgt tag");
416 return false;
419 static bool read_tag_descType(qcms_profile *profile, struct mem_source *src, struct tag_index index, uint32_t tag_id)
421 struct tag *tag = find_tag(index, tag_id);
422 if (tag) {
423 const uint32_t limit = sizeof profile->description;
424 uint32_t offset = tag->offset;
425 uint32_t type = read_u32(src, offset);
426 uint32_t length = read_u32(src, offset+8);
427 uint32_t i, description_offset;
428 bool mluc = false;
429 if (length && type == MLUC_TYPE) {
430 length = read_u32(src, offset+20);
431 if (!length || (length & 1) || (read_u32(src, offset+12) != 12))
432 goto invalid_desc_tag;
433 description_offset = offset + read_u32(src, offset+24);
434 if (!src->valid)
435 goto invalid_desc_tag;
436 mluc = true;
437 } else if (length && type == DESC_TYPE) {
438 description_offset = offset + 12;
439 } else {
440 goto invalid_desc_tag;
442 if (length >= limit)
443 length = limit - 1;
444 for (i = 0; i < length; ++i) {
445 uint8_t value = read_u8(src, description_offset + i);
446 if (!src->valid)
447 goto invalid_desc_tag;
448 if (mluc && !value)
449 value = '.';
450 profile->description[i] = value;
452 profile->description[length] = 0;
453 } else {
454 goto invalid_desc_tag;
457 if (src->valid)
458 return true;
460 invalid_desc_tag:
461 invalid_source(src, "invalid description");
462 return false;
465 #if defined(__APPLE__)
467 // Use the dscm tag to change profile description "Display" to its more specific en-localized monitor name, if any.
469 #define TAG_dscm 0x6473636D // 'dscm'
471 static bool read_tag_dscmType(qcms_profile *profile, struct mem_source *src, struct tag_index index, uint32_t tag_id)
473 if (strcmp(profile->description, "Display") != 0)
474 return true;
476 struct tag *tag = find_tag(index, tag_id);
477 if (tag) {
478 uint32_t offset = tag->offset;
479 uint32_t type = read_u32(src, offset);
480 uint32_t records = read_u32(src, offset+8);
482 if (!src->valid || !records || type != MLUC_TYPE)
483 goto invalid_dscm_tag;
484 if (read_u32(src, offset+12) != 12) // MLUC record size: bytes
485 goto invalid_dscm_tag;
487 for (uint32_t i = 0; i < records; ++i) {
488 const uint32_t limit = sizeof profile->description;
489 const uint16_t isoen = 0x656E; // ISO-3166-1 language 'en'
491 uint16_t language = read_u16(src, offset + 16 + (i * 12) + 0);
492 uint32_t length = read_u32(src, offset + 16 + (i * 12) + 4);
493 uint32_t description_offset = read_u32(src, offset + 16 + (i * 12) + 8);
495 if (!src->valid || !length || (length & 1))
496 goto invalid_dscm_tag;
497 if (language != isoen)
498 continue;
500 // Use a prefix to identify the display description source
501 strcpy(profile->description, "dscm:");
502 length += 5;
504 if (length >= limit)
505 length = limit - 1;
506 for (uint32_t j = 5; j < length; ++j) {
507 uint8_t value = read_u8(src, offset + description_offset + j - 5);
508 if (!src->valid)
509 goto invalid_dscm_tag;
510 profile->description[j] = value ? value : '.';
512 profile->description[length] = 0;
513 break;
517 if (src->valid)
518 return true;
520 invalid_dscm_tag:
521 invalid_source(src, "invalid dscm tag");
522 return false;
525 // Use the mmod tag to change profile description "Display" to its specific mmod maker model data, if any.
527 #define TAG_mmod 0x6D6D6F64 // 'mmod'
529 static bool read_tag_mmodType(qcms_profile *profile, struct mem_source *src, struct tag_index index, uint32_t tag_id)
531 if (strcmp(profile->description, "Display") != 0)
532 return true;
534 struct tag *tag = find_tag(index, tag_id);
535 if (tag) {
536 const uint8_t length = 4 * 4; // Four 4-byte fields: 'mmod', 0, maker, model.
538 uint32_t offset = tag->offset;
539 if (tag->size < 40 || read_u32(src, offset) != MMOD_TYPE)
540 goto invalid_mmod_tag;
542 for (uint8_t i = 0; i < length; ++i) {
543 uint8_t value = read_u8(src, offset + i);
544 if (!src->valid)
545 goto invalid_mmod_tag;
546 profile->description[i] = value ? value : '.';
548 profile->description[length] = 0;
551 if (src->valid)
552 return true;
554 invalid_mmod_tag:
555 invalid_source(src, "invalid mmod tag");
556 return false;
559 #endif // __APPLE__
561 #define XYZ_TYPE 0x58595a20 // 'XYZ '
562 #define CURVE_TYPE 0x63757276 // 'curv'
563 #define PARAMETRIC_CURVE_TYPE 0x70617261 // 'para'
564 #define LUT16_TYPE 0x6d667432 // 'mft2'
565 #define LUT8_TYPE 0x6d667431 // 'mft1'
566 #define LUT_MAB_TYPE 0x6d414220 // 'mAB '
567 #define LUT_MBA_TYPE 0x6d424120 // 'mBA '
568 #define CHROMATIC_TYPE 0x73663332 // 'sf32'
570 static struct matrix read_tag_s15Fixed16ArrayType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
572 struct tag *tag = find_tag(index, tag_id);
573 struct matrix matrix;
574 if (tag) {
575 uint8_t i;
576 uint32_t offset = tag->offset;
577 uint32_t type = read_u32(src, offset);
579 // Check mandatory type signature for s16Fixed16ArrayType
580 if (type != CHROMATIC_TYPE) {
581 invalid_source(src, "unexpected type, expected 'sf32'");
584 for (i = 0; i < 9; i++) {
585 matrix.m[i/3][i%3] = s15Fixed16Number_to_float(read_s15Fixed16Number(src, offset+8+i*4));
587 matrix.invalid = false;
588 } else {
589 matrix.invalid = true;
590 invalid_source(src, "missing sf32tag");
592 return matrix;
595 static struct XYZNumber read_tag_XYZType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
597 struct XYZNumber num = {0, 0, 0};
598 struct tag *tag = find_tag(index, tag_id);
599 if (tag) {
600 uint32_t offset = tag->offset;
602 uint32_t type = read_u32(src, offset);
603 if (type != XYZ_TYPE)
604 invalid_source(src, "unexpected type, expected XYZ");
605 num.X = read_s15Fixed16Number(src, offset+8);
606 num.Y = read_s15Fixed16Number(src, offset+12);
607 num.Z = read_s15Fixed16Number(src, offset+16);
608 } else {
609 invalid_source(src, "missing xyztag");
611 return num;
614 // Read the tag at a given offset rather then the tag_index.
615 // This method is used when reading mAB tags where nested curveType are
616 // present that are not part of the tag_index.
617 static struct curveType *read_curveType(struct mem_source *src, uint32_t offset, uint32_t *len)
619 static const uint32_t COUNT_TO_LENGTH[5] = {1, 3, 4, 5, 7};
620 struct curveType *curve = NULL;
621 uint32_t type = read_u32(src, offset);
622 uint32_t count;
623 int i;
625 if (type != CURVE_TYPE && type != PARAMETRIC_CURVE_TYPE) {
626 invalid_source(src, "unexpected type, expected CURV or PARA");
627 return NULL;
630 if (type == CURVE_TYPE) {
631 count = read_u32(src, offset+8);
633 #define MAX_CURVE_ENTRIES 40000 //arbitrary
634 if (count > MAX_CURVE_ENTRIES) {
635 invalid_source(src, "curve size too large");
636 return NULL;
638 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*count);
639 if (!curve)
640 return NULL;
642 curve->count = count;
643 curve->type = type;
645 for (i=0; i<count; i++) {
646 curve->data[i] = read_u16(src, offset + 12 + i*2);
648 *len = 12 + count * 2;
649 } else { //PARAMETRIC_CURVE_TYPE
650 count = read_u16(src, offset+8);
652 if (count > 4) {
653 invalid_source(src, "parametric function type not supported.");
654 return NULL;
657 curve = malloc(sizeof(struct curveType));
658 if (!curve)
659 return NULL;
661 curve->count = count;
662 curve->type = type;
664 for (i=0; i < COUNT_TO_LENGTH[count]; i++) {
665 curve->parameter[i] = s15Fixed16Number_to_float(read_s15Fixed16Number(src, offset + 12 + i*4));
667 *len = 12 + COUNT_TO_LENGTH[count] * 4;
669 if ((count == 1 || count == 2)) {
670 /* we have a type 1 or type 2 function that has a division by 'a' */
671 float a = curve->parameter[1];
672 if (a == 0.f)
673 invalid_source(src, "parametricCurve definition causes division by zero.");
677 return curve;
680 static struct curveType *read_tag_curveType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
682 struct tag *tag = find_tag(index, tag_id);
683 struct curveType *curve = NULL;
684 if (tag) {
685 uint32_t len;
686 return read_curveType(src, tag->offset, &len);
687 } else {
688 invalid_source(src, "missing curvetag");
691 return curve;
694 #define MAX_CLUT_SIZE 500000 // arbitrary
695 #define MAX_CHANNELS 10 // arbitrary
696 static void read_nested_curveType(struct mem_source *src, struct curveType *(*curveArray)[MAX_CHANNELS], uint8_t num_channels, uint32_t curve_offset)
698 uint32_t channel_offset = 0;
699 int i;
700 for (i = 0; i < num_channels; i++) {
701 uint32_t tag_len = ~0;
703 (*curveArray)[i] = read_curveType(src, curve_offset + channel_offset, &tag_len);
704 if (!(*curveArray)[i]) {
705 invalid_source(src, "invalid nested curveType curve");
708 if (tag_len == ~0) {
709 invalid_source(src, "invalid nested curveType tag length");
710 return;
713 channel_offset += tag_len;
714 // 4 byte aligned
715 if ((tag_len % 4) != 0)
716 channel_offset += 4 - (tag_len % 4);
720 static void mAB_release(struct lutmABType *lut)
722 uint8_t i;
724 for (i = 0; i < lut->num_in_channels; i++){
725 free(lut->a_curves[i]);
727 for (i = 0; i < lut->num_out_channels; i++){
728 free(lut->b_curves[i]);
729 free(lut->m_curves[i]);
731 free(lut);
734 /* See section 10.10 for specs */
735 static struct lutmABType *read_tag_lutmABType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
737 struct tag *tag = find_tag(index, tag_id);
738 uint32_t offset = tag->offset;
739 uint32_t a_curve_offset, b_curve_offset, m_curve_offset;
740 uint32_t matrix_offset;
741 uint32_t clut_offset;
742 uint32_t clut_size = 1;
743 uint8_t clut_precision;
744 uint32_t type = read_u32(src, offset);
745 uint8_t num_in_channels, num_out_channels;
746 struct lutmABType *lut;
747 int i;
749 if (type != LUT_MAB_TYPE && type != LUT_MBA_TYPE) {
750 return NULL;
753 num_in_channels = read_u8(src, offset + 8);
754 num_out_channels = read_u8(src, offset + 8);
755 if (num_in_channels > MAX_CHANNELS || num_out_channels > MAX_CHANNELS)
756 return NULL;
758 // We require 3in/out channels since we only support RGB->XYZ (or RGB->LAB)
759 // XXX: If we remove this restriction make sure that the number of channels
760 // is less or equal to the maximum number of mAB curves in qcmsint.h
761 // also check for clut_size overflow. Also make sure it's != 0
762 if (num_in_channels != 3 || num_out_channels != 3)
763 return NULL;
765 // some of this data is optional and is denoted by a zero offset
766 // we also use this to track their existance
767 a_curve_offset = read_u32(src, offset + 28);
768 clut_offset = read_u32(src, offset + 24);
769 m_curve_offset = read_u32(src, offset + 20);
770 matrix_offset = read_u32(src, offset + 16);
771 b_curve_offset = read_u32(src, offset + 12);
773 // Convert offsets relative to the tag to relative to the profile
774 // preserve zero for optional fields
775 if (a_curve_offset)
776 a_curve_offset += offset;
777 if (clut_offset)
778 clut_offset += offset;
779 if (m_curve_offset)
780 m_curve_offset += offset;
781 if (matrix_offset)
782 matrix_offset += offset;
783 if (b_curve_offset)
784 b_curve_offset += offset;
786 if (clut_offset) {
787 assert (num_in_channels == 3);
788 // clut_size can not overflow since lg(256^num_in_channels) = 24 bits.
789 for (i = 0; i < num_in_channels; i++) {
790 clut_size *= read_u8(src, clut_offset + i);
791 if (clut_size == 0) {
792 invalid_source(src, "bad clut_size");
795 } else {
796 clut_size = 0;
799 // 24bits * 3 won't overflow either
800 clut_size = clut_size * num_out_channels;
802 if (clut_size > MAX_CLUT_SIZE)
803 return NULL;
805 lut = malloc(sizeof(struct lutmABType) + (clut_size) * sizeof(float));
806 if (!lut)
807 return NULL;
808 // we'll fill in the rest below
809 memset(lut, 0, sizeof(struct lutmABType));
810 lut->clut_table = &lut->clut_table_data[0];
812 if (clut_offset) {
813 for (i = 0; i < num_in_channels; i++) {
814 lut->num_grid_points[i] = read_u8(src, clut_offset + i);
815 if (lut->num_grid_points[i] == 0) {
816 invalid_source(src, "bad grid_points");
821 // Reverse the processing of transformation elements for mBA type.
822 lut->reversed = (type == LUT_MBA_TYPE);
824 lut->num_in_channels = num_in_channels;
825 lut->num_out_channels = num_out_channels;
827 if (matrix_offset) {
828 // read the matrix if we have it
829 lut->e00 = read_s15Fixed16Number(src, matrix_offset+4*0);
830 lut->e01 = read_s15Fixed16Number(src, matrix_offset+4*1);
831 lut->e02 = read_s15Fixed16Number(src, matrix_offset+4*2);
832 lut->e10 = read_s15Fixed16Number(src, matrix_offset+4*3);
833 lut->e11 = read_s15Fixed16Number(src, matrix_offset+4*4);
834 lut->e12 = read_s15Fixed16Number(src, matrix_offset+4*5);
835 lut->e20 = read_s15Fixed16Number(src, matrix_offset+4*6);
836 lut->e21 = read_s15Fixed16Number(src, matrix_offset+4*7);
837 lut->e22 = read_s15Fixed16Number(src, matrix_offset+4*8);
838 lut->e03 = read_s15Fixed16Number(src, matrix_offset+4*9);
839 lut->e13 = read_s15Fixed16Number(src, matrix_offset+4*10);
840 lut->e23 = read_s15Fixed16Number(src, matrix_offset+4*11);
843 if (a_curve_offset) {
844 read_nested_curveType(src, &lut->a_curves, num_in_channels, a_curve_offset);
846 if (m_curve_offset) {
847 read_nested_curveType(src, &lut->m_curves, num_out_channels, m_curve_offset);
849 if (b_curve_offset) {
850 read_nested_curveType(src, &lut->b_curves, num_out_channels, b_curve_offset);
851 } else {
852 invalid_source(src, "B curves required");
855 if (clut_offset) {
856 clut_precision = read_u8(src, clut_offset + 16);
857 if (clut_precision == 1) {
858 for (i = 0; i < clut_size; i++) {
859 lut->clut_table[i] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + 20 + i*1));
861 } else if (clut_precision == 2) {
862 for (i = 0; i < clut_size; i++) {
863 lut->clut_table[i] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + 20 + i*2));
865 } else {
866 invalid_source(src, "Invalid clut precision");
870 if (!src->valid) {
871 mAB_release(lut);
872 return NULL;
875 return lut;
878 static struct lutType *read_tag_lutType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
880 struct tag *tag = find_tag(index, tag_id);
881 uint32_t offset = tag->offset;
882 uint32_t type = read_u32(src, offset);
883 uint16_t num_input_table_entries;
884 uint16_t num_output_table_entries;
885 uint8_t in_chan, grid_points, out_chan;
886 size_t clut_offset, output_offset;
887 uint32_t clut_size;
888 size_t entry_size;
889 struct lutType *lut;
890 int i;
892 /* I'm not sure why the spec specifies a fixed number of entries for LUT8 tables even though
893 * they have room for the num_entries fields */
894 if (type == LUT8_TYPE) {
895 num_input_table_entries = 256;
896 num_output_table_entries = 256;
897 entry_size = 1;
898 } else if (type == LUT16_TYPE) {
899 num_input_table_entries = read_u16(src, offset + 48);
900 num_output_table_entries = read_u16(src, offset + 50);
901 if (num_input_table_entries == 0 || num_output_table_entries == 0) {
902 invalid_source(src, "Bad channel count");
903 return NULL;
905 entry_size = 2;
906 } else {
907 assert(0); // the caller checks that this doesn't happen
908 invalid_source(src, "Unexpected lut type");
909 return NULL;
912 in_chan = read_u8(src, offset + 8);
913 out_chan = read_u8(src, offset + 9);
914 grid_points = read_u8(src, offset + 10);
916 clut_size = pow(grid_points, in_chan);
917 if (clut_size > MAX_CLUT_SIZE) {
918 invalid_source(src, "CLUT too large");
919 return NULL;
922 if (clut_size <= 0) {
923 invalid_source(src, "CLUT must not be empty.");
924 return NULL;
927 if (in_chan != 3 || out_chan != 3) {
928 invalid_source(src, "CLUT only supports RGB");
929 return NULL;
932 lut = malloc(sizeof(struct lutType) + (num_input_table_entries * in_chan + clut_size*out_chan + num_output_table_entries * out_chan)*sizeof(float));
933 if (!lut) {
934 invalid_source(src, "CLUT too large");
935 return NULL;
938 /* compute the offsets of tables */
939 lut->input_table = &lut->table_data[0];
940 lut->clut_table = &lut->table_data[in_chan*num_input_table_entries];
941 lut->output_table = &lut->table_data[in_chan*num_input_table_entries + clut_size*out_chan];
943 lut->num_input_table_entries = num_input_table_entries;
944 lut->num_output_table_entries = num_output_table_entries;
945 lut->num_input_channels = in_chan;
946 lut->num_output_channels = out_chan;
947 lut->num_clut_grid_points = grid_points;
948 lut->e00 = read_s15Fixed16Number(src, offset+12);
949 lut->e01 = read_s15Fixed16Number(src, offset+16);
950 lut->e02 = read_s15Fixed16Number(src, offset+20);
951 lut->e10 = read_s15Fixed16Number(src, offset+24);
952 lut->e11 = read_s15Fixed16Number(src, offset+28);
953 lut->e12 = read_s15Fixed16Number(src, offset+32);
954 lut->e20 = read_s15Fixed16Number(src, offset+36);
955 lut->e21 = read_s15Fixed16Number(src, offset+40);
956 lut->e22 = read_s15Fixed16Number(src, offset+44);
958 for (i = 0; i < lut->num_input_table_entries * in_chan; i++) {
959 if (type == LUT8_TYPE) {
960 lut->input_table[i] = uInt8Number_to_float(read_uInt8Number(src, offset + 52 + i * entry_size));
961 } else {
962 lut->input_table[i] = uInt16Number_to_float(read_uInt16Number(src, offset + 52 + i * entry_size));
966 clut_offset = offset + 52 + lut->num_input_table_entries * in_chan * entry_size;
967 for (i = 0; i < clut_size * out_chan; i+=3) {
968 if (type == LUT8_TYPE) {
969 lut->clut_table[i+0] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 0));
970 lut->clut_table[i+1] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 1));
971 lut->clut_table[i+2] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 2));
972 } else {
973 lut->clut_table[i+0] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 0));
974 lut->clut_table[i+1] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 2));
975 lut->clut_table[i+2] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 4));
979 output_offset = clut_offset + clut_size * out_chan * entry_size;
980 for (i = 0; i < lut->num_output_table_entries * out_chan; i++) {
981 if (type == LUT8_TYPE) {
982 lut->output_table[i] = uInt8Number_to_float(read_uInt8Number(src, output_offset + i*entry_size));
983 } else {
984 lut->output_table[i] = uInt16Number_to_float(read_uInt16Number(src, output_offset + i*entry_size));
988 return lut;
991 static void read_rendering_intent(qcms_profile *profile, struct mem_source *src)
993 profile->rendering_intent = read_u32(src, 64);
994 switch (profile->rendering_intent) {
995 case QCMS_INTENT_PERCEPTUAL:
996 case QCMS_INTENT_SATURATION:
997 case QCMS_INTENT_RELATIVE_COLORIMETRIC:
998 case QCMS_INTENT_ABSOLUTE_COLORIMETRIC:
999 break;
1000 default:
1001 invalid_source(src, "unknown rendering intent");
1005 qcms_profile *qcms_profile_create(void)
1007 return calloc(sizeof(qcms_profile), 1);
1012 /* build sRGB gamma table */
1013 /* based on cmsBuildParametricGamma() */
1014 static uint16_t *build_sRGB_gamma_table(int num_entries)
1016 int i;
1017 /* taken from lcms: Build_sRGBGamma() */
1018 double gamma = 2.4;
1019 double a = 1./1.055;
1020 double b = 0.055/1.055;
1021 double c = 1./12.92;
1022 double d = 0.04045;
1024 uint16_t *table = malloc(sizeof(uint16_t) * num_entries);
1025 if (!table)
1026 return NULL;
1028 for (i=0; i<num_entries; i++) {
1029 double x = (double)i / (num_entries-1);
1030 double y, output;
1031 // IEC 61966-2.1 (sRGB)
1032 // Y = (aX + b)^Gamma | X >= d
1033 // Y = cX | X < d
1034 if (x >= d) {
1035 double e = (a*x + b);
1036 if (e > 0)
1037 y = pow(e, gamma);
1038 else
1039 y = 0;
1040 } else {
1041 y = c*x;
1044 // Saturate -- this could likely move to a separate function
1045 output = y * 65535. + .5;
1046 if (output > 65535.)
1047 output = 65535;
1048 if (output < 0)
1049 output = 0;
1050 table[i] = (uint16_t)floor(output);
1052 return table;
1055 static struct curveType *curve_from_table(uint16_t *table, int num_entries)
1057 struct curveType *curve;
1058 int i;
1059 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entries);
1060 if (!curve)
1061 return NULL;
1062 curve->type = CURVE_TYPE;
1063 curve->count = num_entries;
1064 for (i = 0; i < num_entries; i++) {
1065 curve->data[i] = table[i];
1067 return curve;
1070 static uint16_t float_to_u8Fixed8Number(float a)
1072 if (a > (255.f + 255.f/256))
1073 return 0xffff;
1074 else if (a < 0.f)
1075 return 0;
1076 else
1077 return floor(a*256.f + .5f);
1080 static struct curveType *curve_from_gamma(float gamma)
1082 struct curveType *curve;
1083 int num_entries = 1;
1084 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entries);
1085 if (!curve)
1086 return NULL;
1087 curve->count = num_entries;
1088 curve->data[0] = float_to_u8Fixed8Number(gamma);
1089 return curve;
1093 //XXX: it would be nice if we had a way of ensuring
1094 // everything in a profile was initialized regardless of how it was created
1096 //XXX: should this also be taking a black_point?
1097 /* similar to CGColorSpaceCreateCalibratedRGB */
1098 qcms_profile* qcms_profile_create_rgb_with_gamma(
1099 qcms_CIE_xyY white_point,
1100 qcms_CIE_xyYTRIPLE primaries,
1101 float gamma)
1103 qcms_profile* profile = qcms_profile_create();
1104 if (!profile)
1105 return NO_MEM_PROFILE;
1107 //XXX: should store the whitepoint
1108 if (!set_rgb_colorants(profile, white_point, primaries)) {
1109 qcms_profile_release(profile);
1110 return INVALID_PROFILE;
1113 profile->redTRC = curve_from_gamma(gamma);
1114 profile->blueTRC = curve_from_gamma(gamma);
1115 profile->greenTRC = curve_from_gamma(gamma);
1117 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
1118 qcms_profile_release(profile);
1119 return NO_MEM_PROFILE;
1121 profile->class = DISPLAY_DEVICE_PROFILE;
1122 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
1123 profile->color_space = RGB_SIGNATURE;
1124 profile->pcs = XYZ_SIGNATURE;
1125 return profile;
1128 qcms_profile* qcms_profile_create_rgb_with_table(
1129 qcms_CIE_xyY white_point,
1130 qcms_CIE_xyYTRIPLE primaries,
1131 uint16_t *table, int num_entries)
1133 qcms_profile* profile = qcms_profile_create();
1134 if (!profile)
1135 return NO_MEM_PROFILE;
1137 //XXX: should store the whitepoint
1138 if (!set_rgb_colorants(profile, white_point, primaries)) {
1139 qcms_profile_release(profile);
1140 return INVALID_PROFILE;
1143 profile->redTRC = curve_from_table(table, num_entries);
1144 profile->blueTRC = curve_from_table(table, num_entries);
1145 profile->greenTRC = curve_from_table(table, num_entries);
1147 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
1148 qcms_profile_release(profile);
1149 return NO_MEM_PROFILE;
1151 profile->class = DISPLAY_DEVICE_PROFILE;
1152 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
1153 profile->color_space = RGB_SIGNATURE;
1154 profile->pcs = XYZ_SIGNATURE;
1155 return profile;
1158 /* from lcms: cmsWhitePointFromTemp */
1159 /* tempK must be >= 4000. and <= 25000.
1160 * Invalid values of tempK will return
1161 * (x,y,Y) = (-1.0, -1.0, -1.0)
1162 * similar to argyll: icx_DTEMP2XYZ() */
1163 static qcms_CIE_xyY white_point_from_temp(int temp_K)
1165 qcms_CIE_xyY white_point;
1166 double x, y;
1167 double T, T2, T3;
1168 // double M1, M2;
1170 // No optimization provided.
1171 T = temp_K;
1172 T2 = T*T; // Square
1173 T3 = T2*T; // Cube
1175 // For correlated color temperature (T) between 4000K and 7000K:
1176 if (T >= 4000. && T <= 7000.) {
1177 x = -4.6070*(1E9/T3) + 2.9678*(1E6/T2) + 0.09911*(1E3/T) + 0.244063;
1178 } else {
1179 // or for correlated color temperature (T) between 7000K and 25000K:
1180 if (T > 7000.0 && T <= 25000.0) {
1181 x = -2.0064*(1E9/T3) + 1.9018*(1E6/T2) + 0.24748*(1E3/T) + 0.237040;
1182 } else {
1183 // Invalid tempK
1184 white_point.x = -1.0;
1185 white_point.y = -1.0;
1186 white_point.Y = -1.0;
1188 assert(0 && "invalid temp");
1190 return white_point;
1194 // Obtain y(x)
1196 y = -3.000*(x*x) + 2.870*x - 0.275;
1198 // wave factors (not used, but here for futures extensions)
1200 // M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y);
1201 // M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y);
1203 // Fill white_point struct
1204 white_point.x = x;
1205 white_point.y = y;
1206 white_point.Y = 1.0;
1208 return white_point;
1211 qcms_profile* qcms_profile_sRGB(void)
1213 qcms_profile *profile;
1214 uint16_t *table;
1216 qcms_CIE_xyYTRIPLE Rec709Primaries = {
1217 {0.6400, 0.3300, 1.0},
1218 {0.3000, 0.6000, 1.0},
1219 {0.1500, 0.0600, 1.0}
1221 qcms_CIE_xyY D65;
1223 D65 = white_point_from_temp(6504);
1225 table = build_sRGB_gamma_table(1024);
1227 if (!table)
1228 return NO_MEM_PROFILE;
1230 profile = qcms_profile_create_rgb_with_table(D65, Rec709Primaries, table, 1024);
1231 if (profile)
1232 strcpy(profile->description, "sRGB IEC61966-2.1");
1234 free(table);
1235 return profile;
1238 /* qcms_profile_from_memory does not hold a reference to the memory passed in */
1239 qcms_profile* qcms_profile_from_memory(const void *mem, size_t size)
1241 uint32_t length;
1242 struct mem_source source;
1243 struct mem_source *src = &source;
1244 struct tag_index index;
1245 qcms_profile *profile;
1247 source.buf = mem;
1248 source.size = size;
1249 source.valid = true;
1251 if (size < 4)
1252 return INVALID_PROFILE;
1254 length = read_u32(src, 0);
1255 if (length <= size) {
1256 // shrink the area that we can read if appropriate
1257 source.size = length;
1258 } else {
1259 return INVALID_PROFILE;
1262 /* ensure that the profile size is sane so it's easier to reason about */
1263 if (source.size <= 64 || source.size >= MAX_PROFILE_SIZE)
1264 return INVALID_PROFILE;
1266 profile = qcms_profile_create();
1267 if (!profile)
1268 return NO_MEM_PROFILE;
1270 check_CMM_type_signature(src);
1271 check_profile_version(src);
1272 read_class_signature(profile, src);
1273 read_rendering_intent(profile, src);
1274 read_color_space(profile, src);
1275 read_pcs(profile, src);
1276 //TODO read rest of profile stuff
1278 if (!src->valid)
1279 goto invalid_profile;
1281 index = read_tag_table(profile, src);
1282 if (!src->valid || !index.tags)
1283 goto invalid_tag_table;
1285 if (!read_tag_descType(profile, src, index, TAG_desc))
1286 goto invalid_tag_table;
1287 #if defined(__APPLE__)
1288 if (!read_tag_dscmType(profile, src, index, TAG_dscm))
1289 goto invalid_tag_table;
1290 if (!read_tag_mmodType(profile, src, index, TAG_mmod))
1291 goto invalid_tag_table;
1292 #endif // __APPLE__
1294 if (find_tag(index, TAG_CHAD)) {
1295 profile->chromaticAdaption = read_tag_s15Fixed16ArrayType(src, index, TAG_CHAD);
1296 } else {
1297 profile->chromaticAdaption.invalid = true; //Signal the data is not present
1300 if (find_tag(index, TAG_vcgt)) {
1301 if (!read_tag_vcgtType(profile, src, index))
1302 goto invalid_tag_table;
1305 if (profile->class == DISPLAY_DEVICE_PROFILE || profile->class == INPUT_DEVICE_PROFILE ||
1306 profile->class == OUTPUT_DEVICE_PROFILE || profile->class == COLOR_SPACE_PROFILE) {
1307 if (profile->color_space == RGB_SIGNATURE) {
1308 if (find_tag(index, TAG_A2B0)) {
1309 if (read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT8_TYPE ||
1310 read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT16_TYPE) {
1311 profile->A2B0 = read_tag_lutType(src, index, TAG_A2B0);
1312 } else if (read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT_MAB_TYPE) {
1313 profile->mAB = read_tag_lutmABType(src, index, TAG_A2B0);
1316 if (find_tag(index, TAG_B2A0)) {
1317 if (read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT8_TYPE ||
1318 read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT16_TYPE) {
1319 profile->B2A0 = read_tag_lutType(src, index, TAG_B2A0);
1320 } else if (read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT_MBA_TYPE) {
1321 profile->mBA = read_tag_lutmABType(src, index, TAG_B2A0);
1324 if (find_tag(index, TAG_rXYZ) || !qcms_supports_iccv4) {
1325 profile->redColorant = read_tag_XYZType(src, index, TAG_rXYZ);
1326 profile->greenColorant = read_tag_XYZType(src, index, TAG_gXYZ);
1327 profile->blueColorant = read_tag_XYZType(src, index, TAG_bXYZ);
1330 if (!src->valid)
1331 goto invalid_tag_table;
1333 if (find_tag(index, TAG_rTRC) || !qcms_supports_iccv4) {
1334 profile->redTRC = read_tag_curveType(src, index, TAG_rTRC);
1335 profile->greenTRC = read_tag_curveType(src, index, TAG_gTRC);
1336 profile->blueTRC = read_tag_curveType(src, index, TAG_bTRC);
1338 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC)
1339 goto invalid_tag_table;
1341 } else if (profile->color_space == GRAY_SIGNATURE) {
1343 profile->grayTRC = read_tag_curveType(src, index, TAG_kTRC);
1344 if (!profile->grayTRC)
1345 goto invalid_tag_table;
1347 } else {
1348 assert(0 && "read_color_space protects against entering here");
1349 goto invalid_tag_table;
1351 } else {
1352 goto invalid_tag_table;
1355 if (!src->valid)
1356 goto invalid_tag_table;
1358 free(index.tags);
1359 return profile;
1361 invalid_tag_table:
1362 if (index.tags)
1363 free(index.tags);
1364 invalid_profile:
1365 qcms_profile_release(profile);
1366 return INVALID_PROFILE;
1369 qcms_bool qcms_profile_match(qcms_profile *p1, qcms_profile *p2)
1371 return memcmp(p1->description, p2->description, sizeof p1->description) == 0;
1374 const char* qcms_profile_get_description(qcms_profile *profile)
1376 return profile->description;
1379 qcms_intent qcms_profile_get_rendering_intent(qcms_profile *profile)
1381 return profile->rendering_intent;
1384 qcms_color_space qcms_profile_get_color_space(qcms_profile *profile)
1386 return profile->color_space;
1389 size_t qcms_profile_get_vcgt_channel_length(qcms_profile *profile)
1391 return profile->vcgt.length;
1394 // Check unsigned short is uint16_t.
1395 typedef char assert_short_not_16b[(sizeof(unsigned short) == sizeof(uint16_t)) ? 1 : -1];
1397 qcms_bool qcms_profile_get_vcgt_rgb_channels(qcms_profile *profile, unsigned short *data)
1399 size_t vcgt_channel_bytes = qcms_profile_get_vcgt_channel_length(profile) * sizeof(uint16_t);
1401 if (!vcgt_channel_bytes || !data)
1402 return false;
1404 memcpy(data, profile->vcgt.data, 3 * vcgt_channel_bytes);
1405 return true;
1408 static void lut_release(struct lutType *lut)
1410 free(lut);
1413 void qcms_profile_release(qcms_profile *profile)
1415 if (profile->output_table_r)
1416 precache_release(profile->output_table_r);
1417 if (profile->output_table_g)
1418 precache_release(profile->output_table_g);
1419 if (profile->output_table_b)
1420 precache_release(profile->output_table_b);
1422 if (profile->A2B0)
1423 lut_release(profile->A2B0);
1424 if (profile->B2A0)
1425 lut_release(profile->B2A0);
1427 if (profile->mAB)
1428 mAB_release(profile->mAB);
1429 if (profile->mBA)
1430 mAB_release(profile->mBA);
1432 if (profile->vcgt.data)
1433 free(profile->vcgt.data);
1435 free(profile->redTRC);
1436 free(profile->blueTRC);
1437 free(profile->greenTRC);
1438 free(profile->grayTRC);
1439 free(profile);
1442 #include <stdio.h>
1444 qcms_profile* qcms_profile_from_file(FILE *file)
1446 uint32_t length, remaining_length;
1447 qcms_profile *profile;
1448 size_t read_length;
1449 be32 length_be;
1450 void *data;
1452 if (fread(&length_be, 1, sizeof(length_be), file) != sizeof(length_be))
1453 return BAD_VALUE_PROFILE;
1455 length = be32_to_cpu(length_be);
1456 if (length > MAX_PROFILE_SIZE || length < sizeof(length_be))
1457 return BAD_VALUE_PROFILE;
1459 /* allocate room for the entire profile */
1460 data = malloc(length);
1461 if (!data)
1462 return NO_MEM_PROFILE;
1464 /* copy in length to the front so that the buffer will contain the entire profile */
1465 *((be32*)data) = length_be;
1466 remaining_length = length - sizeof(length_be);
1468 /* read the rest profile */
1469 read_length = fread((unsigned char*)data + sizeof(length_be), 1, remaining_length, file);
1470 if (read_length != remaining_length) {
1471 free(data);
1472 return INVALID_PROFILE;
1475 profile = qcms_profile_from_memory(data, length);
1476 free(data);
1477 return profile;
1480 qcms_profile* qcms_profile_from_path(const char *path)
1482 qcms_profile *profile = NULL;
1483 FILE *file = fopen(path, "rb");
1484 if (file) {
1485 profile = qcms_profile_from_file(file);
1486 fclose(file);
1488 return profile;
1491 #ifdef _WIN32
1492 /* Unicode path version */
1493 qcms_profile* qcms_profile_from_unicode_path(const wchar_t *path)
1495 qcms_profile *profile = NULL;
1496 FILE *file = _wfopen(path, L"rb");
1497 if (file) {
1498 profile = qcms_profile_from_file(file);
1499 fclose(file);
1501 return profile;
1503 #endif