]> Pileus Git - ~andy/gtk/blob - contrib/gdk-pixbuf-xlib/gdk-pixbuf-xlib-drawable.c
da9dae2f802629411c2e647082576d6a6ca4a3f5
[~andy/gtk] / contrib / gdk-pixbuf-xlib / gdk-pixbuf-xlib-drawable.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - convert X drawable information to RGB
3  *
4  * Copyright (C) 1999 Michael Zucchi
5  *
6  * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
7  *          Cody Russell <bratsche@dfw.net>
8  *          Federico Mena-Quintero <federico@gimp.org>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 /* Ported to Xlib by John Harper <john@dcs.warwick.ac.uk> */
27
28
29 #include <config.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <gdk-pixbuf/gdk-pixbuf-private.h>
33 #include "gdk-pixbuf-xlib-private.h"
34 #include <X11/Xlib.h>
35 #include <X11/Xutil.h>
36
37 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
38 #define LITTLE
39 #endif
40 #define d(x)
41
42 \f
43
44 static guint32 mask_table[] = {
45         0x00000000, 0x00000001, 0x00000003, 0x00000007,
46         0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
47         0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
48         0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
49         0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
50         0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
51         0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
52         0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
53         0xffffffff
54 };
55
56 \f
57 /* color handling */
58
59 typedef struct xlib_colormap_struct xlib_colormap;
60 struct xlib_colormap_struct {
61         int size;
62         XColor *colors;
63         Visual *visual;
64         Colormap colormap;
65 };
66
67 static xlib_colormap *
68 xlib_get_colormap (Colormap id, Visual *visual)
69 {
70         int i;
71         xlib_colormap *xc = g_new (xlib_colormap, 1);
72
73         xc->size = visual->map_entries;
74         xc->colors = g_new (XColor, xc->size);
75         xc->visual = visual;
76         xc->colormap = id;
77
78         for (i = 0; i < xc->size; i++) {
79                 xc->colors[i].pixel = i;
80                 xc->colors[i].flags = DoRed | DoGreen | DoBlue;
81         }
82
83         XQueryColors (gdk_pixbuf_dpy, xc->colormap, xc->colors, xc->size);
84
85         return xc;
86 }
87
88 static void
89 xlib_colormap_free (xlib_colormap *xc)
90 {
91         g_free (xc->colors);
92         g_free (xc);
93 }
94
95 /* from gdkvisual.c */
96 static void
97 visual_decompose_mask (gulong  mask,
98                        gint   *shift,
99                        gint   *prec)
100 {
101         *shift = 0;
102         *prec = 0;
103
104         while (!(mask & 0x1)) {
105                 (*shift)++;
106                 mask >>= 1;
107         }
108
109         while (mask & 0x1) {
110                 (*prec)++;
111                 mask >>= 1;
112         }
113 }
114
115 static gboolean x_error;
116
117 static int
118 handle_x_error (Display *dpy, XErrorEvent *ev)
119 {
120         x_error = TRUE;
121         return 0;
122 }
123
124 static gboolean
125 drawable_is_pixmap (Drawable d)
126 {
127         /* copied from Imlib */
128
129         XErrorHandler errh;
130         XWindowAttributes wa;
131         gboolean is_pixmap;
132
133         errh = XSetErrorHandler (handle_x_error);
134         x_error = FALSE;
135         XGetWindowAttributes (gdk_pixbuf_dpy, d, &wa);
136         XSync (gdk_pixbuf_dpy, False);
137         is_pixmap = x_error;
138         XSetErrorHandler (errh);
139
140         return is_pixmap;
141 }
142
143 \f
144
145 /*
146   convert 1 bits-pixel data
147   no alpha
148 */
149 static void
150 rgb1 (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
151 {
152         int xx, yy;
153         int width, height;
154         int bpl;
155         guint8 *s;
156         register guint8 data;
157         guint8 *o;
158         guint8 *srow = image->data, *orow = pixels;
159
160         d (printf ("1 bits/pixel\n"));
161
162         /* convert upto 8 pixels/time */
163         /* its probably not worth trying to make this run very fast, who uses
164            1 bit displays anymore? */
165         width = image->width;
166         height = image->height;
167         bpl = image->bytes_per_line;
168
169         for (yy = 0; yy < height; yy++) {
170                 s = srow;
171                 o = orow;
172
173                 for (xx = 0; xx < width; xx ++) {
174                         data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;
175                         *o++ = colormap->colors[data].red;
176                         *o++ = colormap->colors[data].green;
177                         *o++ = colormap->colors[data].blue;
178                 }
179                 srow += bpl;
180                 orow += rowstride;
181         }
182 }
183
184 /*
185   convert 1 bits/pixel data
186   with alpha
187 */
188 static void
189 rgb1a (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
190 {
191         int xx, yy;
192         int width, height;
193         int bpl;
194         guint8 *s;
195         register guint8 data;
196         guint8 *o;
197         guint8 *srow = image->data, *orow = pixels;
198         guint32 remap[2];
199
200         d (printf ("1 bits/pixel\n"));
201
202         /* convert upto 8 pixels/time */
203         /* its probably not worth trying to make this run very fast, who uses
204            1 bit displays anymore? */
205         width = image->width;
206         height = image->height;
207         bpl = image->bytes_per_line;
208
209         for (xx = 0; xx < 2; xx++) {
210 #ifdef LITTLE
211                 remap[xx] = 0xff000000
212                         | colormap->colors[xx].blue << 16
213                         | colormap->colors[xx].green << 8
214                         | colormap->colors[xx].red;
215 #else
216                 remap[xx] = 0xff
217                         | colormap->colors[xx].red << 24
218                         | colormap->colors[xx].green << 16
219                         | colormap->colors[xx].blue << 8;
220 #endif
221         }
222
223         for (yy = 0; yy < height; yy++) {
224                 s = srow;
225                 o = orow;
226
227                 for (xx = 0; xx < width; xx ++) {
228                         data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;
229                         *o++ = remap[data];
230                 }
231                 srow += bpl;
232                 orow += rowstride;
233         }
234 }
235
236 /*
237   convert 8 bits/pixel data
238   no alpha
239 */
240 static void
241 rgb8 (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
242 {
243         int xx, yy;
244         int width, height;
245         int bpl;
246         guint32 mask;
247         register guint32 data;
248         guint8 *srow = image->data, *orow = pixels;
249         register guint8 *s;
250         register guint8 *o;
251
252         width = image->width;
253         height = image->height;
254         bpl = image->bytes_per_line;
255
256         d (printf ("8 bit, no alpha output\n"));
257
258         mask = mask_table[image->depth];
259
260         for (yy = 0; yy < height; yy++) {
261                 s = srow;
262                 o = orow;
263                 for (xx = 0; xx < width; xx++) {
264                         data = *s++ & mask;
265                         *o++ = colormap->colors[data].red;
266                         *o++ = colormap->colors[data].green;
267                         *o++ = colormap->colors[data].blue;
268                 }
269                 srow += bpl;
270                 orow += rowstride;
271         }
272 }
273
274 /*
275   convert 8 bits/pixel data
276   with alpha
277 */
278 static void
279 rgb8a (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
280 {
281         int xx, yy;
282         int width, height;
283         int bpl;
284         guint32 mask;
285         register guint32 data;
286         guint32 remap[256];
287         register guint8 *s;     /* read 2 pixels at once */
288         register guint32 *o;
289         guint8 *srow = image->data, *orow = pixels;
290
291         width = image->width;
292         height = image->height;
293         bpl = image->bytes_per_line;
294
295         d (printf ("8 bit, with alpha output\n"));
296
297         mask = mask_table[image->depth];
298
299         for (xx = 0; xx < colormap->size; xx++) {
300 #ifdef LITTLE
301                 remap[xx] = 0xff000000
302                         | colormap->colors[xx].blue << 16
303                         | colormap->colors[xx].green << 8
304                         | colormap->colors[xx].red;
305 #else
306                 remap[xx] = 0xff
307                         | colormap->colors[xx].red << 24
308                         | colormap->colors[xx].green << 16
309                         | colormap->colors[xx].blue << 8;
310 #endif
311         }
312
313         for (yy = 0; yy < height; yy++) {
314                 s = srow;
315                 o = (guint32 *) orow;
316                 for (xx = 0; xx < width; xx ++) {
317                         data = *s++ & mask;
318                         *o++ = remap[data];
319                 }
320                 srow += bpl;
321                 orow += rowstride;
322         }
323 }
324
325 /*
326   convert 16 bits/pixel data
327   no alpha
328   data in lsb format
329 */
330 static void
331 rgb565lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
332 {
333         int xx, yy;
334         int width, height;
335         int bpl;
336
337 #ifdef LITTLE
338         register guint32 *s;    /* read 2 pixels at once */
339 #else
340         register guint8 *s;     /* read 2 pixels at once */
341 #endif
342         register guint16 *o;
343         guint8 *srow = image->data, *orow = pixels;
344
345         width = image->width;
346         height = image->height;
347         bpl = image->bytes_per_line;
348
349         for (yy = 0; yy < height; yy++) {
350 #ifdef LITTLE
351                 s = (guint32 *) srow;
352 #else
353                 s = srow;
354 #endif
355                 o = (guint16 *) orow;
356                 for (xx = 1; xx < width; xx += 2) {
357                         register guint32 data;
358 #ifdef LITTLE
359                         data = *s++;
360                         *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
361                                 | (data & 0x7e0) << 5 | (data & 0x600) >> 1;
362                         *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
363                                 | (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;
364                         *o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25
365                                 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
366 #else
367                         /* swap endianness first */
368                         data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
369                         s += 4;
370                         *o++ = (data & 0xf800) | (data & 0xe000) >> 5
371                                 | (data & 0x7e0) >> 3 | (data & 0x600) >> 9;
372                         *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
373                                 | (data & 0xf8000000) >> 24 | (data & 0xe0000000) >> 29;
374                         *o++ = (data & 0x7e00000) >> 11 | (data & 0x6000000) >> 17
375                                 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
376 #endif
377                 }
378                 /* check for last remaining pixel */
379                 if (width & 1) {
380                         register guint16 data;
381 #ifdef LITTLE
382                         data = *((short *) s);
383 #else
384                         data = *((short *) s);
385                         data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
386 #endif
387                         ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
388                         ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
389                         ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
390                 }
391                 srow += bpl;
392                 orow += rowstride;
393         }
394 }
395
396 /*
397   convert 16 bits/pixel data
398   no alpha
399   data in msb format
400 */
401 static void
402 rgb565msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
403 {
404         int xx, yy;
405         int width, height;
406         int bpl;
407
408 #ifdef LITTLE
409         register guint8 *s;     /* need to swap data order */
410 #else
411         register guint32 *s;    /* read 2 pixels at once */
412 #endif
413         register guint16 *o;
414         guint8 *srow = image->data, *orow = pixels;
415
416         width = image->width;
417         height = image->height;
418         bpl = image->bytes_per_line;
419
420         for (yy = 0; yy < height; yy++) {
421 #ifdef LITTLE
422                 s = srow;
423 #else
424                 s = (guint32 *) srow;
425 #endif
426                 o = (guint16 *) orow;
427                 for (xx = 1; xx < width; xx += 2) {
428                         register guint32 data;
429 #ifdef LITTLE
430                         /* swap endianness first */
431                         data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
432                         s += 4;
433                         *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
434                                 | (data & 0x7e0) << 5 | (data & 0x600) >> 1;
435                         *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
436                                 | (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;
437                         *o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25
438                                 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
439 #else
440                         data = *s++;
441                         *o++ = (data & 0xf800) | (data & 0xe000) >> 5
442                                 | (data & 0x7e0) >> 3 | (data & 0x600) >> 9;
443                         *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
444                                 | (data & 0xf8000000) >> 24 | (data & 0xe0000000) >> 29;
445                         *o++ = (data & 0x7e00000) >> 11 | (data & 0x6000000) >> 17
446                                 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
447 #endif
448                 }
449                 /* check for last remaining pixel */
450                 if (width & 1) {
451                         register guint16 data;
452 #ifdef LITTLE
453                         data = *((short *) s);
454                         data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
455 #else
456                         data = *((short *) s);
457 #endif
458                         ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
459                         ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
460                         ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
461                 }
462                 srow += bpl;
463                 orow += rowstride;
464         }
465 }
466
467 /*
468   convert 16 bits/pixel data
469   with alpha
470   data in lsb format
471 */
472 static void
473 rgb565alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
474 {
475         int xx, yy;
476         int width, height;
477         int bpl;
478
479 #ifdef LITTLE
480         register guint16 *s;    /* read 1 pixels at once */
481 #else
482         register guint8 *s;
483 #endif
484         register guint32 *o;
485
486         guint8 *srow = image->data, *orow = pixels;
487
488         width = image->width;
489         height = image->height;
490         bpl = image->bytes_per_line;
491
492         for (yy = 0; yy < height; yy++) {
493 #ifdef LITTLE
494                 s = (guint16 *) srow;
495 #else
496                 s = (guint8 *) srow;
497 #endif
498                 o = (guint32 *) orow;
499                 for (xx = 0; xx < width; xx ++) {
500                         register guint32 data;
501                         /*  rrrrrggg gggbbbbb -> rrrrrRRR ggggggGG bbbbbBBB aaaaaaaa */
502                         /*  little endian: aaaaaaaa bbbbbBBB ggggggGG rrrrrRRR */
503 #ifdef LITTLE
504                         data = *s++;
505                         *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
506                                 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
507                                 | (data & 0x1f) << 19 | (data & 0x1c) << 14
508                                 | 0xff000000;
509 #else
510                         /* swap endianness first */
511                         data = s[0] | s[1] << 8;
512                         s += 2;
513                         *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
514                                 | (data & 0x7e0) << 13 | (data & 0x600) << 7
515                                 | (data & 0x1f) << 11 | (data & 0x1c) << 6
516                                 | 0xff;
517 #endif
518                 }
519                 srow += bpl;
520                 orow += rowstride;
521         }
522 }
523
524 /*
525   convert 16 bits/pixel data
526   with alpha
527   data in msb format
528 */
529 static void
530 rgb565amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
531 {
532         int xx, yy;
533         int width, height;
534         int bpl;
535
536 #ifdef LITTLE
537         register guint8 *s;
538 #else
539         register guint16 *s;    /* read 1 pixels at once */
540 #endif
541         register guint32 *o;
542
543         guint8 *srow = image->data, *orow = pixels;
544
545         width = image->width;
546         height = image->height;
547         bpl = image->bytes_per_line;
548
549         for (yy = 0; yy < height; yy++) {
550 #ifdef LITTLE
551                 s = srow;
552 #else
553                 s = (guint16 *) srow;
554 #endif
555                 o = (guint32 *) orow;
556                 for (xx = 0; xx < width; xx ++) {
557                         register guint32 data;
558                         /*  rrrrrggg gggbbbbb -> rrrrrRRR gggggg00 bbbbbBBB aaaaaaaa */
559                         /*  little endian: aaaaaaaa bbbbbBBB gggggg00 rrrrrRRR */
560 #ifdef LITTLE
561                         /* swap endianness first */
562                         data = s[0] | s[1] << 8;
563                         s += 2;
564                         *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
565                                 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
566                                 | (data & 0x1f) << 19 | (data & 0x1c) << 14
567                                 | 0xff000000;
568 #else
569                         data = *s++;
570                         *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
571                                 | (data & 0x7e0) << 13 | (data & 0x600) << 7
572                                 | (data & 0x1f) << 11 | (data & 0x1c) << 6
573                                 | 0xff;
574 #endif
575                 }
576                 srow += bpl;
577                 orow += rowstride;
578         }
579 }
580
581 /*
582   convert 15 bits/pixel data
583   no alpha
584   data in lsb format
585 */
586 static void
587 rgb555lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
588 {
589         int xx, yy;
590         int width, height;
591         int bpl;
592
593 #ifdef LITTLE
594         register guint32 *s;    /* read 2 pixels at once */
595 #else
596         register guint8 *s;     /* read 2 pixels at once */
597 #endif
598         register guint16 *o;
599         guint8 *srow = image->data, *orow = pixels;
600
601         width = image->width;
602         height = image->height;
603         bpl = image->bytes_per_line;
604
605         for (yy = 0; yy < height; yy++) {
606 #ifdef LITTLE
607                 s = (guint32 *) srow;
608 #else
609                 s = srow;
610 #endif
611                 o = (guint16 *) orow;
612                 for (xx = 1; xx < width; xx += 2) {
613                         register guint32 data;
614 #ifdef LITTLE
615                         data = *s++;
616                         *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
617                                 | (data & 0x3e0) << 6 | (data & 0x380) << 1;
618                         *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
619                                 | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
620                         *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
621                                 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
622 #else
623                         /* swap endianness first */
624                         data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
625                         s += 4;
626                         *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
627                                 | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
628                         *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
629                                 | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
630                         *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
631                                 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
632 #endif
633                 }
634                 /* check for last remaining pixel */
635                 if (width & 1) {
636                         register guint16 data;
637 #ifdef LITTLE
638                         data = *((short *) s);
639 #else
640                         data = *((short *) s);
641                         data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
642 #endif
643                         ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
644                         ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
645                         ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
646                 }
647                 srow += bpl;
648                 orow += rowstride;
649         }
650 }
651
652 /*
653   convert 15 bits/pixel data
654   no alpha
655   data in msb format
656 */
657 static void
658 rgb555msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
659 {
660         int xx, yy;
661         int width, height;
662         int bpl;
663
664 #ifdef LITTLE
665         register guint8 *s;     /* read 2 pixels at once */
666 #else
667         register guint32 *s;    /* read 2 pixels at once */
668 #endif
669         register guint16 *o;
670         guint8 *srow = image->data, *orow = pixels;
671
672         width = image->width;
673         height = image->height;
674         bpl = image->bytes_per_line;
675
676         for (yy = 0; yy < height; yy++) {
677 #ifdef LITTLE
678                 s = srow;
679 #else
680                 s = (guint32 *) srow;
681 #endif
682                 o = (guint16 *) orow;
683                 for (xx = 1; xx < width; xx += 2) {
684                         register guint32 data;
685 #ifdef LITTLE
686                         /* swap endianness first */
687                         data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
688                         s += 4;
689                         *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
690                                 | (data & 0x3e0) << 6 | (data & 0x380) << 1;
691                         *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
692                                 | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
693                         *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
694                                 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
695 #else
696                         data = *s++;
697                         *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
698                                 | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
699                         *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
700                                 | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
701                         *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
702                                 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
703 #endif
704                 }
705                 /* check for last remaining pixel */
706                 if (width & 1) {
707                         register guint16 data;
708 #ifdef LITTLE
709                         data = *((short *) s);
710                         data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
711 #else
712                         data = *((short *) s);
713 #endif
714                         ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
715                         ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
716                         ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
717                 }
718                 srow += bpl;
719                 orow += rowstride;
720         }
721 }
722
723 /*
724   convert 15 bits/pixel data
725   with alpha
726   data in lsb format
727 */
728 static void
729 rgb555alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
730 {
731         int xx, yy;
732         int width, height;
733         int bpl;
734
735 #ifdef LITTLE
736         register guint16 *s;    /* read 1 pixels at once */
737 #else
738         register guint8 *s;
739 #endif
740         register guint32 *o;
741
742         guint8 *srow = image->data, *orow = pixels;
743
744         width = image->width;
745         height = image->height;
746         bpl = image->bytes_per_line;
747
748         for (yy = 0; yy < height; yy++) {
749 #ifdef LITTLE
750                 s = (guint16 *) srow;
751 #else
752                 s = srow;
753 #endif
754                 o = (guint32 *) orow;
755                 for (xx = 0; xx < width; xx++) {
756                         register guint32 data;
757                         /*  rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
758                         /*  little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
759 #ifdef LITTLE
760                         data = *s++;
761                         *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
762                                 | (data & 0x3e0) << 6 | (data & 0x380) << 1
763                                 | (data & 0x1f) << 19 | (data & 0x1c) << 14
764                                 | 0xff000000;
765 #else
766                         /* swap endianness first */
767                         data = s[0] | s[1] << 8;
768                         s += 2;
769                         *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
770                                 | (data & 0x3e0) << 14 | (data & 0x380) << 9
771                                 | (data & 0x1f) << 11 | (data & 0x1c) << 6
772                                 | 0xff;
773 #endif
774                 }
775                 srow += bpl;
776                 orow += rowstride;
777         }
778 }
779
780 /*
781   convert 15 bits/pixel data
782   with alpha
783   data in msb format
784 */
785 static void
786 rgb555amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
787 {
788         int xx, yy;
789         int width, height;
790         int bpl;
791
792 #ifdef LITTLE
793         register guint16 *s;    /* read 1 pixels at once */
794 #else
795         register guint8 *s;
796 #endif
797         register guint32 *o;
798
799         guint8 *srow = image->data, *orow = pixels;
800
801         width = image->width;
802         height = image->height;
803         bpl = image->bytes_per_line;
804
805         for (yy = 0; yy < height; yy++) {
806 #ifdef LITTLE
807                 s = (guint16 *) srow;
808 #else
809                 s = srow;
810 #endif
811                 o = (guint32 *) orow;
812                 for (xx = 0; xx < width; xx++) {
813                         register guint32 data;
814                         /*  rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
815                         /*  little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
816 #ifdef LITTLE
817                         /* swap endianness first */
818                         data = s[0] | s[1] << 8;
819                         s += 2;
820                         *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
821                                 | (data & 0x3e0) << 6 | (data & 0x380) << 1
822                                 | (data & 0x1f) << 19 | (data & 0x1c) << 14
823                                 | 0xff000000;
824 #else
825                         data = *s++;
826                         *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
827                                 | (data & 0x3e0) << 14 | (data & 0x380) << 9
828                                 | (data & 0x1f) << 11 | (data & 0x1c) << 6
829                                 | 0xff;
830 #endif
831                 }
832                 srow += bpl;
833                 orow += rowstride;
834         }
835 }
836
837
838 static void
839 rgb888alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
840 {
841         int xx, yy;
842         int width, height;
843         int bpl;
844
845         guint8 *s;      /* for byte order swapping */
846         guint8 *o;
847         guint8 *srow = image->data, *orow = pixels;
848
849         width = image->width;
850         height = image->height;
851         bpl = image->bytes_per_line;
852
853         d (printf ("32 bits/pixel with alpha\n"));
854
855         /* lsb data */
856         for (yy = 0; yy < height; yy++) {
857                 s = srow;
858                 o = orow;
859                 for (xx = 0; xx < width; xx++) {
860                         *o++ = s[2];
861                         *o++ = s[1];
862                         *o++ = s[0];
863                         *o++ = 0xff;
864                         s += 4;
865                 }
866                 srow += bpl;
867                 orow += rowstride;
868         }
869 }
870
871 static void
872 rgb888lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
873 {
874         int xx, yy;
875         int width, height;
876         int bpl;
877
878         guint8 *srow = image->data, *orow = pixels;
879         guint8 *o, *s;
880
881         width = image->width;
882         height = image->height;
883         bpl = image->bytes_per_line;
884
885         d (printf ("32 bit, lsb, no alpha\n"));
886
887         for (yy = 0; yy < height; yy++) {
888                 s = srow;
889                 o = orow;
890                 for (xx = 0; xx < width; xx++) {
891                         *o++ = s[2];
892                         *o++ = s[1];
893                         *o++ = s[0];
894                         s += 4;
895                 }
896                 srow += bpl;
897                 orow += rowstride;
898         }
899 }
900
901 static void
902 rgb888amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
903 {
904         int xx, yy;
905         int width, height;
906         int bpl;
907
908         guint8 *srow = image->data, *orow = pixels;
909 #ifdef LITTLE
910         guint32 *o;
911         guint32 *s;
912 #else
913         guint8 *s;      /* for byte order swapping */
914         guint8 *o;
915 #endif
916
917         d (printf ("32 bit, msb, with alpha\n"));
918
919         width = image->width;
920         height = image->height;
921         bpl = image->bytes_per_line;
922
923         /* msb data */
924         for (yy = 0; yy < height; yy++) {
925 #ifdef LITTLE
926                 s = (guint32 *) srow;
927                 o = (guint32 *) orow;
928 #else
929                 s = srow;
930                 o = orow;
931 #endif
932                 for (xx = 0; xx < width; xx++) {
933 #ifdef LITTLE
934                         *o++ = s[1];
935                         *o++ = s[2];
936                         *o++ = s[3];
937                         *o++ = 0xff;
938                         s += 4;
939 #else
940                         *o++ = (*s << 8) | 0xff; /* untested */
941                         s++;
942 #endif
943                 }
944                 srow += bpl;
945                 orow += rowstride;
946         }
947 }
948
949 static void
950 rgb888msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
951 {
952         int xx, yy;
953         int width, height;
954         int bpl;
955
956         guint8 *srow = image->data, *orow = pixels;
957         guint8 *s;
958         guint8 *o;
959
960         d (printf ("32 bit, msb, no alpha\n"));
961
962         width = image->width;
963         height = image->height;
964         bpl = image->bytes_per_line;
965
966         for (yy = 0; yy < height; yy++) {
967                 s = srow;
968                 o = orow;
969                 for (xx = 0; xx < width; xx++) {
970                         *o++ = s[1];
971                         *o++ = s[2];
972                         *o++ = s[3];
973                         s += 4;
974                 }
975                 srow += bpl;
976                 orow += rowstride;
977         }
978 }
979
980 /*
981   This should work correctly with any display/any endianness, but will probably
982   run quite slow
983 */
984 static void
985 convert_real_slow (XImage *image, guchar *pixels, int rowstride, xlib_colormap *cmap, int alpha)
986 {
987         int xx, yy;
988         int width, height;
989         int bpl;
990         guint8 *srow = image->data, *orow = pixels;
991         guint8 *s;
992         guint8 *o;
993         guint32 pixel;
994         Visual *v;
995         guint8 component;
996         int i;
997         int red_shift, red_prec, green_shift, green_prec, blue_shift, blue_prec;
998
999         width = image->width;
1000         height = image->height;
1001         bpl = image->bytes_per_line;
1002         v = cmap->visual;
1003
1004         visual_decompose_mask (v->red_mask, &red_shift, &red_prec);
1005         visual_decompose_mask (v->green_mask, &green_shift, &green_prec);
1006         visual_decompose_mask (v->blue_mask, &blue_shift, &blue_prec);
1007
1008         d(printf("rgb  mask/shift/prec = %x:%x:%x %d:%d:%d  %d:%d:%d\n",
1009                  v->red_mask, v->green_mask, v->blue_mask,
1010                  red_shift, green_shift, blue_shift,
1011                  red_prec, green_prec, blue_prec));
1012
1013         for (yy = 0; yy < height; yy++) {
1014                 s = srow;
1015                 o = orow;
1016                 for (xx = 0; xx < width; xx++) {
1017                         pixel = XGetPixel (image, xx, yy);
1018                         switch (v->class) {
1019                                 /* I assume this is right for static & greyscale's too? */
1020                         case StaticGray:
1021                         case GrayScale:
1022                         case StaticColor:
1023                         case PseudoColor:
1024                                 *o++ = cmap->colors[pixel].red;
1025                                 *o++ = cmap->colors[pixel].green;
1026                                 *o++ = cmap->colors[pixel].blue;
1027                                 break;
1028                         case TrueColor:
1029                                 /* This is odd because it must sometimes shift left (otherwise
1030                                    I'd just shift >> (*_shift - 8 + *_prec + <0-7>). This logic
1031                                    should work for all bit sizes/shifts/etc. */
1032                                 component = 0;
1033                                 for (i = 24; i < 32; i += red_prec)
1034                                         component |= ((pixel & v->red_mask) << (32 - red_shift - red_prec)) >> i;
1035                                 *o++ = component;
1036                                 component = 0;
1037                                 for (i = 24; i < 32; i += green_prec)
1038                                         component |= ((pixel & v->green_mask) << (32 - green_shift - green_prec)) >> i;
1039                                 *o++ = component;
1040                                 component = 0;
1041                                 for (i = 24; i < 32; i += blue_prec)
1042                                         component |= ((pixel & v->blue_mask) << (32 - blue_shift - blue_prec)) >> i;
1043                                 *o++ = component;
1044                                 break;
1045                         case DirectColor:
1046                                 *o++ = cmap->colors[((pixel & v->red_mask) << (32 - red_shift - red_prec)) >> 24].red;
1047                                 *o++ = cmap->colors[((pixel & v->green_mask) << (32 - green_shift - green_prec)) >> 24].green;
1048                                 *o++ = cmap->colors[((pixel & v->blue_mask) << (32 - blue_shift - blue_prec)) >> 24].blue;
1049                                 break;
1050                         }
1051                         if (alpha)
1052                                 *o++ = 0xff;
1053                 }
1054                 srow += bpl;
1055                 orow += rowstride;
1056         }
1057 }
1058
1059 typedef void (* cfunc) (XImage *image, guchar *pixels, int rowstride, xlib_colormap *cmap);
1060
1061 static cfunc convert_map[] = {
1062         rgb1,rgb1,rgb1a,rgb1a,
1063         rgb8,rgb8,rgb8a,rgb8a,
1064         rgb555lsb,rgb555msb,rgb555alsb,rgb555amsb,
1065         rgb565lsb,rgb565msb,rgb565alsb,rgb565amsb,
1066         rgb888lsb,rgb888msb,rgb888alsb,rgb888amsb
1067 };
1068
1069 /*
1070   perform actual conversion
1071
1072   If we can, try and use the optimised code versions, but as a default
1073   fallback, and always for direct colour, use the generic/slow but complete
1074   conversion function.
1075 */
1076 static void
1077 rgbconvert (XImage *image, guchar *pixels, int rowstride, int alpha, xlib_colormap *cmap)
1078 {
1079         int index = (image->byte_order == MSBFirst) | (alpha != 0) << 1;
1080         int bank=5;             /* default fallback converter */
1081         Visual *v = cmap->visual;
1082
1083         d(printf("masks = %x:%x:%x\n", v->red_mask, v->green_mask, v->blue_mask));
1084         d(printf("image depth = %d, bpp = %d\n", image->depth, image->bits_per_pixel));
1085
1086         switch (v->class) {
1087                                 /* I assume this is right for static & greyscale's too? */
1088         case StaticGray:
1089         case GrayScale:
1090         case StaticColor:
1091         case PseudoColor:
1092                 switch (image->bits_per_pixel) {
1093                 case 1:
1094                         bank = 0;
1095                         break;
1096                 case 8:
1097                         bank = 1;
1098                         break;
1099                 }
1100                 break;
1101         case TrueColor:
1102                 switch (image->depth) {
1103                 case 15:
1104                         if (v->red_mask == 0x7c00 && v->green_mask == 0x3e0 && v->blue_mask == 0x1f
1105                             && image->bits_per_pixel == 16)
1106                                 bank = 2;
1107                         break;
1108                 case 16:
1109                         if (v->red_mask == 0xf800 && v->green_mask == 0x7e0 && v->blue_mask == 0x1f
1110                             && image->bits_per_pixel == 16)
1111                                 bank = 3;
1112                         break;
1113                 case 24:
1114                 case 32:
1115                         if (v->red_mask == 0xff0000 && v->green_mask == 0xff00 && v->blue_mask == 0xff
1116                             && image->bits_per_pixel == 32)
1117                                 bank = 4;
1118                         break;
1119                 }
1120                 break;
1121         case DirectColor:
1122                 /* always use the slow version */
1123                 break;
1124         }
1125
1126         d(printf("converting using conversion function in bank %d\n", bank));
1127
1128         if (bank==5) {
1129                 convert_real_slow(image, pixels, rowstride, cmap, alpha);
1130         } else {
1131                 index |= bank << 2;
1132                 (* convert_map[index]) (image, pixels, rowstride, cmap);
1133         }
1134 }
1135
1136 static gboolean
1137 xlib_window_is_viewable (Window w)
1138 {
1139         XWindowAttributes wa;
1140
1141         while (w != 0) {
1142                 Window parent, root, *children;
1143                 int nchildren;
1144
1145                 XGetWindowAttributes (gdk_pixbuf_dpy, w, &wa);
1146                 if (wa.map_state != IsViewable)
1147                         return FALSE;
1148
1149                 if (!XQueryTree (gdk_pixbuf_dpy, w, &root,
1150                                  &parent, &children, &nchildren))
1151                         return FALSE;
1152
1153                 if (nchildren > 0)
1154                         XFree (children);
1155
1156                 if ((parent == root) || (w == root))
1157                         return TRUE;
1158
1159                 w = parent;
1160         }
1161
1162         return FALSE;
1163 }
1164
1165 static gint
1166 xlib_window_get_origin (Window w, gint *x, gint *y)
1167 {
1168         Window child;
1169         return XTranslateCoordinates (gdk_pixbuf_dpy, w,
1170                                       RootWindow (gdk_pixbuf_dpy,
1171                                                   gdk_pixbuf_screen),
1172                                       0, 0, x, y, &child);
1173 }
1174
1175 /* Exported functions */
1176
1177 /**
1178  * gdk_pixbuf_xlib_get_from_drawable:
1179  * @dest: Destination pixbuf, or NULL if a new pixbuf should be created.
1180  * @src: Source drawable.
1181  * @cmap: A colormap if @src is a pixmap.  If it is a window, this argument will
1182  * be ignored.
1183  * @visual: A visual if @src is a pixmap.  If it is a window, this argument will
1184  * be ignored.
1185  * @src_x: Source X coordinate within drawable.
1186  * @src_y: Source Y coordinate within drawable.
1187  * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1188  * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1189  * @width: Width in pixels of region to get.
1190  * @height: Height in pixels of region to get.
1191  *
1192  * Transfers image data from a Gdk drawable and converts it to an RGB(A)
1193  * representation inside a GdkPixbuf.
1194  *
1195  * If the drawable @src is a pixmap, then a suitable colormap must be specified,
1196  * since pixmaps are just blocks of pixel data without an associated colormap.
1197  * If the drawable is a window, the @cmap argument will be ignored and the
1198  * window's own colormap will be used instead.
1199  *
1200  * If the specified destination pixbuf @dest is #NULL, then this function will
1201  * create an RGB pixbuf with 8 bits per channel and no alpha, with the same size
1202  * specified by the @width and @height arguments.  In this case, the @dest_x and
1203  * @dest_y arguments must be specified as 0, otherwise the function will return
1204  * #NULL.  If the specified destination pixbuf is not NULL and it contains alpha
1205  * information, then the filled pixels will be set to full opacity.
1206  *
1207  * If the specified drawable is a pixmap, then the requested source rectangle
1208  * must be completely contained within the pixmap, otherwise the function will
1209  * return #NULL.
1210  *
1211  * If the specified drawable is a window, then it must be viewable, i.e. all of
1212  * its ancestors up to the root window must be mapped.  Also, the specified
1213  * source rectangle must be completely contained within the window and within
1214  * the screen.  If regions of the window are obscured by noninferior windows, the
1215  * contents of those regions are undefined.  The contents of regions obscured by
1216  * inferior windows of a different depth than that of the source window will also
1217  * be undefined.
1218  *
1219  * Return value: The same pixbuf as @dest if it was non-NULL, or a newly-created
1220  * pixbuf with a reference count of 1 if no destination pixbuf was specified; in
1221  * the latter case, NULL will be returned if not enough memory could be
1222  * allocated for the pixbuf to be created.
1223  **/
1224 GdkPixbuf *
1225 gdk_pixbuf_xlib_get_from_drawable (GdkPixbuf *dest,
1226                                    Drawable src,
1227                                    Colormap cmap, Visual *visual,
1228                                    int src_x, int src_y,
1229                                    int dest_x, int dest_y,
1230                                    int width, int height)
1231 {
1232         int src_width, src_height;
1233         XImage *image;
1234         int rowstride, bpp, alpha;
1235         XWindowAttributes wa;
1236         xlib_colormap *x_cmap;
1237         gboolean is_pixmap;
1238
1239         /* General sanity checks */
1240
1241         g_return_val_if_fail (src != 0, NULL);
1242
1243         is_pixmap = drawable_is_pixmap (src);
1244
1245         if (is_pixmap) {
1246                 g_return_val_if_fail (cmap != 0, NULL);
1247                 g_return_val_if_fail (visual != NULL, NULL);
1248         }
1249         else
1250                 g_return_val_if_fail (xlib_window_is_viewable (src), NULL);
1251
1252         if (!dest)
1253                 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1254         else {
1255                 g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1256                 g_return_val_if_fail (dest->n_channels == 3
1257                                       || dest->n_channels == 4, NULL);
1258                 g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1259         }
1260
1261         /* Coordinate sanity checks */
1262
1263         if (!is_pixmap) {
1264             XGetWindowAttributes (gdk_pixbuf_dpy, src, &wa);
1265             src_width = wa.width;
1266             src_height = wa.height;
1267         } else {
1268             Window root;
1269             int tx, ty, bwidth, depth;
1270             XGetGeometry (gdk_pixbuf_dpy, src, &root, &tx, &ty,
1271                           &src_width, &src_height, &bwidth, &depth);
1272         }
1273
1274         g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1275         g_return_val_if_fail (src_x + width <= src_width
1276                               && src_y + height <= src_height, NULL);
1277
1278         if (dest) {
1279                 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1280                 g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1281                 g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1282         }
1283
1284         if (!is_pixmap) {
1285                 int ret;
1286                 int src_xorigin, src_yorigin;
1287                 int screen_width, screen_height;
1288                 int screen_srcx, screen_srcy;
1289
1290                 ret = xlib_window_get_origin (src, &src_xorigin, &src_yorigin);
1291                 g_return_val_if_fail (ret != FALSE, NULL);
1292
1293                 screen_width = DisplayWidth (gdk_pixbuf_dpy, gdk_pixbuf_screen);
1294                 screen_height = DisplayHeight (gdk_pixbuf_dpy, gdk_pixbuf_screen);
1295
1296                 screen_srcx = src_xorigin + src_x;
1297                 screen_srcy = src_yorigin + src_y;
1298
1299                 g_return_val_if_fail (screen_srcx >= 0 && screen_srcy >= 0, NULL);
1300                 g_return_val_if_fail (screen_srcx + width <= screen_width, NULL);
1301                 g_return_val_if_fail (screen_srcy + height <= screen_height, NULL);
1302         }
1303
1304         /* Get Image in ZPixmap format (packed bits). */
1305         image = XGetImage (gdk_pixbuf_dpy, src, src_x, src_y,
1306                            width, height, AllPlanes, ZPixmap);
1307         g_return_val_if_fail (image != NULL, NULL);
1308
1309         /* Create the pixbuf if needed */
1310         if (!dest) {
1311                 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1312                                        FALSE, 8, width, height);
1313                 if (!dest) {
1314                         XDestroyImage (image);
1315                         return NULL;
1316                 }
1317         }
1318
1319         /* Get the colormap if needed */
1320         if (!is_pixmap)
1321         {
1322                 cmap = wa.colormap;
1323                 visual = wa.visual;
1324         }
1325
1326         x_cmap = xlib_get_colormap (cmap, visual);
1327
1328         alpha = dest->has_alpha;
1329         rowstride = dest->rowstride;
1330         bpp = alpha ? 4 : 3;
1331
1332         /* we offset into the image data based on the position we are retrieving from */
1333         rgbconvert (image, dest->pixels +
1334                     (dest_y * rowstride) + (dest_x * bpp),
1335                     rowstride,
1336                     alpha,
1337                     x_cmap);
1338
1339         xlib_colormap_free (x_cmap);
1340         XDestroyImage (image);
1341
1342         return dest;
1343 }