xref: /plan9/sys/src/cmd/gs/src/gxsamplp.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1997, 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: gxsamplp.h,v 1.2 2005/06/08 14:38:21 igor Exp $ */
18 /* Templates for sample lookup and expansion */
19 
20 /* This module is allowed to include several times into a single .c file.
21    The following macros to be defined in advance :
22 	MULTIPLE_MAPS - 1 if num_components_per_plane > 0 and
23 			components use different maps, 0 otherwise.
24 	TEMPLATE_sample_unpack_1 - a name for the function
25 	TEMPLATE_sample_unpack_2 - a name for the function
26 	TEMPLATE_sample_unpack_4 - a name for the function
27 	TEMPLATE_sample_unpack_8 - a name for the function
28  */
29 
30 #if MULTIPLE_MAPS
31 #   define NEXT_MAP map = smap[++smap_index % num_components_per_plane].table.lookup4x1to32
32 #   define NEXT_MAP8 map = smap[++smap_index % num_components_per_plane].table.lookup8
33 #   define DEFINE_SMAP_INDEX int smap_index = 0;
34 #else
35 #   define NEXT_MAP
36 #   define NEXT_MAP8
37 #   define DEFINE_SMAP_INDEX
38 #endif
39 
40 const byte *
TEMPLATE_sample_unpack_1(byte * bptr,int * pdata_x,const byte * data,int data_x,uint dsize,const sample_map * smap,int spread,int num_components_per_plane)41 TEMPLATE_sample_unpack_1(byte * bptr, int *pdata_x, const byte * data, int data_x,
42 		uint dsize, const sample_map *smap, int spread,
43 		int num_components_per_plane)
44 {
45     const sample_lookup_t * ptab = &smap->table;
46     const byte *psrc = data + (data_x >> 3);
47     int left = dsize - (data_x >> 3);
48     DEFINE_SMAP_INDEX
49 
50     if (spread == 1) {
51 	bits32 *bufp = (bits32 *) bptr;
52 	const bits32 *map = &ptab->lookup4x1to32[0];
53 	uint b;
54 
55 	if (left & 1) {
56 	    b = psrc[0];
57 	    bufp[0] = map[b >> 4];
58 	    NEXT_MAP;
59 	    bufp[1] = map[b & 0xf];
60 	    NEXT_MAP;
61 	    psrc++, bufp += 2;
62 	}
63 	left >>= 1;
64 	while (left--) {
65 	    b = psrc[0];
66 	    bufp[0] = map[b >> 4];
67 	    NEXT_MAP;
68 	    bufp[1] = map[b & 0xf];
69 	    NEXT_MAP;
70 	    b = psrc[1];
71 	    bufp[2] = map[b >> 4];
72 	    NEXT_MAP;
73 	    bufp[3] = map[b & 0xf];
74 	    NEXT_MAP;
75 	    psrc += 2, bufp += 4;
76 	}
77     } else {
78 	byte *bufp = bptr;
79 	const byte *map = &ptab->lookup8[0];
80 
81 	while (left--) {
82 	    uint b = *psrc++;
83 
84 	    *bufp = map[b >> 7];
85 	    NEXT_MAP8;
86 	    bufp += spread;
87 	    *bufp = map[(b >> 6) & 1];
88 	    NEXT_MAP8;
89 	    bufp += spread;
90 	    *bufp = map[(b >> 5) & 1];
91 	    NEXT_MAP8;
92 	    bufp += spread;
93 	    *bufp = map[(b >> 4) & 1];
94 	    NEXT_MAP8;
95 	    bufp += spread;
96 	    *bufp = map[(b >> 3) & 1];
97 	    NEXT_MAP8;
98 	    bufp += spread;
99 	    *bufp = map[(b >> 2) & 1];
100 	    NEXT_MAP8;
101 	    bufp += spread;
102 	    *bufp = map[(b >> 1) & 1];
103 	    NEXT_MAP8;
104 	    bufp += spread;
105 	    *bufp = map[b & 1];
106 	    NEXT_MAP8;
107 	    bufp += spread;
108 	}
109     }
110     *pdata_x = data_x & 7;
111     return bptr;
112 }
113 
114 #undef NEXT_MAP
115 
116 #if MULTIPLE_MAPS
117 #   define NEXT_MAP  map = smap[++smap_index % num_components_per_plane].table.lookup2x2to16
118 #else
119 #   define NEXT_MAP
120 #endif
121 
122 const byte *
TEMPLATE_sample_unpack_2(byte * bptr,int * pdata_x,const byte * data,int data_x,uint dsize,const sample_map * smap,int spread,int num_components_per_plane)123 TEMPLATE_sample_unpack_2(byte * bptr, int *pdata_x, const byte * data, int data_x,
124 		uint dsize, const sample_map *smap, int spread,
125 		int num_components_per_plane)
126 {
127     const sample_lookup_t * ptab = &smap->table;
128     const byte *psrc = data + (data_x >> 2);
129     int left = dsize - (data_x >> 2);
130     DEFINE_SMAP_INDEX
131 
132     if (spread == 1) {
133 	bits16 *bufp = (bits16 *) bptr;
134 	const bits16 *map = &ptab->lookup2x2to16[0];
135 
136 	while (left--) {
137 	    uint b = *psrc++;
138 
139 	    *bufp++ = map[b >> 4];
140 	    NEXT_MAP;
141 	    *bufp++ = map[b & 0xf];
142 	    NEXT_MAP;
143 	}
144     } else {
145 	byte *bufp = bptr;
146 	const byte *map = &ptab->lookup8[0];
147 
148 	while (left--) {
149 	    unsigned b = *psrc++;
150 
151 	    *bufp = map[b >> 6];
152 	    NEXT_MAP8;
153 	    bufp += spread;
154 	    *bufp = map[(b >> 4) & 3];
155 	    NEXT_MAP8;
156 	    bufp += spread;
157 	    *bufp = map[(b >> 2) & 3];
158 	    NEXT_MAP8;
159 	    bufp += spread;
160 	    *bufp = map[b & 3];
161 	    NEXT_MAP8;
162 	    bufp += spread;
163 	}
164     }
165     *pdata_x = data_x & 3;
166     return bptr;
167 }
168 
169 #undef NEXT_MAP
170 
171 const byte *
TEMPLATE_sample_unpack_4(byte * bptr,int * pdata_x,const byte * data,int data_x,uint dsize,const sample_map * smap,int spread,int num_components_per_plane)172 TEMPLATE_sample_unpack_4(byte * bptr, int *pdata_x, const byte * data, int data_x,
173 		uint dsize, const sample_map *smap, int spread,
174 		int num_components_per_plane)
175 {
176     const sample_lookup_t * ptab = &smap->table;
177     byte *bufp = bptr;
178     const byte *psrc = data + (data_x >> 1);
179     int left = dsize - (data_x >> 1);
180     const byte *map = &ptab->lookup8[0];
181     DEFINE_SMAP_INDEX
182 
183     while (left--) {
184 	uint b = *psrc++;
185 
186 	*bufp = map[b >> 4];
187 	NEXT_MAP8;
188 	bufp += spread;
189 	*bufp = map[b & 0xf];
190 	NEXT_MAP8;
191 	bufp += spread;
192     }
193     *pdata_x = data_x & 1;
194     return bptr;
195 }
196 
197 const byte *
TEMPLATE_sample_unpack_8(byte * bptr,int * pdata_x,const byte * data,int data_x,uint dsize,const sample_map * smap,int spread,int num_components_per_plane)198 TEMPLATE_sample_unpack_8(byte * bptr, int *pdata_x, const byte * data, int data_x,
199 		uint dsize, const sample_map *smap, int spread,
200 		int num_components_per_plane)
201 {
202     const sample_lookup_t * ptab = &smap->table;
203     byte *bufp = bptr;
204     const byte *psrc = data + data_x;
205     DEFINE_SMAP_INDEX
206 
207     *pdata_x = 0;
208     if (spread == 1) {
209 	if (MULTIPLE_MAPS ||
210 	    ptab->lookup8[0] != 0 ||
211 	    ptab->lookup8[255] != 255
212 	    ) {
213 	    uint left = dsize - data_x;
214 	    const byte *map = &ptab->lookup8[0];
215 
216 	    while (left--) {
217 		*bufp++ = map[*psrc++];
218 		NEXT_MAP8;
219 	    }
220 	} else {		/* No copying needed, and we'll use the data right away. */
221 	    return psrc;
222 	}
223     } else {
224 	int left = dsize - data_x;
225 	const byte *map = &ptab->lookup8[0];
226 
227 	for (; left--; psrc++, bufp += spread) {
228 	    *bufp = map[*psrc];
229 	    NEXT_MAP8;
230 	}
231     }
232     return bptr;
233 }
234 
235 #undef NEXT_MAP
236 #undef NEXT_MAP8
237 #undef DEFINE_SMAP_INDEX
238