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