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