gscscie.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* Copyright (C) 1998, 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: gscscie.c,v 1.4 2001/03/17 01:15:42 raph Exp $ */
  16. /* CIE color space management */
  17. #include "math_.h"
  18. #include "gx.h"
  19. #include "gserrors.h"
  20. #include "gsstruct.h"
  21. #include "gsmatrix.h" /* for gscolor2.h */
  22. #include "gxcspace.h"
  23. #include "gscolor2.h" /* for gs_set/currentcolorrendering */
  24. #include "gxcie.h"
  25. #include "gxarith.h"
  26. #include "gxdevice.h" /* for gxcmap.h */
  27. #include "gxcmap.h"
  28. #include "gzstate.h"
  29. /* ---------------- Color space definition ---------------- */
  30. /* GC descriptors */
  31. public_st_cie_common();
  32. public_st_cie_common_elements();
  33. private_st_cie_a();
  34. private_st_cie_abc();
  35. private_st_cie_def();
  36. private_st_cie_defg();
  37. /* Define the CIE color space types. */
  38. /* CIEBasedDEFG */
  39. gs_private_st_ptrs1(st_color_space_CIEDEFG, gs_base_color_space,
  40. "gs_color_space(CIEDEFG)", cs_CIEDEFG_enum_ptrs, cs_CIEDEFG_reloc_ptrs,
  41. params.defg);
  42. private cs_proc_adjust_cspace_count(gx_adjust_cspace_CIEDEFG);
  43. const gs_color_space_type gs_color_space_type_CIEDEFG = {
  44. gs_color_space_index_CIEDEFG, true, true,
  45. &st_color_space_CIEDEFG, gx_num_components_4,
  46. gx_no_base_space, gx_cspace_not_equal,
  47. gx_init_CIE, gx_restrict_CIEDEFG,
  48. gx_concrete_space_CIE,
  49. gx_concretize_CIEDEFG, NULL,
  50. gx_default_remap_color, gx_install_CIE,
  51. gx_adjust_cspace_CIEDEFG, gx_no_adjust_color_count
  52. };
  53. /* CIEBasedDEF */
  54. gs_private_st_ptrs1(st_color_space_CIEDEF, gs_base_color_space,
  55. "gs_color_space(CIEDEF)", cs_CIEDEF_enum_ptrs, cs_CIEDEF_reloc_ptrs,
  56. params.def);
  57. private cs_proc_adjust_cspace_count(gx_adjust_cspace_CIEDEF);
  58. const gs_color_space_type gs_color_space_type_CIEDEF = {
  59. gs_color_space_index_CIEDEF, true, true,
  60. &st_color_space_CIEDEF, gx_num_components_3,
  61. gx_no_base_space, gx_cspace_not_equal,
  62. gx_init_CIE, gx_restrict_CIEDEF,
  63. gx_concrete_space_CIE,
  64. gx_concretize_CIEDEF, NULL,
  65. gx_default_remap_color, gx_install_CIE,
  66. gx_adjust_cspace_CIEDEF, gx_no_adjust_color_count
  67. };
  68. /* CIEBasedABC */
  69. gs_private_st_ptrs1(st_color_space_CIEABC, gs_base_color_space,
  70. "gs_color_space(CIEABC)", cs_CIEABC_enum_ptrs, cs_CIEABC_reloc_ptrs,
  71. params.abc);
  72. private cs_proc_adjust_cspace_count(gx_adjust_cspace_CIEABC);
  73. const gs_color_space_type gs_color_space_type_CIEABC = {
  74. gs_color_space_index_CIEABC, true, true,
  75. &st_color_space_CIEABC, gx_num_components_3,
  76. gx_no_base_space, gx_cspace_not_equal,
  77. gx_init_CIE, gx_restrict_CIEABC,
  78. gx_concrete_space_CIE,
  79. gx_concretize_CIEABC, NULL,
  80. gx_remap_CIEABC, gx_install_CIE,
  81. gx_adjust_cspace_CIEABC, gx_no_adjust_color_count
  82. };
  83. /* CIEBasedA */
  84. gs_private_st_ptrs1(st_color_space_CIEA, gs_base_color_space,
  85. "gs_color_space(CIEA)", cs_CIEA_enum_ptrs, cs_CIEA_reloc_ptrs,
  86. params.a);
  87. private cs_proc_adjust_cspace_count(gx_adjust_cspace_CIEA);
  88. const gs_color_space_type gs_color_space_type_CIEA = {
  89. gs_color_space_index_CIEA, true, true,
  90. &st_color_space_CIEA, gx_num_components_1,
  91. gx_no_base_space, gx_cspace_not_equal,
  92. gx_init_CIE, gx_restrict_CIEA,
  93. gx_concrete_space_CIE,
  94. gx_concretize_CIEA, NULL,
  95. gx_default_remap_color, gx_install_CIE,
  96. gx_adjust_cspace_CIEA, gx_no_adjust_color_count
  97. };
  98. /* Determine the concrete space underlying a CIEBased space. */
  99. const gs_color_space *
  100. gx_concrete_space_CIE(const gs_color_space * pcs, const gs_imager_state * pis)
  101. {
  102. const gs_cie_render *pcie = pis->cie_render;
  103. if (pcie == 0 || pcie->RenderTable.lookup.table == 0 ||
  104. pcie->RenderTable.lookup.m == 3
  105. )
  106. return gs_cspace_DeviceRGB(pis);
  107. else /* pcie->RenderTable.lookup.m == 4 */
  108. return gs_cspace_DeviceCMYK(pis);
  109. }
  110. /* Install a CIE space in the graphics state. */
  111. /* We go through an extra level of procedure so that */
  112. /* interpreters can substitute their own installer. */
  113. /* This procedure is exported for the benefit of gsicc.c */
  114. int
  115. gx_install_CIE(const gs_color_space * pcs, gs_state * pgs)
  116. {
  117. return (*pcs->params.a->common.install_cspace) (pcs, pgs);
  118. }
  119. /* Adjust reference counts for a CIE color space */
  120. private void
  121. gx_adjust_cspace_CIEDEFG(const gs_color_space * pcs, int delta)
  122. {
  123. rc_adjust_const(pcs->params.defg, delta, "gx_adjust_cspace_CIEDEFG");
  124. }
  125. private void
  126. gx_adjust_cspace_CIEDEF(const gs_color_space * pcs, int delta)
  127. {
  128. rc_adjust_const(pcs->params.def, delta, "gx_adjust_cspace_CIEDEF");
  129. }
  130. private void
  131. gx_adjust_cspace_CIEABC(const gs_color_space * pcs, int delta)
  132. {
  133. rc_adjust_const(pcs->params.abc, delta, "gx_adjust_cspace_CIEABC");
  134. }
  135. private void
  136. gx_adjust_cspace_CIEA(const gs_color_space * pcs, int delta)
  137. {
  138. rc_adjust_const(pcs->params.a, delta, "gx_adjust_cspace_CIEA");
  139. }
  140. /* ---------------- Procedures ---------------- */
  141. /* ------ Internal initializers ------ */
  142. /*
  143. * Set up the default values for the CIE parameters that are common to
  144. * all CIE color spaces.
  145. *
  146. * There is no default for the white point, so it is set equal to the
  147. * black point. If anyone actually uses the color space in that form,
  148. * the results are likely to be unsatisfactory.
  149. *
  150. * This procedure is exported for the benefit of gsicc.c.
  151. */
  152. void
  153. gx_set_common_cie_defaults(gs_cie_common * pcommon, void *client_data)
  154. {
  155. pcommon->RangeLMN = Range3_default;
  156. pcommon->DecodeLMN = DecodeLMN_default;
  157. pcommon->MatrixLMN = Matrix3_default;
  158. pcommon->points.WhitePoint = BlackPoint_default;
  159. pcommon->points.BlackPoint = BlackPoint_default;
  160. pcommon->client_data = client_data;
  161. }
  162. /*
  163. * Set defaults for a CIEBasedABC color space. This is also used for
  164. * CIEBasedDEF and CIEBasedDEFG color spaces.
  165. */
  166. private void
  167. set_cie_abc_defaults(gs_cie_abc * pabc, void *client_data)
  168. {
  169. gx_set_common_cie_defaults(&pabc->common, client_data);
  170. pabc->RangeABC = Range3_default;
  171. pabc->DecodeABC = DecodeABC_default;
  172. pabc->MatrixABC = Matrix3_default;
  173. }
  174. /*
  175. * Set up a default color lookup table for a CIEBasedDEF[G] space. There is
  176. * no specified default for this structure, so the values used here (aside
  177. * from the input and output component numbers) are intended only to make
  178. * the system fail in a predictable manner.
  179. */
  180. private void
  181. set_ctbl_defaults(gx_color_lookup_table * plktblp, int num_comps)
  182. {
  183. int i;
  184. plktblp->n = num_comps;
  185. plktblp->m = 3; /* always output CIE ABC */
  186. for (i = 0; i < countof(plktblp->dims); i++)
  187. plktblp->dims[i] = 0;
  188. plktblp->table = 0;
  189. }
  190. /*
  191. * Allocate a color space and its parameter structure.
  192. * Return 0 if VMerror, otherwise the parameter structure.
  193. *
  194. * This is exported for the benefit of gsicc.c
  195. */
  196. void *
  197. gx_build_cie_space(gs_color_space ** ppcspace,
  198. const gs_color_space_type * pcstype,
  199. gs_memory_type_ptr_t stype, gs_memory_t * pmem)
  200. {
  201. gs_color_space *pcspace;
  202. int code = gs_cspace_alloc(&pcspace, pcstype, pmem);
  203. gs_cie_common_elements_t *pdata;
  204. if (code < 0)
  205. return 0;
  206. rc_alloc_struct_1(pdata, gs_cie_common_elements_t, stype, pmem,
  207. {
  208. gs_free_object(pmem, pcspace, "gx_build_cie_space");
  209. return 0;
  210. }
  211. ,
  212. "gx_build_cie_space(data)");
  213. *ppcspace = pcspace;
  214. return (void *)pdata;
  215. }
  216. /* ------ Constructors ------ */
  217. int
  218. gs_cspace_build_CIEA(gs_color_space ** ppcspace, void *client_data,
  219. gs_memory_t * pmem)
  220. {
  221. gs_cie_a *pciea =
  222. gx_build_cie_space(ppcspace, &gs_color_space_type_CIEA, &st_cie_a, pmem);
  223. if (pciea == 0)
  224. return_error(gs_error_VMerror);
  225. gx_set_common_cie_defaults(&pciea->common, client_data);
  226. pciea->common.install_cspace = gx_install_CIEA;
  227. pciea->RangeA = RangeA_default;
  228. pciea->DecodeA = DecodeA_default;
  229. pciea->MatrixA = MatrixA_default;
  230. (*ppcspace)->params.a = pciea;
  231. return 0;
  232. }
  233. int
  234. gs_cspace_build_CIEABC(gs_color_space ** ppcspace, void *client_data,
  235. gs_memory_t * pmem)
  236. {
  237. gs_cie_abc *pabc =
  238. gx_build_cie_space(ppcspace, &gs_color_space_type_CIEABC, &st_cie_abc,
  239. pmem);
  240. if (pabc == 0)
  241. return_error(gs_error_VMerror);
  242. set_cie_abc_defaults(pabc, client_data);
  243. pabc->common.install_cspace = gx_install_CIEABC;
  244. (*ppcspace)->params.abc = pabc;
  245. return 0;
  246. }
  247. int
  248. gs_cspace_build_CIEDEF(gs_color_space ** ppcspace, void *client_data,
  249. gs_memory_t * pmem)
  250. {
  251. gs_cie_def *pdef =
  252. gx_build_cie_space(ppcspace, &gs_color_space_type_CIEDEF, &st_cie_def,
  253. pmem);
  254. if (pdef == 0)
  255. return_error(gs_error_VMerror);
  256. set_cie_abc_defaults((gs_cie_abc *) pdef, client_data);
  257. pdef->common.install_cspace = gx_install_CIEDEF;
  258. pdef->RangeDEF = Range3_default;
  259. pdef->DecodeDEF = DecodeDEF_default;
  260. pdef->RangeHIJ = Range3_default;
  261. set_ctbl_defaults(&pdef->Table, 3);
  262. (*ppcspace)->params.def = pdef;
  263. return 0;
  264. }
  265. int
  266. gs_cspace_build_CIEDEFG(gs_color_space ** ppcspace, void *client_data,
  267. gs_memory_t * pmem)
  268. {
  269. gs_cie_defg *pdefg =
  270. gx_build_cie_space(ppcspace, &gs_color_space_type_CIEDEFG, &st_cie_defg,
  271. pmem);
  272. if (pdefg == 0)
  273. return_error(gs_error_VMerror);
  274. set_cie_abc_defaults((gs_cie_abc *) pdefg, client_data);
  275. pdefg->common.install_cspace = gx_install_CIEDEFG;
  276. pdefg->RangeDEFG = Range4_default;
  277. pdefg->DecodeDEFG = DecodeDEFG_default;
  278. pdefg->RangeHIJK = Range4_default;
  279. set_ctbl_defaults(&pdefg->Table, 4);
  280. (*ppcspace)->params.defg = pdefg;
  281. return 0;
  282. }
  283. /* ------ Accessors ------ */
  284. int
  285. gs_cie_defx_set_lookup_table(gs_color_space * pcspace, int *pdims,
  286. const gs_const_string * ptable)
  287. {
  288. gx_color_lookup_table *plktblp;
  289. switch (gs_color_space_get_index(pcspace)) {
  290. case gs_color_space_index_CIEDEF:
  291. plktblp = &pcspace->params.def->Table;
  292. break;
  293. case gs_color_space_index_CIEDEFG:
  294. plktblp = &pcspace->params.defg->Table;
  295. plktblp->dims[3] = pdims[3];
  296. break;
  297. default:
  298. return_error(gs_error_rangecheck);
  299. }
  300. plktblp->dims[0] = pdims[0];
  301. plktblp->dims[1] = pdims[1];
  302. plktblp->dims[2] = pdims[2];
  303. plktblp->table = ptable;
  304. return 0;
  305. }