2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
20 #include "debugtools.h"
25 #include "wine/winuser16.h"
27 DEFAULT_DEBUG_CHANNEL(x11drv);
29 /* GCs used for B&W and color bitmap operations */
30 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
33 /***********************************************************************
36 BOOL X11DRV_BITMAP_Init(void)
40 /* Create the necessary GCs */
42 if ((tmpPixmap = TSXCreatePixmap(display,
43 X11DRV_GetXRootWindow(),
47 BITMAP_monoGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
48 TSXSetGraphicsExposures( display, BITMAP_monoGC, False );
49 TSXFreePixmap( display, tmpPixmap );
52 if (X11DRV_GetDepth() != 1)
54 if ((tmpPixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
55 1, 1, X11DRV_GetDepth())))
57 BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
58 TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
59 TSXFreePixmap( display, tmpPixmap );
65 /***********************************************************************
66 * X11DRV_BITMAP_SelectObject
68 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
72 HBITMAP prevHandle = dc->w.hBitmap;
73 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
76 if (!(dc->w.flags & DC_MEMORY)) return 0;
79 if(!X11DRV_CreateBitmap(hbitmap))
82 if(bmp->funcs != dc->funcs) {
83 WARN("Trying to select non-X11 DDB into an X11 dc\n");
87 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
90 dc->w.totalExtent.left = 0;
91 dc->w.totalExtent.top = 0;
92 dc->w.totalExtent.right = bmp->bitmap.bmWidth;
93 dc->w.totalExtent.bottom = bmp->bitmap.bmHeight;
95 physDev->drawable = (Pixmap)bmp->physBitmap;
96 dc->w.hBitmap = hbitmap;
98 SelectVisRgn16( dc->hSelf, hrgn );
101 /* Change GC depth if needed */
103 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
105 TSXFreeGC( display, physDev->gc );
106 physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
107 TSXSetGraphicsExposures( display, physDev->gc, False );
108 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
115 /***********************************************************************
118 * Wrapper to call XPutImage with CALL_LARGE_STACK.
121 struct XPutImage_descr
129 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
131 return XPutImage( display, (Pixmap)descr->bmp->physBitmap, BITMAP_GC(descr->bmp),
132 descr->image, 0, 0, 0, 0, descr->width, descr->height );
136 /****************************************************************************
138 * X11DRV_CreateBitmap
140 * Create a device dependent X11 bitmap
142 * Returns TRUE on success else FALSE
146 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
148 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
151 WARN("Bad bitmap handle %08x\n", hbitmap);
155 /* Check parameters */
156 if (bmp->bitmap.bmPlanes != 1)
158 GDI_ReleaseObj( hbitmap );
161 if ((bmp->bitmap.bmBitsPixel != 1) &&
162 (bmp->bitmap.bmBitsPixel != X11DRV_GetDepth()))
164 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
165 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
166 GDI_ReleaseObj( hbitmap );
170 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
171 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
173 /* Create the pixmap */
174 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
175 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
176 bmp->bitmap.bmBitsPixel)))
178 WARN("Can't create Pixmap\n");
179 GDI_ReleaseObj( hbitmap );
182 bmp->funcs = &X11DRV_DC_Funcs;
184 if (bmp->bitmap.bmBits) /* Set bitmap bits */
185 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
186 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
189 GDI_ReleaseObj( hbitmap );
194 /***********************************************************************
195 * X11DRV_BITMAP_GetXImage
197 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
199 XImage *X11DRV_BITMAP_GetXImage( const BITMAPOBJ *bmp )
201 return XGetImage( display, (Pixmap)bmp->physBitmap,
202 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
203 AllPlanes, ZPixmap );
207 /***********************************************************************
208 * X11DRV_GetBitmapBits
211 * Success: Number of bytes copied
214 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
216 LONG old_height, height;
218 LPBYTE tbuf, startline;
221 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
223 EnterCriticalSection( &X11DRV_CritSection );
225 /* Hack: change the bitmap height temporarily to avoid */
226 /* getting unnecessary bitmap rows. */
228 old_height = bmp->bitmap.bmHeight;
229 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
231 image = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
233 bmp->bitmap.bmHeight = old_height;
235 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
238 switch (bmp->bitmap.bmBitsPixel)
241 for (h=0;h<height;h++)
245 for (w=0;w<bmp->bitmap.bmWidth;w++)
249 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
250 if ((w&7) == 7) ++tbuf;
252 startline += bmp->bitmap.bmWidthBytes;
256 for (h=0;h<height;h++)
259 for (w=0;w<bmp->bitmap.bmWidth;w++)
261 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
262 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
264 startline += bmp->bitmap.bmWidthBytes;
268 for (h=0;h<height;h++)
271 for (w=0;w<bmp->bitmap.bmWidth;w++)
272 *tbuf++ = XGetPixel(image,w,h);
273 startline += bmp->bitmap.bmWidthBytes;
278 for (h=0;h<height;h++)
281 for (w=0;w<bmp->bitmap.bmWidth;w++)
283 long pixel = XGetPixel(image,w,h);
285 *tbuf++ = pixel & 0xff;
286 *tbuf++ = (pixel>>8) & 0xff;
288 startline += bmp->bitmap.bmWidthBytes;
292 for (h=0;h<height;h++)
295 for (w=0;w<bmp->bitmap.bmWidth;w++)
297 long pixel = XGetPixel(image,w,h);
299 *tbuf++ = pixel & 0xff;
300 *tbuf++ = (pixel>> 8) & 0xff;
301 *tbuf++ = (pixel>>16) & 0xff;
303 startline += bmp->bitmap.bmWidthBytes;
308 for (h=0;h<height;h++)
311 for (w=0;w<bmp->bitmap.bmWidth;w++)
313 long pixel = XGetPixel(image,w,h);
315 *tbuf++ = pixel & 0xff;
316 *tbuf++ = (pixel>> 8) & 0xff;
317 *tbuf++ = (pixel>>16) & 0xff;
318 *tbuf++ = (pixel>>24) & 0xff;
320 startline += bmp->bitmap.bmWidthBytes;
324 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
326 XDestroyImage( image );
327 LeaveCriticalSection( &X11DRV_CritSection );
334 /******************************************************************************
335 * X11DRV_SetBitmapBits
338 * Success: Number of bytes used in setting the bitmap bits
341 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
343 struct XPutImage_descr descr;
346 LPBYTE sbuf, startline;
349 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
351 height = count / bmp->bitmap.bmWidthBytes;
353 EnterCriticalSection( &X11DRV_CritSection );
354 image = XCreateImage( display, X11DRV_GetVisual(), bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
355 bmp->bitmap.bmWidth, height, 32, 0 );
356 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
358 WARN("No memory to create image data.\n");
359 XDestroyImage( image );
360 LeaveCriticalSection( &X11DRV_CritSection );
364 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
368 switch (bmp->bitmap.bmBitsPixel)
371 for (h=0;h<height;h++)
374 for (w=0;w<bmp->bitmap.bmWidth;w++)
376 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
380 startline += bmp->bitmap.bmWidthBytes;
384 for (h=0;h<height;h++)
387 for (w=0;w<bmp->bitmap.bmWidth;w++)
389 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
390 else XPutPixel( image, w, h, *sbuf++ & 0xf );
392 startline += bmp->bitmap.bmWidthBytes;
396 for (h=0;h<height;h++)
399 for (w=0;w<bmp->bitmap.bmWidth;w++)
400 XPutPixel(image,w,h,*sbuf++);
401 startline += bmp->bitmap.bmWidthBytes;
406 for (h=0;h<height;h++)
409 for (w=0;w<bmp->bitmap.bmWidth;w++)
411 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
414 startline += bmp->bitmap.bmWidthBytes;
418 for (h=0;h<height;h++)
421 for (w=0;w<bmp->bitmap.bmWidth;w++)
423 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
426 startline += bmp->bitmap.bmWidthBytes;
430 for (h=0;h<height;h++)
433 for (w=0;w<bmp->bitmap.bmWidth;w++)
435 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
438 startline += bmp->bitmap.bmWidthBytes;
442 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
448 descr.width = bmp->bitmap.bmWidth;
449 descr.height = height;
451 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
452 XDestroyImage( image ); /* frees image->data too */
453 LeaveCriticalSection( &X11DRV_CritSection );
458 /***********************************************************************
461 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
463 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
466 WARN("Bad bitmap handle %08x\n", hbitmap);
471 ret = X11DRV_GetBitmapBits(bmp, bits, count);
472 else if(flags == DDB_SET)
473 ret = X11DRV_SetBitmapBits(bmp, bits, count);
475 ERR("Unknown flags value %d\n", flags);
478 GDI_ReleaseObj( hbitmap );
482 /***********************************************************************
483 * X11DRV_BITMAP_DeleteObject
485 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
487 TSXFreePixmap( display, (Pixmap)bmp->physBitmap );
488 bmp->physBitmap = NULL;
493 /**************************************************************************
494 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
496 * Allocates an HBITMAP which references the Pixmap passed in.
497 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
498 * calling DeleteObject on this will free the Pixmap as well.
500 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
503 BITMAPOBJ *pBmp = NULL;
505 int x,y; /* Unused */
506 unsigned border_width; /* Unused */
507 unsigned int depth, width, height;
509 /* Get the Pixmap dimensions and bit depth */
510 if ( 0 == TSXGetGeometry(display, pixmap, &root, &x, &y, &width, &height,
511 &border_width, &depth) )
514 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
515 width, height, depth);
518 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
519 * and make it a container for the pixmap passed.
521 hBmp = CreateBitmap( width, height, 1, depth, NULL );
523 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
525 pBmp->funcs = &X11DRV_DC_Funcs;
526 pBmp->physBitmap = (void *)pixmap;
527 GDI_ReleaseObj( hBmp );
530 TRACE("\tReturning HBITMAP %x\n", hBmp);
535 /**************************************************************************
536 * X11DRV_BITMAP_CreateBitmapFromPixmap
538 * Allocates an HBITMAP and copies the Pixmap data into it.
539 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
541 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
543 HBITMAP hBmp = 0, hBmpCopy = 0;
544 BITMAPOBJ *pBmp = NULL;
545 unsigned int width, height;
547 /* Allocate an HBITMAP which references the Pixmap passed to us */
548 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
551 TRACE("\tCould not create bitmap header for Pixmap\n");
555 /* Get the bitmap dimensions */
556 width = pBmp->bitmap.bmWidth;
557 height = pBmp->bitmap.bmHeight;
559 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
561 /* We can now get rid of the HBITMAP wrapper we created earlier.
562 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
566 /* Manually clear the bitmap internals to prevent the Pixmap
567 * from being deleted by DeleteObject.
569 pBmp->physBitmap = NULL;
575 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
580 /**************************************************************************
581 * X11DRV_BITMAP_CreatePixmapFromBitmap
583 * Creates a Pixmap from a bitmap
585 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
587 HGLOBAL hPackedDIB = 0;
591 * Create a packed DIB from the bitmap passed to us.
592 * A packed DIB contains a BITMAPINFO structure followed immediately by
593 * an optional color palette and the pixel data.
595 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
597 /* Create a Pixmap from the packed DIB */
598 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
600 /* Free the temporary packed DIB */
601 GlobalFree(hPackedDIB);
607 /***********************************************************************
608 * X11DRV_BITMAP_Pixmap
610 * This function exists solely for x11 driver of the window system.
612 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
615 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
616 pixmap = (Pixmap)bmp->physBitmap;
617 GDI_ReleaseObj( hbitmap );