Extend the enterprise policy for SSL overrides to the "danger" command
[chromium-blink-merge.git] / third_party / qcms / src / iccread.c
blob0deab10a3d06886e8253f520534423a902affd48
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
343 #define TAG_desc 0x64657363
344 #define TAG_vcgt 0x76636774
346 static struct tag *find_tag(struct tag_index index, uint32_t tag_id)
348 unsigned int i;
349 struct tag *tag = NULL;
350 for (i = 0; i < index.count; i++) {
351 if (index.tags[i].signature == tag_id) {
352 return &index.tags[i];
355 return tag;
358 #define DESC_TYPE 0x64657363 // 'desc'
359 #define MLUC_TYPE 0x6d6c7563 // 'mluc'
360 #define MMOD_TYPE 0x6D6D6F64 // 'mmod'
361 #define VCGT_TYPE 0x76636774 // 'vcgt'
363 // Check unsigned short is uint16_t.
364 typedef char assert_short_not_16b[(sizeof(unsigned short) == sizeof(uint16_t)) ? 1 : -1];
366 qcms_bool read_tag_vcgtType(qcms_profile *profile, struct mem_source *src, struct tag_index index) {
367 size_t tag_offset = find_tag(index, TAG_vcgt)->offset;
368 uint32_t tag_type = read_u32(src, tag_offset);
369 uint32_t vcgt_type = read_u32(src, tag_offset + 8);
370 uint16_t channels = read_u16(src, tag_offset + 12);
371 uint16_t elements = read_u16(src, tag_offset + 14);
372 uint16_t byte_depth = read_u16(src, tag_offset + 16);
373 size_t table_offset = tag_offset + 18;
374 uint32_t i;
375 uint16_t *dest;
377 if (!src->valid || tag_type != VCGT_TYPE)
378 goto invalid_vcgt_tag;
380 // Only support 3 channels.
381 if (channels != 3)
382 return true;
383 // Only support single or double byte values.
384 if (byte_depth != 1 && byte_depth != 2)
385 return true;
386 // Only support table data, not equation.
387 if (vcgt_type != 0)
388 return true;
389 // Limit the table to a sensible size; 10-bit gamma is a reasonable
390 // maximum for hardware correction.
391 if (elements > 1024)
392 return true;
394 // Empty table is invalid.
395 if (!elements)
396 goto invalid_vcgt_tag;
398 profile->vcgt.length = elements;
399 profile->vcgt.data = malloc(3 * elements * sizeof(uint16_t));
400 if (!profile->vcgt.data)
401 return false;
403 dest = profile->vcgt.data;
405 for (i = 0; i < 3 * elements; ++i) {
406 if (byte_depth == 1) {
407 *dest++ = read_u8(src, table_offset) * 256;
408 } else {
409 *dest++ = read_u16(src, table_offset);
412 table_offset += byte_depth;
414 if (!src->valid)
415 goto invalid_vcgt_tag;
418 return true;
420 invalid_vcgt_tag:
421 invalid_source(src, "invalid vcgt tag");
422 return false;
425 static bool read_tag_descType(qcms_profile *profile, struct mem_source *src, struct tag_index index, uint32_t tag_id)
427 struct tag *tag = find_tag(index, tag_id);
428 if (tag) {
429 const uint32_t limit = sizeof profile->description;
430 uint32_t offset = tag->offset;
431 uint32_t type = read_u32(src, offset);
432 uint32_t length = read_u32(src, offset+8);
433 uint32_t i, description_offset;
434 bool mluc = false;
435 if (length && type == MLUC_TYPE) {
436 length = read_u32(src, offset+20);
437 if (!length || (length & 1) || (read_u32(src, offset+12) != 12))
438 goto invalid_desc_tag;
439 description_offset = offset + read_u32(src, offset+24);
440 if (!src->valid)
441 goto invalid_desc_tag;
442 mluc = true;
443 } else if (length && type == DESC_TYPE) {
444 description_offset = offset + 12;
445 } else {
446 goto invalid_desc_tag;
448 if (length >= limit)
449 length = limit - 1;
450 for (i = 0; i < length; ++i) {
451 uint8_t value = read_u8(src, description_offset + i);
452 if (!src->valid)
453 goto invalid_desc_tag;
454 if (mluc && !value)
455 value = '.';
456 profile->description[i] = value;
458 profile->description[length] = 0;
459 } else {
460 goto invalid_desc_tag;
463 if (src->valid)
464 return true;
466 invalid_desc_tag:
467 invalid_source(src, "invalid description");
468 return false;
471 #if defined(__APPLE__)
473 // Use the dscm tag to change profile description "Display" to its more specific en-localized monitor name, if any.
475 #define TAG_dscm 0x6473636D // 'dscm'
477 static bool read_tag_dscmType(qcms_profile *profile, struct mem_source *src, struct tag_index index, uint32_t tag_id)
479 if (strcmp(profile->description, "Display") != 0)
480 return true;
482 struct tag *tag = find_tag(index, tag_id);
483 if (tag) {
484 uint32_t offset = tag->offset;
485 uint32_t type = read_u32(src, offset);
486 uint32_t records = read_u32(src, offset+8);
488 if (!src->valid || !records || type != MLUC_TYPE)
489 goto invalid_dscm_tag;
490 if (read_u32(src, offset+12) != 12) // MLUC record size: bytes
491 goto invalid_dscm_tag;
493 for (uint32_t i = 0; i < records; ++i) {
494 const uint32_t limit = sizeof profile->description;
495 const uint16_t isoen = 0x656E; // ISO-3166-1 language 'en'
497 uint16_t language = read_u16(src, offset + 16 + (i * 12) + 0);
498 uint32_t length = read_u32(src, offset + 16 + (i * 12) + 4);
499 uint32_t description_offset = read_u32(src, offset + 16 + (i * 12) + 8);
501 if (!src->valid || !length || (length & 1))
502 goto invalid_dscm_tag;
503 if (language != isoen)
504 continue;
506 // Use a prefix to identify the display description source
507 strcpy(profile->description, "dscm:");
508 length += 5;
510 if (length >= limit)
511 length = limit - 1;
512 for (uint32_t j = 5; j < length; ++j) {
513 uint8_t value = read_u8(src, offset + description_offset + j - 5);
514 if (!src->valid)
515 goto invalid_dscm_tag;
516 profile->description[j] = value ? value : '.';
518 profile->description[length] = 0;
519 break;
523 if (src->valid)
524 return true;
526 invalid_dscm_tag:
527 invalid_source(src, "invalid dscm tag");
528 return false;
531 // Use the mmod tag to change profile description "Display" to its specific mmod maker model data, if any.
533 #define TAG_mmod 0x6D6D6F64 // 'mmod'
535 static bool read_tag_mmodType(qcms_profile *profile, struct mem_source *src, struct tag_index index, uint32_t tag_id)
537 if (strcmp(profile->description, "Display") != 0)
538 return true;
540 struct tag *tag = find_tag(index, tag_id);
541 if (tag) {
542 const uint8_t length = 4 * 4; // Four 4-byte fields: 'mmod', 0, maker, model.
544 uint32_t offset = tag->offset;
545 if (tag->size < 40 || read_u32(src, offset) != MMOD_TYPE)
546 goto invalid_mmod_tag;
548 for (uint8_t i = 0; i < length; ++i) {
549 uint8_t value = read_u8(src, offset + i);
550 if (!src->valid)
551 goto invalid_mmod_tag;
552 profile->description[i] = value ? value : '.';
554 profile->description[length] = 0;
557 if (src->valid)
558 return true;
560 invalid_mmod_tag:
561 invalid_source(src, "invalid mmod tag");
562 return false;
565 #endif // __APPLE__
567 #define XYZ_TYPE 0x58595a20 // 'XYZ '
568 #define CURVE_TYPE 0x63757276 // 'curv'
569 #define PARAMETRIC_CURVE_TYPE 0x70617261 // 'para'
570 #define LUT16_TYPE 0x6d667432 // 'mft2'
571 #define LUT8_TYPE 0x6d667431 // 'mft1'
572 #define LUT_MAB_TYPE 0x6d414220 // 'mAB '
573 #define LUT_MBA_TYPE 0x6d424120 // 'mBA '
574 #define CHROMATIC_TYPE 0x73663332 // 'sf32'
576 static struct matrix read_tag_s15Fixed16ArrayType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
578 struct tag *tag = find_tag(index, tag_id);
579 struct matrix matrix;
580 if (tag) {
581 uint8_t i;
582 uint32_t offset = tag->offset;
583 uint32_t type = read_u32(src, offset);
585 // Check mandatory type signature for s16Fixed16ArrayType
586 if (type != CHROMATIC_TYPE) {
587 invalid_source(src, "unexpected type, expected 'sf32'");
590 for (i = 0; i < 9; i++) {
591 matrix.m[i/3][i%3] = s15Fixed16Number_to_float(read_s15Fixed16Number(src, offset+8+i*4));
593 matrix.invalid = false;
594 } else {
595 matrix.invalid = true;
596 invalid_source(src, "missing sf32tag");
598 return matrix;
601 static struct XYZNumber read_tag_XYZType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
603 struct XYZNumber num = {0, 0, 0};
604 struct tag *tag = find_tag(index, tag_id);
605 if (tag) {
606 uint32_t offset = tag->offset;
608 uint32_t type = read_u32(src, offset);
609 if (type != XYZ_TYPE)
610 invalid_source(src, "unexpected type, expected XYZ");
611 num.X = read_s15Fixed16Number(src, offset+8);
612 num.Y = read_s15Fixed16Number(src, offset+12);
613 num.Z = read_s15Fixed16Number(src, offset+16);
614 } else {
615 invalid_source(src, "missing xyztag");
617 return num;
620 // Read the tag at a given offset rather then the tag_index.
621 // This method is used when reading mAB tags where nested curveType are
622 // present that are not part of the tag_index.
623 static struct curveType *read_curveType(struct mem_source *src, uint32_t offset, uint32_t *len)
625 static const uint32_t COUNT_TO_LENGTH[5] = {1, 3, 4, 5, 7};
626 struct curveType *curve = NULL;
627 uint32_t type = read_u32(src, offset);
628 uint32_t count;
629 int i;
631 if (type != CURVE_TYPE && type != PARAMETRIC_CURVE_TYPE) {
632 invalid_source(src, "unexpected type, expected CURV or PARA");
633 return NULL;
636 if (type == CURVE_TYPE) {
637 count = read_u32(src, offset+8);
639 #define MAX_CURVE_ENTRIES 40000 //arbitrary
640 if (count > MAX_CURVE_ENTRIES) {
641 invalid_source(src, "curve size too large");
642 return NULL;
644 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*count);
645 if (!curve)
646 return NULL;
648 curve->count = count;
649 curve->type = type;
651 for (i=0; i<count; i++) {
652 curve->data[i] = read_u16(src, offset + 12 + i*2);
654 *len = 12 + count * 2;
655 } else { //PARAMETRIC_CURVE_TYPE
656 count = read_u16(src, offset+8);
658 if (count > 4) {
659 invalid_source(src, "parametric function type not supported.");
660 return NULL;
663 curve = malloc(sizeof(struct curveType));
664 if (!curve)
665 return NULL;
667 curve->count = count;
668 curve->type = type;
670 for (i=0; i < COUNT_TO_LENGTH[count]; i++) {
671 curve->parameter[i] = s15Fixed16Number_to_float(read_s15Fixed16Number(src, offset + 12 + i*4));
673 *len = 12 + COUNT_TO_LENGTH[count] * 4;
675 if ((count == 1 || count == 2)) {
676 /* we have a type 1 or type 2 function that has a division by 'a' */
677 float a = curve->parameter[1];
678 if (a == 0.f)
679 invalid_source(src, "parametricCurve definition causes division by zero.");
683 return curve;
686 static struct curveType *read_tag_curveType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
688 struct tag *tag = find_tag(index, tag_id);
689 struct curveType *curve = NULL;
690 if (tag) {
691 uint32_t len;
692 return read_curveType(src, tag->offset, &len);
693 } else {
694 invalid_source(src, "missing curvetag");
697 return curve;
700 #define MAX_CLUT_SIZE 500000 // arbitrary
701 #define MAX_CHANNELS 10 // arbitrary
702 static void read_nested_curveType(struct mem_source *src, struct curveType *(*curveArray)[MAX_CHANNELS], uint8_t num_channels, uint32_t curve_offset)
704 uint32_t channel_offset = 0;
705 int i;
706 for (i = 0; i < num_channels; i++) {
707 uint32_t tag_len = ~0;
709 (*curveArray)[i] = read_curveType(src, curve_offset + channel_offset, &tag_len);
710 if (!(*curveArray)[i]) {
711 invalid_source(src, "invalid nested curveType curve");
714 if (tag_len == ~0) {
715 invalid_source(src, "invalid nested curveType tag length");
716 return;
719 channel_offset += tag_len;
720 // 4 byte aligned
721 if ((tag_len % 4) != 0)
722 channel_offset += 4 - (tag_len % 4);
726 static void mAB_release(struct lutmABType *lut)
728 uint8_t i;
730 for (i = 0; i < lut->num_in_channels; i++){
731 free(lut->a_curves[i]);
733 for (i = 0; i < lut->num_out_channels; i++){
734 free(lut->b_curves[i]);
735 free(lut->m_curves[i]);
737 free(lut);
740 /* See section 10.10 for specs */
741 static struct lutmABType *read_tag_lutmABType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
743 struct tag *tag = find_tag(index, tag_id);
744 uint32_t offset = tag->offset;
745 uint32_t a_curve_offset, b_curve_offset, m_curve_offset;
746 uint32_t matrix_offset;
747 uint32_t clut_offset;
748 uint32_t clut_size = 1;
749 uint8_t clut_precision;
750 uint32_t type = read_u32(src, offset);
751 uint8_t num_in_channels, num_out_channels;
752 struct lutmABType *lut;
753 int i;
755 if (type != LUT_MAB_TYPE && type != LUT_MBA_TYPE) {
756 return NULL;
759 num_in_channels = read_u8(src, offset + 8);
760 num_out_channels = read_u8(src, offset + 8);
761 if (num_in_channels > MAX_CHANNELS || num_out_channels > MAX_CHANNELS)
762 return NULL;
764 // We require 3in/out channels since we only support RGB->XYZ (or RGB->LAB)
765 // XXX: If we remove this restriction make sure that the number of channels
766 // is less or equal to the maximum number of mAB curves in qcmsint.h
767 // also check for clut_size overflow. Also make sure it's != 0
768 if (num_in_channels != 3 || num_out_channels != 3)
769 return NULL;
771 // some of this data is optional and is denoted by a zero offset
772 // we also use this to track their existance
773 a_curve_offset = read_u32(src, offset + 28);
774 clut_offset = read_u32(src, offset + 24);
775 m_curve_offset = read_u32(src, offset + 20);
776 matrix_offset = read_u32(src, offset + 16);
777 b_curve_offset = read_u32(src, offset + 12);
779 // Convert offsets relative to the tag to relative to the profile
780 // preserve zero for optional fields
781 if (a_curve_offset)
782 a_curve_offset += offset;
783 if (clut_offset)
784 clut_offset += offset;
785 if (m_curve_offset)
786 m_curve_offset += offset;
787 if (matrix_offset)
788 matrix_offset += offset;
789 if (b_curve_offset)
790 b_curve_offset += offset;
792 if (clut_offset) {
793 assert (num_in_channels == 3);
794 // clut_size can not overflow since lg(256^num_in_channels) = 24 bits.
795 for (i = 0; i < num_in_channels; i++) {
796 clut_size *= read_u8(src, clut_offset + i);
797 if (clut_size == 0) {
798 invalid_source(src, "bad clut_size");
801 } else {
802 clut_size = 0;
805 // 24bits * 3 won't overflow either
806 clut_size = clut_size * num_out_channels;
808 if (clut_size > MAX_CLUT_SIZE)
809 return NULL;
811 lut = malloc(sizeof(struct lutmABType) + (clut_size) * sizeof(float));
812 if (!lut)
813 return NULL;
814 // we'll fill in the rest below
815 memset(lut, 0, sizeof(struct lutmABType));
816 lut->clut_table = &lut->clut_table_data[0];
818 for (i = 0; i < num_in_channels; i++) {
819 lut->num_grid_points[i] = read_u8(src, clut_offset + i);
820 if (lut->num_grid_points[i] == 0) {
821 invalid_source(src, "bad grid_points");
825 // Reverse the processing of transformation elements for mBA type.
826 lut->reversed = (type == LUT_MBA_TYPE);
828 lut->num_in_channels = num_in_channels;
829 lut->num_out_channels = num_out_channels;
831 if (matrix_offset) {
832 // read the matrix if we have it
833 lut->e00 = read_s15Fixed16Number(src, matrix_offset+4*0);
834 lut->e01 = read_s15Fixed16Number(src, matrix_offset+4*1);
835 lut->e02 = read_s15Fixed16Number(src, matrix_offset+4*2);
836 lut->e10 = read_s15Fixed16Number(src, matrix_offset+4*3);
837 lut->e11 = read_s15Fixed16Number(src, matrix_offset+4*4);
838 lut->e12 = read_s15Fixed16Number(src, matrix_offset+4*5);
839 lut->e20 = read_s15Fixed16Number(src, matrix_offset+4*6);
840 lut->e21 = read_s15Fixed16Number(src, matrix_offset+4*7);
841 lut->e22 = read_s15Fixed16Number(src, matrix_offset+4*8);
842 lut->e03 = read_s15Fixed16Number(src, matrix_offset+4*9);
843 lut->e13 = read_s15Fixed16Number(src, matrix_offset+4*10);
844 lut->e23 = read_s15Fixed16Number(src, matrix_offset+4*11);
847 if (a_curve_offset) {
848 read_nested_curveType(src, &lut->a_curves, num_in_channels, a_curve_offset);
850 if (m_curve_offset) {
851 read_nested_curveType(src, &lut->m_curves, num_out_channels, m_curve_offset);
853 if (b_curve_offset) {
854 read_nested_curveType(src, &lut->b_curves, num_out_channels, b_curve_offset);
855 } else {
856 invalid_source(src, "B curves required");
859 if (clut_offset) {
860 clut_precision = read_u8(src, clut_offset + 16);
861 if (clut_precision == 1) {
862 for (i = 0; i < clut_size; i++) {
863 lut->clut_table[i] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + 20 + i*1));
865 } else if (clut_precision == 2) {
866 for (i = 0; i < clut_size; i++) {
867 lut->clut_table[i] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + 20 + i*2));
869 } else {
870 invalid_source(src, "Invalid clut precision");
874 if (!src->valid) {
875 mAB_release(lut);
876 return NULL;
879 return lut;
882 static struct lutType *read_tag_lutType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
884 struct tag *tag = find_tag(index, tag_id);
885 uint32_t offset = tag->offset;
886 uint32_t type = read_u32(src, offset);
887 uint16_t num_input_table_entries;
888 uint16_t num_output_table_entries;
889 uint8_t in_chan, grid_points, out_chan;
890 size_t clut_offset, output_offset;
891 uint32_t clut_size;
892 size_t entry_size;
893 struct lutType *lut;
894 int i;
896 /* I'm not sure why the spec specifies a fixed number of entries for LUT8 tables even though
897 * they have room for the num_entries fields */
898 if (type == LUT8_TYPE) {
899 num_input_table_entries = 256;
900 num_output_table_entries = 256;
901 entry_size = 1;
902 } else if (type == LUT16_TYPE) {
903 num_input_table_entries = read_u16(src, offset + 48);
904 num_output_table_entries = read_u16(src, offset + 50);
905 if (num_input_table_entries == 0 || num_output_table_entries == 0) {
906 invalid_source(src, "Bad channel count");
907 return NULL;
909 entry_size = 2;
910 } else {
911 assert(0); // the caller checks that this doesn't happen
912 invalid_source(src, "Unexpected lut type");
913 return NULL;
916 in_chan = read_u8(src, offset + 8);
917 out_chan = read_u8(src, offset + 9);
918 grid_points = read_u8(src, offset + 10);
920 clut_size = pow(grid_points, in_chan);
921 if (clut_size > MAX_CLUT_SIZE) {
922 invalid_source(src, "CLUT too large");
923 return NULL;
926 if (in_chan != 3 || out_chan != 3) {
927 invalid_source(src, "CLUT only supports RGB");
928 return NULL;
931 lut = malloc(sizeof(struct lutType) + (num_input_table_entries * in_chan + clut_size*out_chan + num_output_table_entries * out_chan)*sizeof(float));
932 if (!lut) {
933 invalid_source(src, "CLUT too large");
934 return NULL;
937 /* compute the offsets of tables */
938 lut->input_table = &lut->table_data[0];
939 lut->clut_table = &lut->table_data[in_chan*num_input_table_entries];
940 lut->output_table = &lut->table_data[in_chan*num_input_table_entries + clut_size*out_chan];
942 lut->num_input_table_entries = num_input_table_entries;
943 lut->num_output_table_entries = num_output_table_entries;
944 lut->num_input_channels = in_chan;
945 lut->num_output_channels = out_chan;
946 lut->num_clut_grid_points = grid_points;
947 lut->e00 = read_s15Fixed16Number(src, offset+12);
948 lut->e01 = read_s15Fixed16Number(src, offset+16);
949 lut->e02 = read_s15Fixed16Number(src, offset+20);
950 lut->e10 = read_s15Fixed16Number(src, offset+24);
951 lut->e11 = read_s15Fixed16Number(src, offset+28);
952 lut->e12 = read_s15Fixed16Number(src, offset+32);
953 lut->e20 = read_s15Fixed16Number(src, offset+36);
954 lut->e21 = read_s15Fixed16Number(src, offset+40);
955 lut->e22 = read_s15Fixed16Number(src, offset+44);
957 for (i = 0; i < lut->num_input_table_entries * in_chan; i++) {
958 if (type == LUT8_TYPE) {
959 lut->input_table[i] = uInt8Number_to_float(read_uInt8Number(src, offset + 52 + i * entry_size));
960 } else {
961 lut->input_table[i] = uInt16Number_to_float(read_uInt16Number(src, offset + 52 + i * entry_size));
965 clut_offset = offset + 52 + lut->num_input_table_entries * in_chan * entry_size;
966 for (i = 0; i < clut_size * out_chan; i+=3) {
967 if (type == LUT8_TYPE) {
968 lut->clut_table[i+0] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 0));
969 lut->clut_table[i+1] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 1));
970 lut->clut_table[i+2] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 2));
971 } else {
972 lut->clut_table[i+0] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 0));
973 lut->clut_table[i+1] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 2));
974 lut->clut_table[i+2] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 4));
978 output_offset = clut_offset + clut_size * out_chan * entry_size;
979 for (i = 0; i < lut->num_output_table_entries * out_chan; i++) {
980 if (type == LUT8_TYPE) {
981 lut->output_table[i] = uInt8Number_to_float(read_uInt8Number(src, output_offset + i*entry_size));
982 } else {
983 lut->output_table[i] = uInt16Number_to_float(read_uInt16Number(src, output_offset + i*entry_size));
987 return lut;
990 static void read_rendering_intent(qcms_profile *profile, struct mem_source *src)
992 profile->rendering_intent = read_u32(src, 64);
993 switch (profile->rendering_intent) {
994 case QCMS_INTENT_PERCEPTUAL:
995 case QCMS_INTENT_SATURATION:
996 case QCMS_INTENT_RELATIVE_COLORIMETRIC:
997 case QCMS_INTENT_ABSOLUTE_COLORIMETRIC:
998 break;
999 default:
1000 invalid_source(src, "unknown rendering intent");
1004 qcms_profile *qcms_profile_create(void)
1006 return calloc(sizeof(qcms_profile), 1);
1011 /* build sRGB gamma table */
1012 /* based on cmsBuildParametricGamma() */
1013 static uint16_t *build_sRGB_gamma_table(int num_entries)
1015 int i;
1016 /* taken from lcms: Build_sRGBGamma() */
1017 double gamma = 2.4;
1018 double a = 1./1.055;
1019 double b = 0.055/1.055;
1020 double c = 1./12.92;
1021 double d = 0.04045;
1023 uint16_t *table = malloc(sizeof(uint16_t) * num_entries);
1024 if (!table)
1025 return NULL;
1027 for (i=0; i<num_entries; i++) {
1028 double x = (double)i / (num_entries-1);
1029 double y, output;
1030 // IEC 61966-2.1 (sRGB)
1031 // Y = (aX + b)^Gamma | X >= d
1032 // Y = cX | X < d
1033 if (x >= d) {
1034 double e = (a*x + b);
1035 if (e > 0)
1036 y = pow(e, gamma);
1037 else
1038 y = 0;
1039 } else {
1040 y = c*x;
1043 // Saturate -- this could likely move to a separate function
1044 output = y * 65535. + .5;
1045 if (output > 65535.)
1046 output = 65535;
1047 if (output < 0)
1048 output = 0;
1049 table[i] = (uint16_t)floor(output);
1051 return table;
1054 static struct curveType *curve_from_table(uint16_t *table, int num_entries)
1056 struct curveType *curve;
1057 int i;
1058 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entries);
1059 if (!curve)
1060 return NULL;
1061 curve->type = CURVE_TYPE;
1062 curve->count = num_entries;
1063 for (i = 0; i < num_entries; i++) {
1064 curve->data[i] = table[i];
1066 return curve;
1069 static uint16_t float_to_u8Fixed8Number(float a)
1071 if (a > (255.f + 255.f/256))
1072 return 0xffff;
1073 else if (a < 0.f)
1074 return 0;
1075 else
1076 return floor(a*256.f + .5f);
1079 static struct curveType *curve_from_gamma(float gamma)
1081 struct curveType *curve;
1082 int num_entries = 1;
1083 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entries);
1084 if (!curve)
1085 return NULL;
1086 curve->count = num_entries;
1087 curve->data[0] = float_to_u8Fixed8Number(gamma);
1088 return curve;
1092 //XXX: it would be nice if we had a way of ensuring
1093 // everything in a profile was initialized regardless of how it was created
1095 //XXX: should this also be taking a black_point?
1096 /* similar to CGColorSpaceCreateCalibratedRGB */
1097 qcms_profile* qcms_profile_create_rgb_with_gamma(
1098 qcms_CIE_xyY white_point,
1099 qcms_CIE_xyYTRIPLE primaries,
1100 float gamma)
1102 qcms_profile* profile = qcms_profile_create();
1103 if (!profile)
1104 return NO_MEM_PROFILE;
1106 //XXX: should store the whitepoint
1107 if (!set_rgb_colorants(profile, white_point, primaries)) {
1108 qcms_profile_release(profile);
1109 return INVALID_PROFILE;
1112 profile->redTRC = curve_from_gamma(gamma);
1113 profile->blueTRC = curve_from_gamma(gamma);
1114 profile->greenTRC = curve_from_gamma(gamma);
1116 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
1117 qcms_profile_release(profile);
1118 return NO_MEM_PROFILE;
1120 profile->class = DISPLAY_DEVICE_PROFILE;
1121 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
1122 profile->color_space = RGB_SIGNATURE;
1123 return profile;
1126 qcms_profile* qcms_profile_create_rgb_with_table(
1127 qcms_CIE_xyY white_point,
1128 qcms_CIE_xyYTRIPLE primaries,
1129 uint16_t *table, int num_entries)
1131 qcms_profile* profile = qcms_profile_create();
1132 if (!profile)
1133 return NO_MEM_PROFILE;
1135 //XXX: should store the whitepoint
1136 if (!set_rgb_colorants(profile, white_point, primaries)) {
1137 qcms_profile_release(profile);
1138 return INVALID_PROFILE;
1141 profile->redTRC = curve_from_table(table, num_entries);
1142 profile->blueTRC = curve_from_table(table, num_entries);
1143 profile->greenTRC = curve_from_table(table, num_entries);
1145 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
1146 qcms_profile_release(profile);
1147 return NO_MEM_PROFILE;
1149 profile->class = DISPLAY_DEVICE_PROFILE;
1150 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
1151 profile->color_space = RGB_SIGNATURE;
1152 return profile;
1155 /* from lcms: cmsWhitePointFromTemp */
1156 /* tempK must be >= 4000. and <= 25000.
1157 * similar to argyll: icx_DTEMP2XYZ() */
1158 static qcms_CIE_xyY white_point_from_temp(int temp_K)
1160 qcms_CIE_xyY white_point;
1161 double x, y;
1162 double T, T2, T3;
1163 // double M1, M2;
1165 // No optimization provided.
1166 T = temp_K;
1167 T2 = T*T; // Square
1168 T3 = T2*T; // Cube
1170 // For correlated color temperature (T) between 4000K and 7000K:
1171 if (T >= 4000. && T <= 7000.) {
1172 x = -4.6070*(1E9/T3) + 2.9678*(1E6/T2) + 0.09911*(1E3/T) + 0.244063;
1173 } else {
1174 // or for correlated color temperature (T) between 7000K and 25000K:
1175 if (T > 7000.0 && T <= 25000.0) {
1176 x = -2.0064*(1E9/T3) + 1.9018*(1E6/T2) + 0.24748*(1E3/T) + 0.237040;
1177 } else {
1178 assert(0 && "invalid temp");
1182 // Obtain y(x)
1184 y = -3.000*(x*x) + 2.870*x - 0.275;
1186 // wave factors (not used, but here for futures extensions)
1188 // M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y);
1189 // M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y);
1191 // Fill white_point struct
1192 white_point.x = x;
1193 white_point.y = y;
1194 white_point.Y = 1.0;
1196 return white_point;
1199 qcms_profile* qcms_profile_sRGB(void)
1201 qcms_profile *profile;
1202 uint16_t *table;
1204 qcms_CIE_xyYTRIPLE Rec709Primaries = {
1205 {0.6400, 0.3300, 1.0},
1206 {0.3000, 0.6000, 1.0},
1207 {0.1500, 0.0600, 1.0}
1209 qcms_CIE_xyY D65;
1211 D65 = white_point_from_temp(6504);
1213 table = build_sRGB_gamma_table(1024);
1215 if (!table)
1216 return NO_MEM_PROFILE;
1218 profile = qcms_profile_create_rgb_with_table(D65, Rec709Primaries, table, 1024);
1219 if (profile)
1220 strcpy(profile->description, "sRGB IEC61966-2.1");
1222 free(table);
1223 return profile;
1226 /* qcms_profile_from_memory does not hold a reference to the memory passed in */
1227 qcms_profile* qcms_profile_from_memory(const void *mem, size_t size)
1229 uint32_t length;
1230 struct mem_source source;
1231 struct mem_source *src = &source;
1232 struct tag_index index;
1233 qcms_profile *profile;
1235 source.buf = mem;
1236 source.size = size;
1237 source.valid = true;
1239 if (size < 4)
1240 return INVALID_PROFILE;
1242 length = read_u32(src, 0);
1243 if (length <= size) {
1244 // shrink the area that we can read if appropriate
1245 source.size = length;
1246 } else {
1247 return INVALID_PROFILE;
1250 /* ensure that the profile size is sane so it's easier to reason about */
1251 if (source.size <= 64 || source.size >= MAX_PROFILE_SIZE)
1252 return INVALID_PROFILE;
1254 profile = qcms_profile_create();
1255 if (!profile)
1256 return NO_MEM_PROFILE;
1258 check_CMM_type_signature(src);
1259 check_profile_version(src);
1260 read_class_signature(profile, src);
1261 read_rendering_intent(profile, src);
1262 read_color_space(profile, src);
1263 read_pcs(profile, src);
1264 //TODO read rest of profile stuff
1266 if (!src->valid)
1267 goto invalid_profile;
1269 index = read_tag_table(profile, src);
1270 if (!src->valid || !index.tags)
1271 goto invalid_tag_table;
1273 if (!read_tag_descType(profile, src, index, TAG_desc))
1274 goto invalid_tag_table;
1275 #if defined(__APPLE__)
1276 if (!read_tag_dscmType(profile, src, index, TAG_dscm))
1277 goto invalid_tag_table;
1278 if (!read_tag_mmodType(profile, src, index, TAG_mmod))
1279 goto invalid_tag_table;
1280 #endif // __APPLE__
1282 if (find_tag(index, TAG_CHAD)) {
1283 profile->chromaticAdaption = read_tag_s15Fixed16ArrayType(src, index, TAG_CHAD);
1284 } else {
1285 profile->chromaticAdaption.invalid = true; //Signal the data is not present
1288 if (find_tag(index, TAG_vcgt)) {
1289 if (!read_tag_vcgtType(profile, src, index))
1290 goto invalid_tag_table;
1293 if (profile->class == DISPLAY_DEVICE_PROFILE || profile->class == INPUT_DEVICE_PROFILE ||
1294 profile->class == OUTPUT_DEVICE_PROFILE || profile->class == COLOR_SPACE_PROFILE) {
1295 if (profile->color_space == RGB_SIGNATURE) {
1296 if (find_tag(index, TAG_A2B0)) {
1297 if (read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT8_TYPE ||
1298 read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT16_TYPE) {
1299 profile->A2B0 = read_tag_lutType(src, index, TAG_A2B0);
1300 } else if (read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT_MAB_TYPE) {
1301 profile->mAB = read_tag_lutmABType(src, index, TAG_A2B0);
1304 if (find_tag(index, TAG_B2A0)) {
1305 if (read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT8_TYPE ||
1306 read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT16_TYPE) {
1307 profile->B2A0 = read_tag_lutType(src, index, TAG_B2A0);
1308 } else if (read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT_MBA_TYPE) {
1309 profile->mBA = read_tag_lutmABType(src, index, TAG_B2A0);
1312 if (find_tag(index, TAG_rXYZ) || !qcms_supports_iccv4) {
1313 profile->redColorant = read_tag_XYZType(src, index, TAG_rXYZ);
1314 profile->greenColorant = read_tag_XYZType(src, index, TAG_gXYZ);
1315 profile->blueColorant = read_tag_XYZType(src, index, TAG_bXYZ);
1318 if (!src->valid)
1319 goto invalid_tag_table;
1321 if (find_tag(index, TAG_rTRC) || !qcms_supports_iccv4) {
1322 profile->redTRC = read_tag_curveType(src, index, TAG_rTRC);
1323 profile->greenTRC = read_tag_curveType(src, index, TAG_gTRC);
1324 profile->blueTRC = read_tag_curveType(src, index, TAG_bTRC);
1326 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC)
1327 goto invalid_tag_table;
1329 } else if (profile->color_space == GRAY_SIGNATURE) {
1331 profile->grayTRC = read_tag_curveType(src, index, TAG_kTRC);
1332 if (!profile->grayTRC)
1333 goto invalid_tag_table;
1335 } else {
1336 assert(0 && "read_color_space protects against entering here");
1337 goto invalid_tag_table;
1339 } else {
1340 goto invalid_tag_table;
1343 if (!src->valid)
1344 goto invalid_tag_table;
1346 free(index.tags);
1348 return profile;
1350 invalid_tag_table:
1351 free(index.tags);
1352 invalid_profile:
1353 qcms_profile_release(profile);
1354 return INVALID_PROFILE;
1357 qcms_bool qcms_profile_match(qcms_profile *p1, qcms_profile *p2)
1359 return memcmp(p1->description, p2->description, sizeof p1->description) == 0;
1362 const char* qcms_profile_get_description(qcms_profile *profile)
1364 return profile->description;
1367 qcms_intent qcms_profile_get_rendering_intent(qcms_profile *profile)
1369 return profile->rendering_intent;
1372 icColorSpaceSignature
1373 qcms_profile_get_color_space(qcms_profile *profile)
1375 return profile->color_space;
1378 static void lut_release(struct lutType *lut)
1380 free(lut);
1383 size_t qcms_profile_get_vcgt_channel_length(qcms_profile *profile) {
1384 return profile->vcgt.length;
1387 qcms_bool qcms_profile_get_vcgt_rgb_channels(qcms_profile *profile, unsigned short *data) {
1388 size_t vcgt_channel_bytes = qcms_profile_get_vcgt_channel_length(profile) * sizeof(uint16_t);
1389 if (!vcgt_channel_bytes || !data)
1390 return false;
1391 memcpy(data, profile->vcgt.data, 3 * vcgt_channel_bytes);
1392 return true;
1395 void qcms_profile_release(qcms_profile *profile)
1397 if (profile->output_table_r)
1398 precache_release(profile->output_table_r);
1399 if (profile->output_table_g)
1400 precache_release(profile->output_table_g);
1401 if (profile->output_table_b)
1402 precache_release(profile->output_table_b);
1404 if (profile->A2B0)
1405 lut_release(profile->A2B0);
1406 if (profile->B2A0)
1407 lut_release(profile->B2A0);
1409 if (profile->mAB)
1410 mAB_release(profile->mAB);
1411 if (profile->mBA)
1412 mAB_release(profile->mBA);
1414 if (profile->vcgt.data)
1415 free(profile->vcgt.data);
1417 free(profile->redTRC);
1418 free(profile->blueTRC);
1419 free(profile->greenTRC);
1420 free(profile->grayTRC);
1421 free(profile);
1425 #include <stdio.h>
1426 qcms_profile* qcms_profile_from_file(FILE *file)
1428 uint32_t length, remaining_length;
1429 qcms_profile *profile;
1430 size_t read_length;
1431 be32 length_be;
1432 void *data;
1434 if (fread(&length_be, 1, sizeof(length_be), file) != sizeof(length_be))
1435 return BAD_VALUE_PROFILE;
1437 length = be32_to_cpu(length_be);
1438 if (length > MAX_PROFILE_SIZE || length < sizeof(length_be))
1439 return BAD_VALUE_PROFILE;
1441 /* allocate room for the entire profile */
1442 data = malloc(length);
1443 if (!data)
1444 return NO_MEM_PROFILE;
1446 /* copy in length to the front so that the buffer will contain the entire profile */
1447 *((be32*)data) = length_be;
1448 remaining_length = length - sizeof(length_be);
1450 /* read the rest profile */
1451 read_length = fread((unsigned char*)data + sizeof(length_be), 1, remaining_length, file);
1452 if (read_length != remaining_length) {
1453 free(data);
1454 return INVALID_PROFILE;
1457 profile = qcms_profile_from_memory(data, length);
1458 free(data);
1459 return profile;
1462 qcms_profile* qcms_profile_from_path(const char *path)
1464 qcms_profile *profile = NULL;
1465 FILE *file = fopen(path, "rb");
1466 if (file) {
1467 profile = qcms_profile_from_file(file);
1468 fclose(file);
1470 return profile;
1473 #ifdef _WIN32
1474 /* Unicode path version */
1475 qcms_profile* qcms_profile_from_unicode_path(const wchar_t *path)
1477 qcms_profile *profile = NULL;
1478 FILE *file = _wfopen(path, L"rb");
1479 if (file) {
1480 profile = qcms_profile_from_file(file);
1481 fclose(file);
1483 return profile;
1485 #endif