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
27 #include "wine/debug.h"
31 #include "wine/winuser16.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
35 /* GCs used for B&W and color bitmap operations */
36 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
37 Pixmap BITMAP_stock_pixmap; /* pixmap for the default stock bitmap */
39 /***********************************************************************
42 BOOL X11DRV_BITMAP_Init(void)
46 /* Create the necessary GCs */
49 BITMAP_stock_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
50 BITMAP_monoGC = XCreateGC( gdi_display, BITMAP_stock_pixmap, 0, NULL );
51 XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
52 XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
54 if (screen_depth != 1)
56 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
58 BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
59 XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
60 XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
61 XFreePixmap( gdi_display, tmpPixmap );
68 /***********************************************************************
69 * SelectBitmap (X11DRV.@)
71 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
75 if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
78 X11DRV_XRender_UpdateDrawable( physDev );
80 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
81 physDev->drawable = BITMAP_stock_pixmap;
83 physDev->drawable = (Pixmap)bmp->physBitmap;
85 /* Change GC depth if needed */
87 if (physDev->depth != bmp->bitmap.bmBitsPixel)
89 physDev->depth = bmp->bitmap.bmBitsPixel;
91 XFreeGC( gdi_display, physDev->gc );
92 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
93 XSetGraphicsExposures( gdi_display, physDev->gc, False );
94 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
95 XFlush( gdi_display );
98 GDI_ReleaseObj( hbitmap );
103 /****************************************************************************
104 * CreateBitmap (X11DRV.@)
106 * Create a device dependent X11 bitmap
108 * Returns TRUE on success else FALSE
110 BOOL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
112 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
115 WARN("Bad bitmap handle %p\n", hbitmap);
119 /* Check parameters */
120 if (bmp->bitmap.bmPlanes != 1)
122 GDI_ReleaseObj( hbitmap );
125 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
127 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
128 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
129 GDI_ReleaseObj( hbitmap );
132 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
134 ERR( "called for stock bitmap, please report\n" );
135 GDI_ReleaseObj( hbitmap );
139 TRACE("(%p) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
140 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
142 /* Create the pixmap */
144 bmp->physBitmap = (void *)XCreatePixmap(gdi_display, root_window,
145 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
146 bmp->bitmap.bmBitsPixel);
148 if (!bmp->physBitmap)
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 );
424 if (bmp->physBitmap) XFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
425 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(HDC hdc, Pixmap pixmap)
443 HBITMAP hBmp = 0, old;
444 BITMAPOBJ *pBmp = NULL;
446 int x,y; /* Unused */
447 unsigned border_width; /* Unused */
448 unsigned int depth, width, height;
450 /* Get the Pixmap dimensions and bit depth */
452 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
453 &border_width, &depth)) depth = 0;
455 if (!depth) goto END;
457 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
458 width, height, depth);
461 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
462 * and make it a container for the pixmap passed.
464 hBmp = CreateBitmap( width, height, 1, depth, NULL );
466 /* force bitmap to be owned by a screen DC */
467 hdcMem = CreateCompatibleDC( hdc );
468 old = SelectObject( hdcMem, hBmp );
470 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
472 if (pBmp->physBitmap) XFreePixmap( gdi_display, (Pixmap)pBmp->physBitmap );
473 pBmp->physBitmap = (void *)pixmap;
474 GDI_ReleaseObj( hBmp );
476 SelectObject( hdcMem, old );
480 TRACE("\tReturning HBITMAP %p\n", hBmp);
485 /***********************************************************************
486 * X11DRV_BITMAP_Pixmap
488 * This function exists solely for x11 driver of the window system.
490 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
493 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
495 pixmap = (Pixmap)bmp->physBitmap;
496 GDI_ReleaseObj( hbitmap );
499 ERR("handle %p returned no obj\n", hbitmap);