2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
17 #include "debugtools.h"
21 #include "wine/winuser16.h"
23 DEFAULT_DEBUG_CHANNEL(x11drv);
25 /* GCs used for B&W and color bitmap operations */
26 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
29 /***********************************************************************
32 BOOL X11DRV_BITMAP_Init(void)
36 /* Create the necessary GCs */
39 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 )))
41 BITMAP_monoGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
42 XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
43 XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
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 XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
54 XFreePixmap( gdi_display, tmpPixmap );
61 /***********************************************************************
62 * X11DRV_BITMAP_SelectObject
64 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
68 HBITMAP prevHandle = dc->hBitmap;
69 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
72 if (!(dc->flags & DC_MEMORY)) return 0;
75 if(!X11DRV_CreateBitmap(hbitmap))
78 if(bmp->funcs != dc->funcs) {
79 WARN("Trying to select non-X11 DDB into an X11 dc\n");
83 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
86 dc->totalExtent.left = 0;
87 dc->totalExtent.top = 0;
88 dc->totalExtent.right = bmp->bitmap.bmWidth;
89 dc->totalExtent.bottom = bmp->bitmap.bmHeight;
91 physDev->drawable = (Pixmap)bmp->physBitmap;
92 dc->hBitmap = hbitmap;
94 SelectVisRgn16( dc->hSelf, hrgn );
97 /* Change GC depth if needed */
99 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
102 XFreeGC( gdi_display, physDev->gc );
103 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
104 XSetGraphicsExposures( gdi_display, physDev->gc, False );
105 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
106 XFlush( gdi_display );
108 dc->bitsPerPixel = bmp->bitmap.bmBitsPixel;
115 /****************************************************************************
117 * X11DRV_CreateBitmap
119 * Create a device dependent X11 bitmap
121 * Returns TRUE on success else FALSE
125 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
127 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
130 WARN("Bad bitmap handle %08x\n", hbitmap);
134 /* Check parameters */
135 if (bmp->bitmap.bmPlanes != 1)
137 GDI_ReleaseObj( hbitmap );
140 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
142 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
143 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
144 GDI_ReleaseObj( hbitmap );
148 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
149 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
151 /* Create the pixmap */
152 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
153 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
154 bmp->bitmap.bmBitsPixel)))
156 WARN("Can't create Pixmap\n");
157 GDI_ReleaseObj( hbitmap );
160 bmp->funcs = &X11DRV_DC_Funcs;
162 if (bmp->bitmap.bmBits) /* Set bitmap bits */
163 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
164 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
167 GDI_ReleaseObj( hbitmap );
172 /***********************************************************************
173 * X11DRV_GetBitmapBits
176 * Success: Number of bytes copied
179 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
181 LONG old_height, height;
183 LPBYTE tbuf, startline;
186 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
190 /* Hack: change the bitmap height temporarily to avoid */
191 /* getting unnecessary bitmap rows. */
193 old_height = bmp->bitmap.bmHeight;
194 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
196 image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
197 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
198 AllPlanes, ZPixmap );
199 bmp->bitmap.bmHeight = old_height;
201 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
204 switch (bmp->bitmap.bmBitsPixel)
207 for (h=0;h<height;h++)
211 for (w=0;w<bmp->bitmap.bmWidth;w++)
215 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
216 if ((w&7) == 7) ++tbuf;
218 startline += bmp->bitmap.bmWidthBytes;
222 for (h=0;h<height;h++)
225 for (w=0;w<bmp->bitmap.bmWidth;w++)
227 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
228 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
230 startline += bmp->bitmap.bmWidthBytes;
234 for (h=0;h<height;h++)
237 for (w=0;w<bmp->bitmap.bmWidth;w++)
238 *tbuf++ = XGetPixel(image,w,h);
239 startline += bmp->bitmap.bmWidthBytes;
244 for (h=0;h<height;h++)
247 for (w=0;w<bmp->bitmap.bmWidth;w++)
249 long pixel = XGetPixel(image,w,h);
251 *tbuf++ = pixel & 0xff;
252 *tbuf++ = (pixel>>8) & 0xff;
254 startline += bmp->bitmap.bmWidthBytes;
258 for (h=0;h<height;h++)
261 for (w=0;w<bmp->bitmap.bmWidth;w++)
263 long pixel = XGetPixel(image,w,h);
265 *tbuf++ = pixel & 0xff;
266 *tbuf++ = (pixel>> 8) & 0xff;
267 *tbuf++ = (pixel>>16) & 0xff;
269 startline += bmp->bitmap.bmWidthBytes;
274 for (h=0;h<height;h++)
277 for (w=0;w<bmp->bitmap.bmWidth;w++)
279 long pixel = XGetPixel(image,w,h);
281 *tbuf++ = pixel & 0xff;
282 *tbuf++ = (pixel>> 8) & 0xff;
283 *tbuf++ = (pixel>>16) & 0xff;
284 *tbuf++ = (pixel>>24) & 0xff;
286 startline += bmp->bitmap.bmWidthBytes;
290 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
292 XDestroyImage( image );
299 /******************************************************************************
300 * X11DRV_SetBitmapBits
303 * Success: Number of bytes used in setting the bitmap bits
306 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
310 LPBYTE sbuf, startline;
313 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
315 height = count / bmp->bitmap.bmWidthBytes;
318 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
319 bmp->bitmap.bmWidth, height, 32, 0 );
320 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
322 WARN("No memory to create image data.\n");
323 XDestroyImage( image );
328 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
332 switch (bmp->bitmap.bmBitsPixel)
335 for (h=0;h<height;h++)
338 for (w=0;w<bmp->bitmap.bmWidth;w++)
340 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
344 startline += bmp->bitmap.bmWidthBytes;
348 for (h=0;h<height;h++)
351 for (w=0;w<bmp->bitmap.bmWidth;w++)
353 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
354 else XPutPixel( image, w, h, *sbuf++ & 0xf );
356 startline += bmp->bitmap.bmWidthBytes;
360 for (h=0;h<height;h++)
363 for (w=0;w<bmp->bitmap.bmWidth;w++)
364 XPutPixel(image,w,h,*sbuf++);
365 startline += bmp->bitmap.bmWidthBytes;
370 for (h=0;h<height;h++)
373 for (w=0;w<bmp->bitmap.bmWidth;w++)
375 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
378 startline += bmp->bitmap.bmWidthBytes;
382 for (h=0;h<height;h++)
385 for (w=0;w<bmp->bitmap.bmWidth;w++)
387 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
390 startline += bmp->bitmap.bmWidthBytes;
394 for (h=0;h<height;h++)
397 for (w=0;w<bmp->bitmap.bmWidth;w++)
399 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
402 startline += bmp->bitmap.bmWidthBytes;
406 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
409 XPutImage( gdi_display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
410 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
411 XDestroyImage( image ); /* frees image->data too */
416 /***********************************************************************
419 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
421 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
424 WARN("Bad bitmap handle %08x\n", hbitmap);
429 ret = X11DRV_GetBitmapBits(bmp, bits, count);
430 else if(flags == DDB_SET)
431 ret = X11DRV_SetBitmapBits(bmp, bits, count);
433 ERR("Unknown flags value %d\n", flags);
436 GDI_ReleaseObj( hbitmap );
440 /***********************************************************************
441 * X11DRV_BITMAP_DeleteObject
443 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
445 TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
446 bmp->physBitmap = NULL;
451 /**************************************************************************
452 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
454 * Allocates an HBITMAP which references the Pixmap passed in.
455 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
456 * calling DeleteObject on this will free the Pixmap as well.
458 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
461 BITMAPOBJ *pBmp = NULL;
463 int x,y; /* Unused */
464 unsigned border_width; /* Unused */
465 unsigned int depth, width, height;
467 /* Get the Pixmap dimensions and bit depth */
468 if ( 0 == TSXGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
469 &border_width, &depth) )
472 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
473 width, height, depth);
476 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
477 * and make it a container for the pixmap passed.
479 hBmp = CreateBitmap( width, height, 1, depth, NULL );
481 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
483 pBmp->funcs = &X11DRV_DC_Funcs;
484 pBmp->physBitmap = (void *)pixmap;
485 GDI_ReleaseObj( hBmp );
488 TRACE("\tReturning HBITMAP %x\n", hBmp);
493 /**************************************************************************
494 * X11DRV_BITMAP_CreateBitmapFromPixmap
496 * Allocates an HBITMAP and copies the Pixmap data into it.
497 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
499 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
501 HBITMAP hBmp = 0, hBmpCopy = 0;
502 BITMAPOBJ *pBmp = NULL;
503 unsigned int width, height;
505 /* Allocate an HBITMAP which references the Pixmap passed to us */
506 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
509 TRACE("\tCould not create bitmap header for Pixmap\n");
513 /* Get the bitmap dimensions */
514 width = pBmp->bitmap.bmWidth;
515 height = pBmp->bitmap.bmHeight;
517 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
519 /* We can now get rid of the HBITMAP wrapper we created earlier.
520 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
524 /* Manually clear the bitmap internals to prevent the Pixmap
525 * from being deleted by DeleteObject.
527 pBmp->physBitmap = NULL;
533 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
538 /**************************************************************************
539 * X11DRV_BITMAP_CreatePixmapFromBitmap
541 * Creates a Pixmap from a bitmap
543 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
545 HGLOBAL hPackedDIB = 0;
549 * Create a packed DIB from the bitmap passed to us.
550 * A packed DIB contains a BITMAPINFO structure followed immediately by
551 * an optional color palette and the pixel data.
553 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
555 /* Create a Pixmap from the packed DIB */
556 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
558 /* Free the temporary packed DIB */
559 GlobalFree(hPackedDIB);
565 /***********************************************************************
566 * X11DRV_BITMAP_Pixmap
568 * This function exists solely for x11 driver of the window system.
570 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
573 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
574 pixmap = (Pixmap)bmp->physBitmap;
575 GDI_ReleaseObj( hbitmap );