Update the address of the Free Software Foundation.
[wine] / dlls / x11drv / bitmap.c
1 /*
2  * X11DRV bitmap objects
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1999 Noel Borthwick
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "windef.h"
27 #include "wingdi.h"
28 #include "wine/debug.h"
29 #include "x11drv.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
32
33   /* GCs used for B&W and color bitmap operations */
34 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
35 X_PHYSBITMAP BITMAP_stock_phys_bitmap = { 0 };  /* phys bitmap for the default stock bitmap */
36
37 static XContext bitmap_context;  /* X context to associate a phys bitmap to a handle */
38
39 /***********************************************************************
40  *           X11DRV_BITMAP_Init
41  */
42 void X11DRV_BITMAP_Init(void)
43 {
44     Pixmap tmpPixmap;
45
46       /* Create the necessary GCs */
47
48     wine_tsx11_lock();
49     bitmap_context = XUniqueContext();
50     BITMAP_stock_phys_bitmap.pixmap_depth = 1;
51     BITMAP_stock_phys_bitmap.pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
52     BITMAP_monoGC = XCreateGC( gdi_display, BITMAP_stock_phys_bitmap.pixmap, 0, NULL );
53     XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
54     XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
55
56     if (screen_depth != 1)
57     {
58         if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
59         {
60             BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
61             XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
62             XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
63             XFreePixmap( gdi_display, tmpPixmap );
64         }
65     }
66     wine_tsx11_unlock();
67 }
68
69 /***********************************************************************
70  *           SelectBitmap   (X11DRV.@)
71  */
72 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
73 {
74     X_PHYSBITMAP *physBitmap;
75
76     if(physDev->xrender)
77         X11DRV_XRender_UpdateDrawable( physDev );
78
79     if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap) physBitmap = &BITMAP_stock_phys_bitmap;
80     else if (!(physBitmap = X11DRV_get_phys_bitmap( hbitmap ))) return 0;
81
82     physDev->bitmap = physBitmap;
83     physDev->drawable = physBitmap->pixmap;
84
85       /* Change GC depth if needed */
86
87     if (physDev->depth != physBitmap->pixmap_depth)
88     {
89         physDev->depth = physBitmap->pixmap_depth;
90         wine_tsx11_lock();
91         XFreeGC( gdi_display, physDev->gc );
92         physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
93         XSetGraphicsExposures( gdi_display, physDev->gc, False );
94         XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
95         XFlush( gdi_display );
96         wine_tsx11_unlock();
97     }
98     return hbitmap;
99 }
100
101
102 /****************************************************************************
103  *        CreateBitmap   (X11DRV.@)
104  *
105  * Create a device dependent X11 bitmap
106  *
107  * Returns TRUE on success else FALSE
108  */
109 BOOL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, LPVOID bmBits )
110 {
111     X_PHYSBITMAP *physBitmap;
112     BITMAP bitmap;
113
114     if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
115
116       /* Check parameters */
117     if (bitmap.bmPlanes != 1) return FALSE;
118
119     if ((bitmap.bmBitsPixel != 1) && (bitmap.bmBitsPixel != screen_depth))
120     {
121         ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
122             bitmap.bmPlanes, bitmap.bmBitsPixel);
123         return FALSE;
124     }
125     if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
126     {
127         ERR( "called for stock bitmap, please report\n" );
128         return FALSE;
129     }
130
131     TRACE("(%p) %dx%d %d bpp\n", hbitmap, bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
132
133     if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return FALSE;
134
135       /* Create the pixmap */
136     wine_tsx11_lock();
137     physBitmap->pixmap_depth = bitmap.bmBitsPixel;
138     physBitmap->pixmap = XCreatePixmap(gdi_display, root_window,
139                                        bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
140     wine_tsx11_unlock();
141     if (!physBitmap->pixmap)
142     {
143         WARN("Can't create Pixmap\n");
144         HeapFree( GetProcessHeap(), 0, physBitmap );
145         return FALSE;
146     }
147
148     if (bmBits) /* Set bitmap bits */
149     {
150         X11DRV_SetBitmapBits( hbitmap, bmBits, bitmap.bmHeight * bitmap.bmWidthBytes );
151     }
152     else  /* else clear the bitmap */
153     {
154         wine_tsx11_lock();
155         XSetFunction( gdi_display, BITMAP_GC(physBitmap), GXclear );
156         XFillRectangle( gdi_display, physBitmap->pixmap, BITMAP_GC(physBitmap), 0, 0,
157                         bitmap.bmWidth, bitmap.bmHeight );
158         XSetFunction( gdi_display, BITMAP_GC(physBitmap), GXcopy );
159         wine_tsx11_unlock();
160     }
161     return TRUE;
162 }
163
164
165 /***********************************************************************
166  *           GetBitmapBits   (X11DRV.@)
167  *
168  * RETURNS
169  *    Success: Number of bytes copied
170  *    Failure: 0
171  */
172 LONG X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
173 {
174     BITMAP bitmap;
175     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
176     LONG old_height, height;
177     XImage *image;
178     LPBYTE tbuf, startline;
179     int h, w;
180
181     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
182
183     TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", hbitmap, buffer, count);
184
185     wine_tsx11_lock();
186
187     /* Hack: change the bitmap height temporarily to avoid */
188     /*       getting unnecessary bitmap rows. */
189
190     old_height = bitmap.bmHeight;
191     height = bitmap.bmHeight = count / bitmap.bmWidthBytes;
192
193     image = XGetImage( gdi_display, physBitmap->pixmap, 0, 0,
194                        bitmap.bmWidth, bitmap.bmHeight, AllPlanes, ZPixmap );
195     bitmap.bmHeight = old_height;
196
197     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
198
199     startline = buffer;
200     switch (physBitmap->pixmap_depth)
201     {
202     case 1:
203         for (h=0;h<height;h++)
204         {
205             tbuf = startline;
206             *tbuf = 0;
207             for (w=0;w<bitmap.bmWidth;w++)
208             {
209                 if ((w%8) == 0)
210                     *tbuf = 0;
211                 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
212                 if ((w&7) == 7) ++tbuf;
213             }
214             startline += bitmap.bmWidthBytes;
215         }
216         break;
217     case 4:
218         for (h=0;h<height;h++)
219         {
220             tbuf = startline;
221             for (w=0;w<bitmap.bmWidth;w++)
222             {
223                 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
224                 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
225             }
226             startline += bitmap.bmWidthBytes;
227         }
228         break;
229     case 8:
230         for (h=0;h<height;h++)
231         {
232             tbuf = startline;
233             for (w=0;w<bitmap.bmWidth;w++)
234                 *tbuf++ = XGetPixel(image,w,h);
235             startline += bitmap.bmWidthBytes;
236         }
237         break;
238     case 15:
239     case 16:
240         for (h=0;h<height;h++)
241         {
242             tbuf = startline;
243             for (w=0;w<bitmap.bmWidth;w++)
244             {
245                 long pixel = XGetPixel(image,w,h);
246
247                 *tbuf++ = pixel & 0xff;
248                 *tbuf++ = (pixel>>8) & 0xff;
249             }
250             startline += bitmap.bmWidthBytes;
251         }
252         break;
253     case 24:
254         for (h=0;h<height;h++)
255         {
256             tbuf = startline;
257             for (w=0;w<bitmap.bmWidth;w++)
258             {
259                 long pixel = XGetPixel(image,w,h);
260
261                 *tbuf++ = pixel & 0xff;
262                 *tbuf++ = (pixel>> 8) & 0xff;
263                 *tbuf++ = (pixel>>16) & 0xff;
264             }
265             startline += bitmap.bmWidthBytes;
266         }
267         break;
268
269     case 32:
270         for (h=0;h<height;h++)
271         {
272             tbuf = startline;
273             for (w=0;w<bitmap.bmWidth;w++)
274             {
275                 long pixel = XGetPixel(image,w,h);
276
277                 *tbuf++ = pixel & 0xff;
278                 *tbuf++ = (pixel>> 8) & 0xff;
279                 *tbuf++ = (pixel>>16) & 0xff;
280                 *tbuf++ = (pixel>>24) & 0xff;
281             }
282             startline += bitmap.bmWidthBytes;
283         }
284         break;
285     default:
286         FIXME("Unhandled bits:%d\n", physBitmap->pixmap_depth);
287     }
288     XDestroyImage( image );
289     wine_tsx11_unlock();
290     return count;
291 }
292
293
294
295 /******************************************************************************
296  *             SetBitmapBits   (X11DRV.@)
297  *
298  * RETURNS
299  *    Success: Number of bytes used in setting the bitmap bits
300  *    Failure: 0
301  */
302 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
303 {
304     BITMAP bitmap;
305     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
306     LONG height;
307     XImage *image;
308     const BYTE *sbuf, *startline;
309     int w, h;
310
311     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
312
313     TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", hbitmap, bits, count);
314
315     height = count / bitmap.bmWidthBytes;
316
317     wine_tsx11_lock();
318     image = XCreateImage( gdi_display, visual, physBitmap->pixmap_depth, ZPixmap, 0, NULL,
319                           bitmap.bmWidth, height, 32, 0 );
320     if (!(image->data = malloc(image->bytes_per_line * height)))
321     {
322         WARN("No memory to create image data.\n");
323         XDestroyImage( image );
324         wine_tsx11_unlock();
325         return 0;
326     }
327
328     /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
329
330     startline = bits;
331
332     switch (physBitmap->pixmap_depth)
333     {
334     case 1:
335         for (h=0;h<height;h++)
336         {
337             sbuf = startline;
338             for (w=0;w<bitmap.bmWidth;w++)
339             {
340                 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
341                 if ((w&7) == 7)
342                     sbuf++;
343             }
344             startline += bitmap.bmWidthBytes;
345         }
346         break;
347     case 4:
348         for (h=0;h<height;h++)
349         {
350             sbuf = startline;
351             for (w=0;w<bitmap.bmWidth;w++)
352             {
353                 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
354                 else XPutPixel( image, w, h, *sbuf++ & 0xf );
355             }
356             startline += bitmap.bmWidthBytes;
357         }
358         break;
359     case 8:
360         for (h=0;h<height;h++)
361         {
362             sbuf = startline;
363             for (w=0;w<bitmap.bmWidth;w++)
364                 XPutPixel(image,w,h,*sbuf++);
365             startline += bitmap.bmWidthBytes;
366         }
367         break;
368     case 15:
369     case 16:
370         for (h=0;h<height;h++)
371         {
372             sbuf = startline;
373             for (w=0;w<bitmap.bmWidth;w++)
374             {
375                 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
376                 sbuf+=2;
377             }
378             startline += bitmap.bmWidthBytes;
379         }
380         break;
381     case 24:
382         for (h=0;h<height;h++)
383         {
384             sbuf = startline;
385             for (w=0;w<bitmap.bmWidth;w++)
386             {
387                 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
388                 sbuf += 3;
389             }
390             startline += bitmap.bmWidthBytes;
391         }
392         break;
393     case 32:
394         for (h=0;h<height;h++)
395         {
396             sbuf = startline;
397             for (w=0;w<bitmap.bmWidth;w++)
398             {
399                 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
400                 sbuf += 4;
401             }
402             startline += bitmap.bmWidthBytes;
403         }
404         break;
405     default:
406       FIXME("Unhandled bits:%d\n", physBitmap->pixmap_depth);
407
408     }
409     XPutImage( gdi_display, physBitmap->pixmap, BITMAP_GC(physBitmap),
410                image, 0, 0, 0, 0, bitmap.bmWidth, height );
411     XDestroyImage( image ); /* frees image->data too */
412     wine_tsx11_unlock();
413     return count;
414 }
415
416 /***********************************************************************
417  *           DeleteBitmap   (X11DRV.@)
418  */
419 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
420 {
421     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
422
423     if (physBitmap)
424     {
425         DIBSECTION dib;
426
427         if (GetObjectW( hbitmap, sizeof(dib), &dib ) == sizeof(dib))
428             X11DRV_DIB_DeleteDIBSection( physBitmap, &dib );
429
430         if (physBitmap->glxpixmap) destroy_glxpixmap(physBitmap->glxpixmap); 
431         wine_tsx11_lock();
432         if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
433         XDeleteContext( gdi_display, (XID)hbitmap, bitmap_context );
434         wine_tsx11_unlock();
435         HeapFree( GetProcessHeap(), 0, physBitmap );
436     }
437     return TRUE;
438 }
439
440
441 /***********************************************************************
442  *           X11DRV_get_phys_bitmap
443  *
444  * Retrieve the X physical bitmap info.
445  */
446 X_PHYSBITMAP *X11DRV_get_phys_bitmap( HBITMAP hbitmap )
447 {
448     X_PHYSBITMAP *ret;
449
450     wine_tsx11_lock();
451     if (XFindContext( gdi_display, (XID)hbitmap, bitmap_context, (char **)&ret )) ret = NULL;
452     wine_tsx11_unlock();
453     return ret;
454 }
455
456
457 /***********************************************************************
458  *           X11DRV_init_phys_bitmap
459  *
460  * Initialize the X physical bitmap info.
461  */
462 X_PHYSBITMAP *X11DRV_init_phys_bitmap( HBITMAP hbitmap )
463 {
464     X_PHYSBITMAP *ret;
465
466     if ((ret = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret) )) != NULL)
467     {
468         ret->hbitmap = hbitmap;
469         wine_tsx11_lock();
470         XSaveContext( gdi_display, (XID)hbitmap, bitmap_context, (char *)ret );
471         wine_tsx11_unlock();
472     }
473     return ret;
474 }
475
476
477 /***********************************************************************
478  *           X11DRV_get_pixmap
479  *
480  * Retrieve the pixmap associated to a bitmap.
481  */
482 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
483 {
484     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
485
486     if (!physBitmap) return 0;
487     return physBitmap->pixmap;
488 }