Ignore the LPSURFACE bit if ALLOCONLOAD is set.
[wine/multimedia.git] / graphics / x11drv / bitmap.c
blobfb3826c7d3b92b4e8f09ef7f6f30965d221f2a96
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 "ts_xlib.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include "gdi.h"
29 #include "bitmap.h"
30 #include "wine/debug.h"
31 #include "x11drv.h"
32 #include "wingdi.h"
33 #include "windef.h"
34 #include "wine/winuser16.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
38 /* GCs used for B&W and color bitmap operations */
39 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
40 Pixmap BITMAP_stock_pixmap; /* pixmap for the default stock bitmap */
42 extern const DC_FUNCTIONS *X11DRV_DC_Funcs; /* hack */
44 /***********************************************************************
45 * X11DRV_BITMAP_Init
47 BOOL X11DRV_BITMAP_Init(void)
49 Pixmap tmpPixmap;
51 /* Create the necessary GCs */
53 wine_tsx11_lock();
54 BITMAP_stock_pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
55 BITMAP_monoGC = XCreateGC( gdi_display, BITMAP_stock_pixmap, 0, NULL );
56 XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
57 XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
59 if (screen_depth != 1)
61 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
63 BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
64 XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
65 XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
66 XFreePixmap( gdi_display, tmpPixmap );
69 wine_tsx11_unlock();
70 return TRUE;
73 /***********************************************************************
74 * SelectBitmap (X11DRV.@)
76 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
78 BITMAPOBJ *bmp;
79 DC *dc = physDev->dc;
81 if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
83 if(physDev->xrender)
84 X11DRV_XRender_UpdateDrawable( physDev );
86 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
87 physDev->drawable = BITMAP_stock_pixmap;
88 else
89 physDev->drawable = (Pixmap)bmp->physBitmap;
91 /* Change GC depth if needed */
93 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
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 GDI_ReleaseObj( hbitmap );
104 return hbitmap;
108 /****************************************************************************
109 * CreateBitmap (X11DRV.@)
111 * Create a device dependent X11 bitmap
113 * Returns TRUE on success else FALSE
115 BOOL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
117 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
119 if(!bmp) {
120 WARN("Bad bitmap handle %p\n", hbitmap);
121 return FALSE;
124 /* Check parameters */
125 if (bmp->bitmap.bmPlanes != 1)
127 GDI_ReleaseObj( hbitmap );
128 return 0;
130 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
132 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
133 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
134 GDI_ReleaseObj( hbitmap );
135 return FALSE;
137 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
139 ERR( "called for stock bitmap, please report\n" );
140 GDI_ReleaseObj( hbitmap );
141 return FALSE;
144 TRACE("(%p) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
145 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
147 /* Create the pixmap */
148 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
149 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
150 bmp->bitmap.bmBitsPixel)))
152 WARN("Can't create Pixmap\n");
153 GDI_ReleaseObj( hbitmap );
154 return FALSE;
157 if (bmp->bitmap.bmBits) /* Set bitmap bits */
158 X11DRV_SetBitmapBits( hbitmap, bmp->bitmap.bmBits,
159 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes );
161 GDI_ReleaseObj( hbitmap );
162 return TRUE;
166 /***********************************************************************
167 * GetBitmapBits (X11DRV.@)
169 * RETURNS
170 * Success: Number of bytes copied
171 * Failure: 0
173 LONG X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
175 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
176 LONG old_height, height;
177 XImage *image;
178 LPBYTE tbuf, startline;
179 int h, w;
181 if (!bmp) return 0;
182 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
184 wine_tsx11_lock();
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)bmp->physBitmap,
193 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
194 AllPlanes, ZPixmap );
195 bmp->bitmap.bmHeight = old_height;
197 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
199 startline = buffer;
200 switch (bmp->bitmap.bmBitsPixel)
202 case 1:
203 for (h=0;h<height;h++)
205 tbuf = startline;
206 *tbuf = 0;
207 for (w=0;w<bmp->bitmap.bmWidth;w++)
209 if ((w%8) == 0)
210 *tbuf = 0;
211 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
212 if ((w&7) == 7) ++tbuf;
214 startline += bmp->bitmap.bmWidthBytes;
216 break;
217 case 4:
218 for (h=0;h<height;h++)
220 tbuf = startline;
221 for (w=0;w<bmp->bitmap.bmWidth;w++)
223 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
224 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
226 startline += bmp->bitmap.bmWidthBytes;
228 break;
229 case 8:
230 for (h=0;h<height;h++)
232 tbuf = startline;
233 for (w=0;w<bmp->bitmap.bmWidth;w++)
234 *tbuf++ = XGetPixel(image,w,h);
235 startline += bmp->bitmap.bmWidthBytes;
237 break;
238 case 15:
239 case 16:
240 for (h=0;h<height;h++)
242 tbuf = startline;
243 for (w=0;w<bmp->bitmap.bmWidth;w++)
245 long pixel = XGetPixel(image,w,h);
247 *tbuf++ = pixel & 0xff;
248 *tbuf++ = (pixel>>8) & 0xff;
250 startline += bmp->bitmap.bmWidthBytes;
252 break;
253 case 24:
254 for (h=0;h<height;h++)
256 tbuf = startline;
257 for (w=0;w<bmp->bitmap.bmWidth;w++)
259 long pixel = XGetPixel(image,w,h);
261 *tbuf++ = pixel & 0xff;
262 *tbuf++ = (pixel>> 8) & 0xff;
263 *tbuf++ = (pixel>>16) & 0xff;
265 startline += bmp->bitmap.bmWidthBytes;
267 break;
269 case 32:
270 for (h=0;h<height;h++)
272 tbuf = startline;
273 for (w=0;w<bmp->bitmap.bmWidth;w++)
275 long pixel = XGetPixel(image,w,h);
277 *tbuf++ = pixel & 0xff;
278 *tbuf++ = (pixel>> 8) & 0xff;
279 *tbuf++ = (pixel>>16) & 0xff;
280 *tbuf++ = (pixel>>24) & 0xff;
282 startline += bmp->bitmap.bmWidthBytes;
284 break;
285 default:
286 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
288 XDestroyImage( image );
289 wine_tsx11_unlock();
290 GDI_ReleaseObj( hbitmap );
291 return count;
296 /******************************************************************************
297 * SetBitmapBits (X11DRV.@)
299 * RETURNS
300 * Success: Number of bytes used in setting the bitmap bits
301 * Failure: 0
303 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
305 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
306 LONG height;
307 XImage *image;
308 const BYTE *sbuf, *startline;
309 int w, h;
311 if (!bmp) return 0;
312 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
314 height = count / bmp->bitmap.bmWidthBytes;
316 wine_tsx11_lock();
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 );
323 wine_tsx11_unlock();
324 GDI_ReleaseObj( hbitmap );
325 return 0;
328 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
330 startline = bits;
332 switch (bmp->bitmap.bmBitsPixel)
334 case 1:
335 for (h=0;h<height;h++)
337 sbuf = startline;
338 for (w=0;w<bmp->bitmap.bmWidth;w++)
340 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
341 if ((w&7) == 7)
342 sbuf++;
344 startline += bmp->bitmap.bmWidthBytes;
346 break;
347 case 4:
348 for (h=0;h<height;h++)
350 sbuf = startline;
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;
358 break;
359 case 8:
360 for (h=0;h<height;h++)
362 sbuf = startline;
363 for (w=0;w<bmp->bitmap.bmWidth;w++)
364 XPutPixel(image,w,h,*sbuf++);
365 startline += bmp->bitmap.bmWidthBytes;
367 break;
368 case 15:
369 case 16:
370 for (h=0;h<height;h++)
372 sbuf = startline;
373 for (w=0;w<bmp->bitmap.bmWidth;w++)
375 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
376 sbuf+=2;
378 startline += bmp->bitmap.bmWidthBytes;
380 break;
381 case 24:
382 for (h=0;h<height;h++)
384 sbuf = startline;
385 for (w=0;w<bmp->bitmap.bmWidth;w++)
387 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
388 sbuf += 3;
390 startline += bmp->bitmap.bmWidthBytes;
392 break;
393 case 32:
394 for (h=0;h<height;h++)
396 sbuf = startline;
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]);
400 sbuf += 4;
402 startline += bmp->bitmap.bmWidthBytes;
404 break;
405 default:
406 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
409 XPutImage( gdi_display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
410 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
411 XDestroyImage( image ); /* frees image->data too */
412 wine_tsx11_unlock();
413 GDI_ReleaseObj( hbitmap );
414 return count;
417 /***********************************************************************
418 * DeleteBitmap (X11DRV.@)
420 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
422 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
423 if (bmp)
425 if (bmp->physBitmap) TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
426 bmp->physBitmap = NULL;
427 if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
428 GDI_ReleaseObj( hbitmap );
430 return TRUE;
433 /**************************************************************************
434 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
436 * Allocates an HBITMAP which references the Pixmap passed in.
437 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
438 * calling DeleteObject on this will free the Pixmap as well.
440 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
442 HBITMAP hBmp = 0;
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 if ( 0 == TSXGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
451 &border_width, &depth) )
452 goto END;
454 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
455 width, height, depth);
458 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
459 * and make it a container for the pixmap passed.
461 hBmp = CreateBitmap( width, height, 1, depth, NULL );
463 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
465 pBmp->funcs = X11DRV_DC_Funcs;
466 pBmp->physBitmap = (void *)pixmap;
467 GDI_ReleaseObj( hBmp );
469 END:
470 TRACE("\tReturning HBITMAP %p\n", hBmp);
471 return hBmp;
475 /**************************************************************************
476 * X11DRV_BITMAP_CreateBitmapFromPixmap
478 * Allocates an HBITMAP and copies the Pixmap data into it.
479 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
481 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
483 HBITMAP hBmp = 0, hBmpCopy = 0;
484 BITMAPOBJ *pBmp = NULL;
485 unsigned int width, height;
487 /* Allocate an HBITMAP which references the Pixmap passed to us */
488 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
489 if (!hBmp)
491 TRACE("\tCould not create bitmap header for Pixmap\n");
492 goto END;
495 /* Get the bitmap dimensions */
496 width = pBmp->bitmap.bmWidth;
497 height = pBmp->bitmap.bmHeight;
499 hBmpCopy = (HBITMAP)CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
501 /* We can now get rid of the HBITMAP wrapper we created earlier.
502 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
504 if (!bDeletePixmap)
506 /* Manually clear the bitmap internals to prevent the Pixmap
507 * from being deleted by DeleteObject.
509 pBmp->physBitmap = NULL;
510 pBmp->funcs = NULL;
512 DeleteObject(hBmp);
514 END:
515 TRACE("\tReturning HBITMAP %p\n", hBmpCopy);
516 return hBmpCopy;
520 /**************************************************************************
521 * X11DRV_BITMAP_CreatePixmapFromBitmap
523 * Creates a Pixmap from a bitmap
525 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
527 HGLOBAL hPackedDIB = 0;
528 Pixmap pixmap = 0;
531 * Create a packed DIB from the bitmap passed to us.
532 * A packed DIB contains a BITMAPINFO structure followed immediately by
533 * an optional color palette and the pixel data.
535 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
537 /* Create a Pixmap from the packed DIB */
538 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
540 /* Free the temporary packed DIB */
541 GlobalFree(hPackedDIB);
543 return pixmap;
547 /***********************************************************************
548 * X11DRV_BITMAP_Pixmap
550 * This function exists solely for x11 driver of the window system.
552 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
554 Pixmap pixmap;
555 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
556 if (bmp) {
557 pixmap = (Pixmap)bmp->physBitmap;
558 GDI_ReleaseObj( hbitmap );
560 else {
561 ERR("handle %p returned no obj\n", hbitmap);
562 pixmap = 0;
564 return pixmap;