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
28 #include "wine/debug.h"
32 #include "wine/winuser16.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
36 /* GCs used for B&W and color bitmap operations */
37 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
38 Pixmap BITMAP_stock_pixmap; /* pixmap for the default stock bitmap */
40 extern const DC_FUNCTIONS *X11DRV_DC_Funcs; /* hack */
42 /***********************************************************************
45 BOOL X11DRV_BITMAP_Init(void)
49 /* Create the necessary GCs */
52 BITMAP_stock_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
53 BITMAP_monoGC = XCreateGC( gdi_display, BITMAP_stock_pixmap, 0, NULL );
54 XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
55 XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
57 if (screen_depth != 1)
59 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
61 BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
62 XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
63 XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
64 XFreePixmap( gdi_display, tmpPixmap );
71 /***********************************************************************
72 * SelectBitmap (X11DRV.@)
74 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
79 if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
82 X11DRV_XRender_UpdateDrawable( physDev );
84 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
85 physDev->drawable = BITMAP_stock_pixmap;
87 physDev->drawable = (Pixmap)bmp->physBitmap;
89 /* Change GC depth if needed */
91 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
94 XFreeGC( gdi_display, physDev->gc );
95 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
96 XSetGraphicsExposures( gdi_display, physDev->gc, False );
97 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
98 XFlush( gdi_display );
101 GDI_ReleaseObj( hbitmap );
106 /****************************************************************************
107 * CreateBitmap (X11DRV.@)
109 * Create a device dependent X11 bitmap
111 * Returns TRUE on success else FALSE
113 BOOL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
115 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
118 WARN("Bad bitmap handle %p\n", hbitmap);
122 /* Check parameters */
123 if (bmp->bitmap.bmPlanes != 1)
125 GDI_ReleaseObj( hbitmap );
128 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
130 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
131 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
132 GDI_ReleaseObj( hbitmap );
135 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
137 ERR( "called for stock bitmap, please report\n" );
138 GDI_ReleaseObj( hbitmap );
142 TRACE("(%p) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
143 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
145 /* Create the pixmap */
147 bmp->physBitmap = (void *)XCreatePixmap(gdi_display, root_window,
148 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
149 bmp->bitmap.bmBitsPixel);
151 if (!bmp->physBitmap)
153 WARN("Can't create Pixmap\n");
154 GDI_ReleaseObj( hbitmap );
158 if (bmp->bitmap.bmBits) /* Set bitmap bits */
159 X11DRV_SetBitmapBits( hbitmap, bmp->bitmap.bmBits,
160 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes );
162 GDI_ReleaseObj( hbitmap );
167 /***********************************************************************
168 * GetBitmapBits (X11DRV.@)
171 * Success: Number of bytes copied
174 LONG X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
176 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
177 LONG old_height, height;
179 LPBYTE tbuf, startline;
183 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
187 /* Hack: change the bitmap height temporarily to avoid */
188 /* getting unnecessary bitmap rows. */
190 old_height = bmp->bitmap.bmHeight;
191 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
193 image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
194 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
195 AllPlanes, ZPixmap );
196 bmp->bitmap.bmHeight = old_height;
198 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
201 switch (bmp->bitmap.bmBitsPixel)
204 for (h=0;h<height;h++)
208 for (w=0;w<bmp->bitmap.bmWidth;w++)
212 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
213 if ((w&7) == 7) ++tbuf;
215 startline += bmp->bitmap.bmWidthBytes;
219 for (h=0;h<height;h++)
222 for (w=0;w<bmp->bitmap.bmWidth;w++)
224 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
225 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
227 startline += bmp->bitmap.bmWidthBytes;
231 for (h=0;h<height;h++)
234 for (w=0;w<bmp->bitmap.bmWidth;w++)
235 *tbuf++ = XGetPixel(image,w,h);
236 startline += bmp->bitmap.bmWidthBytes;
241 for (h=0;h<height;h++)
244 for (w=0;w<bmp->bitmap.bmWidth;w++)
246 long pixel = XGetPixel(image,w,h);
248 *tbuf++ = pixel & 0xff;
249 *tbuf++ = (pixel>>8) & 0xff;
251 startline += bmp->bitmap.bmWidthBytes;
255 for (h=0;h<height;h++)
258 for (w=0;w<bmp->bitmap.bmWidth;w++)
260 long pixel = XGetPixel(image,w,h);
262 *tbuf++ = pixel & 0xff;
263 *tbuf++ = (pixel>> 8) & 0xff;
264 *tbuf++ = (pixel>>16) & 0xff;
266 startline += bmp->bitmap.bmWidthBytes;
271 for (h=0;h<height;h++)
274 for (w=0;w<bmp->bitmap.bmWidth;w++)
276 long pixel = XGetPixel(image,w,h);
278 *tbuf++ = pixel & 0xff;
279 *tbuf++ = (pixel>> 8) & 0xff;
280 *tbuf++ = (pixel>>16) & 0xff;
281 *tbuf++ = (pixel>>24) & 0xff;
283 startline += bmp->bitmap.bmWidthBytes;
287 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
289 XDestroyImage( image );
291 GDI_ReleaseObj( hbitmap );
297 /******************************************************************************
298 * SetBitmapBits (X11DRV.@)
301 * Success: Number of bytes used in setting the bitmap bits
304 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
306 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
309 const BYTE *sbuf, *startline;
313 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
315 height = count / bmp->bitmap.bmWidthBytes;
318 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
319 bmp->bitmap.bmWidth, height, 32, 0 );
320 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
322 WARN("No memory to create image data.\n");
323 XDestroyImage( image );
325 GDI_ReleaseObj( hbitmap );
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 */
414 GDI_ReleaseObj( hbitmap );
418 /***********************************************************************
419 * DeleteBitmap (X11DRV.@)
421 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
423 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
427 if (bmp->physBitmap) XFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
428 bmp->physBitmap = NULL;
430 if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
431 GDI_ReleaseObj( hbitmap );
436 /**************************************************************************
437 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
439 * Allocates an HBITMAP which references the Pixmap passed in.
440 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
441 * calling DeleteObject on this will free the Pixmap as well.
443 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
446 BITMAPOBJ *pBmp = NULL;
448 int x,y; /* Unused */
449 unsigned border_width; /* Unused */
450 unsigned int depth, width, height;
452 /* Get the Pixmap dimensions and bit depth */
454 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
455 &border_width, &depth)) depth = 0;
457 if (!depth) goto END;
459 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
460 width, height, depth);
463 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
464 * and make it a container for the pixmap passed.
466 hBmp = CreateBitmap( width, height, 1, depth, NULL );
468 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
470 pBmp->funcs = X11DRV_DC_Funcs;
471 pBmp->physBitmap = (void *)pixmap;
472 GDI_ReleaseObj( hBmp );
475 TRACE("\tReturning HBITMAP %p\n", hBmp);
480 /**************************************************************************
481 * X11DRV_BITMAP_CreateBitmapFromPixmap
483 * Allocates an HBITMAP and copies the Pixmap data into it.
484 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
486 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
488 HBITMAP hBmp = 0, hBmpCopy = 0;
489 BITMAPOBJ *pBmp = NULL;
490 unsigned int width, height;
492 /* Allocate an HBITMAP which references the Pixmap passed to us */
493 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
496 TRACE("\tCould not create bitmap header for Pixmap\n");
500 /* Get the bitmap dimensions */
501 width = pBmp->bitmap.bmWidth;
502 height = pBmp->bitmap.bmHeight;
504 hBmpCopy = (HBITMAP)CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
506 /* We can now get rid of the HBITMAP wrapper we created earlier.
507 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
511 /* Manually clear the bitmap internals to prevent the Pixmap
512 * from being deleted by DeleteObject.
514 pBmp->physBitmap = NULL;
520 TRACE("\tReturning HBITMAP %p\n", hBmpCopy);
525 /**************************************************************************
526 * X11DRV_BITMAP_CreatePixmapFromBitmap
528 * Creates a Pixmap from a bitmap
530 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
532 HGLOBAL hPackedDIB = 0;
536 * Create a packed DIB from the bitmap passed to us.
537 * A packed DIB contains a BITMAPINFO structure followed immediately by
538 * an optional color palette and the pixel data.
540 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
542 /* Create a Pixmap from the packed DIB */
543 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
545 /* Free the temporary packed DIB */
546 GlobalFree(hPackedDIB);
552 /***********************************************************************
553 * X11DRV_BITMAP_Pixmap
555 * This function exists solely for x11 driver of the window system.
557 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
560 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
562 pixmap = (Pixmap)bmp->physBitmap;
563 GDI_ReleaseObj( hbitmap );
566 ERR("handle %p returned no obj\n", hbitmap);