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: gxbitmap.h 8022 2007-06-05 22:23:38Z giles $ */
15 /* Definitions for stored bitmaps for Ghostscript */
16 
17 #ifndef gxbitmap_INCLUDED
18 #  define gxbitmap_INCLUDED
19 
20 #include "gstypes.h"		/* for gs_id */
21 #include "gsbitmap.h"
22 
23 /* Define the gx version of a bitmap identifier. */
24 typedef gs_bitmap_id gx_bitmap_id;
25 
26 /* Define the gx version of the "no identifier" value. */
27 #define gx_no_bitmap_id gs_no_bitmap_id
28 
29 /*
30  * Most graphics library procedures that process bitmap data (such as, for
31  * example, the "device" procedures in gdevm*.c) impose two requirements
32  * on such data: an alignment requirement, and a padding requirement.
33  * Both requirements arise from the fact that these library procedures
34  * attempt to process the bits in units of align_bitmap_mod bytes.
35  *
36  * The alignment requirement is that each scan line must start at an
37  * address that is 0 mod align_bitmap_mod.  This requirement is only for
38  * the benefit of the hardware (which may, for example, require that
39  * instructions fetching or storing a 'long' quantity only do so at an
40  * address that is long-aligned), but it must be respected in all
41  * platform-independent code.  More precisely, platform-independent code
42  * can *assume* that Ghostscript allocators return blocks that are adequately
43  * aligned, and then must *ensure* that that alignment is not lost.  (The
44  * assumption is not true in some MSVC implementations, but even in those
45  * implementations, the alignment is sufficient to satisfy the hardware.
46  * See gsmemraw.h for more information about this.)
47  *
48  * The padding requirement is that if the last data byte being operated on
49  * is at offset B relative to the start of the scan line, bytes up to and
50  * including offset ROUND_UP(B + 1, align_bitmap_mod) - 1 may be accessed,
51  * and therefore must be allocated (not cause hardware-level access faults).
52  */
53 /* We assume ARCH_ALIGN_LONG_MOD is 1-4 or 8. */
54 #if ARCH_ALIGN_LONG_MOD <= 4
55 #  define log2_align_bitmap_mod 2
56 #else
57 #if ARCH_ALIGN_LONG_MOD == 8
58 #  define log2_align_bitmap_mod 3
59 #endif
60 #endif
61 #define align_bitmap_mod (1 << log2_align_bitmap_mod)
62 #define bitmap_raster(width_bits)\
63   ((uint)((((width_bits) + (align_bitmap_mod * 8 - 1))\
64     >> (log2_align_bitmap_mod + 3)) << log2_align_bitmap_mod))
65 
66 /*
67  * Define the gx analogue of the basic bitmap structure.  Note that since
68  * all scan lines must be aligned, the requirement on raster is:
69  *      If size.y > 1,
70  *          raster >= bitmap_raster(size.x * depth)
71  *          raster % align_bitmap_mod = 0
72  */
73 #define gx_bitmap_common(data_type) gs_bitmap_common(data_type)
74 typedef struct gx_bitmap_s {
75     gx_bitmap_common(byte);
76 } gx_bitmap;
77 typedef struct gx_const_bitmap_s {
78     gx_bitmap_common(const byte);
79 } gx_const_bitmap;
80 
81 /*
82  * Define the gx analogue of the tile bitmap structure.  Note that if
83  * shift != 0 (for strip bitmaps, see below), size.y and rep_height
84  * mean something slightly different: see below for details.
85  */
86 #define gx_tile_bitmap_common(data_type) gs_tile_bitmap_common(data_type)
87 typedef struct gx_tile_bitmap_s {
88     gx_tile_bitmap_common(byte);
89 } gx_tile_bitmap;
90 typedef struct gx_const_tile_bitmap_s {
91     gx_tile_bitmap_common(const byte);
92 } gx_const_tile_bitmap;
93 
94 /*
95  * For halftones at arbitrary angles, we provide for storing the halftone
96  * data as a strip that must be shifted in X for different values of Y.  For
97  * an ordinary (non-shifted) halftone that has a repetition width of W and a
98  * repetition height of H, the pixel at coordinate (X,Y) corresponds to
99  * halftone pixel (X mod W, Y mod H), ignoring phase; for a strip halftone
100  * with strip shift S and strip height H, the pixel at (X,Y) corresponds to
101  * halftone pixel ((X + S * floor(Y/H)) mod W, Y mod H).  In other words,
102  * each Y increment of H shifts the strip left by S pixels.
103  *
104  * As for non-shifted tiles, a strip bitmap may include multiple copies
105  * in X or Y to reduce loop overhead.  In this case, we must distinguish:
106  *      - The height of an individual strip, which is the same as
107  *      the height of the bitmap being replicated (rep_height, H);
108  *      - The height of the entire bitmap (size.y).
109  * Similarly, we must distinguish:
110  *      - The shift per strip (rep_shift, S);
111  *      - The shift for the entire bitmap (shift).
112  * Note that shift = (rep_shift * size.y / rep_height) mod rep_width,
113  * so the shift member of the structure is only an accelerator.  It is,
114  * however, an important one, since it indicates whether the overall
115  * bitmap requires shifting or not.
116  *
117  * Note that for shifted tiles, size.y is the size of the stored bitmap
118  * (1 or more strips), and NOT the height of the actual tile.  The latter
119  * is not stored in the structure at all: it can be computed as H * W /
120  * gcd(S, W).
121  *
122  * If the bitmap consists of a multiple of W / gcd(S, W) copies in Y, the
123  * effective shift is zero, reducing it to a tile.  For simplicity, we
124  * require that if shift is non-zero, the bitmap height be less than H * W /
125  * gcd(S, W).  I.e., we don't allow strip bitmaps that are large enough to
126  * include a complete tile but that don't include an integral number of
127  * tiles.  Requirements:
128  *      rep_shift < rep_width
129  *      shift = (rep_shift * (size.y / rep_height)) % rep_width
130  */
131 #define gx_strip_bitmap_common(data_type)\
132 	gx_tile_bitmap_common(data_type);\
133 	ushort rep_shift;\
134 	ushort shift
135 typedef struct gx_strip_bitmap_s {
136     gx_strip_bitmap_common(byte);
137 } gx_strip_bitmap;
138 typedef struct gx_const_strip_bitmap_s {
139     gx_strip_bitmap_common(const byte);
140 } gx_const_strip_bitmap;
141 
142 extern_st(st_gx_strip_bitmap);
143 #define public_st_gx_strip_bitmap()	/* in gspcolor.c */\
144   gs_public_st_suffix_add0_local(st_gx_strip_bitmap, gx_strip_bitmap,\
145     "gx_strip_bitmap", bitmap_enum_ptrs, bitmap_reloc_ptrs,\
146     st_gs_tile_bitmap)
147 #define st_gx_strip_bitmap_max_ptrs 1
148 
149 #endif /* gxbitmap_INCLUDED */
150