1 /* GdkPixbuf library - convert X drawable information to RGB
3 * Copyright (C) 1999 Michael Zucchi
5 * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
6 * Cody Russell <bratsche@dfw.net>
7 * Federico Mena-Quintero <federico@gimp.org>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
28 #include "gdk.h" /* For gdk_screen_width/gdk_screen_height */
31 #include "gdkvisual.h"
32 #include "gdkwindow.h"
33 #include "gdkpixbuf.h"
34 #include "gdk-pixbuf-private.h"
36 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
43 static guint32 mask_table[] = {
44 0x00000000, 0x00000001, 0x00000003, 0x00000007,
45 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
46 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
47 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
48 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
49 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
50 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
51 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
58 * convert 1 bits-pixel data
62 rgb1 (GdkImage *image,
65 GdkColormap *colormap)
73 guint8 *srow = image->mem, *orow = pixels;
75 d (printf ("1 bits/pixel\n"));
77 /* convert upto 8 pixels/time */
78 /* its probably not worth trying to make this run very fast, who uses
79 * 1 bit displays anymore?
82 height = image->height;
85 for (yy = 0; yy < height; yy++)
90 for (xx = 0; xx < width; xx ++)
92 data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;
93 *o++ = colormap->colors[data].red;
94 *o++ = colormap->colors[data].green;
95 *o++ = colormap->colors[data].blue;
103 * convert 1 bits/pixel data
107 rgb1a (GdkImage *image,
110 GdkColormap *colormap)
116 register guint8 data;
118 guint8 *srow = image->mem, *orow = pixels;
121 d (printf ("1 bits/pixel\n"));
123 /* convert upto 8 pixels/time */
124 /* its probably not worth trying to make this run very fast, who uses
125 * 1 bit displays anymore? */
126 width = image->width;
127 height = image->height;
130 for (xx = 0; xx < 2; xx++)
133 remap[xx] = 0xff000000
134 | colormap->colors[xx].blue << 16
135 | colormap->colors[xx].green << 8
136 | colormap->colors[xx].red;
139 | colormap->colors[xx].red << 24
140 | colormap->colors[xx].green << 16
141 | colormap->colors[xx].blue << 8;
145 for (yy = 0; yy < height; yy++)
150 for (xx = 0; xx < width; xx ++)
152 data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;
161 * convert 8 bits/pixel data
165 rgb8 (GdkImage *image,
168 GdkColormap *colormap)
174 register guint32 data;
175 guint8 *srow = image->mem, *orow = pixels;
179 width = image->width;
180 height = image->height;
183 d (printf ("8 bit, no alpha output\n"));
185 mask = mask_table[image->depth];
187 for (yy = 0; yy < height; yy++)
191 for (xx = 0; xx < width; xx++) {
193 *o++ = colormap->colors[data].red;
194 *o++ = colormap->colors[data].green;
195 *o++ = colormap->colors[data].blue;
203 * convert 8 bits/pixel data
207 rgb8a (GdkImage *image,
210 GdkColormap *colormap)
216 register guint32 data;
218 register guint8 *s; /* read 2 pixels at once */
220 guint8 *srow = image->mem, *orow = pixels;
222 width = image->width;
223 height = image->height;
226 d (printf ("8 bit, with alpha output\n"));
228 mask = mask_table[image->depth];
230 for (xx = 0; xx < colormap->size; xx++)
233 remap[xx] = 0xff000000
234 | colormap->colors[xx].blue << 16
235 | colormap->colors[xx].green << 8
236 | colormap->colors[xx].red;
239 | colormap->colors[xx].red << 24
240 | colormap->colors[xx].green << 16
241 | colormap->colors[xx].blue << 8;
245 for (yy = 0; yy < height; yy++)
248 o = (guint32 *) orow;
249 for (xx = 0; xx < width; xx ++)
260 * convert 16 bits/pixel data
265 rgb565lsb (GdkImage *image,
268 GdkColormap *colormap)
275 register guint32 *s; /* read 2 pixels at once */
277 register guint8 *s; /* read 2 pixels at once */
280 guint8 *srow = image->mem, *orow = pixels;
282 width = image->width;
283 height = image->height;
286 for (yy = 0; yy < height; yy++)
289 s = (guint32 *) srow;
293 o = (guint16 *) orow;
294 for (xx = 1; xx < width; xx += 2)
296 register guint32 data;
299 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
300 | (data & 0x7e0) << 5 | (data & 0x600) >> 1;
301 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
302 | (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;
303 *o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25
304 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
306 /* swap endianness first */
307 data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
309 *o++ = (data & 0xf800) | (data & 0xe000) >> 5
310 | (data & 0x7e0) >> 3 | (data & 0x600) >> 9;
311 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
312 | (data & 0xf8000000) >> 24 | (data & 0xe0000000) >> 29;
313 *o++ = (data & 0x7e00000) >> 11 | (data & 0x6000000) >> 17
314 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
317 /* check for last remaining pixel */
320 register guint16 data;
322 data = *((short *) s);
324 data = *((short *) s);
325 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
327 ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
328 ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
329 ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
337 * convert 16 bits/pixel data
342 rgb565msb (GdkImage *image,
345 GdkColormap *colormap)
352 register guint8 *s; /* need to swap data order */
354 register guint32 *s; /* read 2 pixels at once */
357 guint8 *srow = image->mem, *orow = pixels;
359 width = image->width;
360 height = image->height;
363 for (yy = 0; yy < height; yy++)
368 s = (guint32 *) srow;
370 o = (guint16 *) orow;
371 for (xx = 1; xx < width; xx += 2)
373 register guint32 data;
375 /* swap endianness first */
376 data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
378 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
379 | (data & 0x7e0) << 5 | (data & 0x600) >> 1;
380 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
381 | (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;
382 *o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25
383 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
386 *o++ = (data & 0xf800) | (data & 0xe000) >> 5
387 | (data & 0x7e0) >> 3 | (data & 0x600) >> 9;
388 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
389 | (data & 0xf8000000) >> 24 | (data & 0xe0000000) >> 29;
390 *o++ = (data & 0x7e00000) >> 11 | (data & 0x6000000) >> 17
391 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
394 /* check for last remaining pixel */
397 register guint16 data;
399 data = *((short *) s);
400 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
402 data = *((short *) s);
404 ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
405 ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
406 ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
414 * convert 16 bits/pixel data
419 rgb565alsb (GdkImage *image,
422 GdkColormap *colormap)
429 register guint16 *s; /* read 1 pixels at once */
435 guint8 *srow = image->mem, *orow = pixels;
437 width = image->width;
438 height = image->height;
441 for (yy = 0; yy < height; yy++)
444 s = (guint16 *) srow;
448 o = (guint32 *) orow;
449 for (xx = 0; xx < width; xx ++)
451 register guint32 data;
452 /* rrrrrggg gggbbbbb -> rrrrrRRR ggggggGG bbbbbBBB aaaaaaaa */
453 /* little endian: aaaaaaaa bbbbbBBB ggggggGG rrrrrRRR */
456 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
457 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
458 | (data & 0x1f) << 19 | (data & 0x1c) << 14
461 /* swap endianness first */
462 data = s[0] | s[1] << 8;
464 *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
465 | (data & 0x7e0) << 13 | (data & 0x600) << 7
466 | (data & 0x1f) << 11 | (data & 0x1c) << 6
476 * convert 16 bits/pixel data
481 rgb565amsb (GdkImage *image,
484 GdkColormap *colormap)
493 register guint16 *s; /* read 1 pixels at once */
497 guint8 *srow = image->mem, *orow = pixels;
499 width = image->width;
500 height = image->height;
503 for (yy = 0; yy < height; yy++)
506 o = (guint32 *) orow;
507 for (xx = 0; xx < width; xx ++)
509 register guint32 data;
510 /* rrrrrggg gggbbbbb -> rrrrrRRR gggggg00 bbbbbBBB aaaaaaaa */
511 /* little endian: aaaaaaaa bbbbbBBB gggggg00 rrrrrRRR */
513 /* swap endianness first */
514 data = s[0] | s[1] << 8;
516 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
517 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
518 | (data & 0x1f) << 19 | (data & 0x1c) << 14
522 *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
523 | (data & 0x7e0) << 13 | (data & 0x600) << 7
524 | (data & 0x1f) << 11 | (data & 0x1c) << 6
534 * convert 15 bits/pixel data
539 rgb555lsb (GdkImage *image,
542 GdkColormap *colormap)
549 register guint32 *s; /* read 2 pixels at once */
551 register guint8 *s; /* read 2 pixels at once */
554 guint8 *srow = image->mem, *orow = pixels;
556 width = image->width;
557 height = image->height;
560 for (yy = 0; yy < height; yy++)
563 s = (guint32 *) srow;
567 o = (guint16 *) orow;
568 for (xx = 1; xx < width; xx += 2)
570 register guint32 data;
573 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
574 | (data & 0x3e0) << 6 | (data & 0x380) << 1;
575 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
576 | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
577 *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
578 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
580 /* swap endianness first */
581 data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
583 *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
584 | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
585 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
586 | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
587 *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
588 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
591 /* check for last remaining pixel */
594 register guint16 data;
596 data = *((short *) s);
598 data = *((short *) s);
599 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
601 ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
602 ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
603 ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
611 * convert 15 bits/pixel data
616 rgb555msb (GdkImage *image,
619 GdkColormap *colormap)
626 register guint8 *s; /* read 2 pixels at once */
628 register guint32 *s; /* read 2 pixels at once */
631 guint8 *srow = image->mem, *orow = pixels;
633 width = image->width;
634 height = image->height;
637 for (yy = 0; yy < height; yy++)
640 o = (guint16 *) orow;
641 for (xx = 1; xx < width; xx += 2)
643 register guint32 data;
645 /* swap endianness first */
646 data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
648 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
649 | (data & 0x3e0) << 6 | (data & 0x380) << 1;
650 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
651 | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
652 *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
653 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
656 *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
657 | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
658 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
659 | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
660 *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
661 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
664 /* check for last remaining pixel */
667 register guint16 data;
669 data = *((short *) s);
670 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
672 data = *((short *) s);
674 ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
675 ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
676 ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
684 * convert 15 bits/pixel data
689 rgb555alsb (GdkImage *image,
692 GdkColormap *colormap)
699 register guint16 *s; /* read 1 pixels at once */
705 guint8 *srow = image->mem, *orow = pixels;
707 width = image->width;
708 height = image->height;
711 for (yy = 0; yy < height; yy++)
714 s = (guint16 *) srow;
718 o = (guint32 *) orow;
719 for (xx = 0; xx < width; xx++)
721 register guint32 data;
722 /* rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
723 /* little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
726 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
727 | (data & 0x3e0) << 6 | (data & 0x380) << 1
728 | (data & 0x1f) << 19 | (data & 0x1c) << 14
731 /* swap endianness first */
732 data = s[0] | s[1] << 8;
734 *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
735 | (data & 0x3e0) << 14 | (data & 0x380) << 9
736 | (data & 0x1f) << 11 | (data & 0x1c) << 6
746 * convert 15 bits/pixel data
751 rgb555amsb (GdkImage *image,
754 GdkColormap *colormap)
761 register guint16 *s; /* read 1 pixels at once */
767 guint8 *srow = image->mem, *orow = pixels;
769 width = image->width;
770 height = image->height;
773 for (yy = 0; yy < height; yy++)
776 s = (guint16 *) srow;
780 o = (guint32 *) orow;
781 for (xx = 0; xx < width; xx++)
783 register guint32 data;
784 /* rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
785 /* little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
787 /* swap endianness first */
788 data = s[0] | s[1] << 8;
790 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
791 | (data & 0x3e0) << 6 | (data & 0x380) << 1
792 | (data & 0x1f) << 19 | (data & 0x1c) << 14
796 *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
797 | (data & 0x3e0) << 14 | (data & 0x380) << 9
798 | (data & 0x1f) << 11 | (data & 0x1c) << 6
809 rgb888alsb (GdkImage *image,
812 GdkColormap *colormap)
818 guint8 *s; /* for byte order swapping */
820 guint8 *srow = image->mem, *orow = pixels;
822 width = image->width;
823 height = image->height;
826 d (printf ("32 bits/pixel with alpha\n"));
829 for (yy = 0; yy < height; yy++)
833 for (xx = 0; xx < width; xx++)
847 rgb888lsb (GdkImage *image,
850 GdkColormap *colormap)
856 guint8 *srow = image->mem, *orow = pixels;
859 width = image->width;
860 height = image->height;
863 d (printf ("32 bit, lsb, no alpha\n"));
865 for (yy = 0; yy < height; yy++)
869 for (xx = 0; xx < width; xx++)
882 rgb888amsb (GdkImage *image,
885 GdkColormap *colormap)
891 guint8 *srow = image->mem, *orow = pixels;
896 guint8 *s; /* for byte order swapping */
900 d (printf ("32 bit, msb, with alpha\n"));
902 width = image->width;
903 height = image->height;
907 for (yy = 0; yy < height; yy++)
910 s = (guint32 *) srow;
911 o = (guint32 *) orow;
916 for (xx = 0; xx < width; xx++)
925 *o++ = (*s << 8) | 0xff; /* untested */
935 rgb888msb (GdkImage *image,
938 GdkColormap *colormap)
944 guint8 *srow = image->mem, *orow = pixels;
948 d (printf ("32 bit, msb, no alpha\n"));
950 width = image->width;
951 height = image->height;
954 for (yy = 0; yy < height; yy++)
958 for (xx = 0; xx < width; xx++)
971 * This should work correctly with any display/any endianness, but will probably
975 convert_real_slow (GdkImage *image,
984 guint8 *srow = image->mem, *orow = pixels;
992 width = image->width;
993 height = image->height;
995 v = gdk_colormap_get_visual(cmap);
997 d(printf("rgb mask/shift/prec = %x:%x:%x %d:%d:%d %d:%d:%d\n",
998 v->red_mask, v->green_mask, v->blue_mask,
999 v->red_shift, v->green_shift, v->blue_shift,
1000 v->red_prec, v->green_prec, v->blue_prec));
1002 for (yy = 0; yy < height; yy++)
1006 for (xx = 0; xx < width; xx++)
1008 pixel = gdk_image_get_pixel(image, xx, yy);
1011 /* I assume this is right for static & greyscale's too? */
1012 case GDK_VISUAL_STATIC_GRAY:
1013 case GDK_VISUAL_GRAYSCALE:
1014 case GDK_VISUAL_STATIC_COLOR:
1015 case GDK_VISUAL_PSEUDO_COLOR:
1016 *o++ = cmap->colors[pixel].red;
1017 *o++ = cmap->colors[pixel].green;
1018 *o++ = cmap->colors[pixel].blue;
1020 case GDK_VISUAL_TRUE_COLOR:
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.
1026 for (i = 24; i < 32; i += v->red_prec)
1027 component |= ((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> i;
1030 for (i = 24; i < 32; i += v->green_prec)
1031 component |= ((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> i;
1034 for (i = 24; i < 32; i += v->blue_prec)
1035 component |= ((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> i;
1038 case GDK_VISUAL_DIRECT_COLOR:
1039 *o++ = cmap->colors[((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> 24].red;
1040 *o++ = cmap->colors[((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> 24].green;
1041 *o++ = cmap->colors[((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> 24].blue;
1052 typedef void (* cfunc) (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *cmap);
1054 static cfunc convert_map[] = {
1055 rgb1,rgb1,rgb1a,rgb1a,
1056 rgb8,rgb8,rgb8a,rgb8a,
1057 rgb555lsb,rgb555msb,rgb555alsb,rgb555amsb,
1058 rgb565lsb,rgb565msb,rgb565alsb,rgb565amsb,
1059 rgb888lsb,rgb888msb,rgb888alsb,rgb888amsb
1063 * perform actual conversion
1065 * If we can, try and use the optimised code versions, but as a default
1066 * fallback, and always for direct colour, use the generic/slow but complete
1067 * conversion function.
1070 rgbconvert (GdkImage *image,
1076 int index = (image->byte_order == GDK_MSB_FIRST) | (alpha != 0) << 1;
1077 int bank=5; /* default fallback converter */
1078 GdkVisual *v = gdk_colormap_get_visual(cmap);
1080 d(printf("masks = %x:%x:%x\n", v->red_mask, v->green_mask, v->blue_mask));
1081 d(printf("image depth = %d, bpp = %d\n", image->depth, image->bpp));
1085 /* I assume this is right for static & greyscale's too? */
1086 case GDK_VISUAL_STATIC_GRAY:
1087 case GDK_VISUAL_GRAYSCALE:
1088 case GDK_VISUAL_STATIC_COLOR:
1089 case GDK_VISUAL_PSEUDO_COLOR:
1100 case GDK_VISUAL_TRUE_COLOR:
1101 switch (image->depth)
1104 if (v->red_mask == 0x7c00 && v->green_mask == 0x3e0 && v->blue_mask == 0x1f
1105 && image->bpp == 16)
1109 if (v->red_mask == 0xf800 && v->green_mask == 0x7e0 && v->blue_mask == 0x1f
1110 && image->bpp == 16)
1115 if (v->red_mask == 0xff0000 && v->green_mask == 0xff00 && v->blue_mask == 0xff
1116 && image->bpp == 32)
1121 case GDK_VISUAL_DIRECT_COLOR:
1122 /* always use the slow version */
1126 d(printf("converting using conversion function in bank %d\n", bank));
1130 convert_real_slow(image, pixels, rowstride, cmap, alpha);
1135 (* convert_map[index]) (image, pixels, rowstride, cmap);
1140 /* Exported functions */
1143 * gdk_pixbuf_get_from_drawable:
1144 * @dest: Destination pixbuf, or NULL if a new pixbuf should be created.
1145 * @src: Source drawable.
1146 * @cmap: A colormap if @src is a pixmap. If it is a window, this argument will
1148 * @src_x: Source X coordinate within drawable.
1149 * @src_y: Source Y coordinate within drawable.
1150 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1151 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1152 * @width: Width in pixels of region to get.
1153 * @height: Height in pixels of region to get.
1155 * Transfers image data from a Gdk drawable and converts it to an RGB(A)
1156 * representation inside a GdkPixbuf.
1158 * If the drawable @src is a pixmap, then a suitable colormap must be specified,
1159 * since pixmaps are just blocks of pixel data without an associated colormap.
1160 * If the drawable is a window, the @cmap argument will be ignored and the
1161 * window's own colormap will be used instead.
1163 * If the specified destination pixbuf @dest is #NULL, then this function will
1164 * create an RGB pixbuf with 8 bits per channel and no alpha, with the same size
1165 * specified by the @width and @height arguments. In this case, the @dest_x and
1166 * @dest_y arguments must be specified as 0, otherwise the function will return
1167 * #NULL. If the specified destination pixbuf is not NULL and it contains alpha
1168 * information, then the filled pixels will be set to full opacity.
1170 * If the specified drawable is a pixmap, then the requested source rectangle
1171 * must be completely contained within the pixmap, otherwise the function will
1174 * If the specified drawable is a window, then it must be viewable, i.e. all of
1175 * its ancestors up to the root window must be mapped. Also, the specified
1176 * source rectangle must be completely contained within the window and within
1177 * the screen. If regions of the window are obscured by noninferior windows, the
1178 * contents of those regions are undefined. The contents of regions obscured by
1179 * inferior windows of a different depth than that of the source window will also
1182 * Return value: The same pixbuf as @dest if it was non-NULL, or a newly-created
1183 * pixbuf with a reference count of 1 if no destination pixbuf was specified; in
1184 * the latter case, NULL will be returned if not enough memory could be
1185 * allocated for the pixbuf to be created.
1188 gdk_pixbuf_get_from_drawable (GdkPixbuf *dest,
1191 int src_x, int src_y,
1192 int dest_x, int dest_y,
1193 int width, int height)
1195 int src_width, src_height;
1197 int rowstride, bpp, alpha;
1199 /* General sanity checks */
1201 g_return_val_if_fail (src != NULL, NULL);
1203 if (GDK_IS_WINDOW (src))
1204 /* FIXME: this is not perfect, since is_viewable() only tests
1205 * recursively up the Gdk parent window tree, but stops at
1206 * foreign windows or Gdk toplevels. I.e. if a window manager
1207 * unmapped one of its own windows, this won't work.
1209 g_return_val_if_fail (gdk_window_is_viewable (src), NULL);
1212 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1215 g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1216 g_return_val_if_fail (dest->n_channels == 3 || dest->n_channels == 4, NULL);
1217 g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1221 cmap = gdk_drawable_get_colormap (src);
1225 g_warning ("%s: Source drawable has no colormap; either pass "
1226 "in a colormap, or set the colormap on the drawable "
1227 "with gdk_drawable_set_colormap()", G_STRLOC);
1231 /* Coordinate sanity checks */
1233 gdk_drawable_get_size (src, &src_width, &src_height);
1235 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1236 g_return_val_if_fail (src_x + width <= src_width && src_y + height <= src_height, NULL);
1240 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1241 g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1242 g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1245 if (GDK_IS_WINDOW (src))
1248 int src_xorigin, src_yorigin;
1249 int screen_width, screen_height;
1250 int screen_srcx, screen_srcy;
1252 ret = gdk_window_get_origin (src, &src_xorigin, &src_yorigin);
1253 g_return_val_if_fail (ret != FALSE, NULL);
1255 screen_width = gdk_screen_width ();
1256 screen_height = gdk_screen_height ();
1258 screen_srcx = src_xorigin + src_x;
1259 screen_srcy = src_yorigin + src_y;
1261 g_return_val_if_fail (screen_srcx >= 0 && screen_srcy >= 0, NULL);
1262 g_return_val_if_fail (screen_srcx + width <= screen_width, NULL);
1263 g_return_val_if_fail (screen_srcy + height <= screen_height, NULL);
1266 /* Get Image in ZPixmap format (packed bits). */
1267 image = gdk_image_get (src, src_x, src_y, width, height);
1268 g_return_val_if_fail (image != NULL, NULL);
1270 /* Create the pixbuf if needed */
1273 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
1276 gdk_image_destroy(image);
1281 alpha = dest->has_alpha;
1282 rowstride = dest->rowstride;
1283 bpp = alpha ? 4 : 3;
1285 /* we offset into the image data based on the position we are retrieving from */
1286 rgbconvert (image, dest->pixels +
1287 (dest_y * rowstride) + (dest_x * bpp),
1292 gdk_image_destroy(image);