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[1] | s[0] << 8 | s[3] << 16 | s[2] << 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[1] | s[0] << 8 | s[3] << 16 | s[2] << 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++) {
553 s = (guint16 *) srow;
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 */
561 /* swap endianness first */
562 data = s[0] | s[1] << 8;
564 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
565 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
566 | (data & 0x1f) << 19 | (data & 0x1c) << 14
570 *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
571 | (data & 0x7e0) << 13 | (data & 0x600) << 7
572 | (data & 0x1f) << 11 | (data & 0x1c) << 6
582 convert 15 bits/pixel data
587 rgb555lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
594 register guint32 *s; /* read 2 pixels at once */
596 register guint8 *s; /* read 2 pixels at once */
599 guint8 *srow = image->data, *orow = pixels;
601 width = image->width;
602 height = image->height;
603 bpl = image->bytes_per_line;
605 for (yy = 0; yy < height; yy++) {
607 s = (guint32 *) srow;
611 o = (guint16 *) orow;
612 for (xx = 1; xx < width; xx += 2) {
613 register guint32 data;
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;
623 /* swap endianness first */
624 data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
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;
634 /* check for last remaining pixel */
636 register guint16 data;
638 data = *((short *) s);
640 data = *((short *) s);
641 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
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;
653 convert 15 bits/pixel data
658 rgb555msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
665 register guint8 *s; /* read 2 pixels at once */
667 register guint32 *s; /* read 2 pixels at once */
670 guint8 *srow = image->data, *orow = pixels;
672 width = image->width;
673 height = image->height;
674 bpl = image->bytes_per_line;
676 for (yy = 0; yy < height; yy++) {
680 s = (guint32 *) srow;
682 o = (guint16 *) orow;
683 for (xx = 1; xx < width; xx += 2) {
684 register guint32 data;
686 /* swap endianness first */
687 data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
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;
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;
705 /* check for last remaining pixel */
707 register guint16 data;
709 data = *((short *) s);
710 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
712 data = *((short *) s);
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;
724 convert 15 bits/pixel data
729 rgb555alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
736 register guint16 *s; /* read 1 pixels at once */
742 guint8 *srow = image->data, *orow = pixels;
744 width = image->width;
745 height = image->height;
746 bpl = image->bytes_per_line;
748 for (yy = 0; yy < height; yy++) {
750 s = (guint16 *) srow;
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 */
761 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
762 | (data & 0x3e0) << 6 | (data & 0x380) << 1
763 | (data & 0x1f) << 19 | (data & 0x1c) << 14
766 /* swap endianness first */
767 data = s[0] | s[1] << 8;
769 *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
770 | (data & 0x3e0) << 14 | (data & 0x380) << 9
771 | (data & 0x1f) << 11 | (data & 0x1c) << 6
781 convert 15 bits/pixel data
786 rgb555amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
793 register guint16 *s; /* read 1 pixels at once */
799 guint8 *srow = image->data, *orow = pixels;
801 width = image->width;
802 height = image->height;
803 bpl = image->bytes_per_line;
805 for (yy = 0; yy < height; yy++) {
807 s = (guint16 *) srow;
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 */
817 /* swap endianness first */
818 data = s[0] | s[1] << 8;
820 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
821 | (data & 0x3e0) << 6 | (data & 0x380) << 1
822 | (data & 0x1f) << 19 | (data & 0x1c) << 14
826 *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
827 | (data & 0x3e0) << 14 | (data & 0x380) << 9
828 | (data & 0x1f) << 11 | (data & 0x1c) << 6
839 rgb888alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
845 guint8 *s; /* for byte order swapping */
847 guint8 *srow = image->data, *orow = pixels;
849 width = image->width;
850 height = image->height;
851 bpl = image->bytes_per_line;
853 d (printf ("32 bits/pixel with alpha\n"));
856 for (yy = 0; yy < height; yy++) {
859 for (xx = 0; xx < width; xx++) {
872 rgb888lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
878 guint8 *srow = image->data, *orow = pixels;
881 width = image->width;
882 height = image->height;
883 bpl = image->bytes_per_line;
885 d (printf ("32 bit, lsb, no alpha\n"));
887 for (yy = 0; yy < height; yy++) {
890 for (xx = 0; xx < width; xx++) {
902 rgb888amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
908 guint8 *srow = image->data, *orow = pixels;
913 guint8 *s; /* for byte order swapping */
917 d (printf ("32 bit, msb, with alpha\n"));
919 width = image->width;
920 height = image->height;
921 bpl = image->bytes_per_line;
924 for (yy = 0; yy < height; yy++) {
926 s = (guint32 *) srow;
927 o = (guint32 *) orow;
932 for (xx = 0; xx < width; xx++) {
940 *o++ = (*s << 8) | 0xff; /* untested */
950 rgb888msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
956 guint8 *srow = image->data, *orow = pixels;
960 d (printf ("32 bit, msb, no alpha\n"));
962 width = image->width;
963 height = image->height;
964 bpl = image->bytes_per_line;
966 for (yy = 0; yy < height; yy++) {
969 for (xx = 0; xx < width; xx++) {
981 This should work correctly with any display/any endianness, but will probably
985 convert_real_slow (XImage *image, guchar *pixels, int rowstride, xlib_colormap *cmap, int alpha)
990 guint8 *srow = image->data, *orow = pixels;
997 int red_shift, red_prec, green_shift, green_prec, blue_shift, blue_prec;
999 width = image->width;
1000 height = image->height;
1001 bpl = image->bytes_per_line;
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);
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));
1013 for (yy = 0; yy < height; yy++) {
1016 for (xx = 0; xx < width; xx++) {
1017 pixel = XGetPixel (image, xx, yy);
1019 /* I assume this is right for static & greyscale's too? */
1024 *o++ = cmap->colors[pixel].red;
1025 *o++ = cmap->colors[pixel].green;
1026 *o++ = cmap->colors[pixel].blue;
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. */
1033 for (i = 24; i < 32; i += red_prec)
1034 component |= ((pixel & v->red_mask) << (32 - red_shift - red_prec)) >> i;
1037 for (i = 24; i < 32; i += green_prec)
1038 component |= ((pixel & v->green_mask) << (32 - green_shift - green_prec)) >> i;
1041 for (i = 24; i < 32; i += blue_prec)
1042 component |= ((pixel & v->blue_mask) << (32 - blue_shift - blue_prec)) >> i;
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;
1059 typedef void (* cfunc) (XImage *image, guchar *pixels, int rowstride, xlib_colormap *cmap);
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
1070 perform actual conversion
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.
1077 rgbconvert (XImage *image, guchar *pixels, int rowstride, int alpha, xlib_colormap *cmap)
1079 int index = (image->byte_order == MSBFirst) | (alpha != 0) << 1;
1080 int bank=5; /* default fallback converter */
1081 Visual *v = cmap->visual;
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));
1087 /* I assume this is right for static & greyscale's too? */
1092 switch (image->bits_per_pixel) {
1102 switch (image->depth) {
1104 if (v->red_mask == 0x7c00 && v->green_mask == 0x3e0 && v->blue_mask == 0x1f
1105 && image->bits_per_pixel == 16)
1109 if (v->red_mask == 0xf800 && v->green_mask == 0x7e0 && v->blue_mask == 0x1f
1110 && image->bits_per_pixel == 16)
1115 if (v->red_mask == 0xff0000 && v->green_mask == 0xff00 && v->blue_mask == 0xff
1116 && image->bits_per_pixel == 32)
1122 /* always use the slow version */
1126 d(printf("converting using conversion function in bank %d\n", bank));
1129 convert_real_slow(image, pixels, rowstride, cmap, alpha);
1132 (* convert_map[index]) (image, pixels, rowstride, cmap);
1137 xlib_window_is_viewable (Window w)
1139 XWindowAttributes wa;
1142 Window parent, root, *children;
1145 XGetWindowAttributes (gdk_pixbuf_dpy, w, &wa);
1146 if (wa.map_state != IsViewable)
1149 if (!XQueryTree (gdk_pixbuf_dpy, w, &root,
1150 &parent, &children, &nchildren))
1156 if ((parent == root) || (w == root))
1166 xlib_window_get_origin (Window w, gint *x, gint *y)
1169 return XTranslateCoordinates (gdk_pixbuf_dpy, w,
1170 RootWindow (gdk_pixbuf_dpy,
1172 0, 0, x, y, &child);
1175 /* Exported functions */
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
1183 * @visual: A visual if @src is a pixmap. If it is a window, this argument will
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.
1192 * Transfers image data from a Gdk drawable and converts it to an RGB(A)
1193 * representation inside a GdkPixbuf.
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.
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.
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
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
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.
1225 gdk_pixbuf_xlib_get_from_drawable (GdkPixbuf *dest,
1227 Colormap cmap, Visual *visual,
1228 int src_x, int src_y,
1229 int dest_x, int dest_y,
1230 int width, int height)
1232 int src_width, src_height;
1234 int rowstride, bpp, alpha;
1235 XWindowAttributes wa;
1236 xlib_colormap *x_cmap;
1239 /* General sanity checks */
1241 g_return_val_if_fail (src != 0, NULL);
1243 is_pixmap = drawable_is_pixmap (src);
1246 g_return_val_if_fail (cmap != 0, NULL);
1247 g_return_val_if_fail (visual != NULL, NULL);
1250 g_return_val_if_fail (xlib_window_is_viewable (src), NULL);
1253 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
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);
1261 /* Coordinate sanity checks */
1264 XGetWindowAttributes (gdk_pixbuf_dpy, src, &wa);
1265 src_width = wa.width;
1266 src_height = wa.height;
1269 int tx, ty, bwidth, depth;
1270 XGetGeometry (gdk_pixbuf_dpy, src, &root, &tx, &ty,
1271 &src_width, &src_height, &bwidth, &depth);
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);
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);
1286 int src_xorigin, src_yorigin;
1287 int screen_width, screen_height;
1288 int screen_srcx, screen_srcy;
1290 ret = xlib_window_get_origin (src, &src_xorigin, &src_yorigin);
1291 g_return_val_if_fail (ret != FALSE, NULL);
1293 screen_width = DisplayWidth (gdk_pixbuf_dpy, gdk_pixbuf_screen);
1294 screen_height = DisplayHeight (gdk_pixbuf_dpy, gdk_pixbuf_screen);
1296 screen_srcx = src_xorigin + src_x;
1297 screen_srcy = src_yorigin + src_y;
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);
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);
1309 /* Create the pixbuf if needed */
1311 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1312 FALSE, 8, width, height);
1314 XDestroyImage (image);
1319 /* Get the colormap if needed */
1326 x_cmap = xlib_get_colormap (cmap, visual);
1328 alpha = dest->has_alpha;
1329 rowstride = dest->rowstride;
1330 bpp = alpha ? 4 : 3;
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),
1339 xlib_colormap_free (x_cmap);
1340 XDestroyImage (image);