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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
33 /* GCs used for B&W and color bitmap operations */
34 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
35 X_PHYSBITMAP BITMAP_stock_phys_bitmap = { 0 }; /* phys bitmap for the default stock bitmap */
37 static XContext bitmap_context; /* X context to associate a phys bitmap to a handle */
39 GC get_bitmap_gc(int depth)
46 return BITMAP_colorGC;
50 /***********************************************************************
53 void X11DRV_BITMAP_Init(void)
57 /* Create the necessary GCs */
60 bitmap_context = XUniqueContext();
61 BITMAP_stock_phys_bitmap.pixmap_depth = 1;
62 BITMAP_stock_phys_bitmap.pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
63 BITMAP_monoGC = XCreateGC( gdi_display, BITMAP_stock_phys_bitmap.pixmap, 0, NULL );
64 XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
65 XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
67 if (screen_depth != 1)
69 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
71 BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
72 XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
73 XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
74 XFreePixmap( gdi_display, tmpPixmap );
80 /***********************************************************************
81 * SelectBitmap (X11DRV.@)
83 HBITMAP CDECL X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
85 X_PHYSBITMAP *physBitmap;
88 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
91 X11DRV_XRender_UpdateDrawable( physDev );
93 if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap) physBitmap = &BITMAP_stock_phys_bitmap;
94 else if (!(physBitmap = X11DRV_get_phys_bitmap( hbitmap ))) return 0;
96 physDev->bitmap = physBitmap;
97 physDev->drawable = physBitmap->pixmap;
98 SetRect( &physDev->drawable_rect, 0, 0, bitmap.bmWidth, bitmap.bmHeight );
99 physDev->dc_rect = physDev->drawable_rect;
101 /* Change GC depth if needed */
103 if (physDev->depth != physBitmap->pixmap_depth)
105 physDev->depth = physBitmap->pixmap_depth;
107 XFreeGC( gdi_display, physDev->gc );
108 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
109 XSetGraphicsExposures( gdi_display, physDev->gc, False );
110 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
111 XFlush( gdi_display );
118 /****************************************************************************
119 * CreateBitmap (X11DRV.@)
121 * Create a device dependent X11 bitmap
123 * Returns TRUE on success else FALSE
125 BOOL CDECL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, LPVOID bmBits )
127 X_PHYSBITMAP *physBitmap;
130 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
132 /* Check parameters */
133 if (bitmap.bmPlanes != 1) return FALSE;
135 /* check if bpp is compatible with screen depth */
136 if (!((bitmap.bmBitsPixel == 1) || (bitmap.bmBitsPixel == screen_bpp)))
138 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
139 bitmap.bmPlanes, bitmap.bmBitsPixel);
142 if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
144 ERR( "called for stock bitmap, please report\n" );
148 TRACE("(%p) %dx%d %d bpp\n", hbitmap, bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
150 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return FALSE;
152 /* Create the pixmap */
154 physBitmap->pixmap_depth = (bitmap.bmBitsPixel == 1) ? 1 : screen_depth;
155 physBitmap->pixmap = XCreatePixmap(gdi_display, root_window,
156 bitmap.bmWidth, bitmap.bmHeight, physBitmap->pixmap_depth);
158 if (!physBitmap->pixmap)
160 WARN("Can't create Pixmap\n");
161 HeapFree( GetProcessHeap(), 0, physBitmap );
165 if (bmBits) /* Set bitmap bits */
167 X11DRV_SetBitmapBits( hbitmap, bmBits, bitmap.bmHeight * bitmap.bmWidthBytes );
169 else /* else clear the bitmap */
171 GC gc = get_bitmap_gc(physBitmap->pixmap_depth);
173 XSetFunction( gdi_display, gc, GXclear );
174 XFillRectangle( gdi_display, physBitmap->pixmap, gc, 0, 0,
175 bitmap.bmWidth, bitmap.bmHeight );
176 XSetFunction( gdi_display, gc, GXcopy );
183 /***********************************************************************
184 * GetBitmapBits (X11DRV.@)
187 * Success: Number of bytes copied
190 LONG CDECL X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
193 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
196 LPBYTE tbuf, startline;
199 if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
201 TRACE("(bmp=%p, buffer=%p, count=0x%x)\n", hbitmap, buffer, count);
204 height = count / bitmap.bmWidthBytes;
205 image = XGetImage( gdi_display, physBitmap->pixmap, 0, 0,
206 bitmap.bmWidth, height, AllPlanes, ZPixmap );
208 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
211 switch (bitmap.bmBitsPixel)
214 for (h=0;h<height;h++)
218 for (w=0;w<bitmap.bmWidth;w++)
222 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
223 if ((w&7) == 7) ++tbuf;
225 startline += bitmap.bmWidthBytes;
229 for (h=0;h<height;h++)
232 for (w=0;w<bitmap.bmWidth;w++)
234 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
235 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
237 startline += bitmap.bmWidthBytes;
241 for (h=0;h<height;h++)
244 for (w=0;w<bitmap.bmWidth;w++)
245 *tbuf++ = XGetPixel(image,w,h);
246 startline += bitmap.bmWidthBytes;
251 for (h=0;h<height;h++)
254 for (w=0;w<bitmap.bmWidth;w++)
256 long pixel = XGetPixel(image,w,h);
258 *tbuf++ = pixel & 0xff;
259 *tbuf++ = (pixel>>8) & 0xff;
261 startline += bitmap.bmWidthBytes;
265 for (h=0;h<height;h++)
268 for (w=0;w<bitmap.bmWidth;w++)
270 long pixel = XGetPixel(image,w,h);
272 *tbuf++ = pixel & 0xff;
273 *tbuf++ = (pixel>> 8) & 0xff;
274 *tbuf++ = (pixel>>16) & 0xff;
276 startline += bitmap.bmWidthBytes;
281 for (h=0;h<height;h++)
284 for (w=0;w<bitmap.bmWidth;w++)
286 long pixel = XGetPixel(image,w,h);
288 *tbuf++ = pixel & 0xff;
289 *tbuf++ = (pixel>> 8) & 0xff;
290 *tbuf++ = (pixel>>16) & 0xff;
291 *tbuf++ = (pixel>>24) & 0xff;
293 startline += bitmap.bmWidthBytes;
297 FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
299 XDestroyImage( image );
306 /******************************************************************************
307 * SetBitmapBits (X11DRV.@)
310 * Success: Number of bytes used in setting the bitmap bits
313 LONG CDECL X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
316 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
319 const BYTE *sbuf, *startline;
322 if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
324 TRACE("(bmp=%p, bits=%p, count=0x%x)\n", hbitmap, bits, count);
326 height = count / bitmap.bmWidthBytes;
329 image = XCreateImage( gdi_display, visual, physBitmap->pixmap_depth, ZPixmap, 0, NULL,
330 bitmap.bmWidth, height, 32, 0 );
331 if (!(image->data = HeapAlloc( GetProcessHeap(), 0, image->bytes_per_line * height )))
333 WARN("No memory to create image data.\n");
334 XDestroyImage( image );
339 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
343 switch (bitmap.bmBitsPixel)
346 for (h=0;h<height;h++)
349 for (w=0;w<bitmap.bmWidth;w++)
351 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
355 startline += bitmap.bmWidthBytes;
359 for (h=0;h<height;h++)
362 for (w=0;w<bitmap.bmWidth;w++)
364 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
365 else XPutPixel( image, w, h, *sbuf++ & 0xf );
367 startline += bitmap.bmWidthBytes;
371 for (h=0;h<height;h++)
374 for (w=0;w<bitmap.bmWidth;w++)
375 XPutPixel(image,w,h,*sbuf++);
376 startline += bitmap.bmWidthBytes;
381 for (h=0;h<height;h++)
384 for (w=0;w<bitmap.bmWidth;w++)
386 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
389 startline += bitmap.bmWidthBytes;
393 for (h=0;h<height;h++)
396 for (w=0;w<bitmap.bmWidth;w++)
398 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
401 startline += bitmap.bmWidthBytes;
405 for (h=0;h<height;h++)
408 for (w=0;w<bitmap.bmWidth;w++)
410 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
413 startline += bitmap.bmWidthBytes;
417 FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
420 XPutImage( gdi_display, physBitmap->pixmap, get_bitmap_gc(physBitmap->pixmap_depth),
421 image, 0, 0, 0, 0, bitmap.bmWidth, height );
422 HeapFree( GetProcessHeap(), 0, image->data );
424 XDestroyImage( image );
429 /***********************************************************************
430 * DeleteBitmap (X11DRV.@)
432 BOOL CDECL X11DRV_DeleteBitmap( HBITMAP hbitmap )
434 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
440 if (GetObjectW( hbitmap, sizeof(dib), &dib ) == sizeof(dib))
441 X11DRV_DIB_DeleteDIBSection( physBitmap, &dib );
443 if (physBitmap->glxpixmap)
444 destroy_glxpixmap( gdi_display, physBitmap->glxpixmap );
446 if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
447 XDeleteContext( gdi_display, (XID)hbitmap, bitmap_context );
449 HeapFree( GetProcessHeap(), 0, physBitmap );
455 /***********************************************************************
456 * X11DRV_get_phys_bitmap
458 * Retrieve the X physical bitmap info.
460 X_PHYSBITMAP *X11DRV_get_phys_bitmap( HBITMAP hbitmap )
465 if (XFindContext( gdi_display, (XID)hbitmap, bitmap_context, (char **)&ret )) ret = NULL;
471 /***********************************************************************
472 * X11DRV_init_phys_bitmap
474 * Initialize the X physical bitmap info.
476 X_PHYSBITMAP *X11DRV_init_phys_bitmap( HBITMAP hbitmap )
480 if ((ret = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret) )) != NULL)
482 ret->hbitmap = hbitmap;
484 XSaveContext( gdi_display, (XID)hbitmap, bitmap_context, (char *)ret );
491 /***********************************************************************
494 * Retrieve the pixmap associated to a bitmap.
496 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
498 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
500 if (!physBitmap) return 0;
501 return physBitmap->pixmap;