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 static GC bitmap_gc[32];
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)
41 if(depth < 1 || depth > 32)
44 return bitmap_gc[depth-1];
47 /***********************************************************************
50 void X11DRV_BITMAP_Init(void)
52 int depth_count, index, i;
57 bitmap_context = XUniqueContext();
58 BITMAP_stock_phys_bitmap.pixmap_depth = 1;
59 BITMAP_stock_phys_bitmap.pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
60 bitmap_gc[0] = XCreateGC( gdi_display, BITMAP_stock_phys_bitmap.pixmap, 0, NULL );
61 XSetGraphicsExposures( gdi_display, bitmap_gc[0], False );
62 XSetSubwindowMode( gdi_display, bitmap_gc[0], IncludeInferiors );
64 /* Create a GC for all available depths. GCs at depths other than 1-bit/screen_depth are for use
65 * in combination with XRender which allows us to create dibsections at more depths.
67 depth_list = XListDepths(gdi_display, DefaultScreen(gdi_display), &depth_count);
68 for (i = 0; i < depth_count; i++)
70 index = depth_list[i] - 1;
71 if (bitmap_gc[index]) continue;
72 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, depth_list[i])))
74 if ((bitmap_gc[index] = XCreateGC( gdi_display, tmpPixmap, 0, NULL )))
76 XSetGraphicsExposures( gdi_display, bitmap_gc[index], False );
77 XSetSubwindowMode( gdi_display, bitmap_gc[index], IncludeInferiors );
79 XFreePixmap( gdi_display, tmpPixmap );
87 /***********************************************************************
88 * SelectBitmap (X11DRV.@)
90 HBITMAP CDECL X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
92 X_PHYSBITMAP *physBitmap;
95 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
98 X11DRV_XRender_UpdateDrawable( physDev );
100 if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap) physBitmap = &BITMAP_stock_phys_bitmap;
101 else if (!(physBitmap = X11DRV_get_phys_bitmap( hbitmap ))) return 0;
103 physDev->bitmap = physBitmap;
104 physDev->drawable = physBitmap->pixmap;
105 SetRect( &physDev->drawable_rect, 0, 0, bitmap.bmWidth, bitmap.bmHeight );
106 physDev->dc_rect = physDev->drawable_rect;
108 /* Change GC depth if needed */
110 if (physDev->depth != physBitmap->pixmap_depth)
112 physDev->depth = physBitmap->pixmap_depth;
114 XFreeGC( gdi_display, physDev->gc );
115 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
116 XSetGraphicsExposures( gdi_display, physDev->gc, False );
117 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
118 XFlush( gdi_display );
122 if(physDev->depth == 1)
123 physDev->color_shifts = NULL;
125 physDev->color_shifts = &physBitmap->pixmap_color_shifts;
131 /****************************************************************************
132 * CreateBitmap (X11DRV.@)
134 * Create a device dependent X11 bitmap
136 * Returns TRUE on success else FALSE
138 BOOL CDECL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, LPVOID bmBits )
140 X_PHYSBITMAP *physBitmap;
143 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
145 /* Check parameters */
146 if (bitmap.bmPlanes != 1) return FALSE;
148 /* check if bpp is compatible with screen depth */
149 if (!((bitmap.bmBitsPixel == 1) || (bitmap.bmBitsPixel == screen_bpp)))
151 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
152 bitmap.bmPlanes, bitmap.bmBitsPixel);
155 if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
157 ERR( "called for stock bitmap, please report\n" );
161 TRACE("(%p) %dx%d %d bpp\n", hbitmap, bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
163 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return FALSE;
165 /* Create the pixmap */
167 if(bitmap.bmBitsPixel == 1)
169 physBitmap->pixmap_depth = 1;
173 physBitmap->pixmap_depth = screen_depth;
174 physBitmap->pixmap_color_shifts = X11DRV_PALETTE_default_shifts;
176 physBitmap->pixmap = XCreatePixmap(gdi_display, root_window,
177 bitmap.bmWidth, bitmap.bmHeight, physBitmap->pixmap_depth);
179 if (!physBitmap->pixmap)
181 WARN("Can't create Pixmap\n");
182 HeapFree( GetProcessHeap(), 0, physBitmap );
186 if (bmBits) /* Set bitmap bits */
188 X11DRV_SetBitmapBits( hbitmap, bmBits, bitmap.bmHeight * bitmap.bmWidthBytes );
190 else /* else clear the bitmap */
192 GC gc = get_bitmap_gc(physBitmap->pixmap_depth);
194 XSetFunction( gdi_display, gc, GXclear );
195 XFillRectangle( gdi_display, physBitmap->pixmap, gc, 0, 0,
196 bitmap.bmWidth, bitmap.bmHeight );
197 XSetFunction( gdi_display, gc, GXcopy );
204 /***********************************************************************
205 * GetBitmapBits (X11DRV.@)
208 * Success: Number of bytes copied
211 LONG CDECL X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
214 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
217 LPBYTE tbuf, startline;
220 if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
222 TRACE("(bmp=%p, buffer=%p, count=0x%x)\n", hbitmap, buffer, count);
225 height = count / bitmap.bmWidthBytes;
226 image = XGetImage( gdi_display, physBitmap->pixmap, 0, 0,
227 bitmap.bmWidth, height, AllPlanes, ZPixmap );
229 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
232 switch (bitmap.bmBitsPixel)
235 for (h=0;h<height;h++)
239 for (w=0;w<bitmap.bmWidth;w++)
243 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
244 if ((w&7) == 7) ++tbuf;
246 startline += bitmap.bmWidthBytes;
250 for (h=0;h<height;h++)
253 for (w=0;w<bitmap.bmWidth;w++)
255 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
256 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
258 startline += bitmap.bmWidthBytes;
262 for (h=0;h<height;h++)
265 for (w=0;w<bitmap.bmWidth;w++)
266 *tbuf++ = XGetPixel(image,w,h);
267 startline += bitmap.bmWidthBytes;
272 for (h=0;h<height;h++)
275 for (w=0;w<bitmap.bmWidth;w++)
277 long pixel = XGetPixel(image,w,h);
279 *tbuf++ = pixel & 0xff;
280 *tbuf++ = (pixel>>8) & 0xff;
282 startline += bitmap.bmWidthBytes;
286 for (h=0;h<height;h++)
289 for (w=0;w<bitmap.bmWidth;w++)
291 long pixel = XGetPixel(image,w,h);
293 *tbuf++ = pixel & 0xff;
294 *tbuf++ = (pixel>> 8) & 0xff;
295 *tbuf++ = (pixel>>16) & 0xff;
297 startline += bitmap.bmWidthBytes;
302 for (h=0;h<height;h++)
305 for (w=0;w<bitmap.bmWidth;w++)
307 long pixel = XGetPixel(image,w,h);
309 *tbuf++ = pixel & 0xff;
310 *tbuf++ = (pixel>> 8) & 0xff;
311 *tbuf++ = (pixel>>16) & 0xff;
312 *tbuf++ = (pixel>>24) & 0xff;
314 startline += bitmap.bmWidthBytes;
318 FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
320 XDestroyImage( image );
327 /******************************************************************************
328 * SetBitmapBits (X11DRV.@)
331 * Success: Number of bytes used in setting the bitmap bits
334 LONG CDECL X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
337 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
340 const BYTE *sbuf, *startline;
343 if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
345 TRACE("(bmp=%p, bits=%p, count=0x%x)\n", hbitmap, bits, count);
347 height = count / bitmap.bmWidthBytes;
350 image = XCreateImage( gdi_display, visual, physBitmap->pixmap_depth, ZPixmap, 0, NULL,
351 bitmap.bmWidth, height, 32, 0 );
352 if (!(image->data = HeapAlloc( GetProcessHeap(), 0, image->bytes_per_line * height )))
354 WARN("No memory to create image data.\n");
355 XDestroyImage( image );
360 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
364 switch (bitmap.bmBitsPixel)
367 for (h=0;h<height;h++)
370 for (w=0;w<bitmap.bmWidth;w++)
372 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
376 startline += bitmap.bmWidthBytes;
380 for (h=0;h<height;h++)
383 for (w=0;w<bitmap.bmWidth;w++)
385 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
386 else XPutPixel( image, w, h, *sbuf++ & 0xf );
388 startline += bitmap.bmWidthBytes;
392 for (h=0;h<height;h++)
395 for (w=0;w<bitmap.bmWidth;w++)
396 XPutPixel(image,w,h,*sbuf++);
397 startline += bitmap.bmWidthBytes;
402 for (h=0;h<height;h++)
405 for (w=0;w<bitmap.bmWidth;w++)
407 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
410 startline += bitmap.bmWidthBytes;
414 for (h=0;h<height;h++)
417 for (w=0;w<bitmap.bmWidth;w++)
419 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
422 startline += bitmap.bmWidthBytes;
426 for (h=0;h<height;h++)
429 for (w=0;w<bitmap.bmWidth;w++)
431 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
434 startline += bitmap.bmWidthBytes;
438 FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
441 XPutImage( gdi_display, physBitmap->pixmap, get_bitmap_gc(physBitmap->pixmap_depth),
442 image, 0, 0, 0, 0, bitmap.bmWidth, height );
443 HeapFree( GetProcessHeap(), 0, image->data );
445 XDestroyImage( image );
450 /***********************************************************************
451 * DeleteBitmap (X11DRV.@)
453 BOOL CDECL X11DRV_DeleteBitmap( HBITMAP hbitmap )
455 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
461 if (GetObjectW( hbitmap, sizeof(dib), &dib ) == sizeof(dib))
462 X11DRV_DIB_DeleteDIBSection( physBitmap, &dib );
464 if (physBitmap->glxpixmap)
465 destroy_glxpixmap( gdi_display, physBitmap->glxpixmap );
467 if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
468 XDeleteContext( gdi_display, (XID)hbitmap, bitmap_context );
470 HeapFree( GetProcessHeap(), 0, physBitmap );
476 /***********************************************************************
477 * X11DRV_get_phys_bitmap
479 * Retrieve the X physical bitmap info.
481 X_PHYSBITMAP *X11DRV_get_phys_bitmap( HBITMAP hbitmap )
486 if (XFindContext( gdi_display, (XID)hbitmap, bitmap_context, (char **)&ret )) ret = NULL;
492 /***********************************************************************
493 * X11DRV_init_phys_bitmap
495 * Initialize the X physical bitmap info.
497 X_PHYSBITMAP *X11DRV_init_phys_bitmap( HBITMAP hbitmap )
501 if ((ret = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret) )) != NULL)
503 ret->hbitmap = hbitmap;
505 XSaveContext( gdi_display, (XID)hbitmap, bitmap_context, (char *)ret );
512 /***********************************************************************
515 * Retrieve the pixmap associated to a bitmap.
517 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
519 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
521 if (!physBitmap) return 0;
522 return physBitmap->pixmap;