Debian Codegraph @

nx-X11/lib/src/PutImage.c

raw
1 /*
2
3 Copyright 1986, 1998 The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
24
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include "Xlibint.h"
31 #include "Xutil.h"
32 #include <stdio.h>
33 #include "Cr.h"
34 #include "ImUtil.h"
35 #include "reallocarray.h"
36
37 #if defined(__STDC__) && ((defined(sun) && defined(SVR4)) || defined(WIN32))
38 #define RConst /**/
39 #else
40 #define RConst const
41 #endif
42
43 /* assumes pad is a power of 2 */
44 #define ROUNDUP(nbytes, pad) (((nbytes) + ((pad) - 1)) & ~(long)((pad) - 1))
45
46 static unsigned char const _reverse_byte[0x100] = {
47 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
48 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
49 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
50 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
51 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
52 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
53 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
54 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
55 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
56 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
57 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
58 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
59 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
60 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
61 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
62 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
63 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
64 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
65 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
66 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
67 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
68 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
69 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
70 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
71 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
72 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
73 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
74 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
75 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
76 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
77 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
78 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
79 };
80
81 static unsigned char const _reverse_nibs[0x100] = {
82 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
83 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0,
84 0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71,
85 0x81, 0x91, 0xa1, 0xb1, 0xc1, 0xd1, 0xe1, 0xf1,
86 0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72,
87 0x82, 0x92, 0xa2, 0xb2, 0xc2, 0xd2, 0xe2, 0xf2,
88 0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73,
89 0x83, 0x93, 0xa3, 0xb3, 0xc3, 0xd3, 0xe3, 0xf3,
90 0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74,
91 0x84, 0x94, 0xa4, 0xb4, 0xc4, 0xd4, 0xe4, 0xf4,
92 0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75,
93 0x85, 0x95, 0xa5, 0xb5, 0xc5, 0xd5, 0xe5, 0xf5,
94 0x06, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x76,
95 0x86, 0x96, 0xa6, 0xb6, 0xc6, 0xd6, 0xe6, 0xf6,
96 0x07, 0x17, 0x27, 0x37, 0x47, 0x57, 0x67, 0x77,
97 0x87, 0x97, 0xa7, 0xb7, 0xc7, 0xd7, 0xe7, 0xf7,
98 0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78,
99 0x88, 0x98, 0xa8, 0xb8, 0xc8, 0xd8, 0xe8, 0xf8,
100 0x09, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x79,
101 0x89, 0x99, 0xa9, 0xb9, 0xc9, 0xd9, 0xe9, 0xf9,
102 0x0a, 0x1a, 0x2a, 0x3a, 0x4a, 0x5a, 0x6a, 0x7a,
103 0x8a, 0x9a, 0xaa, 0xba, 0xca, 0xda, 0xea, 0xfa,
104 0x0b, 0x1b, 0x2b, 0x3b, 0x4b, 0x5b, 0x6b, 0x7b,
105 0x8b, 0x9b, 0xab, 0xbb, 0xcb, 0xdb, 0xeb, 0xfb,
106 0x0c, 0x1c, 0x2c, 0x3c, 0x4c, 0x5c, 0x6c, 0x7c,
107 0x8c, 0x9c, 0xac, 0xbc, 0xcc, 0xdc, 0xec, 0xfc,
108 0x0d, 0x1d, 0x2d, 0x3d, 0x4d, 0x5d, 0x6d, 0x7d,
109 0x8d, 0x9d, 0xad, 0xbd, 0xcd, 0xdd, 0xed, 0xfd,
110 0x0e, 0x1e, 0x2e, 0x3e, 0x4e, 0x5e, 0x6e, 0x7e,
111 0x8e, 0x9e, 0xae, 0xbe, 0xce, 0xde, 0xee, 0xfe,
112 0x0f, 0x1f, 0x2f, 0x3f, 0x4f, 0x5f, 0x6f, 0x7f,
113 0x8f, 0x9f, 0xaf, 0xbf, 0xcf, 0xdf, 0xef, 0xff
114 };
115
116 int
117 _XReverse_Bytes(
118 unsigned char *bpt,
119 int nb)
120 {
121 do {
122 *bpt = _reverse_byte[*bpt];
123 bpt++;
124 } while (--nb > 0);
125 return 0;
126 }
127
128
129 /* XXX the following functions are declared int instead of void because various
130 * compilers and lints complain about later initialization of SwapFunc and/or
131 * (swapfunc == NoSwap) when void is used.
132 */
133
134 /*ARGSUSED*/
135 static void
136 NoSwap (
137 register unsigned char *src,
138 register unsigned char *dest,
139 long srclen,
140 long srcinc,
141 long destinc,
142 unsigned int height,
143 int half_order)
144 {
145 long h = height;
146
147 if (srcinc == destinc)
148 memcpy((char *)dest, (char *)src, (int)(srcinc * (h - 1) + srclen));
149 else
150 for (; --h >= 0; src += srcinc, dest += destinc)
151 memcpy((char *)dest, (char *)src, (int)srclen);
152 }
153
154 static void
155 SwapTwoBytes (
156 register unsigned char *src,
157 register unsigned char *dest,
158 long srclen, long srcinc, long destinc,
159 unsigned int height,
160 int half_order)
161 {
162 long length = ROUNDUP(srclen, 2);
163 register long h, n;
164
165 srcinc -= length;
166 destinc -= length;
167 for (h = height; --h >= 0; src += srcinc, dest += destinc) {
168 if ((h == 0) && (srclen != length)) {
169 length -= 2;
170 if (half_order == MSBFirst)
171 *(dest + length) = *(src + length + 1);
172 else
173 *(dest + length + 1) = *(src + length);
174 }
175 for (n = length; n > 0; n -= 2, src += 2) {
176 *dest++ = *(src + 1);
177 *dest++ = *src;
178 }
179 }
180 }
181
182 static void
183 SwapThreeBytes (
184 register unsigned char *src,
185 register unsigned char *dest,
186 long srclen, long srcinc, long destinc,
187 unsigned int height,
188 int byte_order)
189 {
190 long length = ((srclen + 2) / 3) * 3;
191 register long h, n;
192
193 srcinc -= length;
194 destinc -= length;
195 for (h = height; --h >= 0; src += srcinc, dest += destinc) {
196 if ((h == 0) && (srclen != length)) {
197 length -= 3;
198 if ((srclen - length) == 2)
199 *(dest + length + 1) = *(src + length + 1);
200 if (byte_order == MSBFirst)
201 *(dest + length) = *(src + length + 2);
202 else
203 *(dest + length + 2) = *(src + length);
204 }
205 for (n = length; n > 0; n -= 3, src += 3) {
206 *dest++ = *(src + 2);
207 *dest++ = *(src + 1);
208 *dest++ = *src;
209 }
210 }
211 }
212
213 static void
214 SwapFourBytes (
215 register unsigned char *src,
216 register unsigned char *dest,
217 long srclen, long srcinc, long destinc,
218 unsigned int height,
219 int half_order)
220 {
221 long length = ROUNDUP(srclen, 4);
222 register long h, n;
223
224 srcinc -= length;
225 destinc -= length;
226 for (h = height; --h >= 0; src += srcinc, dest += destinc) {
227 if ((h == 0) && (srclen != length)) {
228 length -= 4;
229 if (half_order == MSBFirst)
230 *(dest + length) = *(src + length + 3);
231 if (((half_order == LSBFirst) && ((srclen - length) == 3)) ||
232 ((half_order == MSBFirst) && (srclen & 2)))
233 *(dest + length + 1) = *(src + length + 2);
234 if (((half_order == MSBFirst) && ((srclen - length) == 3)) ||
235 ((half_order == LSBFirst) && (srclen & 2)))
236 *(dest + length + 2) = *(src + length + 1);
237 if (half_order == LSBFirst)
238 *(dest + length + 3) = *(src + length);
239 }
240 for (n = length; n > 0; n -= 4, src += 4) {
241 *dest++ = *(src + 3);
242 *dest++ = *(src + 2);
243 *dest++ = *(src + 1);
244 *dest++ = *src;
245 }
246 }
247 }
248
249 static void
250 SwapWords (
251 register unsigned char *src,
252 register unsigned char *dest,
253 long srclen, long srcinc, long destinc,
254 unsigned int height,
255 int half_order)
256 {
257 long length = ROUNDUP(srclen, 4);
258 register long h, n;
259
260 srcinc -= length;
261 destinc -= length;
262 for (h = height; --h >= 0; src += srcinc, dest += destinc) {
263 if ((h == 0) && (srclen != length)) {
264 length -= 4;
265 if (half_order == MSBFirst)
266 *(dest + length + 1) = *(src + length + 3);
267 if (((half_order == LSBFirst) && ((srclen - length) == 3)) ||
268 ((half_order == MSBFirst) && (srclen & 2)))
269 *(dest + length) = *(src + length + 2);
270 if (((half_order == MSBFirst) && ((srclen - length) == 3)) ||
271 ((half_order == LSBFirst) && (srclen & 2)))
272 *(dest + length + 3) = *(src + length + 1);
273 if (half_order == LSBFirst)
274 *(dest + length + 2) = *(src + length);
275 }
276 for (n = length; n > 0; n -= 4, src += 2) {
277 *dest++ = *(src + 2);
278 *dest++ = *(src + 3);
279 *dest++ = *src++;
280 *dest++ = *src++;
281 }
282 }
283 }
284
285 static void
286 SwapNibbles(
287 register unsigned char *src,
288 register unsigned char *dest,
289 long srclen, long srcinc, long destinc,
290 unsigned int height)
291 {
292 register long h, n;
293 register const unsigned char *rev = _reverse_nibs;
294
295 srcinc -= srclen;
296 destinc -= srclen;
297 for (h = height; --h >= 0; src += srcinc, dest += destinc)
298 for (n = srclen; --n >= 0; )
299 *dest++ = rev[*src++];
300 }
301
302 static void
303 ShiftNibblesLeft (
304 register unsigned char *src,
305 register unsigned char *dest,
306 long srclen, long srcinc, long destinc,
307 unsigned int height,
308 int nibble_order)
309 {
310 register long h, n;
311 register unsigned char c1, c2;
312
313 srcinc -= srclen;
314 destinc -= srclen;
315 if (nibble_order == MSBFirst) {
316 for (h = height; --h >= 0; src += srcinc, dest += destinc)
317 for (n = srclen; --n >= 0; ) {
318 c1 = *src++;
319 c2 = *src;
320 *dest++ = ((c1 & 0x0f) << 4) | ((c2 & (unsigned)0xf0) >> 4);
321 }
322 } else {
323 for (h = height; --h >= 0; src += srcinc, dest += destinc)
324 for (n = srclen; --n >= 0; ) {
325 c1 = *src++;
326 c2 = *src;
327 *dest++ = ((c2 & 0x0f) << 4) | ((c1 & (unsigned)0xf0) >> 4);
328 }
329 }
330 }
331
332 /*ARGSUSED*/
333 static void
334 SwapBits(
335 register unsigned char *src,
336 register unsigned char *dest,
337 long srclen, long srcinc, long destinc,
338 unsigned int height,
339 int half_order)
340 {
341 register long h, n;
342 register const unsigned char *rev = _reverse_byte;
343
344 srcinc -= srclen;
345 destinc -= srclen;
346 for (h = height; --h >= 0; src += srcinc, dest += destinc)
347 for (n = srclen; --n >= 0; )
348 *dest++ = rev[*src++];
349 }
350
351 static void
352 SwapBitsAndTwoBytes(
353 register unsigned char *src,
354 register unsigned char *dest,
355 long srclen, long srcinc, long destinc,
356 unsigned int height,
357 int half_order)
358 {
359 long length = ROUNDUP(srclen, 2);
360 register long h, n;
361 register const unsigned char *rev = _reverse_byte;
362
363 srcinc -= length;
364 destinc -= length;
365 for (h = height; --h >= 0; src += srcinc, dest += destinc) {
366 if ((h == 0) && (srclen != length)) {
367 length -= 2;
368 if (half_order == MSBFirst)
369 *(dest + length) = rev[*(src + length + 1)];
370 else
371 *(dest + length + 1) = rev[*(src + length)];
372 }
373 for (n = length; n > 0; n -= 2, src += 2) {
374 *dest++ = rev[*(src + 1)];
375 *dest++ = rev[*src];
376 }
377 }
378 }
379
380 static void
381 SwapBitsAndFourBytes(
382 register unsigned char *src,
383 register unsigned char *dest,
384 long srclen, long srcinc, long destinc,
385 unsigned int height,
386 int half_order)
387 {
388 long length = ROUNDUP(srclen, 4);
389 register long h, n;
390 register const unsigned char *rev = _reverse_byte;
391
392 srcinc -= length;
393 destinc -= length;
394 for (h = height; --h >= 0; src += srcinc, dest += destinc) {
395 if ((h == 0) && (srclen != length)) {
396 length -= 4;
397 if (half_order == MSBFirst)
398 *(dest + length) = rev[*(src + length + 3)];
399 if (((half_order == LSBFirst) && ((srclen - length) == 3)) ||
400 ((half_order == MSBFirst) && (srclen & 2)))
401 *(dest + length + 1) = rev[*(src + length + 2)];
402 if (((half_order == MSBFirst) && ((srclen - length) == 3)) ||
403 ((half_order == LSBFirst) && (srclen & 2)))
404 *(dest + length + 2) = rev[*(src + length + 1)];
405 if (half_order == LSBFirst)
406 *(dest + length + 3) = rev[*(src + length)];
407 }
408 for (n = length; n > 0; n -= 4, src += 4) {
409 *dest++ = rev[*(src + 3)];
410 *dest++ = rev[*(src + 2)];
411 *dest++ = rev[*(src + 1)];
412 *dest++ = rev[*src];
413 }
414 }
415 }
416
417 static void
418 SwapBitsAndWords(
419 register unsigned char *src,
420 register unsigned char *dest,
421 long srclen, long srcinc, long destinc,
422 unsigned int height,
423 int half_order)
424 {
425 long length = ROUNDUP(srclen, 4);
426 register long h, n;
427 register const unsigned char *rev = _reverse_byte;
428
429 srcinc -= length;
430 destinc -= length;
431 for (h = height; --h >= 0; src += srcinc, dest += destinc) {
432 if ((h == 0) && (srclen != length)) {
433 length -= 4;
434 if (half_order == MSBFirst)
435 *(dest + length + 1) = rev[*(src + length + 3)];
436 if (((half_order == LSBFirst) && ((srclen - length) == 3)) ||
437 ((half_order == MSBFirst) && (srclen & 2)))
438 *(dest + length) = rev[*(src + length + 2)];
439 if (((half_order == MSBFirst) && ((srclen - length) == 3)) ||
440 ((half_order == LSBFirst) && (srclen & 2)))
441 *(dest + length + 3) = rev[*(src + length + 1)];
442 if (half_order == LSBFirst)
443 *(dest + length + 2) = rev[*(src + length)];
444 }
445 for (n = length; n > 0; n -= 4, src += 2) {
446 *dest++ = rev[*(src + 2)];
447 *dest++ = rev[*(src + 3)];
448 *dest++ = rev[*src++];
449 *dest++ = rev[*src++];
450 }
451 }
452 }
453
454 /*
455
456 The following table gives the bit ordering within bytes (when accessed
457 sequentially) for a scanline containing 32 bits, with bits numbered 0 to
458 31, where bit 0 should be leftmost on the display. For a given byte
459 labelled A-B, A is for the most significant bit of the byte, and B is
460 for the least significant bit.
461
462 legend:
463 1 scanline-unit = 8
464 2 scanline-unit = 16
465 4 scanline-unit = 32
466 M byte-order = MostSignificant
467 L byte-order = LeastSignificant
468 m bit-order = MostSignificant
469 l bit-order = LeastSignificant
470
471
472 format ordering
473
474 1Mm 00-07 08-15 16-23 24-31
475 2Mm 00-07 08-15 16-23 24-31
476 4Mm 00-07 08-15 16-23 24-31
477 1Ml 07-00 15-08 23-16 31-24
478 2Ml 15-08 07-00 31-24 23-16
479 4Ml 31-24 23-16 15-08 07-00
480 1Lm 00-07 08-15 16-23 24-31
481 2Lm 08-15 00-07 24-31 16-23
482 4Lm 24-31 16-23 08-15 00-07
483 1Ll 07-00 15-08 23-16 31-24
484 2Ll 07-00 15-08 23-16 31-24
485 4Ll 07-00 15-08 23-16 31-24
486
487
488 The following table gives the required conversion between any two
489 formats. It is based strictly on the table above. If you believe one,
490 you should believe the other.
491
492 legend:
493 n no changes
494 s reverse 8-bit units within 16-bit units
495 l reverse 8-bit units within 32-bit units
496 w reverse 16-bit units within 32-bit units
497 R reverse bits within 8-bit units
498 S s+R
499 L l+R
500 W w+R
501
502 */
503
504 static void (* RConst (SwapFunction[12][12]))(
505 register unsigned char *src,
506 register unsigned char *dest,
507 long srclen,
508 long srcinc,
509 long destinc,
510 unsigned int height,
511 int half_order) = {
512 #define n NoSwap,
513 #define s SwapTwoBytes,
514 #define l SwapFourBytes,
515 #define w SwapWords,
516 #define R SwapBits,
517 #define S SwapBitsAndTwoBytes,
518 #define L SwapBitsAndFourBytes,
519 #define W SwapBitsAndWords,
520
521 /* 1Mm 2Mm 4Mm 1Ml 2Ml 4Ml 1Lm 2Lm 4Lm 1Ll 2Ll 4Ll */
522 /* 1Mm */ { n n n R S L n s l R R R },
523 /* 2Mm */ { n n n R S L n s l R R R },
524 /* 4Mm */ { n n n R S L n s l R R R },
525 /* 1Ml */ { R R R n s l R S L n n n },
526 /* 2Ml */ { S S S s n w S R W s s s },
527 /* 4Ml */ { L L L l w n L W R l l l },
528 /* 1Lm */ { n n n R S L n s l R R R },
529 /* 2Lm */ { s s s S R W s n w S S S },
530 /* 4Lm */ { l l l L W R l w n L L L },
531 /* 1Ll */ { R R R n s l R S L n n n },
532 /* 2Ll */ { R R R n s l R S L n n n },
533 /* 4Ll */ { R R R n s l R S L n n n }
534
535 #undef n
536 #undef s
537 #undef l
538 #undef w
539 #undef R
540 #undef S
541 #undef L
542 #undef W
543
544 };
545
546 /* Of course, the table above is a lie. We also need to factor in the
547 * order of the source data to cope with swapping half of a unit at the
548 * end of a scanline, since we are trying to avoid de-ref'ing off the
549 * end of the source.
550 *
551 * Defines whether the first half of a unit has the first half of the data
552 */
553 static int const HalfOrder[12] = {
554 LSBFirst, /* 1Mm */
555 LSBFirst, /* 2Mm */
556 LSBFirst, /* 4Mm */
557 LSBFirst, /* 1Ml */
558 MSBFirst, /* 2Ml */
559 MSBFirst, /* 4Ml */
560 LSBFirst, /* 1Lm */
561 MSBFirst, /* 2Lm */
562 MSBFirst, /* 4Lm */
563 LSBFirst, /* 1Ll */
564 LSBFirst, /* 2Ll */
565 LSBFirst /* 4Ll */
566 };
567
568 /* Finally, for SwapWords cases, the half order depends not just on the source
569 * but also on the destination scanline unit. Use of this table changes some
570 * MSBFirsts to LSBFirsts that are "do not care" (because the function will be
571 * NoSwap or SwapBits) in addition to changing the desired ones.
572 */
573
574 static int const HalfOrderWord[12] = {
575 MSBFirst, /* 1Mm */
576 MSBFirst, /* 2Mm */
577 MSBFirst, /* 4Mm */
578 MSBFirst, /* 1Ml */
579 MSBFirst, /* 2Ml */
580 LSBFirst, /* 4Ml */
581 MSBFirst, /* 1Lm */
582 MSBFirst, /* 2Lm */
583 LSBFirst, /* 4Lm */
584 MSBFirst, /* 1Ll */
585 MSBFirst, /* 2Ll */
586 MSBFirst /* 4Ll */
587 };
588
589 /*
590 * This macro creates a value from 0 to 11 suitable for indexing
591 * into the table above.
592 */
593 #define ComposeIndex(bitmap_unit, bitmap_bit_order, byte_order) \
594 (((bitmap_unit == 32) ? 2 : ((bitmap_unit == 16) ? 1 : 0)) \
595 + (((bitmap_bit_order == MSBFirst) ? 0 : 3) \
596 + ((byte_order == MSBFirst) ? 0 : 6)))
597
598 /* Cancel a GetReq operation, before doing _XSend or Data */
599
600 #define UnGetReq(name)\
601 dpy->bufptr -= SIZEOF(x##name##Req);\
602 X_DPY_REQUEST_DECREMENT(dpy)
603
604 static void
605 SendXYImage(
606 register Display *dpy,
607 register xPutImageReq *req,
608 register XImage *image,
609 int req_xoffset, int req_yoffset)
610 {
611 register int j;
612 long total_xoffset, bytes_per_src, bytes_per_dest, length;
613 long bytes_per_line, bytes_per_src_plane, bytes_per_dest_plane;
614 char *src, *dest, *buf;
615 char *extra = (char *)NULL;
616 register void (*swapfunc)(
617 register unsigned char *src,
618 register unsigned char *dest,
619 long srclen,
620 long srcinc,
621 long destinc,
622 unsigned int height,
623 int half_order);
624 int half_order;
625
626 total_xoffset = image->xoffset + req_xoffset;
627 req->leftPad = total_xoffset & (dpy->bitmap_unit - 1);
628 total_xoffset = (unsigned)(total_xoffset - req->leftPad) >> 3;
629 /* The protocol requires left-pad of zero on all ZPixmap, even
630 * though the 1-bit case is identical to bitmap format. This is a
631 * bug in the protocol, caused because 1-bit ZPixmap was added late
632 * in the game. Hairy shifting code compensation isn't worth it,
633 * just use XYPixmap format instead.
634 */
635 if ((req->leftPad != 0) && (req->format == ZPixmap))
636 req->format = XYPixmap;
637 bytes_per_dest = (unsigned long)ROUNDUP((long)req->width + req->leftPad,
638 dpy->bitmap_pad) >> 3;
639 bytes_per_dest_plane = bytes_per_dest * req->height;
640 length = bytes_per_dest_plane * image->depth;
641 req->length += (length + 3) >> 2;
642
643 swapfunc = SwapFunction[ComposeIndex(image->bitmap_unit,
644 image->bitmap_bit_order,
645 image->byte_order)]
646 [ComposeIndex(dpy->bitmap_unit,
647 dpy->bitmap_bit_order,
648 dpy->byte_order)];
649 half_order = HalfOrder[ComposeIndex(image->bitmap_unit,
650 image->bitmap_bit_order,
651 image->byte_order)];
652 if (half_order == MSBFirst)
653 half_order = HalfOrderWord[ComposeIndex(dpy->bitmap_unit,
654 dpy->bitmap_bit_order,
655 dpy->byte_order)];
656
657 src = image->data + (image->bytes_per_line * req_yoffset) + total_xoffset;
658
659 /* when total_xoffset > 0, we have to worry about stepping off the
660 * end of image->data.
661 */
662 if ((swapfunc == NoSwap) &&
663 (image->bytes_per_line == bytes_per_dest) &&
664 (((total_xoffset == 0) &&
665 ((image->depth == 1) || (image->height == req->height))) ||
666 ((image->depth == 1) &&
667 ((req_yoffset + req->height) < (unsigned)image->height)))) {
668 Data(dpy, src, length);
669 return;
670 }
671
672 length = ROUNDUP(length, 4);
673 if ((dpy->bufptr + length) > dpy->bufmax) {
674 if ((buf = _XAllocScratch(dpy, length)) == NULL) {
675 UnGetReq(PutImage);
676 return;
677 }
678 }
679 else
680 buf = dpy->bufptr;
681
682 bytes_per_src = (req->width + req->leftPad + (unsigned)7) >> 3;
683 bytes_per_line = image->bytes_per_line;
684 bytes_per_src_plane = bytes_per_line * image->height;
685 total_xoffset &= (image->bitmap_unit - 1) >> 3;
686
687 if ((total_xoffset > 0) &&
688 (image->byte_order != image->bitmap_bit_order)) {
689 char *temp;
690 long bytes_per_temp_plane, temp_length;
691
692 bytes_per_line = bytes_per_src + total_xoffset;
693 src -= total_xoffset;
694 bytes_per_temp_plane = bytes_per_line * req->height;
695 temp_length = ROUNDUP(bytes_per_temp_plane * image->depth, 4);
696 if (buf == dpy->bufptr) {
697 if (! (temp = _XAllocScratch(dpy, temp_length))) {
698 UnGetReq(PutImage);
699 return;
700 }
701 }
702 else
703 if ((extra = temp = Xmalloc(temp_length)) == NULL) {
704 UnGetReq(PutImage);
705 return;
706 }
707
708 swapfunc = SwapFunction[ComposeIndex(image->bitmap_unit,
709 image->bitmap_bit_order,
710 image->byte_order)]
711 [ComposeIndex(image->bitmap_unit,
712 dpy->byte_order,
713 dpy->byte_order)];
714 for (dest = temp, j = image->depth;
715 --j >= 0;
716 src += bytes_per_src_plane, dest += bytes_per_temp_plane)
717 (*swapfunc)((unsigned char *)src, (unsigned char *)dest,
718 bytes_per_line, (long)image->bytes_per_line,
719 bytes_per_line, req->height, half_order);
720 swapfunc = SwapFunction[ComposeIndex(image->bitmap_unit,
721 dpy->byte_order,
722 dpy->byte_order)]
723 [ComposeIndex(dpy->bitmap_unit,
724 dpy->bitmap_bit_order,
725 dpy->byte_order)];
726 half_order = HalfOrder[ComposeIndex(image->bitmap_unit,
727 dpy->byte_order,
728 dpy->byte_order)];
729 src = temp + total_xoffset;
730 bytes_per_src_plane = bytes_per_temp_plane;
731 }
732
733 for (dest = buf, j = image->depth;
734 --j >= 0;
735 src += bytes_per_src_plane, dest += bytes_per_dest_plane)
736 (*swapfunc)((unsigned char *)src, (unsigned char *)dest,
737 bytes_per_src, bytes_per_line,
738 bytes_per_dest, req->height, half_order);
739
740 Xfree(extra);
741
742 if (buf == dpy->bufptr)
743 dpy->bufptr += length;
744 else
745 _XSend(dpy, buf, length);
746 }
747
748 static void
749 SendZImage(
750 register Display *dpy,
751 register xPutImageReq *req,
752 register XImage *image,
753 int req_xoffset, int req_yoffset,
754 int dest_bits_per_pixel, int dest_scanline_pad)
755 {
756 long bytes_per_src, bytes_per_dest, length;
757 unsigned char *src, *dest;
758 unsigned char *shifted_src = NULL;
759
760 req->leftPad = 0;
761 bytes_per_src = ROUNDUP((long)req->width * image->bits_per_pixel, 8) >> 3;
762 bytes_per_dest = ROUNDUP((long)req->width * dest_bits_per_pixel,
763 dest_scanline_pad) >> 3;
764 length = bytes_per_dest * req->height;
765 req->length += (length + 3) >> 2;
766
767 src = (unsigned char *)image->data +
768 (req_yoffset * image->bytes_per_line) +
769 ((req_xoffset * image->bits_per_pixel) >> 3);
770 if ((image->bits_per_pixel == 4) && ((unsigned int) req_xoffset & 0x01)) {
771 if (! (shifted_src = Xmallocarray(req->height, image->bytes_per_line))) {
772 UnGetReq(PutImage);
773 return;
774 }
775
776 ShiftNibblesLeft(src, shifted_src, bytes_per_src,
777 (long) image->bytes_per_line,
778 (long) image->bytes_per_line, req->height,
779 image->byte_order);
780 src = shifted_src;
781 }
782
783 /* when req_xoffset > 0, we have to worry about stepping off the
784 * end of image->data.
785 */
786 if (((image->byte_order == dpy->byte_order) ||
787 (image->bits_per_pixel == 8)) &&
788 ((long)image->bytes_per_line == bytes_per_dest) &&
789 ((req_xoffset == 0) ||
790 ((req_yoffset + req->height) < (unsigned)image->height))) {
791 Data(dpy, (char *)src, length);
792 Xfree(shifted_src);
793 return;
794 }
795
796 length = ROUNDUP(length, 4);
797 if ((dpy->bufptr + length) <= dpy->bufmax)
798 dest = (unsigned char *)dpy->bufptr;
799 else
800 if ((dest = (unsigned char *)
801 _XAllocScratch(dpy, length)) == NULL) {
802 Xfree(shifted_src);
803 UnGetReq(PutImage);
804 return;
805 }
806
807 if ((image->byte_order == dpy->byte_order) ||
808 (image->bits_per_pixel == 8))
809 NoSwap(src, dest, bytes_per_src, (long)image->bytes_per_line,
810 bytes_per_dest, req->height, image->byte_order);
811 else if (image->bits_per_pixel == 32)
812 SwapFourBytes(src, dest, bytes_per_src, (long)image->bytes_per_line,
813 bytes_per_dest, req->height, image->byte_order);
814 else if (image->bits_per_pixel == 24)
815 SwapThreeBytes(src, dest, bytes_per_src, (long)image->bytes_per_line,
816 bytes_per_dest, req->height, image->byte_order);
817 else if (image->bits_per_pixel == 16)
818 SwapTwoBytes(src, dest, bytes_per_src, (long)image->bytes_per_line,
819 bytes_per_dest, req->height, image->byte_order);
820 else
821 SwapNibbles(src, dest, bytes_per_src, (long)image->bytes_per_line,
822 bytes_per_dest, req->height);
823
824 if (dest == (unsigned char *)dpy->bufptr)
825 dpy->bufptr += length;
826 else
827 _XSend(dpy, (char *)dest, length);
828
829 Xfree(shifted_src);
830 }
831
832 static void
833 PutImageRequest(
834 register Display *dpy,
835 Drawable d,
836 GC gc,
837 register XImage *image,
838 int req_xoffset, int req_yoffset,
839 int x, int y,
840 unsigned int req_width, unsigned int req_height,
841 int dest_bits_per_pixel, int dest_scanline_pad)
842 {
843 register xPutImageReq *req;
844
845 GetReq(PutImage, req);
846 req->drawable = d;
847 req->gc = gc->gid;
848 req->dstX = x;
849 req->dstY = y;
850 req->width = req_width;
851 req->height = req_height;
852 req->depth = image->depth;
853 req->format = image->format;
854 if ((image->bits_per_pixel == 1) || (image->format != ZPixmap))
855 SendXYImage(dpy, req, image, req_xoffset, req_yoffset);
856 else
857 SendZImage(dpy, req, image, req_xoffset, req_yoffset,
858 dest_bits_per_pixel, dest_scanline_pad);
859 }
860
861 static void
862 PutSubImage (
863 register Display *dpy,
864 Drawable d,
865 GC gc,
866 register XImage *image,
867 int req_xoffset,
868 int req_yoffset,
869 int x, int y,
870 unsigned int req_width,
871 unsigned int req_height,
872 int dest_bits_per_pixel,
873 int dest_scanline_pad)
874 {
875 int left_pad, BytesPerRow, Available;
876
877 if ((req_width == 0) || (req_height == 0))
878 return;
879
880 Available = ((65536 < dpy->max_request_size) ? (65536 << 2)
881 : (dpy->max_request_size << 2))
882 - SIZEOF(xPutImageReq);
883
884 if ((image->bits_per_pixel == 1) || (image->format != ZPixmap)) {
885 left_pad = (image->xoffset + req_xoffset) & (dpy->bitmap_unit - 1);
886 BytesPerRow = (ROUNDUP((long)req_width + left_pad,
887 dpy->bitmap_pad) >> 3) * image->depth;
888 } else {
889 left_pad = 0;
890 BytesPerRow = ROUNDUP((long)req_width * dest_bits_per_pixel,
891 dest_scanline_pad) >> 3;
892 }
893
894 if ((BytesPerRow * req_height) <= Available) {
895 PutImageRequest(dpy, d, gc, image, req_xoffset, req_yoffset, x, y,
896 req_width, req_height,
897 dest_bits_per_pixel, dest_scanline_pad);
898 } else if (req_height > 1) {
899 int SubImageHeight = Available / BytesPerRow;
900
901 if (SubImageHeight == 0)
902 SubImageHeight = 1;
903
904 PutSubImage(dpy, d, gc, image, req_xoffset, req_yoffset, x, y,
905 req_width, (unsigned int) SubImageHeight,
906 dest_bits_per_pixel, dest_scanline_pad);
907
908 PutSubImage(dpy, d, gc, image, req_xoffset,
909 req_yoffset + SubImageHeight, x, y + SubImageHeight,
910 req_width, req_height - SubImageHeight,
911 dest_bits_per_pixel, dest_scanline_pad);
912 } else {
913 int SubImageWidth = (((Available << 3) / dest_scanline_pad)
914 * dest_scanline_pad) - left_pad;
915
916 PutSubImage(dpy, d, gc, image, req_xoffset, req_yoffset, x, y,
917 (unsigned int) SubImageWidth, 1,
918 dest_bits_per_pixel, dest_scanline_pad);
919
920 PutSubImage(dpy, d, gc, image, req_xoffset + SubImageWidth,
921 req_yoffset, x + SubImageWidth, y,
922 req_width - SubImageWidth, 1,
923 dest_bits_per_pixel, dest_scanline_pad);
924 }
925 }
926
927
928 int
929 XPutImage (
930 register Display *dpy,
931 Drawable d,
932 GC gc,
933 register XImage *image,
934 int req_xoffset,
935 int req_yoffset,
936 int x,
937 int y,
938 unsigned int req_width,
939 unsigned int req_height)
940
941 {
942 long width = req_width;
943 long height = req_height;
944 int dest_bits_per_pixel, dest_scanline_pad;
945
946 if (req_xoffset < 0) {
947 width += req_xoffset;
948 req_xoffset = 0;
949 }
950 if (req_yoffset < 0) {
951 height += req_yoffset;
952 req_yoffset = 0;
953 }
954 if ((req_xoffset + width) > image->width)
955 width = image->width - req_xoffset;
956 if ((req_yoffset + height) > image->height)
957 height = image->height - req_yoffset;
958 if ((width <= 0) || (height <= 0))
959 return 0;
960
961 if ((image->bits_per_pixel == 1) || (image->format != ZPixmap)) {
962 dest_bits_per_pixel = 1;
963 dest_scanline_pad = dpy->bitmap_pad;
964 } else {
965 register int n;
966 register ScreenFormat *format;
967
968 dest_bits_per_pixel = image->bits_per_pixel;
969 dest_scanline_pad = image->bitmap_pad;
970 for (n = dpy->nformats, format = dpy->pixmap_format; --n >= 0; format++)
971 if (format->depth == image->depth) {
972 dest_bits_per_pixel = format->bits_per_pixel;
973 dest_scanline_pad = format->scanline_pad;
974 }
975 if (dest_bits_per_pixel != image->bits_per_pixel) {
976 XImage img;
977 register long i, j;
978 /* XXX slow, but works */
979 img.width = width;
980 img.height = height;
981 img.xoffset = 0;
982 img.format = ZPixmap;
983 img.byte_order = dpy->byte_order;
984 img.bitmap_unit = dpy->bitmap_unit;
985 img.bitmap_bit_order = dpy->bitmap_bit_order;
986 img.bitmap_pad = dest_scanline_pad;
987 img.depth = image->depth;
988 img.bits_per_pixel = dest_bits_per_pixel;
989 img.bytes_per_line = ROUNDUP((dest_bits_per_pixel * width),
990 dest_scanline_pad) >> 3;
991 img.data = Xmallocarray(height, img.bytes_per_line);
992 if (img.data == NULL)
993 return 0;
994 _XInitImageFuncPtrs(&img);
995 for (j = height; --j >= 0; )
996 for (i = width; --i >= 0; )
997 XPutPixel(&img, i, j, XGetPixel(image, req_xoffset + i,
998 req_yoffset + j));
999 LockDisplay(dpy);
1000 FlushGC(dpy, gc);
1001 PutSubImage(dpy, d, gc, &img, 0, 0, x, y,
1002 (unsigned int) width, (unsigned int) height,
1003 dest_bits_per_pixel, dest_scanline_pad);
1004 UnlockDisplay(dpy);
1005 SyncHandle();
1006 Xfree(img.data);
1007 return 0;
1008 }
1009 }
1010
1011 LockDisplay(dpy);
1012 FlushGC(dpy, gc);
1013
1014 PutSubImage(dpy, d, gc, image, req_xoffset, req_yoffset, x, y,
1015 (unsigned int) width, (unsigned int) height,
1016 dest_bits_per_pixel, dest_scanline_pad);
1017
1018 UnlockDisplay(dpy);
1019 SyncHandle();
1020 #ifdef USE_DYNAMIC_XCURSOR
1021 if (image->bits_per_pixel == 1 &&
1022 x == 0 && y == 0 &&
1023 width == image->width && height == image->height &&
1024 gc->values.function == GXcopy &&
1025 (gc->values.plane_mask & 1))
1026 {
1027 _XNoticePutBitmap (dpy, d, image);
1028 }
1029 #endif
1030 return 0;
1031 }