If the dibsection is based on a file-mapping object, then make sure
[wine.git] / dlls / x11drv / bitmap.c
blob118515080504edfb9606b51e3bc94194f4a8d5f2
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "gdi.h"
27 #include "wine/debug.h"
28 #include "x11drv.h"
29 #include "wingdi.h"
30 #include "windef.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 /***********************************************************************
40 * X11DRV_BITMAP_Init
42 void X11DRV_BITMAP_Init(void)
44 Pixmap tmpPixmap;
46 /* Create the necessary GCs */
48 wine_tsx11_lock();
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 );
64 wine_tsx11_unlock();
67 /***********************************************************************
68 * SelectBitmap (X11DRV.@)
70 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
72 BITMAPOBJ *bmp;
74 if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
76 if(physDev->xrender)
77 X11DRV_XRender_UpdateDrawable( physDev );
79 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
80 physDev->drawable = BITMAP_stock_pixmap;
81 else
82 physDev->drawable = (Pixmap)bmp->physBitmap;
84 /* Change GC depth if needed */
86 if (physDev->depth != bmp->bitmap.bmBitsPixel)
88 physDev->depth = bmp->bitmap.bmBitsPixel;
89 wine_tsx11_lock();
90 XFreeGC( gdi_display, physDev->gc );
91 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
92 XSetGraphicsExposures( gdi_display, physDev->gc, False );
93 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
94 XFlush( gdi_display );
95 wine_tsx11_unlock();
97 GDI_ReleaseObj( hbitmap );
98 return hbitmap;
102 /****************************************************************************
103 * CreateBitmap (X11DRV.@)
105 * Create a device dependent X11 bitmap
107 * Returns TRUE on success else FALSE
109 BOOL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
111 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
113 if(!bmp) {
114 WARN("Bad bitmap handle %p\n", hbitmap);
115 return FALSE;
118 /* Check parameters */
119 if (bmp->bitmap.bmPlanes != 1)
121 GDI_ReleaseObj( hbitmap );
122 return 0;
124 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
126 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
127 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
128 GDI_ReleaseObj( hbitmap );
129 return FALSE;
131 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
133 ERR( "called for stock bitmap, please report\n" );
134 GDI_ReleaseObj( hbitmap );
135 return FALSE;
138 TRACE("(%p) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
139 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
141 /* Create the pixmap */
142 wine_tsx11_lock();
143 bmp->physBitmap = (void *)XCreatePixmap(gdi_display, root_window,
144 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
145 bmp->bitmap.bmBitsPixel);
146 wine_tsx11_unlock();
147 if (!bmp->physBitmap)
149 WARN("Can't create Pixmap\n");
150 GDI_ReleaseObj( hbitmap );
151 return FALSE;
154 if (bmp->bitmap.bmBits) /* Set bitmap bits */
155 X11DRV_SetBitmapBits( hbitmap, bmp->bitmap.bmBits,
156 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes );
158 GDI_ReleaseObj( hbitmap );
159 return TRUE;
163 /***********************************************************************
164 * GetBitmapBits (X11DRV.@)
166 * RETURNS
167 * Success: Number of bytes copied
168 * Failure: 0
170 LONG X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
172 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
173 LONG old_height, height;
174 XImage *image;
175 LPBYTE tbuf, startline;
176 int h, w;
178 if (!bmp) return 0;
179 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
181 wine_tsx11_lock();
183 /* Hack: change the bitmap height temporarily to avoid */
184 /* getting unnecessary bitmap rows. */
186 old_height = bmp->bitmap.bmHeight;
187 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
189 image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
190 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
191 AllPlanes, ZPixmap );
192 bmp->bitmap.bmHeight = old_height;
194 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
196 startline = buffer;
197 switch (bmp->bitmap.bmBitsPixel)
199 case 1:
200 for (h=0;h<height;h++)
202 tbuf = startline;
203 *tbuf = 0;
204 for (w=0;w<bmp->bitmap.bmWidth;w++)
206 if ((w%8) == 0)
207 *tbuf = 0;
208 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
209 if ((w&7) == 7) ++tbuf;
211 startline += bmp->bitmap.bmWidthBytes;
213 break;
214 case 4:
215 for (h=0;h<height;h++)
217 tbuf = startline;
218 for (w=0;w<bmp->bitmap.bmWidth;w++)
220 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
221 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
223 startline += bmp->bitmap.bmWidthBytes;
225 break;
226 case 8:
227 for (h=0;h<height;h++)
229 tbuf = startline;
230 for (w=0;w<bmp->bitmap.bmWidth;w++)
231 *tbuf++ = XGetPixel(image,w,h);
232 startline += bmp->bitmap.bmWidthBytes;
234 break;
235 case 15:
236 case 16:
237 for (h=0;h<height;h++)
239 tbuf = startline;
240 for (w=0;w<bmp->bitmap.bmWidth;w++)
242 long pixel = XGetPixel(image,w,h);
244 *tbuf++ = pixel & 0xff;
245 *tbuf++ = (pixel>>8) & 0xff;
247 startline += bmp->bitmap.bmWidthBytes;
249 break;
250 case 24:
251 for (h=0;h<height;h++)
253 tbuf = startline;
254 for (w=0;w<bmp->bitmap.bmWidth;w++)
256 long pixel = XGetPixel(image,w,h);
258 *tbuf++ = pixel & 0xff;
259 *tbuf++ = (pixel>> 8) & 0xff;
260 *tbuf++ = (pixel>>16) & 0xff;
262 startline += bmp->bitmap.bmWidthBytes;
264 break;
266 case 32:
267 for (h=0;h<height;h++)
269 tbuf = startline;
270 for (w=0;w<bmp->bitmap.bmWidth;w++)
272 long pixel = XGetPixel(image,w,h);
274 *tbuf++ = pixel & 0xff;
275 *tbuf++ = (pixel>> 8) & 0xff;
276 *tbuf++ = (pixel>>16) & 0xff;
277 *tbuf++ = (pixel>>24) & 0xff;
279 startline += bmp->bitmap.bmWidthBytes;
281 break;
282 default:
283 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
285 XDestroyImage( image );
286 wine_tsx11_unlock();
287 GDI_ReleaseObj( hbitmap );
288 return count;
293 /******************************************************************************
294 * SetBitmapBits (X11DRV.@)
296 * RETURNS
297 * Success: Number of bytes used in setting the bitmap bits
298 * Failure: 0
300 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
302 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
303 LONG height;
304 XImage *image;
305 const BYTE *sbuf, *startline;
306 int w, h;
308 if (!bmp) return 0;
309 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
311 height = count / bmp->bitmap.bmWidthBytes;
313 wine_tsx11_lock();
314 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
315 bmp->bitmap.bmWidth, height, 32, 0 );
316 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
318 WARN("No memory to create image data.\n");
319 XDestroyImage( image );
320 wine_tsx11_unlock();
321 GDI_ReleaseObj( hbitmap );
322 return 0;
325 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
327 startline = bits;
329 switch (bmp->bitmap.bmBitsPixel)
331 case 1:
332 for (h=0;h<height;h++)
334 sbuf = startline;
335 for (w=0;w<bmp->bitmap.bmWidth;w++)
337 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
338 if ((w&7) == 7)
339 sbuf++;
341 startline += bmp->bitmap.bmWidthBytes;
343 break;
344 case 4:
345 for (h=0;h<height;h++)
347 sbuf = startline;
348 for (w=0;w<bmp->bitmap.bmWidth;w++)
350 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
351 else XPutPixel( image, w, h, *sbuf++ & 0xf );
353 startline += bmp->bitmap.bmWidthBytes;
355 break;
356 case 8:
357 for (h=0;h<height;h++)
359 sbuf = startline;
360 for (w=0;w<bmp->bitmap.bmWidth;w++)
361 XPutPixel(image,w,h,*sbuf++);
362 startline += bmp->bitmap.bmWidthBytes;
364 break;
365 case 15:
366 case 16:
367 for (h=0;h<height;h++)
369 sbuf = startline;
370 for (w=0;w<bmp->bitmap.bmWidth;w++)
372 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
373 sbuf+=2;
375 startline += bmp->bitmap.bmWidthBytes;
377 break;
378 case 24:
379 for (h=0;h<height;h++)
381 sbuf = startline;
382 for (w=0;w<bmp->bitmap.bmWidth;w++)
384 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
385 sbuf += 3;
387 startline += bmp->bitmap.bmWidthBytes;
389 break;
390 case 32:
391 for (h=0;h<height;h++)
393 sbuf = startline;
394 for (w=0;w<bmp->bitmap.bmWidth;w++)
396 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
397 sbuf += 4;
399 startline += bmp->bitmap.bmWidthBytes;
401 break;
402 default:
403 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
406 XPutImage( gdi_display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
407 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
408 XDestroyImage( image ); /* frees image->data too */
409 wine_tsx11_unlock();
410 GDI_ReleaseObj( hbitmap );
411 return count;
414 /***********************************************************************
415 * DeleteBitmap (X11DRV.@)
417 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
419 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
420 if (bmp)
422 if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
423 wine_tsx11_lock();
424 if (bmp->physBitmap) XFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
425 bmp->physBitmap = NULL;
426 wine_tsx11_unlock();
427 GDI_ReleaseObj( hbitmap );
429 return TRUE;
432 /**************************************************************************
433 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
435 * Allocates an HBITMAP which references the Pixmap passed in.
436 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
437 * calling DeleteObject on this will free the Pixmap as well.
439 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(HDC hdc, Pixmap pixmap)
441 HDC hdcMem;
442 HBITMAP hBmp = 0, old;
443 BITMAPOBJ *pBmp = NULL;
444 Window root;
445 int x,y; /* Unused */
446 unsigned border_width; /* Unused */
447 unsigned int depth, width, height;
449 /* Get the Pixmap dimensions and bit depth */
450 wine_tsx11_lock();
451 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
452 &border_width, &depth)) depth = 0;
453 wine_tsx11_unlock();
454 if (!depth) goto END;
456 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
457 width, height, depth);
460 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
461 * and make it a container for the pixmap passed.
463 hBmp = CreateBitmap( width, height, 1, depth, NULL );
465 /* force bitmap to be owned by a screen DC */
466 hdcMem = CreateCompatibleDC( hdc );
467 old = SelectObject( hdcMem, hBmp );
469 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
471 if (pBmp->physBitmap) XFreePixmap( gdi_display, (Pixmap)pBmp->physBitmap );
472 pBmp->physBitmap = (void *)pixmap;
473 GDI_ReleaseObj( hBmp );
475 SelectObject( hdcMem, old );
476 DeleteDC( hdcMem );
478 END:
479 TRACE("\tReturning HBITMAP %p\n", hBmp);
480 return hBmp;
484 /***********************************************************************
485 * X11DRV_BITMAP_Pixmap
487 * This function exists solely for x11 driver of the window system.
489 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
491 Pixmap pixmap;
492 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
493 if (bmp) {
494 pixmap = (Pixmap)bmp->physBitmap;
495 GDI_ReleaseObj( hbitmap );
497 else {
498 ERR("handle %p returned no obj\n", hbitmap);
499 pixmap = 0;
501 return pixmap;