Test bits with '&' instead of '&&'.
[wine] / graphics / x11drv / bitmap.c
1 /*
2  * X11DRV bitmap objects
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1999 Noel Borthwick
6  */
7
8 #include "config.h"
9
10 #include "ts_xlib.h"
11 #include "ts_xutil.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "gdi.h"
16 #include "bitmap.h"
17 #include "debugtools.h"
18 #include "x11drv.h"
19 #include "wingdi.h"
20 #include "windef.h"
21 #include "wine/winuser16.h"
22
23 DEFAULT_DEBUG_CHANNEL(x11drv);
24
25   /* GCs used for B&W and color bitmap operations */
26 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
27
28 extern const DC_FUNCTIONS *X11DRV_DC_Funcs;  /* hack */
29
30 /***********************************************************************
31  *           X11DRV_BITMAP_Init
32  */
33 BOOL X11DRV_BITMAP_Init(void)
34 {
35     Pixmap tmpPixmap;
36
37       /* Create the necessary GCs */
38
39     wine_tsx11_lock();
40     if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 )))
41     {
42         BITMAP_monoGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
43         XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
44         XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
45         XFreePixmap( gdi_display, tmpPixmap );
46     }
47
48     if (screen_depth != 1)
49     {
50         if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
51         {
52             BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
53             XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
54             XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
55             XFreePixmap( gdi_display, tmpPixmap );
56         }
57     }
58     wine_tsx11_unlock();
59     return TRUE;
60 }
61
62 /***********************************************************************
63  *           X11DRV_BITMAP_SelectObject
64  */
65 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap )
66 {
67     BITMAPOBJ *bmp;
68     HRGN hrgn;
69     HBITMAP prevHandle = dc->hBitmap;
70     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
71
72     if (!(dc->flags & DC_MEMORY)) return 0;
73     if (hbitmap == dc->hBitmap) return hbitmap;  /* nothing to do */
74     if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
75
76     if (bmp->header.dwCount && (hbitmap != GetStockObject(DEFAULT_BITMAP)))
77     {
78         WARN( "Bitmap already selected in another DC\n" );
79         GDI_ReleaseObj( hbitmap );
80         return 0;
81     }
82
83     if(!bmp->physBitmap)
84     {
85         if(!X11DRV_CreateBitmap(hbitmap))
86         {
87             GDI_ReleaseObj( hbitmap );
88             return 0;
89         }
90     }
91
92     if(bmp->funcs != dc->funcs) {
93         WARN("Trying to select non-X11 DDB into an X11 dc\n");
94         GDI_ReleaseObj( hbitmap );
95         return 0;
96     }
97
98     if (!(hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight)))
99     {
100         GDI_ReleaseObj( hbitmap );
101         return 0;
102     }
103
104     dc->totalExtent.left   = 0;
105     dc->totalExtent.top    = 0;
106     dc->totalExtent.right  = bmp->bitmap.bmWidth;
107     dc->totalExtent.bottom = bmp->bitmap.bmHeight;
108
109     physDev->drawable = (Pixmap)bmp->physBitmap;
110     dc->hBitmap     = hbitmap;
111
112     SelectVisRgn16( dc->hSelf, hrgn );
113     DeleteObject( hrgn );
114
115       /* Change GC depth if needed */
116
117     if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
118     {
119         wine_tsx11_lock();
120         XFreeGC( gdi_display, physDev->gc );
121         physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
122         XSetGraphicsExposures( gdi_display, physDev->gc, False );
123         XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
124         XFlush( gdi_display );
125         wine_tsx11_unlock();
126         dc->bitsPerPixel = bmp->bitmap.bmBitsPixel;
127         DC_InitDC( dc );
128     }
129     GDI_ReleaseObj( hbitmap );
130     return prevHandle;
131 }
132
133
134 /****************************************************************************
135  *
136  *        X11DRV_CreateBitmap
137  *
138  * Create a device dependent X11 bitmap
139  *
140  * Returns TRUE on success else FALSE
141  *
142  */
143
144 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
145 {
146     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
147
148     if(!bmp) {
149         WARN("Bad bitmap handle %08x\n", hbitmap);
150         return FALSE;
151     }
152
153       /* Check parameters */
154     if (bmp->bitmap.bmPlanes != 1)
155     {
156         GDI_ReleaseObj( hbitmap );
157         return 0;
158     }
159     if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
160     {
161         ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
162             bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
163         GDI_ReleaseObj( hbitmap );
164         return FALSE;
165     }
166
167     TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
168           bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
169
170       /* Create the pixmap */
171     if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
172                                                     bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
173                                                     bmp->bitmap.bmBitsPixel)))
174     {
175         WARN("Can't create Pixmap\n");
176         GDI_ReleaseObj( hbitmap );
177         return FALSE;
178     }
179     bmp->funcs = X11DRV_DC_Funcs;
180
181     if (bmp->bitmap.bmBits) /* Set bitmap bits */
182         X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
183                            bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
184                            DDB_SET );
185
186     GDI_ReleaseObj( hbitmap );
187     return TRUE;
188 }
189
190
191 /***********************************************************************
192  *           X11DRV_GetBitmapBits
193  * 
194  * RETURNS
195  *    Success: Number of bytes copied
196  *    Failure: 0
197  */
198 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
199 {
200     LONG old_height, height;
201     XImage *image;
202     LPBYTE tbuf, startline;
203     int h, w;
204
205     TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
206
207     wine_tsx11_lock();
208
209     /* Hack: change the bitmap height temporarily to avoid */
210     /*       getting unnecessary bitmap rows. */
211
212     old_height = bmp->bitmap.bmHeight;
213     height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
214
215     image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
216                        0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
217                        AllPlanes, ZPixmap );
218     bmp->bitmap.bmHeight = old_height;
219
220     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
221
222     startline = buffer;
223     switch (bmp->bitmap.bmBitsPixel)
224     {
225     case 1:
226         for (h=0;h<height;h++)
227         {
228             tbuf = startline;
229             *tbuf = 0;
230             for (w=0;w<bmp->bitmap.bmWidth;w++)
231             {
232                 if ((w%8) == 0)
233                     *tbuf = 0;
234                 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
235                 if ((w&7) == 7) ++tbuf;
236             }
237             startline += bmp->bitmap.bmWidthBytes;
238         }
239         break;
240     case 4:
241         for (h=0;h<height;h++)
242         {
243             tbuf = startline;
244             for (w=0;w<bmp->bitmap.bmWidth;w++)
245             {
246                 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
247                 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
248             }
249             startline += bmp->bitmap.bmWidthBytes;
250         }
251         break;
252     case 8:
253         for (h=0;h<height;h++)
254         {
255             tbuf = startline;
256             for (w=0;w<bmp->bitmap.bmWidth;w++)
257                 *tbuf++ = XGetPixel(image,w,h);
258             startline += bmp->bitmap.bmWidthBytes;
259         }
260         break;
261     case 15:
262     case 16:
263         for (h=0;h<height;h++)
264         {
265             tbuf = startline;
266             for (w=0;w<bmp->bitmap.bmWidth;w++)
267             {
268                 long pixel = XGetPixel(image,w,h);
269
270                 *tbuf++ = pixel & 0xff;
271                 *tbuf++ = (pixel>>8) & 0xff;
272             }
273             startline += bmp->bitmap.bmWidthBytes;
274         }
275         break;
276     case 24:
277         for (h=0;h<height;h++)
278         {
279             tbuf = startline;
280             for (w=0;w<bmp->bitmap.bmWidth;w++)
281             {
282                 long pixel = XGetPixel(image,w,h);
283
284                 *tbuf++ = pixel & 0xff;
285                 *tbuf++ = (pixel>> 8) & 0xff;
286                 *tbuf++ = (pixel>>16) & 0xff;
287             }
288             startline += bmp->bitmap.bmWidthBytes;
289         }
290         break;
291
292     case 32:
293         for (h=0;h<height;h++)
294         {
295             tbuf = startline;
296             for (w=0;w<bmp->bitmap.bmWidth;w++)
297             {
298                 long pixel = XGetPixel(image,w,h);
299
300                 *tbuf++ = pixel & 0xff;
301                 *tbuf++ = (pixel>> 8) & 0xff;
302                 *tbuf++ = (pixel>>16) & 0xff;
303                 *tbuf++ = (pixel>>24) & 0xff;
304             }
305             startline += bmp->bitmap.bmWidthBytes;
306         }
307         break;
308     default:
309         FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
310     }
311     XDestroyImage( image );
312     wine_tsx11_unlock();
313     return count;
314 }
315
316
317
318 /******************************************************************************
319  *             X11DRV_SetBitmapBits
320  *
321  * RETURNS
322  *    Success: Number of bytes used in setting the bitmap bits
323  *    Failure: 0
324  */
325 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
326 {
327     LONG height;
328     XImage *image;
329     LPBYTE sbuf, startline;
330     int w, h;
331
332     TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
333     
334     height = count / bmp->bitmap.bmWidthBytes;
335
336     wine_tsx11_lock();
337     image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
338                           bmp->bitmap.bmWidth, height, 32, 0 );
339     if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
340     {
341         WARN("No memory to create image data.\n");
342         XDestroyImage( image );
343         wine_tsx11_unlock();
344         return 0;
345     }
346     
347     /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
348     
349     startline = bits;
350
351     switch (bmp->bitmap.bmBitsPixel)
352     {
353     case 1:
354         for (h=0;h<height;h++)
355         {
356             sbuf = startline;
357             for (w=0;w<bmp->bitmap.bmWidth;w++)
358             {
359                 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
360                 if ((w&7) == 7)
361                     sbuf++;
362             }
363             startline += bmp->bitmap.bmWidthBytes;
364         }
365         break;
366     case 4:
367         for (h=0;h<height;h++)
368         {
369             sbuf = startline;
370             for (w=0;w<bmp->bitmap.bmWidth;w++)
371             {
372                 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
373                 else XPutPixel( image, w, h, *sbuf++ & 0xf );
374             }
375             startline += bmp->bitmap.bmWidthBytes;
376         }
377         break;
378     case 8:
379         for (h=0;h<height;h++)
380         {
381             sbuf = startline;
382             for (w=0;w<bmp->bitmap.bmWidth;w++)
383                 XPutPixel(image,w,h,*sbuf++);
384             startline += bmp->bitmap.bmWidthBytes;
385         }
386         break;
387     case 15:
388     case 16:
389         for (h=0;h<height;h++)
390         {
391             sbuf = startline;
392             for (w=0;w<bmp->bitmap.bmWidth;w++)
393             {
394                 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
395                 sbuf+=2;
396             }
397             startline += bmp->bitmap.bmWidthBytes;
398         }
399         break;
400     case 24:
401         for (h=0;h<height;h++)
402         {
403             sbuf = startline;
404             for (w=0;w<bmp->bitmap.bmWidth;w++)
405             {
406                 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
407                 sbuf += 3;
408             }
409             startline += bmp->bitmap.bmWidthBytes;
410         }
411         break;
412     case 32: 
413         for (h=0;h<height;h++)
414         {
415             sbuf = startline;
416             for (w=0;w<bmp->bitmap.bmWidth;w++)
417             {
418                 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
419                 sbuf += 4;
420             }
421             startline += bmp->bitmap.bmWidthBytes;
422         }
423         break;
424     default:
425       FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
426
427     }
428     XPutImage( gdi_display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
429                image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
430     XDestroyImage( image ); /* frees image->data too */
431     wine_tsx11_unlock();
432     return count;
433 }
434
435 /***********************************************************************
436  *           X11DRV_BitmapBits
437  */
438 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
439 {
440     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
441     LONG ret;
442     if(!bmp) {
443         WARN("Bad bitmap handle %08x\n", hbitmap);
444         return FALSE;
445     }
446
447     if(flags == DDB_GET)
448         ret = X11DRV_GetBitmapBits(bmp, bits, count);
449     else if(flags == DDB_SET)
450         ret = X11DRV_SetBitmapBits(bmp, bits, count);
451     else {
452         ERR("Unknown flags value %d\n", flags);
453         ret = 0;
454     }
455     GDI_ReleaseObj( hbitmap );
456     return ret;
457 }
458
459 /***********************************************************************
460  *           X11DRV_BITMAP_DeleteObject
461  */
462 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap )
463 {
464     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
465     if (bmp)
466     {
467         TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
468         bmp->physBitmap = NULL;
469         bmp->funcs = NULL;
470         GDI_ReleaseObj( hbitmap );
471     }
472     return TRUE;
473 }
474
475 /**************************************************************************
476  *              X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
477  *
478  *  Allocates an HBITMAP which references the Pixmap passed in.
479  *  Note: This function makes the bitmap an owner of the Pixmap so subsequently
480  *  calling DeleteObject on this will free the Pixmap as well.
481  */
482 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
483 {
484     HBITMAP hBmp = 0;
485     BITMAPOBJ *pBmp = NULL;
486     Window root;
487     int x,y;               /* Unused */
488     unsigned border_width; /* Unused */
489     unsigned int depth, width, height;
490
491     /* Get the Pixmap dimensions and bit depth */
492     if ( 0 == TSXGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
493                              &border_width, &depth) )
494         goto END;
495
496     TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
497           width, height, depth);
498     
499     /*
500      * Create an HBITMAP with the same dimensions and BPP as the pixmap,
501      * and make it a container for the pixmap passed.
502      */
503     hBmp = CreateBitmap( width, height, 1, depth, NULL );
504
505     pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
506     
507     pBmp->funcs = X11DRV_DC_Funcs;
508     pBmp->physBitmap = (void *)pixmap;
509     GDI_ReleaseObj( hBmp );
510
511 END:
512     TRACE("\tReturning HBITMAP %x\n", hBmp);
513     return hBmp;
514 }
515
516
517 /**************************************************************************
518  *              X11DRV_BITMAP_CreateBitmapFromPixmap
519  *
520  *  Allocates an HBITMAP and copies the Pixmap data into it.
521  *  If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
522  */
523 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
524 {
525     HBITMAP hBmp = 0, hBmpCopy = 0;
526     BITMAPOBJ *pBmp = NULL;
527     unsigned int width, height;
528
529     /* Allocate an HBITMAP which references the Pixmap passed to us */
530     hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
531     if (!hBmp)
532     {
533         TRACE("\tCould not create bitmap header for Pixmap\n");
534         goto END;
535     }
536
537     /* Get the bitmap dimensions */
538     width = pBmp->bitmap.bmWidth;
539     height = pBmp->bitmap.bmHeight;
540                  
541     hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
542
543     /* We can now get rid of the HBITMAP wrapper we created earlier.
544      * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
545      */
546     if (!bDeletePixmap)
547     {
548         /* Manually clear the bitmap internals to prevent the Pixmap 
549          * from being deleted by DeleteObject.
550          */
551         pBmp->physBitmap = NULL;
552         pBmp->funcs = NULL;
553     }
554     DeleteObject(hBmp);  
555
556 END:
557     TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
558     return hBmpCopy;
559 }
560
561
562 /**************************************************************************
563  *                 X11DRV_BITMAP_CreatePixmapFromBitmap
564  *
565  *    Creates a Pixmap from a bitmap
566  */
567 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
568 {
569     HGLOBAL hPackedDIB = 0;
570     Pixmap pixmap = 0;
571
572     /*
573      * Create a packed DIB from the bitmap passed to us.
574      * A packed DIB contains a BITMAPINFO structure followed immediately by
575      * an optional color palette and the pixel data.
576      */
577     hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
578
579     /* Create a Pixmap from the packed DIB */
580     pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
581
582     /* Free the temporary packed DIB */
583     GlobalFree(hPackedDIB);
584
585     return pixmap;
586 }
587
588
589 /***********************************************************************
590  *           X11DRV_BITMAP_Pixmap
591  *
592  * This function exists solely for x11 driver of the window system.
593  */
594 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
595 {
596     Pixmap pixmap;
597     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
598     if (bmp) {
599       pixmap = (Pixmap)bmp->physBitmap;
600       GDI_ReleaseObj( hbitmap );
601     }
602     else {
603       ERR("handle %08x returned no obj\n", hbitmap);
604       pixmap = 0;
605     }
606     return pixmap;
607 }
608