gdevpdfk.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /* Copyright (C) 2001 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gdevpdfk.c,v 1.10 2005/02/25 07:58:50 igor Exp $ */
  14. /* Lab and ICCBased color space writing */
  15. #include "math_.h"
  16. #include "memory_.h"
  17. #include "gx.h"
  18. #include "gxcspace.h"
  19. #include "stream.h"
  20. #include "gsicc.h"
  21. #include "gserrors.h"
  22. #include "gxcie.h"
  23. #include "gdevpdfx.h"
  24. #include "gdevpdfg.h"
  25. #include "gdevpdfc.h"
  26. #include "gdevpdfo.h"
  27. #include "strimpl.h"
  28. /* ------ CIE space synthesis ------ */
  29. /* Add a /Range entry to a CIE-based color space dictionary. */
  30. private int
  31. pdf_cie_add_ranges(cos_dict_t *pcd, const gs_range *prange, int n, bool clamp)
  32. {
  33. cos_array_t *pca = cos_array_alloc(pcd->pdev, "pdf_cie_add_ranges");
  34. int code = 0, i;
  35. if (pca == 0)
  36. return_error(gs_error_VMerror);
  37. for (i = 0; i < n; ++i) {
  38. floatp rmin = prange[i].rmin, rmax = prange[i].rmax;
  39. if (clamp) {
  40. if (rmin < 0) rmin = 0;
  41. if (rmax > 1) rmax = 1;
  42. }
  43. if ((code = cos_array_add_real(pca, rmin)) < 0 ||
  44. (code = cos_array_add_real(pca, rmax)) < 0
  45. )
  46. break;
  47. }
  48. if (code >= 0)
  49. code = cos_dict_put_c_key_object(pcd, "/Range", COS_OBJECT(pca));
  50. if (code < 0)
  51. COS_FREE(pca, "pdf_cie_add_ranges");
  52. return code;
  53. }
  54. /* Transform a CIEBased color to XYZ. */
  55. private int
  56. cie_to_xyz(const double *in, double out[3], const gs_color_space *pcs,
  57. const gs_imager_state *pis)
  58. {
  59. gs_client_color cc;
  60. frac xyz[3];
  61. int ncomp = gs_color_space_num_components(pcs);
  62. int i;
  63. for (i = 0; i < ncomp; ++i)
  64. cc.paint.values[i] = in[i];
  65. cs_concretize_color(&cc, pcs, xyz, pis);
  66. out[0] = frac2float(xyz[0]);
  67. out[1] = frac2float(xyz[1]);
  68. out[2] = frac2float(xyz[2]);
  69. return 0;
  70. }
  71. /* ------ Lab space writing and synthesis ------ */
  72. /* Transform XYZ values to Lab. */
  73. private double
  74. lab_g_inverse(double v)
  75. {
  76. if (v >= (6.0 * 6.0 * 6.0) / (29 * 29 * 29))
  77. return pow(v, 1.0 / 3); /* use cbrt if available? */
  78. else
  79. return (v * (841.0 / 108) + 4.0 / 29);
  80. }
  81. private void
  82. xyz_to_lab(const double xyz[3], double lab[3], const gs_cie_common *pciec)
  83. {
  84. const gs_vector3 *const pWhitePoint = &pciec->points.WhitePoint;
  85. double L, lunit;
  86. /* Calculate L* first. */
  87. L = lab_g_inverse(xyz[1] / pWhitePoint->v) * 116 - 16;
  88. /* Clamp L* to the PDF range [0..100]. */
  89. if (L < 0)
  90. L = 0;
  91. else if (L > 100)
  92. L = 100;
  93. lab[1] = L;
  94. lunit = (L + 16) / 116;
  95. /* Calculate a* and b*. */
  96. lab[0] = (lab_g_inverse(xyz[0] / pWhitePoint->u) - lunit) * 500;
  97. lab[2] = (lab_g_inverse(xyz[2] / pWhitePoint->w) - lunit) * -200;
  98. }
  99. /* Create a PDF Lab color space corresponding to a CIEBased color space. */
  100. private int
  101. lab_range(gs_range range_out[3] /* only [1] and [2] used */,
  102. const gs_color_space *pcs, const gs_cie_common *pciec,
  103. const gs_range *ranges, gs_memory_t *mem)
  104. {
  105. /*
  106. * Determine the range of a* and b* by evaluating the color space
  107. * mapping at all of its extrema.
  108. */
  109. int ncomp = gs_color_space_num_components(pcs);
  110. gs_imager_state *pis;
  111. int code = gx_cie_to_xyz_alloc(&pis, pcs, mem);
  112. int i, j;
  113. if (code < 0)
  114. return code;
  115. for (j = 1; j < 3; ++j)
  116. range_out[j].rmin = 1000.0, range_out[j].rmax = -1000.0;
  117. for (i = 0; i < 1 << ncomp; ++i) {
  118. double in[4], xyz[3];
  119. for (j = 0; j < ncomp; ++j)
  120. in[j] = (i & (1 << j) ? ranges[j].rmax : ranges[j].rmin);
  121. if (cie_to_xyz(in, xyz, pcs, pis) >= 0) {
  122. double lab[3];
  123. xyz_to_lab(xyz, lab, pciec);
  124. for (j = 1; j < 3; ++j) {
  125. range_out[j].rmin = min(range_out[j].rmin, lab[j]);
  126. range_out[j].rmax = max(range_out[j].rmax, lab[j]);
  127. }
  128. }
  129. }
  130. gx_cie_to_xyz_free(pis);
  131. return 0;
  132. }
  133. /*
  134. * Create a Lab color space object.
  135. * This procedure is exported for Lab color spaces in gdevpdfc.c.
  136. */
  137. int
  138. pdf_put_lab_color_space(cos_array_t *pca, cos_dict_t *pcd,
  139. const gs_range ranges[3] /* only [1] and [2] used */)
  140. {
  141. int code;
  142. cos_value_t v;
  143. if ((code = cos_array_add(pca, cos_c_string_value(&v, "/Lab"))) >= 0)
  144. code = pdf_cie_add_ranges(pcd, ranges + 1, 2, false);
  145. return code;
  146. }
  147. /*
  148. * Create a Lab color space for a CIEBased space that can't be represented
  149. * directly as a Calxxx or Lab space.
  150. */
  151. private int
  152. pdf_convert_cie_to_lab(gx_device_pdf *pdev, cos_array_t *pca,
  153. const gs_color_space *pcs,
  154. const gs_cie_common *pciec, const gs_range *prange)
  155. {
  156. cos_dict_t *pcd;
  157. gs_range ranges[3];
  158. int code;
  159. /****** NOT IMPLEMENTED YET, REQUIRES TRANSFORMING VALUES ******/
  160. if (1) return_error(gs_error_rangecheck);
  161. pcd = cos_dict_alloc(pdev, "pdf_convert_cie_to_lab(dict)");
  162. if (pcd == 0)
  163. return_error(gs_error_VMerror);
  164. if ((code = lab_range(ranges, pcs, pciec, prange, pdev->pdf_memory)) < 0 ||
  165. (code = pdf_put_lab_color_space(pca, pcd, ranges)) < 0 ||
  166. (code = pdf_finish_cie_space(pca, pcd, pciec)) < 0
  167. )
  168. COS_FREE(pcd, "pdf_convert_cie_to_lab(dict)");
  169. return code;
  170. }
  171. /* ------ ICCBased space writing and synthesis ------ */
  172. /*
  173. * Create an ICCBased color space object (internal). The client must write
  174. * the profile data on *ppcstrm.
  175. */
  176. private int
  177. pdf_make_iccbased(gx_device_pdf *pdev, cos_array_t *pca, int ncomps,
  178. const gs_range *prange /*[4]*/,
  179. const gs_color_space *pcs_alt,
  180. cos_stream_t **ppcstrm,
  181. const gs_range_t **pprange /* if scaling is needed */)
  182. {
  183. cos_value_t v;
  184. int code;
  185. cos_stream_t * pcstrm = 0;
  186. cos_array_t * prngca = 0;
  187. bool std_ranges = true;
  188. bool scale_inputs = false;
  189. int i;
  190. /* Check the ranges. */
  191. if (pprange)
  192. *pprange = 0;
  193. for (i = 0; i < ncomps; ++i) {
  194. double rmin = prange[i].rmin, rmax = prange[i].rmax;
  195. if (rmin < 0.0 || rmax > 1.0) {
  196. /* We'll have to scale the inputs. :-( */
  197. if (pprange == 0)
  198. return_error(gs_error_rangecheck); /* scaling not allowed */
  199. *pprange = prange;
  200. scale_inputs = true;
  201. }
  202. else if (rmin > 0.0 || rmax < 1.0)
  203. std_ranges = false;
  204. }
  205. /* ICCBased color spaces are essentially copied to the output. */
  206. if ((code = cos_array_add(pca, cos_c_string_value(&v, "/ICCBased"))) < 0)
  207. return code;
  208. /* Create a stream for the output. */
  209. if ((pcstrm = cos_stream_alloc(pdev, "pdf_make_iccbased(stream)")) == 0) {
  210. code = gs_note_error(gs_error_VMerror);
  211. goto fail;
  212. }
  213. /* Indicate the number of components. */
  214. code = cos_dict_put_c_key_int(cos_stream_dict(pcstrm), "/N", ncomps);
  215. if (code < 0)
  216. goto fail;
  217. /* Indicate the range, if needed. */
  218. if (!std_ranges && !scale_inputs) {
  219. code = pdf_cie_add_ranges(cos_stream_dict(pcstrm), prange, ncomps, true);
  220. if (code < 0)
  221. goto fail;
  222. }
  223. /* Output the alternate color space, if necessary. */
  224. switch (gs_color_space_get_index(pcs_alt)) {
  225. case gs_color_space_index_DeviceGray:
  226. case gs_color_space_index_DeviceRGB:
  227. case gs_color_space_index_DeviceCMYK:
  228. break; /* implicit (default) */
  229. default:
  230. if ((code = pdf_color_space(pdev, &v, NULL, pcs_alt,
  231. &pdf_color_space_names, false)) < 0 ||
  232. (code = cos_dict_put_c_key(cos_stream_dict(pcstrm), "/Alternate",
  233. &v)) < 0
  234. )
  235. goto fail;
  236. }
  237. /* Wrap up. */
  238. if ((code = cos_array_add_object(pca, COS_OBJECT(pcstrm))) < 0)
  239. goto fail;
  240. *ppcstrm = pcstrm;
  241. return code;
  242. fail:
  243. if (prngca)
  244. COS_FREE(prngca, "pdf_make_iccbased(Range)");
  245. if (pcstrm)
  246. COS_FREE(pcstrm, "pdf_make_iccbased(stream)");
  247. return code;
  248. }
  249. /*
  250. * Finish writing the data stream for an ICCBased color space object.
  251. */
  252. private int
  253. pdf_finish_iccbased(cos_stream_t *pcstrm)
  254. {
  255. /*
  256. * The stream must be an indirect object. Assign an ID, and write the
  257. * object out now.
  258. */
  259. gx_device_pdf *pdev = pcstrm->pdev;
  260. pcstrm->id = pdf_obj_ref(pdev);
  261. return cos_write_object(COS_OBJECT(pcstrm), pdev);
  262. }
  263. /*
  264. * Create an ICCBased color space for a CIEBased space that can't be
  265. * represented directly as a Calxxx or Lab space.
  266. */
  267. typedef struct profile_table_s profile_table_t;
  268. struct profile_table_s {
  269. const char *tag;
  270. const byte *data;
  271. uint length;
  272. uint data_length; /* may be < length if write != 0 */
  273. int (*write)(cos_stream_t *, const profile_table_t *, gs_memory_t *);
  274. const void *write_data;
  275. const gs_range_t *ranges;
  276. };
  277. private profile_table_t *
  278. add_table(profile_table_t **ppnt, const char *tag, const byte *data,
  279. uint length)
  280. {
  281. profile_table_t *pnt = (*ppnt)++;
  282. pnt->tag = tag, pnt->data = data, pnt->length = length;
  283. pnt->data_length = length;
  284. pnt->write = NULL;
  285. /* write_data not set */
  286. pnt->ranges = NULL;
  287. return pnt;
  288. }
  289. private void
  290. set_uint32(byte bytes[4], uint value)
  291. {
  292. bytes[0] = (byte)(value >> 24);
  293. bytes[1] = (byte)(value >> 16);
  294. bytes[2] = (byte)(value >> 8);
  295. bytes[3] = (byte)value;
  296. }
  297. private void
  298. set_XYZ(byte bytes[4], floatp value)
  299. {
  300. set_uint32(bytes, (uint)(int)(value * 65536));
  301. }
  302. private void
  303. add_table_xyz3(profile_table_t **ppnt, const char *tag, byte bytes[20],
  304. const gs_vector3 *pv)
  305. {
  306. memcpy(bytes, "XYZ \000\000\000\000", 8);
  307. set_XYZ(bytes + 8, pv->u);
  308. set_XYZ(bytes + 12, pv->v);
  309. set_XYZ(bytes + 16, pv->w);
  310. DISCARD(add_table(ppnt, tag, bytes, 20));
  311. }
  312. private void
  313. set_sample16(byte *p, floatp v)
  314. {
  315. int value = (int)(v * 65535);
  316. if (value < 0)
  317. value = 0;
  318. else if (value > 65535)
  319. value = 65535;
  320. p[0] = (byte)(value >> 8);
  321. p[1] = (byte)value;
  322. }
  323. /* Create and write a TRC curve table. */
  324. private int write_trc_abc(cos_stream_t *, const profile_table_t *, gs_memory_t *);
  325. private int write_trc_lmn(cos_stream_t *, const profile_table_t *, gs_memory_t *);
  326. private profile_table_t *
  327. add_trc(profile_table_t **ppnt, const char *tag, byte bytes[12],
  328. const gs_cie_common *pciec, cie_cache_one_step_t one_step)
  329. {
  330. const int count = gx_cie_cache_size;
  331. profile_table_t *pnt;
  332. memcpy(bytes, "curv\000\000\000\000", 8);
  333. set_uint32(bytes + 8, count);
  334. pnt = add_table(ppnt, tag, bytes, 12);
  335. pnt->length += count * 2;
  336. pnt->write = (one_step == ONE_STEP_ABC ? write_trc_abc : write_trc_lmn);
  337. pnt->write_data = (const gs_cie_abc *)pciec;
  338. return pnt;
  339. }
  340. private int
  341. rgb_to_index(const profile_table_t *pnt)
  342. {
  343. switch (pnt->tag[0]) {
  344. case 'r': return 0;
  345. case 'g': return 1;
  346. case 'b': default: /* others can't happen */ return 2;
  347. }
  348. }
  349. private double
  350. cache_arg(int i, int denom, const gs_range_t *range)
  351. {
  352. double arg = i / (double)denom;
  353. if (range) {
  354. /* Sample over the range [range->rmin .. range->rmax]. */
  355. arg = arg * (range->rmax - range->rmin) + range->rmin;
  356. }
  357. return arg;
  358. }
  359. private int
  360. write_trc_abc(cos_stream_t *pcstrm, const profile_table_t *pnt,
  361. gs_memory_t *ignore_mem)
  362. {
  363. /* Write the curve table from DecodeABC. */
  364. const gs_cie_abc *pabc = pnt->write_data;
  365. int ci = rgb_to_index(pnt);
  366. gs_cie_abc_proc proc = pabc->DecodeABC.procs[ci];
  367. byte samples[gx_cie_cache_size * 2];
  368. byte *p = samples;
  369. int i;
  370. for (i = 0; i < gx_cie_cache_size; ++i, p += 2)
  371. set_sample16(p, proc(cache_arg(i, gx_cie_cache_size - 1, pnt->ranges),
  372. pabc));
  373. return cos_stream_add_bytes(pcstrm, samples, gx_cie_cache_size * 2);
  374. }
  375. private int
  376. write_trc_lmn(cos_stream_t *pcstrm, const profile_table_t *pnt,
  377. gs_memory_t *ignore_mem)
  378. {
  379. const gs_cie_common *pciec = pnt->write_data;
  380. int ci = rgb_to_index(pnt);
  381. gs_cie_common_proc proc = pciec->DecodeLMN.procs[ci];
  382. byte samples[gx_cie_cache_size * 2];
  383. byte *p = samples;
  384. int i;
  385. /* Write the curve table from DecodeLMN. */
  386. for (i = 0; i < gx_cie_cache_size; ++i, p += 2)
  387. set_sample16(p, proc(cache_arg(i, gx_cie_cache_size - 1, pnt->ranges),
  388. pciec));
  389. return cos_stream_add_bytes(pcstrm, samples, gx_cie_cache_size * 2);
  390. }
  391. /* Create and write an a2b0 lookup table. */
  392. #define NUM_IN_ENTRIES 2 /* assume linear interpolation */
  393. #define NUM_OUT_ENTRIES 2 /* ibid. */
  394. #define MAX_CLUT_ENTRIES 2500 /* enough for 7^4 */
  395. typedef struct icc_a2b0_s {
  396. byte header[52];
  397. const gs_color_space *pcs;
  398. int num_points; /* on each axis of LUT */
  399. int count; /* total # of entries in LUT */
  400. } icc_a2b0_t;
  401. private int write_a2b0(cos_stream_t *, const profile_table_t *, gs_memory_t *);
  402. private profile_table_t *
  403. add_a2b0(profile_table_t **ppnt, icc_a2b0_t *pa2b, int ncomps,
  404. const gs_color_space *pcs)
  405. {
  406. static const byte a2b0_data[sizeof(pa2b->header)] = {
  407. 'm', 'f', 't', '2', /* type signature */
  408. 0, 0, 0, 0, /* reserved, 0 */
  409. 0, /* # of input channels **VARIABLE** */
  410. 3, /* # of output channels */
  411. 0, /* # of CLUT points **VARIABLE** */
  412. 0, /* reserved, padding */
  413. 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* matrix column 0 */
  414. 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, /* matrix column 1 */
  415. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, /* matrix column 2 */
  416. 0, NUM_IN_ENTRIES, /* # of input table entries */
  417. 0, NUM_OUT_ENTRIES /* # of output table entries */
  418. };
  419. int num_points = (int)floor(pow(MAX_CLUT_ENTRIES, 1.0 / ncomps));
  420. profile_table_t *pnt;
  421. num_points = min(num_points, 255);
  422. memcpy(pa2b->header, a2b0_data, sizeof(a2b0_data));
  423. pa2b->header[8] = ncomps;
  424. pa2b->header[10] = num_points;
  425. pa2b->pcs = pcs;
  426. pa2b->num_points = num_points;
  427. pa2b->count = (int)pow(num_points, ncomps);
  428. pnt = add_table(ppnt, "A2B0", pa2b->header,
  429. sizeof(pa2b->header) +
  430. ncomps * 2 * NUM_IN_ENTRIES + /* in */
  431. pa2b->count * (3 * 2) + /* clut: XYZ, 16-bit values */
  432. 3 * 2 * NUM_OUT_ENTRIES /* out */
  433. );
  434. pnt->data_length = sizeof(pa2b->header); /* only write fixed part */
  435. pnt->write = write_a2b0;
  436. pnt->write_data = pa2b;
  437. return pnt;
  438. }
  439. private int
  440. write_a2b0(cos_stream_t *pcstrm, const profile_table_t *pnt,
  441. gs_memory_t *mem)
  442. {
  443. const icc_a2b0_t *pa2b = pnt->write_data;
  444. const gs_color_space *pcs = pa2b->pcs;
  445. int ncomps = pa2b->header[8];
  446. int num_points = pa2b->num_points;
  447. int i;
  448. #define MAX_NCOMPS 4 /* CIEBasedDEFG */
  449. static const byte v01[MAX_NCOMPS * 2 * 2] = {
  450. 0,0, 255,255, 0,0, 255,255, 0,0, 255,255, 0,0, 255,255
  451. };
  452. gs_imager_state *pis;
  453. int code;
  454. /* Write the input table. */
  455. if ((code = cos_stream_add_bytes(pcstrm, v01, ncomps * 4)) < 0
  456. )
  457. return code;
  458. /* Write the lookup table. */
  459. code = gx_cie_to_xyz_alloc(&pis, pcs, mem);
  460. if (code < 0)
  461. return code;
  462. for (i = 0; i < pa2b->count; ++i) {
  463. double in[MAX_NCOMPS], xyz[3];
  464. byte entry[3 * 2];
  465. byte *p = entry;
  466. int n, j;
  467. for (n = i, j = ncomps - 1; j >= 0; --j, n /= num_points)
  468. in[j] = cache_arg(n % num_points, num_points - 1,
  469. (pnt->ranges ? pnt->ranges + j : NULL));
  470. cie_to_xyz(in, xyz, pcs, pis);
  471. /*
  472. * NOTE: Due to an obscure provision of the ICC Profile
  473. * specification, values in a2b0 lookup tables do *not* represent
  474. * the range [0 .. 1], but rather the range [0
  475. * .. MAX_ICC_XYZ_VALUE]. This caused us a lot of grief before we
  476. * figured it out!
  477. */
  478. #define MAX_ICC_XYZ_VALUE (1 + 32767.0/32768)
  479. for (j = 0; j < 3; ++j, p += 2)
  480. set_sample16(p, xyz[j] / MAX_ICC_XYZ_VALUE);
  481. #undef MAX_ICC_XYZ_VALUE
  482. if ((code = cos_stream_add_bytes(pcstrm, entry, sizeof(entry))) < 0)
  483. break;
  484. }
  485. gx_cie_to_xyz_free(pis);
  486. if (code < 0)
  487. return code;
  488. /* Write the output table. */
  489. return cos_stream_add_bytes(pcstrm, v01, 3 * 4);
  490. }
  491. private int
  492. pdf_convert_cie_to_iccbased(gx_device_pdf *pdev, cos_array_t *pca,
  493. const gs_color_space *pcs, const char *dcsname,
  494. const gs_cie_common *pciec, const gs_range *prange,
  495. cie_cache_one_step_t one_step,
  496. const gs_matrix3 *pmat, const gs_range_t **pprange)
  497. {
  498. /*
  499. * We have two options for creating an ICCBased color space to represent
  500. * a CIEBased space. For CIEBasedABC spaces using only a single
  501. * Decode step followed by a single Matrix step, we can use [rgb]TRC
  502. * and [rgb]XYZ; for CIEBasedA spaces using only DecodeA, we could use
  503. * kTRC (but don't); otherwise, we must use a mft2 LUT.
  504. */
  505. int code;
  506. int ncomps = gs_color_space_num_components(pcs);
  507. gs_color_space alt_space;
  508. cos_stream_t *pcstrm;
  509. /*
  510. * Even though Ghostscript includes icclib, icclib is unusable here,
  511. * because it requires random access to the output stream.
  512. * Instead, we construct the ICC profile by hand.
  513. */
  514. /* Header */
  515. byte header[128];
  516. static const byte header_data[] = {
  517. 0, 0, 0, 0, /* profile size **VARIABLE** */
  518. 0, 0, 0, 0, /* CMM type signature */
  519. 0x02, 0x20, 0, 0, /* profile version number */
  520. 's', 'c', 'n', 'r', /* profile class signature */
  521. 0, 0, 0, 0, /* data color space **VARIABLE** */
  522. 'X', 'Y', 'Z', ' ', /* connection color space */
  523. 2002 / 256, 2002 % 256, 0, 1, 0, 1, /* date (1/1/2002) */
  524. 0, 0, 0, 0, 0, 0, /* time */
  525. 'a', 'c', 's', 'p', /* profile file signature */
  526. 0, 0, 0, 0, /* primary platform signature */
  527. 0, 0, 0, 3, /* profile flags (embedded use only) */
  528. 0, 0, 0, 0, 0, 0, 0, 0, /* device manufacturer */
  529. 0, 0, 0, 0, /* device model */
  530. 0, 0, 0, 0, 0, 0, 0, 2 /* device attributes */
  531. /* Remaining fields are zero or variable. */
  532. /* [4] */ /* rendering intent */
  533. /* 3 * [4] */ /* illuminant */
  534. };
  535. /* Description */
  536. #define DESC_LENGTH 5 /* "adhoc" */
  537. byte desc[12 + DESC_LENGTH + 1 + 11 + 67];
  538. static const byte desc_data[] = {
  539. 'd', 'e', 's', 'c', /* type signature */
  540. 0, 0, 0, 0, /* reserved, 0 */
  541. 0, 0, 0, DESC_LENGTH + 1, /* ASCII description length */
  542. 'a', 'd', 'h', 'o', 'c', 0, /* ASCII description */
  543. /* Remaining fields are zero. */
  544. };
  545. /* White point */
  546. byte wtpt[20];
  547. /* Copyright (useless, but required by icclib) */
  548. static const byte cprt_data[] = {
  549. 't', 'e', 'x', 't', /* type signature */
  550. 0, 0, 0, 0, /* reserved, 0 */
  551. 'n', 'o', 'n', 'e', 0 /* must be null-terminated (!) */
  552. };
  553. /* Lookup table */
  554. icc_a2b0_t a2b0;
  555. /* [rgb]TRC */
  556. byte rTRC[12], gTRC[12], bTRC[12];
  557. /* [rgb]XYZ */
  558. byte rXYZ[20], gXYZ[20], bXYZ[20];
  559. /* Table structures */
  560. #define MAX_NUM_TABLES 9 /* desc, [rgb]TRC, [rgb]xYZ, wtpt, cprt */
  561. profile_table_t tables[MAX_NUM_TABLES];
  562. profile_table_t *next_table = tables;
  563. pdf_cspace_init_Device(pdev->memory, &alt_space, ncomps); /* can't fail */
  564. code = pdf_make_iccbased(pdev, pca, ncomps, prange, &alt_space,
  565. &pcstrm, pprange);
  566. if (code < 0)
  567. return code;
  568. /* Fill in most of the header, except for the total size. */
  569. memset(header, 0, sizeof(header));
  570. memcpy(header, header_data, sizeof(header_data));
  571. memcpy(header + 16, dcsname, 4);
  572. /* Construct the tables. */
  573. /* desc */
  574. memset(desc, 0, sizeof(desc));
  575. memcpy(desc, desc_data, sizeof(desc_data));
  576. DISCARD(add_table(&next_table, "desc", desc, sizeof(desc)));
  577. /* wtpt */
  578. add_table_xyz3(&next_table, "wtpt", wtpt, &pciec->points.WhitePoint);
  579. memcpy(header + 68, wtpt + 8, 12); /* illuminant = white point */
  580. /* cprt */
  581. /* (We have no use for this tag, but icclib requires it.) */
  582. DISCARD(add_table(&next_table, "cprt", cprt_data, sizeof(cprt_data)));
  583. /* Use TRC + XYZ if possible, otherwise AToB. */
  584. if ((one_step == ONE_STEP_ABC || one_step == ONE_STEP_LMN) && pmat != 0) {
  585. /* Use TRC + XYZ. */
  586. profile_table_t *tr =
  587. add_trc(&next_table, "rTRC", rTRC, pciec, one_step);
  588. profile_table_t *tg =
  589. add_trc(&next_table, "gTRC", gTRC, pciec, one_step);
  590. profile_table_t *tb =
  591. add_trc(&next_table, "bTRC", bTRC, pciec, one_step);
  592. if (*pprange) {
  593. tr->ranges = *pprange;
  594. tg->ranges = *pprange + 1;
  595. tb->ranges = *pprange + 2;
  596. }
  597. add_table_xyz3(&next_table, "rXYZ", rXYZ, &pmat->cu);
  598. add_table_xyz3(&next_table, "gXYZ", gXYZ, &pmat->cv);
  599. add_table_xyz3(&next_table, "bXYZ", bXYZ, &pmat->cw);
  600. } else {
  601. /* General case, use a lookup table. */
  602. /* AToB (mft2) */
  603. profile_table_t *pnt = add_a2b0(&next_table, &a2b0, ncomps, pcs);
  604. pnt->ranges = *pprange;
  605. }
  606. /* Write the profile. */
  607. {
  608. byte bytes[4 + MAX_NUM_TABLES * 12];
  609. int num_tables = next_table - tables;
  610. int i;
  611. byte *p;
  612. uint table_size = 4 + num_tables * 12;
  613. uint offset = sizeof(header) + table_size;
  614. set_uint32(bytes, next_table - tables);
  615. for (i = 0, p = bytes + 4; i < num_tables; ++i, p += 12) {
  616. memcpy(p, tables[i].tag, 4);
  617. set_uint32(p + 4, offset);
  618. set_uint32(p + 8, tables[i].length);
  619. offset += round_up(tables[i].length, 4);
  620. }
  621. set_uint32(header, offset);
  622. if ((code = cos_stream_add_bytes(pcstrm, header, sizeof(header))) < 0 ||
  623. (code = cos_stream_add_bytes(pcstrm, bytes, table_size)) < 0
  624. )
  625. return code;
  626. for (i = 0; i < num_tables; ++i) {
  627. uint len = tables[i].data_length;
  628. static const byte pad[3] = {0, 0, 0};
  629. if ((code = cos_stream_add_bytes(pcstrm, tables[i].data, len)) < 0 ||
  630. (tables[i].write != 0 &&
  631. (code = tables[i].write(pcstrm, &tables[i], pdev->pdf_memory)) < 0) ||
  632. (code = cos_stream_add_bytes(pcstrm, pad,
  633. -(int)(tables[i].length) & 3)) < 0
  634. )
  635. return code;
  636. }
  637. }
  638. return pdf_finish_iccbased(pcstrm);
  639. }
  640. /* ------ Entry points (from gdevpdfc.c) ------ */
  641. /*
  642. * Create an ICCBased color space. This is a single-use procedure,
  643. * broken out only for readability.
  644. */
  645. int
  646. pdf_iccbased_color_space(gx_device_pdf *pdev, cos_value_t *pvalue,
  647. const gs_color_space *pcs, cos_array_t *pca)
  648. {
  649. /*
  650. * This would arise only in a pdf ==> pdf translation, but we
  651. * should allow for it anyway.
  652. */
  653. const gs_icc_params * picc_params = &pcs->params.icc;
  654. const gs_cie_icc * picc_info = picc_params->picc_info;
  655. cos_stream_t * pcstrm;
  656. int code =
  657. pdf_make_iccbased(pdev, pca, picc_info->num_components,
  658. picc_info->Range.ranges,
  659. (const gs_color_space *)&picc_params->alt_space,
  660. &pcstrm, NULL);
  661. if (code < 0)
  662. return code;
  663. /* Transfer the profile stream. */
  664. code = cos_stream_add_stream_contents(pcstrm, picc_info->instrp);
  665. if (code >= 0)
  666. code = pdf_finish_iccbased(pcstrm);
  667. /*
  668. * The stream has been added to the array: in case of failure, the
  669. * caller will free the array, so there is no need to free the stream
  670. * explicitly here.
  671. */
  672. return code;
  673. }
  674. /* Convert a CIEBased space to Lab or ICCBased. */
  675. int
  676. pdf_convert_cie_space(gx_device_pdf *pdev, cos_array_t *pca,
  677. const gs_color_space *pcs, const char *dcsname,
  678. const gs_cie_common *pciec, const gs_range *prange,
  679. cie_cache_one_step_t one_step, const gs_matrix3 *pmat,
  680. const gs_range_t **pprange)
  681. {
  682. return (pdev->CompatibilityLevel < 1.3 ?
  683. /* PDF 1.2 or earlier, use a Lab space. */
  684. pdf_convert_cie_to_lab(pdev, pca, pcs, pciec, prange) :
  685. /* PDF 1.3 or later, use an ICCBased space. */
  686. pdf_convert_cie_to_iccbased(pdev, pca, pcs, dcsname, pciec, prange,
  687. one_step, pmat, pprange)
  688. );
  689. }