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