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
31 #include "wine/debug.h"
35 #include "wine/winuser16.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
39 /* GCs used for B&W and color bitmap operations */
40 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
41 Pixmap BITMAP_stock_pixmap; /* pixmap for the default stock bitmap */
43 extern const DC_FUNCTIONS *X11DRV_DC_Funcs; /* hack */
45 /***********************************************************************
48 BOOL X11DRV_BITMAP_Init(void)
52 /* Create the necessary GCs */
55 BITMAP_stock_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
56 BITMAP_monoGC = XCreateGC( gdi_display, BITMAP_stock_pixmap, 0, NULL );
57 XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
58 XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
60 if (screen_depth != 1)
62 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
64 BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
65 XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
66 XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
67 XFreePixmap( gdi_display, tmpPixmap );
74 /***********************************************************************
75 * SelectBitmap (X11DRV.@)
77 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
82 if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
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 %08x\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("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
143 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
145 /* Create the pixmap */
146 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
147 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
148 bmp->bitmap.bmBitsPixel)))
150 WARN("Can't create Pixmap\n");
151 GDI_ReleaseObj( hbitmap );
155 if (bmp->bitmap.bmBits) /* Set bitmap bits */
156 X11DRV_SetBitmapBits( hbitmap, bmp->bitmap.bmBits,
157 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes );
159 GDI_ReleaseObj( hbitmap );
164 /***********************************************************************
165 * GetBitmapBits (X11DRV.@)
168 * Success: Number of bytes copied
171 LONG X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
173 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
174 LONG old_height, height;
176 LPBYTE tbuf, startline;
180 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
184 /* Hack: change the bitmap height temporarily to avoid */
185 /* getting unnecessary bitmap rows. */
187 old_height = bmp->bitmap.bmHeight;
188 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
190 image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
191 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
192 AllPlanes, ZPixmap );
193 bmp->bitmap.bmHeight = old_height;
195 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
198 switch (bmp->bitmap.bmBitsPixel)
201 for (h=0;h<height;h++)
205 for (w=0;w<bmp->bitmap.bmWidth;w++)
209 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
210 if ((w&7) == 7) ++tbuf;
212 startline += bmp->bitmap.bmWidthBytes;
216 for (h=0;h<height;h++)
219 for (w=0;w<bmp->bitmap.bmWidth;w++)
221 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
222 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
224 startline += bmp->bitmap.bmWidthBytes;
228 for (h=0;h<height;h++)
231 for (w=0;w<bmp->bitmap.bmWidth;w++)
232 *tbuf++ = XGetPixel(image,w,h);
233 startline += bmp->bitmap.bmWidthBytes;
238 for (h=0;h<height;h++)
241 for (w=0;w<bmp->bitmap.bmWidth;w++)
243 long pixel = XGetPixel(image,w,h);
245 *tbuf++ = pixel & 0xff;
246 *tbuf++ = (pixel>>8) & 0xff;
248 startline += bmp->bitmap.bmWidthBytes;
252 for (h=0;h<height;h++)
255 for (w=0;w<bmp->bitmap.bmWidth;w++)
257 long pixel = XGetPixel(image,w,h);
259 *tbuf++ = pixel & 0xff;
260 *tbuf++ = (pixel>> 8) & 0xff;
261 *tbuf++ = (pixel>>16) & 0xff;
263 startline += bmp->bitmap.bmWidthBytes;
268 for (h=0;h<height;h++)
271 for (w=0;w<bmp->bitmap.bmWidth;w++)
273 long pixel = XGetPixel(image,w,h);
275 *tbuf++ = pixel & 0xff;
276 *tbuf++ = (pixel>> 8) & 0xff;
277 *tbuf++ = (pixel>>16) & 0xff;
278 *tbuf++ = (pixel>>24) & 0xff;
280 startline += bmp->bitmap.bmWidthBytes;
284 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
286 XDestroyImage( image );
288 GDI_ReleaseObj( hbitmap );
294 /******************************************************************************
295 * SetBitmapBits (X11DRV.@)
298 * Success: Number of bytes used in setting the bitmap bits
301 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
303 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
306 const BYTE *sbuf, *startline;
310 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
312 height = count / bmp->bitmap.bmWidthBytes;
315 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
316 bmp->bitmap.bmWidth, height, 32, 0 );
317 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
319 WARN("No memory to create image data.\n");
320 XDestroyImage( image );
322 GDI_ReleaseObj( hbitmap );
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( gdi_display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
408 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
409 XDestroyImage( image ); /* frees image->data too */
411 GDI_ReleaseObj( hbitmap );
415 /***********************************************************************
416 * DeleteBitmap (X11DRV.@)
418 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
420 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
423 if (bmp->physBitmap) TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
424 bmp->physBitmap = NULL;
425 if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
426 GDI_ReleaseObj( hbitmap );
431 /**************************************************************************
432 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
434 * Allocates an HBITMAP which references the Pixmap passed in.
435 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
436 * calling DeleteObject on this will free the Pixmap as well.
438 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
441 BITMAPOBJ *pBmp = NULL;
443 int x,y; /* Unused */
444 unsigned border_width; /* Unused */
445 unsigned int depth, width, height;
447 /* Get the Pixmap dimensions and bit depth */
448 if ( 0 == TSXGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
449 &border_width, &depth) )
452 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
453 width, height, depth);
456 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
457 * and make it a container for the pixmap passed.
459 hBmp = CreateBitmap( width, height, 1, depth, NULL );
461 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
463 pBmp->funcs = X11DRV_DC_Funcs;
464 pBmp->physBitmap = (void *)pixmap;
465 GDI_ReleaseObj( hBmp );
468 TRACE("\tReturning HBITMAP %x\n", hBmp);
473 /**************************************************************************
474 * X11DRV_BITMAP_CreateBitmapFromPixmap
476 * Allocates an HBITMAP and copies the Pixmap data into it.
477 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
479 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
481 HBITMAP hBmp = 0, hBmpCopy = 0;
482 BITMAPOBJ *pBmp = NULL;
483 unsigned int width, height;
485 /* Allocate an HBITMAP which references the Pixmap passed to us */
486 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
489 TRACE("\tCould not create bitmap header for Pixmap\n");
493 /* Get the bitmap dimensions */
494 width = pBmp->bitmap.bmWidth;
495 height = pBmp->bitmap.bmHeight;
497 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
499 /* We can now get rid of the HBITMAP wrapper we created earlier.
500 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
504 /* Manually clear the bitmap internals to prevent the Pixmap
505 * from being deleted by DeleteObject.
507 pBmp->physBitmap = NULL;
513 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
518 /**************************************************************************
519 * X11DRV_BITMAP_CreatePixmapFromBitmap
521 * Creates a Pixmap from a bitmap
523 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
525 HGLOBAL hPackedDIB = 0;
529 * Create a packed DIB from the bitmap passed to us.
530 * A packed DIB contains a BITMAPINFO structure followed immediately by
531 * an optional color palette and the pixel data.
533 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
535 /* Create a Pixmap from the packed DIB */
536 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
538 /* Free the temporary packed DIB */
539 GlobalFree(hPackedDIB);
545 /***********************************************************************
546 * X11DRV_BITMAP_Pixmap
548 * This function exists solely for x11 driver of the window system.
550 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
553 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
555 pixmap = (Pixmap)bmp->physBitmap;
556 GDI_ReleaseObj( hbitmap );
559 ERR("handle %08x returned no obj\n", hbitmap);