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 */
39 if ((tmpPixmap = TSXCreatePixmap(display,
40 X11DRV_GetXRootWindow(),
44 BITMAP_monoGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
45 TSXSetGraphicsExposures( display, BITMAP_monoGC, False );
46 TSXFreePixmap( display, tmpPixmap );
49 if (X11DRV_GetDepth() != 1)
51 if ((tmpPixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
52 1, 1, X11DRV_GetDepth())))
54 BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
55 TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
56 TSXFreePixmap( 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)
102 TSXFreeGC( display, physDev->gc );
103 physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
104 TSXSetGraphicsExposures( 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) &&
138 (bmp->bitmap.bmBitsPixel != X11DRV_GetDepth()))
140 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
141 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
142 GDI_ReleaseObj( hbitmap );
146 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
147 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
149 /* Create the pixmap */
150 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
151 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
152 bmp->bitmap.bmBitsPixel)))
154 WARN("Can't create Pixmap\n");
155 GDI_ReleaseObj( hbitmap );
158 bmp->funcs = &X11DRV_DC_Funcs;
160 if (bmp->bitmap.bmBits) /* Set bitmap bits */
161 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
162 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
165 GDI_ReleaseObj( hbitmap );
170 /***********************************************************************
171 * X11DRV_GetBitmapBits
174 * Success: Number of bytes copied
177 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
179 LONG old_height, height;
181 LPBYTE tbuf, startline;
184 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
188 /* Hack: change the bitmap height temporarily to avoid */
189 /* getting unnecessary bitmap rows. */
191 old_height = bmp->bitmap.bmHeight;
192 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
194 image = XGetImage( display, (Pixmap)bmp->physBitmap,
195 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
196 AllPlanes, ZPixmap );
197 bmp->bitmap.bmHeight = old_height;
199 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
202 switch (bmp->bitmap.bmBitsPixel)
205 for (h=0;h<height;h++)
209 for (w=0;w<bmp->bitmap.bmWidth;w++)
213 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
214 if ((w&7) == 7) ++tbuf;
216 startline += bmp->bitmap.bmWidthBytes;
220 for (h=0;h<height;h++)
223 for (w=0;w<bmp->bitmap.bmWidth;w++)
225 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
226 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
228 startline += bmp->bitmap.bmWidthBytes;
232 for (h=0;h<height;h++)
235 for (w=0;w<bmp->bitmap.bmWidth;w++)
236 *tbuf++ = XGetPixel(image,w,h);
237 startline += bmp->bitmap.bmWidthBytes;
242 for (h=0;h<height;h++)
245 for (w=0;w<bmp->bitmap.bmWidth;w++)
247 long pixel = XGetPixel(image,w,h);
249 *tbuf++ = pixel & 0xff;
250 *tbuf++ = (pixel>>8) & 0xff;
252 startline += bmp->bitmap.bmWidthBytes;
256 for (h=0;h<height;h++)
259 for (w=0;w<bmp->bitmap.bmWidth;w++)
261 long pixel = XGetPixel(image,w,h);
263 *tbuf++ = pixel & 0xff;
264 *tbuf++ = (pixel>> 8) & 0xff;
265 *tbuf++ = (pixel>>16) & 0xff;
267 startline += bmp->bitmap.bmWidthBytes;
272 for (h=0;h<height;h++)
275 for (w=0;w<bmp->bitmap.bmWidth;w++)
277 long pixel = XGetPixel(image,w,h);
279 *tbuf++ = pixel & 0xff;
280 *tbuf++ = (pixel>> 8) & 0xff;
281 *tbuf++ = (pixel>>16) & 0xff;
282 *tbuf++ = (pixel>>24) & 0xff;
284 startline += bmp->bitmap.bmWidthBytes;
288 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
290 XDestroyImage( image );
297 /******************************************************************************
298 * X11DRV_SetBitmapBits
301 * Success: Number of bytes used in setting the bitmap bits
304 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
308 LPBYTE sbuf, startline;
311 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
313 height = count / bmp->bitmap.bmWidthBytes;
316 image = XCreateImage( display, X11DRV_GetVisual(), bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
317 bmp->bitmap.bmWidth, height, 32, 0 );
318 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
320 WARN("No memory to create image data.\n");
321 XDestroyImage( image );
326 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
330 switch (bmp->bitmap.bmBitsPixel)
333 for (h=0;h<height;h++)
336 for (w=0;w<bmp->bitmap.bmWidth;w++)
338 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
342 startline += bmp->bitmap.bmWidthBytes;
346 for (h=0;h<height;h++)
349 for (w=0;w<bmp->bitmap.bmWidth;w++)
351 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
352 else XPutPixel( image, w, h, *sbuf++ & 0xf );
354 startline += bmp->bitmap.bmWidthBytes;
358 for (h=0;h<height;h++)
361 for (w=0;w<bmp->bitmap.bmWidth;w++)
362 XPutPixel(image,w,h,*sbuf++);
363 startline += bmp->bitmap.bmWidthBytes;
368 for (h=0;h<height;h++)
371 for (w=0;w<bmp->bitmap.bmWidth;w++)
373 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
376 startline += bmp->bitmap.bmWidthBytes;
380 for (h=0;h<height;h++)
383 for (w=0;w<bmp->bitmap.bmWidth;w++)
385 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
388 startline += bmp->bitmap.bmWidthBytes;
392 for (h=0;h<height;h++)
395 for (w=0;w<bmp->bitmap.bmWidth;w++)
397 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
400 startline += bmp->bitmap.bmWidthBytes;
404 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
407 XPutImage( display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
408 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
409 XDestroyImage( image ); /* frees image->data too */
414 /***********************************************************************
417 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
419 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
422 WARN("Bad bitmap handle %08x\n", hbitmap);
427 ret = X11DRV_GetBitmapBits(bmp, bits, count);
428 else if(flags == DDB_SET)
429 ret = X11DRV_SetBitmapBits(bmp, bits, count);
431 ERR("Unknown flags value %d\n", flags);
434 GDI_ReleaseObj( hbitmap );
438 /***********************************************************************
439 * X11DRV_BITMAP_DeleteObject
441 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
443 TSXFreePixmap( display, (Pixmap)bmp->physBitmap );
444 bmp->physBitmap = NULL;
449 /**************************************************************************
450 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
452 * Allocates an HBITMAP which references the Pixmap passed in.
453 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
454 * calling DeleteObject on this will free the Pixmap as well.
456 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
459 BITMAPOBJ *pBmp = NULL;
461 int x,y; /* Unused */
462 unsigned border_width; /* Unused */
463 unsigned int depth, width, height;
465 /* Get the Pixmap dimensions and bit depth */
466 if ( 0 == TSXGetGeometry(display, pixmap, &root, &x, &y, &width, &height,
467 &border_width, &depth) )
470 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
471 width, height, depth);
474 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
475 * and make it a container for the pixmap passed.
477 hBmp = CreateBitmap( width, height, 1, depth, NULL );
479 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
481 pBmp->funcs = &X11DRV_DC_Funcs;
482 pBmp->physBitmap = (void *)pixmap;
483 GDI_ReleaseObj( hBmp );
486 TRACE("\tReturning HBITMAP %x\n", hBmp);
491 /**************************************************************************
492 * X11DRV_BITMAP_CreateBitmapFromPixmap
494 * Allocates an HBITMAP and copies the Pixmap data into it.
495 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
497 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
499 HBITMAP hBmp = 0, hBmpCopy = 0;
500 BITMAPOBJ *pBmp = NULL;
501 unsigned int width, height;
503 /* Allocate an HBITMAP which references the Pixmap passed to us */
504 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
507 TRACE("\tCould not create bitmap header for Pixmap\n");
511 /* Get the bitmap dimensions */
512 width = pBmp->bitmap.bmWidth;
513 height = pBmp->bitmap.bmHeight;
515 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
517 /* We can now get rid of the HBITMAP wrapper we created earlier.
518 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
522 /* Manually clear the bitmap internals to prevent the Pixmap
523 * from being deleted by DeleteObject.
525 pBmp->physBitmap = NULL;
531 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
536 /**************************************************************************
537 * X11DRV_BITMAP_CreatePixmapFromBitmap
539 * Creates a Pixmap from a bitmap
541 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
543 HGLOBAL hPackedDIB = 0;
547 * Create a packed DIB from the bitmap passed to us.
548 * A packed DIB contains a BITMAPINFO structure followed immediately by
549 * an optional color palette and the pixel data.
551 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
553 /* Create a Pixmap from the packed DIB */
554 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
556 /* Free the temporary packed DIB */
557 GlobalFree(hPackedDIB);
563 /***********************************************************************
564 * X11DRV_BITMAP_Pixmap
566 * This function exists solely for x11 driver of the window system.
568 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
571 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
572 pixmap = (Pixmap)bmp->physBitmap;
573 GDI_ReleaseObj( hbitmap );