2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/debug.h"
34 #include "wine/winuser16.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
38 /* GCs used for B&W and color bitmap operations */
39 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
40 Pixmap BITMAP_stock_pixmap; /* pixmap for the default stock bitmap */
42 extern const DC_FUNCTIONS *X11DRV_DC_Funcs; /* hack */
44 /***********************************************************************
47 BOOL X11DRV_BITMAP_Init(void)
51 /* Create the necessary GCs */
54 BITMAP_stock_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
55 BITMAP_monoGC = XCreateGC( gdi_display, BITMAP_stock_pixmap, 0, NULL );
56 XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
57 XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
59 if (screen_depth != 1)
61 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
63 BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
64 XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
65 XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
66 XFreePixmap( gdi_display, tmpPixmap );
73 /***********************************************************************
74 * SelectBitmap (X11DRV.@)
76 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
81 if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
84 X11DRV_XRender_UpdateDrawable( physDev );
86 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
87 physDev->drawable = BITMAP_stock_pixmap;
89 physDev->drawable = (Pixmap)bmp->physBitmap;
91 /* Change GC depth if needed */
93 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
96 XFreeGC( gdi_display, physDev->gc );
97 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
98 XSetGraphicsExposures( gdi_display, physDev->gc, False );
99 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
100 XFlush( gdi_display );
103 GDI_ReleaseObj( hbitmap );
108 /****************************************************************************
109 * CreateBitmap (X11DRV.@)
111 * Create a device dependent X11 bitmap
113 * Returns TRUE on success else FALSE
115 BOOL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
117 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
120 WARN("Bad bitmap handle %p\n", hbitmap);
124 /* Check parameters */
125 if (bmp->bitmap.bmPlanes != 1)
127 GDI_ReleaseObj( hbitmap );
130 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
132 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
133 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
134 GDI_ReleaseObj( hbitmap );
137 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
139 ERR( "called for stock bitmap, please report\n" );
140 GDI_ReleaseObj( hbitmap );
144 TRACE("(%p) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
145 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
147 /* Create the pixmap */
148 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
149 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
150 bmp->bitmap.bmBitsPixel)))
152 WARN("Can't create Pixmap\n");
153 GDI_ReleaseObj( hbitmap );
157 if (bmp->bitmap.bmBits) /* Set bitmap bits */
158 X11DRV_SetBitmapBits( hbitmap, bmp->bitmap.bmBits,
159 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes );
161 GDI_ReleaseObj( hbitmap );
166 /***********************************************************************
167 * GetBitmapBits (X11DRV.@)
170 * Success: Number of bytes copied
173 LONG X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
175 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
176 LONG old_height, height;
178 LPBYTE tbuf, startline;
182 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
186 /* Hack: change the bitmap height temporarily to avoid */
187 /* getting unnecessary bitmap rows. */
189 old_height = bmp->bitmap.bmHeight;
190 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
192 image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
193 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
194 AllPlanes, ZPixmap );
195 bmp->bitmap.bmHeight = old_height;
197 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
200 switch (bmp->bitmap.bmBitsPixel)
203 for (h=0;h<height;h++)
207 for (w=0;w<bmp->bitmap.bmWidth;w++)
211 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
212 if ((w&7) == 7) ++tbuf;
214 startline += bmp->bitmap.bmWidthBytes;
218 for (h=0;h<height;h++)
221 for (w=0;w<bmp->bitmap.bmWidth;w++)
223 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
224 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
226 startline += bmp->bitmap.bmWidthBytes;
230 for (h=0;h<height;h++)
233 for (w=0;w<bmp->bitmap.bmWidth;w++)
234 *tbuf++ = XGetPixel(image,w,h);
235 startline += bmp->bitmap.bmWidthBytes;
240 for (h=0;h<height;h++)
243 for (w=0;w<bmp->bitmap.bmWidth;w++)
245 long pixel = XGetPixel(image,w,h);
247 *tbuf++ = pixel & 0xff;
248 *tbuf++ = (pixel>>8) & 0xff;
250 startline += bmp->bitmap.bmWidthBytes;
254 for (h=0;h<height;h++)
257 for (w=0;w<bmp->bitmap.bmWidth;w++)
259 long pixel = XGetPixel(image,w,h);
261 *tbuf++ = pixel & 0xff;
262 *tbuf++ = (pixel>> 8) & 0xff;
263 *tbuf++ = (pixel>>16) & 0xff;
265 startline += bmp->bitmap.bmWidthBytes;
270 for (h=0;h<height;h++)
273 for (w=0;w<bmp->bitmap.bmWidth;w++)
275 long pixel = XGetPixel(image,w,h);
277 *tbuf++ = pixel & 0xff;
278 *tbuf++ = (pixel>> 8) & 0xff;
279 *tbuf++ = (pixel>>16) & 0xff;
280 *tbuf++ = (pixel>>24) & 0xff;
282 startline += bmp->bitmap.bmWidthBytes;
286 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
288 XDestroyImage( image );
290 GDI_ReleaseObj( hbitmap );
296 /******************************************************************************
297 * SetBitmapBits (X11DRV.@)
300 * Success: Number of bytes used in setting the bitmap bits
303 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
305 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
308 const BYTE *sbuf, *startline;
312 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
314 height = count / bmp->bitmap.bmWidthBytes;
317 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
318 bmp->bitmap.bmWidth, height, 32, 0 );
319 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
321 WARN("No memory to create image data.\n");
322 XDestroyImage( image );
324 GDI_ReleaseObj( hbitmap );
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 */
413 GDI_ReleaseObj( hbitmap );
417 /***********************************************************************
418 * DeleteBitmap (X11DRV.@)
420 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
422 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
425 if (bmp->physBitmap) TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
426 bmp->physBitmap = NULL;
427 if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
428 GDI_ReleaseObj( hbitmap );
433 /**************************************************************************
434 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
436 * Allocates an HBITMAP which references the Pixmap passed in.
437 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
438 * calling DeleteObject on this will free the Pixmap as well.
440 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
443 BITMAPOBJ *pBmp = NULL;
445 int x,y; /* Unused */
446 unsigned border_width; /* Unused */
447 unsigned int depth, width, height;
449 /* Get the Pixmap dimensions and bit depth */
450 if ( 0 == TSXGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
451 &border_width, &depth) )
454 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
455 width, height, depth);
458 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
459 * and make it a container for the pixmap passed.
461 hBmp = CreateBitmap( width, height, 1, depth, NULL );
463 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
465 pBmp->funcs = X11DRV_DC_Funcs;
466 pBmp->physBitmap = (void *)pixmap;
467 GDI_ReleaseObj( hBmp );
470 TRACE("\tReturning HBITMAP %p\n", hBmp);
475 /**************************************************************************
476 * X11DRV_BITMAP_CreateBitmapFromPixmap
478 * Allocates an HBITMAP and copies the Pixmap data into it.
479 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
481 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
483 HBITMAP hBmp = 0, hBmpCopy = 0;
484 BITMAPOBJ *pBmp = NULL;
485 unsigned int width, height;
487 /* Allocate an HBITMAP which references the Pixmap passed to us */
488 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
491 TRACE("\tCould not create bitmap header for Pixmap\n");
495 /* Get the bitmap dimensions */
496 width = pBmp->bitmap.bmWidth;
497 height = pBmp->bitmap.bmHeight;
499 hBmpCopy = (HBITMAP)CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
501 /* We can now get rid of the HBITMAP wrapper we created earlier.
502 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
506 /* Manually clear the bitmap internals to prevent the Pixmap
507 * from being deleted by DeleteObject.
509 pBmp->physBitmap = NULL;
515 TRACE("\tReturning HBITMAP %p\n", hBmpCopy);
520 /**************************************************************************
521 * X11DRV_BITMAP_CreatePixmapFromBitmap
523 * Creates a Pixmap from a bitmap
525 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
527 HGLOBAL hPackedDIB = 0;
531 * Create a packed DIB from the bitmap passed to us.
532 * A packed DIB contains a BITMAPINFO structure followed immediately by
533 * an optional color palette and the pixel data.
535 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
537 /* Create a Pixmap from the packed DIB */
538 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
540 /* Free the temporary packed DIB */
541 GlobalFree(hPackedDIB);
547 /***********************************************************************
548 * X11DRV_BITMAP_Pixmap
550 * This function exists solely for x11 driver of the window system.
552 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
555 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
557 pixmap = (Pixmap)bmp->physBitmap;
558 GDI_ReleaseObj( hbitmap );
561 ERR("handle %p returned no obj\n", hbitmap);