d1e920794a0acdbdfeeee93153378274fadbdaf4
[xscreensaver] / hacks / glx / grab-ximage.c
1 /* grab-ximage.c --- grab the screen to an XImage for use with OpenGL.
2  * xscreensaver, Copyright (c) 2001, 2003 Jamie Zawinski <jwz@jwz.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <X11/Xlib.h>
20 #include <X11/Xutil.h>
21 #include <GL/gl.h>      /* only for GLfloat */
22
23 #include "grabscreen.h"
24 #include "visual.h"
25
26 extern char *progname;
27
28 #include <X11/Xutil.h>
29
30 #undef MAX
31 #define MAX(a,b) ((a)>(b)?(a):(b))
32
33 #undef countof
34 #define countof(x) (sizeof((x))/sizeof((*x)))
35
36 /* return the next larger power of 2. */
37 static int
38 to_pow2 (int i)
39 {
40   static unsigned int pow2[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
41                                  2048, 4096, 8192, 16384, 32768, 65536 };
42   int j;
43   for (j = 0; j < countof(pow2); j++)
44     if (pow2[j] >= i) return pow2[j];
45   abort();  /* too big! */
46 }
47
48
49 /* Given a bitmask, returns the position and width of the field.
50  */
51 static void
52 decode_mask (unsigned int mask, unsigned int *pos_ret, unsigned int *size_ret)
53 {
54   int i;
55   for (i = 0; i < 32; i++)
56     if (mask & (1L << i))
57       {
58         int j = 0;
59         *pos_ret = i;
60         for (; i < 32; i++, j++)
61           if (! (mask & (1L << i)))
62             break;
63         *size_ret = j;
64         return;
65       }
66 }
67
68
69 /* Given a value and a field-width, expands the field to fill out 8 bits.
70  */
71 static unsigned char
72 spread_bits (unsigned char value, unsigned char width)
73 {
74   switch (width)
75     {
76     case 8: return value;
77     case 7: return (value << 1) | (value >> 6);
78     case 6: return (value << 2) | (value >> 4);
79     case 5: return (value << 3) | (value >> 2);
80     case 4: return (value << 4) | (value);
81     case 3: return (value << 5) | (value << 2) | (value >> 2);
82     case 2: return (value << 6) | (value << 4) | (value);
83     default: abort(); break;
84     }
85 }
86
87
88 /* Returns an XImage structure containing an image of the desktop.
89    (As a side-effect, that image *may* be painted onto the given Window.)
90    This XImage will be 32 bits per pixel, 8 each per R, G, and B, with the
91    extra byte set to 0xFF.
92  */
93 XImage *
94 screen_to_ximage (Screen *screen, Window window)
95 {
96   Display *dpy = DisplayOfScreen (screen);
97   Pixmap pixmap = 0;
98   XWindowAttributes xgwa;
99   int win_width, win_height;
100   int tex_width, tex_height;
101
102   XGetWindowAttributes (dpy, window, &xgwa);
103   win_width = xgwa.width;
104   win_height = xgwa.height;
105
106   pixmap = XCreatePixmap(dpy, window, xgwa.width, xgwa.height, xgwa.depth);
107   load_random_image (screen, window, pixmap);
108
109   /* GL texture sizes must be powers of two. */
110   tex_width  = to_pow2(win_width);
111   tex_height = to_pow2(win_height);
112
113   /* Convert the server-side Pixmap to a client-side GL-ordered XImage.
114    */
115   {
116     XImage *ximage1, *ximage2;
117     XColor *colors = 0;
118
119     ximage1 = XGetImage (dpy, pixmap, 0, 0, win_width, win_height, ~0L,
120                          ZPixmap);
121     XFreePixmap (dpy, pixmap);
122     pixmap = 0;
123
124     ximage2 = XCreateImage (dpy, xgwa.visual, 32, ZPixmap, 0, 0,
125                             tex_width, tex_height, 32, 0);
126
127     ximage2->data = (char *) calloc (tex_height, ximage2->bytes_per_line);
128
129     {
130       Screen *dscreen = DefaultScreenOfDisplay (dpy);
131       Visual *dvisual = DefaultVisualOfScreen (dscreen);
132       if (visual_class (dscreen, dvisual) == PseudoColor ||
133           visual_class (dscreen, dvisual) == GrayScale)
134         {
135           Colormap cmap = DefaultColormapOfScreen(dscreen);
136           int ncolors = visual_cells (dscreen, dvisual);
137           int i;
138           colors = (XColor *) calloc (sizeof (*colors), ncolors+1);
139           for (i = 0; i < ncolors; i++)
140             colors[i].pixel = i;
141           XQueryColors (dpy, cmap, colors, ncolors);
142         }
143     }
144
145     /* Translate the server-ordered image to a client-ordered image.
146      */
147     {
148       int x, y;
149       int crpos, cgpos, cbpos, capos;  /* bitfield positions */
150       int srpos, sgpos, sbpos;
151       int srmsk, sgmsk, sbmsk;
152       int srsiz, sgsiz, sbsiz;
153       int i;
154
155       unsigned char spread_map[3][256];
156
157       if (colors == 0)  /* truecolor */
158         {
159           srmsk = ximage2->red_mask;
160           sgmsk = ximage2->green_mask;
161           sbmsk = ximage2->blue_mask;
162
163           decode_mask (srmsk, &srpos, &srsiz);
164           decode_mask (sgmsk, &sgpos, &sgsiz);
165           decode_mask (sbmsk, &sbpos, &sbsiz);
166         }
167
168       /* Note that unlike X, which is endianness-agnostic (since any XImage
169          can have its own specific bit ordering, with the server reversing
170          things as necessary) OpenGL pretends everything is client-side, so
171          we need to pack things in "RGBA" order on the client machine,
172          regardless of its endianness.
173        */
174       crpos =  0, cgpos =  8, cbpos = 16, capos = 24;
175
176       if (colors == 0)  /* truecolor */
177         {
178           for (i = 0; i < 256; i++)
179             {
180               spread_map[0][i] = spread_bits (i, srsiz);
181               spread_map[1][i] = spread_bits (i, sgsiz);
182               spread_map[2][i] = spread_bits (i, sbsiz);
183             }
184         }
185
186       for (y = 0; y < win_height; y++)
187         {
188           int y2 = (win_height-1-y); /* Texture maps are upside down. */
189           for (x = 0; x < win_width; x++)
190             {
191               unsigned long sp = XGetPixel (ximage1, x, y2);
192               unsigned char sr, sg, sb;
193               unsigned long cp;
194
195               if (colors)
196                 {
197                   sr = colors[sp].red   & 0xFF;
198                   sg = colors[sp].green & 0xFF;
199                   sb = colors[sp].blue  & 0xFF;
200                 }
201               else
202                 {
203                   sr = (sp & srmsk) >> srpos;
204                   sg = (sp & sgmsk) >> sgpos;
205                   sb = (sp & sbmsk) >> sbpos;
206
207                   sr = spread_map[0][sr];
208                   sg = spread_map[1][sg];
209                   sb = spread_map[2][sb];
210                 }
211
212               cp = ((sr << crpos) |
213                     (sg << cgpos) |
214                     (sb << cbpos) |
215                     (0xFF << capos));
216
217               XPutPixel (ximage2, x, y, cp);
218             }
219         }
220     }
221
222     if (pixmap) XFreePixmap (dpy, pixmap);
223     if (colors) free (colors);
224     free (ximage1->data);
225     ximage1->data = 0;
226     XDestroyImage (ximage1);
227
228 #if 0
229     fprintf(stderr, "%s: grabbed %dx%d window 0x%lx to %dx%d texture\n",
230             progname, win_width, win_height, (unsigned long) window,
231             tex_width, tex_height);
232 #endif
233
234     return ximage2;
235   }
236 }