2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
20 #include "debugtools.h"
25 #include "wine/winuser16.h"
27 DEFAULT_DEBUG_CHANNEL(x11drv);
29 /* GCs used for B&W and color bitmap operations */
30 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
33 /***********************************************************************
36 BOOL X11DRV_BITMAP_Init(void)
40 /* Create the necessary GCs */
42 if ((tmpPixmap = TSXCreatePixmap(display,
43 X11DRV_GetXRootWindow(),
47 BITMAP_monoGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
48 TSXSetGraphicsExposures( display, BITMAP_monoGC, False );
49 TSXFreePixmap( display, tmpPixmap );
52 if (X11DRV_GetDepth() != 1)
54 if ((tmpPixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
55 1, 1, X11DRV_GetDepth())))
57 BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
58 TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
59 TSXFreePixmap( display, tmpPixmap );
65 /***********************************************************************
66 * X11DRV_BITMAP_SelectObject
68 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
72 HBITMAP prevHandle = dc->w.hBitmap;
73 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
76 if (!(dc->w.flags & DC_MEMORY)) return 0;
79 if(!X11DRV_CreateBitmap(hbitmap))
82 if(bmp->funcs != dc->funcs) {
83 WARN("Trying to select non-X11 DDB into an X11 dc\n");
87 dc->w.totalExtent.left = 0;
88 dc->w.totalExtent.top = 0;
89 dc->w.totalExtent.right = bmp->bitmap.bmWidth;
90 dc->w.totalExtent.bottom = bmp->bitmap.bmHeight;
93 SetRectRgn( dc->w.hVisRgn, 0, 0,
94 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
97 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
102 physDev->drawable = (Pixmap)bmp->physBitmap;
103 dc->w.hBitmap = hbitmap;
105 /* Change GC depth if needed */
107 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
109 TSXFreeGC( display, physDev->gc );
110 physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
111 TSXSetGraphicsExposures( display, physDev->gc, False );
112 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
115 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
120 /***********************************************************************
123 * Wrapper to call XPutImage with CALL_LARGE_STACK.
126 struct XPutImage_descr
134 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
136 return XPutImage( display, (Pixmap)descr->bmp->physBitmap, BITMAP_GC(descr->bmp),
137 descr->image, 0, 0, 0, 0, descr->width, descr->height );
141 /****************************************************************************
143 * X11DRV_CreateBitmap
145 * Create a device dependent X11 bitmap
147 * Returns TRUE on success else FALSE
151 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
153 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
156 WARN("Bad bitmap handle %08x\n", hbitmap);
160 /* Check parameters */
161 if (bmp->bitmap.bmPlanes != 1) return 0;
162 if ((bmp->bitmap.bmBitsPixel != 1) &&
163 (bmp->bitmap.bmBitsPixel != X11DRV_GetDepth()))
165 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
166 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
167 GDI_HEAP_UNLOCK( hbitmap );
171 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
172 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
174 /* Create the pixmap */
175 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
176 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
177 bmp->bitmap.bmBitsPixel)))
179 WARN("Can't create Pixmap\n");
180 GDI_HEAP_UNLOCK( hbitmap );
183 bmp->funcs = &X11DRV_DC_Funcs;
185 if (bmp->bitmap.bmBits) /* Set bitmap bits */
186 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
187 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
190 GDI_HEAP_UNLOCK( hbitmap );
195 /***********************************************************************
196 * X11DRV_BITMAP_GetXImage
198 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
200 XImage *X11DRV_BITMAP_GetXImage( const BITMAPOBJ *bmp )
202 return XGetImage( display, (Pixmap)bmp->physBitmap,
203 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
204 AllPlanes, ZPixmap );
208 /***********************************************************************
209 * X11DRV_GetBitmapBits
212 * Success: Number of bytes copied
215 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
217 LONG old_height, height;
219 LPBYTE tbuf, startline;
222 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
224 EnterCriticalSection( &X11DRV_CritSection );
226 /* Hack: change the bitmap height temporarily to avoid */
227 /* getting unnecessary bitmap rows. */
229 old_height = bmp->bitmap.bmHeight;
230 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
232 image = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
234 bmp->bitmap.bmHeight = old_height;
236 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
239 switch (bmp->bitmap.bmBitsPixel)
242 for (h=0;h<height;h++)
246 for (w=0;w<bmp->bitmap.bmWidth;w++)
250 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
251 if ((w&7) == 7) ++tbuf;
253 startline += bmp->bitmap.bmWidthBytes;
257 for (h=0;h<height;h++)
260 for (w=0;w<bmp->bitmap.bmWidth;w++)
262 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
263 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
265 startline += bmp->bitmap.bmWidthBytes;
269 for (h=0;h<height;h++)
272 for (w=0;w<bmp->bitmap.bmWidth;w++)
273 *tbuf++ = XGetPixel(image,w,h);
274 startline += bmp->bitmap.bmWidthBytes;
279 for (h=0;h<height;h++)
282 for (w=0;w<bmp->bitmap.bmWidth;w++)
284 long pixel = XGetPixel(image,w,h);
286 *tbuf++ = pixel & 0xff;
287 *tbuf++ = (pixel>>8) & 0xff;
289 startline += bmp->bitmap.bmWidthBytes;
293 for (h=0;h<height;h++)
296 for (w=0;w<bmp->bitmap.bmWidth;w++)
298 long pixel = XGetPixel(image,w,h);
300 *tbuf++ = pixel & 0xff;
301 *tbuf++ = (pixel>> 8) & 0xff;
302 *tbuf++ = (pixel>>16) & 0xff;
304 startline += bmp->bitmap.bmWidthBytes;
309 for (h=0;h<height;h++)
312 for (w=0;w<bmp->bitmap.bmWidth;w++)
314 long pixel = XGetPixel(image,w,h);
316 *tbuf++ = pixel & 0xff;
317 *tbuf++ = (pixel>> 8) & 0xff;
318 *tbuf++ = (pixel>>16) & 0xff;
319 *tbuf++ = (pixel>>24) & 0xff;
321 startline += bmp->bitmap.bmWidthBytes;
325 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
327 XDestroyImage( image );
328 LeaveCriticalSection( &X11DRV_CritSection );
335 /******************************************************************************
336 * X11DRV_SetBitmapBits
339 * Success: Number of bytes used in setting the bitmap bits
342 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
344 struct XPutImage_descr descr;
347 LPBYTE sbuf, startline;
350 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
352 height = count / bmp->bitmap.bmWidthBytes;
354 EnterCriticalSection( &X11DRV_CritSection );
355 image = XCreateImage( display, X11DRV_GetVisual(), bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
356 bmp->bitmap.bmWidth, height, 32, 0 );
357 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
359 WARN("No memory to create image data.\n");
360 XDestroyImage( image );
361 LeaveCriticalSection( &X11DRV_CritSection );
365 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
369 switch (bmp->bitmap.bmBitsPixel)
372 for (h=0;h<height;h++)
375 for (w=0;w<bmp->bitmap.bmWidth;w++)
377 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
381 startline += bmp->bitmap.bmWidthBytes;
385 for (h=0;h<height;h++)
388 for (w=0;w<bmp->bitmap.bmWidth;w++)
390 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
391 else XPutPixel( image, w, h, *sbuf++ & 0xf );
393 startline += bmp->bitmap.bmWidthBytes;
397 for (h=0;h<height;h++)
400 for (w=0;w<bmp->bitmap.bmWidth;w++)
401 XPutPixel(image,w,h,*sbuf++);
402 startline += bmp->bitmap.bmWidthBytes;
407 for (h=0;h<height;h++)
410 for (w=0;w<bmp->bitmap.bmWidth;w++)
412 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
415 startline += bmp->bitmap.bmWidthBytes;
419 for (h=0;h<height;h++)
422 for (w=0;w<bmp->bitmap.bmWidth;w++)
424 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
427 startline += bmp->bitmap.bmWidthBytes;
431 for (h=0;h<height;h++)
434 for (w=0;w<bmp->bitmap.bmWidth;w++)
436 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
439 startline += bmp->bitmap.bmWidthBytes;
443 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
449 descr.width = bmp->bitmap.bmWidth;
450 descr.height = height;
452 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
453 XDestroyImage( image ); /* frees image->data too */
454 LeaveCriticalSection( &X11DRV_CritSection );
459 /***********************************************************************
462 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
464 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
467 WARN("Bad bitmap handle %08x\n", hbitmap);
472 ret = X11DRV_GetBitmapBits(bmp, bits, count);
473 else if(flags == DDB_SET)
474 ret = X11DRV_SetBitmapBits(bmp, bits, count);
476 ERR("Unknown flags value %d\n", flags);
480 GDI_HEAP_UNLOCK( hbitmap );
484 /***********************************************************************
485 * X11DRV_BITMAP_DeleteObject
487 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
489 TSXFreePixmap( display, (Pixmap)bmp->physBitmap );
490 bmp->physBitmap = NULL;
495 /**************************************************************************
496 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
498 * Allocates an HBITMAP which references the Pixmap passed in.
499 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
500 * calling DeleteObject on this will free the Pixmap as well.
502 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
505 BITMAPOBJ *pBmp = NULL;
507 int x,y; /* Unused */
508 unsigned border_width; /* Unused */
509 unsigned int depth, width, height;
511 /* Get the Pixmap dimensions and bit depth */
512 if ( 0 == TSXGetGeometry(display, pixmap, &root, &x, &y, &width, &height,
513 &border_width, &depth) )
516 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
517 width, height, depth);
520 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
521 * and make it a container for the pixmap passed.
523 hBmp = CreateBitmap( width, height, 1, depth, NULL );
525 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
527 pBmp->funcs = &X11DRV_DC_Funcs;
528 pBmp->physBitmap = (void *)pixmap;
531 TRACE("\tReturning HBITMAP %x\n", hBmp);
536 /**************************************************************************
537 * X11DRV_BITMAP_CreateBitmapFromPixmap
539 * Allocates an HBITMAP and copies the Pixmap data into it.
540 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
542 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
544 HBITMAP hBmp = 0, hBmpCopy = 0;
545 BITMAPOBJ *pBmp = NULL;
546 unsigned int width, height;
548 /* Allocate an HBITMAP which references the Pixmap passed to us */
549 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
552 TRACE("\tCould not create bitmap header for Pixmap\n");
556 /* Get the bitmap dimensions */
557 width = pBmp->bitmap.bmWidth;
558 height = pBmp->bitmap.bmHeight;
560 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
562 /* We can now get rid of the HBITMAP wrapper we created earlier.
563 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
567 /* Manually clear the bitmap internals to prevent the Pixmap
568 * from being deleted by DeleteObject.
570 pBmp->physBitmap = NULL;
576 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
581 /**************************************************************************
582 * X11DRV_BITMAP_CreatePixmapFromBitmap
584 * Creates a Pixmap from a bitmap
586 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
588 HGLOBAL hPackedDIB = 0;
592 * Create a packed DIB from the bitmap passed to us.
593 * A packed DIB contains a BITMAPINFO structure followed immediately by
594 * an optional color palette and the pixel data.
596 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
598 /* Create a Pixmap from the packed DIB */
599 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
601 /* Free the temporary packed DIB */
602 GlobalFree(hPackedDIB);
608 /***********************************************************************
609 * X11DRV_BITMAP_Pixmap
611 * This function exists solely for x11 driver of the window system.
613 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
615 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
616 return (Pixmap)bmp->physBitmap;