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