1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - convert X drawable information to RGB
4 * Copyright (C) 1999 Michael Zucchi
6 * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
7 * Cody Russell <bratsche@dfw.net>
8 * Federico Mena-Quintero <federico@gimp.org>
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.
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.
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.
26 /* Ported to Xlib by John Harper <john@dcs.warwick.ac.uk> */
32 #include <gdk-pixbuf/gdk-pixbuf-private.h>
33 #include "gdk-pixbuf-xlib-private.h"
35 #include <X11/Xutil.h>
37 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
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,
59 typedef struct xlib_colormap_struct xlib_colormap;
60 struct xlib_colormap_struct {
67 static xlib_colormap *
68 xlib_get_colormap (Colormap id, Visual *visual)
71 xlib_colormap *xc = g_new (xlib_colormap, 1);
73 xc->size = visual->map_entries;
74 xc->colors = g_new (XColor, xc->size);
78 for (i = 0; i < xc->size; i++) {
79 xc->colors[i].pixel = i;
80 xc->colors[i].flags = DoRed | DoGreen | DoBlue;
83 XQueryColors (gdk_pixbuf_dpy, xc->colormap, xc->colors, xc->size);
89 xlib_colormap_free (xlib_colormap *xc)
95 /* from gdkvisual.c */
97 visual_decompose_mask (gulong mask,
104 while (!(mask & 0x1)) {
115 static gboolean x_error;
118 handle_x_error (Display *dpy, XErrorEvent *ev)
125 drawable_is_pixmap (Drawable d)
127 /* copied from Imlib */
130 XWindowAttributes wa;
133 errh = XSetErrorHandler (handle_x_error);
135 XGetWindowAttributes (gdk_pixbuf_dpy, d, &wa);
136 XSync (gdk_pixbuf_dpy, False);
138 XSetErrorHandler (errh);
146 convert 1 bits-pixel data
150 rgb1 (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
156 register guint8 data;
158 guint8 *srow = image->data, *orow = pixels;
160 d (printf ("1 bits/pixel\n"));
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;
169 for (yy = 0; yy < height; yy++) {
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;
185 convert 1 bits/pixel data
189 rgb1a (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
195 register guint8 data;
197 guint8 *srow = image->data, *orow = pixels;
200 d (printf ("1 bits/pixel\n"));
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;
209 for (xx = 0; xx < 2; xx++) {
211 remap[xx] = 0xff000000
212 | colormap->colors[xx].blue << 16
213 | colormap->colors[xx].green << 8
214 | colormap->colors[xx].red;
217 | colormap->colors[xx].red << 24
218 | colormap->colors[xx].green << 16
219 | colormap->colors[xx].blue << 8;
223 for (yy = 0; yy < height; yy++) {
227 for (xx = 0; xx < width; xx ++) {
228 data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;
237 convert 8 bits/pixel data
241 rgb8 (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
247 register guint32 data;
248 guint8 *srow = image->data, *orow = pixels;
252 width = image->width;
253 height = image->height;
254 bpl = image->bytes_per_line;
256 d (printf ("8 bit, no alpha output\n"));
258 mask = mask_table[image->depth];
260 for (yy = 0; yy < height; yy++) {
263 for (xx = 0; xx < width; xx++) {
265 *o++ = colormap->colors[data].red;
266 *o++ = colormap->colors[data].green;
267 *o++ = colormap->colors[data].blue;
275 convert 8 bits/pixel data
279 rgb8a (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
285 register guint32 data;
287 register guint8 *s; /* read 2 pixels at once */
289 guint8 *srow = image->data, *orow = pixels;
291 width = image->width;
292 height = image->height;
293 bpl = image->bytes_per_line;
295 d (printf ("8 bit, with alpha output\n"));
297 mask = mask_table[image->depth];
299 for (xx = 0; xx < colormap->size; xx++) {
301 remap[xx] = 0xff000000
302 | colormap->colors[xx].blue << 16
303 | colormap->colors[xx].green << 8
304 | colormap->colors[xx].red;
307 | colormap->colors[xx].red << 24
308 | colormap->colors[xx].green << 16
309 | colormap->colors[xx].blue << 8;
313 for (yy = 0; yy < height; yy++) {
315 o = (guint32 *) orow;
316 for (xx = 0; xx < width; xx ++) {
326 convert 16 bits/pixel data
331 rgb565lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
338 register guint32 *s; /* read 2 pixels at once */
340 register guint8 *s; /* read 2 pixels at once */
343 guint8 *srow = image->data, *orow = pixels;
345 width = image->width;
346 height = image->height;
347 bpl = image->bytes_per_line;
349 for (yy = 0; yy < height; yy++) {
351 s = (guint32 *) srow;
355 o = (guint16 *) orow;
356 for (xx = 1; xx < width; xx += 2) {
357 register guint32 data;
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;
367 /* swap endianness first */
368 data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
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;
378 /* check for last remaining pixel */
380 register guint16 data;
382 data = *((short *) s);
384 data = *((short *) s);
385 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
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);
397 convert 16 bits/pixel data
402 rgb565msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
409 register guint8 *s; /* need to swap data order */
411 register guint32 *s; /* read 2 pixels at once */
414 guint8 *srow = image->data, *orow = pixels;
416 width = image->width;
417 height = image->height;
418 bpl = image->bytes_per_line;
420 for (yy = 0; yy < height; yy++) {
424 s = (guint32 *) srow;
426 o = (guint16 *) orow;
427 for (xx = 1; xx < width; xx += 2) {
428 register guint32 data;
430 /* swap endianness first */
431 data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
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;
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;
449 /* check for last remaining pixel */
451 register guint16 data;
453 data = *((short *) s);
454 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
456 data = *((short *) s);
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);
468 convert 16 bits/pixel data
473 rgb565alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
480 register guint16 *s; /* read 1 pixels at once */
486 guint8 *srow = image->data, *orow = pixels;
488 width = image->width;
489 height = image->height;
490 bpl = image->bytes_per_line;
492 for (yy = 0; yy < height; yy++) {
494 s = (guint16 *) srow;
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 */
505 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
506 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
507 | (data & 0x1f) << 19 | (data & 0x1c) << 14
510 /* swap endianness first */
511 data = s[0] | s[1] << 8;
513 *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
514 | (data & 0x7e0) << 13 | (data & 0x600) << 7
515 | (data & 0x1f) << 11 | (data & 0x1c) << 6
525 convert 16 bits/pixel data
530 rgb565amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
539 register guint16 *s; /* read 1 pixels at once */
543 guint8 *srow = image->data, *orow = pixels;
545 width = image->width;
546 height = image->height;
547 bpl = image->bytes_per_line;
549 for (yy = 0; yy < height; yy++) {
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 */
557 /* swap endianness first */
558 data = s[0] | s[1] << 8;
560 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
561 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
562 | (data & 0x1f) << 19 | (data & 0x1c) << 14
566 *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
567 | (data & 0x7e0) << 13 | (data & 0x600) << 7
568 | (data & 0x1f) << 11 | (data & 0x1c) << 6
578 convert 15 bits/pixel data
583 rgb555lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
590 register guint32 *s; /* read 2 pixels at once */
592 register guint8 *s; /* read 2 pixels at once */
595 guint8 *srow = image->data, *orow = pixels;
597 width = image->width;
598 height = image->height;
599 bpl = image->bytes_per_line;
601 for (yy = 0; yy < height; yy++) {
603 s = (guint32 *) srow;
607 o = (guint16 *) orow;
608 for (xx = 1; xx < width; xx += 2) {
609 register guint32 data;
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;
619 /* swap endianness first */
620 data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
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;
630 /* check for last remaining pixel */
632 register guint16 data;
634 data = *((short *) s);
636 data = *((short *) s);
637 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
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;
649 convert 15 bits/pixel data
654 rgb555msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
661 register guint8 *s; /* read 2 pixels at once */
663 register guint32 *s; /* read 2 pixels at once */
666 guint8 *srow = image->data, *orow = pixels;
668 width = image->width;
669 height = image->height;
670 bpl = image->bytes_per_line;
672 for (yy = 0; yy < height; yy++) {
674 o = (guint16 *) orow;
675 for (xx = 1; xx < width; xx += 2) {
676 register guint32 data;
678 /* swap endianness first */
679 data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
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;
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;
697 /* check for last remaining pixel */
699 register guint16 data;
701 data = *((short *) s);
702 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
704 data = *((short *) s);
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;
716 convert 15 bits/pixel data
721 rgb555alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
728 register guint16 *s; /* read 1 pixels at once */
734 guint8 *srow = image->data, *orow = pixels;
736 width = image->width;
737 height = image->height;
738 bpl = image->bytes_per_line;
740 for (yy = 0; yy < height; yy++) {
742 s = (guint16 *) srow;
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 */
753 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
754 | (data & 0x3e0) << 6 | (data & 0x380) << 1
755 | (data & 0x1f) << 19 | (data & 0x1c) << 14
758 /* swap endianness first */
759 data = s[0] | s[1] << 8;
761 *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
762 | (data & 0x3e0) << 14 | (data & 0x380) << 9
763 | (data & 0x1f) << 11 | (data & 0x1c) << 6
773 convert 15 bits/pixel data
778 rgb555amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
785 register guint16 *s; /* read 1 pixels at once */
791 guint8 *srow = image->data, *orow = pixels;
793 width = image->width;
794 height = image->height;
795 bpl = image->bytes_per_line;
797 for (yy = 0; yy < height; yy++) {
799 s = (guint16 *) srow;
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 */
809 /* swap endianness first */
810 data = s[0] | s[1] << 8;
812 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
813 | (data & 0x3e0) << 6 | (data & 0x380) << 1
814 | (data & 0x1f) << 19 | (data & 0x1c) << 14
818 *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
819 | (data & 0x3e0) << 14 | (data & 0x380) << 9
820 | (data & 0x1f) << 11 | (data & 0x1c) << 6
831 rgb888alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
837 guint8 *s; /* for byte order swapping */
839 guint8 *srow = image->data, *orow = pixels;
841 width = image->width;
842 height = image->height;
843 bpl = image->bytes_per_line;
845 d (printf ("32 bits/pixel with alpha\n"));
848 for (yy = 0; yy < height; yy++) {
851 for (xx = 0; xx < width; xx++) {
864 rgb888lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
870 guint8 *srow = image->data, *orow = pixels;
873 width = image->width;
874 height = image->height;
875 bpl = image->bytes_per_line;
877 d (printf ("32 bit, lsb, no alpha\n"));
879 for (yy = 0; yy < height; yy++) {
882 for (xx = 0; xx < width; xx++) {
894 rgb888amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
900 guint8 *srow = image->data, *orow = pixels;
905 guint8 *s; /* for byte order swapping */
909 d (printf ("32 bit, msb, with alpha\n"));
911 width = image->width;
912 height = image->height;
913 bpl = image->bytes_per_line;
916 for (yy = 0; yy < height; yy++) {
918 s = (guint32 *) srow;
919 o = (guint32 *) orow;
924 for (xx = 0; xx < width; xx++) {
932 *o++ = (*s << 8) | 0xff; /* untested */
942 rgb888msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
948 guint8 *srow = image->data, *orow = pixels;
952 d (printf ("32 bit, msb, no alpha\n"));
954 width = image->width;
955 height = image->height;
956 bpl = image->bytes_per_line;
958 for (yy = 0; yy < height; yy++) {
961 for (xx = 0; xx < width; xx++) {
973 This should work correctly with any display/any endianness, but will probably
977 convert_real_slow (XImage *image, guchar *pixels, int rowstride, xlib_colormap *cmap, int alpha)
982 guint8 *srow = image->data, *orow = pixels;
989 int red_shift, red_prec, green_shift, green_prec, blue_shift, blue_prec;
991 width = image->width;
992 height = image->height;
993 bpl = image->bytes_per_line;
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);
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));
1005 for (yy = 0; yy < height; yy++) {
1008 for (xx = 0; xx < width; xx++) {
1009 pixel = XGetPixel (image, xx, yy);
1011 /* I assume this is right for static & greyscale's too? */
1016 *o++ = cmap->colors[pixel].red;
1017 *o++ = cmap->colors[pixel].green;
1018 *o++ = cmap->colors[pixel].blue;
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. */
1025 for (i = 24; i < 32; i += red_prec)
1026 component |= ((pixel & v->red_mask) << (32 - red_shift - red_prec)) >> i;
1029 for (i = 24; i < 32; i += green_prec)
1030 component |= ((pixel & v->green_mask) << (32 - green_shift - green_prec)) >> i;
1033 for (i = 24; i < 32; i += blue_prec)
1034 component |= ((pixel & v->blue_mask) << (32 - blue_shift - blue_prec)) >> i;
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;
1051 typedef void (* cfunc) (XImage *image, guchar *pixels, int rowstride, xlib_colormap *cmap);
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
1062 perform actual conversion
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.
1069 rgbconvert (XImage *image, guchar *pixels, int rowstride, int alpha, xlib_colormap *cmap)
1071 int index = (image->byte_order == MSBFirst) | (alpha != 0) << 1;
1072 int bank=5; /* default fallback converter */
1073 Visual *v = cmap->visual;
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));
1079 /* I assume this is right for static & greyscale's too? */
1084 switch (image->bits_per_pixel) {
1094 switch (image->depth) {
1096 if (v->red_mask == 0x7c00 && v->green_mask == 0x3e0 && v->blue_mask == 0x1f
1097 && image->bits_per_pixel == 16)
1101 if (v->red_mask == 0xf800 && v->green_mask == 0x7e0 && v->blue_mask == 0x1f
1102 && image->bits_per_pixel == 16)
1107 if (v->red_mask == 0xff0000 && v->green_mask == 0xff00 && v->blue_mask == 0xff
1108 && image->bits_per_pixel == 32)
1114 /* always use the slow version */
1118 d(printf("converting using conversion function in bank %d\n", bank));
1121 convert_real_slow(image, pixels, rowstride, cmap, alpha);
1124 (* convert_map[index]) (image, pixels, rowstride, cmap);
1129 xlib_window_is_viewable (Window w)
1131 XWindowAttributes wa;
1134 Window parent, root, *children;
1137 XGetWindowAttributes (gdk_pixbuf_dpy, w, &wa);
1138 if (wa.map_state != IsViewable)
1141 if (!XQueryTree (gdk_pixbuf_dpy, w, &root,
1142 &parent, &children, &nchildren))
1158 xlib_window_get_origin (Window w, gint *x, gint *y)
1161 return XTranslateCoordinates (gdk_pixbuf_dpy, w,
1162 RootWindow (gdk_pixbuf_dpy,
1164 0, 0, x, y, &child);
1167 /* Exported functions */
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
1175 * @visual: A visual if @src is a pixmap. If it is a window, this argument will
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.
1184 * Transfers image data from a Gdk drawable and converts it to an RGB(A)
1185 * representation inside a GdkPixbuf.
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.
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.
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
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
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.
1217 gdk_pixbuf_xlib_get_from_drawable (GdkPixbuf *dest,
1219 Colormap cmap, Visual *visual,
1220 int src_x, int src_y,
1221 int dest_x, int dest_y,
1222 int width, int height)
1224 int src_width, src_height;
1226 int rowstride, bpp, alpha;
1227 XWindowAttributes wa;
1228 xlib_colormap *x_cmap;
1231 /* General sanity checks */
1233 g_return_val_if_fail (src != 0, NULL);
1235 is_pixmap = drawable_is_pixmap (src);
1238 g_return_val_if_fail (cmap != 0, NULL);
1239 g_return_val_if_fail (visual != NULL, NULL);
1242 g_return_val_if_fail (xlib_window_is_viewable (src), NULL);
1245 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
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);
1253 /* Coordinate sanity checks */
1256 XGetWindowAttributes (gdk_pixbuf_dpy, src, &wa);
1257 src_width = wa.width;
1258 src_height = wa.height;
1261 int tx, ty, bwidth, depth;
1262 XGetGeometry (gdk_pixbuf_dpy, src, &root, &tx, &ty,
1263 &src_width, &src_height, &bwidth, &depth);
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);
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);
1278 int src_xorigin, src_yorigin;
1279 int screen_width, screen_height;
1280 int screen_srcx, screen_srcy;
1282 ret = xlib_window_get_origin (src, &src_xorigin, &src_yorigin);
1283 g_return_val_if_fail (ret != FALSE, NULL);
1285 screen_width = DisplayWidth (gdk_pixbuf_dpy, gdk_pixbuf_screen);
1286 screen_height = DisplayHeight (gdk_pixbuf_dpy, gdk_pixbuf_screen);
1288 screen_srcx = src_xorigin + src_x;
1289 screen_srcy = src_yorigin + src_y;
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);
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);
1301 /* Create the pixbuf if needed */
1303 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1304 FALSE, 8, width, height);
1306 XDestroyImage (image);
1311 /* Get the colormap if needed */
1318 x_cmap = xlib_get_colormap (cmap, visual);
1320 alpha = dest->has_alpha;
1321 rowstride = dest->rowstride;
1322 bpp = alpha ? 4 : 3;
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),
1331 xlib_colormap_free (x_cmap);
1332 XDestroyImage (image);