- change the internal functions in windows/cursoricon.c to use 32bit
[wine/multimedia.git] / graphics / x11drv / bitmap.c
blob4a6e03448dc310bca1d1d781828381f74b25d6fc
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 (hbitmap == GetStockObject(DEFAULT_BITMAP))
84 physDev->drawable = BITMAP_stock_pixmap;
85 else
86 physDev->drawable = (Pixmap)bmp->physBitmap;
88 /* Change GC depth if needed */
90 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
92 wine_tsx11_lock();
93 XFreeGC( gdi_display, physDev->gc );
94 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
95 XSetGraphicsExposures( gdi_display, physDev->gc, False );
96 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
97 XFlush( gdi_display );
98 wine_tsx11_unlock();
100 GDI_ReleaseObj( hbitmap );
101 return hbitmap;
105 /****************************************************************************
106 * CreateBitmap (X11DRV.@)
108 * Create a device dependent X11 bitmap
110 * Returns TRUE on success else FALSE
112 BOOL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
114 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
116 if(!bmp) {
117 WARN("Bad bitmap handle %08x\n", hbitmap);
118 return FALSE;
121 /* Check parameters */
122 if (bmp->bitmap.bmPlanes != 1)
124 GDI_ReleaseObj( hbitmap );
125 return 0;
127 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
129 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
130 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
131 GDI_ReleaseObj( hbitmap );
132 return FALSE;
134 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
136 ERR( "called for stock bitmap, please report\n" );
137 GDI_ReleaseObj( hbitmap );
138 return FALSE;
141 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
142 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
144 /* Create the pixmap */
145 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
146 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
147 bmp->bitmap.bmBitsPixel)))
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->physBitmap) TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
423 bmp->physBitmap = NULL;
424 if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
425 GDI_ReleaseObj( hbitmap );
427 return TRUE;
430 /**************************************************************************
431 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
433 * Allocates an HBITMAP which references the Pixmap passed in.
434 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
435 * calling DeleteObject on this will free the Pixmap as well.
437 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
439 HBITMAP hBmp = 0;
440 BITMAPOBJ *pBmp = NULL;
441 Window root;
442 int x,y; /* Unused */
443 unsigned border_width; /* Unused */
444 unsigned int depth, width, height;
446 /* Get the Pixmap dimensions and bit depth */
447 if ( 0 == TSXGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
448 &border_width, &depth) )
449 goto END;
451 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
452 width, height, depth);
455 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
456 * and make it a container for the pixmap passed.
458 hBmp = CreateBitmap( width, height, 1, depth, NULL );
460 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
462 pBmp->funcs = X11DRV_DC_Funcs;
463 pBmp->physBitmap = (void *)pixmap;
464 GDI_ReleaseObj( hBmp );
466 END:
467 TRACE("\tReturning HBITMAP %x\n", hBmp);
468 return hBmp;
472 /**************************************************************************
473 * X11DRV_BITMAP_CreateBitmapFromPixmap
475 * Allocates an HBITMAP and copies the Pixmap data into it.
476 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
478 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
480 HBITMAP hBmp = 0, hBmpCopy = 0;
481 BITMAPOBJ *pBmp = NULL;
482 unsigned int width, height;
484 /* Allocate an HBITMAP which references the Pixmap passed to us */
485 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
486 if (!hBmp)
488 TRACE("\tCould not create bitmap header for Pixmap\n");
489 goto END;
492 /* Get the bitmap dimensions */
493 width = pBmp->bitmap.bmWidth;
494 height = pBmp->bitmap.bmHeight;
496 hBmpCopy = (HBITMAP)CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
498 /* We can now get rid of the HBITMAP wrapper we created earlier.
499 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
501 if (!bDeletePixmap)
503 /* Manually clear the bitmap internals to prevent the Pixmap
504 * from being deleted by DeleteObject.
506 pBmp->physBitmap = NULL;
507 pBmp->funcs = NULL;
509 DeleteObject(hBmp);
511 END:
512 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
513 return hBmpCopy;
517 /**************************************************************************
518 * X11DRV_BITMAP_CreatePixmapFromBitmap
520 * Creates a Pixmap from a bitmap
522 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
524 HGLOBAL hPackedDIB = 0;
525 Pixmap pixmap = 0;
528 * Create a packed DIB from the bitmap passed to us.
529 * A packed DIB contains a BITMAPINFO structure followed immediately by
530 * an optional color palette and the pixel data.
532 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
534 /* Create a Pixmap from the packed DIB */
535 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
537 /* Free the temporary packed DIB */
538 GlobalFree(hPackedDIB);
540 return pixmap;
544 /***********************************************************************
545 * X11DRV_BITMAP_Pixmap
547 * This function exists solely for x11 driver of the window system.
549 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
551 Pixmap pixmap;
552 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
553 if (bmp) {
554 pixmap = (Pixmap)bmp->physBitmap;
555 GDI_ReleaseObj( hbitmap );
557 else {
558 ERR("handle %08x returned no obj\n", hbitmap);
559 pixmap = 0;
561 return pixmap;