1 /*
2 * Bit packing routines for CUPS.
3 *
4 * Copyright 2007 by Apple Inc.
5 * Copyright 1993-2005 by Easy Software Products.
6 *
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "COPYING"
10 * which should have been included with this file.
11 *
12 * Contents:
13 *
14 * cupsPackHorizontal() - Pack pixels horizontally...
15 * cupsPackHorizontal2() - Pack 2-bit pixels horizontally...
16 * cupsPackHorizontalBit() - Pack pixels horizontally by bit...
17 * cupsPackVertical() - Pack pixels vertically...
18 */
19
20 /*
21 * Include necessary headers...
22 */
23
24 #include "driver.h"
25
26
27 /*
28 * 'cupsPackHorizontal()' - Pack pixels horizontally...
29 */
30
31 void
cupsPackHorizontal(const unsigned char * ipixels,unsigned char * obytes,int width,const unsigned char clearto,const int step)32 cupsPackHorizontal(const unsigned char *ipixels,/* I - Input pixels */
33 unsigned char *obytes, /* O - Output bytes */
34 int width, /* I - Number of pixels */
35 const unsigned char clearto, /* I - Initial value of bytes */
36 const int step) /* I - Step value between pixels */
37 {
38 register unsigned char b; /* Current byte */
39
40
41 /*
42 * Do whole bytes first...
43 */
44
45 while (width > 7)
46 {
47 b = clearto;
48
49 if (*ipixels)
50 b ^= 0x80;
51 ipixels += step;
52 if (*ipixels)
53 b ^= 0x40;
54 ipixels += step;
55 if (*ipixels)
56 b ^= 0x20;
57 ipixels += step;
58 if (*ipixels)
59 b ^= 0x10;
60 ipixels += step;
61 if (*ipixels)
62 b ^= 0x08;
63 ipixels += step;
64 if (*ipixels)
65 b ^= 0x04;
66 ipixels += step;
67 if (*ipixels)
68 b ^= 0x02;
69 ipixels += step;
70 if (*ipixels)
71 b ^= 0x01;
72 ipixels += step;
73
74 *obytes++ = b;
75
76 width -= 8;
77 }
78
79 /*
80 * Then do the last N bytes (N < 8)...
81 */
82
83 b = clearto;
84
85 switch (width)
86 {
87 case 7 :
88 if (ipixels[6 * step])
89 b ^= 0x02;
90 case 6 :
91 if (ipixels[5 * step])
92 b ^= 0x04;
93 case 5 :
94 if (ipixels[4 * step])
95 b ^= 0x08;
96 case 4 :
97 if (ipixels[3 * step])
98 b ^= 0x10;
99 case 3 :
100 if (ipixels[2 * step])
101 b ^= 0x20;
102 case 2 :
103 if (ipixels[1 * step])
104 b ^= 0x40;
105 case 1 :
106 if (ipixels[0])
107 b ^= 0x80;
108 *obytes = b;
109 break;
110 }
111 }
112
113
114 /*
115 * 'cupsPackHorizontal2()' - Pack 2-bit pixels horizontally...
116 */
117
118 void
cupsPackHorizontal2(const unsigned char * ipixels,unsigned char * obytes,int width,const int step)119 cupsPackHorizontal2(const unsigned char *ipixels, /* I - Input pixels */
120 unsigned char *obytes, /* O - Output bytes */
121 int width, /* I - Number of pixels */
122 const int step) /* I - Stepping value */
123 {
124 register unsigned char b; /* Current byte */
125
126
127 /*
128 * Do whole bytes first...
129 */
130
131 while (width > 3)
132 {
133 b = *ipixels;
134 ipixels += step;
135 b = (b << 2) | *ipixels;
136 ipixels += step;
137 b = (b << 2) | *ipixels;
138 ipixels += step;
139 b = (b << 2) | *ipixels;
140 ipixels += step;
141
142 *obytes++ = b;
143
144 width -= 4;
145 }
146
147 /*
148 * Then do the last N bytes (N < 4)...
149 */
150
151 b = 0;
152
153 switch (width)
154 {
155 case 3 :
156 b = ipixels[2 * step];
157 case 2 :
158 b = (b << 2) | ipixels[step];
159 case 1 :
160 b = (b << 2) | ipixels[0];
161 *obytes = b << (8 - 2 * width);
162 break;
163 }
164 }
165
166
167 /*
168 * 'cupsPackHorizontalBit()' - Pack pixels horizontally by bit...
169 */
170
171 void
cupsPackHorizontalBit(const unsigned char * ipixels,unsigned char * obytes,int width,const unsigned char clearto,const unsigned char bit)172 cupsPackHorizontalBit(const unsigned char *ipixels, /* I - Input pixels */
173 unsigned char *obytes, /* O - Output bytes */
174 int width, /* I - Number of pixels */
175 const unsigned char clearto, /* I - Initial value of bytes */
176 const unsigned char bit) /* I - Bit to check */
177 {
178 register unsigned char b; /* Current byte */
179
180
181 /*
182 * Do whole bytes first...
183 */
184
185 while (width > 7)
186 {
187 b = clearto;
188
189 if (*ipixels++ & bit)
190 b ^= 0x80;
191 if (*ipixels++ & bit)
192 b ^= 0x40;
193 if (*ipixels++ & bit)
194 b ^= 0x20;
195 if (*ipixels++ & bit)
196 b ^= 0x10;
197 if (*ipixels++ & bit)
198 b ^= 0x08;
199 if (*ipixels++ & bit)
200 b ^= 0x04;
201 if (*ipixels++ & bit)
202 b ^= 0x02;
203 if (*ipixels++ & bit)
204 b ^= 0x01;
205
206 *obytes++ = b;
207
208 width -= 8;
209 }
210
211 /*
212 * Then do the last N bytes (N < 8)...
213 */
214
215 b = clearto;
216
217 switch (width)
218 {
219 case 7 :
220 if (ipixels[6] & bit)
221 b ^= 0x02;
222 case 6 :
223 if (ipixels[5] & bit)
224 b ^= 0x04;
225 case 5 :
226 if (ipixels[4] & bit)
227 b ^= 0x08;
228 case 4 :
229 if (ipixels[3] & bit)
230 b ^= 0x10;
231 case 3 :
232 if (ipixels[2] & bit)
233 b ^= 0x20;
234 case 2 :
235 if (ipixels[1] & bit)
236 b ^= 0x40;
237 case 1 :
238 if (ipixels[0] & bit)
239 b ^= 0x80;
240 *obytes = b;
241 break;
242 }
243 }
244
245
246 /*
247 * 'cupsPackVertical()' - Pack pixels vertically...
248 */
249
250 void
cupsPackVertical(const unsigned char * ipixels,unsigned char * obytes,int width,const unsigned char bit,const int step)251 cupsPackVertical(const unsigned char *ipixels, /* I - Input pixels */
252 unsigned char *obytes, /* O - Output bytes */
253 int width, /* I - Number of input pixels */
254 const unsigned char bit, /* I - Output bit */
255 const int step) /* I - Number of bytes between columns */
256 {
257 /*
258 * Loop through the entire array...
259 */
260
261 while (width > 7)
262 {
263 if (*ipixels++)
264 *obytes ^= bit;
265 obytes += step;
266 if (*ipixels++)
267 *obytes ^= bit;
268 obytes += step;
269 if (*ipixels++)
270 *obytes ^= bit;
271 obytes += step;
272 if (*ipixels++)
273 *obytes ^= bit;
274 obytes += step;
275 if (*ipixels++)
276 *obytes ^= bit;
277 obytes += step;
278 if (*ipixels++)
279 *obytes ^= bit;
280 obytes += step;
281 if (*ipixels++)
282 *obytes ^= bit;
283 obytes += step;
284 if (*ipixels++)
285 *obytes ^= bit;
286 obytes += step;
287
288 width -= 8;
289 }
290
291 while (width > 0)
292 {
293 if (*ipixels++)
294 *obytes ^= bit;
295
296 obytes += step;
297 width --;
298 }
299 }
300
301