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.
30 #include "gdkvisual.h"
31 #include "gdkwindow.h"
32 #include "gdkpixbuf.h"
33 #include "gdkpixmap.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);
417 o = (guint16 *) (((char *) o) + 3);
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] << 24 | s[0] << 16 | s[3] << 8 | s[2];
443 *o++ = (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21
444 | (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25;
445 *o++ = (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10
446 | (data & 0xf800) >> 8 | (data & 0xe000) >> 13;
447 *o++ = (data & 0x7e0) << 5 | (data & 0x600) >> 1
448 | (data & 0x1f) << 3 | (data & 0x1c) >> 2;
451 /* check for last remaining pixel */
454 register guint16 data;
456 data = *((short *) s);
458 data = *((short *) s);
459 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
461 ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
462 ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
463 ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
471 * convert 16 bits/pixel data
476 rgb565msb (GdkImage *image,
483 GdkColormap *colormap)
489 register guint8 *s; /* need to swap data order */
491 register guint32 *s; /* read 2 pixels at once */
494 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
498 for (yy = y1; yy < y2; yy++)
503 s = (guint32 *) srow;
505 o = (guint16 *) orow;
509 /* check for first pixel odd */
512 register guint16 data;
514 data = *((short *) s);
515 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
517 data = *((short *) s);
519 ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
520 ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
521 ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
523 o = (guint16 *) (((char *) o) + 3);
527 g_assert (!(xx & 1));
529 /* if x2 is even, then the -1 does nothing to number of
530 * loop iterations, if x2 is odd then the -1 reduces
533 for (; xx < (x2 - 1); xx += 2)
535 register guint32 data;
537 /* swap endianness first */
538 data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
540 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
541 | (data & 0x7e0) << 5 | (data & 0x600) >> 1;
542 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
543 | (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;
544 *o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25
545 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
548 *o++ = (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21
549 | (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25;
550 *o++ = (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10
551 | (data & 0xf800) >> 8 | (data & 0xe000) >> 13;
552 *o++ = (data & 0x7e0) << 5 | (data & 0x600) >> 1
553 | (data & 0x1f) << 3 | (data & 0x1c) >> 2;
556 /* check for last remaining pixel */
559 register guint16 data;
561 data = *((short *) s);
562 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
564 data = *((short *) s);
566 ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
567 ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
568 ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
576 * convert 16 bits/pixel data
581 rgb565alsb (GdkImage *image,
588 GdkColormap *colormap)
594 register guint16 *s; /* read 1 pixels at once */
600 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
604 for (yy = y1; yy < y2; yy++)
607 s = (guint16 *) srow;
611 o = (guint32 *) orow;
612 for (xx = x1; xx < x2; xx ++)
614 register guint32 data;
615 /* rrrrrggg gggbbbbb -> rrrrrRRR ggggggGG bbbbbBBB aaaaaaaa */
616 /* little endian: aaaaaaaa bbbbbBBB ggggggGG rrrrrRRR */
619 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
620 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
621 | (data & 0x1f) << 19 | (data & 0x1c) << 14
624 /* swap endianness first */
625 data = s[0] | s[1] << 8;
627 *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
628 | (data & 0x7e0) << 13 | (data & 0x600) << 7
629 | (data & 0x1f) << 11 | (data & 0x1c) << 6
639 * convert 16 bits/pixel data
644 rgb565amsb (GdkImage *image,
651 GdkColormap *colormap)
659 register guint16 *s; /* read 1 pixels at once */
663 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
667 for (yy = y1; yy < y2; yy++)
670 o = (guint32 *) orow;
671 for (xx = x1; xx < x2; xx ++)
673 register guint32 data;
674 /* rrrrrggg gggbbbbb -> rrrrrRRR gggggg00 bbbbbBBB aaaaaaaa */
675 /* little endian: aaaaaaaa bbbbbBBB gggggg00 rrrrrRRR */
677 /* swap endianness first */
678 data = s[0] | s[1] << 8;
680 *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
681 | (data & 0x7e0) << 5 | (data & 0x600) >> 1
682 | (data & 0x1f) << 19 | (data & 0x1c) << 14
686 *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
687 | (data & 0x7e0) << 13 | (data & 0x600) << 7
688 | (data & 0x1f) << 11 | (data & 0x1c) << 6
698 * convert 15 bits/pixel data
703 rgb555lsb (GdkImage *image,
710 GdkColormap *colormap)
716 register guint32 *s; /* read 2 pixels at once */
718 register guint8 *s; /* read 2 pixels at once */
721 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
725 for (yy = y1; yy < y2; yy++)
728 s = (guint32 *) srow;
732 o = (guint16 *) orow;
736 /* check for first odd pixel */
739 register guint16 data;
741 data = *((short *) s);
743 data = *((short *) s);
744 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
746 ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
747 ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
748 ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
749 o = (guint16 *) (((char *) o) + 3);
753 g_assert (!(xx & 1));
755 /* if x2 is even, then the -1 does nothing to number of
756 * loop iterations, if x2 is odd then the -1 reduces
759 for (; xx < (x2 - 1); xx += 2)
761 register guint32 data;
764 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
765 | (data & 0x3e0) << 6 | (data & 0x380) << 1;
766 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
767 | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
768 *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
769 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
771 /* swap endianness first */
772 data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
774 *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
775 | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
776 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
777 | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
778 *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
779 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
782 /* check for last remaining pixel */
785 register guint16 data;
787 data = *((short *) s);
789 data = *((short *) s);
790 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
792 ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
793 ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
794 ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
802 * convert 15 bits/pixel data
807 rgb555msb (GdkImage *image,
814 GdkColormap *colormap)
820 register guint8 *s; /* read 2 pixels at once */
822 register guint32 *s; /* read 2 pixels at once */
825 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
829 for (yy = y1; yy < y2; yy++)
832 o = (guint16 *) orow;
835 /* See if first pixel is odd */
838 register guint16 data;
840 data = *((short *) s);
841 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
843 data = *((short *) s);
845 ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
846 ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
847 ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
849 o = (guint16 *) (((char *) o) + 3);
853 g_assert (!(xx & 1));
855 /* if x2 is even, then the -1 does nothing to number of
856 * loop iterations, if x2 is odd then the -1 reduces
859 for (; xx < (x2 - 1); xx += 2)
861 register guint32 data;
863 /* swap endianness first */
864 data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;
866 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
867 | (data & 0x3e0) << 6 | (data & 0x380) << 1;
868 *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
869 | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
870 *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
871 | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
874 *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
875 | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
876 *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
877 | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
878 *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
879 | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
882 /* check for last remaining pixel */
885 register guint16 data;
887 data = *((short *) s);
888 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
890 data = *((short *) s);
892 ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
893 ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
894 ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
902 * convert 15 bits/pixel data
907 rgb555alsb (GdkImage *image,
914 GdkColormap *colormap)
920 register guint16 *s; /* read 1 pixels at once */
926 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
930 for (yy = y1; yy < y2; yy++)
933 s = (guint16 *) srow;
937 o = (guint32 *) orow;
938 for (xx = x1; xx < x2; xx++)
940 register guint32 data;
941 /* rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
942 /* little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
945 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
946 | (data & 0x3e0) << 6 | (data & 0x380) << 1
947 | (data & 0x1f) << 19 | (data & 0x1c) << 14
950 /* swap endianness first */
951 data = s[0] | s[1] << 8;
953 *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
954 | (data & 0x3e0) << 14 | (data & 0x380) << 9
955 | (data & 0x1f) << 11 | (data & 0x1c) << 6
965 * convert 15 bits/pixel data
970 rgb555amsb (GdkImage *image,
977 GdkColormap *colormap)
983 register guint16 *s; /* read 1 pixels at once */
989 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
993 for (yy = y1; yy < y2; yy++)
996 s = (guint16 *) srow;
1000 o = (guint32 *) orow;
1001 for (xx = x1; xx < x2; xx++)
1003 register guint32 data;
1004 /* rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
1005 /* little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
1007 /* swap endianness first */
1008 data = s[0] | s[1] << 8;
1010 *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
1011 | (data & 0x3e0) << 6 | (data & 0x380) << 1
1012 | (data & 0x1f) << 19 | (data & 0x1c) << 14
1016 *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
1017 | (data & 0x3e0) << 14 | (data & 0x380) << 9
1018 | (data & 0x1f) << 11 | (data & 0x1c) << 6
1029 rgb888alsb (GdkImage *image,
1036 GdkColormap *colormap)
1041 guint8 *s; /* for byte order swapping */
1043 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
1047 d (printf ("32 bits/pixel with alpha\n"));
1050 for (yy = y1; yy < y2; yy++)
1054 for (xx = x1; xx < x2; xx++)
1068 rgb888lsb (GdkImage *image,
1075 GdkColormap *colormap)
1080 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
1085 d (printf ("32 bit, lsb, no alpha\n"));
1087 for (yy = y1; yy < y2; yy++)
1091 for (xx = x1; xx < x2; xx++)
1104 rgb888amsb (GdkImage *image,
1111 GdkColormap *colormap)
1116 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
1121 guint8 *s; /* for byte order swapping */
1125 d (printf ("32 bit, msb, with alpha\n"));
1130 for (yy = y1; yy < y2; yy++)
1133 s = (guint32 *) srow;
1134 o = (guint32 *) orow;
1139 for (xx = x1; xx < x2; xx++)
1148 *o++ = (*s << 8) | 0xff; /* untested */
1158 rgb888msb (GdkImage *image,
1165 GdkColormap *colormap)
1170 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
1174 d (printf ("32 bit, msb, no alpha\n"));
1178 for (yy = y1; yy < y2; yy++)
1182 for (xx = x1; xx < x2; xx++)
1195 * This should work correctly with any display/any endianness, but will probably
1199 convert_real_slow (GdkImage *image,
1211 guint8 *orow = pixels;
1219 v = gdk_colormap_get_visual(cmap);
1221 d(printf("rgb mask/shift/prec = %x:%x:%x %d:%d:%d %d:%d:%d\n",
1222 v->red_mask, v->green_mask, v->blue_mask,
1223 v->red_shift, v->green_shift, v->blue_shift,
1224 v->red_prec, v->green_prec, v->blue_prec));
1226 for (yy = y1; yy < y2; yy++)
1229 for (xx = x1; xx < x2; xx++)
1231 pixel = gdk_image_get_pixel(image, xx, yy);
1234 /* I assume this is right for static & greyscale's too? */
1235 case GDK_VISUAL_STATIC_GRAY:
1236 case GDK_VISUAL_GRAYSCALE:
1237 case GDK_VISUAL_STATIC_COLOR:
1238 case GDK_VISUAL_PSEUDO_COLOR:
1239 *o++ = cmap->colors[pixel].red;
1240 *o++ = cmap->colors[pixel].green;
1241 *o++ = cmap->colors[pixel].blue;
1243 case GDK_VISUAL_TRUE_COLOR:
1244 /* This is odd because it must sometimes shift left (otherwise
1245 * I'd just shift >> (*_shift - 8 + *_prec + <0-7>). This logic
1246 * should work for all bit sizes/shifts/etc.
1249 for (i = 24; i < 32; i += v->red_prec)
1250 component |= ((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> i;
1253 for (i = 24; i < 32; i += v->green_prec)
1254 component |= ((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> i;
1257 for (i = 24; i < 32; i += v->blue_prec)
1258 component |= ((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> i;
1261 case GDK_VISUAL_DIRECT_COLOR:
1262 *o++ = cmap->colors[((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> 24].red;
1263 *o++ = cmap->colors[((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> 24].green;
1264 *o++ = cmap->colors[((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> 24].blue;
1274 typedef void (* cfunc) (GdkImage *image,
1283 static cfunc convert_map[] = {
1284 rgb1,rgb1,rgb1a,rgb1a,
1285 rgb8,rgb8,rgb8a,rgb8a,
1286 rgb555lsb,rgb555msb,rgb555alsb,rgb555amsb,
1287 rgb565lsb,rgb565msb,rgb565alsb,rgb565amsb,
1288 rgb888lsb,rgb888msb,rgb888alsb,rgb888amsb
1292 * perform actual conversion
1294 * If we can, try and use the optimised code versions, but as a default
1295 * fallback, and always for direct colour, use the generic/slow but complete
1296 * conversion function.
1299 rgbconvert (GdkImage *image,
1313 g_assert ((x + width) <= image->width);
1314 g_assert ((y + height) <= image->height);
1318 /* Only allowed for bitmaps */
1319 g_return_if_fail (image->depth == 1);
1322 bitmap1a (image, pixels, rowstride,
1323 x, y, x + width, y + height);
1325 bitmap1 (image, pixels, rowstride,
1326 x, y, x + width, y + height);
1331 v = gdk_colormap_get_visual (cmap);
1332 bank = 5; /* default fallback converter */
1333 index = (image->byte_order == GDK_MSB_FIRST) | (alpha != 0) << 1;
1335 d(printf("masks = %x:%x:%x\n", v->red_mask, v->green_mask, v->blue_mask));
1336 d(printf("image depth = %d, bits per pixel = %d\n", image->depth, image->bits_per_pixel));
1340 /* I assume this is right for static & greyscale's too? */
1341 case GDK_VISUAL_STATIC_GRAY:
1342 case GDK_VISUAL_GRAYSCALE:
1343 case GDK_VISUAL_STATIC_COLOR:
1344 case GDK_VISUAL_PSEUDO_COLOR:
1345 switch (image->bits_per_pixel)
1351 if (image->depth == 8)
1356 case GDK_VISUAL_TRUE_COLOR:
1357 switch (image->depth)
1360 if (v->red_mask == 0x7c00 && v->green_mask == 0x3e0 && v->blue_mask == 0x1f
1361 && image->bits_per_pixel == 16)
1365 if (v->red_mask == 0xf800 && v->green_mask == 0x7e0 && v->blue_mask == 0x1f
1366 && image->bits_per_pixel == 16)
1371 if (v->red_mask == 0xff0000 && v->green_mask == 0xff00 && v->blue_mask == 0xff
1372 && image->bits_per_pixel == 32)
1377 case GDK_VISUAL_DIRECT_COLOR:
1378 /* always use the slow version */
1382 d (g_print ("converting using conversion function in bank %d\n", bank));
1386 convert_real_slow (image, pixels, rowstride,
1387 x, y, x + width, y + height,
1393 d (g_print ("converting with index %d\n", index));
1394 (* convert_map[index]) (image, pixels, rowstride,
1395 x, y, x + width, y + height,
1401 /* Exported functions */
1404 * gdk_pixbuf_get_from_drawable:
1405 * @dest: Destination pixbuf, or %NULL if a new pixbuf should be created.
1406 * @src: Source drawable.
1407 * @cmap: A colormap if @src doesn't have one set.
1408 * @src_x: Source X coordinate within drawable.
1409 * @src_y: Source Y coordinate within drawable.
1410 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1411 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1412 * @width: Width in pixels of region to get.
1413 * @height: Height in pixels of region to get.
1415 * Transfers image data from a #GdkDrawable and converts it to an RGB(A)
1416 * representation inside a #GdkPixbuf. In other words, copies
1417 * image data from a server-side drawable to a client-side RGB(A) buffer.
1418 * This allows you to efficiently read individual pixels on the client side.
1420 * If the drawable @src has no colormap (gdk_drawable_get_colormap()
1421 * returns %NULL), then a suitable colormap must be specified.
1422 * Typically a #GdkWindow or a pixmap created by passing a #GdkWindow
1423 * to gdk_pixmap_new() will already have a colormap associated with
1424 * it. If the drawable has a colormap, the @cmap argument will be
1425 * ignored. If the drawable is a bitmap (1 bit per pixel pixmap),
1426 * then a colormap is not required; pixels with a value of 1 are
1427 * assumed to be white, and pixels with a value of 0 are assumed to be
1428 * black. For taking screenshots, gdk_colormap_get_system() returns
1429 * the correct colormap to use.
1431 * If the specified destination pixbuf @dest is %NULL, then this
1432 * function will create an RGB pixbuf with 8 bits per channel and no
1433 * alpha, with the same size specified by the @width and @height
1434 * arguments. In this case, the @dest_x and @dest_y arguments must be
1435 * specified as 0. If the specified destination pixbuf is not %NULL
1436 * and it contains alpha information, then the filled pixels will be
1437 * set to full opacity (alpha = 255).
1439 * If the specified drawable is a pixmap, then the requested source
1440 * rectangle must be completely contained within the pixmap, otherwise
1441 * the function will return %NULL. For pixmaps only (not for windows)
1442 * passing -1 for width or height is allowed to mean the full width
1443 * or height of the pixmap.
1445 * If the specified drawable is a window, and the window is off the
1446 * screen, then there is no image data in the obscured/offscreen
1447 * regions to be placed in the pixbuf. The contents of portions of the
1448 * pixbuf corresponding to the offscreen region are undefined.
1450 * If the window you're obtaining data from is partially obscured by
1451 * other windows, then the contents of the pixbuf areas corresponding
1452 * to the obscured regions are undefined.
1454 * If the target drawable is not mapped (typically because it's
1455 * iconified/minimized or not on the current workspace), then %NULL
1458 * If memory can't be allocated for the return value, %NULL will be returned
1461 * (In short, there are several ways this function can fail, and if it fails
1462 * it returns %NULL; so check the return value.)
1464 * This function calls gdk_drawable_get_image() internally and
1465 * converts the resulting image to a #GdkPixbuf, so the
1466 * documentation for gdk_drawable_get_image() may also be relevant.
1468 * Return value: The same pixbuf as @dest if it was non-%NULL, or a newly-created
1469 * pixbuf with a reference count of 1 if no destination pixbuf was specified, or %NULL on error
1472 gdk_pixbuf_get_from_drawable (GdkPixbuf *dest,
1475 int src_x, int src_y,
1476 int dest_x, int dest_y,
1477 int width, int height)
1479 int src_width, src_height;
1484 /* General sanity checks */
1486 g_return_val_if_fail (src != NULL, NULL);
1488 if (GDK_IS_WINDOW (src))
1489 /* FIXME: this is not perfect, since is_viewable() only tests
1490 * recursively up the Gdk parent window tree, but stops at
1491 * foreign windows or Gdk toplevels. I.e. if a window manager
1492 * unmapped one of its own windows, this won't work.
1494 g_return_val_if_fail (gdk_window_is_viewable (src), NULL);
1497 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1500 g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1501 g_return_val_if_fail (dest->n_channels == 3 || dest->n_channels == 4, NULL);
1502 g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1506 cmap = gdk_drawable_get_colormap (src);
1508 depth = gdk_drawable_get_depth (src);
1510 if (depth != 1 && cmap == NULL)
1512 g_warning ("%s: Source drawable has no colormap; either pass "
1513 "in a colormap, or set the colormap on the drawable "
1514 "with gdk_drawable_set_colormap()", G_STRLOC);
1518 /* Coordinate sanity checks */
1520 if (GDK_IS_PIXMAP (src))
1522 gdk_drawable_get_size (src, &src_width, &src_height);
1526 height = src_height;
1528 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1529 g_return_val_if_fail (src_x + width <= src_width && src_y + height <= src_height, NULL);
1532 /* Create the pixbuf if needed */
1535 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
1542 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1543 g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1544 g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1547 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1549 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1550 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1554 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1556 image = _gdk_image_get_scratch (gdk_drawable_get_screen (src),
1557 width1, height1, depth, &xs0, &ys0);
1559 _gdk_drawable_copy_to_image (src, image,
1560 src_x + x0, src_y + y0,
1561 xs0, ys0, width1, height1);
1563 gdk_pixbuf_get_from_image (dest, image, cmap,
1564 xs0, ys0, dest_x + x0, dest_y + y0,
1573 * gdk_pixbuf_get_from_image:
1574 * @dest: Destination pixbuf, or %NULL if a new pixbuf should be created.
1575 * @src: Source #GdkImage.
1576 * @cmap: A colormap, or %NULL to use the one for @src
1577 * @src_x: Source X coordinate within drawable.
1578 * @src_y: Source Y coordinate within drawable.
1579 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1580 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1581 * @width: Width in pixels of region to get.
1582 * @height: Height in pixels of region to get.
1584 * Same as gdk_pixbuf_get_from_drawable() but gets the pixbuf from
1587 * Return value: @dest, newly-created pixbuf if @dest was %NULL, %NULL on error
1590 gdk_pixbuf_get_from_image (GdkPixbuf *dest,
1600 int rowstride, bpp, alpha;
1602 /* General sanity checks */
1604 g_return_val_if_fail (GDK_IS_IMAGE (src), NULL);
1607 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1610 g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1611 g_return_val_if_fail (dest->n_channels == 3 || dest->n_channels == 4, NULL);
1612 g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1616 cmap = gdk_image_get_colormap (src);
1618 if (src->depth != 1 && cmap == NULL)
1620 g_warning ("%s: Source image has no colormap; either pass "
1621 "in a colormap, or set the colormap on the image "
1622 "with gdk_image_set_colormap()", G_STRLOC);
1626 /* Coordinate sanity checks */
1628 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1629 g_return_val_if_fail (src_x + width <= src->width && src_y + height <= src->height, NULL);
1633 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1634 g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1635 g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1638 /* Create the pixbuf if needed */
1641 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
1646 alpha = dest->has_alpha;
1647 rowstride = dest->rowstride;
1648 bpp = alpha ? 4 : 3;
1650 /* we offset into the image data based on the position we are
1653 rgbconvert (src, dest->pixels +
1654 (dest_y * rowstride) + (dest_x * bpp),