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