| OLD | NEW |
| (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 //TODO: do the check? | |
| 150 | |
| 151 } | |
| 152 | |
| 153 static void check_profile_version(struct mem_source *src) | |
| 154 { | |
| 155 | |
| 156 /* | |
| 157 uint8_t major_revision = read_u8(src, 8 + 0); | |
| 158 uint8_t minor_revision = read_u8(src, 8 + 1); | |
| 159 */ | |
| 160 uint8_t reserved1 = read_u8(src, 8 + 2); | |
| 161 uint8_t reserved2 = read_u8(src, 8 + 3); | |
| 162 /* Checking the version doesn't buy us anything | |
| 163 if (major_revision != 0x4) { | |
| 164 if (major_revision > 0x2) | |
| 165 invalid_source(src, "Unsupported major revision"); | |
| 166 if (minor_revision > 0x40) | |
| 167 invalid_source(src, "Unsupported minor revision"); | |
| 168 } | |
| 169 */ | |
| 170 if (reserved1 != 0 || reserved2 != 0) | |
| 171 invalid_source(src, "Invalid reserved bytes"); | |
| 172 } | |
| 173 | |
| 174 #define INPUT_DEVICE_PROFILE 0x73636e72 // 'scnr' | |
| 175 #define DISPLAY_DEVICE_PROFILE 0x6d6e7472 // 'mntr' | |
| 176 #define OUTPUT_DEVICE_PROFILE 0x70727472 // 'prtr' | |
| 177 #define DEVICE_LINK_PROFILE 0x6c696e6b // 'link' | |
| 178 #define COLOR_SPACE_PROFILE 0x73706163 // 'spac' | |
| 179 #define ABSTRACT_PROFILE 0x61627374 // 'abst' | |
| 180 #define NAMED_COLOR_PROFILE 0x6e6d636c // 'nmcl' | |
| 181 | |
| 182 static void read_class_signature(qcms_profile *profile, struct mem_source *mem) | |
| 183 { | |
| 184 profile->class = read_u32(mem, 12); | |
| 185 switch (profile->class) { | |
| 186 case DISPLAY_DEVICE_PROFILE: | |
| 187 case INPUT_DEVICE_PROFILE: | |
| 188 case OUTPUT_DEVICE_PROFILE: | |
| 189 case COLOR_SPACE_PROFILE: | |
| 190 break; | |
| 191 default: | |
| 192 invalid_source(mem, "Invalid Profile/Device Class signa
ture"); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 static void read_color_space(qcms_profile *profile, struct mem_source *mem) | |
| 197 { | |
| 198 profile->color_space = read_u32(mem, 16); | |
| 199 switch (profile->color_space) { | |
| 200 case RGB_SIGNATURE: | |
| 201 case GRAY_SIGNATURE: | |
| 202 break; | |
| 203 default: | |
| 204 invalid_source(mem, "Unsupported colorspace"); | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 static void read_pcs(qcms_profile *profile, struct mem_source *mem) | |
| 209 { | |
| 210 profile->pcs = read_u32(mem, 20); | |
| 211 switch (profile->pcs) { | |
| 212 case XYZ_SIGNATURE: | |
| 213 case LAB_SIGNATURE: | |
| 214 break; | |
| 215 default: | |
| 216 invalid_source(mem, "Unsupported pcs"); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 struct tag | |
| 221 { | |
| 222 uint32_t signature; | |
| 223 uint32_t offset; | |
| 224 uint32_t size; | |
| 225 }; | |
| 226 | |
| 227 struct tag_index { | |
| 228 uint32_t count; | |
| 229 struct tag *tags; | |
| 230 }; | |
| 231 | |
| 232 static struct tag_index read_tag_table(qcms_profile *profile, struct mem_source
*mem) | |
| 233 { | |
| 234 struct tag_index index = {0, NULL}; | |
| 235 unsigned int i; | |
| 236 | |
| 237 index.count = read_u32(mem, 128); | |
| 238 if (index.count > MAX_TAG_COUNT) { | |
| 239 invalid_source(mem, "max number of tags exceeded"); | |
| 240 return index; | |
| 241 } | |
| 242 | |
| 243 index.tags = malloc(sizeof(struct tag)*index.count); | |
| 244 if (index.tags) { | |
| 245 for (i = 0; i < index.count; i++) { | |
| 246 index.tags[i].signature = read_u32(mem, 128 + 4 + 4*i*3)
; | |
| 247 index.tags[i].offset = read_u32(mem, 128 + 4 + 4*i*3
+ 4); | |
| 248 index.tags[i].size = read_u32(mem, 128 + 4 + 4*i*3
+ 8); | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 return index; | |
| 253 } | |
| 254 | |
| 255 // Checks a profile for obvious inconsistencies and returns | |
| 256 // true if the profile looks bogus and should probably be | |
| 257 // ignored. | |
| 258 qcms_bool qcms_profile_is_bogus(qcms_profile *profile) | |
| 259 { | |
| 260 float sum[3], target[3], tolerance[3]; | |
| 261 float rX, rY, rZ, gX, gY, gZ, bX, bY, bZ; | |
| 262 bool negative; | |
| 263 unsigned i; | |
| 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 // Check if any of the XYZ values are negative (see mozilla bug 498245) | |
| 285 // CIEXYZ tristimulus values cannot be negative according to the spec. | |
| 286 negative = | |
| 287 (rX < 0) || (rY < 0) || (rZ < 0) || | |
| 288 (gX < 0) || (gY < 0) || (gZ < 0) || | |
| 289 (bX < 0) || (bY < 0) || (bZ < 0); | |
| 290 | |
| 291 if (negative) | |
| 292 return true; | |
| 293 | |
| 294 | |
| 295 // Sum the values; they should add up to something close to white | |
| 296 sum[0] = rX + gX + bX; | |
| 297 sum[1] = rY + gY + bY; | |
| 298 sum[2] = rZ + gZ + bZ; | |
| 299 | |
| 300 #if defined (_MSC_VER) | |
| 301 #pragma warning(push) | |
| 302 /* Disable double to float truncation warning 4305 */ | |
| 303 #pragma warning(disable:4305) | |
| 304 #endif | |
| 305 // Build our target vector (see mozilla bug 460629) | |
| 306 target[0] = 0.96420; | |
| 307 target[1] = 1.00000; | |
| 308 target[2] = 0.82491; | |
| 309 | |
| 310 // Our tolerance vector - Recommended by Chris Murphy based on | |
| 311 // conversion from the LAB space criterion of no more than 3 in any one | |
| 312 // channel. This is similar to, but slightly more tolerant than Adobe's | |
| 313 // criterion. | |
| 314 tolerance[0] = 0.02; | |
| 315 tolerance[1] = 0.02; | |
| 316 tolerance[2] = 0.04; | |
| 317 | |
| 318 #if defined (_MSC_VER) | |
| 319 /* Restore warnings */ | |
| 320 #pragma warning(pop) | |
| 321 #endif | |
| 322 // Compare with our tolerance | |
| 323 for (i = 0; i < 3; ++i) { | |
| 324 if (!(((sum[i] - tolerance[i]) <= target[i]) && | |
| 325 ((sum[i] + tolerance[i]) >= target[i]))) | |
| 326 return true; | |
| 327 } | |
| 328 | |
| 329 // All Good | |
| 330 return false; | |
| 331 } | |
| 332 | |
| 333 #define TAG_bXYZ 0x6258595a | |
| 334 #define TAG_gXYZ 0x6758595a | |
| 335 #define TAG_rXYZ 0x7258595a | |
| 336 #define TAG_rTRC 0x72545243 | |
| 337 #define TAG_bTRC 0x62545243 | |
| 338 #define TAG_gTRC 0x67545243 | |
| 339 #define TAG_kTRC 0x6b545243 | |
| 340 #define TAG_A2B0 0x41324230 | |
| 341 #define TAG_B2A0 0x42324130 | |
| 342 #define TAG_CHAD 0x63686164 | |
| 343 #define TAG_desc 0x64657363 | |
| 344 | |
| 345 static struct tag *find_tag(struct tag_index index, uint32_t tag_id) | |
| 346 { | |
| 347 unsigned int i; | |
| 348 struct tag *tag = NULL; | |
| 349 for (i = 0; i < index.count; i++) { | |
| 350 if (index.tags[i].signature == tag_id) { | |
| 351 return &index.tags[i]; | |
| 352 } | |
| 353 } | |
| 354 return tag; | |
| 355 } | |
| 356 | |
| 357 #define DESC_TYPE 0x64657363 // 'desc' | |
| 358 #define MLUC_TYPE 0x6d6c7563 // 'mluc' | |
| 359 #define MMOD_TYPE 0x6D6D6F64 // 'mmod' | |
| 360 | |
| 361 static bool read_tag_descType(qcms_profile *profile, struct mem_source *src, str
uct tag_index index, uint32_t tag_id) | |
| 362 { | |
| 363 struct tag *tag = find_tag(index, tag_id); | |
| 364 if (tag) { | |
| 365 const uint32_t limit = sizeof profile->description; | |
| 366 uint32_t offset = tag->offset; | |
| 367 uint32_t type = read_u32(src, offset); | |
| 368 uint32_t length = read_u32(src, offset+8); | |
| 369 uint32_t i, description_offset; | |
| 370 bool mluc = false; | |
| 371 if (length && type == MLUC_TYPE) { | |
| 372 length = read_u32(src, offset+20); | |
| 373 if (!length || (length & 1) || (read_u32(src, offset+12)
!= 12)) | |
| 374 goto invalid_desc_tag; | |
| 375 description_offset = offset + read_u32(src, offset+24); | |
| 376 if (!src->valid) | |
| 377 goto invalid_desc_tag; | |
| 378 mluc = true; | |
| 379 } else if (length && type == DESC_TYPE) { | |
| 380 description_offset = offset + 12; | |
| 381 } else { | |
| 382 goto invalid_desc_tag; | |
| 383 } | |
| 384 if (length >= limit) | |
| 385 length = limit - 1; | |
| 386 for (i = 0; i < length; ++i) { | |
| 387 uint8_t value = read_u8(src, description_offset + i); | |
| 388 if (!src->valid) | |
| 389 goto invalid_desc_tag; | |
| 390 if (mluc && !value) | |
| 391 value = '.'; | |
| 392 profile->description[i] = value; | |
| 393 } | |
| 394 profile->description[length] = 0; | |
| 395 } else { | |
| 396 goto invalid_desc_tag; | |
| 397 } | |
| 398 | |
| 399 if (src->valid) | |
| 400 return true; | |
| 401 | |
| 402 invalid_desc_tag: | |
| 403 invalid_source(src, "invalid description"); | |
| 404 return false; | |
| 405 } | |
| 406 | |
| 407 #if defined(__APPLE__) | |
| 408 | |
| 409 // Use the dscm tag to change profile description "Display" to its more specific
en-localized monitor name, if any. | |
| 410 | |
| 411 #define TAG_dscm 0x6473636D // 'dscm' | |
| 412 | |
| 413 static bool read_tag_dscmType(qcms_profile *profile, struct mem_source *src, str
uct tag_index index, uint32_t tag_id) | |
| 414 { | |
| 415 if (strcmp(profile->description, "Display") != 0) | |
| 416 return true; | |
| 417 | |
| 418 struct tag *tag = find_tag(index, tag_id); | |
| 419 if (tag) { | |
| 420 uint32_t offset = tag->offset; | |
| 421 uint32_t type = read_u32(src, offset); | |
| 422 uint32_t records = read_u32(src, offset+8); | |
| 423 | |
| 424 if (!src->valid || !records || type != MLUC_TYPE) | |
| 425 goto invalid_dscm_tag; | |
| 426 if (read_u32(src, offset+12) != 12) // MLUC record size: bytes | |
| 427 goto invalid_dscm_tag; | |
| 428 | |
| 429 for (uint32_t i = 0; i < records; ++i) { | |
| 430 const uint32_t limit = sizeof profile->description; | |
| 431 const uint16_t isoen = 0x656E; // ISO-3166-1 language 'e
n' | |
| 432 | |
| 433 uint16_t language = read_u16(src, offset + 16 + (i * 12)
+ 0); | |
| 434 uint32_t length = read_u32(src, offset + 16 + (i * 12) +
4); | |
| 435 uint32_t description_offset = read_u32(src, offset + 16
+ (i * 12) + 8); | |
| 436 | |
| 437 if (!src->valid || !length || (length & 1)) | |
| 438 goto invalid_dscm_tag; | |
| 439 if (language != isoen) | |
| 440 continue; | |
| 441 | |
| 442 // Use a prefix to identify the display description sour
ce | |
| 443 strcpy(profile->description, "dscm:"); | |
| 444 length += 5; | |
| 445 | |
| 446 if (length >= limit) | |
| 447 length = limit - 1; | |
| 448 for (uint32_t j = 5; j < length; ++j) { | |
| 449 uint8_t value = read_u8(src, offset + descriptio
n_offset + j - 5); | |
| 450 if (!src->valid) | |
| 451 goto invalid_dscm_tag; | |
| 452 profile->description[j] = value ? value : '.'; | |
| 453 } | |
| 454 profile->description[length] = 0; | |
| 455 break; | |
| 456 } | |
| 457 } | |
| 458 | |
| 459 if (src->valid) | |
| 460 return true; | |
| 461 | |
| 462 invalid_dscm_tag: | |
| 463 invalid_source(src, "invalid dscm tag"); | |
| 464 return false; | |
| 465 } | |
| 466 | |
| 467 // Use the mmod tag to change profile description "Display" to its specific mmod
maker model data, if any. | |
| 468 | |
| 469 #define TAG_mmod 0x6D6D6F64 // 'mmod' | |
| 470 | |
| 471 static bool read_tag_mmodType(qcms_profile *profile, struct mem_source *src, str
uct tag_index index, uint32_t tag_id) | |
| 472 { | |
| 473 if (strcmp(profile->description, "Display") != 0) | |
| 474 return true; | |
| 475 | |
| 476 struct tag *tag = find_tag(index, tag_id); | |
| 477 if (tag) { | |
| 478 const uint8_t length = 4 * 4; // Four 4-byte fields: 'mmod', 0,
maker, model. | |
| 479 | |
| 480 uint32_t offset = tag->offset; | |
| 481 if (tag->size < 40 || read_u32(src, offset) != MMOD_TYPE) | |
| 482 goto invalid_mmod_tag; | |
| 483 | |
| 484 for (uint8_t i = 0; i < length; ++i) { | |
| 485 uint8_t value = read_u8(src, offset + i); | |
| 486 if (!src->valid) | |
| 487 goto invalid_mmod_tag; | |
| 488 profile->description[i] = value ? value : '.'; | |
| 489 } | |
| 490 profile->description[length] = 0; | |
| 491 } | |
| 492 | |
| 493 if (src->valid) | |
| 494 return true; | |
| 495 | |
| 496 invalid_mmod_tag: | |
| 497 invalid_source(src, "invalid mmod tag"); | |
| 498 return false; | |
| 499 } | |
| 500 | |
| 501 #endif // __APPLE__ | |
| 502 | |
| 503 #define XYZ_TYPE 0x58595a20 // 'XYZ ' | |
| 504 #define CURVE_TYPE 0x63757276 // 'curv' | |
| 505 #define PARAMETRIC_CURVE_TYPE 0x70617261 // 'para' | |
| 506 #define LUT16_TYPE 0x6d667432 // 'mft2' | |
| 507 #define LUT8_TYPE 0x6d667431 // 'mft1' | |
| 508 #define LUT_MAB_TYPE 0x6d414220 // 'mAB ' | |
| 509 #define LUT_MBA_TYPE 0x6d424120 // 'mBA ' | |
| 510 #define CHROMATIC_TYPE 0x73663332 // 'sf32' | |
| 511 | |
| 512 static struct matrix read_tag_s15Fixed16ArrayType(struct mem_source *src, struct
tag_index index, uint32_t tag_id) | |
| 513 { | |
| 514 struct tag *tag = find_tag(index, tag_id); | |
| 515 struct matrix matrix; | |
| 516 if (tag) { | |
| 517 uint8_t i; | |
| 518 uint32_t offset = tag->offset; | |
| 519 uint32_t type = read_u32(src, offset); | |
| 520 | |
| 521 // Check mandatory type signature for s16Fixed16ArrayType | |
| 522 if (type != CHROMATIC_TYPE) { | |
| 523 invalid_source(src, "unexpected type, expected 'sf32'"); | |
| 524 } | |
| 525 | |
| 526 for (i = 0; i < 9; i++) { | |
| 527 matrix.m[i/3][i%3] = s15Fixed16Number_to_float(read_s15F
ixed16Number(src, offset+8+i*4)); | |
| 528 } | |
| 529 matrix.invalid = false; | |
| 530 } else { | |
| 531 matrix.invalid = true; | |
| 532 invalid_source(src, "missing sf32tag"); | |
| 533 } | |
| 534 return matrix; | |
| 535 } | |
| 536 | |
| 537 static struct XYZNumber read_tag_XYZType(struct mem_source *src, struct tag_inde
x index, uint32_t tag_id) | |
| 538 { | |
| 539 struct XYZNumber num = {0, 0, 0}; | |
| 540 struct tag *tag = find_tag(index, tag_id); | |
| 541 if (tag) { | |
| 542 uint32_t offset = tag->offset; | |
| 543 | |
| 544 uint32_t type = read_u32(src, offset); | |
| 545 if (type != XYZ_TYPE) | |
| 546 invalid_source(src, "unexpected type, expected XYZ"); | |
| 547 num.X = read_s15Fixed16Number(src, offset+8); | |
| 548 num.Y = read_s15Fixed16Number(src, offset+12); | |
| 549 num.Z = read_s15Fixed16Number(src, offset+16); | |
| 550 } else { | |
| 551 invalid_source(src, "missing xyztag"); | |
| 552 } | |
| 553 return num; | |
| 554 } | |
| 555 | |
| 556 // Read the tag at a given offset rather then the tag_index. | |
| 557 // This method is used when reading mAB tags where nested curveType are | |
| 558 // present that are not part of the tag_index. | |
| 559 static struct curveType *read_curveType(struct mem_source *src, uint32_t offset,
uint32_t *len) | |
| 560 { | |
| 561 static const uint32_t COUNT_TO_LENGTH[5] = {1, 3, 4, 5, 7}; | |
| 562 struct curveType *curve = NULL; | |
| 563 uint32_t type = read_u32(src, offset); | |
| 564 uint32_t count; | |
| 565 int i; | |
| 566 | |
| 567 if (type != CURVE_TYPE && type != PARAMETRIC_CURVE_TYPE) { | |
| 568 invalid_source(src, "unexpected type, expected CURV or PARA"); | |
| 569 return NULL; | |
| 570 } | |
| 571 | |
| 572 if (type == CURVE_TYPE) { | |
| 573 count = read_u32(src, offset+8); | |
| 574 | |
| 575 #define MAX_CURVE_ENTRIES 40000 //arbitrary | |
| 576 if (count > MAX_CURVE_ENTRIES) { | |
| 577 invalid_source(src, "curve size too large"); | |
| 578 return NULL; | |
| 579 } | |
| 580 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*c
ount); | |
| 581 if (!curve) | |
| 582 return NULL; | |
| 583 | |
| 584 curve->count = count; | |
| 585 curve->type = type; | |
| 586 | |
| 587 for (i=0; i<count; i++) { | |
| 588 curve->data[i] = read_u16(src, offset + 12 + i*2); | |
| 589 } | |
| 590 *len = 12 + count * 2; | |
| 591 } else { //PARAMETRIC_CURVE_TYPE | |
| 592 count = read_u16(src, offset+8); | |
| 593 | |
| 594 if (count > 4) { | |
| 595 invalid_source(src, "parametric function type not suppor
ted."); | |
| 596 return NULL; | |
| 597 } | |
| 598 | |
| 599 curve = malloc(sizeof(struct curveType)); | |
| 600 if (!curve) | |
| 601 return NULL; | |
| 602 | |
| 603 curve->count = count; | |
| 604 curve->type = type; | |
| 605 | |
| 606 for (i=0; i < COUNT_TO_LENGTH[count]; i++) { | |
| 607 curve->parameter[i] = s15Fixed16Number_to_float(read_s15
Fixed16Number(src, offset + 12 + i*4)); | |
| 608 } | |
| 609 *len = 12 + COUNT_TO_LENGTH[count] * 4; | |
| 610 | |
| 611 if ((count == 1 || count == 2)) { | |
| 612 /* we have a type 1 or type 2 function that has a divisi
on by 'a' */ | |
| 613 float a = curve->parameter[1]; | |
| 614 if (a == 0.f) | |
| 615 invalid_source(src, "parametricCurve definition
causes division by zero."); | |
| 616 } | |
| 617 } | |
| 618 | |
| 619 return curve; | |
| 620 } | |
| 621 | |
| 622 static struct curveType *read_tag_curveType(struct mem_source *src, struct tag_i
ndex index, uint32_t tag_id) | |
| 623 { | |
| 624 struct tag *tag = find_tag(index, tag_id); | |
| 625 struct curveType *curve = NULL; | |
| 626 if (tag) { | |
| 627 uint32_t len; | |
| 628 return read_curveType(src, tag->offset, &len); | |
| 629 } else { | |
| 630 invalid_source(src, "missing curvetag"); | |
| 631 } | |
| 632 | |
| 633 return curve; | |
| 634 } | |
| 635 | |
| 636 #define MAX_CLUT_SIZE 500000 // arbitrary | |
| 637 #define MAX_CHANNELS 10 // arbitrary | |
| 638 static void read_nested_curveType(struct mem_source *src, struct curveType *(*cu
rveArray)[MAX_CHANNELS], uint8_t num_channels, uint32_t curve_offset) | |
| 639 { | |
| 640 uint32_t channel_offset = 0; | |
| 641 int i; | |
| 642 for (i = 0; i < num_channels; i++) { | |
| 643 uint32_t tag_len = ~0; | |
| 644 | |
| 645 (*curveArray)[i] = read_curveType(src, curve_offset + channel_of
fset, &tag_len); | |
| 646 if (!(*curveArray)[i]) { | |
| 647 invalid_source(src, "invalid nested curveType curve"); | |
| 648 } | |
| 649 | |
| 650 if (tag_len == ~0) { | |
| 651 invalid_source(src, "invalid nested curveType tag length
"); | |
| 652 return; | |
| 653 } | |
| 654 | |
| 655 channel_offset += tag_len; | |
| 656 // 4 byte aligned | |
| 657 if ((tag_len % 4) != 0) | |
| 658 channel_offset += 4 - (tag_len % 4); | |
| 659 } | |
| 660 } | |
| 661 | |
| 662 static void mAB_release(struct lutmABType *lut) | |
| 663 { | |
| 664 uint8_t i; | |
| 665 | |
| 666 for (i = 0; i < lut->num_in_channels; i++){ | |
| 667 free(lut->a_curves[i]); | |
| 668 } | |
| 669 for (i = 0; i < lut->num_out_channels; i++){ | |
| 670 free(lut->b_curves[i]); | |
| 671 free(lut->m_curves[i]); | |
| 672 } | |
| 673 free(lut); | |
| 674 } | |
| 675 | |
| 676 /* See section 10.10 for specs */ | |
| 677 static struct lutmABType *read_tag_lutmABType(struct mem_source *src, struct tag
_index index, uint32_t tag_id) | |
| 678 { | |
| 679 struct tag *tag = find_tag(index, tag_id); | |
| 680 uint32_t offset = tag->offset; | |
| 681 uint32_t a_curve_offset, b_curve_offset, m_curve_offset; | |
| 682 uint32_t matrix_offset; | |
| 683 uint32_t clut_offset; | |
| 684 uint32_t clut_size = 1; | |
| 685 uint8_t clut_precision; | |
| 686 uint32_t type = read_u32(src, offset); | |
| 687 uint8_t num_in_channels, num_out_channels; | |
| 688 struct lutmABType *lut; | |
| 689 int i; | |
| 690 | |
| 691 if (type != LUT_MAB_TYPE && type != LUT_MBA_TYPE) { | |
| 692 return NULL; | |
| 693 } | |
| 694 | |
| 695 num_in_channels = read_u8(src, offset + 8); | |
| 696 num_out_channels = read_u8(src, offset + 8); | |
| 697 if (num_in_channels > MAX_CHANNELS || num_out_channels > MAX_CHANNELS) | |
| 698 return NULL; | |
| 699 | |
| 700 // We require 3in/out channels since we only support RGB->XYZ (or RGB->L
AB) | |
| 701 // XXX: If we remove this restriction make sure that the number of chann
els | |
| 702 // is less or equal to the maximum number of mAB curves in qcmsint.
h | |
| 703 // also check for clut_size overflow. Also make sure it's != 0 | |
| 704 if (num_in_channels != 3 || num_out_channels != 3) | |
| 705 return NULL; | |
| 706 | |
| 707 // some of this data is optional and is denoted by a zero offset | |
| 708 // we also use this to track their existance | |
| 709 a_curve_offset = read_u32(src, offset + 28); | |
| 710 clut_offset = read_u32(src, offset + 24); | |
| 711 m_curve_offset = read_u32(src, offset + 20); | |
| 712 matrix_offset = read_u32(src, offset + 16); | |
| 713 b_curve_offset = read_u32(src, offset + 12); | |
| 714 | |
| 715 // Convert offsets relative to the tag to relative to the profile | |
| 716 // preserve zero for optional fields | |
| 717 if (a_curve_offset) | |
| 718 a_curve_offset += offset; | |
| 719 if (clut_offset) | |
| 720 clut_offset += offset; | |
| 721 if (m_curve_offset) | |
| 722 m_curve_offset += offset; | |
| 723 if (matrix_offset) | |
| 724 matrix_offset += offset; | |
| 725 if (b_curve_offset) | |
| 726 b_curve_offset += offset; | |
| 727 | |
| 728 if (clut_offset) { | |
| 729 assert (num_in_channels == 3); | |
| 730 // clut_size can not overflow since lg(256^num_in_channels) = 24
bits. | |
| 731 for (i = 0; i < num_in_channels; i++) { | |
| 732 clut_size *= read_u8(src, clut_offset + i); | |
| 733 if (clut_size == 0) { | |
| 734 invalid_source(src, "bad clut_size"); | |
| 735 } | |
| 736 } | |
| 737 } else { | |
| 738 clut_size = 0; | |
| 739 } | |
| 740 | |
| 741 // 24bits * 3 won't overflow either | |
| 742 clut_size = clut_size * num_out_channels; | |
| 743 | |
| 744 if (clut_size > MAX_CLUT_SIZE) | |
| 745 return NULL; | |
| 746 | |
| 747 lut = malloc(sizeof(struct lutmABType) + (clut_size) * sizeof(float)); | |
| 748 if (!lut) | |
| 749 return NULL; | |
| 750 // we'll fill in the rest below | |
| 751 memset(lut, 0, sizeof(struct lutmABType)); | |
| 752 lut->clut_table = &lut->clut_table_data[0]; | |
| 753 | |
| 754 for (i = 0; i < num_in_channels; i++) { | |
| 755 lut->num_grid_points[i] = read_u8(src, clut_offset + i); | |
| 756 if (lut->num_grid_points[i] == 0) { | |
| 757 invalid_source(src, "bad grid_points"); | |
| 758 } | |
| 759 } | |
| 760 | |
| 761 // Reverse the processing of transformation elements for mBA type. | |
| 762 lut->reversed = (type == LUT_MBA_TYPE); | |
| 763 | |
| 764 lut->num_in_channels = num_in_channels; | |
| 765 lut->num_out_channels = num_out_channels; | |
| 766 | |
| 767 if (matrix_offset) { | |
| 768 // read the matrix if we have it | |
| 769 lut->e00 = read_s15Fixed16Number(src, matrix_offset+4*0); | |
| 770 lut->e01 = read_s15Fixed16Number(src, matrix_offset+4*1); | |
| 771 lut->e02 = read_s15Fixed16Number(src, matrix_offset+4*2); | |
| 772 lut->e10 = read_s15Fixed16Number(src, matrix_offset+4*3); | |
| 773 lut->e11 = read_s15Fixed16Number(src, matrix_offset+4*4); | |
| 774 lut->e12 = read_s15Fixed16Number(src, matrix_offset+4*5); | |
| 775 lut->e20 = read_s15Fixed16Number(src, matrix_offset+4*6); | |
| 776 lut->e21 = read_s15Fixed16Number(src, matrix_offset+4*7); | |
| 777 lut->e22 = read_s15Fixed16Number(src, matrix_offset+4*8); | |
| 778 lut->e03 = read_s15Fixed16Number(src, matrix_offset+4*9); | |
| 779 lut->e13 = read_s15Fixed16Number(src, matrix_offset+4*10); | |
| 780 lut->e23 = read_s15Fixed16Number(src, matrix_offset+4*11); | |
| 781 } | |
| 782 | |
| 783 if (a_curve_offset) { | |
| 784 read_nested_curveType(src, &lut->a_curves, num_in_channels, a_cu
rve_offset); | |
| 785 } | |
| 786 if (m_curve_offset) { | |
| 787 read_nested_curveType(src, &lut->m_curves, num_out_channels, m_c
urve_offset); | |
| 788 } | |
| 789 if (b_curve_offset) { | |
| 790 read_nested_curveType(src, &lut->b_curves, num_out_channels, b_c
urve_offset); | |
| 791 } else { | |
| 792 invalid_source(src, "B curves required"); | |
| 793 } | |
| 794 | |
| 795 if (clut_offset) { | |
| 796 clut_precision = read_u8(src, clut_offset + 16); | |
| 797 if (clut_precision == 1) { | |
| 798 for (i = 0; i < clut_size; i++) { | |
| 799 lut->clut_table[i] = uInt8Number_to_float(read_u
Int8Number(src, clut_offset + 20 + i*1)); | |
| 800 } | |
| 801 } else if (clut_precision == 2) { | |
| 802 for (i = 0; i < clut_size; i++) { | |
| 803 lut->clut_table[i] = uInt16Number_to_float(read_
uInt16Number(src, clut_offset + 20 + i*2)); | |
| 804 } | |
| 805 } else { | |
| 806 invalid_source(src, "Invalid clut precision"); | |
| 807 } | |
| 808 } | |
| 809 | |
| 810 if (!src->valid) { | |
| 811 mAB_release(lut); | |
| 812 return NULL; | |
| 813 } | |
| 814 | |
| 815 return lut; | |
| 816 } | |
| 817 | |
| 818 static struct lutType *read_tag_lutType(struct mem_source *src, struct tag_index
index, uint32_t tag_id) | |
| 819 { | |
| 820 struct tag *tag = find_tag(index, tag_id); | |
| 821 uint32_t offset = tag->offset; | |
| 822 uint32_t type = read_u32(src, offset); | |
| 823 uint16_t num_input_table_entries; | |
| 824 uint16_t num_output_table_entries; | |
| 825 uint8_t in_chan, grid_points, out_chan; | |
| 826 size_t clut_offset, output_offset; | |
| 827 uint32_t clut_size; | |
| 828 size_t entry_size; | |
| 829 struct lutType *lut; | |
| 830 int i; | |
| 831 | |
| 832 /* I'm not sure why the spec specifies a fixed number of entries for LUT
8 tables even though | |
| 833 * they have room for the num_entries fields */ | |
| 834 if (type == LUT8_TYPE) { | |
| 835 num_input_table_entries = 256; | |
| 836 num_output_table_entries = 256; | |
| 837 entry_size = 1; | |
| 838 } else if (type == LUT16_TYPE) { | |
| 839 num_input_table_entries = read_u16(src, offset + 48); | |
| 840 num_output_table_entries = read_u16(src, offset + 50); | |
| 841 if (num_input_table_entries == 0 || num_output_table_entries ==
0) { | |
| 842 invalid_source(src, "Bad channel count"); | |
| 843 return NULL; | |
| 844 } | |
| 845 entry_size = 2; | |
| 846 } else { | |
| 847 assert(0); // the caller checks that this doesn't happen | |
| 848 invalid_source(src, "Unexpected lut type"); | |
| 849 return NULL; | |
| 850 } | |
| 851 | |
| 852 in_chan = read_u8(src, offset + 8); | |
| 853 out_chan = read_u8(src, offset + 9); | |
| 854 grid_points = read_u8(src, offset + 10); | |
| 855 | |
| 856 clut_size = pow(grid_points, in_chan); | |
| 857 if (clut_size > MAX_CLUT_SIZE) { | |
| 858 invalid_source(src, "CLUT too large"); | |
| 859 return NULL; | |
| 860 } | |
| 861 | |
| 862 if (in_chan != 3 || out_chan != 3) { | |
| 863 invalid_source(src, "CLUT only supports RGB"); | |
| 864 return NULL; | |
| 865 } | |
| 866 | |
| 867 lut = malloc(sizeof(struct lutType) + (num_input_table_entries * in_chan
+ clut_size*out_chan + num_output_table_entries * out_chan)*sizeof(float)); | |
| 868 if (!lut) { | |
| 869 invalid_source(src, "CLUT too large"); | |
| 870 return NULL; | |
| 871 } | |
| 872 | |
| 873 /* compute the offsets of tables */ | |
| 874 lut->input_table = &lut->table_data[0]; | |
| 875 lut->clut_table = &lut->table_data[in_chan*num_input_table_entries]; | |
| 876 lut->output_table = &lut->table_data[in_chan*num_input_table_entries + c
lut_size*out_chan]; | |
| 877 | |
| 878 lut->num_input_table_entries = num_input_table_entries; | |
| 879 lut->num_output_table_entries = num_output_table_entries; | |
| 880 lut->num_input_channels = in_chan; | |
| 881 lut->num_output_channels = out_chan; | |
| 882 lut->num_clut_grid_points = grid_points; | |
| 883 lut->e00 = read_s15Fixed16Number(src, offset+12); | |
| 884 lut->e01 = read_s15Fixed16Number(src, offset+16); | |
| 885 lut->e02 = read_s15Fixed16Number(src, offset+20); | |
| 886 lut->e10 = read_s15Fixed16Number(src, offset+24); | |
| 887 lut->e11 = read_s15Fixed16Number(src, offset+28); | |
| 888 lut->e12 = read_s15Fixed16Number(src, offset+32); | |
| 889 lut->e20 = read_s15Fixed16Number(src, offset+36); | |
| 890 lut->e21 = read_s15Fixed16Number(src, offset+40); | |
| 891 lut->e22 = read_s15Fixed16Number(src, offset+44); | |
| 892 | |
| 893 for (i = 0; i < lut->num_input_table_entries * in_chan; i++) { | |
| 894 if (type == LUT8_TYPE) { | |
| 895 lut->input_table[i] = uInt8Number_to_float(read_uInt8Num
ber(src, offset + 52 + i * entry_size)); | |
| 896 } else { | |
| 897 lut->input_table[i] = uInt16Number_to_float(read_uInt16N
umber(src, offset + 52 + i * entry_size)); | |
| 898 } | |
| 899 } | |
| 900 | |
| 901 clut_offset = offset + 52 + lut->num_input_table_entries * in_chan * ent
ry_size; | |
| 902 for (i = 0; i < clut_size * out_chan; i+=3) { | |
| 903 if (type == LUT8_TYPE) { | |
| 904 lut->clut_table[i+0] = uInt8Number_to_float(read_uInt8Nu
mber(src, clut_offset + i*entry_size + 0)); | |
| 905 lut->clut_table[i+1] = uInt8Number_to_float(read_uInt8Nu
mber(src, clut_offset + i*entry_size + 1)); | |
| 906 lut->clut_table[i+2] = uInt8Number_to_float(read_uInt8Nu
mber(src, clut_offset + i*entry_size + 2)); | |
| 907 } else { | |
| 908 lut->clut_table[i+0] = uInt16Number_to_float(read_uInt16
Number(src, clut_offset + i*entry_size + 0)); | |
| 909 lut->clut_table[i+1] = uInt16Number_to_float(read_uInt16
Number(src, clut_offset + i*entry_size + 2)); | |
| 910 lut->clut_table[i+2] = uInt16Number_to_float(read_uInt16
Number(src, clut_offset + i*entry_size + 4)); | |
| 911 } | |
| 912 } | |
| 913 | |
| 914 output_offset = clut_offset + clut_size * out_chan * entry_size; | |
| 915 for (i = 0; i < lut->num_output_table_entries * out_chan; i++) { | |
| 916 if (type == LUT8_TYPE) { | |
| 917 lut->output_table[i] = uInt8Number_to_float(read_uInt8Nu
mber(src, output_offset + i*entry_size)); | |
| 918 } else { | |
| 919 lut->output_table[i] = uInt16Number_to_float(read_uInt16
Number(src, output_offset + i*entry_size)); | |
| 920 } | |
| 921 } | |
| 922 | |
| 923 return lut; | |
| 924 } | |
| 925 | |
| 926 static void read_rendering_intent(qcms_profile *profile, struct mem_source *src) | |
| 927 { | |
| 928 profile->rendering_intent = read_u32(src, 64); | |
| 929 switch (profile->rendering_intent) { | |
| 930 case QCMS_INTENT_PERCEPTUAL: | |
| 931 case QCMS_INTENT_SATURATION: | |
| 932 case QCMS_INTENT_RELATIVE_COLORIMETRIC: | |
| 933 case QCMS_INTENT_ABSOLUTE_COLORIMETRIC: | |
| 934 break; | |
| 935 default: | |
| 936 invalid_source(src, "unknown rendering intent"); | |
| 937 } | |
| 938 } | |
| 939 | |
| 940 qcms_profile *qcms_profile_create(void) | |
| 941 { | |
| 942 return calloc(sizeof(qcms_profile), 1); | |
| 943 } | |
| 944 | |
| 945 | |
| 946 | |
| 947 /* build sRGB gamma table */ | |
| 948 /* based on cmsBuildParametricGamma() */ | |
| 949 static uint16_t *build_sRGB_gamma_table(int num_entries) | |
| 950 { | |
| 951 int i; | |
| 952 /* taken from lcms: Build_sRGBGamma() */ | |
| 953 double gamma = 2.4; | |
| 954 double a = 1./1.055; | |
| 955 double b = 0.055/1.055; | |
| 956 double c = 1./12.92; | |
| 957 double d = 0.04045; | |
| 958 | |
| 959 uint16_t *table = malloc(sizeof(uint16_t) * num_entries); | |
| 960 if (!table) | |
| 961 return NULL; | |
| 962 | |
| 963 for (i=0; i<num_entries; i++) { | |
| 964 double x = (double)i / (num_entries-1); | |
| 965 double y, output; | |
| 966 // IEC 61966-2.1 (sRGB) | |
| 967 // Y = (aX + b)^Gamma | X >= d | |
| 968 // Y = cX | X < d | |
| 969 if (x >= d) { | |
| 970 double e = (a*x + b); | |
| 971 if (e > 0) | |
| 972 y = pow(e, gamma); | |
| 973 else | |
| 974 y = 0; | |
| 975 } else { | |
| 976 y = c*x; | |
| 977 } | |
| 978 | |
| 979 // Saturate -- this could likely move to a separate function | |
| 980 output = y * 65535. + .5; | |
| 981 if (output > 65535.) | |
| 982 output = 65535; | |
| 983 if (output < 0) | |
| 984 output = 0; | |
| 985 table[i] = (uint16_t)floor(output); | |
| 986 } | |
| 987 return table; | |
| 988 } | |
| 989 | |
| 990 static struct curveType *curve_from_table(uint16_t *table, int num_entries) | |
| 991 { | |
| 992 struct curveType *curve; | |
| 993 int i; | |
| 994 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entri
es); | |
| 995 if (!curve) | |
| 996 return NULL; | |
| 997 curve->type = CURVE_TYPE; | |
| 998 curve->count = num_entries; | |
| 999 for (i = 0; i < num_entries; i++) { | |
| 1000 curve->data[i] = table[i]; | |
| 1001 } | |
| 1002 return curve; | |
| 1003 } | |
| 1004 | |
| 1005 static uint16_t float_to_u8Fixed8Number(float a) | |
| 1006 { | |
| 1007 if (a > (255.f + 255.f/256)) | |
| 1008 return 0xffff; | |
| 1009 else if (a < 0.f) | |
| 1010 return 0; | |
| 1011 else | |
| 1012 return floor(a*256.f + .5f); | |
| 1013 } | |
| 1014 | |
| 1015 static struct curveType *curve_from_gamma(float gamma) | |
| 1016 { | |
| 1017 struct curveType *curve; | |
| 1018 int num_entries = 1; | |
| 1019 curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entri
es); | |
| 1020 if (!curve) | |
| 1021 return NULL; | |
| 1022 curve->count = num_entries; | |
| 1023 curve->data[0] = float_to_u8Fixed8Number(gamma); | |
| 1024 return curve; | |
| 1025 } | |
| 1026 | |
| 1027 | |
| 1028 //XXX: it would be nice if we had a way of ensuring | |
| 1029 // everything in a profile was initialized regardless of how it was created | |
| 1030 | |
| 1031 //XXX: should this also be taking a black_point? | |
| 1032 /* similar to CGColorSpaceCreateCalibratedRGB */ | |
| 1033 qcms_profile* qcms_profile_create_rgb_with_gamma( | |
| 1034 qcms_CIE_xyY white_point, | |
| 1035 qcms_CIE_xyYTRIPLE primaries, | |
| 1036 float gamma) | |
| 1037 { | |
| 1038 qcms_profile* profile = qcms_profile_create(); | |
| 1039 if (!profile) | |
| 1040 return NO_MEM_PROFILE; | |
| 1041 | |
| 1042 //XXX: should store the whitepoint | |
| 1043 if (!set_rgb_colorants(profile, white_point, primaries)) { | |
| 1044 qcms_profile_release(profile); | |
| 1045 return INVALID_PROFILE; | |
| 1046 } | |
| 1047 | |
| 1048 profile->redTRC = curve_from_gamma(gamma); | |
| 1049 profile->blueTRC = curve_from_gamma(gamma); | |
| 1050 profile->greenTRC = curve_from_gamma(gamma); | |
| 1051 | |
| 1052 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) { | |
| 1053 qcms_profile_release(profile); | |
| 1054 return NO_MEM_PROFILE; | |
| 1055 } | |
| 1056 profile->class = DISPLAY_DEVICE_PROFILE; | |
| 1057 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL; | |
| 1058 profile->color_space = RGB_SIGNATURE; | |
| 1059 return profile; | |
| 1060 } | |
| 1061 | |
| 1062 qcms_profile* qcms_profile_create_rgb_with_table( | |
| 1063 qcms_CIE_xyY white_point, | |
| 1064 qcms_CIE_xyYTRIPLE primaries, | |
| 1065 uint16_t *table, int num_entries) | |
| 1066 { | |
| 1067 qcms_profile* profile = qcms_profile_create(); | |
| 1068 if (!profile) | |
| 1069 return NO_MEM_PROFILE; | |
| 1070 | |
| 1071 //XXX: should store the whitepoint | |
| 1072 if (!set_rgb_colorants(profile, white_point, primaries)) { | |
| 1073 qcms_profile_release(profile); | |
| 1074 return INVALID_PROFILE; | |
| 1075 } | |
| 1076 | |
| 1077 profile->redTRC = curve_from_table(table, num_entries); | |
| 1078 profile->blueTRC = curve_from_table(table, num_entries); | |
| 1079 profile->greenTRC = curve_from_table(table, num_entries); | |
| 1080 | |
| 1081 if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) { | |
| 1082 qcms_profile_release(profile); | |
| 1083 return NO_MEM_PROFILE; | |
| 1084 } | |
| 1085 profile->class = DISPLAY_DEVICE_PROFILE; | |
| 1086 profile->rendering_intent = QCMS_INTENT_PERCEPTUAL; | |
| 1087 profile->color_space = RGB_SIGNATURE; | |
| 1088 return profile; | |
| 1089 } | |
| 1090 | |
| 1091 /* from lcms: cmsWhitePointFromTemp */ | |
| 1092 /* tempK must be >= 4000. and <= 25000. | |
| 1093 * similar to argyll: icx_DTEMP2XYZ() */ | |
| 1094 static qcms_CIE_xyY white_point_from_temp(int temp_K) | |
| 1095 { | |
| 1096 qcms_CIE_xyY white_point; | |
| 1097 double x, y; | |
| 1098 double T, T2, T3; | |
| 1099 // double M1, M2; | |
| 1100 | |
| 1101 // No optimization provided. | |
| 1102 T = temp_K; | |
| 1103 T2 = T*T; // Square | |
| 1104 T3 = T2*T; // Cube | |
| 1105 | |
| 1106 // For correlated color temperature (T) between 4000K and 7000K: | |
| 1107 if (T >= 4000. && T <= 7000.) { | |
| 1108 x = -4.6070*(1E9/T3) + 2.9678*(1E6/T2) + 0.09911*(1E3/T) + 0.244
063; | |
| 1109 } else { | |
| 1110 // or for correlated color temperature (T) between 7000K and 250
00K: | |
| 1111 if (T > 7000.0 && T <= 25000.0) { | |
| 1112 x = -2.0064*(1E9/T3) + 1.9018*(1E6/T2) + 0.24748*(1E3/T)
+ 0.237040; | |
| 1113 } else { | |
| 1114 assert(0 && "invalid temp"); | |
| 1115 } | |
| 1116 } | |
| 1117 | |
| 1118 // Obtain y(x) | |
| 1119 | |
| 1120 y = -3.000*(x*x) + 2.870*x - 0.275; | |
| 1121 | |
| 1122 // wave factors (not used, but here for futures extensions) | |
| 1123 | |
| 1124 // M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y); | |
| 1125 // M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y); | |
| 1126 | |
| 1127 // Fill white_point struct | |
| 1128 white_point.x = x; | |
| 1129 white_point.y = y; | |
| 1130 white_point.Y = 1.0; | |
| 1131 | |
| 1132 return white_point; | |
| 1133 } | |
| 1134 | |
| 1135 qcms_profile* qcms_profile_sRGB(void) | |
| 1136 { | |
| 1137 qcms_profile *profile; | |
| 1138 uint16_t *table; | |
| 1139 | |
| 1140 qcms_CIE_xyYTRIPLE Rec709Primaries = { | |
| 1141 {0.6400, 0.3300, 1.0}, | |
| 1142 {0.3000, 0.6000, 1.0}, | |
| 1143 {0.1500, 0.0600, 1.0} | |
| 1144 }; | |
| 1145 qcms_CIE_xyY D65; | |
| 1146 | |
| 1147 D65 = white_point_from_temp(6504); | |
| 1148 | |
| 1149 table = build_sRGB_gamma_table(1024); | |
| 1150 | |
| 1151 if (!table) | |
| 1152 return NO_MEM_PROFILE; | |
| 1153 | |
| 1154 profile = qcms_profile_create_rgb_with_table(D65, Rec709Primaries, table
, 1024); | |
| 1155 if (profile) | |
| 1156 strcpy(profile->description, "sRGB IEC61966-2.1"); | |
| 1157 | |
| 1158 free(table); | |
| 1159 return profile; | |
| 1160 } | |
| 1161 | |
| 1162 | |
| 1163 /* qcms_profile_from_memory does not hold a reference to the memory passed in */ | |
| 1164 qcms_profile* qcms_profile_from_memory(const void *mem, size_t size) | |
| 1165 { | |
| 1166 uint32_t length; | |
| 1167 struct mem_source source; | |
| 1168 struct mem_source *src = &source; | |
| 1169 struct tag_index index; | |
| 1170 qcms_profile *profile; | |
| 1171 | |
| 1172 source.buf = mem; | |
| 1173 source.size = size; | |
| 1174 source.valid = true; | |
| 1175 | |
| 1176 if (size < 4) | |
| 1177 return INVALID_PROFILE; | |
| 1178 | |
| 1179 length = read_u32(src, 0); | |
| 1180 if (length <= size) { | |
| 1181 // shrink the area that we can read if appropriate | |
| 1182 source.size = length; | |
| 1183 } else { | |
| 1184 return INVALID_PROFILE; | |
| 1185 } | |
| 1186 | |
| 1187 /* ensure that the profile size is sane so it's easier to reason about *
/ | |
| 1188 if (source.size <= 64 || source.size >= MAX_PROFILE_SIZE) | |
| 1189 return INVALID_PROFILE; | |
| 1190 | |
| 1191 profile = qcms_profile_create(); | |
| 1192 if (!profile) | |
| 1193 return NO_MEM_PROFILE; | |
| 1194 | |
| 1195 check_CMM_type_signature(src); | |
| 1196 check_profile_version(src); | |
| 1197 read_class_signature(profile, src); | |
| 1198 read_rendering_intent(profile, src); | |
| 1199 read_color_space(profile, src); | |
| 1200 read_pcs(profile, src); | |
| 1201 //TODO read rest of profile stuff | |
| 1202 | |
| 1203 if (!src->valid) | |
| 1204 goto invalid_profile; | |
| 1205 | |
| 1206 index = read_tag_table(profile, src); | |
| 1207 if (!src->valid || !index.tags) | |
| 1208 goto invalid_tag_table; | |
| 1209 | |
| 1210 if (!read_tag_descType(profile, src, index, TAG_desc)) | |
| 1211 goto invalid_tag_table; | |
| 1212 #if defined(__APPLE__) | |
| 1213 if (!read_tag_dscmType(profile, src, index, TAG_dscm)) | |
| 1214 goto invalid_tag_table; | |
| 1215 if (!read_tag_mmodType(profile, src, index, TAG_mmod)) | |
| 1216 goto invalid_tag_table; | |
| 1217 #endif // __APPLE__ | |
| 1218 | |
| 1219 if (find_tag(index, TAG_CHAD)) { | |
| 1220 profile->chromaticAdaption = read_tag_s15Fixed16ArrayType(src, i
ndex, TAG_CHAD); | |
| 1221 } else { | |
| 1222 profile->chromaticAdaption.invalid = true; //Signal the data is
not present | |
| 1223 } | |
| 1224 | |
| 1225 if (profile->class == DISPLAY_DEVICE_PROFILE || profile->class == INPUT_
DEVICE_PROFILE || | |
| 1226 profile->class == OUTPUT_DEVICE_PROFILE || profile->class == COLOR_
SPACE_PROFILE) { | |
| 1227 if (profile->color_space == RGB_SIGNATURE) { | |
| 1228 if (find_tag(index, TAG_A2B0)) { | |
| 1229 if (read_u32(src, find_tag(index, TAG_A2B0)->off
set) == LUT8_TYPE || | |
| 1230 read_u32(src, find_tag(index, TAG_A2B0)->off
set) == LUT16_TYPE) { | |
| 1231 profile->A2B0 = read_tag_lutType(src, in
dex, TAG_A2B0); | |
| 1232 } else if (read_u32(src, find_tag(index, TAG_A2B
0)->offset) == LUT_MAB_TYPE) { | |
| 1233 profile->mAB = read_tag_lutmABType(src,
index, TAG_A2B0); | |
| 1234 } | |
| 1235 } | |
| 1236 if (find_tag(index, TAG_B2A0)) { | |
| 1237 if (read_u32(src, find_tag(index, TAG_B2A0)->off
set) == LUT8_TYPE || | |
| 1238 read_u32(src, find_tag(index, TAG_B2A0)->off
set) == LUT16_TYPE) { | |
| 1239 profile->B2A0 = read_tag_lutType(src, in
dex, TAG_B2A0); | |
| 1240 } else if (read_u32(src, find_tag(index, TAG_B2A
0)->offset) == LUT_MBA_TYPE) { | |
| 1241 profile->mBA = read_tag_lutmABType(src,
index, TAG_B2A0); | |
| 1242 } | |
| 1243 } | |
| 1244 if (find_tag(index, TAG_rXYZ) || !qcms_supports_iccv4) { | |
| 1245 profile->redColorant = read_tag_XYZType(src, ind
ex, TAG_rXYZ); | |
| 1246 profile->greenColorant = read_tag_XYZType(src, i
ndex, TAG_gXYZ); | |
| 1247 profile->blueColorant = read_tag_XYZType(src, in
dex, TAG_bXYZ); | |
| 1248 } | |
| 1249 | |
| 1250 if (!src->valid) | |
| 1251 goto invalid_tag_table; | |
| 1252 | |
| 1253 if (find_tag(index, TAG_rTRC) || !qcms_supports_iccv4) { | |
| 1254 profile->redTRC = read_tag_curveType(src, index,
TAG_rTRC); | |
| 1255 profile->greenTRC = read_tag_curveType(src, inde
x, TAG_gTRC); | |
| 1256 profile->blueTRC = read_tag_curveType(src, index
, TAG_bTRC); | |
| 1257 | |
| 1258 if (!profile->redTRC || !profile->blueTRC || !pr
ofile->greenTRC) | |
| 1259 goto invalid_tag_table; | |
| 1260 } | |
| 1261 } else if (profile->color_space == GRAY_SIGNATURE) { | |
| 1262 | |
| 1263 profile->grayTRC = read_tag_curveType(src, index, TAG_kT
RC); | |
| 1264 if (!profile->grayTRC) | |
| 1265 goto invalid_tag_table; | |
| 1266 | |
| 1267 } else { | |
| 1268 assert(0 && "read_color_space protects against entering
here"); | |
| 1269 goto invalid_tag_table; | |
| 1270 } | |
| 1271 } else { | |
| 1272 goto invalid_tag_table; | |
| 1273 } | |
| 1274 | |
| 1275 if (!src->valid) | |
| 1276 goto invalid_tag_table; | |
| 1277 | |
| 1278 free(index.tags); | |
| 1279 | |
| 1280 return profile; | |
| 1281 | |
| 1282 invalid_tag_table: | |
| 1283 free(index.tags); | |
| 1284 invalid_profile: | |
| 1285 qcms_profile_release(profile); | |
| 1286 return INVALID_PROFILE; | |
| 1287 } | |
| 1288 | |
| 1289 qcms_bool qcms_profile_match(qcms_profile *p1, qcms_profile *p2) | |
| 1290 { | |
| 1291 return memcmp(p1->description, p2->description, sizeof p1->description) == 0
; | |
| 1292 } | |
| 1293 | |
| 1294 const char* qcms_profile_get_description(qcms_profile *profile) | |
| 1295 { | |
| 1296 return profile->description; | |
| 1297 } | |
| 1298 | |
| 1299 qcms_intent qcms_profile_get_rendering_intent(qcms_profile *profile) | |
| 1300 { | |
| 1301 return profile->rendering_intent; | |
| 1302 } | |
| 1303 | |
| 1304 icColorSpaceSignature | |
| 1305 qcms_profile_get_color_space(qcms_profile *profile) | |
| 1306 { | |
| 1307 return profile->color_space; | |
| 1308 } | |
| 1309 | |
| 1310 static void lut_release(struct lutType *lut) | |
| 1311 { | |
| 1312 free(lut); | |
| 1313 } | |
| 1314 | |
| 1315 void qcms_profile_release(qcms_profile *profile) | |
| 1316 { | |
| 1317 if (profile->output_table_r) | |
| 1318 precache_release(profile->output_table_r); | |
| 1319 if (profile->output_table_g) | |
| 1320 precache_release(profile->output_table_g); | |
| 1321 if (profile->output_table_b) | |
| 1322 precache_release(profile->output_table_b); | |
| 1323 | |
| 1324 if (profile->A2B0) | |
| 1325 lut_release(profile->A2B0); | |
| 1326 if (profile->B2A0) | |
| 1327 lut_release(profile->B2A0); | |
| 1328 | |
| 1329 if (profile->mAB) | |
| 1330 mAB_release(profile->mAB); | |
| 1331 if (profile->mBA) | |
| 1332 mAB_release(profile->mBA); | |
| 1333 | |
| 1334 free(profile->redTRC); | |
| 1335 free(profile->blueTRC); | |
| 1336 free(profile->greenTRC); | |
| 1337 free(profile->grayTRC); | |
| 1338 free(profile); | |
| 1339 } | |
| 1340 | |
| 1341 | |
| 1342 #include <stdio.h> | |
| 1343 qcms_profile* qcms_profile_from_file(FILE *file) | |
| 1344 { | |
| 1345 uint32_t length, remaining_length; | |
| 1346 qcms_profile *profile; | |
| 1347 size_t read_length; | |
| 1348 be32 length_be; | |
| 1349 void *data; | |
| 1350 | |
| 1351 if (fread(&length_be, 1, sizeof(length_be), file) != sizeof(length_be)) | |
| 1352 return BAD_VALUE_PROFILE; | |
| 1353 | |
| 1354 length = be32_to_cpu(length_be); | |
| 1355 if (length > MAX_PROFILE_SIZE || length < sizeof(length_be)) | |
| 1356 return BAD_VALUE_PROFILE; | |
| 1357 | |
| 1358 /* allocate room for the entire profile */ | |
| 1359 data = malloc(length); | |
| 1360 if (!data) | |
| 1361 return NO_MEM_PROFILE; | |
| 1362 | |
| 1363 /* copy in length to the front so that the buffer will contain the entir
e profile */ | |
| 1364 *((be32*)data) = length_be; | |
| 1365 remaining_length = length - sizeof(length_be); | |
| 1366 | |
| 1367 /* read the rest profile */ | |
| 1368 read_length = fread((unsigned char*)data + sizeof(length_be), 1, remaini
ng_length, file); | |
| 1369 if (read_length != remaining_length) { | |
| 1370 free(data); | |
| 1371 return INVALID_PROFILE; | |
| 1372 } | |
| 1373 | |
| 1374 profile = qcms_profile_from_memory(data, length); | |
| 1375 free(data); | |
| 1376 return profile; | |
| 1377 } | |
| 1378 | |
| 1379 qcms_profile* qcms_profile_from_path(const char *path) | |
| 1380 { | |
| 1381 qcms_profile *profile = NULL; | |
| 1382 FILE *file = fopen(path, "rb"); | |
| 1383 if (file) { | |
| 1384 profile = qcms_profile_from_file(file); | |
| 1385 fclose(file); | |
| 1386 } | |
| 1387 return profile; | |
| 1388 } | |
| 1389 | |
| 1390 #ifdef _WIN32 | |
| 1391 /* Unicode path version */ | |
| 1392 qcms_profile* qcms_profile_from_unicode_path(const wchar_t *path) | |
| 1393 { | |
| 1394 qcms_profile *profile = NULL; | |
| 1395 FILE *file = _wfopen(path, L"rb"); | |
| 1396 if (file) { | |
| 1397 profile = qcms_profile_from_file(file); | |
| 1398 fclose(file); | |
| 1399 } | |
| 1400 return profile; | |
| 1401 } | |
| 1402 #endif | |
| OLD | NEW |