gscdevn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* Copyright (C) 1997, 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: gscdevn.c,v 1.6 2001/08/21 23:52:36 dancoby Exp $ */
  16. /* DeviceN color space and operation definition */
  17. #include "memory_.h"
  18. #include "gx.h"
  19. #include "gserrors.h"
  20. #include "gscdevn.h"
  21. #include "gsfunc.h"
  22. #include "gsrefct.h"
  23. #include "gsmatrix.h" /* for gscolor2.h */
  24. #include "gsstruct.h"
  25. #include "gxcspace.h"
  26. #include "gxcdevn.h"
  27. /* ---------------- Color space ---------------- */
  28. /* GC descriptors */
  29. gs_private_st_composite(st_color_space_DeviceN, gs_paint_color_space,
  30. "gs_color_space_DeviceN", cs_DeviceN_enum_ptrs, cs_DeviceN_reloc_ptrs);
  31. private_st_device_n_map();
  32. /* Define the DeviceN color space type. */
  33. private cs_proc_num_components(gx_num_components_DeviceN);
  34. private cs_proc_base_space(gx_alt_space_DeviceN);
  35. private cs_proc_equal(gx_equal_DeviceN);
  36. private cs_proc_init_color(gx_init_DeviceN);
  37. private cs_proc_restrict_color(gx_restrict_DeviceN);
  38. private cs_proc_concrete_space(gx_concrete_space_DeviceN);
  39. private cs_proc_concretize_color(gx_concretize_DeviceN);
  40. private cs_proc_remap_concrete_color(gx_remap_concrete_DeviceN);
  41. private cs_proc_install_cspace(gx_install_DeviceN);
  42. private cs_proc_adjust_cspace_count(gx_adjust_cspace_DeviceN);
  43. const gs_color_space_type gs_color_space_type_DeviceN = {
  44. gs_color_space_index_DeviceN, true, false,
  45. &st_color_space_DeviceN, gx_num_components_DeviceN,
  46. gx_alt_space_DeviceN, gx_equal_DeviceN,
  47. gx_init_DeviceN, gx_restrict_DeviceN,
  48. gx_concrete_space_DeviceN,
  49. gx_concretize_DeviceN, gx_remap_concrete_DeviceN,
  50. gx_default_remap_color, gx_install_DeviceN,
  51. gx_adjust_cspace_DeviceN, gx_no_adjust_color_count
  52. };
  53. /* GC procedures */
  54. private
  55. ENUM_PTRS_WITH(cs_DeviceN_enum_ptrs, gs_color_space *pcs)
  56. {
  57. return ENUM_USING(*pcs->params.device_n.alt_space.type->stype,
  58. &pcs->params.device_n.alt_space,
  59. sizeof(pcs->params.device_n.alt_space), index - 2);
  60. }
  61. ENUM_PTR(0, gs_color_space, params.device_n.names);
  62. ENUM_PTR(1, gs_color_space, params.device_n.map);
  63. ENUM_PTRS_END
  64. private RELOC_PTRS_WITH(cs_DeviceN_reloc_ptrs, gs_color_space *pcs)
  65. {
  66. RELOC_PTR(gs_color_space, params.device_n.names);
  67. RELOC_PTR(gs_color_space, params.device_n.map);
  68. RELOC_USING(*pcs->params.device_n.alt_space.type->stype,
  69. &pcs->params.device_n.alt_space,
  70. sizeof(gs_base_color_space));
  71. }
  72. RELOC_PTRS_END
  73. /* ------ Public procedures ------ */
  74. /*
  75. * Build a DeviceN color space.
  76. */
  77. int
  78. gs_cspace_build_DeviceN(
  79. gs_color_space **ppcspace,
  80. gs_separation_name *psnames,
  81. uint num_components,
  82. const gs_color_space *palt_cspace,
  83. gs_memory_t *pmem
  84. )
  85. {
  86. gs_color_space *pcspace = 0; /* bogus initialization */
  87. gs_device_n_params *pcsdevn = 0; /* bogus initialization */
  88. int code;
  89. if (palt_cspace == 0 || !palt_cspace->type->can_be_alt_space)
  90. return_error(gs_error_rangecheck);
  91. code = gs_cspace_alloc(&pcspace, &gs_color_space_type_DeviceN, pmem);
  92. if (code < 0)
  93. return code;
  94. pcsdevn = &pcspace->params.device_n;
  95. code = alloc_device_n_map(&pcsdevn->map, pmem, "gs_cspace_build_DeviceN");
  96. if (code < 0) {
  97. gs_free_object(pmem, pcspace, "gs_cspace_build_DeviceN");
  98. return code;
  99. }
  100. pcsdevn->names = psnames;
  101. pcsdevn->num_components = num_components;
  102. gs_cspace_init_from((gs_color_space *)&pcsdevn->alt_space, palt_cspace);
  103. *ppcspace = pcspace;
  104. return 0;
  105. }
  106. /* Allocate and initialize a DeviceN map. */
  107. int
  108. alloc_device_n_map(gs_device_n_map ** ppmap, gs_memory_t * mem,
  109. client_name_t cname)
  110. {
  111. gs_device_n_map *pimap;
  112. rc_alloc_struct_1(pimap, gs_device_n_map, &st_device_n_map, mem,
  113. return_error(gs_error_VMerror), cname);
  114. pimap->tint_transform = 0;
  115. pimap->tint_transform_data = 0;
  116. pimap->cache_valid = false;
  117. *ppmap = pimap;
  118. return 0;
  119. }
  120. /*
  121. * Set the DeviceN tint transformation procedure.
  122. */
  123. int
  124. gs_cspace_set_devn_proc(gs_color_space * pcspace,
  125. int (*proc)(P5(const gs_device_n_params *,
  126. const float *,
  127. float *,
  128. const gs_imager_state *,
  129. void *
  130. )),
  131. void *proc_data
  132. )
  133. {
  134. gs_device_n_map *pimap;
  135. if (gs_color_space_get_index(pcspace) != gs_color_space_index_DeviceN)
  136. return_error(gs_error_rangecheck);
  137. pimap = pcspace->params.device_n.map;
  138. pimap->tint_transform = proc;
  139. pimap->tint_transform_data = proc_data;
  140. pimap->cache_valid = false;
  141. return 0;
  142. }
  143. /* Map a DeviceN color using a Function. */
  144. private int
  145. map_devn_using_function(const gs_device_n_params *pcsdevn,
  146. const float *in, float *out,
  147. const gs_imager_state *pis, void *data)
  148. {
  149. gs_function_t *const pfn = data;
  150. return gs_function_evaluate(pfn, in, out);
  151. }
  152. /*
  153. * Set the DeviceN tint transformation procedure to a Function.
  154. */
  155. int
  156. gs_cspace_set_devn_function(gs_color_space *pcspace, gs_function_t *pfn)
  157. {
  158. gs_device_n_map *pimap;
  159. if (gs_color_space_get_index(pcspace) != gs_color_space_index_DeviceN ||
  160. pfn->params.m != pcspace->params.device_n.num_components ||
  161. pfn->params.n !=
  162. gs_color_space_num_components((gs_color_space *)
  163. &pcspace->params.device_n.alt_space)
  164. )
  165. return_error(gs_error_rangecheck);
  166. pimap = pcspace->params.device_n.map;
  167. pimap->tint_transform = map_devn_using_function;
  168. pimap->tint_transform_data = pfn;
  169. pimap->cache_valid = false;
  170. return 0;
  171. }
  172. /*
  173. * If the DeviceN tint transformation procedure is a Function,
  174. * return the function object, otherwise return 0.
  175. */
  176. gs_function_t *
  177. gs_cspace_get_devn_function(const gs_color_space *pcspace)
  178. {
  179. if (gs_color_space_get_index(pcspace) == gs_color_space_index_DeviceN &&
  180. pcspace->params.device_n.map->tint_transform ==
  181. map_devn_using_function)
  182. return pcspace->params.device_n.map->tint_transform_data;
  183. return 0;
  184. }
  185. /* ------ Color space implementation ------ */
  186. /* Return the number of components of a DeviceN space. */
  187. private int
  188. gx_num_components_DeviceN(const gs_color_space * pcs)
  189. {
  190. return pcs->params.device_n.num_components;
  191. }
  192. /* Return the alternate space of a DeviceN space. */
  193. private const gs_color_space *
  194. gx_alt_space_DeviceN(const gs_color_space * pcs)
  195. {
  196. return (const gs_color_space *)&(pcs->params.device_n.alt_space);
  197. }
  198. /* Test whether one DeviceN color space equals another. */
  199. private bool
  200. gx_equal_DeviceN(const gs_color_space *pcs1, const gs_color_space *pcs2)
  201. {
  202. return (gs_color_space_equal(gx_alt_space_DeviceN(pcs1),
  203. gx_alt_space_DeviceN(pcs2)) &&
  204. pcs1->params.device_n.num_components ==
  205. pcs2->params.device_n.num_components &&
  206. !memcmp(pcs1->params.device_n.names, pcs2->params.device_n.names,
  207. pcs1->params.device_n.num_components *
  208. sizeof(pcs1->params.device_n.names[0])) &&
  209. pcs1->params.device_n.map->tint_transform ==
  210. pcs2->params.device_n.map->tint_transform &&
  211. pcs1->params.device_n.map->tint_transform_data ==
  212. pcs2->params.device_n.map->tint_transform_data);
  213. }
  214. /* Initialize a DeviceN color. */
  215. private void
  216. gx_init_DeviceN(gs_client_color * pcc, const gs_color_space * pcs)
  217. {
  218. int i;
  219. for (i = 0; i < pcs->params.device_n.num_components; ++i)
  220. pcc->paint.values[i] = 1.0;
  221. }
  222. /* Force a DeviceN color into legal range. */
  223. private void
  224. gx_restrict_DeviceN(gs_client_color * pcc, const gs_color_space * pcs)
  225. {
  226. int i;
  227. for (i = 0; i < pcs->params.device_n.num_components; ++i) {
  228. floatp value = pcc->paint.values[i];
  229. pcc->paint.values[i] = (value <= 0 ? 0 : value >= 1 ? 1 : value);
  230. }
  231. }
  232. /* Remap a DeviceN color. */
  233. private const gs_color_space *
  234. gx_concrete_space_DeviceN(const gs_color_space * pcs,
  235. const gs_imager_state * pis)
  236. {
  237. /* We don't support concrete DeviceN spaces yet. */
  238. const gs_color_space *pacs =
  239. (const gs_color_space *)&pcs->params.device_n.alt_space;
  240. return cs_concrete_space(pacs, pis);
  241. }
  242. private int
  243. gx_concretize_DeviceN(const gs_client_color * pc, const gs_color_space * pcs,
  244. frac * pconc, const gs_imager_state * pis)
  245. {
  246. int code, tcode;
  247. gs_client_color cc;
  248. const gs_color_space *pacs =
  249. (const gs_color_space *)&pcs->params.device_n.alt_space;
  250. gs_device_n_map *map = pcs->params.device_n.map;
  251. /* Check the 1-element cache first. */
  252. if (map->cache_valid) {
  253. int i;
  254. for (i = pcs->params.device_n.num_components; --i >= 0;) {
  255. if (map->tint[i] != pc->paint.values[i])
  256. break;
  257. }
  258. if (i < 0) {
  259. int num_out = gs_color_space_num_components(pacs);
  260. for (i = 0; i < num_out; ++i)
  261. pconc[i] = map->conc[i];
  262. return 0;
  263. }
  264. }
  265. /*
  266. * We always map into the alternate color space. We must preserve
  267. * tcode for implementing a semi-hack in the interpreter.
  268. */
  269. tcode = (*pcs->params.device_n.map->tint_transform)
  270. (&pcs->params.device_n, pc->paint.values, &cc.paint.values[0],
  271. pis, pcs->params.device_n.map->tint_transform_data);
  272. if (tcode < 0)
  273. return tcode;
  274. code = (*pacs->type->concretize_color) (&cc, pacs, pconc, pis);
  275. return (code < 0 || tcode == 0 ? code : tcode);
  276. }
  277. private int
  278. gx_remap_concrete_DeviceN(const frac * pconc,
  279. gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
  280. gs_color_select_t select)
  281. {
  282. /* We don't support concrete DeviceN colors yet. */
  283. return_error(gs_error_rangecheck);
  284. }
  285. /* Install a DeviceN color space. */
  286. private int
  287. gx_install_DeviceN(const gs_color_space * pcs, gs_state * pgs)
  288. {
  289. const gs_separation_name *names = pcs->params.device_n.names;
  290. uint i, j;
  291. const char none_str[] = "None";
  292. const int none_size = strlen(none_str);
  293. /*
  294. * Postscript does not accept /None as a color component but it is
  295. * allowed in PDF so we accept it. Except for /None, no components
  296. * are allowed to have duplicated names.
  297. */
  298. for (i = 1; i < pcs->params.device_n.num_components; ++i) {
  299. byte *pname;
  300. uint name_size;
  301. pcs->params.device_n.get_colorname_string(names[i], &pname, &name_size);
  302. if (name_size != none_size ||
  303. (strncmp(none_str, (const char *) pname, name_size)!=0)) {
  304. for (j = 0; j < i; ++j) {
  305. if (names[i] == names[j])
  306. return_error(gs_error_rangecheck);
  307. }
  308. }
  309. }
  310. return (*pcs->params.device_n.alt_space.type->install_cspace)
  311. ((const gs_color_space *) & pcs->params.device_n.alt_space, pgs);
  312. }
  313. /* Adjust the reference count of a DeviceN color space. */
  314. private void
  315. gx_adjust_cspace_DeviceN(const gs_color_space * pcs, int delta)
  316. {
  317. rc_adjust_const(pcs->params.device_n.map, delta, "gx_adjust_DeviceN");
  318. (*pcs->params.device_n.alt_space.type->adjust_cspace_count)
  319. ((const gs_color_space *)&pcs->params.device_n.alt_space, delta);
  320. }