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 XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
45 XFreePixmap( gdi_display, tmpPixmap );
48 if (screen_depth != 1)
50 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
52 BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
53 XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
54 XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
55 XFreePixmap( gdi_display, tmpPixmap );
62 /***********************************************************************
63 * X11DRV_BITMAP_SelectObject
65 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
69 HBITMAP prevHandle = dc->hBitmap;
70 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
73 if (!(dc->flags & DC_MEMORY)) return 0;
76 if(!X11DRV_CreateBitmap(hbitmap))
79 if(bmp->funcs != dc->funcs) {
80 WARN("Trying to select non-X11 DDB into an X11 dc\n");
84 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
87 dc->totalExtent.left = 0;
88 dc->totalExtent.top = 0;
89 dc->totalExtent.right = bmp->bitmap.bmWidth;
90 dc->totalExtent.bottom = bmp->bitmap.bmHeight;
92 physDev->drawable = (Pixmap)bmp->physBitmap;
93 dc->hBitmap = hbitmap;
95 SelectVisRgn16( dc->hSelf, hrgn );
98 /* Change GC depth if needed */
100 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
103 XFreeGC( gdi_display, physDev->gc );
104 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
105 XSetGraphicsExposures( gdi_display, physDev->gc, False );
106 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
107 XFlush( gdi_display );
109 dc->bitsPerPixel = bmp->bitmap.bmBitsPixel;
116 /****************************************************************************
118 * X11DRV_CreateBitmap
120 * Create a device dependent X11 bitmap
122 * Returns TRUE on success else FALSE
126 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
128 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
131 WARN("Bad bitmap handle %08x\n", hbitmap);
135 /* Check parameters */
136 if (bmp->bitmap.bmPlanes != 1)
138 GDI_ReleaseObj( hbitmap );
141 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
143 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
144 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
145 GDI_ReleaseObj( hbitmap );
149 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
150 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
152 /* Create the pixmap */
153 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
154 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
155 bmp->bitmap.bmBitsPixel)))
157 WARN("Can't create Pixmap\n");
158 GDI_ReleaseObj( hbitmap );
161 bmp->funcs = &X11DRV_DC_Funcs;
163 if (bmp->bitmap.bmBits) /* Set bitmap bits */
164 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
165 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
168 GDI_ReleaseObj( hbitmap );
173 /***********************************************************************
174 * X11DRV_GetBitmapBits
177 * Success: Number of bytes copied
180 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
182 LONG old_height, height;
184 LPBYTE tbuf, startline;
187 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
191 /* Hack: change the bitmap height temporarily to avoid */
192 /* getting unnecessary bitmap rows. */
194 old_height = bmp->bitmap.bmHeight;
195 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
197 image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
198 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
199 AllPlanes, ZPixmap );
200 bmp->bitmap.bmHeight = old_height;
202 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
205 switch (bmp->bitmap.bmBitsPixel)
208 for (h=0;h<height;h++)
212 for (w=0;w<bmp->bitmap.bmWidth;w++)
216 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
217 if ((w&7) == 7) ++tbuf;
219 startline += bmp->bitmap.bmWidthBytes;
223 for (h=0;h<height;h++)
226 for (w=0;w<bmp->bitmap.bmWidth;w++)
228 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
229 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
231 startline += bmp->bitmap.bmWidthBytes;
235 for (h=0;h<height;h++)
238 for (w=0;w<bmp->bitmap.bmWidth;w++)
239 *tbuf++ = XGetPixel(image,w,h);
240 startline += bmp->bitmap.bmWidthBytes;
245 for (h=0;h<height;h++)
248 for (w=0;w<bmp->bitmap.bmWidth;w++)
250 long pixel = XGetPixel(image,w,h);
252 *tbuf++ = pixel & 0xff;
253 *tbuf++ = (pixel>>8) & 0xff;
255 startline += bmp->bitmap.bmWidthBytes;
259 for (h=0;h<height;h++)
262 for (w=0;w<bmp->bitmap.bmWidth;w++)
264 long pixel = XGetPixel(image,w,h);
266 *tbuf++ = pixel & 0xff;
267 *tbuf++ = (pixel>> 8) & 0xff;
268 *tbuf++ = (pixel>>16) & 0xff;
270 startline += bmp->bitmap.bmWidthBytes;
275 for (h=0;h<height;h++)
278 for (w=0;w<bmp->bitmap.bmWidth;w++)
280 long pixel = XGetPixel(image,w,h);
282 *tbuf++ = pixel & 0xff;
283 *tbuf++ = (pixel>> 8) & 0xff;
284 *tbuf++ = (pixel>>16) & 0xff;
285 *tbuf++ = (pixel>>24) & 0xff;
287 startline += bmp->bitmap.bmWidthBytes;
291 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
293 XDestroyImage( image );
300 /******************************************************************************
301 * X11DRV_SetBitmapBits
304 * Success: Number of bytes used in setting the bitmap bits
307 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
311 LPBYTE sbuf, startline;
314 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
316 height = count / bmp->bitmap.bmWidthBytes;
319 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
320 bmp->bitmap.bmWidth, height, 32, 0 );
321 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
323 WARN("No memory to create image data.\n");
324 XDestroyImage( image );
329 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
333 switch (bmp->bitmap.bmBitsPixel)
336 for (h=0;h<height;h++)
339 for (w=0;w<bmp->bitmap.bmWidth;w++)
341 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
345 startline += bmp->bitmap.bmWidthBytes;
349 for (h=0;h<height;h++)
352 for (w=0;w<bmp->bitmap.bmWidth;w++)
354 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
355 else XPutPixel( image, w, h, *sbuf++ & 0xf );
357 startline += bmp->bitmap.bmWidthBytes;
361 for (h=0;h<height;h++)
364 for (w=0;w<bmp->bitmap.bmWidth;w++)
365 XPutPixel(image,w,h,*sbuf++);
366 startline += bmp->bitmap.bmWidthBytes;
371 for (h=0;h<height;h++)
374 for (w=0;w<bmp->bitmap.bmWidth;w++)
376 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
379 startline += bmp->bitmap.bmWidthBytes;
383 for (h=0;h<height;h++)
386 for (w=0;w<bmp->bitmap.bmWidth;w++)
388 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
391 startline += bmp->bitmap.bmWidthBytes;
395 for (h=0;h<height;h++)
398 for (w=0;w<bmp->bitmap.bmWidth;w++)
400 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
403 startline += bmp->bitmap.bmWidthBytes;
407 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
410 XPutImage( gdi_display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
411 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
412 XDestroyImage( image ); /* frees image->data too */
417 /***********************************************************************
420 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
422 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
425 WARN("Bad bitmap handle %08x\n", hbitmap);
430 ret = X11DRV_GetBitmapBits(bmp, bits, count);
431 else if(flags == DDB_SET)
432 ret = X11DRV_SetBitmapBits(bmp, bits, count);
434 ERR("Unknown flags value %d\n", flags);
437 GDI_ReleaseObj( hbitmap );
441 /***********************************************************************
442 * X11DRV_BITMAP_DeleteObject
444 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
446 TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
447 bmp->physBitmap = NULL;
452 /**************************************************************************
453 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
455 * Allocates an HBITMAP which references the Pixmap passed in.
456 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
457 * calling DeleteObject on this will free the Pixmap as well.
459 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
462 BITMAPOBJ *pBmp = NULL;
464 int x,y; /* Unused */
465 unsigned border_width; /* Unused */
466 unsigned int depth, width, height;
468 /* Get the Pixmap dimensions and bit depth */
469 if ( 0 == TSXGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
470 &border_width, &depth) )
473 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
474 width, height, depth);
477 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
478 * and make it a container for the pixmap passed.
480 hBmp = CreateBitmap( width, height, 1, depth, NULL );
482 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
484 pBmp->funcs = &X11DRV_DC_Funcs;
485 pBmp->physBitmap = (void *)pixmap;
486 GDI_ReleaseObj( hBmp );
489 TRACE("\tReturning HBITMAP %x\n", hBmp);
494 /**************************************************************************
495 * X11DRV_BITMAP_CreateBitmapFromPixmap
497 * Allocates an HBITMAP and copies the Pixmap data into it.
498 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
500 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
502 HBITMAP hBmp = 0, hBmpCopy = 0;
503 BITMAPOBJ *pBmp = NULL;
504 unsigned int width, height;
506 /* Allocate an HBITMAP which references the Pixmap passed to us */
507 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
510 TRACE("\tCould not create bitmap header for Pixmap\n");
514 /* Get the bitmap dimensions */
515 width = pBmp->bitmap.bmWidth;
516 height = pBmp->bitmap.bmHeight;
518 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
520 /* We can now get rid of the HBITMAP wrapper we created earlier.
521 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
525 /* Manually clear the bitmap internals to prevent the Pixmap
526 * from being deleted by DeleteObject.
528 pBmp->physBitmap = NULL;
534 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
539 /**************************************************************************
540 * X11DRV_BITMAP_CreatePixmapFromBitmap
542 * Creates a Pixmap from a bitmap
544 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
546 HGLOBAL hPackedDIB = 0;
550 * Create a packed DIB from the bitmap passed to us.
551 * A packed DIB contains a BITMAPINFO structure followed immediately by
552 * an optional color palette and the pixel data.
554 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
556 /* Create a Pixmap from the packed DIB */
557 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
559 /* Free the temporary packed DIB */
560 GlobalFree(hPackedDIB);
566 /***********************************************************************
567 * X11DRV_BITMAP_Pixmap
569 * This function exists solely for x11 driver of the window system.
571 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
574 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
575 pixmap = (Pixmap)bmp->physBitmap;
576 GDI_ReleaseObj( hbitmap );