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;
28 extern const DC_FUNCTIONS *X11DRV_DC_Funcs; /* hack */
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;
72 if (!(dc->flags & DC_MEMORY)) return 0;
73 if (hbitmap == dc->hBitmap) return hbitmap; /* nothing to do */
74 if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
76 if (bmp->header.dwCount && (hbitmap != GetStockObject(DEFAULT_BITMAP)))
78 WARN( "Bitmap already selected in another DC\n" );
79 GDI_ReleaseObj( hbitmap );
85 if(!X11DRV_CreateBitmap(hbitmap))
87 GDI_ReleaseObj( hbitmap );
92 if(bmp->funcs != dc->funcs) {
93 WARN("Trying to select non-X11 DDB into an X11 dc\n");
94 GDI_ReleaseObj( hbitmap );
98 if (!(hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight)))
100 GDI_ReleaseObj( hbitmap );
104 dc->totalExtent.left = 0;
105 dc->totalExtent.top = 0;
106 dc->totalExtent.right = bmp->bitmap.bmWidth;
107 dc->totalExtent.bottom = bmp->bitmap.bmHeight;
109 physDev->drawable = (Pixmap)bmp->physBitmap;
110 dc->hBitmap = hbitmap;
112 SelectVisRgn16( dc->hSelf, hrgn );
113 DeleteObject( hrgn );
115 /* Change GC depth if needed */
117 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
120 XFreeGC( gdi_display, physDev->gc );
121 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
122 XSetGraphicsExposures( gdi_display, physDev->gc, False );
123 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
124 XFlush( gdi_display );
126 dc->bitsPerPixel = bmp->bitmap.bmBitsPixel;
129 GDI_ReleaseObj( hbitmap );
134 /****************************************************************************
136 * X11DRV_CreateBitmap
138 * Create a device dependent X11 bitmap
140 * Returns TRUE on success else FALSE
144 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
146 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
149 WARN("Bad bitmap handle %08x\n", hbitmap);
153 /* Check parameters */
154 if (bmp->bitmap.bmPlanes != 1)
156 GDI_ReleaseObj( hbitmap );
159 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
161 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
162 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
163 GDI_ReleaseObj( hbitmap );
167 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
168 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
170 /* Create the pixmap */
171 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
172 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
173 bmp->bitmap.bmBitsPixel)))
175 WARN("Can't create Pixmap\n");
176 GDI_ReleaseObj( hbitmap );
179 bmp->funcs = X11DRV_DC_Funcs;
181 if (bmp->bitmap.bmBits) /* Set bitmap bits */
182 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
183 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
186 GDI_ReleaseObj( hbitmap );
191 /***********************************************************************
192 * X11DRV_GetBitmapBits
195 * Success: Number of bytes copied
198 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
200 LONG old_height, height;
202 LPBYTE tbuf, startline;
205 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
209 /* Hack: change the bitmap height temporarily to avoid */
210 /* getting unnecessary bitmap rows. */
212 old_height = bmp->bitmap.bmHeight;
213 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
215 image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
216 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
217 AllPlanes, ZPixmap );
218 bmp->bitmap.bmHeight = old_height;
220 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
223 switch (bmp->bitmap.bmBitsPixel)
226 for (h=0;h<height;h++)
230 for (w=0;w<bmp->bitmap.bmWidth;w++)
234 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
235 if ((w&7) == 7) ++tbuf;
237 startline += bmp->bitmap.bmWidthBytes;
241 for (h=0;h<height;h++)
244 for (w=0;w<bmp->bitmap.bmWidth;w++)
246 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
247 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
249 startline += bmp->bitmap.bmWidthBytes;
253 for (h=0;h<height;h++)
256 for (w=0;w<bmp->bitmap.bmWidth;w++)
257 *tbuf++ = XGetPixel(image,w,h);
258 startline += bmp->bitmap.bmWidthBytes;
263 for (h=0;h<height;h++)
266 for (w=0;w<bmp->bitmap.bmWidth;w++)
268 long pixel = XGetPixel(image,w,h);
270 *tbuf++ = pixel & 0xff;
271 *tbuf++ = (pixel>>8) & 0xff;
273 startline += bmp->bitmap.bmWidthBytes;
277 for (h=0;h<height;h++)
280 for (w=0;w<bmp->bitmap.bmWidth;w++)
282 long pixel = XGetPixel(image,w,h);
284 *tbuf++ = pixel & 0xff;
285 *tbuf++ = (pixel>> 8) & 0xff;
286 *tbuf++ = (pixel>>16) & 0xff;
288 startline += bmp->bitmap.bmWidthBytes;
293 for (h=0;h<height;h++)
296 for (w=0;w<bmp->bitmap.bmWidth;w++)
298 long pixel = XGetPixel(image,w,h);
300 *tbuf++ = pixel & 0xff;
301 *tbuf++ = (pixel>> 8) & 0xff;
302 *tbuf++ = (pixel>>16) & 0xff;
303 *tbuf++ = (pixel>>24) & 0xff;
305 startline += bmp->bitmap.bmWidthBytes;
309 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
311 XDestroyImage( image );
318 /******************************************************************************
319 * X11DRV_SetBitmapBits
322 * Success: Number of bytes used in setting the bitmap bits
325 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
329 LPBYTE sbuf, startline;
332 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
334 height = count / bmp->bitmap.bmWidthBytes;
337 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
338 bmp->bitmap.bmWidth, height, 32, 0 );
339 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
341 WARN("No memory to create image data.\n");
342 XDestroyImage( image );
347 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
351 switch (bmp->bitmap.bmBitsPixel)
354 for (h=0;h<height;h++)
357 for (w=0;w<bmp->bitmap.bmWidth;w++)
359 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
363 startline += bmp->bitmap.bmWidthBytes;
367 for (h=0;h<height;h++)
370 for (w=0;w<bmp->bitmap.bmWidth;w++)
372 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
373 else XPutPixel( image, w, h, *sbuf++ & 0xf );
375 startline += bmp->bitmap.bmWidthBytes;
379 for (h=0;h<height;h++)
382 for (w=0;w<bmp->bitmap.bmWidth;w++)
383 XPutPixel(image,w,h,*sbuf++);
384 startline += bmp->bitmap.bmWidthBytes;
389 for (h=0;h<height;h++)
392 for (w=0;w<bmp->bitmap.bmWidth;w++)
394 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
397 startline += bmp->bitmap.bmWidthBytes;
401 for (h=0;h<height;h++)
404 for (w=0;w<bmp->bitmap.bmWidth;w++)
406 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
409 startline += bmp->bitmap.bmWidthBytes;
413 for (h=0;h<height;h++)
416 for (w=0;w<bmp->bitmap.bmWidth;w++)
418 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
421 startline += bmp->bitmap.bmWidthBytes;
425 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
428 XPutImage( gdi_display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
429 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
430 XDestroyImage( image ); /* frees image->data too */
435 /***********************************************************************
438 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
440 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
443 WARN("Bad bitmap handle %08x\n", hbitmap);
448 ret = X11DRV_GetBitmapBits(bmp, bits, count);
449 else if(flags == DDB_SET)
450 ret = X11DRV_SetBitmapBits(bmp, bits, count);
452 ERR("Unknown flags value %d\n", flags);
455 GDI_ReleaseObj( hbitmap );
459 /***********************************************************************
460 * X11DRV_BITMAP_DeleteObject
462 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap )
464 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
467 TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
468 bmp->physBitmap = NULL;
470 GDI_ReleaseObj( hbitmap );
475 /**************************************************************************
476 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
478 * Allocates an HBITMAP which references the Pixmap passed in.
479 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
480 * calling DeleteObject on this will free the Pixmap as well.
482 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
485 BITMAPOBJ *pBmp = NULL;
487 int x,y; /* Unused */
488 unsigned border_width; /* Unused */
489 unsigned int depth, width, height;
491 /* Get the Pixmap dimensions and bit depth */
492 if ( 0 == TSXGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
493 &border_width, &depth) )
496 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
497 width, height, depth);
500 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
501 * and make it a container for the pixmap passed.
503 hBmp = CreateBitmap( width, height, 1, depth, NULL );
505 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
507 pBmp->funcs = X11DRV_DC_Funcs;
508 pBmp->physBitmap = (void *)pixmap;
509 GDI_ReleaseObj( hBmp );
512 TRACE("\tReturning HBITMAP %x\n", hBmp);
517 /**************************************************************************
518 * X11DRV_BITMAP_CreateBitmapFromPixmap
520 * Allocates an HBITMAP and copies the Pixmap data into it.
521 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
523 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
525 HBITMAP hBmp = 0, hBmpCopy = 0;
526 BITMAPOBJ *pBmp = NULL;
527 unsigned int width, height;
529 /* Allocate an HBITMAP which references the Pixmap passed to us */
530 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
533 TRACE("\tCould not create bitmap header for Pixmap\n");
537 /* Get the bitmap dimensions */
538 width = pBmp->bitmap.bmWidth;
539 height = pBmp->bitmap.bmHeight;
541 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
543 /* We can now get rid of the HBITMAP wrapper we created earlier.
544 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
548 /* Manually clear the bitmap internals to prevent the Pixmap
549 * from being deleted by DeleteObject.
551 pBmp->physBitmap = NULL;
557 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
562 /**************************************************************************
563 * X11DRV_BITMAP_CreatePixmapFromBitmap
565 * Creates a Pixmap from a bitmap
567 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
569 HGLOBAL hPackedDIB = 0;
573 * Create a packed DIB from the bitmap passed to us.
574 * A packed DIB contains a BITMAPINFO structure followed immediately by
575 * an optional color palette and the pixel data.
577 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
579 /* Create a Pixmap from the packed DIB */
580 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
582 /* Free the temporary packed DIB */
583 GlobalFree(hPackedDIB);
589 /***********************************************************************
590 * X11DRV_BITMAP_Pixmap
592 * This function exists solely for x11 driver of the window system.
594 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
597 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
599 pixmap = (Pixmap)bmp->physBitmap;
600 GDI_ReleaseObj( hbitmap );
603 ERR("handle %08x returned no obj\n", hbitmap);