iccread.c 39 KB

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