ok() does not support '%S'. Store the Ansi version, convert to Unicode
[wine] / graphics / 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 "ts_xlib.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include "gdi.h"
29 #include "bitmap.h"
30 #include "wine/debug.h"
31 #include "x11drv.h"
32 #include "wingdi.h"
33 #include "windef.h"
34 #include "wine/winuser16.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
37
38   /* GCs used for B&W and color bitmap operations */
39 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
40 Pixmap BITMAP_stock_pixmap;   /* pixmap for the default stock bitmap */
41
42 extern const DC_FUNCTIONS *X11DRV_DC_Funcs;  /* hack */
43
44 /***********************************************************************
45  *           X11DRV_BITMAP_Init
46  */
47 BOOL X11DRV_BITMAP_Init(void)
48 {
49     Pixmap tmpPixmap;
50
51       /* Create the necessary GCs */
52
53     wine_tsx11_lock();
54     BITMAP_stock_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
55     BITMAP_monoGC = XCreateGC( gdi_display, BITMAP_stock_pixmap, 0, NULL );
56     XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
57     XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
58
59     if (screen_depth != 1)
60     {
61         if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
62         {
63             BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
64             XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
65             XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
66             XFreePixmap( gdi_display, tmpPixmap );
67         }
68     }
69     wine_tsx11_unlock();
70     return TRUE;
71 }
72
73 /***********************************************************************
74  *           SelectBitmap   (X11DRV.@)
75  */
76 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
77 {
78     BITMAPOBJ *bmp;
79     DC *dc = physDev->dc;
80
81     if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
82
83     if(physDev->xrender)
84         X11DRV_XRender_UpdateDrawable( physDev );
85
86     if (hbitmap == GetStockObject(DEFAULT_BITMAP))
87         physDev->drawable = BITMAP_stock_pixmap;
88     else
89         physDev->drawable = (Pixmap)bmp->physBitmap;
90
91       /* Change GC depth if needed */
92
93     if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
94     {
95         wine_tsx11_lock();
96         XFreeGC( gdi_display, physDev->gc );
97         physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
98         XSetGraphicsExposures( gdi_display, physDev->gc, False );
99         XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
100         XFlush( gdi_display );
101         wine_tsx11_unlock();
102     }
103     GDI_ReleaseObj( hbitmap );
104     return hbitmap;
105 }
106
107
108 /****************************************************************************
109  *        CreateBitmap   (X11DRV.@)
110  *
111  * Create a device dependent X11 bitmap
112  *
113  * Returns TRUE on success else FALSE
114  */
115 BOOL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
116 {
117     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
118
119     if(!bmp) {
120         WARN("Bad bitmap handle %p\n", hbitmap);
121         return FALSE;
122     }
123
124       /* Check parameters */
125     if (bmp->bitmap.bmPlanes != 1)
126     {
127         GDI_ReleaseObj( hbitmap );
128         return 0;
129     }
130     if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
131     {
132         ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
133             bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
134         GDI_ReleaseObj( hbitmap );
135         return FALSE;
136     }
137     if (hbitmap == GetStockObject(DEFAULT_BITMAP))
138     {
139         ERR( "called for stock bitmap, please report\n" );
140         GDI_ReleaseObj( hbitmap );
141         return FALSE;
142     }
143
144     TRACE("(%p) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
145           bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
146
147       /* Create the pixmap */
148     if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
149                                                     bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
150                                                     bmp->bitmap.bmBitsPixel)))
151     {
152         WARN("Can't create Pixmap\n");
153         GDI_ReleaseObj( hbitmap );
154         return FALSE;
155     }
156
157     if (bmp->bitmap.bmBits) /* Set bitmap bits */
158         X11DRV_SetBitmapBits( hbitmap, bmp->bitmap.bmBits,
159                               bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes );
160
161     GDI_ReleaseObj( hbitmap );
162     return TRUE;
163 }
164
165
166 /***********************************************************************
167  *           GetBitmapBits   (X11DRV.@)
168  *
169  * RETURNS
170  *    Success: Number of bytes copied
171  *    Failure: 0
172  */
173 LONG X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
174 {
175     BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
176     LONG old_height, height;
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)bmp->physBitmap,
193                        0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
194                        AllPlanes, ZPixmap );
195     bmp->bitmap.bmHeight = old_height;
196
197     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
198
199     startline = buffer;
200     switch (bmp->bitmap.bmBitsPixel)
201     {
202     case 1:
203         for (h=0;h<height;h++)
204         {
205             tbuf = startline;
206             *tbuf = 0;
207             for (w=0;w<bmp->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 += bmp->bitmap.bmWidthBytes;
215         }
216         break;
217     case 4:
218         for (h=0;h<height;h++)
219         {
220             tbuf = startline;
221             for (w=0;w<bmp->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 += bmp->bitmap.bmWidthBytes;
227         }
228         break;
229     case 8:
230         for (h=0;h<height;h++)
231         {
232             tbuf = startline;
233             for (w=0;w<bmp->bitmap.bmWidth;w++)
234                 *tbuf++ = XGetPixel(image,w,h);
235             startline += bmp->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<bmp->bitmap.bmWidth;w++)
244             {
245                 long pixel = XGetPixel(image,w,h);
246
247                 *tbuf++ = pixel & 0xff;
248                 *tbuf++ = (pixel>>8) & 0xff;
249             }
250             startline += bmp->bitmap.bmWidthBytes;
251         }
252         break;
253     case 24:
254         for (h=0;h<height;h++)
255         {
256             tbuf = startline;
257             for (w=0;w<bmp->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 += bmp->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<bmp->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 += bmp->bitmap.bmWidthBytes;
283         }
284         break;
285     default:
286         FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
287     }
288     XDestroyImage( image );
289     wine_tsx11_unlock();
290     GDI_ReleaseObj( hbitmap );
291     return count;
292 }
293
294
295
296 /******************************************************************************
297  *             SetBitmapBits   (X11DRV.@)
298  *
299  * RETURNS
300  *    Success: Number of bytes used in setting the bitmap bits
301  *    Failure: 0
302  */
303 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
304 {
305     BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
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)bmp->physBitmap, 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         if (bmp->physBitmap) TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
426         bmp->physBitmap = NULL;
427         if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
428         GDI_ReleaseObj( hbitmap );
429     }
430     return TRUE;
431 }
432
433 /**************************************************************************
434  *              X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
435  *
436  *  Allocates an HBITMAP which references the Pixmap passed in.
437  *  Note: This function makes the bitmap an owner of the Pixmap so subsequently
438  *  calling DeleteObject on this will free the Pixmap as well.
439  */
440 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
441 {
442     HBITMAP hBmp = 0;
443     BITMAPOBJ *pBmp = NULL;
444     Window root;
445     int x,y;               /* Unused */
446     unsigned border_width; /* Unused */
447     unsigned int depth, width, height;
448
449     /* Get the Pixmap dimensions and bit depth */
450     if ( 0 == TSXGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
451                              &border_width, &depth) )
452         goto END;
453
454     TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
455           width, height, depth);
456
457     /*
458      * Create an HBITMAP with the same dimensions and BPP as the pixmap,
459      * and make it a container for the pixmap passed.
460      */
461     hBmp = CreateBitmap( width, height, 1, depth, NULL );
462
463     pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
464
465     pBmp->funcs = X11DRV_DC_Funcs;
466     pBmp->physBitmap = (void *)pixmap;
467     GDI_ReleaseObj( hBmp );
468
469 END:
470     TRACE("\tReturning HBITMAP %p\n", hBmp);
471     return hBmp;
472 }
473
474
475 /**************************************************************************
476  *              X11DRV_BITMAP_CreateBitmapFromPixmap
477  *
478  *  Allocates an HBITMAP and copies the Pixmap data into it.
479  *  If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
480  */
481 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
482 {
483     HBITMAP hBmp = 0, hBmpCopy = 0;
484     BITMAPOBJ *pBmp = NULL;
485     unsigned int width, height;
486
487     /* Allocate an HBITMAP which references the Pixmap passed to us */
488     hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
489     if (!hBmp)
490     {
491         TRACE("\tCould not create bitmap header for Pixmap\n");
492         goto END;
493     }
494
495     /* Get the bitmap dimensions */
496     width = pBmp->bitmap.bmWidth;
497     height = pBmp->bitmap.bmHeight;
498
499     hBmpCopy = (HBITMAP)CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
500
501     /* We can now get rid of the HBITMAP wrapper we created earlier.
502      * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
503      */
504     if (!bDeletePixmap)
505     {
506         /* Manually clear the bitmap internals to prevent the Pixmap
507          * from being deleted by DeleteObject.
508          */
509         pBmp->physBitmap = NULL;
510         pBmp->funcs = NULL;
511     }
512     DeleteObject(hBmp);
513
514 END:
515     TRACE("\tReturning HBITMAP %p\n", hBmpCopy);
516     return hBmpCopy;
517 }
518
519
520 /**************************************************************************
521  *                 X11DRV_BITMAP_CreatePixmapFromBitmap
522  *
523  *    Creates a Pixmap from a bitmap
524  */
525 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
526 {
527     HGLOBAL hPackedDIB = 0;
528     Pixmap pixmap = 0;
529
530     /*
531      * Create a packed DIB from the bitmap passed to us.
532      * A packed DIB contains a BITMAPINFO structure followed immediately by
533      * an optional color palette and the pixel data.
534      */
535     hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
536
537     /* Create a Pixmap from the packed DIB */
538     pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
539
540     /* Free the temporary packed DIB */
541     GlobalFree(hPackedDIB);
542
543     return pixmap;
544 }
545
546
547 /***********************************************************************
548  *           X11DRV_BITMAP_Pixmap
549  *
550  * This function exists solely for x11 driver of the window system.
551  */
552 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
553 {
554     Pixmap pixmap;
555     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
556     if (bmp) {
557       pixmap = (Pixmap)bmp->physBitmap;
558       GDI_ReleaseObj( hbitmap );
559     }
560     else {
561       ERR("handle %p returned no obj\n", hbitmap);
562       pixmap = 0;
563     }
564     return pixmap;
565 }