1 /* Copyright (C) 2001-2006 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied, modified
8    or distributed except as expressly authorized under the terms of that
9    license.  Refer to licensing information at http://www.artifex.com/
10    or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
11    San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12 */
13 /* $Id: gxblend.h 9666 2009-04-20 19:16:24Z mvrhel $ */
14 /* PDF 1.4 blending functions */
15 
16 #ifndef gxblend_INCLUDED
17 #  define gxblend_INCLUDED
18 
19 #include "gxcindex.h"
20 #include "gxcvalue.h"
21 #include "gxfrac.h"
22 #include "gxdevcli.h"
23 
24 #define RAW_DUMP 0
25 
26 /* #define DUMP_TO_PNG */
27 
28 #define	PDF14_MAX_PLANES GX_DEVICE_COLOR_MAX_COMPONENTS+3  /* Needed for alpha channel, shape, group alpha */
29 
30 typedef bits16 ArtPixMaxDepth;
31 
32 #define ART_MAX_CHAN GX_DEVICE_COLOR_MAX_COMPONENTS
33 
34 #ifndef pdf14_device_DEFINED
35 #  define pdf14_device_DEFINED
36 typedef struct pdf14_device_s pdf14_device;
37 #endif
38 
39 #ifndef pdf14_buf_DEFINED
40 #  define pdf14_buf_DEFINED
41 typedef struct pdf14_buf_s pdf14_buf;
42 #endif
43 
44 #ifndef gx_device_DEFINED
45 #  define gx_device_DEFINED
46 typedef struct gx_device_s gx_device;
47 #endif
48 
49 #ifndef gs_separations_DEFINED
50 #   define gs_separations_DEFINED
51     typedef struct gs_separations_s gs_separations;
52 #endif
53 
54 /*
55  * This structure contains procedures for processing which differ
56  * between the different blending color spaces.
57  *
58  * The Luminosity, Color, Saturation, and Hue blend modes depend
59  * upon the blending color space.  Currently the blending color space
60  * matches the process color model of the compositing device.  We need
61  * two routines to implement the four 'non separable' blend modes.
62  */
63 typedef struct {
64     /*
65      * Perform luminosity and color blending.  (Also used for hue blending.)
66      */
67     void (* blend_luminosity)(int n_chan, byte *dst,
68 		    const byte *backdrop, const byte *src);
69     /*
70      * Perform saturation blending.  (Also used for hue blending.)
71      */
72     void (* blend_saturation)(int n_chan, byte *dst,
73 		    const byte *backdrop, const byte *src);
74 } pdf14_nonseparable_blending_procs_s;
75 
76 typedef pdf14_nonseparable_blending_procs_s
77 		pdf14_nonseparable_blending_procs_t;
78 
79 
80 /* This is used to so that we can change procedures based
81  * upon the Smask color space. previously we always
82  *  went to the device space */
83 
84 typedef struct {
85 
86     pdf14_nonseparable_blending_procs_t device_procs;
87     gx_device_procs color_mapping_procs;
88 
89 } pdf14_parent_cs_params_s;
90 
91 typedef pdf14_parent_cs_params_s pdf14_parent_cs_params_t;
92 
93 /* This function is used for mapping Smask CMYK or RGB data to a monochrome alpha buffer */
94 
95 void Smask_Luminosity_Mapping(int num_rows, int num_cols, int n_chan, int row_stride,
96                          int plane_stride, byte *dst, const byte *src, bool isadditive,
97                             bool SMask_is_CIE, gs_transparency_mask_subtype_t SMask_SubType);
98 
99 
100 /**
101  * art_blend_pixel: Compute PDF 1.4 blending function.
102  * @dst: Where to store resulting pixel.
103  * @backdrop: Backdrop pixel color.
104  * @src: Source pixel color.
105  * @n_chan: Number of channels.
106  * @blend_mode: Blend mode.
107  *
108  * Computes the blend of two pixels according the PDF 1.4 transparency
109  * spec (section 3.2, Blend Mode). A few things to keep in mind about
110  * this implementation:
111  *
112  * 1. This is a reference implementation, not a high-performance one.
113  * Blending using this function will incur a function call and switch
114  * statement per pixel, and will also incur the extra cost of 16 bit
115  * math.
116  *
117  * 2. Zero is black, one is white. In a subtractive color space such
118  * as CMYK, all pixels should be represented as "complemented", as
119  * described in section 3.1 (Blending Color Space) of the PDF 1.4
120  * transparency spec.
121  *
122  * 3. I haven't really figured out how to handle the Compatible blend
123  * mode. I wouldn't be surprised if it required an API change.
124  **/
125 void
126 art_blend_pixel(ArtPixMaxDepth * dst, const ArtPixMaxDepth * backdrop,
127 		const ArtPixMaxDepth * src, int n_chan,
128 		gs_blend_mode_t blend_mode);
129 
130 /**
131  * art_blend_pixel_8: Compute PDF 1.4 blending function on 8-bit pixels.
132  * @dst: Where to store resulting pixel.
133  * @backdrop: Backdrop pixel color.
134  * @src: Source pixel color.
135  * @n_chan: Number of channels.
136  * @blend_mode: Blend mode.
137  * @pblend_procs: Procs for handling non separable blending modes.
138  *
139  * Computes the blend of two pixels according the PDF 1.4 transparency
140  * spec (section 3.2, Blend Mode). A few things to keep in mind about
141  * this implementation:
142  *
143  * 1. This is a reference implementation, not a high-performance one.
144  * Blending using this function will incur a function call and switch
145  * statement per pixel, and will also incur the extra cost of 16 bit
146  * math.
147  *
148  * 2. Zero is black, one is white. In a subtractive color space such
149  * as CMYK, all pixels should be represented as "complemented", as
150  * described in section 3.1 (Blending Color Space) of the PDF 1.4
151  * transparency spec.
152  *
153  * 3. I haven't really figured out how to handle the Compatible blend
154  * mode. I wouldn't be surprised if it required an API change.
155  **/
156 void
157 art_blend_pixel_8(byte *dst, const byte *backdrop,
158 		const byte *src, int n_chan, gs_blend_mode_t blend_mode,
159        		const pdf14_nonseparable_blending_procs_t * pblend_procs);
160 
161 /**
162  * art_pdf_union_8: Union together two alpha values.
163  * @alpha1: One alpha value.
164  * @alpha2: Another alpha value.
165  *
166  * Return value: Union (@alpha1, @alpha2).
167  **/
168 byte art_pdf_union_8(byte alpha1, byte alpha2);
169 
170 /**
171  * art_pdf_union_mul_8: Union together two alpha values, with mask.
172  * @alpha1: One alpha value.
173  * @alpha2: Another alpha value.
174  * @alpha_mask: A mask alpha value;
175  *
176  * Return value: Union (@alpha1, @alpha2 * @alpha_mask).
177  **/
178 byte art_pdf_union_mul_8(byte alpha1, byte alpha2, byte alpha_mask);
179 
180 /**
181  * art_pdf_composite_pixel_alpha_8: Composite two alpha pixels.
182  * @dst: Where to store resulting pixel, also initially backdrop color.
183  * @src: Source pixel color.
184  * @n_chan: Number of channels.
185  * @blend_mode: Blend mode.
186  * @pblend_procs: Procs for handling non separable blending modes.
187  *
188  * Composites two pixels using the basic compositing operation. A few
189  * things to keep in mind:
190  *
191  * 1. This is a reference implementation, not a high-performance one.
192  *
193  * 2. All pixels are assumed to have a single alpha channel.
194  *
195  * 3. Zero is black, one is white.
196  *
197  * Also note that src and dst are expected to be allocated aligned to
198  * 32 bit boundaries, ie bytes from [0] to [(n_chan + 3) & -4] may
199  * be accessed.
200  **/
201 void
202 art_pdf_composite_pixel_alpha_8(byte *dst, const byte *src, int n_chan,
203 	gs_blend_mode_t blend_mode,
204        	const pdf14_nonseparable_blending_procs_t * pblend_procs);
205 
206 /**
207  * art_pdf_uncomposite_group_8: Uncomposite group pixel.
208  * @dst: Where to store uncomposited pixel.
209  * @backdrop: Backdrop.
210  * @src: Composited source pixel.
211  * @src_alpha_g: alpha_g value associated with @src.
212  * @n_chan: Number of channels.
213  *
214  * Performs uncompositing operation as described in 5.3 of the Adobe spec.
215  **/
216 void
217 art_pdf_uncomposite_group_8(byte *dst,
218 			    const byte *backdrop,
219 
220 			    const byte *src, byte src_alpha_g, int n_chan);
221 
222 /**
223  * art_pdf_recomposite_group_8: Recomposite group pixel.
224  * @dst: Where to store pixel, also initial backdrop of group.
225  * @dst_alpha_g: Optional pointer to alpha g value associated with @dst.
226  * @alpha: Alpha mask value.
227  * @src_alpha_g: alpha_g value associated with @src.
228  * @blend_mode: Blend mode for compositing.
229  * @pblend_procs: Procs for handling non separable blending modes.
230  *
231  * Note: this is only for non-isolated groups. This covers only the
232  * single-alpha case. A separate function is needed for dual-alpha,
233  * and that probably needs to treat knockout separately.
234  *
235  * @src_alpha_g corresponds to $\alpha g_n$ in the Adobe notation.
236  *
237  * @alpha corresponds to $fk_i \cdot fm_i \cdot qk_i \cdot qm_i$.
238  **/
239 void
240 art_pdf_recomposite_group_8(byte *dst, byte *dst_alpha_g,
241 	const byte *src, byte src_alpha_g, int n_chan,
242 	byte alpha, gs_blend_mode_t blend_mode,
243        	const pdf14_nonseparable_blending_procs_t * pblend_procs);
244 
245 /**
246  * art_pdf_composite_group_8: Composite group pixel.
247  * @dst: Where to store pixel, also initial backdrop of group.
248  * @dst_alpha_g: Optional pointer to alpha g value.
249  * @alpha: Alpha mask value.
250  * @blend_mode: Blend mode for compositing.
251  * @pblend_procs: Procs for handling non separable blending modes.
252  *
253  * Note: this is only for isolated groups. This covers only the
254  * single-alpha case. A separate function is needed for dual-alpha,
255  * and that probably needs to treat knockout separately.
256  *
257  * @alpha corresponds to $fk_i \cdot fm_i \cdot qk_i \cdot qm_i$.
258  **/
259 void
260 art_pdf_composite_group_8(byte *dst, byte *dst_alpha_g,
261 	const byte *src, int n_chan, byte alpha, gs_blend_mode_t blend_mode,
262        	const pdf14_nonseparable_blending_procs_t * pblend_procs);
263 
264 /**
265  * art_pdf_composite_knockout_simple_8: Simple knockout compositing.
266  * @dst: Destination pixel.
267  * @dst_shape: Shape associated with @dst.
268  * @src: Source pixel.
269  * @n_chan: Number of channels.
270  * @opacity: Opacity.
271  *
272  * This function handles the simplest knockout case: an isolated
273  * knockout group, and an elementary shape. The alpha channel of @src
274  * is interpreted as shape.
275  **/
276 void
277 art_pdf_composite_knockout_simple_8(byte *dst,
278 				    byte *dst_shape,
279 
280 				    const byte *src,
281 				    int n_chan, byte opacity);
282 
283 /**
284  * art_pdf_composite_knockout_isolated_8: Simple knockout compositing.
285  * @dst: Destination pixel.
286  * @dst_shape: Shape associated with @dst.
287  * @src: Source pixel.
288  * @n_chan: Number of channels.
289  * @shape: Shape.
290  * @alpha_mask: Alpha mask.
291  * @shape_mask: Shape mask.
292  *
293  * This function handles compositin in an isolated knockout case. The
294  * alpha channel of @src is interpreted as alpha.
295  **/
296 void
297 art_pdf_composite_knockout_isolated_8(byte *dst,
298 				      byte *dst_shape,
299 				      const byte *src,
300 				      int n_chan,
301 				      byte shape,
302 				      byte alpha_mask, byte shape_mask);
303 
304 /**
305  * art_pdf_composite_knockout_8: General knockout compositing.
306  * @dst: Destination pixel.
307  * @dst_alpha_g: Pointer to alpha g value associated with @dst.
308  * @backdrop: Backdrop pixel (initial backdrop of knockout group).
309  * @src: Source pixel.
310  * @n_chan: Number of channels.
311  * @shape: Shape.
312  * @alpha_mask: Alpha mask.
313  * @shape_mask: Shape mask.
314  * @blend_mode: Blend mode for compositing.
315  * @pblend_procs: Procs for handling non separable blending modes.
316  *
317  * This function handles compositing in the case where the knockout
318  * group is non-isolated. If the @src pixels themselves come from a
319  * non-isolated group, they should be uncomposited before calling this
320  * routine.
321  **/
322 void
323 art_pdf_composite_knockout_8(byte *dst,
324 		byte *dst_alpha_g, const byte *backdrop, const byte *src,
325 		int n_chan, byte shape, byte alpha_mask,
326 		byte shape_mask, gs_blend_mode_t blend_mode,
327        		const pdf14_nonseparable_blending_procs_t * pblend_procs);
328 
329 /*
330  * Routines for handling the non separable blending modes.
331  */
332 /* RGB blending color space */
333 void art_blend_luminosity_rgb_8(int n_chan, byte *dst, const byte *backdrop,
334 			   const byte *src);
335 void art_blend_saturation_rgb_8(int n_chan, byte *dst, const byte *backdrop,
336 			   const byte *src);
337 /* CMYK and CMYK + spot blending color space */
338 void art_blend_saturation_cmyk_8(int n_chan, byte *dst, const byte *backdrop,
339 			   const byte *src);
340 void art_blend_luminosity_cmyk_8(int n_chan, byte *dst, const byte *backdrop,
341 			   const byte *src);
342 /* 'Custom' i.e. unknown blending color space. */
343 void art_blend_luminosity_custom_8(int n_chan, byte *dst, const byte *backdrop,
344 			   const byte *src);
345 void art_blend_saturation_custom_8(int n_chan, byte *dst, const byte *backdrop,
346 			   const byte *src);
347 
348 void pdf14_unpack_additive(int num_comp, gx_color_index color,
349 			       	pdf14_device * p14dev, byte * out);
350 void pdf14_unpack_subtractive(int num_comp, gx_color_index color,
351 			       	pdf14_device * p14dev, byte * out);
352 
353 void pdf14_unpack_compressed(int num_comp, gx_color_index color,
354 			       	pdf14_device * p14dev, byte * out);
355 
356 void pdf14_unpack_custom(int num_comp, gx_color_index color,
357 			       	pdf14_device * p14dev, byte * out);
358 
359 void pdf14_preserve_backdrop(pdf14_buf *buf, pdf14_buf *tos, bool has_shape);
360 
361 void pdf14_compose_group(pdf14_buf *tos, pdf14_buf *nos, pdf14_buf *maskbuf,
362 	      int x0, int x1, int y0, int y1, int n_chan, bool additive,
363 	      const pdf14_nonseparable_blending_procs_t * pblend_procs);
364 
365 gx_color_index pdf14_encode_smask_color(gx_device *dev,
366              const gx_color_value colors[], int ncomp);
367 
368 int pdf14_decode_smask_color(gx_device * dev, gx_color_index color,
369                              gx_color_value * out, int ncomp);
370 
371 
372 gx_color_index pdf14_encode_color(gx_device *dev, const gx_color_value colors[]);
373 
374 int pdf14_decode_color(gx_device * dev, gx_color_index color, gx_color_value * out);
375 gx_color_index pdf14_compressed_encode_color(gx_device *dev, const gx_color_value colors[]);
376 int pdf14_compressed_decode_color(gx_device * dev, gx_color_index color,
377 	       						gx_color_value * out);
378 void pdf14_gray_cs_to_cmyk_cm(gx_device * dev, frac gray, frac out[]);
379 void pdf14_rgb_cs_to_cmyk_cm(gx_device * dev, const gs_imager_state *pis,
380   			   frac r, frac g, frac b, frac out[]);
381 void pdf14_cmyk_cs_to_cmyk_cm(gx_device * dev, frac c, frac m, frac y, frac k, frac out[]);
382 
383 void gx_build_blended_image_row(byte *buf_ptr, int y, int planestride,
384 			   int width, int num_comp, byte bg, byte *linebuf);
385 int gx_put_blended_image_cmykspot(gx_device *target, byte *buf_ptr,
386 		      int planestride, int rowstride,
387 		      int x0, int y0, int width, int height, int num_comp, byte bg,
388 		      gs_separations *pseparations);
389 int gx_put_blended_image_custom(gx_device *target, byte *buf_ptr,
390 		      int planestride, int rowstride,
391 		      int x0, int y0, int width, int height, int num_comp, byte bg);
392 
393 #if RAW_DUMP
394 
395 void dump_raw_buffer(int num_rows, int width, int n_chan,
396                     int plane_stride, int rowstride,
397                     char filename[],byte *Buffer);
398 #endif
399 
400 #endif /* gxblend_INCLUDED */
401