Merged X11DRV_BITMAP_CreateBitmapHeaderFromPixmap into
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "gdi.h"
27 #include "wine/debug.h"
28 #include "x11drv.h"
29 #include "wingdi.h"
30 #include "windef.h"
31 #include "wine/winuser16.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
34
35   /* GCs used for B&W and color bitmap operations */
36 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
37 HBITMAP BITMAP_stock_bitmap = 0;   /* default stock bitmap */
38 Pixmap BITMAP_stock_pixmap = 0;    /* pixmap for the default stock bitmap */
39
40 /***********************************************************************
41  *           X11DRV_BITMAP_Init
42  */
43 void X11DRV_BITMAP_Init(void)
44 {
45     Pixmap tmpPixmap;
46
47       /* Create the necessary GCs */
48
49     wine_tsx11_lock();
50     BITMAP_stock_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
51     BITMAP_monoGC = XCreateGC( gdi_display, BITMAP_stock_pixmap, 0, NULL );
52     XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
53     XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
54
55     if (screen_depth != 1)
56     {
57         if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
58         {
59             BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
60             XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
61             XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
62             XFreePixmap( gdi_display, tmpPixmap );
63         }
64     }
65     wine_tsx11_unlock();
66 }
67
68 /***********************************************************************
69  *           SelectBitmap   (X11DRV.@)
70  */
71 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
72 {
73     BITMAPOBJ *bmp;
74
75     if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
76
77     if(physDev->xrender)
78         X11DRV_XRender_UpdateDrawable( physDev );
79
80     if (hbitmap == BITMAP_stock_bitmap)
81         physDev->drawable = BITMAP_stock_pixmap;
82     else
83         physDev->drawable = X11DRV_get_pixmap( hbitmap );
84
85       /* Change GC depth if needed */
86
87     if (physDev->depth != bmp->bitmap.bmBitsPixel)
88     {
89         physDev->depth = bmp->bitmap.bmBitsPixel;
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     GDI_ReleaseObj( hbitmap );
99     return hbitmap;
100 }
101
102
103 /****************************************************************************
104  *        CreateBitmap   (X11DRV.@)
105  *
106  * Create a device dependent X11 bitmap
107  *
108  * Returns TRUE on success else FALSE
109  */
110 BOOL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
111 {
112     Pixmap pixmap;
113     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
114
115     if(!bmp) {
116         WARN("Bad bitmap handle %p\n", hbitmap);
117         return FALSE;
118     }
119
120       /* Check parameters */
121     if (bmp->bitmap.bmPlanes != 1)
122     {
123         GDI_ReleaseObj( hbitmap );
124         return 0;
125     }
126     if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
127     {
128         ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
129             bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
130         GDI_ReleaseObj( hbitmap );
131         return FALSE;
132     }
133     if (hbitmap == BITMAP_stock_bitmap)
134     {
135         ERR( "called for stock bitmap, please report\n" );
136         GDI_ReleaseObj( hbitmap );
137         return FALSE;
138     }
139
140     TRACE("(%p) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
141           bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
142
143       /* Create the pixmap */
144     wine_tsx11_lock();
145     pixmap = XCreatePixmap(gdi_display, root_window,
146                            bmp->bitmap.bmWidth, bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
147     wine_tsx11_unlock();
148     if (!pixmap)
149     {
150         WARN("Can't create Pixmap\n");
151         GDI_ReleaseObj( hbitmap );
152         return FALSE;
153     }
154     X11DRV_set_pixmap( hbitmap, pixmap );
155
156     if (bmp->bitmap.bmBits) /* Set bitmap bits */
157         X11DRV_SetBitmapBits( hbitmap, bmp->bitmap.bmBits,
158                               bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes );
159
160     GDI_ReleaseObj( hbitmap );
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     BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
175     LONG old_height, height;
176     Pixmap pixmap = X11DRV_get_pixmap( hbitmap );
177     XImage *image;
178     LPBYTE tbuf, startline;
179     int h, w;
180
181     if (!bmp) return 0;
182     TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
183
184     wine_tsx11_lock();
185
186     /* Hack: change the bitmap height temporarily to avoid */
187     /*       getting unnecessary bitmap rows. */
188
189     old_height = bmp->bitmap.bmHeight;
190     height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
191
192     image = XGetImage( gdi_display, pixmap, 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
193                        AllPlanes, ZPixmap );
194     bmp->bitmap.bmHeight = old_height;
195
196     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
197
198     startline = buffer;
199     switch (bmp->bitmap.bmBitsPixel)
200     {
201     case 1:
202         for (h=0;h<height;h++)
203         {
204             tbuf = startline;
205             *tbuf = 0;
206             for (w=0;w<bmp->bitmap.bmWidth;w++)
207             {
208                 if ((w%8) == 0)
209                     *tbuf = 0;
210                 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
211                 if ((w&7) == 7) ++tbuf;
212             }
213             startline += bmp->bitmap.bmWidthBytes;
214         }
215         break;
216     case 4:
217         for (h=0;h<height;h++)
218         {
219             tbuf = startline;
220             for (w=0;w<bmp->bitmap.bmWidth;w++)
221             {
222                 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
223                 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
224             }
225             startline += bmp->bitmap.bmWidthBytes;
226         }
227         break;
228     case 8:
229         for (h=0;h<height;h++)
230         {
231             tbuf = startline;
232             for (w=0;w<bmp->bitmap.bmWidth;w++)
233                 *tbuf++ = XGetPixel(image,w,h);
234             startline += bmp->bitmap.bmWidthBytes;
235         }
236         break;
237     case 15:
238     case 16:
239         for (h=0;h<height;h++)
240         {
241             tbuf = startline;
242             for (w=0;w<bmp->bitmap.bmWidth;w++)
243             {
244                 long pixel = XGetPixel(image,w,h);
245
246                 *tbuf++ = pixel & 0xff;
247                 *tbuf++ = (pixel>>8) & 0xff;
248             }
249             startline += bmp->bitmap.bmWidthBytes;
250         }
251         break;
252     case 24:
253         for (h=0;h<height;h++)
254         {
255             tbuf = startline;
256             for (w=0;w<bmp->bitmap.bmWidth;w++)
257             {
258                 long pixel = XGetPixel(image,w,h);
259
260                 *tbuf++ = pixel & 0xff;
261                 *tbuf++ = (pixel>> 8) & 0xff;
262                 *tbuf++ = (pixel>>16) & 0xff;
263             }
264             startline += bmp->bitmap.bmWidthBytes;
265         }
266         break;
267
268     case 32:
269         for (h=0;h<height;h++)
270         {
271             tbuf = startline;
272             for (w=0;w<bmp->bitmap.bmWidth;w++)
273             {
274                 long pixel = XGetPixel(image,w,h);
275
276                 *tbuf++ = pixel & 0xff;
277                 *tbuf++ = (pixel>> 8) & 0xff;
278                 *tbuf++ = (pixel>>16) & 0xff;
279                 *tbuf++ = (pixel>>24) & 0xff;
280             }
281             startline += bmp->bitmap.bmWidthBytes;
282         }
283         break;
284     default:
285         FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
286     }
287     XDestroyImage( image );
288     wine_tsx11_unlock();
289     GDI_ReleaseObj( hbitmap );
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     BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
305     Pixmap pixmap = X11DRV_get_pixmap( hbitmap );
306     LONG height;
307     XImage *image;
308     const BYTE *sbuf, *startline;
309     int w, h;
310
311     if (!bmp) return 0;
312     TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
313
314     height = count / bmp->bitmap.bmWidthBytes;
315
316     wine_tsx11_lock();
317     image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
318                           bmp->bitmap.bmWidth, height, 32, 0 );
319     if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
320     {
321         WARN("No memory to create image data.\n");
322         XDestroyImage( image );
323         wine_tsx11_unlock();
324         GDI_ReleaseObj( hbitmap );
325         return 0;
326     }
327
328     /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
329
330     startline = bits;
331
332     switch (bmp->bitmap.bmBitsPixel)
333     {
334     case 1:
335         for (h=0;h<height;h++)
336         {
337             sbuf = startline;
338             for (w=0;w<bmp->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 += bmp->bitmap.bmWidthBytes;
345         }
346         break;
347     case 4:
348         for (h=0;h<height;h++)
349         {
350             sbuf = startline;
351             for (w=0;w<bmp->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 += bmp->bitmap.bmWidthBytes;
357         }
358         break;
359     case 8:
360         for (h=0;h<height;h++)
361         {
362             sbuf = startline;
363             for (w=0;w<bmp->bitmap.bmWidth;w++)
364                 XPutPixel(image,w,h,*sbuf++);
365             startline += bmp->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<bmp->bitmap.bmWidth;w++)
374             {
375                 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
376                 sbuf+=2;
377             }
378             startline += bmp->bitmap.bmWidthBytes;
379         }
380         break;
381     case 24:
382         for (h=0;h<height;h++)
383         {
384             sbuf = startline;
385             for (w=0;w<bmp->bitmap.bmWidth;w++)
386             {
387                 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
388                 sbuf += 3;
389             }
390             startline += bmp->bitmap.bmWidthBytes;
391         }
392         break;
393     case 32:
394         for (h=0;h<height;h++)
395         {
396             sbuf = startline;
397             for (w=0;w<bmp->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 += bmp->bitmap.bmWidthBytes;
403         }
404         break;
405     default:
406       FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
407
408     }
409     XPutImage( gdi_display, pixmap, BITMAP_GC(bmp),
410                image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
411     XDestroyImage( image ); /* frees image->data too */
412     wine_tsx11_unlock();
413     GDI_ReleaseObj( hbitmap );
414     return count;
415 }
416
417 /***********************************************************************
418  *           DeleteBitmap   (X11DRV.@)
419  */
420 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
421 {
422     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
423     if (bmp)
424     {
425         Pixmap pixmap;
426         if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
427         pixmap = X11DRV_set_pixmap( hbitmap, 0 );
428         wine_tsx11_lock();
429         if (pixmap) XFreePixmap( gdi_display, pixmap );
430         wine_tsx11_unlock();
431         GDI_ReleaseObj( hbitmap );
432     }
433     return TRUE;
434 }
435
436
437 /***********************************************************************
438  *           X11DRV_set_pixmap
439  *
440  * Set the pixmap associated to a bitmap, and return the previous one.
441  */
442 Pixmap X11DRV_set_pixmap( HBITMAP hbitmap, Pixmap pixmap )
443 {
444     Pixmap ret = 0;
445     BITMAPOBJ *bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
446     if (bmp)
447     {
448         ret = (Pixmap)bmp->physBitmap;
449         bmp->physBitmap = (void *)pixmap;
450         GDI_ReleaseObj( hbitmap );
451     }
452     return ret;
453 }
454
455
456 /***********************************************************************
457  *           X11DRV_get_pixmap
458  *
459  * Retrieve the pixmap associated to a bitmap.
460  */
461 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
462 {
463     Pixmap pixmap = 0;
464     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
465     if (bmp)
466     {
467         pixmap = (Pixmap)bmp->physBitmap;
468         GDI_ReleaseObj( hbitmap );
469     }
470     return pixmap;
471 }