zcie.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /* Copyright (C) 1992, 2000 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: zcie.c,v 1.13 2005/07/28 15:24:29 alexcher Exp $ */
  14. /* CIE color operators */
  15. #include "math_.h"
  16. #include "memory_.h"
  17. #include "ghost.h"
  18. #include "oper.h"
  19. #include "gsstruct.h"
  20. #include "gxcspace.h" /* gscolor2.h requires gscspace.h */
  21. #include "gscolor2.h"
  22. #include "gscie.h"
  23. #include "estack.h"
  24. #include "ialloc.h"
  25. #include "idict.h"
  26. #include "idparam.h"
  27. #include "igstate.h"
  28. #include "icie.h"
  29. #include "isave.h"
  30. #include "ivmspace.h"
  31. #include "store.h" /* for make_null */
  32. /* Empty procedures */
  33. static const ref empty_procs[4] =
  34. {
  35. empty_ref_data(t_array, a_readonly | a_executable),
  36. empty_ref_data(t_array, a_readonly | a_executable),
  37. empty_ref_data(t_array, a_readonly | a_executable),
  38. empty_ref_data(t_array, a_readonly | a_executable)
  39. };
  40. /* ------ Parameter extraction utilities ------ */
  41. /* Get a range array parameter from a dictionary. */
  42. /* We know that count <= 4. */
  43. int
  44. dict_ranges_param(const gs_memory_t *mem,
  45. const ref * pdref, const char *kstr, int count,
  46. gs_range * prange)
  47. {
  48. int code = dict_floats_param(mem, pdref, kstr, count * 2,
  49. (float *)prange, NULL);
  50. if (code < 0)
  51. return code;
  52. else if (code == 0)
  53. memcpy(prange, Range4_default.ranges, count * sizeof(gs_range));
  54. return 0;
  55. }
  56. /* Get an array of procedures from a dictionary. */
  57. /* We know count <= countof(empty_procs). */
  58. int
  59. dict_proc_array_param(const gs_memory_t *mem,
  60. const ref *pdict, const char *kstr,
  61. uint count, ref *pparray)
  62. {
  63. ref *pvalue;
  64. if (dict_find_string(pdict, kstr, &pvalue) > 0) {
  65. uint i;
  66. check_array_only(*pvalue);
  67. if (r_size(pvalue) != count)
  68. return_error(e_rangecheck);
  69. for (i = 0; i < count; i++) {
  70. ref proc;
  71. array_get(mem, pvalue, (long)i, &proc);
  72. check_proc_only(proc);
  73. }
  74. *pparray = *pvalue;
  75. } else
  76. make_const_array(pparray, a_readonly | avm_foreign,
  77. count, &empty_procs[0]);
  78. return 0;
  79. }
  80. /* Get 3 ranges from a dictionary. */
  81. int
  82. dict_range3_param(const gs_memory_t *mem,
  83. const ref *pdref, const char *kstr,
  84. gs_range3 *prange3)
  85. {
  86. return dict_ranges_param(mem, pdref, kstr, 3, prange3->ranges);
  87. }
  88. /* Get a 3x3 matrix from a dictionary. */
  89. int
  90. dict_matrix3_param(const gs_memory_t *mem,
  91. const ref *pdref, const char *kstr, gs_matrix3 *pmat3)
  92. {
  93. /*
  94. * We can't simply call dict_float_array_param with the matrix
  95. * cast to a 9-element float array, because compilers may insert
  96. * padding elements after each of the vectors. However, we can be
  97. * confident that there is no padding within a single vector.
  98. */
  99. float values[9], defaults[9];
  100. int code;
  101. memcpy(&defaults[0], &Matrix3_default.cu, 3 * sizeof(float));
  102. memcpy(&defaults[3], &Matrix3_default.cv, 3 * sizeof(float));
  103. memcpy(&defaults[6], &Matrix3_default.cw, 3 * sizeof(float));
  104. code = dict_floats_param(mem, pdref, kstr, 9, values, defaults);
  105. if (code < 0)
  106. return code;
  107. memcpy(&pmat3->cu, &values[0], 3 * sizeof(float));
  108. memcpy(&pmat3->cv, &values[3], 3 * sizeof(float));
  109. memcpy(&pmat3->cw, &values[6], 3 * sizeof(float));
  110. return 0;
  111. }
  112. /* Get 3 procedures from a dictionary. */
  113. int
  114. dict_proc3_param(const gs_memory_t *mem, const ref *pdref, const char *kstr, ref proc3[3])
  115. {
  116. return dict_proc_array_param(mem, pdref, kstr, 3, proc3);
  117. }
  118. /* Get WhitePoint and BlackPoint values. */
  119. int
  120. cie_points_param(const gs_memory_t *mem,
  121. const ref * pdref, gs_cie_wb * pwb)
  122. {
  123. int code;
  124. if ((code = dict_floats_param(mem, pdref, "WhitePoint", 3, (float *)&pwb->WhitePoint, NULL)) < 0 ||
  125. (code = dict_floats_param(mem, pdref, "BlackPoint", 3, (float *)&pwb->BlackPoint, (const float *)&BlackPoint_default)) < 0
  126. )
  127. return code;
  128. if (pwb->WhitePoint.u <= 0 ||
  129. pwb->WhitePoint.v != 1 ||
  130. pwb->WhitePoint.w <= 0 ||
  131. pwb->BlackPoint.u < 0 ||
  132. pwb->BlackPoint.v < 0 ||
  133. pwb->BlackPoint.w < 0
  134. )
  135. return_error(e_rangecheck);
  136. return 0;
  137. }
  138. /* Process a 3- or 4-dimensional lookup table from a dictionary. */
  139. /* The caller has set pclt->n and pclt->m. */
  140. /* ptref is known to be a readable array of size at least n+1. */
  141. private int cie_3d_table_param(const ref * ptable, uint count, uint nbytes,
  142. gs_const_string * strings);
  143. int
  144. cie_table_param(const ref * ptref, gx_color_lookup_table * pclt,
  145. gs_memory_t * mem)
  146. {
  147. int n = pclt->n, m = pclt->m;
  148. const ref *pta = ptref->value.const_refs;
  149. int i;
  150. uint nbytes;
  151. int code;
  152. gs_const_string *table;
  153. for (i = 0; i < n; ++i) {
  154. check_type_only(pta[i], t_integer);
  155. if (pta[i].value.intval <= 1 || pta[i].value.intval > max_ushort)
  156. return_error(e_rangecheck);
  157. pclt->dims[i] = (int)pta[i].value.intval;
  158. }
  159. nbytes = m * pclt->dims[n - 2] * pclt->dims[n - 1];
  160. if (n == 3) {
  161. table =
  162. gs_alloc_struct_array(mem, pclt->dims[0], gs_const_string,
  163. &st_const_string_element, "cie_table_param");
  164. if (table == 0)
  165. return_error(e_VMerror);
  166. code = cie_3d_table_param(pta + 3, pclt->dims[0], nbytes, table);
  167. } else { /* n == 4 */
  168. int d0 = pclt->dims[0], d1 = pclt->dims[1];
  169. uint ntables = d0 * d1;
  170. const ref *psuba;
  171. check_read_type(pta[4], t_array);
  172. if (r_size(pta + 4) != d0)
  173. return_error(e_rangecheck);
  174. table =
  175. gs_alloc_struct_array(mem, ntables, gs_const_string,
  176. &st_const_string_element, "cie_table_param");
  177. if (table == 0)
  178. return_error(e_VMerror);
  179. psuba = pta[4].value.const_refs;
  180. /*
  181. * We know that d0 > 0, so code will always be set in the loop:
  182. * we initialize code to 0 here solely to pacify stupid compilers.
  183. */
  184. for (code = 0, i = 0; i < d0; ++i) {
  185. code = cie_3d_table_param(psuba + i, d1, nbytes, table + d1 * i);
  186. if (code < 0)
  187. break;
  188. }
  189. }
  190. if (code < 0) {
  191. gs_free_object(mem, table, "cie_table_param");
  192. return code;
  193. }
  194. pclt->table = table;
  195. return 0;
  196. }
  197. private int
  198. cie_3d_table_param(const ref * ptable, uint count, uint nbytes,
  199. gs_const_string * strings)
  200. {
  201. const ref *rstrings;
  202. uint i;
  203. check_read_type(*ptable, t_array);
  204. if (r_size(ptable) != count)
  205. return_error(e_rangecheck);
  206. rstrings = ptable->value.const_refs;
  207. for (i = 0; i < count; ++i) {
  208. const ref *const prt2 = rstrings + i;
  209. check_read_type(*prt2, t_string);
  210. if (r_size(prt2) != nbytes)
  211. return_error(e_rangecheck);
  212. strings[i].data = prt2->value.const_bytes;
  213. strings[i].size = nbytes;
  214. }
  215. return 0;
  216. }
  217. /* ------ CIE setcolorspace ------ */
  218. /* Common code for the CIEBased* cases of setcolorspace. */
  219. private int
  220. cie_lmnp_param(const gs_memory_t *mem, const ref * pdref, gs_cie_common * pcie, ref_cie_procs * pcprocs)
  221. {
  222. int code;
  223. if ((code = dict_range3_param(mem, pdref, "RangeLMN", &pcie->RangeLMN)) < 0 ||
  224. (code = dict_proc3_param(mem, pdref, "DecodeLMN", &pcprocs->DecodeLMN)) < 0 ||
  225. (code = dict_matrix3_param(mem, pdref, "MatrixLMN", &pcie->MatrixLMN)) < 0 ||
  226. (code = cie_points_param(mem, pdref, &pcie->points)) < 0
  227. )
  228. return code;
  229. pcie->DecodeLMN = DecodeLMN_default;
  230. return 0;
  231. }
  232. /* Common code for the CIEBasedABC/DEF[G] cases of setcolorspace. */
  233. private int
  234. cie_abc_param(const gs_memory_t *mem, const ref * pdref, gs_cie_abc * pcie, ref_cie_procs * pcprocs)
  235. {
  236. int code;
  237. if ((code = dict_range3_param(mem, pdref, "RangeABC", &pcie->RangeABC)) < 0 ||
  238. (code = dict_proc3_param(mem, pdref, "DecodeABC", &pcprocs->Decode.ABC)) < 0 ||
  239. (code = dict_matrix3_param(mem, pdref, "MatrixABC", &pcie->MatrixABC)) < 0 ||
  240. (code = cie_lmnp_param(mem, pdref, &pcie->common, pcprocs)) < 0
  241. )
  242. return code;
  243. pcie->DecodeABC = DecodeABC_default;
  244. return 0;
  245. }
  246. /* Finish setting a CIE space (successful or not). */
  247. int
  248. cie_set_finish(i_ctx_t *i_ctx_p, gs_color_space * pcs,
  249. const ref_cie_procs * pcprocs, int edepth, int code)
  250. {
  251. if (code >= 0)
  252. code = gs_setcolorspace(igs, pcs);
  253. /* Delete the extra reference to the parameter tables. */
  254. gs_cspace_release(pcs);
  255. /* Free the top-level object, which was copied by gs_setcolorspace. */
  256. gs_free_object(gs_state_memory(igs), pcs, "cie_set_finish");
  257. if (code < 0) {
  258. ref_stack_pop_to(&e_stack, edepth);
  259. return code;
  260. }
  261. istate->colorspace.procs.cie = *pcprocs;
  262. pop(1);
  263. return (ref_stack_count(&e_stack) == edepth ? 0 : o_push_estack);
  264. }
  265. /* Forward references */
  266. private int cache_common(i_ctx_t *, gs_cie_common *, const ref_cie_procs *,
  267. void *, gs_ref_memory_t *);
  268. private int cache_abc_common(i_ctx_t *, gs_cie_abc *, const ref_cie_procs *,
  269. void *, gs_ref_memory_t *);
  270. /* <dict> .setciedefgspace - */
  271. private int cie_defg_finish(i_ctx_t *);
  272. private int
  273. zsetciedefgspace(i_ctx_t *i_ctx_p)
  274. {
  275. os_ptr op = osp;
  276. int edepth = ref_stack_count(&e_stack);
  277. gs_memory_t *mem = gs_state_memory(igs);
  278. gs_ref_memory_t *imem = (gs_ref_memory_t *)mem;
  279. gs_color_space *pcs;
  280. ref_cie_procs procs;
  281. gs_cie_defg *pcie;
  282. int code;
  283. ref *ptref;
  284. check_type(*op, t_dictionary);
  285. check_dict_read(*op);
  286. if ((code = dict_find_string(op, "Table", &ptref)) <= 0)
  287. return (code < 0 ? code : gs_note_error(e_rangecheck));
  288. check_read_type(*ptref, t_array);
  289. if (r_size(ptref) != 5)
  290. return_error(e_rangecheck);
  291. procs = istate->colorspace.procs.cie;
  292. code = gs_cspace_build_CIEDEFG(&pcs, NULL, mem);
  293. if (code < 0)
  294. return code;
  295. pcie = pcs->params.defg;
  296. pcie->Table.n = 4;
  297. pcie->Table.m = 3;
  298. if ((code = dict_ranges_param(mem, op, "RangeDEFG", 4, pcie->RangeDEFG.ranges)) < 0 ||
  299. (code = dict_proc_array_param(mem, op, "DecodeDEFG", 4, &procs.PreDecode.DEFG)) < 0 ||
  300. (code = dict_ranges_param(mem, op, "RangeHIJK", 4, pcie->RangeHIJK.ranges)) < 0 ||
  301. (code = cie_table_param(ptref, &pcie->Table, mem)) < 0 ||
  302. (code = cie_abc_param(imemory, op, (gs_cie_abc *) pcie, &procs)) < 0 ||
  303. (code = cie_cache_joint(i_ctx_p, &istate->colorrendering.procs, (gs_cie_common *)pcie, igs)) < 0 || /* do this last */
  304. (code = cie_cache_push_finish(i_ctx_p, cie_defg_finish, imem, pcie)) < 0 ||
  305. (code = cie_prepare_cache4(i_ctx_p, &pcie->RangeDEFG,
  306. procs.PreDecode.DEFG.value.const_refs,
  307. &pcie->caches_defg.DecodeDEFG[0],
  308. pcie, imem, "Decode.DEFG")) < 0 ||
  309. (code = cache_abc_common(i_ctx_p, (gs_cie_abc *)pcie, &procs, pcie, imem)) < 0
  310. )
  311. DO_NOTHING;
  312. return cie_set_finish(i_ctx_p, pcs, &procs, edepth, code);
  313. }
  314. private int
  315. cie_defg_finish(i_ctx_t *i_ctx_p)
  316. {
  317. os_ptr op = osp;
  318. gs_cie_defg *pcie = r_ptr(op, gs_cie_defg);
  319. pcie->DecodeDEFG = DecodeDEFG_from_cache;
  320. pcie->DecodeABC = DecodeABC_from_cache;
  321. pcie->common.DecodeLMN = DecodeLMN_from_cache;
  322. gs_cie_defg_complete(pcie);
  323. pop(1);
  324. return 0;
  325. }
  326. /* <dict> .setciedefspace - */
  327. private int cie_def_finish(i_ctx_t *);
  328. private int
  329. zsetciedefspace(i_ctx_t *i_ctx_p)
  330. {
  331. os_ptr op = osp;
  332. int edepth = ref_stack_count(&e_stack);
  333. gs_memory_t *mem = gs_state_memory(igs);
  334. gs_ref_memory_t *imem = (gs_ref_memory_t *)mem;
  335. gs_color_space *pcs;
  336. ref_cie_procs procs;
  337. gs_cie_def *pcie;
  338. int code;
  339. ref *ptref;
  340. check_type(*op, t_dictionary);
  341. check_dict_read(*op);
  342. if ((code = dict_find_string(op, "Table", &ptref)) <= 0)
  343. return (code < 0 ? code : gs_note_error(e_rangecheck));
  344. check_read_type(*ptref, t_array);
  345. if (r_size(ptref) != 4)
  346. return_error(e_rangecheck);
  347. procs = istate->colorspace.procs.cie;
  348. code = gs_cspace_build_CIEDEF(&pcs, NULL, mem);
  349. if (code < 0)
  350. return code;
  351. pcie = pcs->params.def;
  352. pcie->Table.n = 3;
  353. pcie->Table.m = 3;
  354. if ((code = dict_range3_param(mem, op, "RangeDEF", &pcie->RangeDEF)) < 0 ||
  355. (code = dict_proc3_param(mem, op, "DecodeDEF", &procs.PreDecode.DEF)) < 0 ||
  356. (code = dict_range3_param(mem, op, "RangeHIJ", &pcie->RangeHIJ)) < 0 ||
  357. (code = cie_table_param(ptref, &pcie->Table, mem)) < 0 ||
  358. (code = cie_abc_param(imemory, op, (gs_cie_abc *) pcie, &procs)) < 0 ||
  359. (code = cie_cache_joint(i_ctx_p, &istate->colorrendering.procs, (gs_cie_common *)pcie, igs)) < 0 || /* do this last */
  360. (code = cie_cache_push_finish(i_ctx_p, cie_def_finish, imem, pcie)) < 0 ||
  361. (code = cie_prepare_cache3(i_ctx_p, &pcie->RangeDEF,
  362. procs.PreDecode.DEF.value.const_refs,
  363. &pcie->caches_def.DecodeDEF[0],
  364. pcie, imem, "Decode.DEF")) < 0 ||
  365. (code = cache_abc_common(i_ctx_p, (gs_cie_abc *)pcie, &procs, pcie, imem)) < 0
  366. )
  367. DO_NOTHING;
  368. return cie_set_finish(i_ctx_p, pcs, &procs, edepth, code);
  369. }
  370. private int
  371. cie_def_finish(i_ctx_t *i_ctx_p)
  372. {
  373. os_ptr op = osp;
  374. gs_cie_def *pcie = r_ptr(op, gs_cie_def);
  375. pcie->DecodeDEF = DecodeDEF_from_cache;
  376. pcie->DecodeABC = DecodeABC_from_cache;
  377. pcie->common.DecodeLMN = DecodeLMN_from_cache;
  378. gs_cie_def_complete(pcie);
  379. pop(1);
  380. return 0;
  381. }
  382. /* <dict> .setcieabcspace - */
  383. private int cie_abc_finish(i_ctx_t *);
  384. private int
  385. zsetcieabcspace(i_ctx_t *i_ctx_p)
  386. {
  387. os_ptr op = osp;
  388. int edepth = ref_stack_count(&e_stack);
  389. gs_memory_t *mem = gs_state_memory(igs);
  390. gs_ref_memory_t *imem = (gs_ref_memory_t *)mem;
  391. gs_color_space *pcs;
  392. ref_cie_procs procs;
  393. gs_cie_abc *pcie;
  394. int code;
  395. check_type(*op, t_dictionary);
  396. check_dict_read(*op);
  397. procs = istate->colorspace.procs.cie;
  398. code = gs_cspace_build_CIEABC(&pcs, NULL, mem);
  399. if (code < 0)
  400. return code;
  401. pcie = pcs->params.abc;
  402. code = cie_abc_param(imemory, op, pcie, &procs);
  403. if (code < 0 ||
  404. (code = cie_cache_joint(i_ctx_p, &istate->colorrendering.procs, (gs_cie_common *)pcie, igs)) < 0 || /* do this last */
  405. (code = cie_cache_push_finish(i_ctx_p, cie_abc_finish, imem, pcie)) < 0 ||
  406. (code = cache_abc_common(i_ctx_p, pcie, &procs, pcie, imem)) < 0
  407. )
  408. DO_NOTHING;
  409. return cie_set_finish(i_ctx_p, pcs, &procs, edepth, code);
  410. }
  411. private int
  412. cie_abc_finish(i_ctx_t *i_ctx_p)
  413. {
  414. os_ptr op = osp;
  415. gs_cie_abc *pcie = r_ptr(op, gs_cie_abc);
  416. pcie->DecodeABC = DecodeABC_from_cache;
  417. pcie->common.DecodeLMN = DecodeLMN_from_cache;
  418. gs_cie_abc_complete(pcie);
  419. pop(1);
  420. return 0;
  421. }
  422. /* <dict> .setcieaspace - */
  423. private int cie_a_finish(i_ctx_t *);
  424. private int
  425. zsetcieaspace(i_ctx_t *i_ctx_p)
  426. {
  427. os_ptr op = osp;
  428. int edepth = ref_stack_count(&e_stack);
  429. gs_memory_t *mem = gs_state_memory(igs);
  430. gs_ref_memory_t *imem = (gs_ref_memory_t *)mem;
  431. gs_color_space *pcs;
  432. ref_cie_procs procs;
  433. gs_cie_a *pcie;
  434. int code;
  435. check_type(*op, t_dictionary);
  436. check_dict_read(*op);
  437. procs = istate->colorspace.procs.cie;
  438. if ((code = dict_proc_param(op, "DecodeA", &procs.Decode.A, true)) < 0)
  439. return code;
  440. code = gs_cspace_build_CIEA(&pcs, NULL, mem);
  441. if (code < 0)
  442. return code;
  443. pcie = pcs->params.a;
  444. if ((code = dict_floats_param(imemory, op, "RangeA", 2, (float *)&pcie->RangeA, (const float *)&RangeA_default)) < 0 ||
  445. (code = dict_floats_param(imemory, op, "MatrixA", 3, (float *)&pcie->MatrixA, (const float *)&MatrixA_default)) < 0 ||
  446. (code = cie_lmnp_param(imemory, op, &pcie->common, &procs)) < 0 ||
  447. (code = cie_cache_joint(i_ctx_p, &istate->colorrendering.procs, (gs_cie_common *)pcie, igs)) < 0 || /* do this last */
  448. (code = cie_cache_push_finish(i_ctx_p, cie_a_finish, imem, pcie)) < 0 ||
  449. (code = cie_prepare_cache(i_ctx_p, &pcie->RangeA, &procs.Decode.A, &pcie->caches.DecodeA.floats, pcie, imem, "Decode.A")) < 0 ||
  450. (code = cache_common(i_ctx_p, &pcie->common, &procs, pcie, imem)) < 0
  451. )
  452. DO_NOTHING;
  453. pcie->DecodeA = DecodeA_default;
  454. return cie_set_finish(i_ctx_p, pcs, &procs, edepth, code);
  455. }
  456. private int
  457. cie_a_finish(i_ctx_t *i_ctx_p)
  458. {
  459. os_ptr op = osp;
  460. gs_cie_a *pcie = r_ptr(op, gs_cie_a);
  461. pcie->DecodeA = DecodeA_from_cache;
  462. pcie->common.DecodeLMN = DecodeLMN_from_cache;
  463. gs_cie_a_complete(pcie);
  464. pop(1);
  465. return 0;
  466. }
  467. /* Common cache code */
  468. private int
  469. cache_abc_common(i_ctx_t *i_ctx_p, gs_cie_abc * pcie,
  470. const ref_cie_procs * pcprocs,
  471. void *container, gs_ref_memory_t * imem)
  472. {
  473. int code =
  474. cie_prepare_cache3(i_ctx_p, &pcie->RangeABC,
  475. pcprocs->Decode.ABC.value.const_refs,
  476. pcie->caches.DecodeABC.caches, pcie, imem,
  477. "Decode.ABC");
  478. return (code < 0 ? code :
  479. cache_common(i_ctx_p, &pcie->common, pcprocs, pcie, imem));
  480. }
  481. private int
  482. cache_common(i_ctx_t *i_ctx_p, gs_cie_common * pcie,
  483. const ref_cie_procs * pcprocs,
  484. void *container, gs_ref_memory_t * imem)
  485. {
  486. return cie_prepare_cache3(i_ctx_p, &pcie->RangeLMN,
  487. pcprocs->DecodeLMN.value.const_refs,
  488. &pcie->caches.DecodeLMN[0], container, imem,
  489. "Decode.LMN");
  490. }
  491. /* ------ Internal routines ------ */
  492. /* Prepare to cache the values for one or more procedures. */
  493. private int cie_cache_finish1(i_ctx_t *);
  494. private int cie_cache_finish(i_ctx_t *);
  495. int
  496. cie_prepare_cache(i_ctx_t *i_ctx_p, const gs_range * domain, const ref * proc,
  497. cie_cache_floats * pcache, void *container,
  498. gs_ref_memory_t * imem, client_name_t cname)
  499. {
  500. int space = imemory_space(imem);
  501. gs_sample_loop_params_t lp;
  502. es_ptr ep;
  503. gs_cie_cache_init(&pcache->params, &lp, domain, cname);
  504. pcache->params.is_identity = r_size(proc) == 0;
  505. check_estack(9);
  506. ep = esp;
  507. make_real(ep + 9, lp.A);
  508. make_int(ep + 8, lp.N);
  509. make_real(ep + 7, lp.B);
  510. ep[6] = *proc;
  511. r_clear_attrs(ep + 6, a_executable);
  512. make_op_estack(ep + 5, zcvx);
  513. make_op_estack(ep + 4, zfor_samples);
  514. make_op_estack(ep + 3, cie_cache_finish);
  515. esp += 9;
  516. /*
  517. * The caches are embedded in the middle of other
  518. * structures, so we represent the pointer to the cache
  519. * as a pointer to the container plus an offset.
  520. */
  521. make_int(ep + 2, (char *)pcache - (char *)container);
  522. make_struct(ep + 1, space, container);
  523. return o_push_estack;
  524. }
  525. /* Note that pc3 may be 0, indicating that there are only 3 caches to load. */
  526. int
  527. cie_prepare_caches_4(i_ctx_t *i_ctx_p, const gs_range * domains,
  528. const ref * procs,
  529. cie_cache_floats * pc0, cie_cache_floats * pc1,
  530. cie_cache_floats * pc2, cie_cache_floats * pc3,
  531. void *container,
  532. gs_ref_memory_t * imem, client_name_t cname)
  533. {
  534. cie_cache_floats *pcn[4];
  535. int i, n, code = 0;
  536. pcn[0] = pc0, pcn[1] = pc1, pcn[2] = pc2;
  537. if (pc3 == 0)
  538. n = 3;
  539. else
  540. pcn[3] = pc3, n = 4;
  541. for (i = 0; i < n && code >= 0; ++i)
  542. code = cie_prepare_cache(i_ctx_p, domains + i, procs + i, pcn[i],
  543. container, imem, cname);
  544. return code;
  545. }
  546. /* Store the result of caching one procedure. */
  547. private int
  548. cie_cache_finish_store(i_ctx_t *i_ctx_p, bool replicate)
  549. {
  550. os_ptr op = osp;
  551. cie_cache_floats *pcache;
  552. int code;
  553. check_esp(2);
  554. /* See above for the container + offset representation of */
  555. /* the pointer to the cache. */
  556. pcache = (cie_cache_floats *) (r_ptr(esp - 1, char) + esp->value.intval);
  557. pcache->params.is_identity = false; /* cache_set_linear computes this */
  558. if_debug3('c', "[c]cache 0x%lx base=%g, factor=%g:\n",
  559. (ulong) pcache, pcache->params.base, pcache->params.factor);
  560. if (replicate ||
  561. (code = float_params(op, gx_cie_cache_size, &pcache->values[0])) < 0
  562. ) {
  563. /* We might have underflowed the current stack block. */
  564. /* Handle the parameters one-by-one. */
  565. uint i;
  566. for (i = 0; i < gx_cie_cache_size; i++) {
  567. code = float_param(ref_stack_index(&o_stack,
  568. (replicate ? 0 : gx_cie_cache_size - 1 - i)),
  569. &pcache->values[i]);
  570. if (code < 0)
  571. return code;
  572. }
  573. }
  574. #ifdef DEBUG
  575. if (gs_debug_c('c')) {
  576. int i;
  577. for (i = 0; i < gx_cie_cache_size; i += 4)
  578. dlprintf5("[c] cache[%3d]=%g, %g, %g, %g\n", i,
  579. pcache->values[i], pcache->values[i + 1],
  580. pcache->values[i + 2], pcache->values[i + 3]);
  581. }
  582. #endif
  583. ref_stack_pop(&o_stack, (replicate ? 1 : gx_cie_cache_size));
  584. esp -= 2; /* pop pointer to cache */
  585. return o_pop_estack;
  586. }
  587. private int
  588. cie_cache_finish(i_ctx_t *i_ctx_p)
  589. {
  590. return cie_cache_finish_store(i_ctx_p, false);
  591. }
  592. private int
  593. cie_cache_finish1(i_ctx_t *i_ctx_p)
  594. {
  595. return cie_cache_finish_store(i_ctx_p, true);
  596. }
  597. /* Push a finishing procedure on the e-stack. */
  598. /* ptr will be the top element of the o-stack. */
  599. int
  600. cie_cache_push_finish(i_ctx_t *i_ctx_p, op_proc_t finish_proc,
  601. gs_ref_memory_t * imem, void *data)
  602. {
  603. check_estack(2);
  604. push_op_estack(finish_proc);
  605. ++esp;
  606. make_struct(esp, imemory_space(imem), data);
  607. return o_push_estack;
  608. }
  609. /* ------ Initialization procedure ------ */
  610. const op_def zcie_l2_op_defs[] =
  611. {
  612. op_def_begin_level2(),
  613. {"1.setcieaspace", zsetcieaspace},
  614. {"1.setcieabcspace", zsetcieabcspace},
  615. {"1.setciedefspace", zsetciedefspace},
  616. {"1.setciedefgspace", zsetciedefgspace},
  617. /* Internal operators */
  618. {"1%cie_defg_finish", cie_defg_finish},
  619. {"1%cie_def_finish", cie_def_finish},
  620. {"1%cie_abc_finish", cie_abc_finish},
  621. {"1%cie_a_finish", cie_a_finish},
  622. {"0%cie_cache_finish", cie_cache_finish},
  623. {"1%cie_cache_finish1", cie_cache_finish1},
  624. op_def_end(0)
  625. };