iccread.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. // qcms
  2. // Copyright (C) 2009 Mozilla Foundation
  3. // Copyright (C) 1998-2007 Marti Maria
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the Software
  10. // is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  17. // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #include <math.h>
  23. #include <assert.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "qcmsint.h"
  27. typedef uint32_t be32;
  28. typedef uint16_t be16;
  29. #if 0
  30. not used yet
  31. /* __builtin_bswap isn't available in older gccs
  32. * so open code it for now */
  33. static be32 cpu_to_be32(int32_t v)
  34. {
  35. #ifdef IS_LITTLE_ENDIAN
  36. return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | ((v & 0xff000000) >> 24);
  37. //return __builtin_bswap32(v);
  38. return v;
  39. #endif
  40. }
  41. #endif
  42. static uint32_t be32_to_cpu(be32 v)
  43. {
  44. #ifdef IS_LITTLE_ENDIAN
  45. return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | ((v & 0xff000000) >> 24);
  46. //return __builtin_bswap32(v);
  47. #else
  48. return v;
  49. #endif
  50. }
  51. static uint16_t be16_to_cpu(be16 v)
  52. {
  53. #ifdef IS_LITTLE_ENDIAN
  54. return ((v & 0xff) << 8) | ((v & 0xff00) >> 8);
  55. #else
  56. return v;
  57. #endif
  58. }
  59. /* a wrapper around the memory that we are going to parse
  60. * into a qcms_profile */
  61. struct mem_source
  62. {
  63. const unsigned char *buf;
  64. size_t size;
  65. qcms_bool valid;
  66. const char *invalid_reason;
  67. };
  68. static void invalid_source(struct mem_source *mem, const char *reason)
  69. {
  70. mem->valid = false;
  71. mem->invalid_reason = reason;
  72. }
  73. static uint32_t read_u32(struct mem_source *mem, size_t offset)
  74. {
  75. /* Subtract from mem->size instead of the more intuitive adding to offset.
  76. * This avoids overflowing offset. The subtraction is safe because
  77. * mem->size is guaranteed to be > 4 */
  78. if (offset > mem->size - 4) {
  79. invalid_source(mem, "Invalid offset");
  80. return 0;
  81. } else {
  82. be32 k;
  83. memcpy(&k, mem->buf + offset, sizeof(k));
  84. return be32_to_cpu(k);
  85. }
  86. }
  87. static uint16_t read_u16(struct mem_source *mem, size_t offset)
  88. {
  89. if (offset > mem->size - 2) {
  90. invalid_source(mem, "Invalid offset");
  91. return 0;
  92. } else {
  93. be16 k;
  94. memcpy(&k, mem->buf + offset, sizeof(k));
  95. return be16_to_cpu(k);
  96. }
  97. }
  98. static uint8_t read_u8(struct mem_source *mem, size_t offset)
  99. {
  100. if (offset > mem->size - 1) {
  101. invalid_source(mem, "Invalid offset");
  102. return 0;
  103. } else {
  104. return *(uint8_t*)(mem->buf + offset);
  105. }
  106. }
  107. static s15Fixed16Number read_s15Fixed16Number(struct mem_source *mem, size_t offset)
  108. {
  109. return read_u32(mem, offset);
  110. }
  111. #if 0
  112. static uInt16Number read_uInt16Number(struct mem_source *mem, size_t offset)
  113. {
  114. return read_u16(mem, offset);
  115. }
  116. #endif
  117. #define BAD_VALUE_PROFILE NULL
  118. #define INVALID_PROFILE NULL
  119. #define NO_MEM_PROFILE NULL
  120. /* An arbitrary 4MB limit on profile size */
  121. #define MAX_PROFILE_SIZE 1024*1024*4
  122. #define MAX_TAG_COUNT 1024
  123. static void check_CMM_type_signature(struct mem_source *src)
  124. {
  125. //uint32_t CMM_type_signature = read_u32(src, 4);
  126. //TODO: do the check?
  127. }
  128. static void check_profile_version(struct mem_source *src)
  129. {
  130. uint8_t major_revision = read_u8(src, 8 + 0);
  131. uint8_t minor_revision = read_u8(src, 8 + 1);
  132. uint8_t reserved1 = read_u8(src, 8 + 2);
  133. uint8_t reserved2 = read_u8(src, 8 + 3);
  134. if (major_revision != 0x4) {
  135. if (major_revision > 0x2)
  136. invalid_source(src, "Unsupported major revision");
  137. if (minor_revision > 0x40)
  138. invalid_source(src, "Unsupported minor revision");
  139. }
  140. if (reserved1 != 0 || reserved2 != 0)
  141. invalid_source(src, "Invalid reserved bytes");
  142. }
  143. #define INPUT_DEVICE_PROFILE 0x73636e72 // 'scnr'
  144. #define DISPLAY_DEVICE_PROFILE 0x6d6e7472 // 'mntr'
  145. #define OUTPUT_DEVICE_PROFILE 0x70727472 // 'prtr'
  146. #define DEVICE_LINK_PROFILE 0x6c696e6b // 'link'
  147. #define COLOR_SPACE_PROFILE 0x73706163 // 'spac'
  148. #define ABSTRACT_PROFILE 0x61627374 // 'abst'
  149. #define NAMED_COLOR_PROFILE 0x6e6d636c // 'nmcl'
  150. static void read_class_signature(qcms_profile *profile, struct mem_source *mem)
  151. {
  152. profile->class = read_u32(mem, 12);
  153. switch (profile->class) {
  154. case DISPLAY_DEVICE_PROFILE:
  155. case INPUT_DEVICE_PROFILE:
  156. break;
  157. case OUTPUT_DEVICE_PROFILE:
  158. default:
  159. invalid_source(mem, "Invalid Profile/Device Class signature");
  160. }
  161. }
  162. static void read_color_space(qcms_profile *profile, struct mem_source *mem)
  163. {
  164. profile->color_space = read_u32(mem, 16);
  165. switch (profile->color_space) {
  166. case RGB_SIGNATURE:
  167. case GRAY_SIGNATURE:
  168. break;
  169. default:
  170. invalid_source(mem, "Unsupported colorspace");
  171. }
  172. }
  173. struct tag
  174. {
  175. uint32_t signature;
  176. uint32_t offset;
  177. uint32_t size;
  178. };
  179. struct tag_index {
  180. uint32_t count;
  181. struct tag *tags;
  182. };
  183. static struct tag_index read_tag_table(qcms_profile *profile, struct mem_source *mem)
  184. {
  185. struct tag_index index = {0, NULL};
  186. unsigned int i;
  187. index.count = read_u32(mem, 128);
  188. if (index.count > MAX_TAG_COUNT) {
  189. invalid_source(mem, "max number of tags exceeded");
  190. return index;
  191. }
  192. index.tags = malloc(sizeof(struct tag)*index.count);
  193. if (index.tags) {
  194. for (i = 0; i < index.count; i++) {
  195. index.tags[i].signature = read_u32(mem, 128 + 4 + 4*i*3);
  196. index.tags[i].offset = read_u32(mem, 128 + 4 + 4*i*3 + 4);
  197. index.tags[i].size = read_u32(mem, 128 + 4 + 4*i*3 + 8);
  198. }
  199. }
  200. return index;
  201. }
  202. // Checks a profile for obvious inconsistencies and returns
  203. // true if the profile looks bogus and should probably be
  204. // ignored.
  205. qcms_bool qcms_profile_is_bogus(qcms_profile *profile)
  206. {
  207. float sum[3], target[3], tolerance[3];
  208. float rX, rY, rZ, gX, gY, gZ, bX, bY, bZ;
  209. bool negative;
  210. unsigned i;
  211. // We currently only check the bogosity of RGB profiles
  212. if (profile->color_space != RGB_SIGNATURE)
  213. return false;
  214. rX = s15Fixed16Number_to_float(profile->redColorant.X);
  215. rY = s15Fixed16Number_to_float(profile->redColorant.Y);
  216. rZ = s15Fixed16Number_to_float(profile->redColorant.Z);
  217. gX = s15Fixed16Number_to_float(profile->greenColorant.X);
  218. gY = s15Fixed16Number_to_float(profile->greenColorant.Y);
  219. gZ = s15Fixed16Number_to_float(profile->greenColorant.Z);
  220. bX = s15Fixed16Number_to_float(profile->blueColorant.X);
  221. bY = s15Fixed16Number_to_float(profile->blueColorant.Y);
  222. bZ = s15Fixed16Number_to_float(profile->blueColorant.Z);
  223. // Check if any of the XYZ values are negative (see mozilla bug 498245)
  224. // CIEXYZ tristimulus values cannot be negative according to the spec.
  225. negative =
  226. (rX < 0) || (rY < 0) || (rZ < 0) ||
  227. (gX < 0) || (gY < 0) || (gZ < 0) ||
  228. (bX < 0) || (bY < 0) || (bZ < 0);
  229. if (negative)
  230. return true;
  231. // Sum the values; they should add up to something close to white
  232. sum[0] = rX + gX + bX;
  233. sum[1] = rY + gY + bY;
  234. sum[2] = rZ + gZ + bZ;
  235. // Build our target vector (see mozilla bug 460629)
  236. target[0] = 0.96420;
  237. target[1] = 1.00000;
  238. target[2] = 0.82491;
  239. // Our tolerance vector - Recommended by Chris Murphy based on
  240. // conversion from the LAB space criterion of no more than 3 in any one
  241. // channel. This is similar to, but slightly more tolerant than Adobe's
  242. // criterion.
  243. tolerance[0] = 0.02;
  244. tolerance[1] = 0.02;
  245. tolerance[2] = 0.04;
  246. // Compare with our tolerance
  247. for (i = 0; i < 3; ++i) {
  248. if (!(((sum[i] - tolerance[i]) <= target[i]) &&
  249. ((sum[i] + tolerance[i]) >= target[i])))
  250. return true;
  251. }
  252. // All Good
  253. return false;
  254. }
  255. #define TAG_bXYZ 0x6258595a
  256. #define TAG_gXYZ 0x6758595a
  257. #define TAG_rXYZ 0x7258595a
  258. #define TAG_rTRC 0x72545243
  259. #define TAG_bTRC 0x62545243
  260. #define TAG_gTRC 0x67545243
  261. #define TAG_kTRC 0x6b545243
  262. #define TAG_A2B0 0x41324230
  263. static struct tag *find_tag(struct tag_index index, uint32_t tag_id)
  264. {
  265. unsigned int i;
  266. struct tag *tag = NULL;
  267. for (i = 0; i < index.count; i++) {
  268. if (index.tags[i].signature == tag_id) {
  269. return &index.tags[i];
  270. }
  271. }
  272. return tag;
  273. }
  274. #define XYZ_TYPE 0x58595a20 // 'XYZ '
  275. #define CURVE_TYPE 0x63757276 // 'curv'
  276. #define LUT16_TYPE 0x6d667432 // 'mft2'
  277. #define LUT8_TYPE 0x6d667431 // 'mft1'
  278. static struct XYZNumber read_tag_XYZType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
  279. {
  280. struct XYZNumber num = {0, 0, 0};
  281. struct tag *tag = find_tag(index, tag_id);
  282. if (tag) {
  283. uint32_t offset = tag->offset;
  284. uint32_t type = read_u32(src, offset);
  285. if (type != XYZ_TYPE)
  286. invalid_source(src, "unexpected type, expected XYZ");
  287. num.X = read_s15Fixed16Number(src, offset+8);
  288. num.Y = read_s15Fixed16Number(src, offset+12);
  289. num.Z = read_s15Fixed16Number(src, offset+16);
  290. } else {
  291. invalid_source(src, "missing xyztag");
  292. }
  293. return num;
  294. }
  295. static struct curveType *read_tag_curveType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
  296. {
  297. struct tag *tag = find_tag(index, tag_id);
  298. struct curveType *curve = NULL;
  299. if (tag) {
  300. uint32_t offset = tag->offset;
  301. uint32_t type = read_u32(src, offset);
  302. uint32_t count = read_u32(src, offset+8);
  303. unsigned int i;
  304. if (type != CURVE_TYPE) {
  305. invalid_source(src, "unexpected type, expected CURV");
  306. return NULL;
  307. }
  308. #define MAX_CURVE_ENTRIES 40000 //arbitrary
  309. if (count > MAX_CURVE_ENTRIES) {
  310. invalid_source(src, "curve size too large");
  311. return NULL;
  312. }
  313. curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*count);
  314. if (!curve)
  315. return NULL;
  316. curve->count = count;
  317. for (i=0; i<count; i++) {
  318. curve->data[i] = read_u16(src, offset + 12 + i *2);
  319. }
  320. } else {
  321. invalid_source(src, "missing curvetag");
  322. }
  323. return curve;
  324. }
  325. /* This function's not done yet */
  326. static struct lutType *read_tag_lutType(struct mem_source *src, struct tag_index index, uint32_t tag_id)
  327. {
  328. struct tag *tag = find_tag(index, tag_id);
  329. uint32_t offset = tag->offset;
  330. uint32_t type = read_u32(src, offset);
  331. uint16_t num_input_table_entries;
  332. uint16_t num_output_table_entries;
  333. uint8_t in_chan, grid_points, out_chan;
  334. uint32_t clut_size;
  335. struct lutType *lut;
  336. int i;
  337. num_input_table_entries = read_u16(src, offset + 48);
  338. num_output_table_entries = read_u16(src, offset + 50);
  339. in_chan = read_u8(src, offset + 8);
  340. out_chan = read_u8(src, offset + 9);
  341. grid_points = read_u8(src, offset + 10);
  342. if (!src->valid)
  343. return NULL;
  344. clut_size = in_chan * grid_points * out_chan;
  345. #define MAX_CLUT_SIZE 10000 // arbitrary
  346. if (clut_size > MAX_CLUT_SIZE) {
  347. return NULL;
  348. }
  349. if (type != LUT16_TYPE && type != LUT8_TYPE)
  350. return NULL;
  351. lut = malloc(sizeof(struct lutType) + (clut_size + num_input_table_entries + num_output_table_entries)*sizeof(uint8_t));
  352. if (!lut)
  353. return NULL;
  354. lut->num_input_channels = read_u8(src, offset + 8);
  355. lut->num_output_channels = read_u8(src, offset + 9);
  356. lut->num_clut_grid_points = read_u8(src, offset + 10);
  357. lut->e00 = read_s15Fixed16Number(src, offset+12);
  358. lut->e01 = read_s15Fixed16Number(src, offset+16);
  359. lut->e02 = read_s15Fixed16Number(src, offset+20);
  360. lut->e10 = read_s15Fixed16Number(src, offset+24);
  361. lut->e11 = read_s15Fixed16Number(src, offset+28);
  362. lut->e12 = read_s15Fixed16Number(src, offset+32);
  363. lut->e20 = read_s15Fixed16Number(src, offset+36);
  364. lut->e21 = read_s15Fixed16Number(src, offset+40);
  365. lut->e22 = read_s15Fixed16Number(src, offset+44);
  366. //TODO: finish up
  367. return lut;
  368. }
  369. static void read_rendering_intent(qcms_profile *profile, struct mem_source *src)
  370. {
  371. profile->rendering_intent = read_u32(src, 64);
  372. switch (profile->rendering_intent) {
  373. case QCMS_INTENT_PERCEPTUAL:
  374. case QCMS_INTENT_SATURATION:
  375. case QCMS_INTENT_RELATIVE_COLORIMETRIC:
  376. case QCMS_INTENT_ABSOLUTE_COLORIMETRIC:
  377. break;
  378. default:
  379. invalid_source(src, "unknown rendering intent");
  380. }
  381. }
  382. qcms_profile *qcms_profile_create(void)
  383. {
  384. return calloc(sizeof(qcms_profile), 1);
  385. }
  386. /* build sRGB gamma table */
  387. /* based on cmsBuildParametricGamma() */
  388. static uint16_t *build_sRGB_gamma_table(int num_entries)
  389. {
  390. int i;
  391. /* taken from lcms: Build_sRGBGamma() */
  392. double gamma = 2.4;
  393. double a = 1./1.055;
  394. double b = 0.055/1.055;
  395. double c = 1./12.92;
  396. double d = 0.04045;
  397. uint16_t *table = malloc(sizeof(uint16_t) * num_entries);
  398. if (!table)
  399. return NULL;
  400. for (i=0; i<num_entries; i++) {
  401. double x = (double)i / (num_entries-1);
  402. double y, output;
  403. // IEC 61966-2.1 (sRGB)
  404. // Y = (aX + b)^Gamma | X >= d
  405. // Y = cX | X < d
  406. if (x >= d) {
  407. double e = (a*x + b);
  408. if (e > 0)
  409. y = pow(e, gamma);
  410. else
  411. y = 0;
  412. } else {
  413. y = c*x;
  414. }
  415. // Saturate -- this could likely move to a separate function
  416. output = y * 65535. + .5;
  417. if (output > 65535.)
  418. output = 65535;
  419. if (output < 0)
  420. output = 0;
  421. table[i] = (uint16_t)floor(output);
  422. }
  423. return table;
  424. }
  425. static struct curveType *curve_from_table(uint16_t *table, int num_entries)
  426. {
  427. struct curveType *curve;
  428. int i;
  429. curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entries);
  430. if (!curve)
  431. return NULL;
  432. curve->count = num_entries;
  433. for (i = 0; i < num_entries; i++) {
  434. curve->data[i] = table[i];
  435. }
  436. return curve;
  437. }
  438. static uint16_t float_to_u8Fixed8Number(float a)
  439. {
  440. if (a > (255.f + 255.f/256))
  441. return 0xffff;
  442. else if (a < 0.f)
  443. return 0;
  444. else
  445. return floor(a*256.f + .5f);
  446. }
  447. static struct curveType *curve_from_gamma(float gamma)
  448. {
  449. struct curveType *curve;
  450. int num_entries = 1;
  451. curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entries);
  452. if (!curve)
  453. return NULL;
  454. curve->count = num_entries;
  455. curve->data[0] = float_to_u8Fixed8Number(gamma);
  456. return curve;
  457. }
  458. static void qcms_profile_fini(qcms_profile *profile)
  459. {
  460. free(profile->redTRC);
  461. free(profile->blueTRC);
  462. free(profile->greenTRC);
  463. free(profile->grayTRC);
  464. free(profile);
  465. }
  466. //XXX: it would be nice if we had a way of ensuring
  467. // everything in a profile was initialized regardless of how it was created
  468. //XXX: should this also be taking a black_point?
  469. /* similar to CGColorSpaceCreateCalibratedRGB */
  470. qcms_profile* qcms_profile_create_rgb_with_gamma(
  471. qcms_CIE_xyY white_point,
  472. qcms_CIE_xyYTRIPLE primaries,
  473. float gamma)
  474. {
  475. qcms_profile* profile = qcms_profile_create();
  476. if (!profile)
  477. return NO_MEM_PROFILE;
  478. //XXX: should store the whitepoint
  479. if (!set_rgb_colorants(profile, white_point, primaries)) {
  480. qcms_profile_fini(profile);
  481. return INVALID_PROFILE;
  482. }
  483. profile->redTRC = curve_from_gamma(gamma);
  484. profile->blueTRC = curve_from_gamma(gamma);
  485. profile->greenTRC = curve_from_gamma(gamma);
  486. if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
  487. qcms_profile_fini(profile);
  488. return NO_MEM_PROFILE;
  489. }
  490. profile->class = DISPLAY_DEVICE_PROFILE;
  491. profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
  492. profile->color_space = RGB_SIGNATURE;
  493. return profile;
  494. }
  495. qcms_profile* qcms_profile_create_rgb_with_table(
  496. qcms_CIE_xyY white_point,
  497. qcms_CIE_xyYTRIPLE primaries,
  498. uint16_t *table, int num_entries)
  499. {
  500. qcms_profile* profile = qcms_profile_create();
  501. if (!profile)
  502. return NO_MEM_PROFILE;
  503. //XXX: should store the whitepoint
  504. if (!set_rgb_colorants(profile, white_point, primaries)) {
  505. qcms_profile_fini(profile);
  506. return INVALID_PROFILE;
  507. }
  508. profile->redTRC = curve_from_table(table, num_entries);
  509. profile->blueTRC = curve_from_table(table, num_entries);
  510. profile->greenTRC = curve_from_table(table, num_entries);
  511. if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) {
  512. qcms_profile_fini(profile);
  513. return NO_MEM_PROFILE;
  514. }
  515. profile->class = DISPLAY_DEVICE_PROFILE;
  516. profile->rendering_intent = QCMS_INTENT_PERCEPTUAL;
  517. profile->color_space = RGB_SIGNATURE;
  518. return profile;
  519. }
  520. /* from lcms: cmsWhitePointFromTemp */
  521. /* tempK must be >= 4000. and <= 25000.
  522. * similar to argyll: icx_DTEMP2XYZ() */
  523. static qcms_CIE_xyY white_point_from_temp(int temp_K)
  524. {
  525. qcms_CIE_xyY white_point;
  526. double x, y;
  527. double T, T2, T3;
  528. // double M1, M2;
  529. // No optimization provided.
  530. T = temp_K;
  531. T2 = T*T; // Square
  532. T3 = T2*T; // Cube
  533. // For correlated color temperature (T) between 4000K and 7000K:
  534. if (T >= 4000. && T <= 7000.) {
  535. x = -4.6070*(1E9/T3) + 2.9678*(1E6/T2) + 0.09911*(1E3/T) + 0.244063;
  536. } else {
  537. // or for correlated color temperature (T) between 7000K and 25000K:
  538. if (T > 7000.0 && T <= 25000.0) {
  539. x = -2.0064*(1E9/T3) + 1.9018*(1E6/T2) + 0.24748*(1E3/T) + 0.237040;
  540. } else {
  541. assert(0 && "invalid temp");
  542. }
  543. }
  544. // Obtain y(x)
  545. y = -3.000*(x*x) + 2.870*x - 0.275;
  546. // wave factors (not used, but here for futures extensions)
  547. // M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y);
  548. // M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y);
  549. // Fill white_point struct
  550. white_point.x = x;
  551. white_point.y = y;
  552. white_point.Y = 1.0;
  553. return white_point;
  554. }
  555. qcms_profile* qcms_profile_sRGB(void)
  556. {
  557. qcms_profile *profile;
  558. uint16_t *table;
  559. qcms_CIE_xyYTRIPLE Rec709Primaries = {
  560. {0.6400, 0.3300, 1.0},
  561. {0.3000, 0.6000, 1.0},
  562. {0.1500, 0.0600, 1.0}
  563. };
  564. qcms_CIE_xyY D65;
  565. D65 = white_point_from_temp(6504);
  566. table = build_sRGB_gamma_table(1024);
  567. if (!table)
  568. return NO_MEM_PROFILE;
  569. profile = qcms_profile_create_rgb_with_table(D65, Rec709Primaries, table, 1024);
  570. free(table);
  571. return profile;
  572. }
  573. /* qcms_profile_from_memory does not hold a reference to the memory passed in */
  574. qcms_profile* qcms_profile_from_memory(const void *mem, size_t size)
  575. {
  576. uint32_t length;
  577. struct mem_source source;
  578. struct mem_source *src = &source;
  579. struct tag_index index;
  580. qcms_profile *profile;
  581. source.buf = mem;
  582. source.size = size;
  583. source.valid = true;
  584. length = read_u32(src, 0);
  585. if (length <= size) {
  586. // shrink the area that we can read if appropriate
  587. source.size = length;
  588. } else {
  589. return INVALID_PROFILE;
  590. }
  591. /* ensure that the profile size is sane so it's easier to reason about */
  592. if (source.size <= 64 || source.size >= MAX_PROFILE_SIZE)
  593. return INVALID_PROFILE;
  594. profile = qcms_profile_create();
  595. if (!profile)
  596. return NO_MEM_PROFILE;
  597. check_CMM_type_signature(src);
  598. check_profile_version(src);
  599. read_class_signature(profile, src);
  600. read_rendering_intent(profile, src);
  601. read_color_space(profile, src);
  602. //TODO read rest of profile stuff
  603. if (!src->valid)
  604. goto invalid_profile;
  605. index = read_tag_table(profile, src);
  606. if (!src->valid || !index.tags)
  607. goto invalid_tag_table;
  608. if (profile->class == DISPLAY_DEVICE_PROFILE || profile->class == INPUT_DEVICE_PROFILE) {
  609. if (profile->color_space == RGB_SIGNATURE) {
  610. profile->redColorant = read_tag_XYZType(src, index, TAG_rXYZ);
  611. profile->greenColorant = read_tag_XYZType(src, index, TAG_gXYZ);
  612. profile->blueColorant = read_tag_XYZType(src, index, TAG_bXYZ);
  613. if (!src->valid)
  614. goto invalid_tag_table;
  615. profile->redTRC = read_tag_curveType(src, index, TAG_rTRC);
  616. profile->greenTRC = read_tag_curveType(src, index, TAG_gTRC);
  617. profile->blueTRC = read_tag_curveType(src, index, TAG_bTRC);
  618. if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC)
  619. goto invalid_tag_table;
  620. } else if (profile->color_space == GRAY_SIGNATURE) {
  621. profile->grayTRC = read_tag_curveType(src, index, TAG_kTRC);
  622. if (!profile->grayTRC)
  623. goto invalid_tag_table;
  624. } else {
  625. goto invalid_tag_table;
  626. }
  627. } else if (0 && profile->class == OUTPUT_DEVICE_PROFILE) {
  628. profile->A2B0 = read_tag_lutType(src, index, TAG_A2B0);
  629. } else {
  630. goto invalid_tag_table;
  631. }
  632. if (!src->valid)
  633. goto invalid_tag_table;
  634. free(index.tags);
  635. return profile;
  636. invalid_tag_table:
  637. free(index.tags);
  638. invalid_profile:
  639. qcms_profile_fini(profile);
  640. return INVALID_PROFILE;
  641. }
  642. qcms_intent qcms_profile_get_rendering_intent(qcms_profile *profile)
  643. {
  644. return profile->rendering_intent;
  645. }
  646. icColorSpaceSignature
  647. qcms_profile_get_color_space(qcms_profile *profile)
  648. {
  649. return profile->color_space;
  650. }
  651. void qcms_profile_release(qcms_profile *profile)
  652. {
  653. if (profile->output_table_r)
  654. precache_release(profile->output_table_r);
  655. if (profile->output_table_g)
  656. precache_release(profile->output_table_g);
  657. if (profile->output_table_b)
  658. precache_release(profile->output_table_b);
  659. qcms_profile_fini(profile);
  660. }
  661. #include <stdio.h>
  662. qcms_profile* qcms_profile_from_file(FILE *file)
  663. {
  664. uint32_t length, remaining_length;
  665. qcms_profile *profile;
  666. size_t read_length;
  667. be32 length_be;
  668. void *data;
  669. if (fread(&length_be, 1, sizeof(length_be), file) != sizeof(length_be))
  670. return BAD_VALUE_PROFILE;
  671. length = be32_to_cpu(length_be);
  672. if (length > MAX_PROFILE_SIZE || length < sizeof(length_be))
  673. return BAD_VALUE_PROFILE;
  674. /* allocate room for the entire profile */
  675. data = malloc(length);
  676. if (!data)
  677. return NO_MEM_PROFILE;
  678. /* copy in length to the front so that the buffer will contain the entire profile */
  679. *((be32*)data) = length_be;
  680. remaining_length = length - sizeof(length_be);
  681. /* read the rest profile */
  682. read_length = fread((unsigned char*)data + sizeof(length_be), 1, remaining_length, file);
  683. if (read_length != remaining_length) {
  684. free(data);
  685. return INVALID_PROFILE;
  686. }
  687. profile = qcms_profile_from_memory(data, length);
  688. free(data);
  689. return profile;
  690. }
  691. qcms_profile* qcms_profile_from_path(const char *path)
  692. {
  693. qcms_profile *profile = NULL;
  694. FILE *file = fopen(path, "rb");
  695. if (file) {
  696. profile = qcms_profile_from_file(file);
  697. fclose(file);
  698. }
  699. return profile;
  700. }
  701. #ifdef _WIN32
  702. /* Unicode path version */
  703. qcms_profile* qcms_profile_from_unicode_path(const wchar_t *path)
  704. {
  705. qcms_profile *profile = NULL;
  706. FILE *file = _wfopen(path, L"rb");
  707. if (file) {
  708. profile = qcms_profile_from_file(file);
  709. fclose(file);
  710. }
  711. return profile;
  712. }
  713. #endif