ole32: Add some tests for the running object table.
[wine.git] / dlls / winex11.drv / bitmap.c
blob75623e4a6b37ed6ddfb50a88a4a263a5d5c3bead
1 /*
2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 * 1999 Noel Borthwick
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
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "windef.h"
27 #include "wingdi.h"
28 #include "wine/debug.h"
29 #include "x11drv.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 /***********************************************************************
40 * X11DRV_BITMAP_Init
42 void X11DRV_BITMAP_Init(void)
44 Pixmap tmpPixmap;
46 /* Create the necessary GCs */
48 wine_tsx11_lock();
49 bitmap_context = XUniqueContext();
50 BITMAP_stock_phys_bitmap.pixmap_depth = 1;
51 BITMAP_stock_phys_bitmap.pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
52 BITMAP_monoGC = XCreateGC( gdi_display, BITMAP_stock_phys_bitmap.pixmap, 0, NULL );
53 XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
54 XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
56 if (screen_depth != 1)
58 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
60 BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
61 XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
62 XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
63 XFreePixmap( gdi_display, tmpPixmap );
66 wine_tsx11_unlock();
69 /***********************************************************************
70 * SelectBitmap (X11DRV.@)
72 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
74 X_PHYSBITMAP *physBitmap;
75 BITMAP bitmap;
77 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
79 if(physDev->xrender)
80 X11DRV_XRender_UpdateDrawable( physDev );
82 if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap) physBitmap = &BITMAP_stock_phys_bitmap;
83 else if (!(physBitmap = X11DRV_get_phys_bitmap( hbitmap ))) return 0;
85 physDev->bitmap = physBitmap;
86 physDev->drawable = physBitmap->pixmap;
87 SetRect( &physDev->drawable_rect, 0, 0, bitmap.bmWidth, bitmap.bmHeight );
88 physDev->dc_rect = physDev->drawable_rect;
90 /* Change GC depth if needed */
92 if (physDev->depth != physBitmap->pixmap_depth)
94 physDev->depth = physBitmap->pixmap_depth;
95 wine_tsx11_lock();
96 XFreeGC( gdi_display, physDev->gc );
97 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
98 XSetGraphicsExposures( gdi_display, physDev->gc, False );
99 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
100 XFlush( gdi_display );
101 wine_tsx11_unlock();
103 return hbitmap;
107 /****************************************************************************
108 * CreateBitmap (X11DRV.@)
110 * Create a device dependent X11 bitmap
112 * Returns TRUE on success else FALSE
114 BOOL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, LPVOID bmBits )
116 X_PHYSBITMAP *physBitmap;
117 BITMAP bitmap;
119 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
121 /* Check parameters */
122 if (bitmap.bmPlanes != 1) return FALSE;
124 if ((bitmap.bmBitsPixel != 1) && (bitmap.bmBitsPixel != screen_depth))
126 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
127 bitmap.bmPlanes, bitmap.bmBitsPixel);
128 return FALSE;
130 if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
132 ERR( "called for stock bitmap, please report\n" );
133 return FALSE;
136 TRACE("(%p) %dx%d %d bpp\n", hbitmap, bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
138 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return FALSE;
140 /* Create the pixmap */
141 wine_tsx11_lock();
142 physBitmap->pixmap_depth = bitmap.bmBitsPixel;
143 physBitmap->pixmap = XCreatePixmap(gdi_display, root_window,
144 bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
145 wine_tsx11_unlock();
146 if (!physBitmap->pixmap)
148 WARN("Can't create Pixmap\n");
149 HeapFree( GetProcessHeap(), 0, physBitmap );
150 return FALSE;
153 if (bmBits) /* Set bitmap bits */
155 X11DRV_SetBitmapBits( hbitmap, bmBits, bitmap.bmHeight * bitmap.bmWidthBytes );
157 else /* else clear the bitmap */
159 wine_tsx11_lock();
160 XSetFunction( gdi_display, BITMAP_GC(physBitmap), GXclear );
161 XFillRectangle( gdi_display, physBitmap->pixmap, BITMAP_GC(physBitmap), 0, 0,
162 bitmap.bmWidth, bitmap.bmHeight );
163 XSetFunction( gdi_display, BITMAP_GC(physBitmap), GXcopy );
164 wine_tsx11_unlock();
166 return TRUE;
170 /***********************************************************************
171 * GetBitmapBits (X11DRV.@)
173 * RETURNS
174 * Success: Number of bytes copied
175 * Failure: 0
177 LONG X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
179 BITMAP bitmap;
180 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
181 LONG old_height, height;
182 XImage *image;
183 LPBYTE tbuf, startline;
184 int h, w;
186 if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
188 TRACE("(bmp=%p, buffer=%p, count=0x%x)\n", hbitmap, buffer, count);
190 wine_tsx11_lock();
192 /* Hack: change the bitmap height temporarily to avoid */
193 /* getting unnecessary bitmap rows. */
195 old_height = bitmap.bmHeight;
196 height = bitmap.bmHeight = count / bitmap.bmWidthBytes;
198 image = XGetImage( gdi_display, physBitmap->pixmap, 0, 0,
199 bitmap.bmWidth, bitmap.bmHeight, AllPlanes, ZPixmap );
200 bitmap.bmHeight = old_height;
202 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
204 startline = buffer;
205 switch (physBitmap->pixmap_depth)
207 case 1:
208 for (h=0;h<height;h++)
210 tbuf = startline;
211 *tbuf = 0;
212 for (w=0;w<bitmap.bmWidth;w++)
214 if ((w%8) == 0)
215 *tbuf = 0;
216 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
217 if ((w&7) == 7) ++tbuf;
219 startline += bitmap.bmWidthBytes;
221 break;
222 case 4:
223 for (h=0;h<height;h++)
225 tbuf = startline;
226 for (w=0;w<bitmap.bmWidth;w++)
228 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
229 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
231 startline += bitmap.bmWidthBytes;
233 break;
234 case 8:
235 for (h=0;h<height;h++)
237 tbuf = startline;
238 for (w=0;w<bitmap.bmWidth;w++)
239 *tbuf++ = XGetPixel(image,w,h);
240 startline += bitmap.bmWidthBytes;
242 break;
243 case 15:
244 case 16:
245 for (h=0;h<height;h++)
247 tbuf = startline;
248 for (w=0;w<bitmap.bmWidth;w++)
250 long pixel = XGetPixel(image,w,h);
252 *tbuf++ = pixel & 0xff;
253 *tbuf++ = (pixel>>8) & 0xff;
255 startline += bitmap.bmWidthBytes;
257 break;
258 case 24:
259 for (h=0;h<height;h++)
261 tbuf = startline;
262 for (w=0;w<bitmap.bmWidth;w++)
264 long pixel = XGetPixel(image,w,h);
266 *tbuf++ = pixel & 0xff;
267 *tbuf++ = (pixel>> 8) & 0xff;
268 *tbuf++ = (pixel>>16) & 0xff;
270 startline += bitmap.bmWidthBytes;
272 break;
274 case 32:
275 for (h=0;h<height;h++)
277 tbuf = startline;
278 for (w=0;w<bitmap.bmWidth;w++)
280 long pixel = XGetPixel(image,w,h);
282 *tbuf++ = pixel & 0xff;
283 *tbuf++ = (pixel>> 8) & 0xff;
284 *tbuf++ = (pixel>>16) & 0xff;
285 *tbuf++ = (pixel>>24) & 0xff;
287 startline += bitmap.bmWidthBytes;
289 break;
290 default:
291 FIXME("Unhandled bits:%d\n", physBitmap->pixmap_depth);
293 XDestroyImage( image );
294 wine_tsx11_unlock();
295 return count;
300 /******************************************************************************
301 * SetBitmapBits (X11DRV.@)
303 * RETURNS
304 * Success: Number of bytes used in setting the bitmap bits
305 * Failure: 0
307 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
309 BITMAP bitmap;
310 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
311 LONG height;
312 XImage *image;
313 const BYTE *sbuf, *startline;
314 int w, h;
316 if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
318 TRACE("(bmp=%p, bits=%p, count=0x%x)\n", hbitmap, bits, count);
320 height = count / bitmap.bmWidthBytes;
322 wine_tsx11_lock();
323 image = XCreateImage( gdi_display, visual, physBitmap->pixmap_depth, ZPixmap, 0, NULL,
324 bitmap.bmWidth, height, 32, 0 );
325 if (!(image->data = malloc(image->bytes_per_line * height)))
327 WARN("No memory to create image data.\n");
328 XDestroyImage( image );
329 wine_tsx11_unlock();
330 return 0;
333 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
335 startline = bits;
337 switch (physBitmap->pixmap_depth)
339 case 1:
340 for (h=0;h<height;h++)
342 sbuf = startline;
343 for (w=0;w<bitmap.bmWidth;w++)
345 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
346 if ((w&7) == 7)
347 sbuf++;
349 startline += bitmap.bmWidthBytes;
351 break;
352 case 4:
353 for (h=0;h<height;h++)
355 sbuf = startline;
356 for (w=0;w<bitmap.bmWidth;w++)
358 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
359 else XPutPixel( image, w, h, *sbuf++ & 0xf );
361 startline += bitmap.bmWidthBytes;
363 break;
364 case 8:
365 for (h=0;h<height;h++)
367 sbuf = startline;
368 for (w=0;w<bitmap.bmWidth;w++)
369 XPutPixel(image,w,h,*sbuf++);
370 startline += bitmap.bmWidthBytes;
372 break;
373 case 15:
374 case 16:
375 for (h=0;h<height;h++)
377 sbuf = startline;
378 for (w=0;w<bitmap.bmWidth;w++)
380 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
381 sbuf+=2;
383 startline += bitmap.bmWidthBytes;
385 break;
386 case 24:
387 for (h=0;h<height;h++)
389 sbuf = startline;
390 for (w=0;w<bitmap.bmWidth;w++)
392 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
393 sbuf += 3;
395 startline += bitmap.bmWidthBytes;
397 break;
398 case 32:
399 for (h=0;h<height;h++)
401 sbuf = startline;
402 for (w=0;w<bitmap.bmWidth;w++)
404 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
405 sbuf += 4;
407 startline += bitmap.bmWidthBytes;
409 break;
410 default:
411 FIXME("Unhandled bits:%d\n", physBitmap->pixmap_depth);
414 XPutImage( gdi_display, physBitmap->pixmap, BITMAP_GC(physBitmap),
415 image, 0, 0, 0, 0, bitmap.bmWidth, height );
416 XDestroyImage( image ); /* frees image->data too */
417 wine_tsx11_unlock();
418 return count;
421 /***********************************************************************
422 * DeleteBitmap (X11DRV.@)
424 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
426 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
428 if (physBitmap)
430 DIBSECTION dib;
432 if (GetObjectW( hbitmap, sizeof(dib), &dib ) == sizeof(dib))
433 X11DRV_DIB_DeleteDIBSection( physBitmap, &dib );
435 if (physBitmap->glxpixmap) destroy_glxpixmap(physBitmap->glxpixmap);
436 wine_tsx11_lock();
437 if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
438 XDeleteContext( gdi_display, (XID)hbitmap, bitmap_context );
439 wine_tsx11_unlock();
440 HeapFree( GetProcessHeap(), 0, physBitmap );
442 return TRUE;
446 /***********************************************************************
447 * X11DRV_get_phys_bitmap
449 * Retrieve the X physical bitmap info.
451 X_PHYSBITMAP *X11DRV_get_phys_bitmap( HBITMAP hbitmap )
453 X_PHYSBITMAP *ret;
455 wine_tsx11_lock();
456 if (XFindContext( gdi_display, (XID)hbitmap, bitmap_context, (char **)&ret )) ret = NULL;
457 wine_tsx11_unlock();
458 return ret;
462 /***********************************************************************
463 * X11DRV_init_phys_bitmap
465 * Initialize the X physical bitmap info.
467 X_PHYSBITMAP *X11DRV_init_phys_bitmap( HBITMAP hbitmap )
469 X_PHYSBITMAP *ret;
471 if ((ret = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret) )) != NULL)
473 ret->hbitmap = hbitmap;
474 wine_tsx11_lock();
475 XSaveContext( gdi_display, (XID)hbitmap, bitmap_context, (char *)ret );
476 wine_tsx11_unlock();
478 return ret;
482 /***********************************************************************
483 * X11DRV_get_pixmap
485 * Retrieve the pixmap associated to a bitmap.
487 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
489 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
491 if (!physBitmap) return 0;
492 return physBitmap->pixmap;