Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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 static be32 cpu_to_be32(uint32_t v)
38 {
39 #ifdef IS_LITTLE_ENDIAN
40 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | ((v & 0xff000000) >> 24);
41 #else
42 return v;
43 #endif
44 }
46 static be16 cpu_to_be16(uint16_t v)
47 {
48 #ifdef IS_LITTLE_ENDIAN
49 return ((v & 0xff) << 8) | ((v & 0xff00) >> 8);
50 #else
51 return v;
52 #endif
53 }
55 static uint32_t be32_to_cpu(be32 v)
56 {
57 #ifdef IS_LITTLE_ENDIAN
58 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | ((v & 0xff000000) >> 24);
59 //return __builtin_bswap32(v);
60 #else
61 return v;
62 #endif
63 }
65 static uint16_t be16_to_cpu(be16 v)
66 {
67 #ifdef IS_LITTLE_ENDIAN
68 return ((v & 0xff) << 8) | ((v & 0xff00) >> 8);
69 #else
70 return v;
71 #endif
72 }
74 /* a wrapper around the memory that we are going to parse
75 * into a qcms_profile */
76 struct mem_source
77 {
78 const unsigned char *buf;
79 size_t size;
80 qcms_bool valid;
81 const char *invalid_reason;
82 };
84 static void invalid_source(struct mem_source *mem, const char *reason)
85 {
86 mem->valid = false;
87 mem->invalid_reason = reason;
88 }
90 static uint32_t read_u32(struct mem_source *mem, size_t offset)
91 {
92 /* Subtract from mem->size instead of the more intuitive adding to offset.
93 * This avoids overflowing offset. The subtraction is safe because
94 * mem->size is guaranteed to be > 4 */
95 if (offset > mem->size - 4) {
96 invalid_source(mem, "Invalid offset");
97 return 0;
98 } else {
99 be32 k;
100 memcpy(&k, mem->buf + offset, sizeof(k));
101 return be32_to_cpu(k);
102 }
103 }
105 static uint16_t read_u16(struct mem_source *mem, size_t offset)
106 {
107 if (offset > mem->size - 2) {
108 invalid_source(mem, "Invalid offset");
109 return 0;
110 } else {
111 be16 k;
112 memcpy(&k, mem->buf + offset, sizeof(k));
113 return be16_to_cpu(k);
114 }
115 }
117 static uint8_t read_u8(struct mem_source *mem, size_t offset)
118 {
119 if (offset > mem->size - 1) {
120 invalid_source(mem, "Invalid offset");
121 return 0;
122 } else {
123 return *(uint8_t*)(mem->buf + offset);
124 }
125 }
127 static s15Fixed16Number read_s15Fixed16Number(struct mem_source *mem, size_t offset)
128 {
129 return read_u32(mem, offset);
130 }
132 static uInt8Number read_uInt8Number(struct mem_source *mem, size_t offset)
133 {
134 return read_u8(mem, offset);
135 }
137 static uInt16Number read_uInt16Number(struct mem_source *mem, size_t offset)
138 {
139 return read_u16(mem, offset);
140 }
142 static void write_u32(void *mem, size_t offset, uint32_t value)
143 {
144 *((uint32_t *)((unsigned char*)mem + offset)) = cpu_to_be32(value);
145 }
147 static void write_u16(void *mem, size_t offset, uint16_t value)
148 {
149 *((uint16_t *)((unsigned char*)mem + offset)) = cpu_to_be16(value);
150 }
152 #define BAD_VALUE_PROFILE NULL
153 #define INVALID_PROFILE NULL
154 #define NO_MEM_PROFILE NULL
156 /* An arbitrary 4MB limit on profile size */
157 #define MAX_PROFILE_SIZE 1024*1024*4
158 #define MAX_TAG_COUNT 1024
160 static void check_CMM_type_signature(struct mem_source *src)
161 {
162 //uint32_t CMM_type_signature = read_u32(src, 4);
163 //TODO: do the check?
165 }
167 static void check_profile_version(struct mem_source *src)
168 {
170 /*
171 uint8_t major_revision = read_u8(src, 8 + 0);
172 uint8_t minor_revision = read_u8(src, 8 + 1);
173 */
174 uint8_t reserved1 = read_u8(src, 8 + 2);
175 uint8_t reserved2 = read_u8(src, 8 + 3);
176 /* Checking the version doesn't buy us anything
177 if (major_revision != 0x4) {
178 if (major_revision > 0x2)
179 invalid_source(src, "Unsupported major revision");
180 if (minor_revision > 0x40)
181 invalid_source(src, "Unsupported minor revision");
182 }
183 */
184 if (reserved1 != 0 || reserved2 != 0)
185 invalid_source(src, "Invalid reserved bytes");
186 }
188 #define INPUT_DEVICE_PROFILE 0x73636e72 // 'scnr'
189 #define DISPLAY_DEVICE_PROFILE 0x6d6e7472 // 'mntr'
190 #define OUTPUT_DEVICE_PROFILE 0x70727472 // 'prtr'
191 #define DEVICE_LINK_PROFILE 0x6c696e6b // 'link'
192 #define COLOR_SPACE_PROFILE 0x73706163 // 'spac'
193 #define ABSTRACT_PROFILE 0x61627374 // 'abst'
194 #define NAMED_COLOR_PROFILE 0x6e6d636c // 'nmcl'
196 static void read_class_signature(qcms_profile *profile, struct mem_source *mem)
197 {
198 profile->class = read_u32(mem, 12);
199 switch (profile->class) {
200 case DISPLAY_DEVICE_PROFILE:
201 case INPUT_DEVICE_PROFILE:
202 case OUTPUT_DEVICE_PROFILE:
203 case COLOR_SPACE_PROFILE:
204 break;
205 default:
206 invalid_source(mem, "Invalid Profile/Device Class signature");
207 }
208 }
210 static void read_color_space(qcms_profile *profile, struct mem_source *mem)
211 {
212 profile->color_space = read_u32(mem, 16);
213 switch (profile->color_space) {
214 case RGB_SIGNATURE:
215 case GRAY_SIGNATURE:
216 break;
217 default:
218 invalid_source(mem, "Unsupported colorspace");
219 }
220 }
222 static void read_pcs(qcms_profile *profile, struct mem_source *mem)
223 {
224 profile->pcs = read_u32(mem, 20);
225 switch (profile->pcs) {
226 case XYZ_SIGNATURE:
227 case LAB_SIGNATURE:
228 break;
229 default:
230 invalid_source(mem, "Unsupported pcs");
231 }
232 }
234 struct tag
235 {
236 uint32_t signature;
237 uint32_t offset;
238 uint32_t size;
239 };
241 struct tag_index {
242 uint32_t count;
243 struct tag *tags;
244 };
246 static struct tag_index read_tag_table(qcms_profile *profile, struct mem_source *mem)
247 {
248 struct tag_index index = {0, NULL};
249 unsigned int i;
251 index.count = read_u32(mem, 128);
252 if (index.count > MAX_TAG_COUNT) {
253 invalid_source(mem, "max number of tags exceeded");
254 return index;
255 }
257 index.tags = malloc(sizeof(struct tag)*index.count);
258 if (index.tags) {
259 for (i = 0; i < index.count; i++) {
260 index.tags[i].signature = read_u32(mem, 128 + 4 + 4*i*3);
261 index.tags[i].offset = read_u32(mem, 128 + 4 + 4*i*3 + 4);
262 index.tags[i].size = read_u32(mem, 128 + 4 + 4*i*3 + 8);
263 }
264 }
266 return index;
267 }
269 // Checks a profile for obvious inconsistencies and returns
270 // true if the profile looks bogus and should probably be
271 // ignored.
272 qcms_bool qcms_profile_is_bogus(qcms_profile *profile)
273 {
274 float sum[3], target[3], tolerance[3];
275 float rX, rY, rZ, gX, gY, gZ, bX, bY, bZ;
276 bool negative;
277 unsigned i;
279 // We currently only check the bogosity of RGB profiles
280 if (profile->color_space != RGB_SIGNATURE)
281 return false;
283 if (profile->A2B0 || profile->B2A0)
284 return false;
286 rX = s15Fixed16Number_to_float(profile->redColorant.X);
287 rY = s15Fixed16Number_to_float(profile->redColorant.Y);
288 rZ = s15Fixed16Number_to_float(profile->redColorant.Z);
290 gX = s15Fixed16Number_to_float(profile->greenColorant.X);
291 gY = s15Fixed16Number_to_float(profile->greenColorant.Y);
292 gZ = s15Fixed16Number_to_float(profile->greenColorant.Z);
294 bX = s15Fixed16Number_to_float(profile->blueColorant.X);
295 bY = s15Fixed16Number_to_float(profile->blueColorant.Y);
296 bZ = s15Fixed16Number_to_float(profile->blueColorant.Z);
298 // Check if any of the XYZ values are negative (see mozilla bug 498245)
299 // CIEXYZ tristimulus values cannot be negative according to the spec.
300 negative =
301 (rX < 0) || (rY < 0) || (rZ < 0) ||
302 (gX < 0) || (gY < 0) || (gZ < 0) ||
303 (bX < 0) || (bY < 0) || (bZ < 0);
305 if (negative)
306 return true;
309 // Sum the values; they should add up to something close to white
310 sum[0] = rX + gX + bX;
311 sum[1] = rY + gY + bY;
312 sum[2] = rZ + gZ + bZ;
314 // Build our target vector (see mozilla bug 460629)
315 target[0] = 0.96420;
316 target[1] = 1.00000;
317 target[2] = 0.82491;
319 // Our tolerance vector - Recommended by Chris Murphy based on
320 // conversion from the LAB space criterion of no more than 3 in any one
321 // channel. This is similar to, but slightly more tolerant than Adobe's
322 // criterion.
323 tolerance[0] = 0.02;
324 tolerance[1] = 0.02;
325 tolerance[2] = 0.04;
327 // Compare with our tolerance
328 for (i = 0; i < 3; ++i) {
329 if (!(((sum[i] - tolerance[i]) <= target[i]) &&
330 ((sum[i] + tolerance[i]) >= target[i])))
331 return true;
332 }
334 // All Good
335 return false;
336 }
338 #define TAG_bXYZ 0x6258595a
339 #define TAG_gXYZ 0x6758595a
340 #define TAG_rXYZ 0x7258595a
341 #define TAG_rTRC 0x72545243
342 #define TAG_bTRC 0x62545243
343 #define TAG_gTRC 0x67545243
344 #define TAG_kTRC 0x6b545243
345 #define TAG_A2B0 0x41324230
346 #define TAG_B2A0 0x42324130
347 #define TAG_CHAD 0x63686164
349 static struct tag *find_tag(struct tag_index index, uint32_t tag_id)
350 {
351 unsigned int i;
352 struct tag *tag = NULL;
353 for (i = 0; i < index.count; i++) {
354 if (index.tags[i].signature == tag_id) {
355 return &index.tags[i];
356 }
357 }
358 return tag;
359 }
361 #define XYZ_TYPE 0x58595a20 // 'XYZ '
362 #define CURVE_TYPE 0x63757276 // 'curv'
363 #define PARAMETRIC_CURVE_TYPE 0x70617261 // 'para'
364 #define LUT16_TYPE 0x6d667432 // 'mft2'
365 #define LUT8_TYPE 0x6d667431 // 'mft1'
366 #define LUT_MAB_TYPE 0x6d414220 // 'mAB '
367 #define LUT_MBA_TYPE 0x6d424120 // 'mBA '
368 #define CHROMATIC_TYPE 0x73663332 // 'sf32'
370 static struct matrix read_tag_s15Fixed16ArrayType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
371 {
372 struct tag *tag = find_tag(index, tag_id);
373 struct matrix matrix;
374 if (tag) {
375 uint8_t i;
376 uint32_t offset = tag->offset;
377 uint32_t type = read_u32(src, offset);
379 // Check mandatory type signature for s16Fixed16ArrayType
380 if (type != CHROMATIC_TYPE) {
381 invalid_source(src, "unexpected type, expected 'sf32'");
382 }
384 for (i = 0; i < 9; i++) {
385 matrix.m[i/3][i%3] = s15Fixed16Number_to_float(read_s15Fixed16Number(src, offset+8+i*4));
386 }
387 matrix.invalid = false;
388 } else {
389 matrix.invalid = true;
390 invalid_source(src, "missing sf32tag");
391 }
392 return matrix;
393 }
395 static struct XYZNumber read_tag_XYZType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
396 {
397 struct XYZNumber num = {0, 0, 0};
398 struct tag *tag = find_tag(index, tag_id);
399 if (tag) {
400 uint32_t offset = tag->offset;
402 uint32_t type = read_u32(src, offset);
403 if (type != XYZ_TYPE)
404 invalid_source(src, "unexpected type, expected XYZ");
405 num.X = read_s15Fixed16Number(src, offset+8);
406 num.Y = read_s15Fixed16Number(src, offset+12);
407 num.Z = read_s15Fixed16Number(src, offset+16);
408 } else {
409 invalid_source(src, "missing xyztag");
410 }
411 return num;
412 }
414 // Read the tag at a given offset rather then the tag_index.
415 // This method is used when reading mAB tags where nested curveType are
416 // present that are not part of the tag_index.
417 static struct curveType *read_curveType(struct mem_source *src, uint32_t offset, uint32_t *len)
418 {
419 static const uint32_t COUNT_TO_LENGTH[5] = {1, 3, 4, 5, 7};
420 struct curveType *curve = NULL;
421 uint32_t type = read_u32(src, offset);
422 uint32_t count;
423 uint32_t i;
425 if (type != CURVE_TYPE && type != PARAMETRIC_CURVE_TYPE) {
426 invalid_source(src, "unexpected type, expected CURV or PARA");
427 return NULL;
428 }
430 if (type == CURVE_TYPE) {
431 count = read_u32(src, offset+8);
433 #define MAX_CURVE_ENTRIES 40000 //arbitrary
434 if (count > MAX_CURVE_ENTRIES) {
435 invalid_source(src, "curve size too large");
436 return NULL;
437 }
438 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*count);
439 if (!curve)
440 return NULL;
442 curve->count = count;
443 curve->type = CURVE_TYPE;
445 for (i=0; i<count; i++) {
446 curve->data[i] = read_u16(src, offset + 12 + i*2);
447 }
448 *len = 12 + count * 2;
449 } else { //PARAMETRIC_CURVE_TYPE
450 count = read_u16(src, offset+8);
452 if (count > 4) {
453 invalid_source(src, "parametric function type not supported.");
454 return NULL;
455 }
457 curve = malloc(sizeof(struct curveType));
458 if (!curve)
459 return NULL;
461 curve->count = count;
462 curve->type = PARAMETRIC_CURVE_TYPE;
464 for (i=0; i < COUNT_TO_LENGTH[count]; i++) {
465 curve->parameter[i] = s15Fixed16Number_to_float(read_s15Fixed16Number(src, offset + 12 + i*4));
466 }
467 *len = 12 + COUNT_TO_LENGTH[count] * 4;
469 if ((count == 1 || count == 2)) {
470 /* we have a type 1 or type 2 function that has a division by 'a' */
471 float a = curve->parameter[1];
472 if (a == 0.f)
473 invalid_source(src, "parametricCurve definition causes division by zero.");
474 }
475 }
477 return curve;
478 }
480 static struct curveType *read_tag_curveType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
481 {
482 struct tag *tag = find_tag(index, tag_id);
483 struct curveType *curve = NULL;
484 if (tag) {
485 uint32_t len;
486 return read_curveType(src, tag->offset, &len);
487 } else {
488 invalid_source(src, "missing curvetag");
489 }
491 return curve;
492 }
494 #define MAX_CLUT_SIZE 500000 // arbitrary
495 #define MAX_CHANNELS 10 // arbitrary
496 static void read_nested_curveType(struct mem_source *src, struct curveType *(*curveArray)[MAX_CHANNELS], uint8_t num_channels, uint32_t curve_offset)
497 {
498 uint32_t channel_offset = 0;
499 int i;
500 for (i = 0; i < num_channels; i++) {
501 uint32_t tag_len;
503 (*curveArray)[i] = read_curveType(src, curve_offset + channel_offset, &tag_len);
504 if (!(*curveArray)[i]) {
505 invalid_source(src, "invalid nested curveType curve");
506 }
508 channel_offset += tag_len;
509 // 4 byte aligned
510 if ((tag_len % 4) != 0)
511 channel_offset += 4 - (tag_len % 4);
512 }
514 }
516 static void mAB_release(struct lutmABType *lut)
517 {
518 uint8_t i;
520 for (i = 0; i < lut->num_in_channels; i++){
521 free(lut->a_curves[i]);
522 }
523 for (i = 0; i < lut->num_out_channels; i++){
524 free(lut->b_curves[i]);
525 free(lut->m_curves[i]);
526 }
527 free(lut);
528 }
530 /* See section 10.10 for specs */
531 static struct lutmABType *read_tag_lutmABType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
532 {
533 struct tag *tag = find_tag(index, tag_id);
534 uint32_t offset = tag->offset;
535 uint32_t a_curve_offset, b_curve_offset, m_curve_offset;
536 uint32_t matrix_offset;
537 uint32_t clut_offset;
538 uint32_t clut_size = 1;
539 uint8_t clut_precision;
540 uint32_t type = read_u32(src, offset);
541 uint8_t num_in_channels, num_out_channels;
542 struct lutmABType *lut;
543 uint32_t i;
545 if (type != LUT_MAB_TYPE && type != LUT_MBA_TYPE) {
546 return NULL;
547 }
549 num_in_channels = read_u8(src, offset + 8);
550 num_out_channels = read_u8(src, offset + 8);
551 if (num_in_channels > MAX_CHANNELS || num_out_channels > MAX_CHANNELS)
552 return NULL;
554 // We require 3in/out channels since we only support RGB->XYZ (or RGB->LAB)
555 // XXX: If we remove this restriction make sure that the number of channels
556 // is less or equal to the maximum number of mAB curves in qcmsint.h
557 // also check for clut_size overflow.
558 if (num_in_channels != 3 || num_out_channels != 3)
559 return NULL;
561 // some of this data is optional and is denoted by a zero offset
562 // we also use this to track their existance
563 a_curve_offset = read_u32(src, offset + 28);
564 clut_offset = read_u32(src, offset + 24);
565 m_curve_offset = read_u32(src, offset + 20);
566 matrix_offset = read_u32(src, offset + 16);
567 b_curve_offset = read_u32(src, offset + 12);
569 // Convert offsets relative to the tag to relative to the profile
570 // preserve zero for optional fields
571 if (a_curve_offset)
572 a_curve_offset += offset;
573 if (clut_offset)
574 clut_offset += offset;
575 if (m_curve_offset)
576 m_curve_offset += offset;
577 if (matrix_offset)
578 matrix_offset += offset;
579 if (b_curve_offset)
580 b_curve_offset += offset;
582 if (clut_offset) {
583 assert (num_in_channels == 3);
584 // clut_size can not overflow since lg(256^num_in_channels) = 24 bits.
585 for (i = 0; i < num_in_channels; i++) {
586 clut_size *= read_u8(src, clut_offset + i);
587 }
588 } else {
589 clut_size = 0;
590 }
592 // 24bits * 3 won't overflow either
593 clut_size = clut_size * num_out_channels;
595 if (clut_size > MAX_CLUT_SIZE)
596 return NULL;
598 lut = malloc(sizeof(struct lutmABType) + (clut_size) * sizeof(float));
599 if (!lut)
600 return NULL;
601 // we'll fill in the rest below
602 memset(lut, 0, sizeof(struct lutmABType));
603 lut->clut_table = &lut->clut_table_data[0];
605 for (i = 0; i < num_in_channels; i++) {
606 lut->num_grid_points[i] = read_u8(src, clut_offset + i);
607 }
609 // Reverse the processing of transformation elements for mBA type.
610 lut->reversed = (type == LUT_MBA_TYPE);
612 lut->num_in_channels = num_in_channels;
613 lut->num_out_channels = num_out_channels;
615 if (matrix_offset) {
616 // read the matrix if we have it
617 lut->e00 = read_s15Fixed16Number(src, matrix_offset+4*0);
618 lut->e01 = read_s15Fixed16Number(src, matrix_offset+4*1);
619 lut->e02 = read_s15Fixed16Number(src, matrix_offset+4*2);
620 lut->e10 = read_s15Fixed16Number(src, matrix_offset+4*3);
621 lut->e11 = read_s15Fixed16Number(src, matrix_offset+4*4);
622 lut->e12 = read_s15Fixed16Number(src, matrix_offset+4*5);
623 lut->e20 = read_s15Fixed16Number(src, matrix_offset+4*6);
624 lut->e21 = read_s15Fixed16Number(src, matrix_offset+4*7);
625 lut->e22 = read_s15Fixed16Number(src, matrix_offset+4*8);
626 lut->e03 = read_s15Fixed16Number(src, matrix_offset+4*9);
627 lut->e13 = read_s15Fixed16Number(src, matrix_offset+4*10);
628 lut->e23 = read_s15Fixed16Number(src, matrix_offset+4*11);
629 }
631 if (a_curve_offset) {
632 read_nested_curveType(src, &lut->a_curves, num_in_channels, a_curve_offset);
633 }
634 if (m_curve_offset) {
635 read_nested_curveType(src, &lut->m_curves, num_out_channels, m_curve_offset);
636 }
637 if (b_curve_offset) {
638 read_nested_curveType(src, &lut->b_curves, num_out_channels, b_curve_offset);
639 } else {
640 invalid_source(src, "B curves required");
641 }
643 if (clut_offset) {
644 clut_precision = read_u8(src, clut_offset + 16);
645 if (clut_precision == 1) {
646 for (i = 0; i < clut_size; i++) {
647 lut->clut_table[i] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + 20 + i*1));
648 }
649 } else if (clut_precision == 2) {
650 for (i = 0; i < clut_size; i++) {
651 lut->clut_table[i] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + 20 + i*2));
652 }
653 } else {
654 invalid_source(src, "Invalid clut precision");
655 }
656 }
658 if (!src->valid) {
659 mAB_release(lut);
660 return NULL;
661 }
663 return lut;
664 }
666 static struct lutType *read_tag_lutType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
667 {
668 struct tag *tag = find_tag(index, tag_id);
669 uint32_t offset = tag->offset;
670 uint32_t type = read_u32(src, offset);
671 uint16_t num_input_table_entries;
672 uint16_t num_output_table_entries;
673 uint8_t in_chan, grid_points, out_chan;
674 uint32_t clut_offset, output_offset;
675 uint32_t clut_size;
676 size_t entry_size;
677 struct lutType *lut;
678 uint32_t i;
680 /* I'm not sure why the spec specifies a fixed number of entries for LUT8 tables even though
681 * they have room for the num_entries fields */
682 if (type == LUT8_TYPE) {
683 num_input_table_entries = 256;
684 num_output_table_entries = 256;
685 entry_size = 1;
686 } else if (type == LUT16_TYPE) {
687 num_input_table_entries = read_u16(src, offset + 48);
688 num_output_table_entries = read_u16(src, offset + 50);
689 entry_size = 2;
690 } else {
691 assert(0); // the caller checks that this doesn't happen
692 invalid_source(src, "Unexpected lut type");
693 return NULL;
694 }
696 in_chan = read_u8(src, offset + 8);
697 out_chan = read_u8(src, offset + 9);
698 grid_points = read_u8(src, offset + 10);
700 clut_size = pow(grid_points, in_chan);
701 if (clut_size > MAX_CLUT_SIZE) {
702 return NULL;
703 }
705 if (in_chan != 3 || out_chan != 3) {
706 return NULL;
707 }
709 lut = malloc(sizeof(struct lutType) + (num_input_table_entries * in_chan + clut_size*out_chan + num_output_table_entries * out_chan)*sizeof(float));
710 if (!lut) {
711 return NULL;
712 }
714 /* compute the offsets of tables */
715 lut->input_table = &lut->table_data[0];
716 lut->clut_table = &lut->table_data[in_chan*num_input_table_entries];
717 lut->output_table = &lut->table_data[in_chan*num_input_table_entries + clut_size*out_chan];
719 lut->num_input_table_entries = num_input_table_entries;
720 lut->num_output_table_entries = num_output_table_entries;
721 lut->num_input_channels = read_u8(src, offset + 8);
722 lut->num_output_channels = read_u8(src, offset + 9);
723 lut->num_clut_grid_points = read_u8(src, offset + 10);
724 lut->e00 = read_s15Fixed16Number(src, offset+12);
725 lut->e01 = read_s15Fixed16Number(src, offset+16);
726 lut->e02 = read_s15Fixed16Number(src, offset+20);
727 lut->e10 = read_s15Fixed16Number(src, offset+24);
728 lut->e11 = read_s15Fixed16Number(src, offset+28);
729 lut->e12 = read_s15Fixed16Number(src, offset+32);
730 lut->e20 = read_s15Fixed16Number(src, offset+36);
731 lut->e21 = read_s15Fixed16Number(src, offset+40);
732 lut->e22 = read_s15Fixed16Number(src, offset+44);
734 for (i = 0; i < lut->num_input_table_entries * in_chan; i++) {
735 if (type == LUT8_TYPE) {
736 lut->input_table[i] = uInt8Number_to_float(read_uInt8Number(src, offset + 52 + i * entry_size));
737 } else {
738 lut->input_table[i] = uInt16Number_to_float(read_uInt16Number(src, offset + 52 + i * entry_size));
739 }
740 }
742 clut_offset = offset + 52 + lut->num_input_table_entries * in_chan * entry_size;
743 for (i = 0; i < clut_size * out_chan; i+=3) {
744 if (type == LUT8_TYPE) {
745 lut->clut_table[i+0] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 0));
746 lut->clut_table[i+1] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 1));
747 lut->clut_table[i+2] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 2));
748 } else {
749 lut->clut_table[i+0] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 0));
750 lut->clut_table[i+1] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 2));
751 lut->clut_table[i+2] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 4));
752 }
753 }
755 output_offset = clut_offset + clut_size * out_chan * entry_size;
756 for (i = 0; i < lut->num_output_table_entries * out_chan; i++) {
757 if (type == LUT8_TYPE) {
758 lut->output_table[i] = uInt8Number_to_float(read_uInt8Number(src, output_offset + i*entry_size));
759 } else {
760 lut->output_table[i] = uInt16Number_to_float(read_uInt16Number(src, output_offset + i*entry_size));
761 }
762 }
764 return lut;
765 }
767 static void read_rendering_intent(qcms_profile *profile, struct mem_source *src)
768 {
769 profile->rendering_intent = read_u32(src, 64);
770 switch (profile->rendering_intent) {
771 case QCMS_INTENT_PERCEPTUAL:
772 case QCMS_INTENT_SATURATION:
773 case QCMS_INTENT_RELATIVE_COLORIMETRIC:
774 case QCMS_INTENT_ABSOLUTE_COLORIMETRIC:
775 break;
776 default:
777 invalid_source(src, "unknown rendering intent");
778 }
779 }
781 qcms_profile *qcms_profile_create(void)
782 {
783 return calloc(sizeof(qcms_profile), 1);
784 }
788 /* build sRGB gamma table */
789 /* based on cmsBuildParametricGamma() */
790 static uint16_t *build_sRGB_gamma_table(int num_entries)
791 {
792 int i;
793 /* taken from lcms: Build_sRGBGamma() */
794 double gamma = 2.4;
795 double a = 1./1.055;
796 double b = 0.055/1.055;
797 double c = 1./12.92;
798 double d = 0.04045;
800 uint16_t *table = malloc(sizeof(uint16_t) * num_entries);
801 if (!table)
802 return NULL;
804 for (i=0; i<num_entries; i++) {
805 double x = (double)i / (num_entries-1);
806 double y, output;
807 // IEC 61966-2.1 (sRGB)
808 // Y = (aX + b)^Gamma | X >= d
809 // Y = cX | X < d
810 if (x >= d) {
811 double e = (a*x + b);
812 if (e > 0)
813 y = pow(e, gamma);
814 else
815 y = 0;
816 } else {
817 y = c*x;
818 }
820 // Saturate -- this could likely move to a separate function
821 output = y * 65535. + .5;
822 if (output > 65535.)
823 output = 65535;
824 if (output < 0)
825 output = 0;
826 table[i] = (uint16_t)floor(output);
827 }
828 return table;
829 }
831 static struct curveType *curve_from_table(uint16_t *table, int num_entries)
832 {
833 struct curveType *curve;
834 int i;
835 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entries);
836 if (!curve)
837 return NULL;
838 curve->type = CURVE_TYPE;
839 curve->count = num_entries;
840 for (i = 0; i < num_entries; i++) {
841 curve->data[i] = table[i];
842 }
843 return curve;
844 }
846 static uint16_t float_to_u8Fixed8Number(float a)
847 {
848 if (a > (255.f + 255.f/256))
849 return 0xffff;
850 else if (a < 0.f)
851 return 0;
852 else
853 return floorf(a*256.f + .5f);
854 }
856 static struct curveType *curve_from_gamma(float gamma)
857 {
858 struct curveType *curve;
859 int num_entries = 1;
860 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entries);
861 if (!curve)
862 return NULL;
863 curve->count = num_entries;
864 curve->data[0] = float_to_u8Fixed8Number(gamma);
865 curve->type = CURVE_TYPE;
866 return curve;
867 }
869 //XXX: it would be nice if we had a way of ensuring
870 // everything in a profile was initialized regardless of how it was created
872 //XXX: should this also be taking a black_point?
873 /* similar to CGColorSpaceCreateCalibratedRGB */
874 qcms_profile* qcms_profile_create_rgb_with_gamma(
875 qcms_CIE_xyY white_point,
876 qcms_CIE_xyYTRIPLE primaries,
877 float gamma)
878 {
879 qcms_profile* profile = qcms_profile_create();
880 if (!profile)
881 return NO_MEM_PROFILE;
883 //XXX: should store the whitepoint
884 if (!set_rgb_colorants(profile, white_point, primaries)) {
885 qcms_profile_release(profile);
886 return INVALID_PROFILE;
887 }
889 profile->redTRC = curve_from_gamma(gamma);
890 profile->blueTRC = curve_from_gamma(gamma);
891 profile->greenTRC = curve_from_gamma(gamma);
893 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
894 qcms_profile_release(profile);
895 return NO_MEM_PROFILE;
896 }
897 profile->class = DISPLAY_DEVICE_PROFILE;
898 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
899 profile->color_space = RGB_SIGNATURE;
900 return profile;
901 }
903 qcms_profile* qcms_profile_create_rgb_with_table(
904 qcms_CIE_xyY white_point,
905 qcms_CIE_xyYTRIPLE primaries,
906 uint16_t *table, int num_entries)
907 {
908 qcms_profile* profile = qcms_profile_create();
909 if (!profile)
910 return NO_MEM_PROFILE;
912 //XXX: should store the whitepoint
913 if (!set_rgb_colorants(profile, white_point, primaries)) {
914 qcms_profile_release(profile);
915 return INVALID_PROFILE;
916 }
918 profile->redTRC = curve_from_table(table, num_entries);
919 profile->blueTRC = curve_from_table(table, num_entries);
920 profile->greenTRC = curve_from_table(table, num_entries);
922 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
923 qcms_profile_release(profile);
924 return NO_MEM_PROFILE;
925 }
926 profile->class = DISPLAY_DEVICE_PROFILE;
927 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
928 profile->color_space = RGB_SIGNATURE;
929 return profile;
930 }
932 /* from lcms: cmsWhitePointFromTemp */
933 /* tempK must be >= 4000. and <= 25000.
934 * Invalid values of tempK will return
935 * (x,y,Y) = (-1.0, -1.0, -1.0)
936 * similar to argyll: icx_DTEMP2XYZ() */
937 static qcms_CIE_xyY white_point_from_temp(int temp_K)
938 {
939 qcms_CIE_xyY white_point;
940 double x, y;
941 double T, T2, T3;
942 // double M1, M2;
944 // No optimization provided.
945 T = temp_K;
946 T2 = T*T; // Square
947 T3 = T2*T; // Cube
949 // For correlated color temperature (T) between 4000K and 7000K:
950 if (T >= 4000. && T <= 7000.) {
951 x = -4.6070*(1E9/T3) + 2.9678*(1E6/T2) + 0.09911*(1E3/T) + 0.244063;
952 } else {
953 // or for correlated color temperature (T) between 7000K and 25000K:
954 if (T > 7000.0 && T <= 25000.0) {
955 x = -2.0064*(1E9/T3) + 1.9018*(1E6/T2) + 0.24748*(1E3/T) + 0.237040;
956 } else {
957 // Invalid tempK
958 white_point.x = -1.0;
959 white_point.y = -1.0;
960 white_point.Y = -1.0;
962 assert(0 && "invalid temp");
964 return white_point;
965 }
966 }
968 // Obtain y(x)
970 y = -3.000*(x*x) + 2.870*x - 0.275;
972 // wave factors (not used, but here for futures extensions)
974 // M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y);
975 // M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y);
977 // Fill white_point struct
978 white_point.x = x;
979 white_point.y = y;
980 white_point.Y = 1.0;
982 return white_point;
983 }
985 qcms_profile* qcms_profile_sRGB(void)
986 {
987 qcms_profile *profile;
988 uint16_t *table;
990 qcms_CIE_xyYTRIPLE Rec709Primaries = {
991 {0.6400, 0.3300, 1.0},
992 {0.3000, 0.6000, 1.0},
993 {0.1500, 0.0600, 1.0}
994 };
995 qcms_CIE_xyY D65;
997 D65 = white_point_from_temp(6504);
999 table = build_sRGB_gamma_table(1024);
1001 if (!table)
1002 return NO_MEM_PROFILE;
1004 profile = qcms_profile_create_rgb_with_table(D65, Rec709Primaries, table, 1024);
1005 free(table);
1006 return profile;
1007 }
1010 /* qcms_profile_from_memory does not hold a reference to the memory passed in */
1011 qcms_profile* qcms_profile_from_memory(const void *mem, size_t size)
1012 {
1013 uint32_t length;
1014 struct mem_source source;
1015 struct mem_source *src = &source;
1016 struct tag_index index;
1017 qcms_profile *profile;
1019 source.buf = mem;
1020 source.size = size;
1021 source.valid = true;
1023 if (size < 4)
1024 return INVALID_PROFILE;
1026 length = read_u32(src, 0);
1027 if (length <= size) {
1028 // shrink the area that we can read if appropriate
1029 source.size = length;
1030 } else {
1031 return INVALID_PROFILE;
1032 }
1034 /* ensure that the profile size is sane so it's easier to reason about */
1035 if (source.size <= 64 || source.size >= MAX_PROFILE_SIZE)
1036 return INVALID_PROFILE;
1038 profile = qcms_profile_create();
1039 if (!profile)
1040 return NO_MEM_PROFILE;
1042 check_CMM_type_signature(src);
1043 check_profile_version(src);
1044 read_class_signature(profile, src);
1045 read_rendering_intent(profile, src);
1046 read_color_space(profile, src);
1047 read_pcs(profile, src);
1048 //TODO read rest of profile stuff
1050 if (!src->valid)
1051 goto invalid_profile;
1053 index = read_tag_table(profile, src);
1054 if (!src->valid || !index.tags)
1055 goto invalid_tag_table;
1057 if (find_tag(index, TAG_CHAD)) {
1058 profile->chromaticAdaption = read_tag_s15Fixed16ArrayType(src, index, TAG_CHAD);
1059 } else {
1060 profile->chromaticAdaption.invalid = true; //Signal the data is not present
1061 }
1063 if (profile->class == DISPLAY_DEVICE_PROFILE || profile->class == INPUT_DEVICE_PROFILE ||
1064 profile->class == OUTPUT_DEVICE_PROFILE || profile->class == COLOR_SPACE_PROFILE) {
1065 if (profile->color_space == RGB_SIGNATURE) {
1066 if (find_tag(index, TAG_A2B0)) {
1067 if (read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT8_TYPE ||
1068 read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT16_TYPE) {
1069 profile->A2B0 = read_tag_lutType(src, index, TAG_A2B0);
1070 } else if (read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT_MAB_TYPE) {
1071 profile->mAB = read_tag_lutmABType(src, index, TAG_A2B0);
1072 }
1073 }
1074 if (find_tag(index, TAG_B2A0)) {
1075 if (read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT8_TYPE ||
1076 read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT16_TYPE) {
1077 profile->B2A0 = read_tag_lutType(src, index, TAG_B2A0);
1078 } else if (read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT_MBA_TYPE) {
1079 profile->mBA = read_tag_lutmABType(src, index, TAG_B2A0);
1080 }
1081 }
1082 if (find_tag(index, TAG_rXYZ) || !qcms_supports_iccv4) {
1083 profile->redColorant = read_tag_XYZType(src, index, TAG_rXYZ);
1084 profile->greenColorant = read_tag_XYZType(src, index, TAG_gXYZ);
1085 profile->blueColorant = read_tag_XYZType(src, index, TAG_bXYZ);
1086 }
1088 if (!src->valid)
1089 goto invalid_tag_table;
1091 if (find_tag(index, TAG_rTRC) || !qcms_supports_iccv4) {
1092 profile->redTRC = read_tag_curveType(src, index, TAG_rTRC);
1093 profile->greenTRC = read_tag_curveType(src, index, TAG_gTRC);
1094 profile->blueTRC = read_tag_curveType(src, index, TAG_bTRC);
1096 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC)
1097 goto invalid_tag_table;
1098 }
1099 } else if (profile->color_space == GRAY_SIGNATURE) {
1101 profile->grayTRC = read_tag_curveType(src, index, TAG_kTRC);
1102 if (!profile->grayTRC)
1103 goto invalid_tag_table;
1105 } else {
1106 assert(0 && "read_color_space protects against entering here");
1107 goto invalid_tag_table;
1108 }
1109 } else {
1110 goto invalid_tag_table;
1111 }
1113 if (!src->valid)
1114 goto invalid_tag_table;
1116 free(index.tags);
1118 return profile;
1120 invalid_tag_table:
1121 free(index.tags);
1122 invalid_profile:
1123 qcms_profile_release(profile);
1124 return INVALID_PROFILE;
1125 }
1127 qcms_intent qcms_profile_get_rendering_intent(qcms_profile *profile)
1128 {
1129 return profile->rendering_intent;
1130 }
1132 icColorSpaceSignature
1133 qcms_profile_get_color_space(qcms_profile *profile)
1134 {
1135 return profile->color_space;
1136 }
1138 static void lut_release(struct lutType *lut)
1139 {
1140 free(lut);
1141 }
1143 void qcms_profile_release(qcms_profile *profile)
1144 {
1145 if (profile->output_table_r)
1146 precache_release(profile->output_table_r);
1147 if (profile->output_table_g)
1148 precache_release(profile->output_table_g);
1149 if (profile->output_table_b)
1150 precache_release(profile->output_table_b);
1152 if (profile->A2B0)
1153 lut_release(profile->A2B0);
1154 if (profile->B2A0)
1155 lut_release(profile->B2A0);
1157 if (profile->mAB)
1158 mAB_release(profile->mAB);
1159 if (profile->mBA)
1160 mAB_release(profile->mBA);
1162 free(profile->redTRC);
1163 free(profile->blueTRC);
1164 free(profile->greenTRC);
1165 free(profile->grayTRC);
1166 free(profile);
1167 }
1170 #include <stdio.h>
1171 static void qcms_data_from_file(FILE *file, void **mem, size_t *size)
1172 {
1173 uint32_t length, remaining_length;
1174 size_t read_length;
1175 be32 length_be;
1176 void *data;
1178 *mem = NULL;
1179 *size = 0;
1181 if (fread(&length_be, 1, sizeof(length_be), file) != sizeof(length_be))
1182 return;
1184 length = be32_to_cpu(length_be);
1185 if (length > MAX_PROFILE_SIZE || length < sizeof(length_be))
1186 return;
1188 /* allocate room for the entire profile */
1189 data = malloc(length);
1190 if (!data)
1191 return;
1193 /* copy in length to the front so that the buffer will contain the entire profile */
1194 *((be32*)data) = length_be;
1195 remaining_length = length - sizeof(length_be);
1197 /* read the rest profile */
1198 read_length = fread((unsigned char*)data + sizeof(length_be), 1, remaining_length, file);
1199 if (read_length != remaining_length) {
1200 free(data);
1201 return;
1202 }
1204 /* successfully get the profile.*/
1205 *mem = data;
1206 *size = length;
1207 }
1209 qcms_profile* qcms_profile_from_file(FILE *file)
1210 {
1211 size_t length;
1212 qcms_profile *profile;
1213 void *data;
1215 qcms_data_from_file(file, &data, &length);
1216 if ((data == NULL) || (length == 0))
1217 return INVALID_PROFILE;
1219 profile = qcms_profile_from_memory(data, length);
1220 free(data);
1221 return profile;
1222 }
1224 qcms_profile* qcms_profile_from_path(const char *path)
1225 {
1226 qcms_profile *profile = NULL;
1227 FILE *file = fopen(path, "rb");
1228 if (file) {
1229 profile = qcms_profile_from_file(file);
1230 fclose(file);
1231 }
1232 return profile;
1233 }
1235 void qcms_data_from_path(const char *path, void **mem, size_t *size)
1236 {
1237 FILE *file = NULL;
1238 *mem = NULL;
1239 *size = 0;
1241 file = fopen(path, "rb");
1242 if (file) {
1243 qcms_data_from_file(file, mem, size);
1244 fclose(file);
1245 }
1246 }
1248 #ifdef _WIN32
1249 /* Unicode path version */
1250 qcms_profile* qcms_profile_from_unicode_path(const wchar_t *path)
1251 {
1252 qcms_profile *profile = NULL;
1253 FILE *file = _wfopen(path, L"rb");
1254 if (file) {
1255 profile = qcms_profile_from_file(file);
1256 fclose(file);
1257 }
1258 return profile;
1259 }
1261 void qcms_data_from_unicode_path(const wchar_t *path, void **mem, size_t *size)
1262 {
1263 FILE *file = NULL;
1264 *mem = NULL;
1265 *size = 0;
1267 file = _wfopen(path, L"rb");
1268 if (file) {
1269 qcms_data_from_file(file, mem, size);
1270 fclose(file);
1271 }
1272 }
1273 #endif
1275 /*
1276 * This function constructs an ICC profile memory with given header and tag data,
1277 * which can be read via qcms_profile_from_memory(). that means, we must satisfy
1278 * the profiler header type check (which seems not complete till now) and proper
1279 * information to read data from the tag table and tag data elements memory.
1280 *
1281 * To construct a valid ICC profile, its divided into three steps :
1282 * (1) construct the r/g/bXYZ part
1283 * (2) construct the r/g/bTRC part
1284 * (3) construct the profile header
1285 * this is a hardcode step just for "create_rgb_with_gamma", it is the only
1286 * requirement till now, maybe we can make this method more general in future,
1287 *
1288 * NOTE : some of the parameters below are hardcode, please refer to the ICC documentation.
1289 */
1290 #define ICC_PROFILE_HEADER_LENGTH 128
1291 void qcms_data_create_rgb_with_gamma(qcms_CIE_xyY white_point, qcms_CIE_xyYTRIPLE primaries, float gamma, void **mem, size_t *size)
1292 {
1293 uint32_t length, offset, index, xyz_count, trc_count;
1294 size_t tag_table_offset, tag_data_offset;
1295 void *data;
1296 struct matrix colorants;
1298 uint32_t TAG_XYZ[3] = {TAG_rXYZ, TAG_gXYZ, TAG_bXYZ};
1299 uint32_t TAG_TRC[3] = {TAG_rTRC, TAG_gTRC, TAG_bTRC};
1301 if ((mem == NULL) || (size == NULL))
1302 return;
1304 *mem = NULL;
1305 *size = 0;
1307 /*
1308 * total length = icc profile header(128) + tag count(4) +
1309 * (tag table item (12) * total tag (6 = 3 rTRC + 3 rXYZ)) + rTRC elements data (3 * 20)
1310 * + rXYZ elements data (3*16), and all tag data elements must start at the 4-byte boundary.
1311 */
1312 xyz_count = 3; // rXYZ, gXYZ, bXYZ
1313 trc_count = 3; // rTRC, gTRC, bTRC
1314 length = ICC_PROFILE_HEADER_LENGTH + 4 + (12 * (xyz_count + trc_count)) + (xyz_count * 20) + (trc_count * 16);
1316 // reserve the total memory.
1317 data = malloc(length);
1318 if (!data)
1319 return;
1320 memset(data, 0, length);
1322 // Part1 : write rXYZ, gXYZ and bXYZ
1323 if (!get_rgb_colorants(&colorants, white_point, primaries)) {
1324 free(data);
1325 return;
1326 }
1328 // the position of first tag's signature in tag table
1329 tag_table_offset = ICC_PROFILE_HEADER_LENGTH + 4;
1330 tag_data_offset = ICC_PROFILE_HEADER_LENGTH + 4 +
1331 (12 * (xyz_count + trc_count)); // the start of tag data elements.
1333 for (index = 0; index < xyz_count; ++index) {
1334 // tag table
1335 write_u32(data, tag_table_offset, TAG_XYZ[index]);
1336 write_u32(data, tag_table_offset+4, tag_data_offset);
1337 write_u32(data, tag_table_offset+8, 20); // 20 bytes per TAG_(r/g/b)XYZ tag element
1339 // tag data element
1340 write_u32(data, tag_data_offset, XYZ_TYPE);
1341 // reserved 4 bytes.
1342 write_u32(data, tag_data_offset+8, double_to_s15Fixed16Number(colorants.m[0][index]));
1343 write_u32(data, tag_data_offset+12, double_to_s15Fixed16Number(colorants.m[1][index]));
1344 write_u32(data, tag_data_offset+16, double_to_s15Fixed16Number(colorants.m[2][index]));
1346 tag_table_offset += 12;
1347 tag_data_offset += 20;
1348 }
1350 // Part2 : write rTRC, gTRC and bTRC
1351 for (index = 0; index < trc_count; ++index) {
1352 // tag table
1353 write_u32(data, tag_table_offset, TAG_TRC[index]);
1354 write_u32(data, tag_table_offset+4, tag_data_offset);
1355 write_u32(data, tag_table_offset+8, 14); // 14 bytes per TAG_(r/g/b)TRC element
1357 // tag data element
1358 write_u32(data, tag_data_offset, CURVE_TYPE);
1359 // reserved 4 bytes.
1360 write_u32(data, tag_data_offset+8, 1); // count
1361 write_u16(data, tag_data_offset+12, float_to_u8Fixed8Number(gamma));
1363 tag_table_offset += 12;
1364 tag_data_offset += 16;
1365 }
1367 /* Part3 : write profile header
1368 *
1369 * Important header fields are left empty. This generates a profile for internal use only.
1370 * We should be generating: Profile version (04300000h), Profile signature (acsp),
1371 * PCS illumiant field. Likewise mandatory profile tags are omitted.
1372 */
1373 write_u32(data, 0, length); // the total length of this memory
1374 write_u32(data, 12, DISPLAY_DEVICE_PROFILE); // profile->class
1375 write_u32(data, 16, RGB_SIGNATURE); // profile->color_space
1376 write_u32(data, 20, XYZ_SIGNATURE); // profile->pcs
1377 write_u32(data, 64, QCMS_INTENT_PERCEPTUAL); // profile->rendering_intent
1379 write_u32(data, ICC_PROFILE_HEADER_LENGTH, 6); // total tag count
1381 // prepare the result
1382 *mem = data;
1383 *size = length;
1384 }