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 HBITMAP BITMAP_stock_bitmap = 0; /* default stock bitmap */
38 Pixmap BITMAP_stock_pixmap = 0; /* pixmap for the default stock bitmap */
40 /***********************************************************************
43 void X11DRV_BITMAP_Init(void)
47 /* Create the necessary GCs */
50 BITMAP_stock_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
51 BITMAP_monoGC = XCreateGC( gdi_display, BITMAP_stock_pixmap, 0, NULL );
52 XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
53 XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
55 if (screen_depth != 1)
57 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
59 BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
60 XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
61 XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
62 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 == BITMAP_stock_bitmap)
81 physDev->drawable = BITMAP_stock_pixmap;
83 physDev->drawable = X11DRV_get_pixmap( hbitmap );
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 )
113 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
116 WARN("Bad bitmap handle %p\n", hbitmap);
120 /* Check parameters */
121 if (bmp->bitmap.bmPlanes != 1)
123 GDI_ReleaseObj( hbitmap );
126 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
128 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
129 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
130 GDI_ReleaseObj( hbitmap );
133 if (hbitmap == BITMAP_stock_bitmap)
135 ERR( "called for stock bitmap, please report\n" );
136 GDI_ReleaseObj( hbitmap );
140 TRACE("(%p) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
141 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
143 /* Create the pixmap */
145 pixmap = XCreatePixmap(gdi_display, root_window,
146 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
150 WARN("Can't create Pixmap\n");
151 GDI_ReleaseObj( hbitmap );
154 X11DRV_set_pixmap( hbitmap, pixmap );
156 if (bmp->bitmap.bmBits) /* Set bitmap bits */
157 X11DRV_SetBitmapBits( hbitmap, bmp->bitmap.bmBits,
158 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes );
160 GDI_ReleaseObj( hbitmap );
165 /***********************************************************************
166 * GetBitmapBits (X11DRV.@)
169 * Success: Number of bytes copied
172 LONG X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
174 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
175 LONG old_height, height;
176 Pixmap pixmap = X11DRV_get_pixmap( hbitmap );
178 LPBYTE tbuf, startline;
182 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
186 /* Hack: change the bitmap height temporarily to avoid */
187 /* getting unnecessary bitmap rows. */
189 old_height = bmp->bitmap.bmHeight;
190 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
192 image = XGetImage( gdi_display, pixmap, 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
193 AllPlanes, ZPixmap );
194 bmp->bitmap.bmHeight = old_height;
196 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
199 switch (bmp->bitmap.bmBitsPixel)
202 for (h=0;h<height;h++)
206 for (w=0;w<bmp->bitmap.bmWidth;w++)
210 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
211 if ((w&7) == 7) ++tbuf;
213 startline += bmp->bitmap.bmWidthBytes;
217 for (h=0;h<height;h++)
220 for (w=0;w<bmp->bitmap.bmWidth;w++)
222 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
223 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
225 startline += bmp->bitmap.bmWidthBytes;
229 for (h=0;h<height;h++)
232 for (w=0;w<bmp->bitmap.bmWidth;w++)
233 *tbuf++ = XGetPixel(image,w,h);
234 startline += bmp->bitmap.bmWidthBytes;
239 for (h=0;h<height;h++)
242 for (w=0;w<bmp->bitmap.bmWidth;w++)
244 long pixel = XGetPixel(image,w,h);
246 *tbuf++ = pixel & 0xff;
247 *tbuf++ = (pixel>>8) & 0xff;
249 startline += bmp->bitmap.bmWidthBytes;
253 for (h=0;h<height;h++)
256 for (w=0;w<bmp->bitmap.bmWidth;w++)
258 long pixel = XGetPixel(image,w,h);
260 *tbuf++ = pixel & 0xff;
261 *tbuf++ = (pixel>> 8) & 0xff;
262 *tbuf++ = (pixel>>16) & 0xff;
264 startline += bmp->bitmap.bmWidthBytes;
269 for (h=0;h<height;h++)
272 for (w=0;w<bmp->bitmap.bmWidth;w++)
274 long pixel = XGetPixel(image,w,h);
276 *tbuf++ = pixel & 0xff;
277 *tbuf++ = (pixel>> 8) & 0xff;
278 *tbuf++ = (pixel>>16) & 0xff;
279 *tbuf++ = (pixel>>24) & 0xff;
281 startline += bmp->bitmap.bmWidthBytes;
285 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
287 XDestroyImage( image );
289 GDI_ReleaseObj( hbitmap );
295 /******************************************************************************
296 * SetBitmapBits (X11DRV.@)
299 * Success: Number of bytes used in setting the bitmap bits
302 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
304 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
305 Pixmap pixmap = X11DRV_get_pixmap( hbitmap );
308 const BYTE *sbuf, *startline;
312 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
314 height = count / bmp->bitmap.bmWidthBytes;
317 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
318 bmp->bitmap.bmWidth, height, 32, 0 );
319 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
321 WARN("No memory to create image data.\n");
322 XDestroyImage( image );
324 GDI_ReleaseObj( hbitmap );
328 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
332 switch (bmp->bitmap.bmBitsPixel)
335 for (h=0;h<height;h++)
338 for (w=0;w<bmp->bitmap.bmWidth;w++)
340 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
344 startline += bmp->bitmap.bmWidthBytes;
348 for (h=0;h<height;h++)
351 for (w=0;w<bmp->bitmap.bmWidth;w++)
353 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
354 else XPutPixel( image, w, h, *sbuf++ & 0xf );
356 startline += bmp->bitmap.bmWidthBytes;
360 for (h=0;h<height;h++)
363 for (w=0;w<bmp->bitmap.bmWidth;w++)
364 XPutPixel(image,w,h,*sbuf++);
365 startline += bmp->bitmap.bmWidthBytes;
370 for (h=0;h<height;h++)
373 for (w=0;w<bmp->bitmap.bmWidth;w++)
375 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
378 startline += bmp->bitmap.bmWidthBytes;
382 for (h=0;h<height;h++)
385 for (w=0;w<bmp->bitmap.bmWidth;w++)
387 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
390 startline += bmp->bitmap.bmWidthBytes;
394 for (h=0;h<height;h++)
397 for (w=0;w<bmp->bitmap.bmWidth;w++)
399 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
402 startline += bmp->bitmap.bmWidthBytes;
406 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
409 XPutImage( gdi_display, pixmap, BITMAP_GC(bmp),
410 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
411 XDestroyImage( image ); /* frees image->data too */
413 GDI_ReleaseObj( hbitmap );
417 /***********************************************************************
418 * DeleteBitmap (X11DRV.@)
420 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
422 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
426 if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
427 pixmap = X11DRV_set_pixmap( hbitmap, 0 );
429 if (pixmap) XFreePixmap( gdi_display, pixmap );
431 GDI_ReleaseObj( hbitmap );
437 /***********************************************************************
440 * Set the pixmap associated to a bitmap, and return the previous one.
442 Pixmap X11DRV_set_pixmap( HBITMAP hbitmap, Pixmap pixmap )
445 BITMAPOBJ *bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
448 ret = (Pixmap)bmp->physBitmap;
449 bmp->physBitmap = (void *)pixmap;
450 GDI_ReleaseObj( hbitmap );
456 /***********************************************************************
459 * Retrieve the pixmap associated to a bitmap.
461 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
464 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
467 pixmap = (Pixmap)bmp->physBitmap;
468 GDI_ReleaseObj( hbitmap );