2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
18 #include "debugtools.h"
22 #include "wine/winuser16.h"
24 DEFAULT_DEBUG_CHANNEL(x11drv);
26 /* GCs used for B&W and color bitmap operations */
27 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
30 /***********************************************************************
33 BOOL X11DRV_BITMAP_Init(void)
37 /* Create the necessary GCs */
40 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 )))
42 BITMAP_monoGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
43 XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
44 XFreePixmap( gdi_display, tmpPixmap );
47 if (screen_depth != 1)
49 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
51 BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
52 XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
53 XFreePixmap( gdi_display, tmpPixmap );
60 /***********************************************************************
61 * X11DRV_BITMAP_SelectObject
63 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
67 HBITMAP prevHandle = dc->hBitmap;
68 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
71 if (!(dc->flags & DC_MEMORY)) return 0;
74 if(!X11DRV_CreateBitmap(hbitmap))
77 if(bmp->funcs != dc->funcs) {
78 WARN("Trying to select non-X11 DDB into an X11 dc\n");
82 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
85 dc->totalExtent.left = 0;
86 dc->totalExtent.top = 0;
87 dc->totalExtent.right = bmp->bitmap.bmWidth;
88 dc->totalExtent.bottom = bmp->bitmap.bmHeight;
90 physDev->drawable = (Pixmap)bmp->physBitmap;
91 dc->hBitmap = hbitmap;
93 SelectVisRgn16( dc->hSelf, hrgn );
96 /* Change GC depth if needed */
98 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
101 XFreeGC( gdi_display, physDev->gc );
102 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
103 XSetGraphicsExposures( gdi_display, physDev->gc, False );
105 dc->bitsPerPixel = bmp->bitmap.bmBitsPixel;
112 /****************************************************************************
114 * X11DRV_CreateBitmap
116 * Create a device dependent X11 bitmap
118 * Returns TRUE on success else FALSE
122 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
124 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
127 WARN("Bad bitmap handle %08x\n", hbitmap);
131 /* Check parameters */
132 if (bmp->bitmap.bmPlanes != 1)
134 GDI_ReleaseObj( hbitmap );
137 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
139 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
140 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
141 GDI_ReleaseObj( hbitmap );
145 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
146 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
148 /* Create the pixmap */
149 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
150 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
151 bmp->bitmap.bmBitsPixel)))
153 WARN("Can't create Pixmap\n");
154 GDI_ReleaseObj( hbitmap );
157 bmp->funcs = &X11DRV_DC_Funcs;
159 if (bmp->bitmap.bmBits) /* Set bitmap bits */
160 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
161 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
164 GDI_ReleaseObj( hbitmap );
169 /***********************************************************************
170 * X11DRV_GetBitmapBits
173 * Success: Number of bytes copied
176 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
178 LONG old_height, height;
180 LPBYTE tbuf, startline;
183 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
187 /* Hack: change the bitmap height temporarily to avoid */
188 /* getting unnecessary bitmap rows. */
190 old_height = bmp->bitmap.bmHeight;
191 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
193 image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
194 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
195 AllPlanes, ZPixmap );
196 bmp->bitmap.bmHeight = old_height;
198 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
201 switch (bmp->bitmap.bmBitsPixel)
204 for (h=0;h<height;h++)
208 for (w=0;w<bmp->bitmap.bmWidth;w++)
212 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
213 if ((w&7) == 7) ++tbuf;
215 startline += bmp->bitmap.bmWidthBytes;
219 for (h=0;h<height;h++)
222 for (w=0;w<bmp->bitmap.bmWidth;w++)
224 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
225 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
227 startline += bmp->bitmap.bmWidthBytes;
231 for (h=0;h<height;h++)
234 for (w=0;w<bmp->bitmap.bmWidth;w++)
235 *tbuf++ = XGetPixel(image,w,h);
236 startline += bmp->bitmap.bmWidthBytes;
241 for (h=0;h<height;h++)
244 for (w=0;w<bmp->bitmap.bmWidth;w++)
246 long pixel = XGetPixel(image,w,h);
248 *tbuf++ = pixel & 0xff;
249 *tbuf++ = (pixel>>8) & 0xff;
251 startline += bmp->bitmap.bmWidthBytes;
255 for (h=0;h<height;h++)
258 for (w=0;w<bmp->bitmap.bmWidth;w++)
260 long pixel = XGetPixel(image,w,h);
262 *tbuf++ = pixel & 0xff;
263 *tbuf++ = (pixel>> 8) & 0xff;
264 *tbuf++ = (pixel>>16) & 0xff;
266 startline += bmp->bitmap.bmWidthBytes;
271 for (h=0;h<height;h++)
274 for (w=0;w<bmp->bitmap.bmWidth;w++)
276 long pixel = XGetPixel(image,w,h);
278 *tbuf++ = pixel & 0xff;
279 *tbuf++ = (pixel>> 8) & 0xff;
280 *tbuf++ = (pixel>>16) & 0xff;
281 *tbuf++ = (pixel>>24) & 0xff;
283 startline += bmp->bitmap.bmWidthBytes;
287 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
289 XDestroyImage( image );
296 /******************************************************************************
297 * X11DRV_SetBitmapBits
300 * Success: Number of bytes used in setting the bitmap bits
303 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
307 LPBYTE sbuf, startline;
310 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
312 height = count / bmp->bitmap.bmWidthBytes;
315 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
316 bmp->bitmap.bmWidth, height, 32, 0 );
317 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
319 WARN("No memory to create image data.\n");
320 XDestroyImage( image );
325 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
329 switch (bmp->bitmap.bmBitsPixel)
332 for (h=0;h<height;h++)
335 for (w=0;w<bmp->bitmap.bmWidth;w++)
337 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
341 startline += bmp->bitmap.bmWidthBytes;
345 for (h=0;h<height;h++)
348 for (w=0;w<bmp->bitmap.bmWidth;w++)
350 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
351 else XPutPixel( image, w, h, *sbuf++ & 0xf );
353 startline += bmp->bitmap.bmWidthBytes;
357 for (h=0;h<height;h++)
360 for (w=0;w<bmp->bitmap.bmWidth;w++)
361 XPutPixel(image,w,h,*sbuf++);
362 startline += bmp->bitmap.bmWidthBytes;
367 for (h=0;h<height;h++)
370 for (w=0;w<bmp->bitmap.bmWidth;w++)
372 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
375 startline += bmp->bitmap.bmWidthBytes;
379 for (h=0;h<height;h++)
382 for (w=0;w<bmp->bitmap.bmWidth;w++)
384 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
387 startline += bmp->bitmap.bmWidthBytes;
391 for (h=0;h<height;h++)
394 for (w=0;w<bmp->bitmap.bmWidth;w++)
396 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
399 startline += bmp->bitmap.bmWidthBytes;
403 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
406 XPutImage( gdi_display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
407 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
408 XDestroyImage( image ); /* frees image->data too */
413 /***********************************************************************
416 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
418 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
421 WARN("Bad bitmap handle %08x\n", hbitmap);
426 ret = X11DRV_GetBitmapBits(bmp, bits, count);
427 else if(flags == DDB_SET)
428 ret = X11DRV_SetBitmapBits(bmp, bits, count);
430 ERR("Unknown flags value %d\n", flags);
433 GDI_ReleaseObj( hbitmap );
437 /***********************************************************************
438 * X11DRV_BITMAP_DeleteObject
440 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
442 TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
443 bmp->physBitmap = NULL;
448 /**************************************************************************
449 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
451 * Allocates an HBITMAP which references the Pixmap passed in.
452 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
453 * calling DeleteObject on this will free the Pixmap as well.
455 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
458 BITMAPOBJ *pBmp = NULL;
460 int x,y; /* Unused */
461 unsigned border_width; /* Unused */
462 unsigned int depth, width, height;
464 /* Get the Pixmap dimensions and bit depth */
465 if ( 0 == TSXGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
466 &border_width, &depth) )
469 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
470 width, height, depth);
473 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
474 * and make it a container for the pixmap passed.
476 hBmp = CreateBitmap( width, height, 1, depth, NULL );
478 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
480 pBmp->funcs = &X11DRV_DC_Funcs;
481 pBmp->physBitmap = (void *)pixmap;
482 GDI_ReleaseObj( hBmp );
485 TRACE("\tReturning HBITMAP %x\n", hBmp);
490 /**************************************************************************
491 * X11DRV_BITMAP_CreateBitmapFromPixmap
493 * Allocates an HBITMAP and copies the Pixmap data into it.
494 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
496 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
498 HBITMAP hBmp = 0, hBmpCopy = 0;
499 BITMAPOBJ *pBmp = NULL;
500 unsigned int width, height;
502 /* Allocate an HBITMAP which references the Pixmap passed to us */
503 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
506 TRACE("\tCould not create bitmap header for Pixmap\n");
510 /* Get the bitmap dimensions */
511 width = pBmp->bitmap.bmWidth;
512 height = pBmp->bitmap.bmHeight;
514 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
516 /* We can now get rid of the HBITMAP wrapper we created earlier.
517 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
521 /* Manually clear the bitmap internals to prevent the Pixmap
522 * from being deleted by DeleteObject.
524 pBmp->physBitmap = NULL;
530 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
535 /**************************************************************************
536 * X11DRV_BITMAP_CreatePixmapFromBitmap
538 * Creates a Pixmap from a bitmap
540 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
542 HGLOBAL hPackedDIB = 0;
546 * Create a packed DIB from the bitmap passed to us.
547 * A packed DIB contains a BITMAPINFO structure followed immediately by
548 * an optional color palette and the pixel data.
550 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
552 /* Create a Pixmap from the packed DIB */
553 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
555 /* Free the temporary packed DIB */
556 GlobalFree(hPackedDIB);
562 /***********************************************************************
563 * X11DRV_BITMAP_Pixmap
565 * This function exists solely for x11 driver of the window system.
567 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
570 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
571 pixmap = (Pixmap)bmp->physBitmap;
572 GDI_ReleaseObj( hbitmap );