2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
19 #include "debugtools.h"
24 #include "wine/winuser16.h"
26 DEFAULT_DEBUG_CHANNEL(x11drv);
28 /* GCs used for B&W and color bitmap operations */
29 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
32 /***********************************************************************
35 BOOL X11DRV_BITMAP_Init(void)
39 /* Create the necessary GCs */
41 if ((tmpPixmap = TSXCreatePixmap(display,
42 X11DRV_GetXRootWindow(),
46 BITMAP_monoGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
47 TSXSetGraphicsExposures( display, BITMAP_monoGC, False );
48 TSXFreePixmap( display, tmpPixmap );
51 if (X11DRV_GetDepth() != 1)
53 if ((tmpPixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
54 1, 1, X11DRV_GetDepth())))
56 BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
57 TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
58 TSXFreePixmap( display, tmpPixmap );
64 /***********************************************************************
65 * X11DRV_BITMAP_SelectObject
67 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
71 HBITMAP prevHandle = dc->hBitmap;
72 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
75 if (!(dc->flags & DC_MEMORY)) return 0;
78 if(!X11DRV_CreateBitmap(hbitmap))
81 if(bmp->funcs != dc->funcs) {
82 WARN("Trying to select non-X11 DDB into an X11 dc\n");
86 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
89 dc->totalExtent.left = 0;
90 dc->totalExtent.top = 0;
91 dc->totalExtent.right = bmp->bitmap.bmWidth;
92 dc->totalExtent.bottom = bmp->bitmap.bmHeight;
94 physDev->drawable = (Pixmap)bmp->physBitmap;
95 dc->hBitmap = hbitmap;
97 SelectVisRgn16( dc->hSelf, hrgn );
100 /* Change GC depth if needed */
102 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
104 TSXFreeGC( display, physDev->gc );
105 physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
106 TSXSetGraphicsExposures( display, physDev->gc, False );
107 dc->bitsPerPixel = bmp->bitmap.bmBitsPixel;
114 /***********************************************************************
117 * Wrapper to call XPutImage with CALL_LARGE_STACK.
120 struct XPutImage_descr
128 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
130 return XPutImage( display, (Pixmap)descr->bmp->physBitmap, BITMAP_GC(descr->bmp),
131 descr->image, 0, 0, 0, 0, descr->width, descr->height );
135 /****************************************************************************
137 * X11DRV_CreateBitmap
139 * Create a device dependent X11 bitmap
141 * Returns TRUE on success else FALSE
145 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
147 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
150 WARN("Bad bitmap handle %08x\n", hbitmap);
154 /* Check parameters */
155 if (bmp->bitmap.bmPlanes != 1)
157 GDI_ReleaseObj( hbitmap );
160 if ((bmp->bitmap.bmBitsPixel != 1) &&
161 (bmp->bitmap.bmBitsPixel != X11DRV_GetDepth()))
163 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
164 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
165 GDI_ReleaseObj( hbitmap );
169 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
170 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
172 /* Create the pixmap */
173 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
174 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
175 bmp->bitmap.bmBitsPixel)))
177 WARN("Can't create Pixmap\n");
178 GDI_ReleaseObj( hbitmap );
181 bmp->funcs = &X11DRV_DC_Funcs;
183 if (bmp->bitmap.bmBits) /* Set bitmap bits */
184 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
185 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
188 GDI_ReleaseObj( hbitmap );
193 /***********************************************************************
194 * X11DRV_BITMAP_GetXImage
196 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
198 XImage *X11DRV_BITMAP_GetXImage( const BITMAPOBJ *bmp )
200 return XGetImage( display, (Pixmap)bmp->physBitmap,
201 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
202 AllPlanes, ZPixmap );
206 /***********************************************************************
207 * X11DRV_GetBitmapBits
210 * Success: Number of bytes copied
213 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
215 LONG old_height, height;
217 LPBYTE tbuf, startline;
220 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
222 EnterCriticalSection( &X11DRV_CritSection );
224 /* Hack: change the bitmap height temporarily to avoid */
225 /* getting unnecessary bitmap rows. */
227 old_height = bmp->bitmap.bmHeight;
228 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
230 image = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
232 bmp->bitmap.bmHeight = old_height;
234 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
237 switch (bmp->bitmap.bmBitsPixel)
240 for (h=0;h<height;h++)
244 for (w=0;w<bmp->bitmap.bmWidth;w++)
248 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
249 if ((w&7) == 7) ++tbuf;
251 startline += bmp->bitmap.bmWidthBytes;
255 for (h=0;h<height;h++)
258 for (w=0;w<bmp->bitmap.bmWidth;w++)
260 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
261 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
263 startline += bmp->bitmap.bmWidthBytes;
267 for (h=0;h<height;h++)
270 for (w=0;w<bmp->bitmap.bmWidth;w++)
271 *tbuf++ = XGetPixel(image,w,h);
272 startline += bmp->bitmap.bmWidthBytes;
277 for (h=0;h<height;h++)
280 for (w=0;w<bmp->bitmap.bmWidth;w++)
282 long pixel = XGetPixel(image,w,h);
284 *tbuf++ = pixel & 0xff;
285 *tbuf++ = (pixel>>8) & 0xff;
287 startline += bmp->bitmap.bmWidthBytes;
291 for (h=0;h<height;h++)
294 for (w=0;w<bmp->bitmap.bmWidth;w++)
296 long pixel = XGetPixel(image,w,h);
298 *tbuf++ = pixel & 0xff;
299 *tbuf++ = (pixel>> 8) & 0xff;
300 *tbuf++ = (pixel>>16) & 0xff;
302 startline += bmp->bitmap.bmWidthBytes;
307 for (h=0;h<height;h++)
310 for (w=0;w<bmp->bitmap.bmWidth;w++)
312 long pixel = XGetPixel(image,w,h);
314 *tbuf++ = pixel & 0xff;
315 *tbuf++ = (pixel>> 8) & 0xff;
316 *tbuf++ = (pixel>>16) & 0xff;
317 *tbuf++ = (pixel>>24) & 0xff;
319 startline += bmp->bitmap.bmWidthBytes;
323 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
325 XDestroyImage( image );
326 LeaveCriticalSection( &X11DRV_CritSection );
333 /******************************************************************************
334 * X11DRV_SetBitmapBits
337 * Success: Number of bytes used in setting the bitmap bits
340 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
342 struct XPutImage_descr descr;
345 LPBYTE sbuf, startline;
348 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
350 height = count / bmp->bitmap.bmWidthBytes;
352 EnterCriticalSection( &X11DRV_CritSection );
353 image = XCreateImage( display, X11DRV_GetVisual(), bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
354 bmp->bitmap.bmWidth, height, 32, 0 );
355 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
357 WARN("No memory to create image data.\n");
358 XDestroyImage( image );
359 LeaveCriticalSection( &X11DRV_CritSection );
363 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
367 switch (bmp->bitmap.bmBitsPixel)
370 for (h=0;h<height;h++)
373 for (w=0;w<bmp->bitmap.bmWidth;w++)
375 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
379 startline += bmp->bitmap.bmWidthBytes;
383 for (h=0;h<height;h++)
386 for (w=0;w<bmp->bitmap.bmWidth;w++)
388 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
389 else XPutPixel( image, w, h, *sbuf++ & 0xf );
391 startline += bmp->bitmap.bmWidthBytes;
395 for (h=0;h<height;h++)
398 for (w=0;w<bmp->bitmap.bmWidth;w++)
399 XPutPixel(image,w,h,*sbuf++);
400 startline += bmp->bitmap.bmWidthBytes;
405 for (h=0;h<height;h++)
408 for (w=0;w<bmp->bitmap.bmWidth;w++)
410 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
413 startline += bmp->bitmap.bmWidthBytes;
417 for (h=0;h<height;h++)
420 for (w=0;w<bmp->bitmap.bmWidth;w++)
422 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
425 startline += bmp->bitmap.bmWidthBytes;
429 for (h=0;h<height;h++)
432 for (w=0;w<bmp->bitmap.bmWidth;w++)
434 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
437 startline += bmp->bitmap.bmWidthBytes;
441 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
447 descr.width = bmp->bitmap.bmWidth;
448 descr.height = height;
450 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
451 XDestroyImage( image ); /* frees image->data too */
452 LeaveCriticalSection( &X11DRV_CritSection );
457 /***********************************************************************
460 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
462 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
465 WARN("Bad bitmap handle %08x\n", hbitmap);
470 ret = X11DRV_GetBitmapBits(bmp, bits, count);
471 else if(flags == DDB_SET)
472 ret = X11DRV_SetBitmapBits(bmp, bits, count);
474 ERR("Unknown flags value %d\n", flags);
477 GDI_ReleaseObj( hbitmap );
481 /***********************************************************************
482 * X11DRV_BITMAP_DeleteObject
484 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
486 TSXFreePixmap( display, (Pixmap)bmp->physBitmap );
487 bmp->physBitmap = NULL;
492 /**************************************************************************
493 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
495 * Allocates an HBITMAP which references the Pixmap passed in.
496 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
497 * calling DeleteObject on this will free the Pixmap as well.
499 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
502 BITMAPOBJ *pBmp = NULL;
504 int x,y; /* Unused */
505 unsigned border_width; /* Unused */
506 unsigned int depth, width, height;
508 /* Get the Pixmap dimensions and bit depth */
509 if ( 0 == TSXGetGeometry(display, pixmap, &root, &x, &y, &width, &height,
510 &border_width, &depth) )
513 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
514 width, height, depth);
517 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
518 * and make it a container for the pixmap passed.
520 hBmp = CreateBitmap( width, height, 1, depth, NULL );
522 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
524 pBmp->funcs = &X11DRV_DC_Funcs;
525 pBmp->physBitmap = (void *)pixmap;
526 GDI_ReleaseObj( hBmp );
529 TRACE("\tReturning HBITMAP %x\n", hBmp);
534 /**************************************************************************
535 * X11DRV_BITMAP_CreateBitmapFromPixmap
537 * Allocates an HBITMAP and copies the Pixmap data into it.
538 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
540 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
542 HBITMAP hBmp = 0, hBmpCopy = 0;
543 BITMAPOBJ *pBmp = NULL;
544 unsigned int width, height;
546 /* Allocate an HBITMAP which references the Pixmap passed to us */
547 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
550 TRACE("\tCould not create bitmap header for Pixmap\n");
554 /* Get the bitmap dimensions */
555 width = pBmp->bitmap.bmWidth;
556 height = pBmp->bitmap.bmHeight;
558 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
560 /* We can now get rid of the HBITMAP wrapper we created earlier.
561 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
565 /* Manually clear the bitmap internals to prevent the Pixmap
566 * from being deleted by DeleteObject.
568 pBmp->physBitmap = NULL;
574 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
579 /**************************************************************************
580 * X11DRV_BITMAP_CreatePixmapFromBitmap
582 * Creates a Pixmap from a bitmap
584 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
586 HGLOBAL hPackedDIB = 0;
590 * Create a packed DIB from the bitmap passed to us.
591 * A packed DIB contains a BITMAPINFO structure followed immediately by
592 * an optional color palette and the pixel data.
594 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
596 /* Create a Pixmap from the packed DIB */
597 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
599 /* Free the temporary packed DIB */
600 GlobalFree(hPackedDIB);
606 /***********************************************************************
607 * X11DRV_BITMAP_Pixmap
609 * This function exists solely for x11 driver of the window system.
611 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
614 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
615 pixmap = (Pixmap)bmp->physBitmap;
616 GDI_ReleaseObj( hbitmap );