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