2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
10 #ifndef X_DISPLAY_MISSING
23 #include "debugtools.h"
27 #include "wine/winuser16.h"
29 DEFAULT_DEBUG_CHANNEL(x11drv)
31 /* GCs used for B&W and color bitmap operations */
32 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
35 /***********************************************************************
38 BOOL X11DRV_BITMAP_Init(void)
42 /* Create the necessary GCs */
44 if ((tmpPixmap = TSXCreatePixmap(display,
45 X11DRV_GetXRootWindow(),
49 BITMAP_monoGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
50 TSXSetGraphicsExposures( display, BITMAP_monoGC, False );
51 TSXFreePixmap( display, tmpPixmap );
54 if (MONITOR_GetDepth(&MONITOR_PrimaryMonitor) != 1)
56 if ((tmpPixmap = TSXCreatePixmap(display,
57 X11DRV_GetXRootWindow(),
59 MONITOR_GetDepth(&MONITOR_PrimaryMonitor))))
61 BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
62 TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
63 TSXFreePixmap( display, tmpPixmap );
69 /***********************************************************************
70 * X11DRV_BITMAP_SelectObject
72 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
76 HBITMAP prevHandle = dc->w.hBitmap;
77 X11DRV_PHYSBITMAP *pbitmap;
78 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
81 if (!(dc->w.flags & DC_MEMORY)) return 0;
84 if(!X11DRV_CreateBitmap(hbitmap))
87 if(bmp->DDBitmap->funcs != dc->funcs) {
88 WARN("Trying to select non-X11 DDB into an X11 dc\n");
92 pbitmap = bmp->DDBitmap->physBitmap;
94 dc->w.totalExtent.left = 0;
95 dc->w.totalExtent.top = 0;
96 dc->w.totalExtent.right = bmp->bitmap.bmWidth;
97 dc->w.totalExtent.bottom = bmp->bitmap.bmHeight;
100 SetRectRgn( dc->w.hVisRgn, 0, 0,
101 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
104 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
106 dc->w.hVisRgn = hrgn;
109 physDev->drawable = pbitmap->pixmap;
110 dc->w.hBitmap = hbitmap;
112 /* Change GC depth if needed */
114 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
116 TSXFreeGC( display, physDev->gc );
117 physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
118 TSXSetGraphicsExposures( display, physDev->gc, False );
119 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
122 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
127 /***********************************************************************
130 * Wrapper to call XPutImage with CALL_LARGE_STACK.
133 struct XPutImage_descr
141 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
143 return XPutImage( display,
144 ((X11DRV_PHYSBITMAP *)descr->bmp->DDBitmap->physBitmap)->pixmap,
145 BITMAP_GC(descr->bmp),
146 descr->image, 0, 0, 0, 0, descr->width, descr->height );
150 /***************************************************************************
154 * Allocate DDBitmap and physBitmap
157 X11DRV_PHYSBITMAP *X11DRV_AllocBitmap( BITMAPOBJ *bmp )
159 X11DRV_PHYSBITMAP *pbitmap;
161 if(!(bmp->DDBitmap = HeapAlloc(GetProcessHeap(), 0, sizeof(DDBITMAP)))) {
162 WARN("Can't alloc DDBITMAP\n");
166 if(!(pbitmap = HeapAlloc(GetProcessHeap(), 0,sizeof(X11DRV_PHYSBITMAP)))) {
167 WARN("Can't alloc X11DRV_PHYSBITMAP\n");
168 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
172 bmp->DDBitmap->physBitmap = pbitmap;
173 bmp->DDBitmap->funcs = DRIVER_FindDriver( "DISPLAY" );
178 /****************************************************************************
180 * X11DRV_CreateBitmap
182 * Create a device dependent X11 bitmap
184 * Returns TRUE on success else FALSE
188 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
190 X11DRV_PHYSBITMAP *pbitmap;
191 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
194 WARN("Bad bitmap handle %08x\n", hbitmap);
198 /* Check parameters */
199 if (bmp->bitmap.bmPlanes != 1) return 0;
200 if ((bmp->bitmap.bmBitsPixel != 1) &&
201 (bmp->bitmap.bmBitsPixel != MONITOR_GetDepth(&MONITOR_PrimaryMonitor))) {
202 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
203 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
204 GDI_HEAP_UNLOCK( hbitmap );
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 );
225 if (bmp->bitmap.bmBits) /* Set bitmap bits */
226 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
227 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
230 GDI_HEAP_UNLOCK( hbitmap );
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
253 * Success: Number of bytes copied
256 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
258 LONG old_height, height;
260 LPBYTE tbuf, startline;
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 */
280 switch (bmp->bitmap.bmBitsPixel)
283 for (h=0;h<height;h++)
287 for (w=0;w<bmp->bitmap.bmWidth;w++)
291 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
292 if ((w&7) == 7) ++tbuf;
294 startline += bmp->bitmap.bmWidthBytes;
298 for (h=0;h<height;h++)
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;
310 for (h=0;h<height;h++)
313 for (w=0;w<bmp->bitmap.bmWidth;w++)
314 *tbuf++ = XGetPixel(image,w,h);
315 startline += bmp->bitmap.bmWidthBytes;
320 for (h=0;h<height;h++)
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;
334 for (h=0;h<height;h++)
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;
350 for (h=0;h<height;h++)
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;
366 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
368 XDestroyImage( image );
369 LeaveCriticalSection( &X11DRV_CritSection );
376 /******************************************************************************
377 * X11DRV_SetBitmapBits
380 * Success: Number of bytes used in setting the bitmap bits
383 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
385 struct XPutImage_descr descr;
388 LPBYTE sbuf, startline;
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, DefaultVisualOfScreen(X11DRV_GetXScreen()),
397 bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
398 bmp->bitmap.bmWidth, height, 32, 0 );
399 image->data = (LPBYTE)xmalloc(image->bytes_per_line * height);
401 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
405 switch (bmp->bitmap.bmBitsPixel)
408 for (h=0;h<height;h++)
411 for (w=0;w<bmp->bitmap.bmWidth;w++)
413 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
417 startline += bmp->bitmap.bmWidthBytes;
421 for (h=0;h<height;h++)
424 for (w=0;w<bmp->bitmap.bmWidth;w++)
426 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
427 else XPutPixel( image, w, h, *sbuf++ & 0xf );
429 startline += bmp->bitmap.bmWidthBytes;
433 for (h=0;h<height;h++)
436 for (w=0;w<bmp->bitmap.bmWidth;w++)
437 XPutPixel(image,w,h,*sbuf++);
438 startline += bmp->bitmap.bmWidthBytes;
443 for (h=0;h<height;h++)
446 for (w=0;w<bmp->bitmap.bmWidth;w++)
448 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
451 startline += bmp->bitmap.bmWidthBytes;
455 for (h=0;h<height;h++)
458 for (w=0;w<bmp->bitmap.bmWidth;w++)
460 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
463 startline += bmp->bitmap.bmWidthBytes;
467 for (h=0;h<height;h++)
470 for (w=0;w<bmp->bitmap.bmWidth;w++)
472 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
475 startline += bmp->bitmap.bmWidthBytes;
479 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
485 descr.width = bmp->bitmap.bmWidth;
486 descr.height = height;
488 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
489 XDestroyImage( image ); /* frees image->data too */
490 LeaveCriticalSection( &X11DRV_CritSection );
495 /***********************************************************************
498 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
500 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
503 WARN("Bad bitmap handle %08x\n", hbitmap);
508 ret = X11DRV_GetBitmapBits(bmp, bits, count);
509 else if(flags == DDB_SET)
510 ret = X11DRV_SetBitmapBits(bmp, bits, count);
512 ERR("Unknown flags value %d\n", flags);
516 GDI_HEAP_UNLOCK( hbitmap );
520 /***********************************************************************
521 * X11DRV_BITMAP_DeleteObject
523 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
525 X11DRV_PHYSBITMAP *pbitmap = bmp->DDBitmap->physBitmap;
527 TSXFreePixmap( display, pbitmap->pixmap );
529 HeapFree( GetProcessHeap(), 0, bmp->DDBitmap->physBitmap );
530 HeapFree( GetProcessHeap(), 0, bmp->DDBitmap );
531 bmp->DDBitmap = NULL;
536 /**************************************************************************
537 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
539 * Allocates an HBITMAP which references the Pixmap passed in.
540 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
541 * calling DeleteObject on this will free the Pixmap as well.
543 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
546 BITMAPOBJ *pBmp = NULL;
547 X11DRV_PHYSBITMAP *pPhysBmp = NULL;
549 int x,y; /* Unused */
550 unsigned border_width; /* Unused */
551 unsigned int depth, width, height;
553 /* Get the Pixmap dimensions and bit depth */
554 if ( 0 == TSXGetGeometry(display, pixmap, &root, &x, &y, &width, &height,
555 &border_width, &depth) )
558 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
559 width, height, depth);
562 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
563 * and make it a container for the pixmap passed.
565 hBmp = CreateBitmap( width, height, 1, depth, NULL );
567 /* Allocate DDBitmap and physBitmap structures in BITMAPOBJ.
568 * The hBmp is just a filled in BITMAPOBJ header at this point.
570 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
571 pPhysBmp = X11DRV_AllocBitmap( pBmp );
579 /* Point to our Pixmap in the physical bitmap structure */
580 pPhysBmp->pixmap = pixmap;
583 TRACE("\tReturning HBITMAP %x\n", hBmp);
588 /**************************************************************************
589 * X11DRV_BITMAP_CreateBitmapFromPixmap
591 * Allocates an HBITMAP and copies the Pixmap data into it.
592 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
594 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
596 HBITMAP hBmp = 0, hBmpCopy = 0;
597 BITMAPOBJ *pBmp = NULL;
598 unsigned int width, height;
600 /* Allocate an HBITMAP which references the Pixmap passed to us */
601 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
604 TRACE("\tCould not create bitmap header for Pixmap\n");
608 /* Get the bitmap dimensions */
609 width = pBmp->bitmap.bmWidth;
610 height = pBmp->bitmap.bmHeight;
612 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
614 /* We can now get rid of the HBITMAP wrapper we created earlier.
615 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
619 /* Manually free the DDBitmap internals to prevent the Pixmap
620 * from being deleted by DeleteObject.
622 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
623 HeapFree( GetProcessHeap(), 0, pBmp->DDBitmap->physBitmap );
624 HeapFree( GetProcessHeap(), 0, pBmp->DDBitmap );
625 pBmp->DDBitmap = NULL;
630 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
635 /**************************************************************************
636 * X11DRV_BITMAP_CreatePixmapFromBitmap
638 * Creates a Pixmap from a bitmap
640 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
642 HGLOBAL hPackedDIB = NULL;
643 Pixmap pixmap = NULL;
646 * Create a packed DIB from the bitmap passed to us.
647 * A packed DIB contains a BITMAPINFO structure followed immediately by
648 * an optional color palette and the pixel data.
650 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
652 /* Create a Pixmap from the packed DIB */
653 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
655 /* Free the temporary packed DIB */
656 GlobalFree(hPackedDIB);
662 /***********************************************************************
663 * X11DRV_BITMAP_Pixmap
665 * This function exists solely for x11 driver of the window system.
667 BOOL X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
669 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
670 return ((X11DRV_PHYSBITMAP *)(bmp->DDBitmap->physBitmap))->pixmap;
673 #endif /* !defined(X_DISPLAY_MISSING) */