Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: third_party/qcms/src/iccread.c

Issue 2014023003: Add exact version of qcms used by Chrome for testing and comparison (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/qcms/src/halffloat.h ('k') | third_party/qcms/src/matrix.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 Softwar e
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.
23
24 #include <math.h>
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <string.h> //memset
28 #include "qcmsint.h"
29
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 */
33
34 typedef uint32_t be32;
35 typedef uint16_t be16;
36
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)
42 {
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
48 }
49 #endif
50
51 static uint32_t be32_to_cpu(be32 v)
52 {
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
59 }
60
61 static uint16_t be16_to_cpu(be16 v)
62 {
63 #ifdef IS_LITTLE_ENDIAN
64 return ((v & 0xff) << 8) | ((v & 0xff00) >> 8);
65 #else
66 return v;
67 #endif
68 }
69
70 /* a wrapper around the memory that we are going to parse
71 * into a qcms_profile */
72 struct mem_source
73 {
74 const unsigned char *buf;
75 size_t size;
76 qcms_bool valid;
77 const char *invalid_reason;
78 };
79
80 static void invalid_source(struct mem_source *mem, const char *reason)
81 {
82 mem->valid = false;
83 mem->invalid_reason = reason;
84 }
85
86 static uint32_t read_u32(struct mem_source *mem, size_t offset)
87 {
88 /* Subtract from mem->size instead of the more intuitive adding to offse t.
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);
98 }
99 }
100
101 static uint16_t read_u16(struct mem_source *mem, size_t offset)
102 {
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);
110 }
111 }
112
113 static uint8_t read_u8(struct mem_source *mem, size_t offset)
114 {
115 if (offset > mem->size - 1) {
116 invalid_source(mem, "Invalid offset");
117 return 0;
118 } else {
119 return *(uint8_t*)(mem->buf + offset);
120 }
121 }
122
123 static s15Fixed16Number read_s15Fixed16Number(struct mem_source *mem, size_t off set)
124 {
125 return read_u32(mem, offset);
126 }
127
128 static uInt8Number read_uInt8Number(struct mem_source *mem, size_t offset)
129 {
130 return read_u8(mem, offset);
131 }
132
133 static uInt16Number read_uInt16Number(struct mem_source *mem, size_t offset)
134 {
135 return read_u16(mem, offset);
136 }
137
138 #define BAD_VALUE_PROFILE NULL
139 #define INVALID_PROFILE NULL
140 #define NO_MEM_PROFILE NULL
141
142 /* An arbitrary 4MB limit on profile size */
143 #define MAX_PROFILE_SIZE 1024*1024*4
144 #define MAX_TAG_COUNT 1024
145
146 static void check_CMM_type_signature(struct mem_source *src)
147 {
148 //uint32_t CMM_type_signature = read_u32(src, 4);
149 }
150
151 static void read_profile_version(qcms_profile *profile, struct mem_source *src)
152 {
153 uint8_t major_revision = read_u8(src, 8 + 0);
154 uint8_t minor_revision = read_u8(src, 8 + 1);
155 uint8_t reserved_byte1 = read_u8(src, 8 + 2);
156 uint8_t reserved_byte2 = read_u8(src, 8 + 3);
157
158 profile->icc_version = major_revision << 8 | minor_revision;
159
160 if (reserved_byte1 || reserved_byte2) {
161 invalid_source(src, "Invalid reserved bytes");
162 return;
163 }
164
165 if (major_revision == 2)
166 return; // ICC V2.X color profile
167 if (major_revision == 4 && qcms_supports_iccv4)
168 return; // ICC V4.X color profile
169
170 /* Checking the version doesn't buy us anything: permit any
171 version without failure for now */
172 // invalid_source(src, "Unsupported ICC revision");
173 return;
174 }
175
176 #define INPUT_DEVICE_PROFILE 0x73636e72 // 'scnr'
177 #define DISPLAY_DEVICE_PROFILE 0x6d6e7472 // 'mntr'
178 #define OUTPUT_DEVICE_PROFILE 0x70727472 // 'prtr'
179 #define DEVICE_LINK_PROFILE 0x6c696e6b // 'link'
180 #define COLOR_SPACE_PROFILE 0x73706163 // 'spac'
181 #define ABSTRACT_PROFILE 0x61627374 // 'abst'
182 #define NAMED_COLOR_PROFILE 0x6e6d636c // 'nmcl'
183
184 static void read_class_signature(qcms_profile *profile, struct mem_source *mem)
185 {
186 profile->class = read_u32(mem, 12);
187 switch (profile->class) {
188 case DISPLAY_DEVICE_PROFILE:
189 case INPUT_DEVICE_PROFILE:
190 case OUTPUT_DEVICE_PROFILE:
191 case COLOR_SPACE_PROFILE:
192 break;
193 default:
194 invalid_source(mem, "Invalid Profile/Device Class signa ture");
195 }
196 }
197
198 static void read_color_space(qcms_profile *profile, struct mem_source *mem)
199 {
200 profile->color_space = read_u32(mem, 16);
201 switch (profile->color_space) {
202 case RGB_SIGNATURE:
203 case GRAY_SIGNATURE:
204 break;
205 default:
206 invalid_source(mem, "Unsupported colorspace");
207 }
208 }
209
210 static void read_pcs(qcms_profile *profile, struct mem_source *mem)
211 {
212 profile->pcs = read_u32(mem, 20);
213 switch (profile->pcs) {
214 case XYZ_SIGNATURE:
215 case LAB_SIGNATURE:
216 break;
217 default:
218 invalid_source(mem, "Unsupported pcs");
219 }
220 }
221
222 struct tag {
223 uint32_t signature;
224 uint32_t offset;
225 uint32_t size;
226 };
227
228 struct tag_index {
229 uint32_t count;
230 struct tag *tags;
231 };
232
233 static struct tag_index read_tag_table(qcms_profile *profile, struct mem_source *mem)
234 {
235 struct tag_index index = {0, NULL};
236 unsigned int i;
237
238 index.count = read_u32(mem, 128);
239 if (index.count > MAX_TAG_COUNT) {
240 invalid_source(mem, "max number of tags exceeded");
241 return index;
242 }
243
244 index.tags = malloc(sizeof(struct tag)*index.count);
245 if (index.tags) {
246 for (i = 0; i < index.count; i++) {
247 index.tags[i].signature = read_u32(mem, 128 + 4 + 4*i*3) ;
248 index.tags[i].offset = read_u32(mem, 128 + 4 + 4*i*3 + 4);
249 index.tags[i].size = read_u32(mem, 128 + 4 + 4*i*3 + 8);
250 }
251 }
252
253 return index;
254 }
255
256 /* Checks a profile for obvious inconsistencies and return true if the
257 * profile looks bogus and should probably be ignored.
258 */
259 qcms_bool qcms_profile_is_bogus(qcms_profile *profile)
260 {
261 float rX, rY, rZ, gX, gY, gZ, bX, bY, bZ;
262 float target[3], tolerance[3], sum[3];
263 unsigned i;
264
265 // We currently only check the bogosity of RGB profiles.
266 if (profile->color_space != RGB_SIGNATURE)
267 return false;
268
269 if (qcms_supports_iccv4 && (profile->A2B0 || profile->B2A0))
270 return false;
271
272 rX = s15Fixed16Number_to_float(profile->redColorant.X);
273 rY = s15Fixed16Number_to_float(profile->redColorant.Y);
274 rZ = s15Fixed16Number_to_float(profile->redColorant.Z);
275
276 gX = s15Fixed16Number_to_float(profile->greenColorant.X);
277 gY = s15Fixed16Number_to_float(profile->greenColorant.Y);
278 gZ = s15Fixed16Number_to_float(profile->greenColorant.Z);
279
280 bX = s15Fixed16Number_to_float(profile->blueColorant.X);
281 bY = s15Fixed16Number_to_float(profile->blueColorant.Y);
282 bZ = s15Fixed16Number_to_float(profile->blueColorant.Z);
283
284 // Build our target vector: CIE D50 white. See also mozilla bug 460629,
285 // and http://www.color.org/whyd50.xalter "Why is the media white point
286 // of a display profile always D50?"
287
288 target[0] = (float) 0.96420;
289 target[1] = (float) 1.00000;
290 target[2] = (float) 0.82491;
291
292 // Our tolerance vector - Recommended by Chris Murphy [1] based on
293 // conversion from the L*a*b space criterion of no more than 3 in any
294 // one channel. This is similar to, but slightly more tolerant than
295 // Adobe's criterion. [1] https://bugzil.la/460629#c10
296
297 tolerance[0] = (float) 0.02;
298 tolerance[1] = (float) 0.02;
299 tolerance[2] = (float) 0.04;
300
301 // Sum the XYZ values: they should add to D50 white, within tolerance.
302
303 // FIXME: this test assumes the TRC RGB curves equal 1.0 for the white
304 // input (255,255,255) RGB test color. For user display profiles, that
305 // is the normal case. Profiles with abnormal TRC exist. A better test
306 // would transform 255,255,255 white through the profile to either XYZ
307 // or L*a*b color and compare the result to D50 in XYZ or L*a*b color.
308
309 sum[0] = rX + gX + bX;
310 sum[1] = rY + gY + bY;
311 sum[2] = rZ + gZ + bZ;
312
313 for (i = 0; i < 3; ++i) {
314 if (!(((sum[i] - tolerance[i]) <= target[i]) &&
315 ((sum[i] + tolerance[i]) >= target[i]))) {
316 return true; // out of tolerance: bogus
317 }
318 }
319
320 #ifndef __APPLE__
321 // Check if any of the XYZ values are negative (see mozilla bug 498245)
322 // CIEXYZ tristimulus values cannot be negative according to the spec.
323
324 bool negative =
325 (rX < 0) || (rY < 0) || (rZ < 0) ||
326 (gX < 0) || (gY < 0) || (gZ < 0) ||
327 (bX < 0) || (bY < 0) || (bZ < 0);
328
329 if (negative)
330 return true; // bogus
331 #else
332 // Chromatic adaption to D50 can result in negative XYZ, but the white
333 // point D50 tolerance test has passed. Accept negative values herein.
334 // See https://bugzilla.mozilla.org/show_bug.cgi?id=498245#c18 onwards
335 // for discussion about whether profile XYZ can or cannot be negative,
336 // per the spec. Also the https://bugzil.la/450923 user report.
337
338 // FIXME: allow this relaxation on all ports?
339 #endif
340 // All good.
341 return false;
342 }
343
344 qcms_bool qcms_profile_has_white_point(qcms_profile *profile)
345 {
346 struct XYZNumber wp = profile->mediaWhitePoint;
347
348 return (wp.X != 0) && (wp.Y != 0) && (wp.Z != 0);
349 }
350
351 qcms_xyz_float qcms_profile_get_white_point(qcms_profile *profile)
352 {
353 qcms_xyz_float wp = { 0.0f, 0.0f, 0.0f };
354
355 if (qcms_profile_has_white_point(profile)) {
356 wp.X = s15Fixed16Number_to_float(profile->mediaWhitePoint.X);
357 wp.Y = s15Fixed16Number_to_float(profile->mediaWhitePoint.Y);
358 wp.Z = s15Fixed16Number_to_float(profile->mediaWhitePoint.Z);
359 }
360
361 return wp;
362 }
363
364 #define TAG_bXYZ 0x6258595a
365 #define TAG_gXYZ 0x6758595a
366 #define TAG_rXYZ 0x7258595a
367 #define TAG_rTRC 0x72545243
368 #define TAG_bTRC 0x62545243
369 #define TAG_gTRC 0x67545243
370 #define TAG_kTRC 0x6b545243
371 #define TAG_A2B0 0x41324230
372 #define TAG_B2A0 0x42324130
373 #define TAG_CHAD 0x63686164
374 #define TAG_desc 0x64657363
375 #define TAG_vcgt 0x76636774
376 #define TAG_wtpt 0x77747074
377
378 static struct tag *find_tag(struct tag_index index, uint32_t tag_id)
379 {
380 unsigned int i;
381
382 for (i = 0; i < index.count; i++) {
383 if (index.tags[i].signature == tag_id)
384 return &index.tags[i];
385 }
386
387 return NULL;
388 }
389
390 #define DESC_TYPE 0x64657363 // 'desc'
391 #define MLUC_TYPE 0x6d6c7563 // 'mluc'
392 #define MMOD_TYPE 0x6D6D6F64 // 'mmod'
393 #define VCGT_TYPE 0x76636774 // 'vcgt'
394
395 enum {
396 VCGT_TYPE_TABLE,
397 VCGT_TYPE_FORMULA,
398 VCGT_TYPE_LAST = VCGT_TYPE_FORMULA
399 };
400
401 static qcms_bool read_tag_vcgtType(qcms_profile *profile, struct mem_source *src , struct tag_index index)
402 {
403 size_t tag_offset = find_tag(index, TAG_vcgt)->offset;
404 uint32_t tag_type = read_u32(src, tag_offset);
405 uint32_t vcgt_type = read_u32(src, tag_offset + 8);
406
407 if (!src->valid || tag_type != VCGT_TYPE)
408 goto invalid_vcgt_tag;
409
410 // Only support table and equation types.
411 if (vcgt_type > VCGT_TYPE_LAST)
412 return true;
413
414 if (vcgt_type == VCGT_TYPE_TABLE) {
415 uint16_t channels = read_u16(src, tag_offset + 12);
416 uint16_t elements = read_u16(src, tag_offset + 14);
417 uint16_t byte_depth = read_u16(src, tag_offset + 16);
418 size_t table_offset = tag_offset + 18;
419 uint32_t i;
420 uint16_t *dest;
421
422 if (!src->valid)
423 goto invalid_vcgt_tag;
424
425 // Only support 3 channels.
426 if (channels != 3)
427 return true;
428 // Only support single or double byte values.
429 if (byte_depth != 1 && byte_depth != 2)
430 return true;
431 // Limit the table to a sensible size; 10-bit gamma is a reasona ble
432 // maximum for hardware correction.
433 if (elements > 1024)
434 return true;
435
436 // Empty table is invalid.
437 if (!elements)
438 goto invalid_vcgt_tag;
439
440 profile->vcgt.length = elements;
441 profile->vcgt.data = malloc(3 * elements * sizeof(uint16_t));
442 if (!profile->vcgt.data)
443 return false;
444
445 dest = profile->vcgt.data;
446
447 for (i = 0; i < 3 * elements; ++i) {
448 if (byte_depth == 1) {
449 *dest++ = read_u8(src, table_offset) * 256;
450 } else {
451 *dest++ = read_u16(src, table_offset);
452 }
453
454 table_offset += byte_depth;
455
456 if (!src->valid)
457 goto invalid_vcgt_tag;
458 }
459 } else {
460 size_t formula_offset = tag_offset + 12;
461 int i, j;
462 uint16_t *dest;
463
464 // For formula always provide an 8-bit lut.
465 profile->vcgt.length = 256;
466 profile->vcgt.data = malloc(3 * profile->vcgt.length * sizeof(ui nt16_t));
467 if (!profile->vcgt.data)
468 return false;
469
470 dest = profile->vcgt.data;
471 for (i = 0; i < 3; ++i) {
472 float gamma = s15Fixed16Number_to_float(
473 read_s15Fixed16Number(src, formula_offse t + 12 * i));
474 float min = s15Fixed16Number_to_float(
475 read_s15Fixed16Number(src, formula_offse t + 4 + 12 * i));
476 float max = s15Fixed16Number_to_float(
477 read_s15Fixed16Number(src, formula_offse t + 8 + 12 * i));
478 float range = max - min;
479
480 if (!src->valid)
481 goto invalid_vcgt_tag;
482
483 for (j = 0; j < profile->vcgt.length; ++j) {
484 *dest++ = 65535.f *
485 (min + range * pow((float)j / (profile-> vcgt.length - 1), gamma));
486 }
487 }
488 }
489
490 return true;
491
492 invalid_vcgt_tag:
493 invalid_source(src, "invalid vcgt tag");
494 return false;
495 }
496
497 static bool read_tag_descType(qcms_profile *profile, struct mem_source *src, str uct tag_index index, uint32_t tag_id)
498 {
499 struct tag *tag = find_tag(index, tag_id);
500 if (tag) {
501 const uint32_t limit = sizeof profile->description;
502 uint32_t offset = tag->offset;
503 uint32_t type = read_u32(src, offset);
504 uint32_t length = read_u32(src, offset+8);
505 uint32_t i, description_offset;
506 bool mluc = false;
507 if (length && type == MLUC_TYPE) {
508 length = read_u32(src, offset+20);
509 if (!length || (length & 1) || (read_u32(src, offset+12) != 12))
510 goto invalid_desc_tag;
511 description_offset = offset + read_u32(src, offset+24);
512 if (!src->valid)
513 goto invalid_desc_tag;
514 mluc = true;
515 } else if (length && type == DESC_TYPE) {
516 description_offset = offset + 12;
517 } else {
518 goto invalid_desc_tag;
519 }
520 if (length >= limit)
521 length = limit - 1;
522 for (i = 0; i < length; ++i) {
523 uint8_t value = read_u8(src, description_offset + i);
524 if (!src->valid)
525 goto invalid_desc_tag;
526 if (mluc && !value)
527 value = '.';
528 profile->description[i] = value;
529 }
530 profile->description[length] = 0;
531 } else {
532 goto invalid_desc_tag;
533 }
534
535 if (src->valid)
536 return true;
537
538 invalid_desc_tag:
539 invalid_source(src, "invalid description");
540 return false;
541 }
542
543 #if defined(__APPLE__)
544
545 // Use the dscm tag to change profile description "Display" to its more specific en-localized monitor name, if any.
546
547 #define TAG_dscm 0x6473636D // 'dscm'
548
549 static bool read_tag_dscmType(qcms_profile *profile, struct mem_source *src, str uct tag_index index, uint32_t tag_id)
550 {
551 if (strcmp(profile->description, "Display") != 0)
552 return true;
553
554 struct tag *tag = find_tag(index, tag_id);
555 if (tag) {
556 uint32_t offset = tag->offset;
557 uint32_t type = read_u32(src, offset);
558 uint32_t records = read_u32(src, offset+8);
559
560 if (!src->valid || !records || type != MLUC_TYPE)
561 goto invalid_dscm_tag;
562 if (read_u32(src, offset+12) != 12) // MLUC record size: bytes
563 goto invalid_dscm_tag;
564
565 for (uint32_t i = 0; i < records; ++i) {
566 const uint32_t limit = sizeof profile->description;
567 const uint16_t isoen = 0x656E; // ISO-3166-1 language 'e n'
568
569 uint16_t language = read_u16(src, offset + 16 + (i * 12) + 0);
570 uint32_t length = read_u32(src, offset + 16 + (i * 12) + 4);
571 uint32_t description_offset = read_u32(src, offset + 16 + (i * 12) + 8);
572
573 if (!src->valid || !length || (length & 1))
574 goto invalid_dscm_tag;
575 if (language != isoen)
576 continue;
577
578 // Use a prefix to identify the display description sour ce
579 strcpy(profile->description, "dscm:");
580 length += 5;
581
582 if (length >= limit)
583 length = limit - 1;
584 for (uint32_t j = 5; j < length; ++j) {
585 uint8_t value = read_u8(src, offset + descriptio n_offset + j - 5);
586 if (!src->valid)
587 goto invalid_dscm_tag;
588 profile->description[j] = value ? value : '.';
589 }
590 profile->description[length] = 0;
591 break;
592 }
593 }
594
595 if (src->valid)
596 return true;
597
598 invalid_dscm_tag:
599 invalid_source(src, "invalid dscm tag");
600 return false;
601 }
602
603 // Use the mmod tag to change profile description "Display" to its specific mmod maker model data, if any.
604
605 #define TAG_mmod 0x6D6D6F64 // 'mmod'
606
607 static bool read_tag_mmodType(qcms_profile *profile, struct mem_source *src, str uct tag_index index, uint32_t tag_id)
608 {
609 if (strcmp(profile->description, "Display") != 0)
610 return true;
611
612 struct tag *tag = find_tag(index, tag_id);
613 if (tag) {
614 const uint8_t length = 4 * 4; // Four 4-byte fields: 'mmod', 0, maker, model.
615
616 uint32_t offset = tag->offset;
617 if (tag->size < 40 || read_u32(src, offset) != MMOD_TYPE)
618 goto invalid_mmod_tag;
619
620 for (uint8_t i = 0; i < length; ++i) {
621 uint8_t value = read_u8(src, offset + i);
622 if (!src->valid)
623 goto invalid_mmod_tag;
624 profile->description[i] = value ? value : '.';
625 }
626 profile->description[length] = 0;
627 }
628
629 if (src->valid)
630 return true;
631
632 invalid_mmod_tag:
633 invalid_source(src, "invalid mmod tag");
634 return false;
635 }
636
637 #endif // __APPLE__
638
639 #define XYZ_TYPE 0x58595a20 // 'XYZ '
640 #define CURVE_TYPE 0x63757276 // 'curv'
641 #define PARAMETRIC_CURVE_TYPE 0x70617261 // 'para'
642 #define LUT16_TYPE 0x6d667432 // 'mft2'
643 #define LUT8_TYPE 0x6d667431 // 'mft1'
644 #define LUT_MAB_TYPE 0x6d414220 // 'mAB '
645 #define LUT_MBA_TYPE 0x6d424120 // 'mBA '
646 #define CHROMATIC_TYPE 0x73663332 // 'sf32'
647
648 static struct matrix read_tag_s15Fixed16ArrayType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
649 {
650 struct tag *tag = find_tag(index, tag_id);
651 struct matrix matrix;
652 if (tag) {
653 uint8_t i;
654 uint32_t offset = tag->offset;
655 uint32_t type = read_u32(src, offset);
656
657 // Check mandatory type signature for s16Fixed16ArrayType
658 if (type != CHROMATIC_TYPE) {
659 invalid_source(src, "unexpected type, expected 'sf32'");
660 }
661
662 for (i = 0; i < 9; i++) {
663 matrix.m[i/3][i%3] = s15Fixed16Number_to_float(read_s15F ixed16Number(src, offset+8+i*4));
664 }
665 matrix.invalid = false;
666 } else {
667 matrix.invalid = true;
668 invalid_source(src, "missing sf32tag");
669 }
670 return matrix;
671 }
672
673 static struct XYZNumber read_tag_XYZType(struct mem_source *src, struct tag_inde x index, uint32_t tag_id)
674 {
675 struct XYZNumber num = {0, 0, 0};
676 struct tag *tag = find_tag(index, tag_id);
677 if (tag) {
678 uint32_t offset = tag->offset;
679
680 uint32_t type = read_u32(src, offset);
681 if (type != XYZ_TYPE)
682 invalid_source(src, "unexpected type, expected XYZ");
683 num.X = read_s15Fixed16Number(src, offset+8);
684 num.Y = read_s15Fixed16Number(src, offset+12);
685 num.Z = read_s15Fixed16Number(src, offset+16);
686 } else {
687 invalid_source(src, "missing xyztag");
688 }
689 return num;
690 }
691
692 // Read the tag at a given offset rather then the tag_index.
693 // This method is used when reading mAB tags where nested curveType are
694 // present that are not part of the tag_index.
695 static struct curveType *read_curveType(struct mem_source *src, uint32_t offset, uint32_t *len)
696 {
697 static const uint32_t COUNT_TO_LENGTH[5] = {1, 3, 4, 5, 7};
698 struct curveType *curve = NULL;
699 uint32_t type = read_u32(src, offset);
700 uint32_t count;
701 int i;
702
703 if (type != CURVE_TYPE && type != PARAMETRIC_CURVE_TYPE) {
704 invalid_source(src, "unexpected type, expected CURV or PARA");
705 return NULL;
706 }
707
708 if (type == CURVE_TYPE) {
709 count = read_u32(src, offset+8);
710
711 #define MAX_CURVE_ENTRIES 40000 //arbitrary
712 if (count > MAX_CURVE_ENTRIES) {
713 invalid_source(src, "curve size too large");
714 return NULL;
715 }
716 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*c ount);
717 if (!curve)
718 return NULL;
719
720 curve->count = count;
721 curve->type = type;
722
723 for (i=0; i<count; i++) {
724 curve->data[i] = read_u16(src, offset + 12 + i*2);
725 }
726 *len = 12 + count * 2;
727 } else { //PARAMETRIC_CURVE_TYPE
728 count = read_u16(src, offset+8);
729
730 if (count > 4) {
731 invalid_source(src, "parametric function type not suppor ted.");
732 return NULL;
733 }
734
735 curve = malloc(sizeof(struct curveType));
736 if (!curve)
737 return NULL;
738
739 curve->count = count;
740 curve->type = type;
741
742 for (i=0; i < COUNT_TO_LENGTH[count]; i++) {
743 curve->parameter[i] = s15Fixed16Number_to_float(read_s15 Fixed16Number(src, offset + 12 + i*4));
744 }
745 *len = 12 + COUNT_TO_LENGTH[count] * 4;
746
747 if ((count == 1 || count == 2)) {
748 /* we have a type 1 or type 2 function that has a divisi on by 'a' */
749 float a = curve->parameter[1];
750 if (a == 0.f)
751 invalid_source(src, "parametricCurve definition causes division by zero.");
752 }
753 }
754
755 return curve;
756 }
757
758 static struct curveType *read_tag_curveType(struct mem_source *src, struct tag_i ndex index, uint32_t tag_id)
759 {
760 struct tag *tag = find_tag(index, tag_id);
761 struct curveType *curve = NULL;
762 if (tag) {
763 uint32_t len;
764 return read_curveType(src, tag->offset, &len);
765 } else {
766 invalid_source(src, "missing curvetag");
767 }
768
769 return curve;
770 }
771
772 #define MAX_CLUT_SIZE 500000 // arbitrary
773 #define MAX_CHANNELS 10 // arbitrary
774 static void read_nested_curveType(struct mem_source *src, struct curveType *(*cu rveArray)[MAX_CHANNELS], uint8_t num_channels, uint32_t curve_offset)
775 {
776 uint32_t channel_offset = 0;
777 int i;
778 for (i = 0; i < num_channels; i++) {
779 uint32_t tag_len = ~0;
780
781 (*curveArray)[i] = read_curveType(src, curve_offset + channel_of fset, &tag_len);
782 if (!(*curveArray)[i]) {
783 invalid_source(src, "invalid nested curveType curve");
784 }
785
786 if (tag_len == ~0) {
787 invalid_source(src, "invalid nested curveType tag length ");
788 return;
789 }
790
791 channel_offset += tag_len;
792 // 4 byte aligned
793 if ((tag_len % 4) != 0)
794 channel_offset += 4 - (tag_len % 4);
795 }
796 }
797
798 static void mAB_release(struct lutmABType *lut)
799 {
800 uint8_t i;
801
802 for (i = 0; i < lut->num_in_channels; i++){
803 free(lut->a_curves[i]);
804 }
805 for (i = 0; i < lut->num_out_channels; i++){
806 free(lut->b_curves[i]);
807 free(lut->m_curves[i]);
808 }
809 free(lut);
810 }
811
812 /* See section 10.10 for specs */
813 static struct lutmABType *read_tag_lutmABType(struct mem_source *src, struct tag _index index, uint32_t tag_id)
814 {
815 struct tag *tag = find_tag(index, tag_id);
816 uint32_t offset = tag->offset;
817 uint32_t a_curve_offset, b_curve_offset, m_curve_offset;
818 uint32_t matrix_offset;
819 uint32_t clut_offset;
820 uint32_t clut_size = 1;
821 uint8_t clut_precision;
822 uint32_t type = read_u32(src, offset);
823 uint8_t num_in_channels, num_out_channels;
824 struct lutmABType *lut;
825 int i;
826
827 if (type != LUT_MAB_TYPE && type != LUT_MBA_TYPE) {
828 return NULL;
829 }
830
831 num_in_channels = read_u8(src, offset + 8);
832 num_out_channels = read_u8(src, offset + 8);
833 if (num_in_channels > MAX_CHANNELS || num_out_channels > MAX_CHANNELS)
834 return NULL;
835
836 // We require 3in/out channels since we only support RGB->XYZ (or RGB->L AB)
837 // XXX: If we remove this restriction make sure that the number of chann els
838 // is less or equal to the maximum number of mAB curves in qcmsint. h
839 // also check for clut_size overflow. Also make sure it's != 0
840 if (num_in_channels != 3 || num_out_channels != 3)
841 return NULL;
842
843 // some of this data is optional and is denoted by a zero offset
844 // we also use this to track their existance
845 a_curve_offset = read_u32(src, offset + 28);
846 clut_offset = read_u32(src, offset + 24);
847 m_curve_offset = read_u32(src, offset + 20);
848 matrix_offset = read_u32(src, offset + 16);
849 b_curve_offset = read_u32(src, offset + 12);
850
851 // Convert offsets relative to the tag to relative to the profile
852 // preserve zero for optional fields
853 if (a_curve_offset)
854 a_curve_offset += offset;
855 if (clut_offset)
856 clut_offset += offset;
857 if (m_curve_offset)
858 m_curve_offset += offset;
859 if (matrix_offset)
860 matrix_offset += offset;
861 if (b_curve_offset)
862 b_curve_offset += offset;
863
864 if (clut_offset) {
865 assert (num_in_channels == 3);
866 // clut_size can not overflow since lg(256^num_in_channels) = 24 bits.
867 for (i = 0; i < num_in_channels; i++) {
868 clut_size *= read_u8(src, clut_offset + i);
869 if (clut_size == 0) {
870 invalid_source(src, "bad clut_size");
871 }
872 }
873 } else {
874 clut_size = 0;
875 }
876
877 // 24bits * 3 won't overflow either
878 clut_size = clut_size * num_out_channels;
879
880 if (clut_size > MAX_CLUT_SIZE)
881 return NULL;
882
883 lut = malloc(sizeof(struct lutmABType) + (clut_size) * sizeof(float));
884 if (!lut)
885 return NULL;
886 // we'll fill in the rest below
887 memset(lut, 0, sizeof(struct lutmABType));
888 lut->clut_table = &lut->clut_table_data[0];
889
890 if (clut_offset) {
891 for (i = 0; i < num_in_channels; i++) {
892 lut->num_grid_points[i] = read_u8(src, clut_offset + i);
893 if (lut->num_grid_points[i] == 0) {
894 invalid_source(src, "bad grid_points");
895 }
896 }
897 }
898
899 // Reverse the processing of transformation elements for mBA type.
900 lut->reversed = (type == LUT_MBA_TYPE);
901
902 lut->num_in_channels = num_in_channels;
903 lut->num_out_channels = num_out_channels;
904
905 if (matrix_offset) {
906 // read the matrix if we have it
907 lut->e00 = read_s15Fixed16Number(src, matrix_offset+4*0);
908 lut->e01 = read_s15Fixed16Number(src, matrix_offset+4*1);
909 lut->e02 = read_s15Fixed16Number(src, matrix_offset+4*2);
910 lut->e10 = read_s15Fixed16Number(src, matrix_offset+4*3);
911 lut->e11 = read_s15Fixed16Number(src, matrix_offset+4*4);
912 lut->e12 = read_s15Fixed16Number(src, matrix_offset+4*5);
913 lut->e20 = read_s15Fixed16Number(src, matrix_offset+4*6);
914 lut->e21 = read_s15Fixed16Number(src, matrix_offset+4*7);
915 lut->e22 = read_s15Fixed16Number(src, matrix_offset+4*8);
916 lut->e03 = read_s15Fixed16Number(src, matrix_offset+4*9);
917 lut->e13 = read_s15Fixed16Number(src, matrix_offset+4*10);
918 lut->e23 = read_s15Fixed16Number(src, matrix_offset+4*11);
919 }
920
921 if (a_curve_offset) {
922 read_nested_curveType(src, &lut->a_curves, num_in_channels, a_cu rve_offset);
923 }
924 if (m_curve_offset) {
925 read_nested_curveType(src, &lut->m_curves, num_out_channels, m_c urve_offset);
926 }
927 if (b_curve_offset) {
928 read_nested_curveType(src, &lut->b_curves, num_out_channels, b_c urve_offset);
929 } else {
930 invalid_source(src, "B curves required");
931 }
932
933 if (clut_offset) {
934 clut_precision = read_u8(src, clut_offset + 16);
935 if (clut_precision == 1) {
936 for (i = 0; i < clut_size; i++) {
937 lut->clut_table[i] = uInt8Number_to_float(read_u Int8Number(src, clut_offset + 20 + i*1));
938 }
939 } else if (clut_precision == 2) {
940 for (i = 0; i < clut_size; i++) {
941 lut->clut_table[i] = uInt16Number_to_float(read_ uInt16Number(src, clut_offset + 20 + i*2));
942 }
943 } else {
944 invalid_source(src, "Invalid clut precision");
945 }
946 }
947
948 if (!src->valid) {
949 mAB_release(lut);
950 return NULL;
951 }
952
953 return lut;
954 }
955
956 static struct lutType *read_tag_lutType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
957 {
958 struct tag *tag = find_tag(index, tag_id);
959 uint32_t offset = tag->offset;
960 uint32_t type = read_u32(src, offset);
961 uint16_t num_input_table_entries;
962 uint16_t num_output_table_entries;
963 uint8_t in_chan, grid_points, out_chan;
964 size_t clut_offset, output_offset;
965 uint32_t clut_size;
966 size_t entry_size;
967 struct lutType *lut;
968 int i;
969
970 /* I'm not sure why the spec specifies a fixed number of entries for LUT 8 tables even though
971 * they have room for the num_entries fields */
972 if (type == LUT8_TYPE) {
973 num_input_table_entries = 256;
974 num_output_table_entries = 256;
975 entry_size = 1;
976 } else if (type == LUT16_TYPE) {
977 num_input_table_entries = read_u16(src, offset + 48);
978 num_output_table_entries = read_u16(src, offset + 50);
979 if (num_input_table_entries == 0 || num_output_table_entries == 0) {
980 invalid_source(src, "Bad channel count");
981 return NULL;
982 }
983 entry_size = 2;
984 } else {
985 assert(0); // the caller checks that this doesn't happen
986 invalid_source(src, "Unexpected lut type");
987 return NULL;
988 }
989
990 in_chan = read_u8(src, offset + 8);
991 out_chan = read_u8(src, offset + 9);
992 grid_points = read_u8(src, offset + 10);
993
994 clut_size = pow(grid_points, in_chan);
995 if (clut_size > MAX_CLUT_SIZE) {
996 invalid_source(src, "CLUT too large");
997 return NULL;
998 }
999
1000 if (clut_size <= 0) {
1001 invalid_source(src, "CLUT must not be empty.");
1002 return NULL;
1003 }
1004
1005 if (in_chan != 3 || out_chan != 3) {
1006 invalid_source(src, "CLUT only supports RGB");
1007 return NULL;
1008 }
1009
1010 lut = malloc(sizeof(struct lutType) + (num_input_table_entries * in_chan + clut_size*out_chan + num_output_table_entries * out_chan)*sizeof(float));
1011 if (!lut) {
1012 invalid_source(src, "CLUT too large");
1013 return NULL;
1014 }
1015
1016 /* compute the offsets of tables */
1017 lut->input_table = &lut->table_data[0];
1018 lut->clut_table = &lut->table_data[in_chan*num_input_table_entries];
1019 lut->output_table = &lut->table_data[in_chan*num_input_table_entries + c lut_size*out_chan];
1020
1021 lut->num_input_table_entries = num_input_table_entries;
1022 lut->num_output_table_entries = num_output_table_entries;
1023 lut->num_input_channels = in_chan;
1024 lut->num_output_channels = out_chan;
1025 lut->num_clut_grid_points = grid_points;
1026 lut->e00 = read_s15Fixed16Number(src, offset+12);
1027 lut->e01 = read_s15Fixed16Number(src, offset+16);
1028 lut->e02 = read_s15Fixed16Number(src, offset+20);
1029 lut->e10 = read_s15Fixed16Number(src, offset+24);
1030 lut->e11 = read_s15Fixed16Number(src, offset+28);
1031 lut->e12 = read_s15Fixed16Number(src, offset+32);
1032 lut->e20 = read_s15Fixed16Number(src, offset+36);
1033 lut->e21 = read_s15Fixed16Number(src, offset+40);
1034 lut->e22 = read_s15Fixed16Number(src, offset+44);
1035
1036 for (i = 0; i < lut->num_input_table_entries * in_chan; i++) {
1037 if (type == LUT8_TYPE) {
1038 lut->input_table[i] = uInt8Number_to_float(read_uInt8Num ber(src, offset + 52 + i * entry_size));
1039 } else {
1040 lut->input_table[i] = uInt16Number_to_float(read_uInt16N umber(src, offset + 52 + i * entry_size));
1041 }
1042 }
1043
1044 clut_offset = offset + 52 + lut->num_input_table_entries * in_chan * ent ry_size;
1045 for (i = 0; i < clut_size * out_chan; i+=3) {
1046 if (type == LUT8_TYPE) {
1047 lut->clut_table[i+0] = uInt8Number_to_float(read_uInt8Nu mber(src, clut_offset + i*entry_size + 0));
1048 lut->clut_table[i+1] = uInt8Number_to_float(read_uInt8Nu mber(src, clut_offset + i*entry_size + 1));
1049 lut->clut_table[i+2] = uInt8Number_to_float(read_uInt8Nu mber(src, clut_offset + i*entry_size + 2));
1050 } else {
1051 lut->clut_table[i+0] = uInt16Number_to_float(read_uInt16 Number(src, clut_offset + i*entry_size + 0));
1052 lut->clut_table[i+1] = uInt16Number_to_float(read_uInt16 Number(src, clut_offset + i*entry_size + 2));
1053 lut->clut_table[i+2] = uInt16Number_to_float(read_uInt16 Number(src, clut_offset + i*entry_size + 4));
1054 }
1055 }
1056
1057 output_offset = clut_offset + clut_size * out_chan * entry_size;
1058 for (i = 0; i < lut->num_output_table_entries * out_chan; i++) {
1059 if (type == LUT8_TYPE) {
1060 lut->output_table[i] = uInt8Number_to_float(read_uInt8Nu mber(src, output_offset + i*entry_size));
1061 } else {
1062 lut->output_table[i] = uInt16Number_to_float(read_uInt16 Number(src, output_offset + i*entry_size));
1063 }
1064 }
1065
1066 return lut;
1067 }
1068
1069 static void read_rendering_intent(qcms_profile *profile, struct mem_source *src)
1070 {
1071 profile->rendering_intent = read_u32(src, 64);
1072 switch (profile->rendering_intent) {
1073 case QCMS_INTENT_PERCEPTUAL:
1074 case QCMS_INTENT_SATURATION:
1075 case QCMS_INTENT_RELATIVE_COLORIMETRIC:
1076 case QCMS_INTENT_ABSOLUTE_COLORIMETRIC:
1077 break;
1078 default:
1079 invalid_source(src, "unknown rendering intent");
1080 }
1081 }
1082
1083 qcms_profile *qcms_profile_create(void)
1084 {
1085 return calloc(sizeof(qcms_profile), 1);
1086 }
1087
1088
1089
1090 /* build sRGB gamma table */
1091 /* based on cmsBuildParametricGamma() */
1092 static uint16_t *build_sRGB_gamma_table(int num_entries)
1093 {
1094 int i;
1095 /* taken from lcms: Build_sRGBGamma() */
1096 double gamma = 2.4;
1097 double a = 1./1.055;
1098 double b = 0.055/1.055;
1099 double c = 1./12.92;
1100 double d = 0.04045;
1101
1102 uint16_t *table = malloc(sizeof(uint16_t) * num_entries);
1103 if (!table)
1104 return NULL;
1105
1106 for (i=0; i<num_entries; i++) {
1107 double x = (double)i / (num_entries-1);
1108 double y, output;
1109 // IEC 61966-2.1 (sRGB)
1110 // Y = (aX + b)^Gamma | X >= d
1111 // Y = cX | X < d
1112 if (x >= d) {
1113 double e = (a*x + b);
1114 if (e > 0)
1115 y = pow(e, gamma);
1116 else
1117 y = 0;
1118 } else {
1119 y = c*x;
1120 }
1121
1122 // Saturate -- this could likely move to a separate function
1123 output = y * 65535. + .5;
1124 if (output > 65535.)
1125 output = 65535;
1126 if (output < 0)
1127 output = 0;
1128 table[i] = (uint16_t)floor(output);
1129 }
1130 return table;
1131 }
1132
1133 static struct curveType *curve_from_table(uint16_t *table, int num_entries)
1134 {
1135 struct curveType *curve;
1136 int i;
1137 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entri es);
1138 if (!curve)
1139 return NULL;
1140 curve->type = CURVE_TYPE;
1141 curve->count = num_entries;
1142 for (i = 0; i < num_entries; i++) {
1143 curve->data[i] = table[i];
1144 }
1145 return curve;
1146 }
1147
1148 static uint16_t float_to_u8Fixed8Number(float a)
1149 {
1150 if (a > (255.f + 255.f/256))
1151 return 0xffff;
1152 else if (a < 0.f)
1153 return 0;
1154 else
1155 return floor(a*256.f + .5f);
1156 }
1157
1158 static struct curveType *curve_from_gamma(float gamma)
1159 {
1160 struct curveType *curve;
1161 int num_entries = 1;
1162 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entri es);
1163 if (!curve)
1164 return NULL;
1165 curve->count = num_entries;
1166 curve->data[0] = float_to_u8Fixed8Number(gamma);
1167 return curve;
1168 }
1169
1170
1171 //XXX: it would be nice if we had a way of ensuring
1172 // everything in a profile was initialized regardless of how it was created
1173
1174 //XXX: should this also be taking a black_point?
1175 /* similar to CGColorSpaceCreateCalibratedRGB */
1176 qcms_profile* qcms_profile_create_rgb_with_gamma(
1177 qcms_CIE_xyY white_point,
1178 qcms_CIE_xyYTRIPLE primaries,
1179 float gamma)
1180 {
1181 qcms_profile* profile = qcms_profile_create();
1182 if (!profile)
1183 return NO_MEM_PROFILE;
1184
1185 if (!set_rgb_colorants(profile, white_point, primaries)) {
1186 qcms_profile_release(profile);
1187 return INVALID_PROFILE;
1188 }
1189
1190 profile->redTRC = curve_from_gamma(gamma);
1191 profile->blueTRC = curve_from_gamma(gamma);
1192 profile->greenTRC = curve_from_gamma(gamma);
1193
1194 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
1195 qcms_profile_release(profile);
1196 return NO_MEM_PROFILE;
1197 }
1198
1199 profile->class = DISPLAY_DEVICE_PROFILE;
1200 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
1201 profile->color_space = RGB_SIGNATURE;
1202 profile->pcs = XYZ_SIGNATURE;
1203 return profile;
1204 }
1205
1206 qcms_profile* qcms_profile_create_rgb_with_table(
1207 qcms_CIE_xyY white_point,
1208 qcms_CIE_xyYTRIPLE primaries,
1209 uint16_t *table, int num_entries)
1210 {
1211 qcms_profile* profile = qcms_profile_create();
1212 if (!profile)
1213 return NO_MEM_PROFILE;
1214
1215 if (!set_rgb_colorants(profile, white_point, primaries)) {
1216 qcms_profile_release(profile);
1217 return INVALID_PROFILE;
1218 }
1219
1220 profile->redTRC = curve_from_table(table, num_entries);
1221 profile->blueTRC = curve_from_table(table, num_entries);
1222 profile->greenTRC = curve_from_table(table, num_entries);
1223
1224 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
1225 qcms_profile_release(profile);
1226 return NO_MEM_PROFILE;
1227 }
1228
1229 profile->class = DISPLAY_DEVICE_PROFILE;
1230 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
1231 profile->color_space = RGB_SIGNATURE;
1232 profile->pcs = XYZ_SIGNATURE;
1233 return profile;
1234 }
1235
1236 /* from lcms: cmsWhitePointFromTemp */
1237 /* tempK must be >= 4000. and <= 25000.
1238 * Invalid values of tempK will return
1239 * (x,y,Y) = (-1.0, -1.0, -1.0)
1240 * similar to argyll: icx_DTEMP2XYZ() */
1241 qcms_CIE_xyY white_point_from_temp(int temp_K)
1242 {
1243 qcms_CIE_xyY white_point;
1244 double x, y;
1245 double T, T2, T3;
1246 // double M1, M2;
1247
1248 // No optimization provided.
1249 T = temp_K;
1250 T2 = T*T; // Square
1251 T3 = T2*T; // Cube
1252
1253 // For correlated color temperature (T) between 4000K and 7000K:
1254 if (T >= 4000. && T <= 7000.) {
1255 x = -4.6070*(1E9/T3) + 2.9678*(1E6/T2) + 0.09911*(1E3/T) + 0.244 063;
1256 } else {
1257 // or for correlated color temperature (T) between 7000K and 250 00K:
1258 if (T > 7000.0 && T <= 25000.0) {
1259 x = -2.0064*(1E9/T3) + 1.9018*(1E6/T2) + 0.24748*(1E3/T) + 0.237040;
1260 } else {
1261 // Invalid tempK
1262 white_point.x = -1.0;
1263 white_point.y = -1.0;
1264 white_point.Y = -1.0;
1265
1266 assert(0 && "invalid temp");
1267
1268 return white_point;
1269 }
1270 }
1271
1272 // Obtain y(x)
1273
1274 y = -3.000*(x*x) + 2.870*x - 0.275;
1275
1276 // wave factors (not used, but here for futures extensions)
1277
1278 // M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y);
1279 // M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y);
1280
1281 // Fill white_point struct
1282 white_point.x = x;
1283 white_point.y = y;
1284 white_point.Y = 1.0;
1285
1286 return white_point;
1287 }
1288
1289 qcms_profile* qcms_profile_sRGB(void)
1290 {
1291 qcms_profile *profile;
1292 uint16_t *table;
1293
1294 // Standard Illuminant D65 in XYZ coordinates, which is the standard
1295 // sRGB IEC61966-2.1 / Rec.709 profile reference media white point.
1296 struct XYZNumber D65 = {
1297 0xf351, 0x10000, 0x116cc // ( 0.950455, 1.000000, 1.089050 )
1298 };
1299
1300 // sRGB IEC61966-2.1 / Rec.709 color profile primaries, chromatically
1301 // adapted (via Bradford procedures) to D50 white point.
1302 // For details, refer to crbug/580917
1303 #if 0
1304 // lindbloom: ASTM E308-01 D50 White point.
1305 s15Fixed16Number primaries[3][3] = {
1306 { 0x06fa3, 0x06294, 0x024a1 }, // ( 0.436081, 0.385071, 0.143082 )
1307 { 0x038f6, 0x0b785, 0x00f85 }, // ( 0.222504, 0.716873, 0.060623 )
1308 { 0x00391, 0x018dc, 0x0b6d4 }, // ( 0.013931, 0.097107, 0.714172 )
1309 };
1310 #else
1311 // ninedegreesbelow: ICC D50 White point.
1312 s15Fixed16Number primaries[3][3] = {
1313 { 0x06fa0, 0x06296, 0x024a0 }, // ( 0.436035, 0.385101, 0.143066 )
1314 { 0x038f2, 0x0b789, 0x00f85 }, // ( 0.222443, 0.716934, 0.060623 )
1315 { 0x0038f, 0x018da, 0x0b6c4 }, // ( 0.013901, 0.097076, 0.713928 )
1316 };
1317 #endif
1318
1319 table = build_sRGB_gamma_table(1024);
1320
1321 if (!table)
1322 return NO_MEM_PROFILE;
1323
1324 profile = qcms_profile_create();
1325
1326 if (!profile) {
1327 free(table);
1328 return NO_MEM_PROFILE;
1329 }
1330
1331 profile->redTRC = curve_from_table(table, 1024);
1332 profile->blueTRC = curve_from_table(table, 1024);
1333 profile->greenTRC = curve_from_table(table, 1024);
1334
1335 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
1336 qcms_profile_release(profile);
1337 free(table);
1338 return NO_MEM_PROFILE;
1339 }
1340
1341 profile->redColorant.X = primaries[0][0];
1342 profile->redColorant.Y = primaries[1][0];
1343 profile->redColorant.Z = primaries[2][0];
1344
1345 profile->greenColorant.X = primaries[0][1];
1346 profile->greenColorant.Y = primaries[1][1];
1347 profile->greenColorant.Z = primaries[2][1];
1348
1349 profile->blueColorant.X = primaries[0][2];
1350 profile->blueColorant.Y = primaries[1][2];
1351 profile->blueColorant.Z = primaries[2][2];
1352
1353 profile->mediaWhitePoint.X = D65.X;
1354 profile->mediaWhitePoint.Y = D65.Y;
1355 profile->mediaWhitePoint.Z = D65.Z;
1356
1357 profile->class = DISPLAY_DEVICE_PROFILE;
1358 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
1359 profile->color_space = RGB_SIGNATURE;
1360 profile->pcs = XYZ_SIGNATURE;
1361
1362 strcpy(profile->description, "sRGB IEC61966-2.1");
1363
1364 free(table);
1365
1366 return profile;
1367 }
1368
1369 /* qcms_profile_from_memory does not hold a reference to the memory passed in */
1370 qcms_profile* qcms_profile_from_memory(const void *mem, size_t size)
1371 {
1372 uint32_t length;
1373 struct mem_source source;
1374 struct mem_source *src = &source;
1375 struct tag_index index;
1376 qcms_profile *profile;
1377
1378 source.buf = mem;
1379 source.size = size;
1380 source.valid = true;
1381
1382 if (size < 4)
1383 return INVALID_PROFILE;
1384
1385 length = read_u32(src, 0);
1386 if (length <= size) {
1387 // shrink the area that we can read if appropriate
1388 source.size = length;
1389 } else {
1390 return INVALID_PROFILE;
1391 }
1392
1393 /* ensure that the profile size is sane so it's easier to reason about * /
1394 if (source.size <= 64 || source.size >= MAX_PROFILE_SIZE)
1395 return INVALID_PROFILE;
1396
1397 profile = qcms_profile_create();
1398 if (!profile)
1399 return NO_MEM_PROFILE;
1400
1401 check_CMM_type_signature(src);
1402 read_profile_version(profile, src);
1403 read_class_signature(profile, src);
1404 read_rendering_intent(profile, src);
1405 read_color_space(profile, src);
1406 read_pcs(profile, src);
1407 //TODO read rest of profile stuff
1408
1409 if (!src->valid)
1410 goto invalid_profile;
1411
1412 index = read_tag_table(profile, src);
1413 if (!src->valid || !index.tags)
1414 goto invalid_tag_table;
1415
1416 if (!read_tag_descType(profile, src, index, TAG_desc))
1417 goto invalid_tag_table;
1418 #if defined(__APPLE__)
1419 if (!read_tag_dscmType(profile, src, index, TAG_dscm))
1420 goto invalid_tag_table;
1421 if (!read_tag_mmodType(profile, src, index, TAG_mmod))
1422 goto invalid_tag_table;
1423 #endif // __APPLE__
1424
1425 if (find_tag(index, TAG_CHAD)) {
1426 profile->chromaticAdaption = read_tag_s15Fixed16ArrayType(src, i ndex, TAG_CHAD);
1427 } else {
1428 profile->chromaticAdaption.invalid = true; //Signal the data is not present
1429 }
1430
1431 if (find_tag(index, TAG_vcgt)) {
1432 if (!read_tag_vcgtType(profile, src, index))
1433 goto invalid_tag_table;
1434 }
1435
1436 if (profile->class == DISPLAY_DEVICE_PROFILE || profile->class == INPUT_ DEVICE_PROFILE ||
1437 profile->class == OUTPUT_DEVICE_PROFILE || profile->class == COLOR_ SPACE_PROFILE) {
1438 if (profile->color_space == RGB_SIGNATURE) {
1439 if (find_tag(index, TAG_A2B0)) {
1440 if (read_u32(src, find_tag(index, TAG_A2B0)->off set) == LUT8_TYPE ||
1441 read_u32(src, find_tag(index, TAG_A2B0)->off set) == LUT16_TYPE) {
1442 profile->A2B0 = read_tag_lutType(src, in dex, TAG_A2B0);
1443 } else if (read_u32(src, find_tag(index, TAG_A2B 0)->offset) == LUT_MAB_TYPE) {
1444 profile->mAB = read_tag_lutmABType(src, index, TAG_A2B0);
1445 }
1446 }
1447 if (find_tag(index, TAG_B2A0)) {
1448 if (read_u32(src, find_tag(index, TAG_B2A0)->off set) == LUT8_TYPE ||
1449 read_u32(src, find_tag(index, TAG_B2A0)->off set) == LUT16_TYPE) {
1450 profile->B2A0 = read_tag_lutType(src, in dex, TAG_B2A0);
1451 } else if (read_u32(src, find_tag(index, TAG_B2A 0)->offset) == LUT_MBA_TYPE) {
1452 profile->mBA = read_tag_lutmABType(src, index, TAG_B2A0);
1453 }
1454 }
1455 if (find_tag(index, TAG_rXYZ) || !qcms_supports_iccv4) {
1456 profile->redColorant = read_tag_XYZType(src, ind ex, TAG_rXYZ);
1457 profile->greenColorant = read_tag_XYZType(src, i ndex, TAG_gXYZ);
1458 profile->blueColorant = read_tag_XYZType(src, in dex, TAG_bXYZ);
1459 }
1460
1461 if (!src->valid)
1462 goto invalid_tag_table;
1463
1464 if (find_tag(index, TAG_rTRC) || !qcms_supports_iccv4) {
1465 profile->redTRC = read_tag_curveType(src, index, TAG_rTRC);
1466 profile->greenTRC = read_tag_curveType(src, inde x, TAG_gTRC);
1467 profile->blueTRC = read_tag_curveType(src, index , TAG_bTRC);
1468
1469 if (!profile->redTRC || !profile->blueTRC || !pr ofile->greenTRC)
1470 goto invalid_tag_table;
1471 }
1472 } else if (profile->color_space == GRAY_SIGNATURE) {
1473
1474 profile->grayTRC = read_tag_curveType(src, index, TAG_kT RC);
1475 if (!profile->grayTRC)
1476 goto invalid_tag_table;
1477
1478 } else {
1479 assert(0 && "read_color_space protects against entering here");
1480 goto invalid_tag_table;
1481 }
1482 } else {
1483 goto invalid_tag_table;
1484 }
1485
1486 // Profiles other than DeviceLink should have a media white point.
1487 // Here we read it if present.
1488 if (find_tag(index, TAG_wtpt)) {
1489 profile->mediaWhitePoint = read_tag_XYZType(src, index, TAG_wtpt );
1490 }
1491
1492 if (!src->valid)
1493 goto invalid_tag_table;
1494
1495 free(index.tags);
1496 return profile;
1497
1498 invalid_tag_table:
1499 if (index.tags)
1500 free(index.tags);
1501 invalid_profile:
1502 qcms_profile_release(profile);
1503 return INVALID_PROFILE;
1504 }
1505
1506 qcms_bool qcms_profile_match(qcms_profile *p1, qcms_profile *p2)
1507 {
1508 return memcmp(p1->description, p2->description, sizeof p1->description) == 0;
1509 }
1510
1511 const char* qcms_profile_get_description(qcms_profile *profile)
1512 {
1513 return profile->description;
1514 }
1515
1516 qcms_intent qcms_profile_get_rendering_intent(qcms_profile *profile)
1517 {
1518 return profile->rendering_intent;
1519 }
1520
1521 qcms_color_space qcms_profile_get_color_space(qcms_profile *profile)
1522 {
1523 return profile->color_space;
1524 }
1525
1526 unsigned qcms_profile_get_version(qcms_profile *profile)
1527 {
1528 return profile->icc_version & 0xffff;
1529 }
1530
1531 size_t qcms_profile_get_vcgt_channel_length(qcms_profile *profile)
1532 {
1533 return profile->vcgt.length;
1534 }
1535
1536 // Check unsigned short is uint16_t.
1537 typedef char assert_short_not_16b[(sizeof(unsigned short) == sizeof(uint16_t)) ? 1 : -1];
1538
1539 qcms_bool qcms_profile_get_vcgt_rgb_channels(qcms_profile *profile, unsigned sho rt *data)
1540 {
1541 size_t vcgt_channel_bytes = qcms_profile_get_vcgt_channel_length(profile ) * sizeof(uint16_t);
1542
1543 if (!vcgt_channel_bytes || !data)
1544 return false;
1545
1546 memcpy(data, profile->vcgt.data, 3 * vcgt_channel_bytes);
1547 return true;
1548 }
1549
1550 static void lut_release(struct lutType *lut)
1551 {
1552 free(lut);
1553 }
1554
1555 void qcms_profile_release(qcms_profile *profile)
1556 {
1557 if (profile->output_table_r)
1558 precache_release(profile->output_table_r);
1559 if (profile->output_table_g)
1560 precache_release(profile->output_table_g);
1561 if (profile->output_table_b)
1562 precache_release(profile->output_table_b);
1563
1564 if (profile->A2B0)
1565 lut_release(profile->A2B0);
1566 if (profile->B2A0)
1567 lut_release(profile->B2A0);
1568
1569 if (profile->mAB)
1570 mAB_release(profile->mAB);
1571 if (profile->mBA)
1572 mAB_release(profile->mBA);
1573
1574 if (profile->vcgt.data)
1575 free(profile->vcgt.data);
1576
1577 free(profile->redTRC);
1578 free(profile->blueTRC);
1579 free(profile->greenTRC);
1580 free(profile->grayTRC);
1581 free(profile);
1582 }
1583
1584 #include <stdio.h>
1585
1586 qcms_profile* qcms_profile_from_file(FILE *file)
1587 {
1588 uint32_t length, remaining_length;
1589 qcms_profile *profile;
1590 size_t read_length;
1591 be32 length_be;
1592 void *data;
1593
1594 if (fread(&length_be, 1, sizeof(length_be), file) != sizeof(length_be))
1595 return BAD_VALUE_PROFILE;
1596
1597 length = be32_to_cpu(length_be);
1598 if (length > MAX_PROFILE_SIZE || length < sizeof(length_be))
1599 return BAD_VALUE_PROFILE;
1600
1601 /* allocate room for the entire profile */
1602 data = malloc(length);
1603 if (!data)
1604 return NO_MEM_PROFILE;
1605
1606 /* copy in length to the front so that the buffer will contain the entir e profile */
1607 *((be32*)data) = length_be;
1608 remaining_length = length - sizeof(length_be);
1609
1610 /* read the rest profile */
1611 read_length = fread((unsigned char*)data + sizeof(length_be), 1, remaini ng_length, file);
1612 if (read_length != remaining_length) {
1613 free(data);
1614 return INVALID_PROFILE;
1615 }
1616
1617 profile = qcms_profile_from_memory(data, length);
1618 free(data);
1619 return profile;
1620 }
1621
1622 qcms_profile* qcms_profile_from_path(const char *path)
1623 {
1624 qcms_profile *profile = NULL;
1625 FILE *file = fopen(path, "rb");
1626 if (file) {
1627 profile = qcms_profile_from_file(file);
1628 fclose(file);
1629 }
1630 return profile;
1631 }
1632
1633 #ifdef _WIN32
1634 /* Unicode path version */
1635 qcms_profile* qcms_profile_from_unicode_path(const wchar_t *path)
1636 {
1637 qcms_profile *profile = NULL;
1638 FILE *file = _wfopen(path, L"rb");
1639 if (file) {
1640 profile = qcms_profile_from_file(file);
1641 fclose(file);
1642 }
1643 return profile;
1644 }
1645 #endif
OLDNEW
« no previous file with comments | « third_party/qcms/src/halffloat.h ('k') | third_party/qcms/src/matrix.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698