gscrd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Copyright (C) 1998, 1999 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: gscrd.c,v 1.6 2004/08/04 19:36:12 stefan Exp $ */
  14. /* CIE color rendering dictionary creation */
  15. #include "math_.h"
  16. #include "memory_.h"
  17. #include "string_.h"
  18. #include "gx.h"
  19. #include "gscdefs.h" /* for gs_lib_device_list */
  20. #include "gsdevice.h"
  21. #include "gserrors.h"
  22. #include "gsmatrix.h" /* for gscolor2.h */
  23. #include "gsparam.h"
  24. #include "gsstruct.h"
  25. #include "gsutil.h"
  26. #include "gxcspace.h"
  27. #include "gscolor2.h" /* for gs_set/currentcolorrendering */
  28. #include "gscrd.h"
  29. /* Import gs_lib_device_list() */
  30. extern_gs_lib_device_list();
  31. /* Allocator structure type */
  32. public_st_cie_render1();
  33. private
  34. ENUM_PTRS_WITH(cie_render1_enum_ptrs, gs_cie_render *pcrd) return 0;
  35. case 0: return ENUM_OBJ(pcrd->client_data);
  36. case 1: return ENUM_OBJ(pcrd->RenderTable.lookup.table);
  37. case 2: return (pcrd->RenderTable.lookup.table ?
  38. ENUM_CONST_STRING(&pcrd->TransformPQR.proc_data) :
  39. 0);
  40. ENUM_PTRS_END
  41. private RELOC_PTRS_WITH(cie_render1_reloc_ptrs, gs_cie_render *pcrd);
  42. RELOC_OBJ_VAR(pcrd->client_data);
  43. if (pcrd->RenderTable.lookup.table)
  44. {
  45. RELOC_OBJ_VAR(pcrd->RenderTable.lookup.table);
  46. RELOC_CONST_STRING_VAR(pcrd->TransformPQR.proc_data);
  47. }
  48. RELOC_PTRS_END
  49. /* Default CRD procedures. */
  50. private int
  51. tpqr_identity(int index, floatp in, const gs_cie_wbsd * pwbsd,
  52. gs_cie_render * pcrd, float *out)
  53. {
  54. *out = in;
  55. return 0;
  56. }
  57. private int
  58. tpqr_from_cache(int index, floatp in, const gs_cie_wbsd * pwbsd,
  59. gs_cie_render * pcrd, float *out)
  60. {
  61. /*
  62. * Since the TransformPQR cache is in the joint caches, not in the
  63. * CRD cache, we can't actually implement this procedure.
  64. * Instead, the place that calls it checks for it specially.
  65. */
  66. *out = in;
  67. return 0;
  68. }
  69. private float
  70. render_identity(floatp in, const gs_cie_render * pcrd)
  71. {
  72. return in;
  73. }
  74. private frac
  75. render_table_identity(byte in, const gs_cie_render * pcrd)
  76. {
  77. return byte2frac(in);
  78. }
  79. /* Transformation procedures that just consult the cache. */
  80. private float
  81. EncodeABC_cached_A(floatp in, const gs_cie_render * pcrd)
  82. {
  83. return gs_cie_cached_value(in, &pcrd->caches.EncodeABC[0].floats);
  84. }
  85. private float
  86. EncodeABC_cached_B(floatp in, const gs_cie_render * pcrd)
  87. {
  88. return gs_cie_cached_value(in, &pcrd->caches.EncodeABC[1].floats);
  89. }
  90. private float
  91. EncodeABC_cached_C(floatp in, const gs_cie_render * pcrd)
  92. {
  93. return gs_cie_cached_value(in, &pcrd->caches.EncodeABC[2].floats);
  94. }
  95. private float
  96. EncodeLMN_cached_L(floatp in, const gs_cie_render * pcrd)
  97. {
  98. return gs_cie_cached_value(in, &pcrd->caches.EncodeLMN.caches[0].floats);
  99. }
  100. private float
  101. EncodeLMN_cached_M(floatp in, const gs_cie_render * pcrd)
  102. {
  103. return gs_cie_cached_value(in, &pcrd->caches.EncodeLMN.caches[1].floats);
  104. }
  105. private float
  106. EncodeLMN_cached_N(floatp in, const gs_cie_render * pcrd)
  107. {
  108. return gs_cie_cached_value(in, &pcrd->caches.EncodeLMN.caches[2].floats);
  109. }
  110. private frac
  111. RTT_cached(byte in, const gs_cie_render * pcrd, int i)
  112. {
  113. return pcrd->caches.RenderTableT[i].fracs.values[
  114. in * (gx_cie_cache_size - 1) / 255
  115. ];
  116. }
  117. private frac
  118. RTT_cached_0(byte in, const gs_cie_render * pcrd)
  119. {
  120. return RTT_cached(in, pcrd, 0);
  121. }
  122. private frac
  123. RTT_cached_1(byte in, const gs_cie_render * pcrd)
  124. {
  125. return RTT_cached(in, pcrd, 1);
  126. }
  127. private frac
  128. RTT_cached_2(byte in, const gs_cie_render * pcrd)
  129. {
  130. return RTT_cached(in, pcrd, 2);
  131. }
  132. private frac
  133. RTT_cached_3(byte in, const gs_cie_render * pcrd)
  134. {
  135. return RTT_cached(in, pcrd, 3);
  136. }
  137. /* Define the TransformPQR trampoline procedure that looks up proc_name. */
  138. private int
  139. tpqr_do_lookup(gs_cie_render *pcrd, const gx_device *dev_proto)
  140. {
  141. gx_device *dev;
  142. gs_memory_t *mem = pcrd->rc.memory;
  143. gs_c_param_list list;
  144. gs_param_string proc_addr;
  145. int code;
  146. /* Device prototypes are const, so we must create a copy. */
  147. code = gs_copydevice(&dev, dev_proto, mem);
  148. if (code < 0)
  149. return code;
  150. gs_c_param_list_write(&list, mem);
  151. code = param_request((gs_param_list *)&list,
  152. pcrd->TransformPQR.proc_name);
  153. if (code >= 0) {
  154. code = gs_getdeviceparams(dev, (gs_param_list *)&list);
  155. if (code >= 0) {
  156. gs_c_param_list_read(&list);
  157. code = param_read_string((gs_param_list *)&list,
  158. pcrd->TransformPQR.proc_name,
  159. &proc_addr);
  160. if (code == 0 && proc_addr.size == sizeof(gs_cie_transform_proc)) {
  161. memcpy(&pcrd->TransformPQR.proc, proc_addr.data,
  162. sizeof(gs_cie_transform_proc));
  163. } else
  164. code = gs_note_error(gs_error_rangecheck);
  165. }
  166. }
  167. gs_c_param_list_release(&list);
  168. gs_free_object(mem, dev, "tpqr_do_lookup(device)");
  169. return code;
  170. }
  171. private int
  172. tpqr_lookup(int index, floatp in, const gs_cie_wbsd * pwbsd,
  173. gs_cie_render * pcrd, float *out)
  174. {
  175. const gx_device *const *dev_list;
  176. int count = gs_lib_device_list(&dev_list, NULL);
  177. int i;
  178. int code;
  179. for (i = 0; i < count; ++i)
  180. if (!strcmp(gs_devicename(dev_list[i]),
  181. pcrd->TransformPQR.driver_name))
  182. break;
  183. if (i < count)
  184. code = tpqr_do_lookup(pcrd, dev_list[i]);
  185. else
  186. code = gs_note_error(gs_error_undefined);
  187. if (code < 0)
  188. return code;
  189. return pcrd->TransformPQR.proc(index, in, pwbsd, pcrd, out);
  190. }
  191. /* Default vectors. */
  192. const gs_cie_transform_proc3 TransformPQR_default = {
  193. tpqr_identity,
  194. 0, /* proc_name */
  195. {0, 0}, /* proc_data */
  196. 0 /* driver_name */
  197. };
  198. const gs_cie_transform_proc3 TransformPQR_from_cache = {
  199. tpqr_from_cache,
  200. 0, /* proc_name */
  201. {0, 0}, /* proc_data */
  202. 0 /* driver_name */
  203. };
  204. const gs_cie_transform_proc TransformPQR_lookup_proc_name = tpqr_lookup;
  205. const gs_cie_render_proc3 Encode_default = {
  206. {render_identity, render_identity, render_identity}
  207. };
  208. const gs_cie_render_proc3 EncodeLMN_from_cache = {
  209. {EncodeLMN_cached_L, EncodeLMN_cached_M, EncodeLMN_cached_N}
  210. };
  211. const gs_cie_render_proc3 EncodeABC_from_cache = {
  212. {EncodeABC_cached_A, EncodeABC_cached_B, EncodeABC_cached_C}
  213. };
  214. const gs_cie_render_table_procs RenderTableT_default = {
  215. {render_table_identity, render_table_identity, render_table_identity,
  216. render_table_identity
  217. }
  218. };
  219. const gs_cie_render_table_procs RenderTableT_from_cache = {
  220. {RTT_cached_0, RTT_cached_1, RTT_cached_2, RTT_cached_3}
  221. };
  222. /*
  223. * Allocate and minimally initialize a CRD. Note that this procedure sets
  224. * the reference count of the structure to 1, not 0. gs_setcolorrendering
  225. * will increment the reference count again, so unless you want the
  226. * structure to stay allocated permanently (or until a garbage collection),
  227. * you should call rc_decrement(pcrd, "client name") *after* calling
  228. * gs_setcolorrendering.
  229. */
  230. int
  231. gs_cie_render1_build(gs_cie_render ** ppcrd, gs_memory_t * mem,
  232. client_name_t cname)
  233. {
  234. gs_cie_render *pcrd;
  235. rc_alloc_struct_1(pcrd, gs_cie_render, &st_cie_render1, mem,
  236. return_error(gs_error_VMerror), cname);
  237. pcrd->id = gs_next_ids(mem, 1);
  238. /* Initialize pointers for the GC. */
  239. pcrd->client_data = 0;
  240. pcrd->RenderTable.lookup.table = 0;
  241. pcrd->status = CIE_RENDER_STATUS_BUILT;
  242. *ppcrd = pcrd;
  243. return 0;
  244. }
  245. /*
  246. * Initialize a CRD given all of the relevant parameters.
  247. * Any of the pointers except WhitePoint may be zero, meaning
  248. * use the default values.
  249. *
  250. * The actual point, matrix, range, and procedure values are copied into the
  251. * CRD, but only the pointer to the color lookup table is copied.
  252. *
  253. * If pfrom_crd is not NULL, then if the EncodeLMN, EncodeABC, or
  254. * RenderTable.T procedures indicate that the values exist only in the
  255. * cache, the corresponding values will be copied from pfrom_crd.
  256. * Note that NULL values for the individual pointers still represent
  257. * default values.
  258. */
  259. int
  260. gs_cie_render1_init_from(const gs_memory_t *mem,
  261. gs_cie_render * pcrd,
  262. void *client_data,
  263. const gs_cie_render * pfrom_crd,
  264. const gs_vector3 * WhitePoint,
  265. const gs_vector3 * BlackPoint,
  266. const gs_matrix3 * MatrixPQR,
  267. const gs_range3 * RangePQR,
  268. const gs_cie_transform_proc3 * TransformPQR,
  269. const gs_matrix3 * MatrixLMN,
  270. const gs_cie_render_proc3 * EncodeLMN,
  271. const gs_range3 * RangeLMN,
  272. const gs_matrix3 * MatrixABC,
  273. const gs_cie_render_proc3 * EncodeABC,
  274. const gs_range3 * RangeABC,
  275. const gs_cie_render_table_t * RenderTable)
  276. {
  277. pcrd->id = gs_next_ids(mem, 1);
  278. pcrd->client_data = client_data;
  279. pcrd->points.WhitePoint = *WhitePoint;
  280. pcrd->points.BlackPoint =
  281. *(BlackPoint ? BlackPoint : &BlackPoint_default);
  282. pcrd->MatrixPQR = *(MatrixPQR ? MatrixPQR : &Matrix3_default);
  283. pcrd->RangePQR = *(RangePQR ? RangePQR : &Range3_default);
  284. pcrd->TransformPQR =
  285. *(TransformPQR ? TransformPQR : &TransformPQR_default);
  286. pcrd->MatrixLMN = *(MatrixLMN ? MatrixLMN : &Matrix3_default);
  287. pcrd->EncodeLMN = *(EncodeLMN ? EncodeLMN : &Encode_default);
  288. if (pfrom_crd &&
  289. !memcmp(&pcrd->EncodeLMN, &EncodeLMN_from_cache,
  290. sizeof(EncodeLMN_from_cache))
  291. )
  292. memcpy(&pcrd->caches.EncodeLMN, &pfrom_crd->caches.EncodeLMN,
  293. sizeof(pcrd->caches.EncodeLMN));
  294. pcrd->RangeLMN = *(RangeLMN ? RangeLMN : &Range3_default);
  295. pcrd->MatrixABC = *(MatrixABC ? MatrixABC : &Matrix3_default);
  296. pcrd->EncodeABC = *(EncodeABC ? EncodeABC : &Encode_default);
  297. if (pfrom_crd &&
  298. !memcmp(&pcrd->EncodeABC, &EncodeABC_from_cache,
  299. sizeof(EncodeABC_from_cache))
  300. )
  301. memcpy(pcrd->caches.EncodeABC, pfrom_crd->caches.EncodeABC,
  302. sizeof(pcrd->caches.EncodeABC));
  303. pcrd->RangeABC = *(RangeABC ? RangeABC : &Range3_default);
  304. if (RenderTable) {
  305. pcrd->RenderTable = *RenderTable;
  306. if (pfrom_crd &&
  307. !memcmp(&pcrd->RenderTable.T, &RenderTableT_from_cache,
  308. sizeof(RenderTableT_from_cache))
  309. ) {
  310. memcpy(pcrd->caches.RenderTableT, pfrom_crd->caches.RenderTableT,
  311. sizeof(pcrd->caches.RenderTableT));
  312. pcrd->caches.RenderTableT_is_identity =
  313. pfrom_crd->caches.RenderTableT_is_identity;
  314. }
  315. } else {
  316. pcrd->RenderTable.lookup.table = 0;
  317. pcrd->RenderTable.T = RenderTableT_default;
  318. }
  319. pcrd->status = CIE_RENDER_STATUS_BUILT;
  320. return 0;
  321. }
  322. /*
  323. * Initialize a CRD without the option of copying cached values.
  324. */
  325. int
  326. gs_cie_render1_initialize(const gs_memory_t *mem,
  327. gs_cie_render * pcrd, void *client_data,
  328. const gs_vector3 * WhitePoint,
  329. const gs_vector3 * BlackPoint,
  330. const gs_matrix3 * MatrixPQR,
  331. const gs_range3 * RangePQR,
  332. const gs_cie_transform_proc3 * TransformPQR,
  333. const gs_matrix3 * MatrixLMN,
  334. const gs_cie_render_proc3 * EncodeLMN,
  335. const gs_range3 * RangeLMN,
  336. const gs_matrix3 * MatrixABC,
  337. const gs_cie_render_proc3 * EncodeABC,
  338. const gs_range3 * RangeABC,
  339. const gs_cie_render_table_t * RenderTable)
  340. {
  341. return gs_cie_render1_init_from(mem, pcrd, client_data, NULL,
  342. WhitePoint, BlackPoint,
  343. MatrixPQR, RangePQR, TransformPQR,
  344. MatrixLMN, EncodeLMN, RangeLMN,
  345. MatrixABC, EncodeABC, RangeABC,
  346. RenderTable);
  347. }