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 
14 /* $Id: gximage.h 8868 2008-07-23 23:29:39Z mvrhel $ */
15 /* Default image rendering state structure */
16 /* Requires gxcpath.h, gxdevmem.h, gxdcolor.h, gzpath.h */
17 
18 #ifndef gximage_INCLUDED
19 #  define gximage_INCLUDED
20 
21 #include "gsiparam.h"
22 #include "gxcspace.h"
23 #include "strimpl.h"		/* for sisparam.h */
24 #include "sisparam.h"
25 #include "gxdda.h"
26 #include "gxiclass.h"
27 #include "gxiparam.h"
28 #include "gxsample.h"
29 
30 /* Define the abstract type for the image enumerator state. */
31 /*typedef struct gx_image_enum_s gx_image_enum;*/  /* in gxiclass.h */
32 
33 /*
34  * Incoming samples may go through two different transformations:
35  *
36  *      - For N-bit input samples with N <= 8, N-to-8-bit expansion
37  *      may involve a lookup map.  Currently this map is either an
38  *      identity function or a subtraction from 1 (inversion).
39  *
40  *      - The 8-bit or frac expanded sample may undergo decoding (a linear
41  *      transformation) before being handed off to the color mapping
42  *      machinery.
43  *
44  * If the decoding function's range is [0..1], we fold it into the
45  * expansion lookup; otherwise we must compute it separately.
46  * For speed, we distinguish 3 different cases of the decoding step:
47  */
48 typedef enum {
49     sd_none,			/* decoded during expansion */
50     sd_lookup,			/* use lookup_decode table */
51     sd_compute			/* compute using base and factor */
52 } sample_decoding;
53 struct sample_map_s {
54 
55     sample_lookup_t table;
56 
57     /*
58      * If an 8-bit fraction doesn't represent the decoded value
59      * accurately enough, but the samples have 4 bits or fewer,
60      * we precompute the decoded values into a table.
61      * Different entries are used depending on bits/sample:
62      *      1,8,12 bits/sample: 0,15
63      *      2 bits/sample: 0,5,10,15
64      *      4 bits/sample: all
65      */
66 
67     float decode_lookup[16];
68 #define decode_base decode_lookup[0]
69 #define decode_max decode_lookup[15]
70 
71     /*
72      * In the worst case, we have to do the decoding on the fly.
73      * The value is base + sample * factor, where the sample is
74      * an 8-bit (unsigned) integer or a frac.
75      */
76 
77     double decode_factor;
78 
79     sample_decoding decoding;
80 
81     /*
82      * If decoding is sd_none for a non-mask image, we still need to know
83      * whether the table includes an inversion, so that we can transform
84      * mask values correctly.
85      */
86 
87     bool inverted;
88 
89 };
90 
91 #ifndef sample_map_DEFINED
92 #define sample_map_DEFINED
93 typedef struct sample_map_s sample_map;
94 #endif
95 
96 /* Decode an 8-bit sample into a floating point color component. */
97 /* penum points to the gx_image_enum structure. */
98 #define decode_sample(sample_value, cc, i)\
99   switch ( penum->map[i].decoding )\
100   {\
101   case sd_none:\
102     cc.paint.values[i] = (sample_value) * (1.0 / 255.0);  /* faster than / */\
103     break;\
104   case sd_lookup:	/* <= 4 significant bits */\
105     cc.paint.values[i] =\
106       penum->map[i].decode_lookup[(sample_value) >> 4];\
107     break;\
108   case sd_compute:\
109     cc.paint.values[i] =\
110       penum->map[i].decode_base + (sample_value) * penum->map[i].decode_factor;\
111   }
112 
113 /* Decode a frac value similarly. */
114 #define decode_frac(frac_value, cc, i)\
115   cc.paint.values[i] =\
116     penum->map[i].decode_base + (frac_value) * penum->map[i].decode_factor
117 
118 
119 /* Decode a frac value, to our 16 bit frac form. */
120 #define DECODE_FRAC_FRAC(frac_value, frac_value_out, i)\
121   frac_value_out =\
122     gx_unit_frac(penum->map[i].decode_base + (frac_value) * penum->map[i].decode_factor)
123 
124 /*
125  * Declare the pointer that holds the 12-bit unpacking procedure
126  * if 12-bit samples are supported, 0 otherwise.
127  */
128 extern const sample_unpack_proc_t sample_unpack_12_proc;
129 
130 /*
131  * Declare the pointer that holds the 16-bit unpacking procedure
132  * if 16-bit samples are supported, 0 otherwise.
133  */
134 extern const sample_unpack_proc_t sample_unpack_16_proc;
135 
136 /* Define the distinct postures of an image. */
137 /* Each posture includes its reflected variant. */
138 typedef enum {
139     image_portrait = 0,		/* 0 or 180 degrees */
140     image_landscape,		/* 90 or 270 degrees */
141     image_skewed		/* any other transformation */
142 } image_posture;
143 
144 /*
145  * Define an entry in the image color table.  For single-source-plane
146  * images, the table index is the sample value, and the key is not used;
147  * for multiple-plane (color) images, the table index is a hash of the key,
148  * which is the concatenation of the source pixel components.
149  * "Clue" = Color LookUp Entry (by analogy with CLUT).
150  */
151 typedef struct gx_image_clue_s {
152     gx_device_color dev_color;
153     bits32 key;
154 } gx_image_clue;
155 
156 /* Main state structure */
157 
158 #ifndef gx_device_clip_DEFINED
159 #  define gx_device_clip_DEFINED
160 typedef struct gx_device_clip_s gx_device_clip;
161 #endif
162 
163 #ifndef gx_device_rop_texture_DEFINED
164 #  define gx_device_rop_texture_DEFINED
165 typedef struct gx_device_rop_texture_s gx_device_rop_texture;
166 #endif
167 
168 struct gx_image_enum_s {
169     gx_image_enum_common;
170     /* We really want the map structure to be long-aligned, */
171     /* so we choose shorter types for some flags. */
172     /* Following are set at structure initialization */
173     int Width;			/* Full image width */
174     int Height;			/* Full image height */
175     byte bps;			/* bits per sample: 1, 2, 4, 8, 12 */
176     byte unpack_bps;		/* bps for computing unpack proc, */
177 				/* set to 8 if no unpacking */
178     byte log2_xbytes;		/* log2(bytes per expanded sample): */
179 				/* 0 if bps <= 8, log2(sizeof(frac)) */
180 				/* if bps > 8 */
181     byte spp;			/* samples per pixel */
182     gs_image_alpha_t alpha;	/* Alpha from image structure */
183     struct mc_ {
184 	uint values[GS_IMAGE_MAX_COMPONENTS * 2]; /* MaskColor values, */
185 				/* always as ranges, guaranteed in range */
186 				/* and in order (v0 <= v1) */
187 	bits32 mask, test;	/* (if spp > 1, bps <= 8) */
188 				/* mask & test value for quick filtering */
189 	bool exact;		/* (if spp > 1, bps <= 8) */
190 				/* if true, mask/test filter is exact */
191     } mask_color;		/* (if ImageType 4) */
192     byte use_mask_color;	/* true if color masking is being used */
193     /*byte num_planes; */	/* (in common part) */
194     byte spread;		/* (spp if multi-plane, 1 if not) */
195 				/* << log2_xbytes */
196     byte masked;		/* 0 = [color]image, 1 = imagemask */
197     byte interpolate;		/* true if Interpolate requested */
198     gs_matrix matrix;		/* image space -> device space */
199     struct r_ {
200 	int x, y, w, h;		/* subrectangle being rendered */
201     } rect;
202     fixed dst_height;		/* Full image height in the device space for siscale.c only;
203 				   assumes posture == image_portrait. */
204     fixed dst_width;		/* Full image width in the device space for siscale.c only;
205 				   assumes posture == image_portrait. */
206     gs_fixed_point x_extent, y_extent;	/* extent of one row of rect */
207     SAMPLE_UNPACK_PROC((*unpack));
208     irender_proc((*render));
209     const gs_imager_state *pis;
210     const gs_color_space *pcs;	/* color space of image */
211     byte *buffer;		/* for expanding samples to a */
212 				/* byte or frac */
213     uint buffer_size;
214     byte *line;			/* buffer for an output scan line */
215     uint line_size;
216     uint line_width;		/* width of line in device pixels */
217     image_posture posture;
218     byte use_rop;		/* true if CombineWithColor requested */
219     byte clip_image;		/* mask, see below */
220     /* Either we are clipping to a rectangle, in which case */
221     /* the individual x/y flags may be set, or we are clipping */
222     /* to a general region, in which case only clip_region */
223     /* is set. */
224 #define image_clip_xmin 1
225 #define image_clip_xmax 2
226 #define image_clip_ymin 4
227 #define image_clip_ymax 8
228 #define image_clip_region 0x10
229     byte slow_loop;		/* true if must use slower loop */
230 				/* (if needed) */
231     byte device_color;		/* true if device color space and */
232 				/* standard decoding */
233     gs_fixed_rect clip_outer;	/* outer box of clip path */
234     gs_fixed_rect clip_inner;	/* inner box of clip path */
235     gs_logical_operation_t log_op;	/* logical operation */
236     fixed adjust;		/* adjustment when rendering */
237 				/* characters */
238     fixed dxx, dxy;		/* fixed versions of matrix */
239 				/* components (as needed) */
240     gx_device_clip *clip_dev;	/* clipping device (if needed) */
241     gx_device_rop_texture *rop_dev;	/* RasterOp device (if needed) */
242     stream_image_scale_state *scaler;	/* scale state for Interpolate */
243 				/* (if needed) */
244     /* Following are updated dynamically */
245     int y;			/* next source y */
246     gs_int_point used;		/* amount of data already used, if */
247 				/* interrupted by error */
248     gs_fixed_point cur, prev;	/* device x, y of current & */
249 				/* previous row */
250     struct dd_ {
251 	gx_dda_fixed_point row;	/* DDA for row origin, has been */
252 				/* advanced when render proc called */
253 	gx_dda_fixed_point strip;  /* row + rect.x */
254 	gx_dda_fixed_point pixel0;	/* DDA for first pixel to render, */
255 				/* strip + used.x */
256     } dda;
257     int line_xy;		/* x or y value at start of buffered line */
258     int xi_next;		/* expected xci of next row */
259 				/* (landscape only) */
260     gs_int_point xyi;		/* integer origin of row */
261 				/* (Interpolate only) */
262     int yi0;			/* integer y of entire image origin. */
263     int yci, hci;		/* integer y & h of row (portrait) */
264     int xci, wci;		/* integer x & w of row (landscape) */
265     /* The maps are set at initialization.  We put them here */
266     /* so that the scalars will have smaller offsets. */
267     sample_map map[GS_IMAGE_MAX_COMPONENTS];
268     /* Entries 0 and 255 of the following are set at initialization */
269     /* for monochrome images; other entries are updated dynamically. */
270     gx_image_clue clues[256];
271 #define icolor0 clues[0].dev_color
272 #define icolor1 clues[255].dev_color
273 };
274 
275 /* Enumerate the pointers in an image enumerator. */
276 #define gx_image_enum_do_ptrs(m)\
277   m(0,pis) m(1,pcs) m(2,dev) m(3,buffer) m(4,line)\
278   m(5,clip_dev) m(6,rop_dev) m(7,scaler)
279 #define gx_image_enum_num_ptrs 8
280 #define private_st_gx_image_enum() /* in gsimage.c */\
281   gs_private_st_composite(st_gx_image_enum, gx_image_enum, "gx_image_enum",\
282     image_enum_enum_ptrs, image_enum_reloc_ptrs)
283 
284 /* Compare two device colors for equality. */
285 /* We can special-case this for speed later if we care. */
286 #define dev_color_eq(devc1, devc2)\
287   gx_device_color_equal(&(devc1), &(devc2))
288 
289 /*
290  * Scale a pair of mask_color values to match the scaling of each sample to
291  * a full byte, and complement and swap them if the map incorporates
292  * a Decode = [1 0] inversion.
293  */
294 void gx_image_scale_mask_colors(gx_image_enum *penum,
295 				int component_index);
296 
297 /*
298  * Do common initialization for processing an ImageType 1 or 4 image.
299  * Allocate the enumerator and fill in the following members:
300  *	rect
301  */
302 int
303 gx_image_enum_alloc(const gs_image_common_t * pic,
304 		    const gs_int_rect * prect,
305 		    gs_memory_t * mem, gx_image_enum **ppenum);
306 
307 /*
308  * Finish initialization for processing an ImageType 1 or 4 image.
309  * Assumes the following members of *penum are set in addition to those
310  * set by gx_image_enum_alloc:
311  *	alpha, use_mask_color, mask_color (if use_mask_color is true),
312  *	masked, adjust
313  */
314 int
315 gx_image_enum_begin(gx_device * dev, const gs_imager_state * pis,
316 		    const gs_matrix *pmat, const gs_image_common_t * pic,
317 		    const gx_drawing_color * pdcolor,
318 		    const gx_clip_path * pcpath,
319 		    gs_memory_t * mem, gx_image_enum *penum);
320 
321 /*
322  * Clear the relevant clues. Exported for use by image_render_*
323  * when ht_tile cache is invalidated.
324  */
325 void
326 image_init_clues(gx_image_enum * penum, int bps, int spp);
327 
328 #endif /* gximage_INCLUDED */
329