gscsepr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* Copyright (C) 1994, 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gscsepr.c,v 1.12.2.1 2001/10/21 06:05:44 raph Exp $ */
  16. /* Separation color space and operation definition */
  17. #include "memory_.h"
  18. #include "gx.h"
  19. #include "gserrors.h"
  20. #include "gsfunc.h"
  21. #include "gsrefct.h"
  22. #include "gsmatrix.h" /* for gscolor2.h */
  23. #include "gscsepr.h"
  24. #include "gxcspace.h"
  25. #include "gxfixed.h" /* for gxcolor2.h */
  26. #include "gxcolor2.h" /* for gs_indexed_map */
  27. #include "gzstate.h" /* for pgs->overprint */
  28. /* ---------------- Color space ---------------- */
  29. gs_private_st_composite(st_color_space_Separation, gs_paint_color_space,
  30. "gs_color_space_Separation",
  31. cs_Separation_enum_ptrs, cs_Separation_reloc_ptrs);
  32. /* Define the Separation color space type. */
  33. private cs_proc_base_space(gx_alt_space_Separation);
  34. private cs_proc_equal(gx_equal_Separation);
  35. private cs_proc_init_color(gx_init_Separation);
  36. private cs_proc_concrete_space(gx_concrete_space_Separation);
  37. private cs_proc_concretize_color(gx_concretize_Separation);
  38. private cs_proc_remap_concrete_color(gx_remap_concrete_Separation);
  39. private cs_proc_remap_color(gx_remap_Separation);
  40. private cs_proc_install_cspace(gx_install_Separation);
  41. private cs_proc_adjust_cspace_count(gx_adjust_cspace_Separation);
  42. const gs_color_space_type gs_color_space_type_Separation = {
  43. gs_color_space_index_Separation, true, false,
  44. &st_color_space_Separation, gx_num_components_1,
  45. gx_alt_space_Separation, gx_equal_Separation,
  46. gx_init_Separation, gx_restrict01_paint_1,
  47. gx_concrete_space_Separation,
  48. gx_concretize_Separation, gx_remap_concrete_Separation,
  49. gx_remap_Separation, gx_install_Separation,
  50. gx_adjust_cspace_Separation, gx_no_adjust_color_count
  51. };
  52. /* GC procedures */
  53. private
  54. ENUM_PTRS_WITH(cs_Separation_enum_ptrs, gs_color_space *pcs)
  55. {
  56. return ENUM_USING(*pcs->params.separation.alt_space.type->stype,
  57. &pcs->params.separation.alt_space,
  58. sizeof(pcs->params.separation.alt_space), index - 1);
  59. }
  60. ENUM_PTR(0, gs_color_space, params.separation.map);
  61. ENUM_PTRS_END
  62. private RELOC_PTRS_WITH(cs_Separation_reloc_ptrs, gs_color_space *pcs)
  63. {
  64. RELOC_PTR(gs_color_space, params.separation.map);
  65. RELOC_USING(*pcs->params.separation.alt_space.type->stype,
  66. &pcs->params.separation.alt_space,
  67. sizeof(gs_base_color_space));
  68. }
  69. RELOC_PTRS_END
  70. /* Get the alternate space for a Separation space. */
  71. private const gs_color_space *
  72. gx_alt_space_Separation(const gs_color_space * pcs)
  73. {
  74. return (const gs_color_space *)&(pcs->params.separation.alt_space);
  75. }
  76. /* Test whether one Separation color space equals another. */
  77. private bool
  78. gx_equal_Separation(const gs_color_space *pcs1, const gs_color_space *pcs2)
  79. {
  80. return (gs_color_space_equal(gx_alt_space_Separation(pcs1),
  81. gx_alt_space_Separation(pcs2)) &&
  82. pcs1->params.separation.sname == pcs2->params.separation.sname &&
  83. ((pcs1->params.separation.map->proc.tint_transform ==
  84. pcs2->params.separation.map->proc.tint_transform &&
  85. pcs1->params.separation.map->proc_data ==
  86. pcs2->params.separation.map->proc_data) ||
  87. !memcmp(pcs1->params.separation.map->values,
  88. pcs2->params.separation.map->values,
  89. pcs1->params.separation.map->num_values *
  90. sizeof(pcs1->params.separation.map->values[0]))));
  91. }
  92. /* Get the concrete space for a Separation space. */
  93. /* (We don't support concrete Separation spaces yet.) */
  94. private const gs_color_space *
  95. gx_concrete_space_Separation(const gs_color_space * pcs,
  96. const gs_imager_state * pis)
  97. {
  98. const gs_color_space *pacs =
  99. (const gs_color_space *)&pcs->params.separation.alt_space;
  100. return cs_concrete_space(pacs, pis);
  101. }
  102. /* Install a Separation color space. */
  103. private int
  104. gx_install_Separation(const gs_color_space * pcs, gs_state * pgs)
  105. {
  106. return (*pcs->params.separation.alt_space.type->install_cspace)
  107. ((const gs_color_space *) & pcs->params.separation.alt_space, pgs);
  108. }
  109. /* Adjust the reference count of a Separation color space. */
  110. private void
  111. gx_adjust_cspace_Separation(const gs_color_space * pcs, int delta)
  112. {
  113. rc_adjust_const(pcs->params.separation.map, delta,
  114. "gx_adjust_Separation");
  115. (*pcs->params.separation.alt_space.type->adjust_cspace_count)
  116. ((const gs_color_space *)&pcs->params.separation.alt_space, delta);
  117. }
  118. /* ------ Constructors/accessors ------ */
  119. /*
  120. * The default separation tint transformation function. This will just return
  121. * the information in the cache or, if the cache is of zero size, set all
  122. * components in the alternative color space to 0.
  123. *
  124. * No special cases are provided for this routine, as the use of separations
  125. * (particular in this form) is sufficiently rare to not have a significant
  126. * performance impact.
  127. */
  128. private int
  129. map_tint_value(const gs_separation_params * pcssepr, floatp in_val,
  130. float *out_vals)
  131. {
  132. int ncomps =
  133. cs_num_components((const gs_color_space *)&pcssepr->alt_space);
  134. int nentries = pcssepr->map->num_values / ncomps;
  135. int indx;
  136. const float *pv = pcssepr->map->values;
  137. int i;
  138. if (nentries == 0) {
  139. for (i = 0; i < ncomps; i++)
  140. out_vals[i] = 0.0;
  141. return 0;
  142. }
  143. if (in_val > 1)
  144. indx = nentries - 1;
  145. else if (in_val <= 0)
  146. indx = 0;
  147. else
  148. indx = (int)(in_val * nentries + 0.5);
  149. pv += indx * ncomps;
  150. for (i = 0; i < ncomps; i++)
  151. out_vals[i] = pv[i];
  152. return 0;
  153. }
  154. /*
  155. * Allocate the indexed map required by a separation color space.
  156. */
  157. private gs_indexed_map *
  158. alloc_separation_map(const gs_color_space * palt_cspace, int cache_size,
  159. gs_memory_t * pmem)
  160. {
  161. int num_values =
  162. (cache_size == 0 ? 0 :
  163. cache_size * gs_color_space_num_components(palt_cspace));
  164. gs_indexed_map *pimap;
  165. int code = alloc_indexed_map(&pimap, num_values, pmem,
  166. "gs_cspace_build_Separation");
  167. if (code < 0)
  168. return 0;
  169. pimap->proc.tint_transform = map_tint_value;
  170. return pimap;
  171. }
  172. /*
  173. * Build a separation color space.
  174. *
  175. * The values array provided with separation color spaces is actually cached
  176. * information, but filled in by the client. The alternative space is the
  177. * color space in which the tint procedure will provide alternative colors.
  178. */
  179. int
  180. gs_cspace_build_Separation(
  181. gs_color_space ** ppcspace,
  182. gs_separation_name sname,
  183. const gs_color_space * palt_cspace,
  184. int cache_size,
  185. gs_memory_t * pmem
  186. )
  187. {
  188. gs_color_space *pcspace = 0;
  189. gs_separation_params *pcssepr = 0;
  190. int code;
  191. if (palt_cspace == 0 || !palt_cspace->type->can_be_alt_space)
  192. return_error(gs_error_rangecheck);
  193. code = gs_cspace_alloc(&pcspace, &gs_color_space_type_Separation, pmem);
  194. if (code < 0)
  195. return code;
  196. pcssepr = &pcspace->params.separation;
  197. pcssepr->map = alloc_separation_map(palt_cspace, cache_size, pmem);
  198. if (pcssepr->map == 0) {
  199. gs_free_object(pmem, pcspace, "gs_cspace_build_Separation");
  200. return_error(gs_error_VMerror);
  201. }
  202. pcssepr->sname = sname;
  203. gs_cspace_init_from((gs_color_space *) & pcssepr->alt_space, palt_cspace);
  204. *ppcspace = pcspace;
  205. return 0;
  206. }
  207. /*
  208. * Get the cached value array for a separation color space. This will return
  209. * a null pointer if the color space is not a separation color space, or if
  210. * the separation color space has a cache size of 0.
  211. */
  212. float *
  213. gs_cspace_get_sepr_value_array(const gs_color_space * pcspace)
  214. {
  215. if (gs_color_space_get_index(pcspace) != gs_color_space_index_Separation)
  216. return 0;
  217. return pcspace->params.separation.map->values;
  218. }
  219. /*
  220. * Set the tint transformation procedure used by a Separation color space.
  221. */
  222. int
  223. gs_cspace_set_sepr_proc(gs_color_space * pcspace,
  224. int (*proc) (P3(const gs_separation_params *, floatp, float *)))
  225. {
  226. gs_indexed_map *pimap;
  227. if (gs_color_space_get_index(pcspace) != gs_color_space_index_Separation)
  228. return_error(gs_error_rangecheck);
  229. pimap = pcspace->params.separation.map;
  230. pimap->proc.tint_transform = proc;
  231. pimap->proc_data = 0;
  232. return 0;
  233. }
  234. /* Map a Separation tint using a Function. */
  235. private int
  236. map_sepr_using_function(const gs_separation_params * pcssepr,
  237. floatp in_val, float *out_vals)
  238. {
  239. float in = in_val;
  240. gs_function_t *const pfn = pcssepr->map->proc_data;
  241. return gs_function_evaluate(pfn, &in, out_vals);
  242. }
  243. /*
  244. * Set the Separation tint transformation procedure to a Function.
  245. */
  246. int
  247. gs_cspace_set_sepr_function(const gs_color_space *pcspace, gs_function_t *pfn)
  248. {
  249. gs_indexed_map *pimap;
  250. if (gs_color_space_get_index(pcspace) != gs_color_space_index_Separation ||
  251. pfn->params.m != 1 ||
  252. pfn->params.n !=
  253. gs_color_space_num_components((const gs_color_space *)
  254. &pcspace->params.separation.alt_space)
  255. )
  256. return_error(gs_error_rangecheck);
  257. pimap = pcspace->params.separation.map;
  258. pimap->proc.tint_transform = map_sepr_using_function;
  259. pimap->proc_data = pfn;
  260. return 0;
  261. }
  262. /*
  263. * If the Separation tint transformation procedure is a Function,
  264. * return the function object, otherwise return 0.
  265. */
  266. gs_function_t *
  267. gs_cspace_get_sepr_function(const gs_color_space *pcspace)
  268. {
  269. if (gs_color_space_get_index(pcspace) == gs_color_space_index_Separation &&
  270. pcspace->params.separation.map->proc.tint_transform ==
  271. map_sepr_using_function)
  272. return pcspace->params.separation.map->proc_data;
  273. return 0;
  274. }
  275. /* ---------------- Graphics state ---------------- */
  276. /* setoverprint */
  277. void
  278. gs_setoverprint(gs_state * pgs, bool ovp)
  279. {
  280. pgs->overprint = ovp;
  281. }
  282. /* currentoverprint */
  283. bool
  284. gs_currentoverprint(const gs_state * pgs)
  285. {
  286. return pgs->overprint;
  287. }
  288. /* setoverprintmode */
  289. int
  290. gs_setoverprintmode(gs_state * pgs, int mode)
  291. {
  292. if (mode < 0 || mode > 1)
  293. return_error(gs_error_rangecheck);
  294. pgs->overprint_mode = mode;
  295. return 0;
  296. }
  297. /* currentoverprintmode */
  298. int
  299. gs_currentoverprintmode(const gs_state * pgs)
  300. {
  301. return pgs->overprint_mode;
  302. }
  303. /* ------ Internal procedures ------ */
  304. /* Initialize a Separation color. */
  305. private void
  306. gx_init_Separation(gs_client_color * pcc, const gs_color_space * pcs)
  307. {
  308. pcc->paint.values[0] = 1.0;
  309. }
  310. /* Remap a Separation color. */
  311. private int
  312. gx_remap_Separation(const gs_client_color * pcc, const gs_color_space * pcs,
  313. gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
  314. gs_color_select_t select)
  315. {
  316. if (pcs->params.separation.sep_type != SEP_NONE)
  317. return gx_default_remap_color(pcc, pcs, pdc, pis, dev, select);
  318. color_set_null(pdc);
  319. return 0;
  320. }
  321. private int
  322. gx_concretize_Separation(const gs_client_color *pc, const gs_color_space *pcs,
  323. frac *pconc, const gs_imager_state *pis)
  324. {
  325. float tint;
  326. int code;
  327. gs_client_color cc;
  328. const gs_color_space *pacs =
  329. (const gs_color_space *)&pcs->params.separation.alt_space;
  330. if (pcs->params.separation.sep_type == SEP_ALL) {
  331. /* "All" means setting all device components to same value. */
  332. const gs_color_space *pconcs = cs_concrete_space(pacs, pis);
  333. int i, n = cs_num_components(pconcs);
  334. frac conc;
  335. gs_client_color hack_color = *pc;
  336. /* Invert the photometric interpretation for additive
  337. * color spaces because separations are always subtractive.
  338. * fixme: this code sets all colorants in the alternative
  339. * color space, not the destination color space. This is
  340. * wrong.
  341. */
  342. if(n==1 || n==3)
  343. hack_color.paint.values[0] = 1 - pc->paint.values[0];
  344. /* hack: using DeviceGray's function to concretize single component color : */
  345. code = gx_concretize_DeviceGray(&hack_color, pacs, &conc, pis);
  346. for (i = 0; i < n; i++)
  347. pconc[i] = conc;
  348. return code;
  349. }
  350. tint = pc->paint.values[0];
  351. if (tint < 0)
  352. tint = 0;
  353. else if (tint > 1)
  354. tint = 1;
  355. /* We always map into the alternate color space. */
  356. code = (*pcs->params.separation.map->proc.tint_transform) (&pcs->params.separation, tint, &cc.paint.values[0]);
  357. if (code < 0)
  358. return code;
  359. return (*pacs->type->concretize_color) (&cc, pacs, pconc, pis);
  360. }
  361. private int
  362. gx_remap_concrete_Separation(const frac * pconc,
  363. gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
  364. gs_color_select_t select)
  365. { /* We don't support concrete Separation colors yet. */
  366. return_error(gs_error_rangecheck);
  367. }
  368. /* ---------------- Notes on real Separation colors ---------------- */
  369. typedef ulong gs_separation; /* BOGUS */
  370. #define gs_no_separation ((gs_separation)(-1L))
  371. #define dev_proc_lookup_separation(proc)\
  372. gs_separation proc(P4(gx_device *dev, const byte *sname, uint len,\
  373. gx_color_value *num_levels))
  374. #define dev_proc_map_tint_color(proc)\
  375. gx_color_index proc(P4(gx_device *dev, gs_separation sepr, bool overprint,\
  376. gx_color_value tint))
  377. /*
  378. * In principle, setting a Separation color space, or setting the device
  379. * when the current color space is a Separation space, calls the
  380. * lookup_separation device procedure to obtain the separation ID and
  381. * the number of achievable levels. Currently, the only hooks for doing
  382. * this are unsuitable: gx_set_cmap_procs isn't called when the color
  383. * space changes, and doing it in gx_remap_Separation is inefficient.
  384. * Probably the best approach is to call gx_set_cmap_procs whenever the
  385. * color space changes. In fact, if we do this, we can probably short-cut
  386. * two levels of procedure call in color remapping (gx_remap_color, by
  387. * turning it into a macro, and gx_remap_DeviceXXX, by calling the
  388. * cmap_proc procedure directly). Some care will be required for the
  389. * implicit temporary resetting of the color space in [color]image.
  390. *
  391. * For actual remapping of Separation colors, we need cmap_separation_direct
  392. * and cmap_separation_halftoned, just as for the other device color spaces.
  393. * So we need to break apart gx_render_gray in gxdither.c so it can also
  394. * do the job for separations.
  395. */