]> git.ipfire.org Git - thirdparty/cups.git/blob - pstoraster/gscscie.c
Import cups.org releases
[thirdparty/cups.git] / pstoraster / gscscie.c
1 /* Copyright (C) 1998 Aladdin Enterprises. All rights reserved.
2
3 This file is part of GNU Ghostscript.
4
5 GNU Ghostscript is distributed in the hope that it will be useful, but
6 WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
7 to anyone for the consequences of using it or for whether it serves any
8 particular purpose or works at all, unless he says so in writing. Refer
9 to the GNU General Public License for full details.
10
11 Everyone is granted permission to copy, modify and redistribute GNU
12 Ghostscript, but only under the conditions described in the GNU General
13 Public License. A copy of this license is supposed to have been given
14 to you along with GNU Ghostscript so you can know your rights and
15 responsibilities. It should be in a file named COPYING. Among other
16 things, the copyright notice and this notice must be preserved on all
17 copies.
18
19 Aladdin Enterprises supports the work of the GNU Project, but is not
20 affiliated with the Free Software Foundation or the GNU Project. GNU
21 Ghostscript, as distributed by Aladdin Enterprises, does not require any
22 GNU software to build or run it.
23 */
24
25 /*$Id$ */
26 /* CIE color space management */
27 #include "math_.h"
28 #include "gx.h"
29 #include "gserrors.h"
30 #include "gsstruct.h"
31 #include "gsmatrix.h" /* for gscolor2.h */
32 #include "gxcspace.h"
33 #include "gscolor2.h" /* for gs_set/currentcolorrendering */
34 #include "gscie.h"
35 #include "gxarith.h"
36 #include "gxdevice.h" /* for gxcmap.h */
37 #include "gxcmap.h"
38 #include "gzstate.h"
39
40 /* ---------------- Color space definition ---------------- */
41
42 /* GC descriptors */
43 private_st_cie_common();
44 private_st_cie_common_elements();
45 private_st_cie_a();
46 private_st_cie_abc();
47 private_st_cie_def();
48 private_st_cie_defg();
49
50 /* Define the CIE color space types. */
51 /* We use CIExxx rather than CIEBasedxxx in some places because */
52 /* gcc under VMS only retains 23 characters of procedure names, */
53 /* and DEC C truncates all identifiers at 31 characters. */
54 extern cs_proc_init_color(gx_init_CIE);
55 private cs_proc_concrete_space(gx_concrete_space_CIE);
56 private cs_proc_install_cspace(gx_install_CIE);
57
58 /* CIEBasedDEFG */
59 gs_private_st_ptrs1(st_color_space_CIEDEFG, gs_base_color_space,
60 "gs_color_space(CIEDEFG)", cs_CIEDEFG_enum_ptrs, cs_CIEDEFG_reloc_ptrs,
61 params.defg);
62 extern cs_proc_restrict_color(gx_restrict_CIEDEFG);
63 extern cs_proc_concretize_color(gx_concretize_CIEDEFG);
64 extern cs_proc_install_cspace(gx_install_CIEDEFG);
65 private cs_proc_adjust_cspace_count(gx_adjust_cspace_CIEDEFG);
66 const gs_color_space_type gs_color_space_type_CIEDEFG = {
67 gs_color_space_index_CIEDEFG, true, true,
68 &st_color_space_CIEDEFG, gx_num_components_4,
69 gx_no_base_space,
70 gx_init_CIE, gx_restrict_CIEDEFG,
71 gx_concrete_space_CIE,
72 gx_concretize_CIEDEFG, NULL,
73 gx_default_remap_color, gx_install_CIE,
74 gx_adjust_cspace_CIEDEFG, gx_no_adjust_color_count
75 };
76
77 /* CIEBasedDEF */
78 gs_private_st_ptrs1(st_color_space_CIEDEF, gs_base_color_space,
79 "gs_color_space(CIEDEF)", cs_CIEDEF_enum_ptrs, cs_CIEDEF_reloc_ptrs,
80 params.def);
81 extern cs_proc_restrict_color(gx_restrict_CIEDEF);
82 extern cs_proc_concretize_color(gx_concretize_CIEDEF);
83 extern cs_proc_install_cspace(gx_install_CIEDEF);
84 private cs_proc_adjust_cspace_count(gx_adjust_cspace_CIEDEF);
85 const gs_color_space_type gs_color_space_type_CIEDEF = {
86 gs_color_space_index_CIEDEF, true, true,
87 &st_color_space_CIEDEF, gx_num_components_3,
88 gx_no_base_space,
89 gx_init_CIE, gx_restrict_CIEDEF,
90 gx_concrete_space_CIE,
91 gx_concretize_CIEDEF, NULL,
92 gx_default_remap_color, gx_install_CIE,
93 gx_adjust_cspace_CIEDEF, gx_no_adjust_color_count
94 };
95
96 /* CIEBasedABC */
97 gs_private_st_ptrs1(st_color_space_CIEABC, gs_base_color_space,
98 "gs_color_space(CIEABC)", cs_CIEABC_enum_ptrs, cs_CIEABC_reloc_ptrs,
99 params.abc);
100 cs_proc_restrict_color(gx_restrict_CIEABC);
101 cs_proc_concretize_color(gx_concretize_CIEABC);
102 cs_proc_install_cspace(gx_install_CIEABC);
103 private cs_proc_adjust_cspace_count(gx_adjust_cspace_CIEABC);
104 extern cs_proc_remap_color(gx_remap_CIEABC);
105 const gs_color_space_type gs_color_space_type_CIEABC = {
106 gs_color_space_index_CIEABC, true, true,
107 &st_color_space_CIEABC, gx_num_components_3,
108 gx_no_base_space,
109 gx_init_CIE, gx_restrict_CIEABC,
110 gx_concrete_space_CIE,
111 gx_concretize_CIEABC, NULL,
112 gx_remap_CIEABC, gx_install_CIE,
113 gx_adjust_cspace_CIEABC, gx_no_adjust_color_count
114 };
115
116 /* CIEBasedA */
117 gs_private_st_ptrs1(st_color_space_CIEA, gs_base_color_space,
118 "gs_color_space(CIEA)", cs_CIEA_enum_ptrs, cs_CIEA_reloc_ptrs,
119 params.a);
120 cs_proc_restrict_color(gx_restrict_CIEA);
121 cs_proc_concretize_color(gx_concretize_CIEA);
122 cs_proc_install_cspace(gx_install_CIEA);
123 private cs_proc_adjust_cspace_count(gx_adjust_cspace_CIEA);
124 const gs_color_space_type gs_color_space_type_CIEA = {
125 gs_color_space_index_CIEA, true, true,
126 &st_color_space_CIEA, gx_num_components_1,
127 gx_no_base_space,
128 gx_init_CIE, gx_restrict_CIEA,
129 gx_concrete_space_CIE,
130 gx_concretize_CIEA, NULL,
131 gx_default_remap_color, gx_install_CIE,
132 gx_adjust_cspace_CIEA, gx_no_adjust_color_count
133 };
134
135 /* Determine the concrete space underlying a CIEBased space. */
136 private const gs_color_space *
137 gx_concrete_space_CIE(const gs_color_space * pcs, const gs_imager_state * pis)
138 {
139 const gs_cie_render *pcie = pis->cie_render;
140
141 if (pcie == 0 || pcie->RenderTable.lookup.table == 0 ||
142 pcie->RenderTable.lookup.m == 3
143 )
144 return gs_cspace_DeviceRGB(pis);
145 else /* pcie->RenderTable.lookup.m == 4 */
146 return gs_cspace_DeviceCMYK(pis);
147 }
148
149 /* Install a CIE space in the graphics state. */
150 /* We go through an extra level of procedure so that */
151 /* interpreters can substitute their own installer. */
152 private int
153 gx_install_CIE(gs_color_space * pcs, gs_state * pgs)
154 {
155 return (*pcs->params.a->common.install_cspace) (pcs, pgs);
156 }
157
158 /* Adjust reference counts for a CIE color space */
159 private void
160 gx_adjust_cspace_CIEDEFG(const gs_color_space * pcs, int delta)
161 {
162 rc_adjust_const(pcs->params.defg, delta, "gx_adjust_cspace_CIEDEFG");
163 }
164
165 private void
166 gx_adjust_cspace_CIEDEF(const gs_color_space * pcs, int delta)
167 {
168 rc_adjust_const(pcs->params.def, delta, "gx_adjust_cspace_CIEDEF");
169 }
170
171 private void
172 gx_adjust_cspace_CIEABC(const gs_color_space * pcs, int delta)
173 {
174 rc_adjust_const(pcs->params.abc, delta, "gx_adjust_cspace_CIEABC");
175 }
176
177 private void
178 gx_adjust_cspace_CIEA(const gs_color_space * pcs, int delta)
179 {
180 rc_adjust_const(pcs->params.a, delta, "gx_adjust_cspace_CIEA");
181 }
182
183 /* ---------------- Procedures ---------------- */
184
185 /* ------ Internal initializers ------ */
186
187 /*
188 * Set up the default values for the CIE parameters that are common to
189 * all CIE color spaces.
190 *
191 * There is no default for the white point, so it is set equal to the
192 * black point. If anyone actually uses the color space in that form,
193 * the results are likely to be unsatisfactory.
194 */
195 private void
196 set_common_cie_defaults(gs_cie_common * pcommon, void *client_data)
197 {
198 pcommon->RangeLMN = Range3_default;
199 pcommon->DecodeLMN = DecodeLMN_default;
200 pcommon->MatrixLMN = Matrix3_default;
201 pcommon->points.WhitePoint = BlackPoint_default;
202 pcommon->points.BlackPoint = BlackPoint_default;
203 pcommon->client_data = client_data;
204 }
205
206 /*
207 * Set defaults for a CIEBasedABC color space. This is also used for
208 * CIEBasedDEF and CIEBasedDEFG color spaces.
209 */
210 private void
211 set_cie_abc_defaults(gs_cie_abc * pabc, void *client_data)
212 {
213 set_common_cie_defaults(&pabc->common, client_data);
214 pabc->RangeABC = Range3_default;
215 pabc->DecodeABC = DecodeABC_default;
216 pabc->MatrixABC = Matrix3_default;
217 }
218
219 /*
220 * Set up a default color lookup table for a CIEBasedDEF[G] space. There is
221 * no specified default for this structure, so the values used here (aside
222 * from the input and output component numbers) are intended only to make
223 * the system fail in a predictable manner.
224 */
225 private void
226 set_ctbl_defaults(gx_color_lookup_table * plktblp, int num_comps)
227 {
228 int i;
229
230 plktblp->n = num_comps;
231 plktblp->m = 3; /* always output CIE ABC */
232 for (i = 0; i < countof(plktblp->dims); i++)
233 plktblp->dims[i] = 0;
234 plktblp->table = 0;
235 }
236
237 /*
238 * Allocate a color space and its parameter structure.
239 * Return 0 if VMerror, otherwise the parameter structure.
240 */
241 private void *
242 build_cie_space(gs_color_space ** ppcspace, const gs_color_space_type * pcstype,
243 gs_memory_type_ptr_t stype, gs_memory_t * pmem)
244 {
245 gs_color_space *pcspace =
246 gs_alloc_struct(pmem, gs_color_space, &st_color_space,
247 "build_cie_space");
248 gs_cie_common_elements_t *pdata;
249
250 if (pcspace == 0)
251 return 0;
252 rc_alloc_struct_1(pdata, gs_cie_common_elements_t, stype, pmem,
253 {
254 gs_free_object(pmem, pcspace, "build_cie_space");
255 return 0;
256 }
257 ,
258 "build_cie_space(data)");
259 pcspace->pmem = pmem;
260 pcspace->type = pcstype;
261 *ppcspace = pcspace;
262 return (void *)pdata;
263 }
264
265 /* ------ Constructors ------ */
266
267 int
268 gs_cspace_build_CIEA(gs_color_space ** ppcspace, void *client_data,
269 gs_memory_t * pmem)
270 {
271 gs_cie_a *pciea =
272 build_cie_space(ppcspace, &gs_color_space_type_CIEA, &st_cie_a, pmem);
273
274 if (pciea == 0)
275 return_error(gs_error_VMerror);
276
277 set_common_cie_defaults(&pciea->common, client_data);
278 pciea->common.install_cspace = gx_install_CIEA;
279 pciea->RangeA = RangeA_default;
280 pciea->DecodeA = DecodeA_default;
281 pciea->MatrixA = MatrixA_default;
282
283 (*ppcspace)->params.a = pciea;
284 return 0;
285 }
286
287 int
288 gs_cspace_build_CIEABC(gs_color_space ** ppcspace, void *client_data,
289 gs_memory_t * pmem)
290 {
291 gs_cie_abc *pabc =
292 build_cie_space(ppcspace, &gs_color_space_type_CIEABC, &st_cie_abc,
293 pmem);
294
295 if (pabc == 0)
296 return_error(gs_error_VMerror);
297
298 set_cie_abc_defaults(pabc, client_data);
299 pabc->common.install_cspace = gx_install_CIEABC;
300
301 (*ppcspace)->params.abc = pabc;
302 return 0;
303 }
304
305 int
306 gs_cspace_build_CIEDEF(gs_color_space ** ppcspace, void *client_data,
307 gs_memory_t * pmem)
308 {
309 gs_cie_def *pdef =
310 build_cie_space(ppcspace, &gs_color_space_type_CIEDEF, &st_cie_def,
311 pmem);
312
313 if (pdef == 0)
314 return_error(gs_error_VMerror);
315
316 set_cie_abc_defaults((gs_cie_abc *) pdef, client_data);
317 pdef->common.install_cspace = gx_install_CIEDEF;
318 pdef->RangeDEF = Range3_default;
319 pdef->DecodeDEF = DecodeDEF_default;
320 pdef->RangeHIJ = Range3_default;
321 set_ctbl_defaults(&pdef->Table, 3);
322
323 (*ppcspace)->params.def = pdef;
324 return 0;
325 }
326
327 int
328 gs_cspace_build_CIEDEFG(gs_color_space ** ppcspace, void *client_data,
329 gs_memory_t * pmem)
330 {
331 gs_cie_defg *pdefg =
332 build_cie_space(ppcspace, &gs_color_space_type_CIEDEFG, &st_cie_defg,
333 pmem);
334
335 if (pdefg == 0)
336 return_error(gs_error_VMerror);
337
338 set_cie_abc_defaults((gs_cie_abc *) pdefg, client_data);
339 pdefg->common.install_cspace = gx_install_CIEDEFG;
340 pdefg->RangeDEFG = Range4_default;
341 pdefg->DecodeDEFG = DecodeDEFG_default;
342 pdefg->RangeHIJK = Range4_default;
343 set_ctbl_defaults(&pdefg->Table, 4);
344
345 (*ppcspace)->params.defg = pdefg;
346 return 0;
347 }
348
349 /* ------ Accessors ------ */
350
351 int
352 gs_cie_defx_set_lookup_table(gs_color_space * pcspace, int *pdims,
353 const gs_const_string * ptable)
354 {
355 gx_color_lookup_table *plktblp;
356
357 switch (gs_color_space_get_index(pcspace)) {
358 case gs_color_space_index_CIEDEF:
359 plktblp = &pcspace->params.def->Table;
360 break;
361 case gs_color_space_index_CIEDEFG:
362 plktblp = &pcspace->params.defg->Table;
363 plktblp->dims[3] = pdims[3];
364 break;
365 default:
366 return_error(gs_error_rangecheck);
367 }
368
369 plktblp->dims[0] = pdims[0];
370 plktblp->dims[1] = pdims[1];
371 plktblp->dims[2] = pdims[2];
372 plktblp->table = ptable;
373 return 0;
374 }