]> Pileus Git - ~andy/gtk/blob - gdk/gdkpixbuf-drawable.c
Added #include "gdk-pixbuf-drawable.h" to the top. This fixes remaining
[~andy/gtk] / gdk / gdkpixbuf-drawable.c
1 /*
2  * creates an ArtPixBuf from a Drawable
3  *
4  * Author:
5  *   Cody Russell <bratsche@dfw.net>
6  */
7 #include <config.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <glib.h>
11 #include <gmodule.h>
12 #include "gdk-pixbuf.h"
13 #include "gdk-pixbuf-drawable.h"
14
15 /* private function */
16
17 static ArtPixBuf *
18 art_pixbuf_from_drawable_core (GdkWindow *window,
19                                gint x, gint y,
20                                gint width, gint height,
21                                gint with_alpha)
22 {
23         GdkImage *image;
24         ArtPixBuf *pixbuf;
25         GdkColormap *colormap;
26         art_u8 *buff;
27         art_u8 *pixels;
28         gulong pixel;
29         gint rowstride;
30         art_u8 r, g, b;
31         gint xx, yy;
32         gint fatness;
33
34         g_return_val_if_fail (window != NULL, NULL);
35
36         image = gdk_image_get (window, x, y, width, height);
37         colormap = gdk_rgb_get_cmap ();
38
39         fatness = with_alpha ? 4 : 3;
40         rowstride = width * fatness;
41
42         buff = art_alloc (rowstride * height);
43         pixels = buff;
44
45         switch (image->depth)
46         {
47         case 0:
48         case 1:
49         case 2:
50         case 3:
51         case 4:
52         case 5:
53         case 6:
54         case 7:
55         case 8:
56                 for (yy = 0; yy < height; yy++)
57                 {
58                         for (xx = 0; xx < width; xx++)
59                         {
60                                 pixel = gdk_image_get_pixel (image, xx, yy);
61                                 pixels[0] = colormap->colors[pixel].red;
62                                 pixels[1] = colormap->colors[pixel].green;
63                                 pixels[2] = colormap->colors[pixel].blue;
64                                 if (with_alpha)
65                                         pixels[3] = 0;
66                                 pixels += fatness;
67            
68                         }
69                 }
70                 break;
71
72         case 16:
73         case 15:
74                 for (yy = 0; yy < height; yy++)
75                 {
76                         for (xx = 0; xx < width; xx++)
77                         {
78                                 pixel = gdk_image_get_pixel (image, xx, yy);
79                                 r =  (pixel >> 8) & 0xf8;
80                                 g =  (pixel >> 3) & 0xfc;
81                                 b =  (pixel << 3) & 0xf8;
82                                 pixels[0] = r;
83                                 pixels[1] = g;
84                                 pixels[2] = b;
85                                 if (with_alpha)
86                                         pixels[3] = 0; /* GdkImages don't have alpha =) */
87                                 pixels += fatness;
88                         }
89                 }
90                 break;
91
92         case 24:
93         case 32:
94                 for (yy = 0; yy < height; yy++)
95                 {
96                         for (xx = 0; xx < width; xx++)
97                         {
98                                 pixel = gdk_image_get_pixel (image, xx, yy);
99                                 r =  (pixel >> 16) & 0xff;
100                                 g =  (pixel >> 8)  & 0xff;
101                                 b = pixel & 0xff;
102                                 pixels[0] = r;
103                                 pixels[1] = g;
104                                 pixels[2] = b;
105                                 if (with_alpha)
106                                         pixels[3] = 0;  /* GdkImages don't have alpha.. */
107                                 pixels += fatness;
108                         }
109                 }
110                 break;
111
112         default:
113                 g_error ("art_pixbuf_from_drawable_core (): Unknown depth.");
114         }
115
116         return with_alpha ? art_pixbuf_new_rgba (buff, width, height, rowstride) :
117                 art_pixbuf_new_rgb (buff, width, height, rowstride);
118 }
119
120 /* Public functions */
121
122 ArtPixBuf *
123 art_pixbuf_rgb_from_drawable  (GdkWindow *window,
124                               gint x, gint y,
125                               gint width, gint height)
126 {
127         return art_pixbuf_from_drawable_core  (window, x, y, width, height, 0);
128 }
129           
130 ArtPixBuf *
131 art_pixbuf_rgba_from_drawable  (GdkWindow *window,
132                                gint x, gint y,
133                                gint width, gint height)
134 {
135         return art_pixbuf_from_drawable_core  (window, x, y, width, height, 1);
136 }