Release 20000326.
[wine/gsoc-2012-control.git] / graphics / x11drv / bitmap.c
blob23ae16f72fd0db20faad88ba7387548c92c81674
1 /*
2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 * 1999 Noel Borthwick
6 */
8 #include "config.h"
10 #ifndef X_DISPLAY_MISSING
12 #include "ts_xlib.h"
13 #include "ts_xutil.h"
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include "gdi.h"
18 #include "callback.h"
19 #include "dc.h"
20 #include "bitmap.h"
21 #include "heap.h"
22 #include "debugtools.h"
23 #include "xmalloc.h"
24 #include "local.h"
25 #include "x11drv.h"
26 #include "wingdi.h"
27 #include "windef.h"
28 #include "wine/winuser16.h"
30 DEFAULT_DEBUG_CHANNEL(x11drv);
32 /* GCs used for B&W and color bitmap operations */
33 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
36 /***********************************************************************
37 * X11DRV_BITMAP_Init
39 BOOL X11DRV_BITMAP_Init(void)
41 Pixmap tmpPixmap;
43 /* Create the necessary GCs */
45 if ((tmpPixmap = TSXCreatePixmap(display,
46 X11DRV_GetXRootWindow(),
47 1, 1,
48 1)))
50 BITMAP_monoGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
51 TSXSetGraphicsExposures( display, BITMAP_monoGC, False );
52 TSXFreePixmap( display, tmpPixmap );
55 if (X11DRV_GetDepth() != 1)
57 if ((tmpPixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
58 1, 1, X11DRV_GetDepth())))
60 BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
61 TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
62 TSXFreePixmap( display, tmpPixmap );
65 return TRUE;
68 /***********************************************************************
69 * X11DRV_BITMAP_SelectObject
71 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
72 BITMAPOBJ * bmp )
74 HRGN hrgn;
75 HBITMAP prevHandle = dc->w.hBitmap;
76 X11DRV_PHYSBITMAP *pbitmap;
77 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
80 if (!(dc->w.flags & DC_MEMORY)) return 0;
82 if(!bmp->DDBitmap)
83 if(!X11DRV_CreateBitmap(hbitmap))
84 return 0;
86 if(bmp->DDBitmap->funcs != dc->funcs) {
87 WARN("Trying to select non-X11 DDB into an X11 dc\n");
88 return 0;
91 pbitmap = bmp->DDBitmap->physBitmap;
93 dc->w.totalExtent.left = 0;
94 dc->w.totalExtent.top = 0;
95 dc->w.totalExtent.right = bmp->bitmap.bmWidth;
96 dc->w.totalExtent.bottom = bmp->bitmap.bmHeight;
98 if (dc->w.hVisRgn)
99 SetRectRgn( dc->w.hVisRgn, 0, 0,
100 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
101 else
103 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
104 if (!hrgn) return 0;
105 dc->w.hVisRgn = hrgn;
108 physDev->drawable = pbitmap->pixmap;
109 dc->w.hBitmap = hbitmap;
111 /* Change GC depth if needed */
113 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
115 TSXFreeGC( display, physDev->gc );
116 physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
117 TSXSetGraphicsExposures( display, physDev->gc, False );
118 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
119 DC_InitDC( dc );
121 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
122 return prevHandle;
126 /***********************************************************************
127 * XPutImage_wrapper
129 * Wrapper to call XPutImage with CALL_LARGE_STACK.
132 struct XPutImage_descr
134 BITMAPOBJ *bmp;
135 XImage *image;
136 INT width;
137 INT height;
140 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
142 return XPutImage( display,
143 ((X11DRV_PHYSBITMAP *)descr->bmp->DDBitmap->physBitmap)->pixmap,
144 BITMAP_GC(descr->bmp),
145 descr->image, 0, 0, 0, 0, descr->width, descr->height );
149 /***************************************************************************
151 * X11DRV_AllocBitmap
153 * Allocate DDBitmap and physBitmap
156 X11DRV_PHYSBITMAP *X11DRV_AllocBitmap( BITMAPOBJ *bmp )
158 X11DRV_PHYSBITMAP *pbitmap;
160 if(!(bmp->DDBitmap = HeapAlloc(GetProcessHeap(), 0, sizeof(DDBITMAP)))) {
161 WARN("Can't alloc DDBITMAP\n");
162 return NULL;
165 if(!(pbitmap = HeapAlloc(GetProcessHeap(), 0,sizeof(X11DRV_PHYSBITMAP)))) {
166 WARN("Can't alloc X11DRV_PHYSBITMAP\n");
167 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
168 return NULL;
171 bmp->DDBitmap->physBitmap = pbitmap;
172 bmp->DDBitmap->funcs = DRIVER_FindDriver( "DISPLAY" );
173 return pbitmap;
177 /****************************************************************************
179 * X11DRV_CreateBitmap
181 * Create a device dependent X11 bitmap
183 * Returns TRUE on success else FALSE
187 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
189 X11DRV_PHYSBITMAP *pbitmap;
190 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
192 if(!bmp) {
193 WARN("Bad bitmap handle %08x\n", hbitmap);
194 return FALSE;
197 /* Check parameters */
198 if (bmp->bitmap.bmPlanes != 1) return 0;
199 if ((bmp->bitmap.bmBitsPixel != 1) &&
200 (bmp->bitmap.bmBitsPixel != X11DRV_GetDepth()))
202 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
203 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
204 GDI_HEAP_UNLOCK( hbitmap );
205 return FALSE;
208 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
209 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
211 pbitmap = X11DRV_AllocBitmap( bmp );
212 if(!pbitmap) return FALSE;
214 /* Create the pixmap */
215 pbitmap->pixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(), bmp->bitmap.bmWidth,
216 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
217 if (!pbitmap->pixmap) {
218 WARN("Can't create Pixmap\n");
219 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap->physBitmap);
220 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
221 GDI_HEAP_UNLOCK( hbitmap );
222 return FALSE;
225 if (bmp->bitmap.bmBits) /* Set bitmap bits */
226 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
227 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
228 DDB_SET );
230 GDI_HEAP_UNLOCK( hbitmap );
231 return TRUE;
235 /***********************************************************************
236 * X11DRV_BITMAP_GetXImage
238 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
240 XImage *X11DRV_BITMAP_GetXImage( const BITMAPOBJ *bmp )
242 return XGetImage( display,
243 ((X11DRV_PHYSBITMAP *)bmp->DDBitmap->physBitmap)->pixmap,
244 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
245 AllPlanes, ZPixmap );
249 /***********************************************************************
250 * X11DRV_GetBitmapBits
252 * RETURNS
253 * Success: Number of bytes copied
254 * Failure: 0
256 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
258 LONG old_height, height;
259 XImage *image;
260 LPBYTE tbuf, startline;
261 int h, w;
263 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
265 EnterCriticalSection( &X11DRV_CritSection );
267 /* Hack: change the bitmap height temporarily to avoid */
268 /* getting unnecessary bitmap rows. */
270 old_height = bmp->bitmap.bmHeight;
271 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
273 image = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
275 bmp->bitmap.bmHeight = old_height;
277 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
279 startline = buffer;
280 switch (bmp->bitmap.bmBitsPixel)
282 case 1:
283 for (h=0;h<height;h++)
285 tbuf = startline;
286 *tbuf = 0;
287 for (w=0;w<bmp->bitmap.bmWidth;w++)
289 if ((w%8) == 0)
290 *tbuf = 0;
291 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
292 if ((w&7) == 7) ++tbuf;
294 startline += bmp->bitmap.bmWidthBytes;
296 break;
297 case 4:
298 for (h=0;h<height;h++)
300 tbuf = startline;
301 for (w=0;w<bmp->bitmap.bmWidth;w++)
303 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
304 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
306 startline += bmp->bitmap.bmWidthBytes;
308 break;
309 case 8:
310 for (h=0;h<height;h++)
312 tbuf = startline;
313 for (w=0;w<bmp->bitmap.bmWidth;w++)
314 *tbuf++ = XGetPixel(image,w,h);
315 startline += bmp->bitmap.bmWidthBytes;
317 break;
318 case 15:
319 case 16:
320 for (h=0;h<height;h++)
322 tbuf = startline;
323 for (w=0;w<bmp->bitmap.bmWidth;w++)
325 long pixel = XGetPixel(image,w,h);
327 *tbuf++ = pixel & 0xff;
328 *tbuf++ = (pixel>>8) & 0xff;
330 startline += bmp->bitmap.bmWidthBytes;
332 break;
333 case 24:
334 for (h=0;h<height;h++)
336 tbuf = startline;
337 for (w=0;w<bmp->bitmap.bmWidth;w++)
339 long pixel = XGetPixel(image,w,h);
341 *tbuf++ = pixel & 0xff;
342 *tbuf++ = (pixel>> 8) & 0xff;
343 *tbuf++ = (pixel>>16) & 0xff;
345 startline += bmp->bitmap.bmWidthBytes;
347 break;
349 case 32:
350 for (h=0;h<height;h++)
352 tbuf = startline;
353 for (w=0;w<bmp->bitmap.bmWidth;w++)
355 long pixel = XGetPixel(image,w,h);
357 *tbuf++ = pixel & 0xff;
358 *tbuf++ = (pixel>> 8) & 0xff;
359 *tbuf++ = (pixel>>16) & 0xff;
360 *tbuf++ = (pixel>>24) & 0xff;
362 startline += bmp->bitmap.bmWidthBytes;
364 break;
365 default:
366 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
368 XDestroyImage( image );
369 LeaveCriticalSection( &X11DRV_CritSection );
371 return count;
376 /******************************************************************************
377 * X11DRV_SetBitmapBits
379 * RETURNS
380 * Success: Number of bytes used in setting the bitmap bits
381 * Failure: 0
383 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
385 struct XPutImage_descr descr;
386 LONG height;
387 XImage *image;
388 LPBYTE sbuf, startline;
389 int w, h;
391 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
393 height = count / bmp->bitmap.bmWidthBytes;
395 EnterCriticalSection( &X11DRV_CritSection );
396 image = XCreateImage( display, X11DRV_GetVisual(), bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
397 bmp->bitmap.bmWidth, height, 32, 0 );
398 image->data = (LPBYTE)xmalloc(image->bytes_per_line * height);
400 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
402 startline = bits;
404 switch (bmp->bitmap.bmBitsPixel)
406 case 1:
407 for (h=0;h<height;h++)
409 sbuf = startline;
410 for (w=0;w<bmp->bitmap.bmWidth;w++)
412 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
413 if ((w&7) == 7)
414 sbuf++;
416 startline += bmp->bitmap.bmWidthBytes;
418 break;
419 case 4:
420 for (h=0;h<height;h++)
422 sbuf = startline;
423 for (w=0;w<bmp->bitmap.bmWidth;w++)
425 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
426 else XPutPixel( image, w, h, *sbuf++ & 0xf );
428 startline += bmp->bitmap.bmWidthBytes;
430 break;
431 case 8:
432 for (h=0;h<height;h++)
434 sbuf = startline;
435 for (w=0;w<bmp->bitmap.bmWidth;w++)
436 XPutPixel(image,w,h,*sbuf++);
437 startline += bmp->bitmap.bmWidthBytes;
439 break;
440 case 15:
441 case 16:
442 for (h=0;h<height;h++)
444 sbuf = startline;
445 for (w=0;w<bmp->bitmap.bmWidth;w++)
447 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
448 sbuf+=2;
450 startline += bmp->bitmap.bmWidthBytes;
452 break;
453 case 24:
454 for (h=0;h<height;h++)
456 sbuf = startline;
457 for (w=0;w<bmp->bitmap.bmWidth;w++)
459 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
460 sbuf += 3;
462 startline += bmp->bitmap.bmWidthBytes;
464 break;
465 case 32:
466 for (h=0;h<height;h++)
468 sbuf = startline;
469 for (w=0;w<bmp->bitmap.bmWidth;w++)
471 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
472 sbuf += 4;
474 startline += bmp->bitmap.bmWidthBytes;
476 break;
477 default:
478 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
482 descr.bmp = bmp;
483 descr.image = image;
484 descr.width = bmp->bitmap.bmWidth;
485 descr.height = height;
487 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
488 XDestroyImage( image ); /* frees image->data too */
489 LeaveCriticalSection( &X11DRV_CritSection );
491 return count;
494 /***********************************************************************
495 * X11DRV_BitmapBits
497 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
499 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
500 LONG ret;
501 if(!bmp) {
502 WARN("Bad bitmap handle %08x\n", hbitmap);
503 return FALSE;
506 if(flags == DDB_GET)
507 ret = X11DRV_GetBitmapBits(bmp, bits, count);
508 else if(flags == DDB_SET)
509 ret = X11DRV_SetBitmapBits(bmp, bits, count);
510 else {
511 ERR("Unknown flags value %d\n", flags);
512 ret = 0;
515 GDI_HEAP_UNLOCK( hbitmap );
516 return ret;
519 /***********************************************************************
520 * X11DRV_BITMAP_DeleteObject
522 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
524 X11DRV_PHYSBITMAP *pbitmap = bmp->DDBitmap->physBitmap;
526 TSXFreePixmap( display, pbitmap->pixmap );
528 HeapFree( GetProcessHeap(), 0, bmp->DDBitmap->physBitmap );
529 HeapFree( GetProcessHeap(), 0, bmp->DDBitmap );
530 bmp->DDBitmap = NULL;
532 return TRUE;
535 /**************************************************************************
536 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
538 * Allocates an HBITMAP which references the Pixmap passed in.
539 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
540 * calling DeleteObject on this will free the Pixmap as well.
542 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
544 HBITMAP hBmp = 0;
545 BITMAPOBJ *pBmp = NULL;
546 X11DRV_PHYSBITMAP *pPhysBmp = NULL;
547 Window root;
548 int x,y; /* Unused */
549 unsigned border_width; /* Unused */
550 unsigned int depth, width, height;
552 /* Get the Pixmap dimensions and bit depth */
553 if ( 0 == TSXGetGeometry(display, pixmap, &root, &x, &y, &width, &height,
554 &border_width, &depth) )
555 goto END;
557 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
558 width, height, depth);
561 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
562 * and make it a container for the pixmap passed.
564 hBmp = CreateBitmap( width, height, 1, depth, NULL );
566 /* Allocate DDBitmap and physBitmap structures in BITMAPOBJ.
567 * The hBmp is just a filled in BITMAPOBJ header at this point.
569 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
570 pPhysBmp = X11DRV_AllocBitmap( pBmp );
571 if( !pPhysBmp )
573 DeleteObject(hBmp);
574 hBmp = NULL;
575 goto END;
578 /* Point to our Pixmap in the physical bitmap structure */
579 pPhysBmp->pixmap = pixmap;
581 END:
582 TRACE("\tReturning HBITMAP %x\n", hBmp);
583 return hBmp;
587 /**************************************************************************
588 * X11DRV_BITMAP_CreateBitmapFromPixmap
590 * Allocates an HBITMAP and copies the Pixmap data into it.
591 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
593 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
595 HBITMAP hBmp = 0, hBmpCopy = 0;
596 BITMAPOBJ *pBmp = NULL;
597 unsigned int width, height;
599 /* Allocate an HBITMAP which references the Pixmap passed to us */
600 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
601 if (!hBmp)
603 TRACE("\tCould not create bitmap header for Pixmap\n");
604 goto END;
607 /* Get the bitmap dimensions */
608 width = pBmp->bitmap.bmWidth;
609 height = pBmp->bitmap.bmHeight;
611 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
613 /* We can now get rid of the HBITMAP wrapper we created earlier.
614 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
616 if (!bDeletePixmap)
618 /* Manually free the DDBitmap internals to prevent the Pixmap
619 * from being deleted by DeleteObject.
621 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
622 HeapFree( GetProcessHeap(), 0, pBmp->DDBitmap->physBitmap );
623 HeapFree( GetProcessHeap(), 0, pBmp->DDBitmap );
624 pBmp->DDBitmap = NULL;
626 DeleteObject(hBmp);
628 END:
629 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
630 return hBmpCopy;
634 /**************************************************************************
635 * X11DRV_BITMAP_CreatePixmapFromBitmap
637 * Creates a Pixmap from a bitmap
639 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
641 HGLOBAL hPackedDIB = NULL;
642 Pixmap pixmap = NULL;
645 * Create a packed DIB from the bitmap passed to us.
646 * A packed DIB contains a BITMAPINFO structure followed immediately by
647 * an optional color palette and the pixel data.
649 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
651 /* Create a Pixmap from the packed DIB */
652 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
654 /* Free the temporary packed DIB */
655 GlobalFree(hPackedDIB);
657 return pixmap;
661 /***********************************************************************
662 * X11DRV_BITMAP_Pixmap
664 * This function exists solely for x11 driver of the window system.
666 BOOL X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
668 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
669 return ((X11DRV_PHYSBITMAP *)(bmp->DDBitmap->physBitmap))->pixmap;
672 #endif /* !defined(X_DISPLAY_MISSING) */