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