]> Pileus Git - ~andy/gtk/blob - contrib/gdk-pixbuf-xlib/gdk-pixbuf-xlib-drawable.c
contrib subdir
[~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[0] | s[1] << 8 | s[2] << 16 | s[3] << 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[0] | s[1] << 8 | s[2] << 16 | s[3] << 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                 s = srow;
551                 o = (guint32 *) orow;
552                 for (xx = 0; xx < width; xx ++) {
553                         register guint32 data;
554                         /*  rrrrrggg gggbbbbb -> rrrrrRRR gggggg00 bbbbbBBB aaaaaaaa */
555                         /*  little endian: aaaaaaaa bbbbbBBB gggggg00 rrrrrRRR */
556 #ifdef LITTLE
557                         /* swap endianness first */
558                         data = s[0] | s[1] << 8;
559                         s += 2;
560                         *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
561                                 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
562                                 | (data & 0x1f) << 19 | (data & 0x1c) << 14
563                                 | 0xff000000;
564 #else
565                         data = *s++;
566                         *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
567                                 | (data & 0x7e0) << 13 | (data & 0x600) << 7
568                                 | (data & 0x1f) << 11 | (data & 0x1c) << 6
569                                 | 0xff;
570 #endif
571                 }
572                 srow += bpl;
573                 orow += rowstride;
574         }
575 }
576
577 /*
578   convert 15 bits/pixel data
579   no alpha
580   data in lsb format
581 */
582 static void
583 rgb555lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
584 {
585         int xx, yy;
586         int width, height;
587         int bpl;
588
589 #ifdef LITTLE
590         register guint32 *s;    /* read 2 pixels at once */
591 #else
592         register guint8 *s;     /* read 2 pixels at once */
593 #endif
594         register guint16 *o;
595         guint8 *srow = image->data, *orow = pixels;
596
597         width = image->width;
598         height = image->height;
599         bpl = image->bytes_per_line;
600
601         for (yy = 0; yy < height; yy++) {
602 #ifdef LITTLE
603                 s = (guint32 *) srow;
604 #else
605                 s = srow;
606 #endif
607                 o = (guint16 *) orow;
608                 for (xx = 1; xx < width; xx += 2) {
609                         register guint32 data;
610 #ifdef LITTLE
611                         data = *s++;
612                         *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
613                                 | (data & 0x3e0) << 6 | (data & 0x380) << 1;
614                         *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
615                                 | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
616                         *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
617                                 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
618 #else
619                         /* swap endianness first */
620                         data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
621                         s += 4;
622                         *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
623                                 | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
624                         *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
625                                 | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
626                         *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
627                                 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
628 #endif
629                 }
630                 /* check for last remaining pixel */
631                 if (width & 1) {
632                         register guint16 data;
633 #ifdef LITTLE
634                         data = *((short *) s);
635 #else
636                         data = *((short *) s);
637                         data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
638 #endif
639                         ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
640                         ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
641                         ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
642                 }
643                 srow += bpl;
644                 orow += rowstride;
645         }
646 }
647
648 /*
649   convert 15 bits/pixel data
650   no alpha
651   data in msb format
652 */
653 static void
654 rgb555msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
655 {
656         int xx, yy;
657         int width, height;
658         int bpl;
659
660 #ifdef LITTLE
661         register guint8 *s;     /* read 2 pixels at once */
662 #else
663         register guint32 *s;    /* read 2 pixels at once */
664 #endif
665         register guint16 *o;
666         guint8 *srow = image->data, *orow = pixels;
667
668         width = image->width;
669         height = image->height;
670         bpl = image->bytes_per_line;
671
672         for (yy = 0; yy < height; yy++) {
673                 s = srow;
674                 o = (guint16 *) orow;
675                 for (xx = 1; xx < width; xx += 2) {
676                         register guint32 data;
677 #ifdef LITTLE
678                         /* swap endianness first */
679                         data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
680                         s += 4;
681                         *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
682                                 | (data & 0x3e0) << 6 | (data & 0x380) << 1;
683                         *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
684                                 | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
685                         *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
686                                 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
687 #else
688                         data = *s++;
689                         *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
690                                 | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
691                         *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
692                                 | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
693                         *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
694                                 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
695 #endif
696                 }
697                 /* check for last remaining pixel */
698                 if (width & 1) {
699                         register guint16 data;
700 #ifdef LITTLE
701                         data = *((short *) s);
702                         data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
703 #else
704                         data = *((short *) s);
705 #endif
706                         ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
707                         ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
708                         ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
709                 }
710                 srow += bpl;
711                 orow += rowstride;
712         }
713 }
714
715 /*
716   convert 15 bits/pixel data
717   with alpha
718   data in lsb format
719 */
720 static void
721 rgb555alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
722 {
723         int xx, yy;
724         int width, height;
725         int bpl;
726
727 #ifdef LITTLE
728         register guint16 *s;    /* read 1 pixels at once */
729 #else
730         register guint8 *s;
731 #endif
732         register guint32 *o;
733
734         guint8 *srow = image->data, *orow = pixels;
735
736         width = image->width;
737         height = image->height;
738         bpl = image->bytes_per_line;
739
740         for (yy = 0; yy < height; yy++) {
741 #ifdef LITTLE
742                 s = (guint16 *) srow;
743 #else
744                 s = srow;
745 #endif
746                 o = (guint32 *) orow;
747                 for (xx = 0; xx < width; xx++) {
748                         register guint32 data;
749                         /*  rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
750                         /*  little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
751 #ifdef LITTLE
752                         data = *s++;
753                         *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
754                                 | (data & 0x3e0) << 6 | (data & 0x380) << 1
755                                 | (data & 0x1f) << 19 | (data & 0x1c) << 14
756                                 | 0xff000000;
757 #else
758                         /* swap endianness first */
759                         data = s[0] | s[1] << 8;
760                         s += 2;
761                         *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
762                                 | (data & 0x3e0) << 14 | (data & 0x380) << 9
763                                 | (data & 0x1f) << 11 | (data & 0x1c) << 6
764                                 | 0xff;
765 #endif
766                 }
767                 srow += bpl;
768                 orow += rowstride;
769         }
770 }
771
772 /*
773   convert 15 bits/pixel data
774   with alpha
775   data in msb format
776 */
777 static void
778 rgb555amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
779 {
780         int xx, yy;
781         int width, height;
782         int bpl;
783
784 #ifdef LITTLE
785         register guint16 *s;    /* read 1 pixels at once */
786 #else
787         register guint8 *s;
788 #endif
789         register guint32 *o;
790
791         guint8 *srow = image->data, *orow = pixels;
792
793         width = image->width;
794         height = image->height;
795         bpl = image->bytes_per_line;
796
797         for (yy = 0; yy < height; yy++) {
798 #ifdef LITTLE
799                 s = (guint16 *) srow;
800 #else
801                 s = srow;
802 #endif
803                 o = (guint32 *) orow;
804                 for (xx = 0; xx < width; xx++) {
805                         register guint32 data;
806                         /*  rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
807                         /*  little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
808 #ifdef LITTLE
809                         /* swap endianness first */
810                         data = s[0] | s[1] << 8;
811                         s += 2;
812                         *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
813                                 | (data & 0x3e0) << 6 | (data & 0x380) << 1
814                                 | (data & 0x1f) << 19 | (data & 0x1c) << 14
815                                 | 0xff000000;
816 #else
817                         data = *s++;
818                         *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
819                                 | (data & 0x3e0) << 14 | (data & 0x380) << 9
820                                 | (data & 0x1f) << 11 | (data & 0x1c) << 6
821                                 | 0xff;
822 #endif
823                 }
824                 srow += bpl;
825                 orow += rowstride;
826         }
827 }
828
829
830 static void
831 rgb888alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
832 {
833         int xx, yy;
834         int width, height;
835         int bpl;
836
837         guint8 *s;      /* for byte order swapping */
838         guint8 *o;
839         guint8 *srow = image->data, *orow = pixels;
840
841         width = image->width;
842         height = image->height;
843         bpl = image->bytes_per_line;
844
845         d (printf ("32 bits/pixel with alpha\n"));
846
847         /* lsb data */
848         for (yy = 0; yy < height; yy++) {
849                 s = srow;
850                 o = orow;
851                 for (xx = 0; xx < width; xx++) {
852                         *o++ = s[2];
853                         *o++ = s[1];
854                         *o++ = s[0];
855                         *o++ = 0xff;
856                         s += 4;
857                 }
858                 srow += bpl;
859                 orow += rowstride;
860         }
861 }
862
863 static void
864 rgb888lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
865 {
866         int xx, yy;
867         int width, height;
868         int bpl;
869
870         guint8 *srow = image->data, *orow = pixels;
871         guint8 *o, *s;
872
873         width = image->width;
874         height = image->height;
875         bpl = image->bytes_per_line;
876
877         d (printf ("32 bit, lsb, no alpha\n"));
878
879         for (yy = 0; yy < height; yy++) {
880                 s = srow;
881                 o = orow;
882                 for (xx = 0; xx < width; xx++) {
883                         *o++ = s[2];
884                         *o++ = s[1];
885                         *o++ = s[0];
886                         s += 4;
887                 }
888                 srow += bpl;
889                 orow += rowstride;
890         }
891 }
892
893 static void
894 rgb888amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
895 {
896         int xx, yy;
897         int width, height;
898         int bpl;
899
900         guint8 *srow = image->data, *orow = pixels;
901 #ifdef LITTLE
902         guint32 *o;
903         guint32 *s;
904 #else
905         guint8 *s;      /* for byte order swapping */
906         guint8 *o;
907 #endif
908
909         d (printf ("32 bit, msb, with alpha\n"));
910
911         width = image->width;
912         height = image->height;
913         bpl = image->bytes_per_line;
914
915         /* msb data */
916         for (yy = 0; yy < height; yy++) {
917 #ifdef LITTLE
918                 s = (guint32 *) srow;
919                 o = (guint32 *) orow;
920 #else
921                 s = srow;
922                 o = orow;
923 #endif
924                 for (xx = 0; xx < width; xx++) {
925 #ifdef LITTLE
926                         *o++ = s[1];
927                         *o++ = s[2];
928                         *o++ = s[3];
929                         *o++ = 0xff;
930                         s += 4;
931 #else
932                         *o++ = (*s << 8) | 0xff; /* untested */
933                         s++;
934 #endif
935                 }
936                 srow += bpl;
937                 orow += rowstride;
938         }
939 }
940
941 static void
942 rgb888msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
943 {
944         int xx, yy;
945         int width, height;
946         int bpl;
947
948         guint8 *srow = image->data, *orow = pixels;
949         guint8 *s;
950         guint8 *o;
951
952         d (printf ("32 bit, msb, no alpha\n"));
953
954         width = image->width;
955         height = image->height;
956         bpl = image->bytes_per_line;
957
958         for (yy = 0; yy < height; yy++) {
959                 s = srow;
960                 o = orow;
961                 for (xx = 0; xx < width; xx++) {
962                         *o++ = s[1];
963                         *o++ = s[2];
964                         *o++ = s[3];
965                         s += 4;
966                 }
967                 srow += bpl;
968                 orow += rowstride;
969         }
970 }
971
972 /*
973   This should work correctly with any display/any endianness, but will probably
974   run quite slow
975 */
976 static void
977 convert_real_slow (XImage *image, guchar *pixels, int rowstride, xlib_colormap *cmap, int alpha)
978 {
979         int xx, yy;
980         int width, height;
981         int bpl;
982         guint8 *srow = image->data, *orow = pixels;
983         guint8 *s;
984         guint8 *o;
985         guint32 pixel;
986         Visual *v;
987         guint8 component;
988         int i;
989         int red_shift, red_prec, green_shift, green_prec, blue_shift, blue_prec;
990
991         width = image->width;
992         height = image->height;
993         bpl = image->bytes_per_line;
994         v = cmap->visual;
995
996         visual_decompose_mask (v->red_mask, &red_shift, &red_prec);
997         visual_decompose_mask (v->green_mask, &green_shift, &green_prec);
998         visual_decompose_mask (v->blue_mask, &blue_shift, &blue_prec);
999
1000         d(printf("rgb  mask/shift/prec = %x:%x:%x %d:%d:%d  %d:%d:%d\n",
1001                  v->red_mask, v->green_mask, v->blue_mask,
1002                  red_shift, green_shift, blue_shift,
1003                  red_prec, green_prec, blue_prec));
1004
1005         for (yy = 0; yy < height; yy++) {
1006                 s = srow;
1007                 o = orow;
1008                 for (xx = 0; xx < width; xx++) {
1009                         pixel = XGetPixel (image, xx, yy);
1010                         switch (v->class) {
1011                                 /* I assume this is right for static & greyscale's too? */
1012                         case StaticGray:
1013                         case GrayScale:
1014                         case StaticColor:
1015                         case PseudoColor:
1016                                 *o++ = cmap->colors[pixel].red;
1017                                 *o++ = cmap->colors[pixel].green;
1018                                 *o++ = cmap->colors[pixel].blue;
1019                                 break;
1020                         case TrueColor:
1021                                 /* This is odd because it must sometimes shift left (otherwise
1022                                    I'd just shift >> (*_shift - 8 + *_prec + <0-7>). This logic
1023                                    should work for all bit sizes/shifts/etc. */
1024                                 component = 0;
1025                                 for (i = 24; i < 32; i += red_prec)
1026                                         component |= ((pixel & v->red_mask) << (32 - red_shift - red_prec)) >> i;
1027                                 *o++ = component;
1028                                 component = 0;
1029                                 for (i = 24; i < 32; i += green_prec)
1030                                         component |= ((pixel & v->green_mask) << (32 - green_shift - green_prec)) >> i;
1031                                 *o++ = component;
1032                                 component = 0;
1033                                 for (i = 24; i < 32; i += blue_prec)
1034                                         component |= ((pixel & v->blue_mask) << (32 - blue_shift - blue_prec)) >> i;
1035                                 *o++ = component;
1036                                 break;
1037                         case DirectColor:
1038                                 *o++ = cmap->colors[((pixel & v->red_mask) << (32 - red_shift - red_prec)) >> 24].red;
1039                                 *o++ = cmap->colors[((pixel & v->green_mask) << (32 - green_shift - green_prec)) >> 24].green;
1040                                 *o++ = cmap->colors[((pixel & v->blue_mask) << (32 - blue_shift - blue_prec)) >> 24].blue;
1041                                 break;
1042                         }
1043                         if (alpha)
1044                                 *o++ = 0xff;
1045                 }
1046                 srow += bpl;
1047                 orow += rowstride;
1048         }
1049 }
1050
1051 typedef void (* cfunc) (XImage *image, guchar *pixels, int rowstride, xlib_colormap *cmap);
1052
1053 static cfunc convert_map[] = {
1054         rgb1,rgb1,rgb1a,rgb1a,
1055         rgb8,rgb8,rgb8a,rgb8a,
1056         rgb555lsb,rgb555msb,rgb555alsb,rgb555amsb,
1057         rgb565lsb,rgb565msb,rgb565alsb,rgb565amsb,
1058         rgb888lsb,rgb888msb,rgb888alsb,rgb888amsb
1059 };
1060
1061 /*
1062   perform actual conversion
1063
1064   If we can, try and use the optimised code versions, but as a default
1065   fallback, and always for direct colour, use the generic/slow but complete
1066   conversion function.
1067 */
1068 static void
1069 rgbconvert (XImage *image, guchar *pixels, int rowstride, int alpha, xlib_colormap *cmap)
1070 {
1071         int index = (image->byte_order == MSBFirst) | (alpha != 0) << 1;
1072         int bank=5;             /* default fallback converter */
1073         Visual *v = cmap->visual;
1074
1075         d(printf("masks = %x:%x:%x\n", v->red_mask, v->green_mask, v->blue_mask));
1076         d(printf("image depth = %d, bpp = %d\n", image->depth, image->bits_per_pixel));
1077
1078         switch (v->class) {
1079                                 /* I assume this is right for static & greyscale's too? */
1080         case StaticGray:
1081         case GrayScale:
1082         case StaticColor:
1083         case PseudoColor:
1084                 switch (image->bits_per_pixel) {
1085                 case 1:
1086                         bank = 0;
1087                         break;
1088                 case 8:
1089                         bank = 1;
1090                         break;
1091                 }
1092                 break;
1093         case TrueColor:
1094                 switch (image->depth) {
1095                 case 15:
1096                         if (v->red_mask == 0x7c00 && v->green_mask == 0x3e0 && v->blue_mask == 0x1f
1097                             && image->bits_per_pixel == 16)
1098                                 bank = 2;
1099                         break;
1100                 case 16:
1101                         if (v->red_mask == 0xf800 && v->green_mask == 0x7e0 && v->blue_mask == 0x1f
1102                             && image->bits_per_pixel == 16)
1103                                 bank = 3;
1104                         break;
1105                 case 24:
1106                 case 32:
1107                         if (v->red_mask == 0xff0000 && v->green_mask == 0xff00 && v->blue_mask == 0xff
1108                             && image->bits_per_pixel == 32)
1109                                 bank = 4;
1110                         break;
1111                 }
1112                 break;
1113         case DirectColor:
1114                 /* always use the slow version */
1115                 break;
1116         }
1117
1118         d(printf("converting using conversion function in bank %d\n", bank));
1119
1120         if (bank==5) {
1121                 convert_real_slow(image, pixels, rowstride, cmap, alpha);
1122         } else {
1123                 index |= bank << 2;
1124                 (* convert_map[index]) (image, pixels, rowstride, cmap);
1125         }
1126 }
1127
1128 static gboolean
1129 xlib_window_is_viewable (Window w)
1130 {
1131         XWindowAttributes wa;
1132
1133         while (w != 0) {
1134                 Window parent, root, *children;
1135                 int nchildren;
1136
1137                 XGetWindowAttributes (gdk_pixbuf_dpy, w, &wa);
1138                 if (wa.map_state != IsViewable)
1139                         return FALSE;
1140
1141                 if (!XQueryTree (gdk_pixbuf_dpy, w, &root,
1142                                  &parent, &children, &nchildren))
1143                         return FALSE;
1144
1145                 if (nchildren > 0)
1146                         XFree (children);
1147
1148                 if (parent == root)
1149                         return TRUE;
1150
1151                 w = parent;
1152         }
1153
1154         return FALSE;
1155 }
1156
1157 static gint
1158 xlib_window_get_origin (Window w, gint *x, gint *y)
1159 {
1160         Window child;
1161         return XTranslateCoordinates (gdk_pixbuf_dpy, w,
1162                                       RootWindow (gdk_pixbuf_dpy,
1163                                                   gdk_pixbuf_screen),
1164                                       0, 0, x, y, &child);
1165 }
1166
1167 /* Exported functions */
1168
1169 /**
1170  * gdk_pixbuf_xlib_get_from_drawable:
1171  * @dest: Destination pixbuf, or NULL if a new pixbuf should be created.
1172  * @src: Source drawable.
1173  * @cmap: A colormap if @src is a pixmap.  If it is a window, this argument will
1174  * be ignored.
1175  * @visual: A visual if @src is a pixmap.  If it is a window, this argument will
1176  * be ignored.
1177  * @src_x: Source X coordinate within drawable.
1178  * @src_y: Source Y coordinate within drawable.
1179  * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1180  * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1181  * @width: Width in pixels of region to get.
1182  * @height: Height in pixels of region to get.
1183  *
1184  * Transfers image data from a Gdk drawable and converts it to an RGB(A)
1185  * representation inside a GdkPixbuf.
1186  *
1187  * If the drawable @src is a pixmap, then a suitable colormap must be specified,
1188  * since pixmaps are just blocks of pixel data without an associated colormap.
1189  * If the drawable is a window, the @cmap argument will be ignored and the
1190  * window's own colormap will be used instead.
1191  *
1192  * If the specified destination pixbuf @dest is #NULL, then this function will
1193  * create an RGB pixbuf with 8 bits per channel and no alpha, with the same size
1194  * specified by the @width and @height arguments.  In this case, the @dest_x and
1195  * @dest_y arguments must be specified as 0, otherwise the function will return
1196  * #NULL.  If the specified destination pixbuf is not NULL and it contains alpha
1197  * information, then the filled pixels will be set to full opacity.
1198  *
1199  * If the specified drawable is a pixmap, then the requested source rectangle
1200  * must be completely contained within the pixmap, otherwise the function will
1201  * return #NULL.
1202  *
1203  * If the specified drawable is a window, then it must be viewable, i.e. all of
1204  * its ancestors up to the root window must be mapped.  Also, the specified
1205  * source rectangle must be completely contained within the window and within
1206  * the screen.  If regions of the window are obscured by noninferior windows, the
1207  * contents of those regions are undefined.  The contents of regions obscured by
1208  * inferior windows of a different depth than that of the source window will also
1209  * be undefined.
1210  *
1211  * Return value: The same pixbuf as @dest if it was non-NULL, or a newly-created
1212  * pixbuf with a reference count of 1 if no destination pixbuf was specified; in
1213  * the latter case, NULL will be returned if not enough memory could be
1214  * allocated for the pixbuf to be created.
1215  **/
1216 GdkPixbuf *
1217 gdk_pixbuf_xlib_get_from_drawable (GdkPixbuf *dest,
1218                                    Drawable src,
1219                                    Colormap cmap, Visual *visual,
1220                                    int src_x, int src_y,
1221                                    int dest_x, int dest_y,
1222                                    int width, int height)
1223 {
1224         int src_width, src_height;
1225         XImage *image;
1226         int rowstride, bpp, alpha;
1227         XWindowAttributes wa;
1228         xlib_colormap *x_cmap;
1229         gboolean is_pixmap;
1230
1231         /* General sanity checks */
1232
1233         g_return_val_if_fail (src != 0, NULL);
1234
1235         is_pixmap = drawable_is_pixmap (src);
1236
1237         if (is_pixmap) {
1238                 g_return_val_if_fail (cmap != 0, NULL);
1239                 g_return_val_if_fail (visual != NULL, NULL);
1240         }
1241         else
1242                 g_return_val_if_fail (xlib_window_is_viewable (src), NULL);
1243
1244         if (!dest)
1245                 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1246         else {
1247                 g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1248                 g_return_val_if_fail (dest->n_channels == 3
1249                                       || dest->n_channels == 4, NULL);
1250                 g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1251         }
1252
1253         /* Coordinate sanity checks */
1254
1255         if (!is_pixmap) {
1256             XGetWindowAttributes (gdk_pixbuf_dpy, src, &wa);
1257             src_width = wa.width;
1258             src_height = wa.height;
1259         } else {
1260             Window root;
1261             int tx, ty, bwidth, depth;
1262             XGetGeometry (gdk_pixbuf_dpy, src, &root, &tx, &ty,
1263                           &src_width, &src_height, &bwidth, &depth);
1264         }
1265
1266         g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1267         g_return_val_if_fail (src_x + width <= src_width
1268                               && src_y + height <= src_height, NULL);
1269
1270         if (dest) {
1271                 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1272                 g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1273                 g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1274         }
1275
1276         if (!is_pixmap) {
1277                 int ret;
1278                 int src_xorigin, src_yorigin;
1279                 int screen_width, screen_height;
1280                 int screen_srcx, screen_srcy;
1281
1282                 ret = xlib_window_get_origin (src, &src_xorigin, &src_yorigin);
1283                 g_return_val_if_fail (ret != FALSE, NULL);
1284
1285                 screen_width = DisplayWidth (gdk_pixbuf_dpy, gdk_pixbuf_screen);
1286                 screen_height = DisplayHeight (gdk_pixbuf_dpy, gdk_pixbuf_screen);
1287
1288                 screen_srcx = src_xorigin + src_x;
1289                 screen_srcy = src_yorigin + src_y;
1290
1291                 g_return_val_if_fail (screen_srcx >= 0 && screen_srcy >= 0, NULL);
1292                 g_return_val_if_fail (screen_srcx + width <= screen_width, NULL);
1293                 g_return_val_if_fail (screen_srcy + height <= screen_height, NULL);
1294         }
1295
1296         /* Get Image in ZPixmap format (packed bits). */
1297         image = XGetImage (gdk_pixbuf_dpy, src, src_x, src_y,
1298                            width, height, AllPlanes, ZPixmap);
1299         g_return_val_if_fail (image != NULL, NULL);
1300
1301         /* Create the pixbuf if needed */
1302         if (!dest) {
1303                 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1304                                        FALSE, 8, width, height);
1305                 if (!dest) {
1306                         XDestroyImage (image);
1307                         return NULL;
1308                 }
1309         }
1310
1311         /* Get the colormap if needed */
1312         if (!is_pixmap)
1313         {
1314                 cmap = wa.colormap;
1315                 visual = wa.visual;
1316         }
1317
1318         x_cmap = xlib_get_colormap (cmap, visual);
1319
1320         alpha = dest->has_alpha;
1321         rowstride = dest->rowstride;
1322         bpp = alpha ? 4 : 3;
1323
1324         /* we offset into the image data based on the position we are retrieving from */
1325         rgbconvert (image, dest->pixels +
1326                     (dest_y * rowstride) + (dest_x * bpp),
1327                     rowstride,
1328                     alpha,
1329                     x_cmap);
1330
1331         xlib_colormap_free (x_cmap);
1332         XDestroyImage (image);
1333
1334         return dest;
1335 }