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"
35 #include "gdkinternals.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 * convert bitmap data to pixbuf without alpha,
60 * without using a colormap
63 bitmap1 (GdkImage *image,
75 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
77 d (printf ("bitmap, no alpha\n"));
81 for (yy = y1; yy < y2; yy++)
85 for (xx = x1; xx < x2; xx ++)
87 /* top 29 bits of xx (xx >> 3) indicate the byte the bit is inside,
88 * bottom 3 bits (xx & 7) indicate bit inside that byte,
89 * we don't bother to canonicalize data to 1 or 0, just
90 * leave the relevant bit in-place.
92 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
115 * convert bitmap data to pixbuf with alpha,
116 * without using a colormap
119 bitmap1a (GdkImage *image,
129 register guint8 data;
131 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
133 d (printf ("bitmap, with alpha\n"));
137 for (yy = y1; yy < y2; yy++)
141 for (xx = x1; xx < x2; xx ++)
143 /* see comment in bitmap1() */
144 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
169 * convert 1 bits-pixel data
173 rgb1 (GdkImage *image,
180 GdkColormap *colormap)
184 register guint8 data;
186 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
188 d (printf ("1 bits/pixel\n"));
190 /* convert upto 8 pixels/time */
191 /* its probably not worth trying to make this run very fast, who uses
192 * 1 bit displays anymore?
196 for (yy = y1; yy < y2; yy++)
200 for (xx = x1; xx < x2; xx ++)
202 /* see comment in bitmap1() */
203 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
207 *o++ = colormap->colors[data].red;
208 *o++ = colormap->colors[data].green;
209 *o++ = colormap->colors[data].blue;
217 * convert 1 bits/pixel data
221 rgb1a (GdkImage *image,
228 GdkColormap *colormap)
232 register guint8 data;
234 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
236 d (printf ("1 bits/pixel\n"));
238 /* convert upto 8 pixels/time */
239 /* its probably not worth trying to make this run very fast, who uses
240 * 1 bit displays anymore? */
243 for (yy = y1; yy < y2; yy++)
247 for (xx = x1; xx < x2; xx ++)
249 /* see comment in bitmap1() */
250 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
254 *o++ = colormap->colors[data].red;
255 *o++ = colormap->colors[data].green;
256 *o++ = colormap->colors[data].blue;
265 * convert 8 bits/pixel data
269 rgb8 (GdkImage *image,
276 GdkColormap *colormap)
281 register guint32 data;
282 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
288 d (printf ("8 bit, no alpha output\n"));
290 mask = mask_table[image->depth];
292 for (yy = y1; yy < y2; yy++)
296 for (xx = x1; xx < x2; xx++)
299 *o++ = colormap->colors[data].red;
300 *o++ = colormap->colors[data].green;
301 *o++ = colormap->colors[data].blue;
309 * convert 8 bits/pixel data
313 rgb8a (GdkImage *image,
320 GdkColormap *colormap)
325 register guint32 data;
327 register guint8 *s; /* read 2 pixels at once */
329 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
333 d (printf ("8 bit, with alpha output\n"));
335 mask = mask_table[image->depth];
337 for (xx = x1; xx < colormap->size; xx++)
340 remap[xx] = 0xff000000
341 | colormap->colors[xx].blue << 16
342 | colormap->colors[xx].green << 8
343 | colormap->colors[xx].red;
346 | colormap->colors[xx].red << 24
347 | colormap->colors[xx].green << 16
348 | colormap->colors[xx].blue << 8;
352 for (yy = y1; yy < y2; yy++)
355 o = (guint32 *) orow;
356 for (xx = x1; xx < x2; xx ++)
367 * convert 16 bits/pixel data
372 rgb565lsb (GdkImage *image,
379 GdkColormap *colormap)
385 register guint32 *s; /* read 2 pixels at once */
387 register guint8 *s; /* read 2 pixels at once */
390 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
394 for (yy = y1; yy < y2; yy++)
397 s = (guint32 *) srow;
401 o = (guint16 *) orow;
403 /* check for first pixel odd */
407 register guint16 data;
409 data = *((short *) s);
411 data = *((short *) s);
412 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
414 ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
415 ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
416 ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
421 g_assert (!(xx & 1));
423 /* if x2 is even, then the -1 does nothing to number of
424 * loop iterations, if x2 is odd then the -1 reduces
427 for (; xx < (x2 - 1); xx += 2)
429 register guint32 data;
432 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
433 | (data & 0x7e0) << 5 | (data & 0x600) >> 1;
434 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
435 | (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;
436 *o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25
437 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
439 /* swap endianness first */
440 data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
442 *o++ = (data & 0xf800) | (data & 0xe000) >> 5
443 | (data & 0x7e0) >> 3 | (data & 0x600) >> 9;
444 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
445 | (data & 0xf8000000) >> 24 | (data & 0xe0000000) >> 29;
446 *o++ = (data & 0x7e00000) >> 11 | (data & 0x6000000) >> 17
447 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
450 /* check for last remaining pixel */
453 register guint16 data;
455 data = *((short *) s);
457 data = *((short *) s);
458 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
460 ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
461 ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
462 ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
470 * convert 16 bits/pixel data
475 rgb565msb (GdkImage *image,
482 GdkColormap *colormap)
488 register guint8 *s; /* need to swap data order */
490 register guint32 *s; /* read 2 pixels at once */
493 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
497 for (yy = y1; yy < y2; yy++)
502 s = (guint32 *) srow;
504 o = (guint16 *) orow;
508 /* check for first pixel odd */
511 register guint16 data;
513 data = *((short *) s);
514 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
516 data = *((short *) s);
518 ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
519 ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
520 ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
526 g_assert (!(xx & 1));
528 /* if x2 is even, then the -1 does nothing to number of
529 * loop iterations, if x2 is odd then the -1 reduces
532 for (; xx < (x2 - 1); xx += 2)
534 register guint32 data;
536 /* swap endianness first */
537 data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
539 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
540 | (data & 0x7e0) << 5 | (data & 0x600) >> 1;
541 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
542 | (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;
543 *o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25
544 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
547 *o++ = (data & 0xf800) | (data & 0xe000) >> 5
548 | (data & 0x7e0) >> 3 | (data & 0x600) >> 9;
549 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
550 | (data & 0xf8000000) >> 24 | (data & 0xe0000000) >> 29;
551 *o++ = (data & 0x7e00000) >> 11 | (data & 0x6000000) >> 17
552 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
555 /* check for last remaining pixel */
558 register guint16 data;
560 data = *((short *) s);
561 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
563 data = *((short *) s);
565 ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
566 ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
567 ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
575 * convert 16 bits/pixel data
580 rgb565alsb (GdkImage *image,
587 GdkColormap *colormap)
593 register guint16 *s; /* read 1 pixels at once */
599 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
603 for (yy = y1; yy < y2; yy++)
606 s = (guint16 *) srow;
610 o = (guint32 *) orow;
611 for (xx = x1; xx < x2; xx ++)
613 register guint32 data;
614 /* rrrrrggg gggbbbbb -> rrrrrRRR ggggggGG bbbbbBBB aaaaaaaa */
615 /* little endian: aaaaaaaa bbbbbBBB ggggggGG rrrrrRRR */
618 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
619 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
620 | (data & 0x1f) << 19 | (data & 0x1c) << 14
623 /* swap endianness first */
624 data = s[0] | s[1] << 8;
626 *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
627 | (data & 0x7e0) << 13 | (data & 0x600) << 7
628 | (data & 0x1f) << 11 | (data & 0x1c) << 6
638 * convert 16 bits/pixel data
643 rgb565amsb (GdkImage *image,
650 GdkColormap *colormap)
658 register guint16 *s; /* read 1 pixels at once */
662 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
666 for (yy = y1; yy < y2; yy++)
669 o = (guint32 *) orow;
670 for (xx = x1; xx < x2; xx ++)
672 register guint32 data;
673 /* rrrrrggg gggbbbbb -> rrrrrRRR gggggg00 bbbbbBBB aaaaaaaa */
674 /* little endian: aaaaaaaa bbbbbBBB gggggg00 rrrrrRRR */
676 /* swap endianness first */
677 data = s[0] | s[1] << 8;
679 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
680 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
681 | (data & 0x1f) << 19 | (data & 0x1c) << 14
685 *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
686 | (data & 0x7e0) << 13 | (data & 0x600) << 7
687 | (data & 0x1f) << 11 | (data & 0x1c) << 6
697 * convert 15 bits/pixel data
702 rgb555lsb (GdkImage *image,
709 GdkColormap *colormap)
715 register guint32 *s; /* read 2 pixels at once */
717 register guint8 *s; /* read 2 pixels at once */
720 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
724 for (yy = y1; yy < y2; yy++)
727 s = (guint32 *) srow;
731 o = (guint16 *) orow;
735 /* check for first odd pixel */
738 register guint16 data;
740 data = *((short *) s);
742 data = *((short *) s);
743 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
745 ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
746 ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
747 ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
752 g_assert (!(xx & 1));
754 /* if x2 is even, then the -1 does nothing to number of
755 * loop iterations, if x2 is odd then the -1 reduces
758 for (; xx < (x2 - 1); xx += 2)
760 register guint32 data;
763 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
764 | (data & 0x3e0) << 6 | (data & 0x380) << 1;
765 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
766 | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
767 *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
768 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
770 /* swap endianness first */
771 data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
773 *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
774 | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
775 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
776 | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
777 *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
778 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
781 /* check for last remaining pixel */
784 register guint16 data;
786 data = *((short *) s);
788 data = *((short *) s);
789 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
791 ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
792 ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
793 ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
801 * convert 15 bits/pixel data
806 rgb555msb (GdkImage *image,
813 GdkColormap *colormap)
819 register guint8 *s; /* read 2 pixels at once */
821 register guint32 *s; /* read 2 pixels at once */
824 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
828 for (yy = y1; yy < y2; yy++)
831 o = (guint16 *) orow;
834 /* See if first pixel is odd */
837 register guint16 data;
839 data = *((short *) s);
840 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
842 data = *((short *) s);
844 ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
845 ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
846 ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
852 g_assert (!(xx & 1));
854 /* if x2 is even, then the -1 does nothing to number of
855 * loop iterations, if x2 is odd then the -1 reduces
858 for (; xx < (x2 - 1); xx += 2)
860 register guint32 data;
862 /* swap endianness first */
863 data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
865 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
866 | (data & 0x3e0) << 6 | (data & 0x380) << 1;
867 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
868 | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
869 *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
870 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
873 *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
874 | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
875 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
876 | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
877 *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
878 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
881 /* check for last remaining pixel */
884 register guint16 data;
886 data = *((short *) s);
887 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
889 data = *((short *) s);
891 ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
892 ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
893 ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
901 * convert 15 bits/pixel data
906 rgb555alsb (GdkImage *image,
913 GdkColormap *colormap)
919 register guint16 *s; /* read 1 pixels at once */
925 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
929 for (yy = y1; yy < y2; yy++)
932 s = (guint16 *) srow;
936 o = (guint32 *) orow;
937 for (xx = x1; xx < x2; xx++)
939 register guint32 data;
940 /* rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
941 /* little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
944 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
945 | (data & 0x3e0) << 6 | (data & 0x380) << 1
946 | (data & 0x1f) << 19 | (data & 0x1c) << 14
949 /* swap endianness first */
950 data = s[0] | s[1] << 8;
952 *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
953 | (data & 0x3e0) << 14 | (data & 0x380) << 9
954 | (data & 0x1f) << 11 | (data & 0x1c) << 6
964 * convert 15 bits/pixel data
969 rgb555amsb (GdkImage *image,
976 GdkColormap *colormap)
982 register guint16 *s; /* read 1 pixels at once */
988 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
992 for (yy = y1; yy < y2; yy++)
995 s = (guint16 *) srow;
999 o = (guint32 *) orow;
1000 for (xx = x1; xx < x2; xx++)
1002 register guint32 data;
1003 /* rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
1004 /* little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
1006 /* swap endianness first */
1007 data = s[0] | s[1] << 8;
1009 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
1010 | (data & 0x3e0) << 6 | (data & 0x380) << 1
1011 | (data & 0x1f) << 19 | (data & 0x1c) << 14
1015 *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
1016 | (data & 0x3e0) << 14 | (data & 0x380) << 9
1017 | (data & 0x1f) << 11 | (data & 0x1c) << 6
1028 rgb888alsb (GdkImage *image,
1035 GdkColormap *colormap)
1040 guint8 *s; /* for byte order swapping */
1042 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
1046 d (printf ("32 bits/pixel with alpha\n"));
1049 for (yy = y1; yy < y2; yy++)
1053 for (xx = x1; xx < x2; xx++)
1067 rgb888lsb (GdkImage *image,
1074 GdkColormap *colormap)
1079 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
1084 d (printf ("32 bit, lsb, no alpha\n"));
1086 for (yy = y1; yy < y2; yy++)
1090 for (xx = x1; xx < x2; xx++)
1103 rgb888amsb (GdkImage *image,
1110 GdkColormap *colormap)
1115 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
1120 guint8 *s; /* for byte order swapping */
1124 d (printf ("32 bit, msb, with alpha\n"));
1129 for (yy = y1; yy < y2; yy++)
1132 s = (guint32 *) srow;
1133 o = (guint32 *) orow;
1138 for (xx = x1; xx < x2; xx++)
1147 *o++ = (*s << 8) | 0xff; /* untested */
1157 rgb888msb (GdkImage *image,
1164 GdkColormap *colormap)
1169 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
1173 d (printf ("32 bit, msb, no alpha\n"));
1177 for (yy = y1; yy < y2; yy++)
1181 for (xx = x1; xx < x2; xx++)
1194 * This should work correctly with any display/any endianness, but will probably
1198 convert_real_slow (GdkImage *image,
1210 guint8 *orow = pixels;
1218 v = gdk_colormap_get_visual(cmap);
1220 d(printf("rgb mask/shift/prec = %x:%x:%x %d:%d:%d %d:%d:%d\n",
1221 v->red_mask, v->green_mask, v->blue_mask,
1222 v->red_shift, v->green_shift, v->blue_shift,
1223 v->red_prec, v->green_prec, v->blue_prec));
1225 for (yy = y1; yy < y2; yy++)
1228 for (xx = x1; xx < x2; xx++)
1230 pixel = gdk_image_get_pixel(image, xx, yy);
1233 /* I assume this is right for static & greyscale's too? */
1234 case GDK_VISUAL_STATIC_GRAY:
1235 case GDK_VISUAL_GRAYSCALE:
1236 case GDK_VISUAL_STATIC_COLOR:
1237 case GDK_VISUAL_PSEUDO_COLOR:
1238 *o++ = cmap->colors[pixel].red;
1239 *o++ = cmap->colors[pixel].green;
1240 *o++ = cmap->colors[pixel].blue;
1242 case GDK_VISUAL_TRUE_COLOR:
1243 /* This is odd because it must sometimes shift left (otherwise
1244 * I'd just shift >> (*_shift - 8 + *_prec + <0-7>). This logic
1245 * should work for all bit sizes/shifts/etc.
1248 for (i = 24; i < 32; i += v->red_prec)
1249 component |= ((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> i;
1252 for (i = 24; i < 32; i += v->green_prec)
1253 component |= ((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> i;
1256 for (i = 24; i < 32; i += v->blue_prec)
1257 component |= ((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> i;
1260 case GDK_VISUAL_DIRECT_COLOR:
1261 *o++ = cmap->colors[((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> 24].red;
1262 *o++ = cmap->colors[((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> 24].green;
1263 *o++ = cmap->colors[((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> 24].blue;
1273 typedef void (* cfunc) (GdkImage *image,
1282 static cfunc convert_map[] = {
1283 rgb1,rgb1,rgb1a,rgb1a,
1284 rgb8,rgb8,rgb8a,rgb8a,
1285 rgb555lsb,rgb555msb,rgb555alsb,rgb555amsb,
1286 rgb565lsb,rgb565msb,rgb565alsb,rgb565amsb,
1287 rgb888lsb,rgb888msb,rgb888alsb,rgb888amsb
1291 * perform actual conversion
1293 * If we can, try and use the optimised code versions, but as a default
1294 * fallback, and always for direct colour, use the generic/slow but complete
1295 * conversion function.
1298 rgbconvert (GdkImage *image,
1312 g_assert ((x + width) <= image->width);
1313 g_assert ((y + height) <= image->height);
1317 /* Only allowed for bitmaps */
1318 g_return_if_fail (image->depth == 1);
1321 bitmap1a (image, pixels, rowstride,
1322 x, y, x + width, y + height);
1324 bitmap1 (image, pixels, rowstride,
1325 x, y, x + width, y + height);
1330 v = gdk_colormap_get_visual (cmap);
1331 bank = 5; /* default fallback converter */
1332 index = (image->byte_order == GDK_MSB_FIRST) | (alpha != 0) << 1;
1334 d(printf("masks = %x:%x:%x\n", v->red_mask, v->green_mask, v->blue_mask));
1335 d(printf("image depth = %d, bits per pixel = %d\n", image->depth, image->bits_per_pixel));
1339 /* I assume this is right for static & greyscale's too? */
1340 case GDK_VISUAL_STATIC_GRAY:
1341 case GDK_VISUAL_GRAYSCALE:
1342 case GDK_VISUAL_STATIC_COLOR:
1343 case GDK_VISUAL_PSEUDO_COLOR:
1344 switch (image->bits_per_pixel)
1350 if (image->depth == 8)
1355 case GDK_VISUAL_TRUE_COLOR:
1356 switch (image->depth)
1359 if (v->red_mask == 0x7c00 && v->green_mask == 0x3e0 && v->blue_mask == 0x1f
1360 && image->bits_per_pixel == 16)
1364 if (v->red_mask == 0xf800 && v->green_mask == 0x7e0 && v->blue_mask == 0x1f
1365 && image->bits_per_pixel == 16)
1370 if (v->red_mask == 0xff0000 && v->green_mask == 0xff00 && v->blue_mask == 0xff
1371 && image->bits_per_pixel == 32)
1376 case GDK_VISUAL_DIRECT_COLOR:
1377 /* always use the slow version */
1381 d (g_print ("converting using conversion function in bank %d\n", bank));
1385 convert_real_slow (image, pixels, rowstride,
1386 x, y, x + width, y + height,
1392 d (g_print ("converting with index %d\n", index));
1393 (* convert_map[index]) (image, pixels, rowstride,
1394 x, y, x + width, y + height,
1400 /* Exported functions */
1403 * gdk_pixbuf_get_from_drawable:
1404 * @dest: Destination pixbuf, or %NULL if a new pixbuf should be created.
1405 * @src: Source drawable.
1406 * @cmap: A colormap if @src doesn't have one set.
1407 * @src_x: Source X coordinate within drawable.
1408 * @src_y: Source Y coordinate within drawable.
1409 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1410 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1411 * @width: Width in pixels of region to get.
1412 * @height: Height in pixels of region to get.
1414 * Transfers image data from a #GdkDrawable and converts it to an RGB(A)
1415 * representation inside a #GdkPixbuf. In other words, copies
1416 * image data from a server-side drawable to a client-side RGB(A) buffer.
1417 * This allows you to efficiently read individual pixels on the client side.
1419 * If the drawable @src has no colormap (gdk_drawable_get_colormap()
1420 * returns %NULL), then a suitable colormap must be specified.
1421 * Typically a #GdkWindow or a pixmap created by passing a #GdkWindow
1422 * to gdk_pixmap_new() will already have a colormap associated with
1423 * it. If the drawable has a colormap, the @cmap argument will be
1424 * ignored. If the drawable is a bitmap (1 bit per pixel pixmap),
1425 * then a colormap is not required; pixels with a value of 1 are
1426 * assumed to be white, and pixels with a value of 0 are assumed to be
1427 * black. For taking screenshots, gdk_colormap_get_system() returns
1428 * the correct colormap to use.
1430 * If the specified destination pixbuf @dest is %NULL, then this
1431 * function will create an RGB pixbuf with 8 bits per channel and no
1432 * alpha, with the same size specified by the @width and @height
1433 * arguments. In this case, the @dest_x and @dest_y arguments must be
1434 * specified as 0. If the specified destination pixbuf is not %NULL
1435 * and it contains alpha information, then the filled pixels will be
1436 * set to full opacity (alpha = 255).
1438 * If the specified drawable is a pixmap, then the requested source
1439 * rectangle must be completely contained within the pixmap, otherwise
1440 * the function will return %NULL. For pixmaps only (not for windows)
1441 * passing -1 for width or height is allowed to mean the full width
1442 * or height of the pixmap.
1444 * If the specified drawable is a window, and the window is off the
1445 * screen, then there is no image data in the obscured/offscreen
1446 * regions to be placed in the pixbuf. The contents of portions of the
1447 * pixbuf corresponding to the offscreen region are undefined.
1449 * If the window you're obtaining data from is partially obscured by
1450 * other windows, then the contents of the pixbuf areas corresponding
1451 * to the obscured regions are undefined.
1453 * If the target drawable is not mapped (typically because it's
1454 * iconified/minimized or not on the current workspace), then %NULL
1457 * If memory can't be allocated for the return value, %NULL will be returned
1460 * (In short, there are several ways this function can fail, and if it fails
1461 * it returns %NULL; so check the return value.)
1463 * This function calls gdk_drawable_get_image() internally and
1464 * converts the resulting image to a #GdkPixbuf, so the
1465 * documentation for gdk_drawable_get_image() may also be relevant.
1467 * Return value: The same pixbuf as @dest if it was non-%NULL, or a newly-created
1468 * pixbuf with a reference count of 1 if no destination pixbuf was specified, or %NULL on error
1471 gdk_pixbuf_get_from_drawable (GdkPixbuf *dest,
1474 int src_x, int src_y,
1475 int dest_x, int dest_y,
1476 int width, int height)
1478 int src_width, src_height;
1483 /* General sanity checks */
1485 g_return_val_if_fail (src != NULL, NULL);
1487 if (GDK_IS_WINDOW (src))
1488 /* FIXME: this is not perfect, since is_viewable() only tests
1489 * recursively up the Gdk parent window tree, but stops at
1490 * foreign windows or Gdk toplevels. I.e. if a window manager
1491 * unmapped one of its own windows, this won't work.
1493 g_return_val_if_fail (gdk_window_is_viewable (src), NULL);
1496 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1499 g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1500 g_return_val_if_fail (dest->n_channels == 3 || dest->n_channels == 4, NULL);
1501 g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1504 /* Create the pixbuf if needed */
1507 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
1513 cmap = gdk_drawable_get_colormap (src);
1515 depth = gdk_drawable_get_depth (src);
1517 if (depth != 1 && cmap == NULL)
1519 g_warning ("%s: Source drawable has no colormap; either pass "
1520 "in a colormap, or set the colormap on the drawable "
1521 "with gdk_drawable_set_colormap()", G_STRLOC);
1525 /* Coordinate sanity checks */
1527 if (GDK_IS_PIXMAP (src))
1529 gdk_drawable_get_size (src, &src_width, &src_height);
1533 height = src_height;
1535 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1536 g_return_val_if_fail (src_x + width <= src_width && src_y + height <= src_height, NULL);
1541 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1542 g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1543 g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1546 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1548 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1549 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1553 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1555 image = _gdk_image_get_scratch (width1, height1, depth, &xs0, &ys0);
1557 _gdk_drawable_copy_to_image (src, image,
1558 src_x + x0, src_y + y0,
1559 xs0, ys0, width1, height1);
1561 gdk_pixbuf_get_from_image (dest, image, cmap,
1562 xs0, ys0, dest_x + x0, dest_y + y0,
1571 * gdk_pixbuf_get_from_image:
1572 * @dest: Destination pixbuf, or %NULL if a new pixbuf should be created.
1573 * @src: Source #GdkImage.
1574 * @cmap: A colormap, or %NULL to use the one for @src
1575 * @src_x: Source X coordinate within drawable.
1576 * @src_y: Source Y coordinate within drawable.
1577 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1578 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1579 * @width: Width in pixels of region to get.
1580 * @height: Height in pixels of region to get.
1582 * Same as gdk_pixbuf_get_from_drawable() but gets the pixbuf from
1585 * Return value: @dest, newly-created pixbuf if @dest was %NULL, %NULL on error
1588 gdk_pixbuf_get_from_image (GdkPixbuf *dest,
1598 int rowstride, bpp, alpha;
1600 /* General sanity checks */
1602 g_return_val_if_fail (GDK_IS_IMAGE (src), NULL);
1605 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1608 g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1609 g_return_val_if_fail (dest->n_channels == 3 || dest->n_channels == 4, NULL);
1610 g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1614 cmap = gdk_image_get_colormap (src);
1616 if (src->depth != 1 && cmap == NULL)
1618 g_warning ("%s: Source image has no colormap; either pass "
1619 "in a colormap, or set the colormap on the image "
1620 "with gdk_image_set_colormap()", G_STRLOC);
1624 /* Coordinate sanity checks */
1626 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1627 g_return_val_if_fail (src_x + width <= src->width && src_y + height <= src->height, NULL);
1631 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1632 g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1633 g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1636 /* Create the pixbuf if needed */
1639 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
1644 alpha = dest->has_alpha;
1645 rowstride = dest->rowstride;
1646 bpp = alpha ? 4 : 3;
1648 /* we offset into the image data based on the position we are
1651 rgbconvert (src, dest->pixels +
1652 (dest_y * rowstride) + (dest_x * bpp),