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 const 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)
155 register guint8 data;
157 guint8 *srow = image->data, *orow = pixels;
159 d (printf ("1 bits/pixel\n"));
161 /* convert upto 8 pixels/time */
162 /* its probably not worth trying to make this run very fast, who uses
163 1 bit displays anymore? */
164 width = image->width;
165 height = image->height;
166 bpl = image->bytes_per_line;
168 for (yy = 0; yy < height; yy++) {
171 for (xx = 0; xx < width; xx ++) {
172 data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;
173 *o++ = colormap->colors[data].red;
174 *o++ = colormap->colors[data].green;
175 *o++ = colormap->colors[data].blue;
183 convert 1 bits/pixel data
187 rgb1a (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
192 register guint8 data;
194 guint8 *srow = image->data, *orow = pixels;
197 d (printf ("1 bits/pixel\n"));
199 /* convert upto 8 pixels/time */
200 /* its probably not worth trying to make this run very fast, who uses
201 1 bit displays anymore? */
202 width = image->width;
203 height = image->height;
204 bpl = image->bytes_per_line;
206 for (xx = 0; xx < 2; xx++) {
208 remap[xx] = 0xff000000
209 | colormap->colors[xx].blue << 16
210 | colormap->colors[xx].green << 8
211 | colormap->colors[xx].red;
214 | colormap->colors[xx].red << 24
215 | colormap->colors[xx].green << 16
216 | colormap->colors[xx].blue << 8;
220 for (yy = 0; yy < height; yy++) {
223 for (xx = 0; xx < width; xx ++) {
224 data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;
233 convert 8 bits/pixel data
237 rgb8 (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
243 register guint32 data;
244 guint8 *srow = image->data, *orow = pixels;
248 width = image->width;
249 height = image->height;
250 bpl = image->bytes_per_line;
252 d (printf ("8 bit, no alpha output\n"));
254 mask = mask_table[image->depth];
256 for (yy = 0; yy < height; yy++) {
259 for (xx = 0; xx < width; xx++) {
261 *o++ = colormap->colors[data].red;
262 *o++ = colormap->colors[data].green;
263 *o++ = colormap->colors[data].blue;
271 convert 8 bits/pixel data
275 rgb8a (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
281 register guint32 data;
283 register guint8 *s; /* read 2 pixels at once */
285 guint8 *srow = image->data, *orow = pixels;
287 width = image->width;
288 height = image->height;
289 bpl = image->bytes_per_line;
291 d (printf ("8 bit, with alpha output\n"));
293 mask = mask_table[image->depth];
295 for (xx = 0; xx < colormap->size; xx++) {
297 remap[xx] = 0xff000000
298 | colormap->colors[xx].blue << 16
299 | colormap->colors[xx].green << 8
300 | colormap->colors[xx].red;
303 | colormap->colors[xx].red << 24
304 | colormap->colors[xx].green << 16
305 | colormap->colors[xx].blue << 8;
309 for (yy = 0; yy < height; yy++) {
311 o = (guint32 *) orow;
312 for (xx = 0; xx < width; xx ++) {
322 convert 16 bits/pixel data
327 rgb565lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
334 register guint32 *s; /* read 2 pixels at once */
336 register guint8 *s; /* read 2 pixels at once */
339 guint8 *srow = image->data, *orow = pixels;
341 width = image->width;
342 height = image->height;
343 bpl = image->bytes_per_line;
345 for (yy = 0; yy < height; yy++) {
347 s = (guint32 *) srow;
351 o = (guint16 *) orow;
352 for (xx = 1; xx < width; xx += 2) {
353 register guint32 data;
356 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
357 | (data & 0x7e0) << 5 | (data & 0x600) >> 1;
358 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
359 | (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;
360 *o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25
361 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
363 /* swap endianness first */
364 data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
366 *o++ = (data & 0xf800) | (data & 0xe000) >> 5
367 | (data & 0x7e0) >> 3 | (data & 0x600) >> 9;
368 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
369 | (data & 0xf8000000) >> 24 | (data & 0xe0000000) >> 29;
370 *o++ = (data & 0x7e00000) >> 11 | (data & 0x6000000) >> 17
371 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
374 /* check for last remaining pixel */
376 register guint16 data;
378 data = *((short *) s);
380 data = *((short *) s);
381 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
383 ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
384 ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
385 ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
393 convert 16 bits/pixel data
398 rgb565msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
405 register guint8 *s; /* need to swap data order */
407 register guint32 *s; /* read 2 pixels at once */
410 guint8 *srow = image->data, *orow = pixels;
412 width = image->width;
413 height = image->height;
414 bpl = image->bytes_per_line;
416 for (yy = 0; yy < height; yy++) {
420 s = (guint32 *) srow;
422 o = (guint16 *) orow;
423 for (xx = 1; xx < width; xx += 2) {
424 register guint32 data;
426 /* swap endianness first */
427 data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
429 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
430 | (data & 0x7e0) << 5 | (data & 0x600) >> 1;
431 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
432 | (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;
433 *o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25
434 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
437 *o++ = (data & 0xf800) | (data & 0xe000) >> 5
438 | (data & 0x7e0) >> 3 | (data & 0x600) >> 9;
439 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
440 | (data & 0xf8000000) >> 24 | (data & 0xe0000000) >> 29;
441 *o++ = (data & 0x7e00000) >> 11 | (data & 0x6000000) >> 17
442 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
445 /* check for last remaining pixel */
447 register guint16 data;
449 data = *((short *) s);
450 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
452 data = *((short *) s);
454 ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
455 ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
456 ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
464 convert 16 bits/pixel data
469 rgb565alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
476 register guint16 *s; /* read 1 pixels at once */
482 guint8 *srow = image->data, *orow = pixels;
484 width = image->width;
485 height = image->height;
486 bpl = image->bytes_per_line;
488 for (yy = 0; yy < height; yy++) {
490 s = (guint16 *) srow;
494 o = (guint32 *) orow;
495 for (xx = 0; xx < width; xx ++) {
496 register guint32 data;
497 /* rrrrrggg gggbbbbb -> rrrrrRRR ggggggGG bbbbbBBB aaaaaaaa */
498 /* little endian: aaaaaaaa bbbbbBBB ggggggGG rrrrrRRR */
501 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
502 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
503 | (data & 0x1f) << 19 | (data & 0x1c) << 14
506 /* swap endianness first */
507 data = s[0] | s[1] << 8;
509 *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
510 | (data & 0x7e0) << 13 | (data & 0x600) << 7
511 | (data & 0x1f) << 11 | (data & 0x1c) << 6
521 convert 16 bits/pixel data
526 rgb565amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
535 register guint16 *s; /* read 1 pixels at once */
539 guint8 *srow = image->data, *orow = pixels;
541 width = image->width;
542 height = image->height;
543 bpl = image->bytes_per_line;
545 for (yy = 0; yy < height; yy++) {
549 s = (guint16 *) 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 */
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[1] | s[0] << 8 | s[3] << 16 | s[2] << 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++) {
676 s = (guint32 *) srow;
678 o = (guint16 *) orow;
679 for (xx = 1; xx < width; xx += 2) {
680 register guint32 data;
682 /* swap endianness first */
683 data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
685 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
686 | (data & 0x3e0) << 6 | (data & 0x380) << 1;
687 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
688 | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
689 *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
690 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
693 *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
694 | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
695 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
696 | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
697 *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
698 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
701 /* check for last remaining pixel */
703 register guint16 data;
705 data = *((short *) s);
706 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
708 data = *((short *) s);
710 ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
711 ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
712 ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
720 convert 15 bits/pixel data
725 rgb555alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
732 register guint16 *s; /* read 1 pixels at once */
738 guint8 *srow = image->data, *orow = pixels;
740 width = image->width;
741 height = image->height;
742 bpl = image->bytes_per_line;
744 for (yy = 0; yy < height; yy++) {
746 s = (guint16 *) srow;
750 o = (guint32 *) orow;
751 for (xx = 0; xx < width; xx++) {
752 register guint32 data;
753 /* rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
754 /* little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
757 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
758 | (data & 0x3e0) << 6 | (data & 0x380) << 1
759 | (data & 0x1f) << 19 | (data & 0x1c) << 14
762 /* swap endianness first */
763 data = s[0] | s[1] << 8;
765 *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
766 | (data & 0x3e0) << 14 | (data & 0x380) << 9
767 | (data & 0x1f) << 11 | (data & 0x1c) << 6
777 convert 15 bits/pixel data
782 rgb555amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
789 register guint16 *s; /* read 1 pixels at once */
795 guint8 *srow = image->data, *orow = pixels;
797 width = image->width;
798 height = image->height;
799 bpl = image->bytes_per_line;
801 for (yy = 0; yy < height; yy++) {
803 s = (guint16 *) srow;
807 o = (guint32 *) orow;
808 for (xx = 0; xx < width; xx++) {
809 register guint32 data;
810 /* rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
811 /* little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
813 /* swap endianness first */
814 data = s[0] | s[1] << 8;
816 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
817 | (data & 0x3e0) << 6 | (data & 0x380) << 1
818 | (data & 0x1f) << 19 | (data & 0x1c) << 14
822 *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
823 | (data & 0x3e0) << 14 | (data & 0x380) << 9
824 | (data & 0x1f) << 11 | (data & 0x1c) << 6
835 rgb888alsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
841 guint8 *s; /* for byte order swapping */
843 guint8 *srow = image->data, *orow = pixels;
845 width = image->width;
846 height = image->height;
847 bpl = image->bytes_per_line;
849 d (printf ("32 bits/pixel with alpha\n"));
852 for (yy = 0; yy < height; yy++) {
855 for (xx = 0; xx < width; xx++) {
868 rgb888lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
874 guint8 *srow = image->data, *orow = pixels;
877 width = image->width;
878 height = image->height;
879 bpl = image->bytes_per_line;
881 d (printf ("32 bit, lsb, no alpha\n"));
883 for (yy = 0; yy < height; yy++) {
886 for (xx = 0; xx < width; xx++) {
898 rgb888amsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
904 guint8 *srow = image->data, *orow = pixels;
909 guint8 *s; /* for byte order swapping */
913 d (printf ("32 bit, msb, with alpha\n"));
915 width = image->width;
916 height = image->height;
917 bpl = image->bytes_per_line;
920 for (yy = 0; yy < height; yy++) {
922 s = (guint32 *) srow;
923 o = (guint32 *) orow;
928 for (xx = 0; xx < width; xx++) {
936 *o++ = (*s << 8) | 0xff; /* untested */
946 rgb888msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap)
952 guint8 *srow = image->data, *orow = pixels;
956 d (printf ("32 bit, msb, no alpha\n"));
958 width = image->width;
959 height = image->height;
960 bpl = image->bytes_per_line;
962 for (yy = 0; yy < height; yy++) {
965 for (xx = 0; xx < width; xx++) {
977 This should work correctly with any display/any endianness, but will probably
981 convert_real_slow (XImage *image, guchar *pixels, int rowstride, xlib_colormap *cmap, int alpha)
986 guint8 *srow = image->data, *orow = pixels;
992 int red_shift, red_prec, green_shift, green_prec, blue_shift, blue_prec;
994 width = image->width;
995 height = image->height;
996 bpl = image->bytes_per_line;
999 visual_decompose_mask (v->red_mask, &red_shift, &red_prec);
1000 visual_decompose_mask (v->green_mask, &green_shift, &green_prec);
1001 visual_decompose_mask (v->blue_mask, &blue_shift, &blue_prec);
1003 d(printf("rgb mask/shift/prec = %x:%x:%x %d:%d:%d %d:%d:%d\n",
1004 v->red_mask, v->green_mask, v->blue_mask,
1005 red_shift, green_shift, blue_shift,
1006 red_prec, green_prec, blue_prec));
1008 for (yy = 0; yy < height; yy++) {
1010 for (xx = 0; xx < width; xx++) {
1011 pixel = XGetPixel (image, xx, yy);
1013 /* I assume this is right for static & greyscale's too? */
1018 *o++ = cmap->colors[pixel].red;
1019 *o++ = cmap->colors[pixel].green;
1020 *o++ = cmap->colors[pixel].blue;
1023 /* This is odd because it must sometimes shift left (otherwise
1024 I'd just shift >> (*_shift - 8 + *_prec + <0-7>). This logic
1025 should work for all bit sizes/shifts/etc. */
1027 for (i = 24; i < 32; i += red_prec)
1028 component |= ((pixel & v->red_mask) << (32 - red_shift - red_prec)) >> i;
1031 for (i = 24; i < 32; i += green_prec)
1032 component |= ((pixel & v->green_mask) << (32 - green_shift - green_prec)) >> i;
1035 for (i = 24; i < 32; i += blue_prec)
1036 component |= ((pixel & v->blue_mask) << (32 - blue_shift - blue_prec)) >> i;
1040 *o++ = cmap->colors[((pixel & v->red_mask) << (32 - red_shift - red_prec)) >> 24].red;
1041 *o++ = cmap->colors[((pixel & v->green_mask) << (32 - green_shift - green_prec)) >> 24].green;
1042 *o++ = cmap->colors[((pixel & v->blue_mask) << (32 - blue_shift - blue_prec)) >> 24].blue;
1053 typedef void (* cfunc) (XImage *image, guchar *pixels, int rowstride, xlib_colormap *cmap);
1055 static const cfunc convert_map[] = {
1056 rgb1,rgb1,rgb1a,rgb1a,
1057 rgb8,rgb8,rgb8a,rgb8a,
1058 rgb555lsb,rgb555msb,rgb555alsb,rgb555amsb,
1059 rgb565lsb,rgb565msb,rgb565alsb,rgb565amsb,
1060 rgb888lsb,rgb888msb,rgb888alsb,rgb888amsb
1064 perform actual conversion
1066 If we can, try and use the optimised code versions, but as a default
1067 fallback, and always for direct colour, use the generic/slow but complete
1068 conversion function.
1071 rgbconvert (XImage *image, guchar *pixels, int rowstride, int alpha, xlib_colormap *cmap)
1073 int index = (image->byte_order == MSBFirst) | (alpha != 0) << 1;
1074 int bank=5; /* default fallback converter */
1075 Visual *v = cmap->visual;
1077 d(printf("masks = %x:%x:%x\n", v->red_mask, v->green_mask, v->blue_mask));
1078 d(printf("image depth = %d, bpp = %d\n", image->depth, image->bits_per_pixel));
1081 /* I assume this is right for static & greyscale's too? */
1086 switch (image->bits_per_pixel) {
1096 switch (image->depth) {
1098 if (v->red_mask == 0x7c00 && v->green_mask == 0x3e0 && v->blue_mask == 0x1f
1099 && image->bits_per_pixel == 16)
1103 if (v->red_mask == 0xf800 && v->green_mask == 0x7e0 && v->blue_mask == 0x1f
1104 && image->bits_per_pixel == 16)
1109 if (v->red_mask == 0xff0000 && v->green_mask == 0xff00 && v->blue_mask == 0xff
1110 && image->bits_per_pixel == 32)
1116 /* always use the slow version */
1120 d(printf("converting using conversion function in bank %d\n", bank));
1123 convert_real_slow(image, pixels, rowstride, cmap, alpha);
1126 (* convert_map[index]) (image, pixels, rowstride, cmap);
1131 xlib_window_is_viewable (Window w)
1133 XWindowAttributes wa;
1136 Window parent, root, *children;
1139 XGetWindowAttributes (gdk_pixbuf_dpy, w, &wa);
1140 if (wa.map_state != IsViewable)
1143 if (!XQueryTree (gdk_pixbuf_dpy, w, &root,
1144 &parent, &children, &nchildren))
1150 if ((parent == root) || (w == root))
1160 xlib_window_get_origin (Window w, gint *x, gint *y)
1163 return XTranslateCoordinates (gdk_pixbuf_dpy, w,
1164 RootWindow (gdk_pixbuf_dpy,
1166 0, 0, x, y, &child);
1169 /* Exported functions */
1172 * gdk_pixbuf_xlib_get_from_drawable:
1173 * @dest: Destination pixbuf, or NULL if a new pixbuf should be created.
1174 * @src: Source drawable.
1175 * @cmap: A colormap if @src is a pixmap. If it is a window, this argument will
1177 * @visual: A visual if @src is a pixmap. If it is a window, this argument will
1179 * @src_x: Source X coordinate within drawable.
1180 * @src_y: Source Y coordinate within drawable.
1181 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1182 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1183 * @width: Width in pixels of region to get.
1184 * @height: Height in pixels of region to get.
1186 * Transfers image data from a Gdk drawable and converts it to an RGB(A)
1187 * representation inside a GdkPixbuf.
1189 * If the drawable @src is a pixmap, then a suitable colormap must be specified,
1190 * since pixmaps are just blocks of pixel data without an associated colormap.
1191 * If the drawable is a window, the @cmap argument will be ignored and the
1192 * window's own colormap will be used instead.
1194 * If the specified destination pixbuf @dest is #NULL, then this function will
1195 * create an RGB pixbuf with 8 bits per channel and no alpha, with the same size
1196 * specified by the @width and @height arguments. In this case, the @dest_x and
1197 * @dest_y arguments must be specified as 0, otherwise the function will return
1198 * #NULL. If the specified destination pixbuf is not NULL and it contains alpha
1199 * information, then the filled pixels will be set to full opacity.
1201 * If the specified drawable is a pixmap, then the requested source rectangle
1202 * must be completely contained within the pixmap, otherwise the function will
1205 * If the specified drawable is a window, then it must be viewable, i.e. all of
1206 * its ancestors up to the root window must be mapped. Also, the specified
1207 * source rectangle must be completely contained within the window and within
1208 * the screen. If regions of the window are obscured by noninferior windows, the
1209 * contents of those regions are undefined. The contents of regions obscured by
1210 * inferior windows of a different depth than that of the source window will also
1213 * Return value: The same pixbuf as @dest if it was non-NULL, or a newly-created
1214 * pixbuf with a reference count of 1 if no destination pixbuf was specified; in
1215 * the latter case, NULL will be returned if not enough memory could be
1216 * allocated for the pixbuf to be created.
1219 gdk_pixbuf_xlib_get_from_drawable (GdkPixbuf *dest,
1221 Colormap cmap, Visual *visual,
1222 int src_x, int src_y,
1223 int dest_x, int dest_y,
1224 int width, int height)
1226 int src_width, src_height;
1228 int rowstride, bpp, alpha;
1229 XWindowAttributes wa;
1230 xlib_colormap *x_cmap;
1233 /* General sanity checks */
1235 g_return_val_if_fail (src != 0, NULL);
1237 is_pixmap = drawable_is_pixmap (src);
1240 g_return_val_if_fail (cmap != 0, NULL);
1241 g_return_val_if_fail (visual != NULL, NULL);
1244 g_return_val_if_fail (xlib_window_is_viewable (src), NULL);
1247 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1249 g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1250 g_return_val_if_fail (dest->n_channels == 3
1251 || dest->n_channels == 4, NULL);
1252 g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1255 /* Coordinate sanity checks */
1258 XGetWindowAttributes (gdk_pixbuf_dpy, src, &wa);
1259 src_width = wa.width;
1260 src_height = wa.height;
1263 int tx, ty, bwidth, depth;
1264 XGetGeometry (gdk_pixbuf_dpy, src, &root, &tx, &ty,
1265 &src_width, &src_height, &bwidth, &depth);
1268 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1269 g_return_val_if_fail (src_x + width <= src_width
1270 && src_y + height <= src_height, NULL);
1273 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1274 g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1275 g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1280 int src_xorigin, src_yorigin;
1281 int screen_width, screen_height;
1282 int screen_srcx, screen_srcy;
1284 ret = xlib_window_get_origin (src, &src_xorigin, &src_yorigin);
1285 g_return_val_if_fail (ret != FALSE, NULL);
1287 screen_width = DisplayWidth (gdk_pixbuf_dpy, gdk_pixbuf_screen);
1288 screen_height = DisplayHeight (gdk_pixbuf_dpy, gdk_pixbuf_screen);
1290 screen_srcx = src_xorigin + src_x;
1291 screen_srcy = src_yorigin + src_y;
1293 g_return_val_if_fail (screen_srcx >= 0 && screen_srcy >= 0, NULL);
1294 g_return_val_if_fail (screen_srcx + width <= screen_width, NULL);
1295 g_return_val_if_fail (screen_srcy + height <= screen_height, NULL);
1298 /* Get Image in ZPixmap format (packed bits). */
1299 image = XGetImage (gdk_pixbuf_dpy, src, src_x, src_y,
1300 width, height, AllPlanes, ZPixmap);
1301 g_return_val_if_fail (image != NULL, NULL);
1303 /* Create the pixbuf if needed */
1305 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1306 FALSE, 8, width, height);
1308 XDestroyImage (image);
1313 /* Get the colormap if needed */
1320 x_cmap = xlib_get_colormap (cmap, visual);
1322 alpha = dest->has_alpha;
1323 rowstride = dest->rowstride;
1324 bpp = alpha ? 4 : 3;
1326 /* we offset into the image data based on the position we are retrieving from */
1327 rgbconvert (image, dest->pixels +
1328 (dest_y * rowstride) + (dest_x * bpp),
1333 xlib_colormap_free (x_cmap);
1334 XDestroyImage (image);