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