]> git.ipfire.org Git - thirdparty/cups.git/blob - pstoraster/gscsepr.c
Import cups.org releases
[thirdparty/cups.git] / pstoraster / gscsepr.c
1 /* Copyright (C) 1994, 1996, 1997, 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 /* Separation color space and operation definition */
27 #include "gx.h"
28 #include "gserrors.h"
29 #include "gsrefct.h"
30 #include "gsmatrix.h" /* for gscolor2.h */
31 #include "gscsepr.h"
32 #include "gxcspace.h"
33 #include "gxfixed.h" /* for gxcolor2.h */
34 #include "gxcolor2.h" /* for gs_indexed_map */
35 #include "gzstate.h" /* for pgs->overprint */
36
37 /* ---------------- Color space ---------------- */
38
39 gs_private_st_composite(st_color_space_Separation, gs_paint_color_space,
40 "gs_color_space_Separation",
41 cs_Separation_enum_ptrs, cs_Separation_reloc_ptrs);
42
43 /* Define the Separation color space type. */
44 private cs_proc_base_space(gx_alt_space_Separation);
45 private cs_proc_init_color(gx_init_Separation);
46 private cs_proc_concrete_space(gx_concrete_space_Separation);
47 private cs_proc_concretize_color(gx_concretize_Separation);
48 private cs_proc_remap_concrete_color(gx_remap_concrete_Separation);
49 private cs_proc_install_cspace(gx_install_Separation);
50 private cs_proc_adjust_cspace_count(gx_adjust_cspace_Separation);
51 const gs_color_space_type gs_color_space_type_Separation = {
52 gs_color_space_index_Separation, true, false,
53 &st_color_space_Separation, gx_num_components_1,
54 gx_alt_space_Separation,
55 gx_init_Separation, gx_restrict01_paint_1,
56 gx_concrete_space_Separation,
57 gx_concretize_Separation, gx_remap_concrete_Separation,
58 gx_default_remap_color, gx_install_Separation,
59 gx_adjust_cspace_Separation, gx_no_adjust_color_count
60 };
61
62 /* GC procedures */
63
64 #define pcs ((gs_color_space *)vptr)
65
66 private
67 ENUM_PTRS_BEGIN(cs_Separation_enum_ptrs)
68 {
69 return ENUM_USING(*pcs->params.separation.alt_space.type->stype,
70 &pcs->params.separation.alt_space,
71 sizeof(pcs->params.separation.alt_space), index - 1);
72 }
73 ENUM_PTR(0, gs_color_space, params.separation.map);
74 ENUM_PTRS_END
75 private RELOC_PTRS_BEGIN(cs_Separation_reloc_ptrs)
76 {
77 RELOC_PTR(gs_color_space, params.separation.map);
78 RELOC_USING(*pcs->params.separation.alt_space.type->stype,
79 &pcs->params.separation.alt_space,
80 sizeof(gs_base_color_space));
81 }
82 RELOC_PTRS_END
83
84 #undef pcs
85
86 /* Get the alternate space for a Separation space. */
87 private const gs_color_space *
88 gx_alt_space_Separation(const gs_color_space * pcs)
89 {
90 return (const gs_color_space *)&(pcs->params.separation.alt_space);
91 }
92
93 /* Get the concrete space for a Separation space. */
94 /* (We don't support concrete Separation spaces yet.) */
95 private const gs_color_space *
96 gx_concrete_space_Separation(const gs_color_space * pcs,
97 const gs_imager_state * pis)
98 {
99 const gs_color_space *pacs =
100 (const gs_color_space *)&pcs->params.separation.alt_space;
101
102 return cs_concrete_space(pacs, pis);
103 }
104
105 /* Install a Separation color space. */
106 private int
107 gx_install_Separation(gs_color_space * pcs, gs_state * pgs)
108 {
109 return (*pcs->params.separation.alt_space.type->install_cspace)
110 ((gs_color_space *) & pcs->params.separation.alt_space, pgs);
111 }
112
113 /* Adjust the reference count of a Separation color space. */
114 private void
115 gx_adjust_cspace_Separation(const gs_color_space * pcs, int delta)
116 {
117 rc_adjust_const(pcs->params.separation.map, delta,
118 "gx_adjust_Separation");
119 (*pcs->params.separation.alt_space.type->adjust_cspace_count)
120 ((const gs_color_space *)&pcs->params.separation.alt_space, delta);
121 }
122
123 /* ------ Constructors/accessors ------ */
124
125 /*
126 * The default separation tint transformation function. This will just return
127 * the information in the cache or, if the cache is of zero size, set all
128 * components in the alternative color space to 0.
129 *
130 * No special cases are provided for this routine, as the use of separations
131 * (particular in this form) is sufficiently rare to not have a significant
132 * performance impact.
133 */
134 private int
135 map_tint_value(const gs_separation_params * pcssepr, floatp in_val,
136 float *out_vals)
137 {
138 int ncomps =
139 cs_num_components((const gs_color_space *)&pcssepr->alt_space);
140 int nentries = pcssepr->map->num_values / ncomps;
141 int indx;
142 const float *pv = pcssepr->map->values;
143 int i;
144
145 if (nentries == 0) {
146 for (i = 0; i < ncomps; i++)
147 out_vals[i] = 0.0;
148 return 0;
149 }
150 if (in_val > 1)
151 indx = nentries - 1;
152 else if (in_val <= 0)
153 indx = 0;
154 else
155 indx = (int)(in_val * nentries + 0.5);
156 pv += indx * ncomps;
157
158 for (i = 0; i < ncomps; i++)
159 out_vals[i] = pv[i];
160 return 0;
161 }
162
163 /*
164 * Allocate the indexed map required by a separation color space.
165 */
166 private gs_indexed_map *
167 alloc_separation_map(const gs_color_space * palt_cspace, int cache_size,
168 gs_memory_t * pmem)
169 {
170 gs_indexed_map *pimap;
171
172 rc_alloc_struct_1(pimap, gs_indexed_map, &st_indexed_map, pmem,
173 return 0,
174 "gs_cspace_build_Separation"
175 );
176 pimap->rc.free = free_indexed_map;
177 pimap->proc.tint_transform = map_tint_value;
178
179 if (cache_size != 0) {
180 int num_comps = gs_color_space_num_components(palt_cspace);
181
182 cache_size *= num_comps;
183 pimap->num_values = cache_size;
184 pimap->values =
185 (float *)gs_alloc_byte_array(pmem, cache_size, sizeof(float),
186 "gs_cspace_build_Separation"
187 );
188
189 if (pimap->values == 0)
190 rc_decrement(pimap, "gs_cspace_build_Separation"); /* sets pimap = 0 */
191
192 } else {
193 pimap->num_values = 0;
194 pimap->values = 0;
195 }
196 return pimap;
197 }
198
199 /*
200 * Build a separation color space.
201 *
202 * The values array provided with separation color spaces is actually cached
203 * information, but filled in by the client. The alternative space is the
204 * color space in which the tint procedure will provide alternative colors.
205 */
206 int
207 gs_cspace_build_Separation(
208 gs_color_space ** ppcspace,
209 gs_separation_name sname,
210 const gs_color_space * palt_cspace,
211 int cache_size,
212 gs_memory_t * pmem
213 )
214 {
215 gs_color_space *pcspace = 0;
216 gs_separation_params *pcssepr = 0;
217 int code;
218
219 if (palt_cspace == 0 || !palt_cspace->type->can_be_alt_space)
220 return_error(gs_error_rangecheck);
221
222 code = gs_cspace_alloc(&pcspace, &gs_color_space_type_Separation, pmem);
223 if (code < 0)
224 return code;
225 pcssepr = &pcspace->params.separation;
226 pcssepr->map = alloc_separation_map(palt_cspace, cache_size, pmem);
227 if (pcssepr->map == 0) {
228 gs_free_object(pmem, pcspace, "gs_cspace_build_Separation");
229 return_error(gs_error_VMerror);
230 }
231 pcssepr->sname = sname;
232 gs_cspace_init_from((gs_color_space *) & pcssepr->alt_space, palt_cspace);
233 *ppcspace = pcspace;
234 return 0;
235 }
236
237 /*
238 * Get the cached value array for a separation color space. This will return
239 * a null pointer if the color space is not a separation color space, or if
240 * the separation color space has a cache size of 0.
241 */
242 float *
243 gs_cspace_get_separation_value_array(const gs_color_space * pcspace)
244 {
245 if (gs_color_space_get_index(pcspace) != gs_color_space_index_Separation)
246 return 0;
247 return pcspace->params.separation.map->values;
248 }
249
250 /*
251 * Set the tint transformation procedure used by a Separation color space.
252 */
253 int
254 gs_cspace_set_tint_transform_proc(gs_color_space * pcspace,
255 int (*proc) (P3(const gs_separation_params *, floatp, float *)))
256 {
257 if (gs_color_space_get_index(pcspace) != gs_color_space_index_Separation)
258 return_error(gs_error_rangecheck);
259 pcspace->params.separation.map->proc.tint_transform = proc;
260 return 0;
261 }
262
263 /* ---------------- Graphics state ---------------- */
264
265 /* setoverprint */
266 void
267 gs_setoverprint(gs_state * pgs, bool ovp)
268 {
269 pgs->overprint = ovp;
270 }
271
272 /* currentoverprint */
273 bool
274 gs_currentoverprint(const gs_state * pgs)
275 {
276 return pgs->overprint;
277 }
278
279 /* ------ Internal procedures ------ */
280
281 /* Initialize a Separation color. */
282
283 private void
284 gx_init_Separation(gs_client_color * pcc, const gs_color_space * pcs)
285 {
286 pcc->paint.values[0] = 1.0;
287 }
288
289 /* Remap a Separation color. */
290
291 private int
292 gx_concretize_Separation(const gs_client_color * pc, const gs_color_space * pcs,
293 frac * pconc, const gs_imager_state * pis)
294 {
295 float tint = pc->paint.values[0];
296 int code;
297 gs_client_color cc;
298 const gs_color_space *pacs =
299 (const gs_color_space *)&pcs->params.separation.alt_space;
300
301 if (tint < 0)
302 tint = 0;
303 else if (tint > 1)
304 tint = 1;
305 /* We always map into the alternate color space. */
306 code = (*pcs->params.separation.map->proc.tint_transform) (&pcs->params.separation, tint, &cc.paint.values[0]);
307 if (code < 0)
308 return code;
309 return (*pacs->type->concretize_color) (&cc, pacs, pconc, pis);
310 }
311
312 private int
313 gx_remap_concrete_Separation(const frac * pconc,
314 gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
315 gs_color_select_t select)
316 { /* We don't support concrete Separation colors yet. */
317 return_error(gs_error_rangecheck);
318 }
319
320 /* ---------------- Notes on real Separation colors ---------------- */
321
322 typedef ulong gs_separation; /* BOGUS */
323
324 #define gs_no_separation ((gs_separation)(-1L))
325
326 #define dev_proc_lookup_separation(proc)\
327 gs_separation proc(P4(gx_device *dev, const byte *sname, uint len,\
328 gx_color_value *num_levels))
329
330 #define dev_proc_map_tint_color(proc)\
331 gx_color_index proc(P4(gx_device *dev, gs_separation sepr, bool overprint,\
332 gx_color_value tint))
333
334 /*
335 * In principle, setting a Separation color space, or setting the device
336 * when the current color space is a Separation space, calls the
337 * lookup_separation device procedure to obtain the separation ID and
338 * the number of achievable levels. Currently, the only hooks for doing
339 * this are unsuitable: gx_set_cmap_procs isn't called when the color
340 * space changes, and doing it in gx_remap_Separation is inefficient.
341 * Probably the best approach is to call gx_set_cmap_procs whenever the
342 * color space changes. In fact, if we do this, we can probably short-cut
343 * two levels of procedure call in color remapping (gx_remap_color, by
344 * turning it into a macro, and gx_remap_DeviceXXX, by calling the
345 * cmap_proc procedure directly). Some care will be required for the
346 * implicit temporary resetting of the color space in [color]image.
347 *
348 * For actual remapping of Separation colors, we need cmap_separation_direct
349 * and cmap_separation_halftoned, just as for the other device color spaces.
350 * So we need to break apart gx_render_gray in gxdither.c so it can also
351 * do the job for separations.
352 */