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