2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
10 #ifndef X_DISPLAY_MISSING
23 #include "debugtools.h"
29 #include "wine/winuser16.h"
31 DEFAULT_DEBUG_CHANNEL(x11drv)
33 /* GCs used for B&W and color bitmap operations */
34 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
37 /***********************************************************************
40 BOOL X11DRV_BITMAP_Init(void)
44 /* Create the necessary GCs */
46 if ((tmpPixmap = TSXCreatePixmap(display,
47 X11DRV_GetXRootWindow(),
51 BITMAP_monoGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
52 TSXSetGraphicsExposures( display, BITMAP_monoGC, False );
53 TSXFreePixmap( display, tmpPixmap );
56 if (MONITOR_GetDepth(&MONITOR_PrimaryMonitor) != 1)
58 if ((tmpPixmap = TSXCreatePixmap(display,
59 X11DRV_GetXRootWindow(),
61 MONITOR_GetDepth(&MONITOR_PrimaryMonitor))))
63 BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
64 TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
65 TSXFreePixmap( display, tmpPixmap );
71 /***********************************************************************
72 * X11DRV_BITMAP_SelectObject
74 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
78 HBITMAP prevHandle = dc->w.hBitmap;
79 X11DRV_PHYSBITMAP *pbitmap;
80 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
83 if (!(dc->w.flags & DC_MEMORY)) return 0;
86 if(!X11DRV_CreateBitmap(hbitmap))
89 if(bmp->DDBitmap->funcs != dc->funcs) {
90 WARN("Trying to select non-X11 DDB into an X11 dc\n");
94 pbitmap = bmp->DDBitmap->physBitmap;
96 dc->w.totalExtent.left = 0;
97 dc->w.totalExtent.top = 0;
98 dc->w.totalExtent.right = bmp->bitmap.bmWidth;
99 dc->w.totalExtent.bottom = bmp->bitmap.bmHeight;
102 SetRectRgn( dc->w.hVisRgn, 0, 0,
103 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
106 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
108 dc->w.hVisRgn = hrgn;
111 physDev->drawable = pbitmap->pixmap;
112 dc->w.hBitmap = hbitmap;
114 /* Change GC depth if needed */
116 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
118 TSXFreeGC( display, physDev->gc );
119 physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
120 TSXSetGraphicsExposures( display, physDev->gc, False );
121 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
124 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
129 /***********************************************************************
132 * Wrapper to call XPutImage with CALL_LARGE_STACK.
135 struct XPutImage_descr
143 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
145 return XPutImage( display,
146 ((X11DRV_PHYSBITMAP *)descr->bmp->DDBitmap->physBitmap)->pixmap,
147 BITMAP_GC(descr->bmp),
148 descr->image, 0, 0, 0, 0, descr->width, descr->height );
152 /***************************************************************************
156 * Allocate DDBitmap and physBitmap
159 X11DRV_PHYSBITMAP *X11DRV_AllocBitmap( BITMAPOBJ *bmp )
161 X11DRV_PHYSBITMAP *pbitmap;
163 if(!(bmp->DDBitmap = HeapAlloc(GetProcessHeap(), 0, sizeof(DDBITMAP)))) {
164 WARN("Can't alloc DDBITMAP\n");
168 if(!(pbitmap = HeapAlloc(GetProcessHeap(), 0,sizeof(X11DRV_PHYSBITMAP)))) {
169 WARN("Can't alloc X11DRV_PHYSBITMAP\n");
170 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
174 bmp->DDBitmap->physBitmap = pbitmap;
175 bmp->DDBitmap->funcs = DRIVER_FindDriver( "DISPLAY" );
180 /****************************************************************************
182 * X11DRV_CreateBitmap
184 * Create a device dependent X11 bitmap
186 * Returns TRUE on success else FALSE
190 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
192 X11DRV_PHYSBITMAP *pbitmap;
193 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
196 WARN("Bad bitmap handle %08x\n", hbitmap);
200 /* Check parameters */
201 if (bmp->bitmap.bmPlanes != 1) return 0;
202 if ((bmp->bitmap.bmBitsPixel != 1) &&
203 (bmp->bitmap.bmBitsPixel != MONITOR_GetDepth(&MONITOR_PrimaryMonitor))) {
204 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
205 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
206 GDI_HEAP_UNLOCK( hbitmap );
210 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
211 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
213 pbitmap = X11DRV_AllocBitmap( bmp );
214 if(!pbitmap) return FALSE;
216 /* Create the pixmap */
217 pbitmap->pixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(), bmp->bitmap.bmWidth,
218 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
219 if (!pbitmap->pixmap) {
220 WARN("Can't create Pixmap\n");
221 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap->physBitmap);
222 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
223 GDI_HEAP_UNLOCK( hbitmap );
227 if (bmp->bitmap.bmBits) /* Set bitmap bits */
228 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
229 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
232 GDI_HEAP_UNLOCK( hbitmap );
237 /***********************************************************************
238 * X11DRV_BITMAP_GetXImage
240 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
242 XImage *X11DRV_BITMAP_GetXImage( const BITMAPOBJ *bmp )
244 return XGetImage( display,
245 ((X11DRV_PHYSBITMAP *)bmp->DDBitmap->physBitmap)->pixmap,
246 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
247 AllPlanes, ZPixmap );
251 /***********************************************************************
252 * X11DRV_GetBitmapBits
255 * Success: Number of bytes copied
258 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
260 LONG old_height, height;
262 LPBYTE tbuf, startline;
265 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
267 EnterCriticalSection( &X11DRV_CritSection );
269 /* Hack: change the bitmap height temporarily to avoid */
270 /* getting unnecessary bitmap rows. */
272 old_height = bmp->bitmap.bmHeight;
273 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
275 image = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
277 bmp->bitmap.bmHeight = old_height;
279 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
282 switch (bmp->bitmap.bmBitsPixel)
285 for (h=0;h<height;h++)
289 for (w=0;w<bmp->bitmap.bmWidth;w++)
293 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
294 if ((w&7) == 7) ++tbuf;
296 startline += bmp->bitmap.bmWidthBytes;
300 for (h=0;h<height;h++)
303 for (w=0;w<bmp->bitmap.bmWidth;w++)
305 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
306 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
308 startline += bmp->bitmap.bmWidthBytes;
312 for (h=0;h<height;h++)
315 for (w=0;w<bmp->bitmap.bmWidth;w++)
316 *tbuf++ = XGetPixel(image,w,h);
317 startline += bmp->bitmap.bmWidthBytes;
322 for (h=0;h<height;h++)
325 for (w=0;w<bmp->bitmap.bmWidth;w++)
327 long pixel = XGetPixel(image,w,h);
329 *tbuf++ = pixel & 0xff;
330 *tbuf++ = (pixel>>8) & 0xff;
332 startline += bmp->bitmap.bmWidthBytes;
336 for (h=0;h<height;h++)
339 for (w=0;w<bmp->bitmap.bmWidth;w++)
341 long pixel = XGetPixel(image,w,h);
343 *tbuf++ = pixel & 0xff;
344 *tbuf++ = (pixel>> 8) & 0xff;
345 *tbuf++ = (pixel>>16) & 0xff;
347 startline += bmp->bitmap.bmWidthBytes;
352 for (h=0;h<height;h++)
355 for (w=0;w<bmp->bitmap.bmWidth;w++)
357 long pixel = XGetPixel(image,w,h);
359 *tbuf++ = pixel & 0xff;
360 *tbuf++ = (pixel>> 8) & 0xff;
361 *tbuf++ = (pixel>>16) & 0xff;
362 *tbuf++ = (pixel>>24) & 0xff;
364 startline += bmp->bitmap.bmWidthBytes;
368 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
370 XDestroyImage( image );
371 LeaveCriticalSection( &X11DRV_CritSection );
378 /******************************************************************************
379 * X11DRV_SetBitmapBits
382 * Success: Number of bytes used in setting the bitmap bits
385 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
387 struct XPutImage_descr descr;
390 LPBYTE sbuf, startline;
393 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
395 height = count / bmp->bitmap.bmWidthBytes;
397 EnterCriticalSection( &X11DRV_CritSection );
398 image = XCreateImage( display, DefaultVisualOfScreen(X11DRV_GetXScreen()),
399 bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
400 bmp->bitmap.bmWidth, height, 32, 0 );
401 image->data = (LPBYTE)xmalloc(image->bytes_per_line * height);
403 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
407 switch (bmp->bitmap.bmBitsPixel)
410 for (h=0;h<height;h++)
413 for (w=0;w<bmp->bitmap.bmWidth;w++)
415 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
419 startline += bmp->bitmap.bmWidthBytes;
423 for (h=0;h<height;h++)
426 for (w=0;w<bmp->bitmap.bmWidth;w++)
428 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
429 else XPutPixel( image, w, h, *sbuf++ & 0xf );
431 startline += bmp->bitmap.bmWidthBytes;
435 for (h=0;h<height;h++)
438 for (w=0;w<bmp->bitmap.bmWidth;w++)
439 XPutPixel(image,w,h,*sbuf++);
440 startline += bmp->bitmap.bmWidthBytes;
445 for (h=0;h<height;h++)
448 for (w=0;w<bmp->bitmap.bmWidth;w++)
450 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
453 startline += bmp->bitmap.bmWidthBytes;
457 for (h=0;h<height;h++)
460 for (w=0;w<bmp->bitmap.bmWidth;w++)
462 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
465 startline += bmp->bitmap.bmWidthBytes;
469 for (h=0;h<height;h++)
472 for (w=0;w<bmp->bitmap.bmWidth;w++)
474 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
477 startline += bmp->bitmap.bmWidthBytes;
481 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
487 descr.width = bmp->bitmap.bmWidth;
488 descr.height = height;
490 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
491 XDestroyImage( image ); /* frees image->data too */
492 LeaveCriticalSection( &X11DRV_CritSection );
497 /***********************************************************************
500 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
502 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
505 WARN("Bad bitmap handle %08x\n", hbitmap);
510 ret = X11DRV_GetBitmapBits(bmp, bits, count);
511 else if(flags == DDB_SET)
512 ret = X11DRV_SetBitmapBits(bmp, bits, count);
514 ERR("Unknown flags value %d\n", flags);
518 GDI_HEAP_UNLOCK( hbitmap );
522 /***********************************************************************
523 * X11DRV_BITMAP_DeleteObject
525 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
527 X11DRV_PHYSBITMAP *pbitmap = bmp->DDBitmap->physBitmap;
529 TSXFreePixmap( display, pbitmap->pixmap );
531 HeapFree( GetProcessHeap(), 0, bmp->DDBitmap->physBitmap );
532 HeapFree( GetProcessHeap(), 0, bmp->DDBitmap );
533 bmp->DDBitmap = NULL;
538 /**************************************************************************
539 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
541 * Allocates an HBITMAP which references the Pixmap passed in.
542 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
543 * calling DeleteObject on this will free the Pixmap as well.
545 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
548 BITMAPOBJ *pBmp = NULL;
549 X11DRV_PHYSBITMAP *pPhysBmp = NULL;
551 int x,y; /* Unused */
552 unsigned border_width; /* Unused */
553 unsigned int depth, width, height;
555 /* Get the Pixmap dimensions and bit depth */
556 if ( 0 == TSXGetGeometry(display, pixmap, &root, &x, &y, &width, &height,
557 &border_width, &depth) )
560 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
561 width, height, depth);
564 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
565 * and make it a container for the pixmap passed.
567 hBmp = CreateBitmap( width, height, 1, depth, NULL );
569 /* Allocate DDBitmap and physBitmap structures in BITMAPOBJ.
570 * The hBmp is just a filled in BITMAPOBJ header at this point.
572 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
573 pPhysBmp = X11DRV_AllocBitmap( pBmp );
581 /* Point to our Pixmap in the physical bitmap structure */
582 pPhysBmp->pixmap = pixmap;
585 TRACE("\tReturning HBITMAP %x\n", hBmp);
590 /**************************************************************************
591 * X11DRV_BITMAP_CreateBitmapFromPixmap
593 * Allocates an HBITMAP and copies the Pixmap data into it.
594 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
596 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
598 HBITMAP hBmp = 0, hBmpCopy = 0;
599 BITMAPOBJ *pBmp = NULL;
600 unsigned int width, height;
602 /* Allocate an HBITMAP which references the Pixmap passed to us */
603 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
606 TRACE("\tCould not create bitmap header for Pixmap\n");
610 /* Get the bitmap dimensions */
611 width = pBmp->bitmap.bmWidth;
612 height = pBmp->bitmap.bmHeight;
614 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
616 /* We can now get rid of the HBITMAP wrapper we created earlier.
617 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
621 /* Manually free the DDBitmap internals to prevent the Pixmap
622 * from being deleted by DeleteObject.
624 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
625 HeapFree( GetProcessHeap(), 0, pBmp->DDBitmap->physBitmap );
626 HeapFree( GetProcessHeap(), 0, pBmp->DDBitmap );
627 pBmp->DDBitmap = NULL;
632 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
637 /**************************************************************************
638 * X11DRV_BITMAP_CreatePixmapFromBitmap
640 * Creates a Pixmap from a bitmap
642 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
644 HGLOBAL hPackedDIB = NULL;
645 Pixmap pixmap = NULL;
648 * Create a packed DIB from the bitmap passed to us.
649 * A packed DIB contains a BITMAPINFO structure followed immediately by
650 * an optional color palette and the pixel data.
652 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
654 /* Create a Pixmap from the packed DIB */
655 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
657 /* Free the temporary packed DIB */
658 GlobalFree(hPackedDIB);
664 /***********************************************************************
665 * X11DRV_BITMAP_Pixmap
667 * This function exists solely for x11 driver of the window system.
669 BOOL X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
671 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
672 return ((X11DRV_PHYSBITMAP *)(bmp->DDBitmap->physBitmap))->pixmap;
675 #endif /* !defined(X_DISPLAY_MISSING) */