Added LGPL standard comment, and copyright notices where necessary.
[wine/multimedia.git] / graphics / x11drv / bitmap.c
blob3ab5f71322de72bdeb27ab68096f24c79f0aa8fe
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"
25 #include "ts_xutil.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include "gdi.h"
30 #include "bitmap.h"
31 #include "wine/debug.h"
32 #include "x11drv.h"
33 #include "wingdi.h"
34 #include "windef.h"
35 #include "wine/winuser16.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
39 /* GCs used for B&W and color bitmap operations */
40 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
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 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 )))
56 BITMAP_monoGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
57 XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
58 XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
59 XFreePixmap( gdi_display, tmpPixmap );
62 if (screen_depth != 1)
64 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
66 BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
67 XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
68 XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
69 XFreePixmap( gdi_display, tmpPixmap );
72 wine_tsx11_unlock();
73 return TRUE;
76 /***********************************************************************
77 * X11DRV_BITMAP_SelectObject
79 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap )
81 BITMAPOBJ *bmp;
82 HRGN hrgn;
83 HBITMAP prevHandle = dc->hBitmap;
84 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
86 if (!(dc->flags & DC_MEMORY)) return 0;
87 if (hbitmap == dc->hBitmap) return hbitmap; /* nothing to do */
88 if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
90 if (bmp->header.dwCount && (hbitmap != GetStockObject(DEFAULT_BITMAP)))
92 WARN( "Bitmap already selected in another DC\n" );
93 GDI_ReleaseObj( hbitmap );
94 return 0;
97 if(!bmp->physBitmap)
99 if(!X11DRV_CreateBitmap(hbitmap))
101 GDI_ReleaseObj( hbitmap );
102 return 0;
106 if(bmp->funcs != dc->funcs) {
107 WARN("Trying to select non-X11 DDB into an X11 dc\n");
108 GDI_ReleaseObj( hbitmap );
109 return 0;
112 if (!(hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight)))
114 GDI_ReleaseObj( hbitmap );
115 return 0;
118 dc->totalExtent.left = 0;
119 dc->totalExtent.top = 0;
120 dc->totalExtent.right = bmp->bitmap.bmWidth;
121 dc->totalExtent.bottom = bmp->bitmap.bmHeight;
123 physDev->drawable = (Pixmap)bmp->physBitmap;
124 dc->hBitmap = hbitmap;
126 SelectVisRgn16( dc->hSelf, hrgn );
127 DeleteObject( hrgn );
129 /* Change GC depth if needed */
131 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
133 wine_tsx11_lock();
134 XFreeGC( gdi_display, physDev->gc );
135 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
136 XSetGraphicsExposures( gdi_display, physDev->gc, False );
137 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
138 XFlush( gdi_display );
139 wine_tsx11_unlock();
140 dc->bitsPerPixel = bmp->bitmap.bmBitsPixel;
141 DC_InitDC( dc );
143 GDI_ReleaseObj( hbitmap );
144 return prevHandle;
148 /****************************************************************************
150 * X11DRV_CreateBitmap
152 * Create a device dependent X11 bitmap
154 * Returns TRUE on success else FALSE
158 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
160 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
162 if(!bmp) {
163 WARN("Bad bitmap handle %08x\n", hbitmap);
164 return FALSE;
167 /* Check parameters */
168 if (bmp->bitmap.bmPlanes != 1)
170 GDI_ReleaseObj( hbitmap );
171 return 0;
173 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
175 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
176 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
177 GDI_ReleaseObj( hbitmap );
178 return FALSE;
181 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
182 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
184 /* Create the pixmap */
185 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
186 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
187 bmp->bitmap.bmBitsPixel)))
189 WARN("Can't create Pixmap\n");
190 GDI_ReleaseObj( hbitmap );
191 return FALSE;
193 bmp->funcs = X11DRV_DC_Funcs;
195 if (bmp->bitmap.bmBits) /* Set bitmap bits */
196 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
197 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
198 DDB_SET );
200 GDI_ReleaseObj( hbitmap );
201 return TRUE;
205 /***********************************************************************
206 * X11DRV_GetBitmapBits
208 * RETURNS
209 * Success: Number of bytes copied
210 * Failure: 0
212 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
214 LONG old_height, height;
215 XImage *image;
216 LPBYTE tbuf, startline;
217 int h, w;
219 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
221 wine_tsx11_lock();
223 /* Hack: change the bitmap height temporarily to avoid */
224 /* getting unnecessary bitmap rows. */
226 old_height = bmp->bitmap.bmHeight;
227 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
229 image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
230 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
231 AllPlanes, ZPixmap );
232 bmp->bitmap.bmHeight = old_height;
234 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
236 startline = buffer;
237 switch (bmp->bitmap.bmBitsPixel)
239 case 1:
240 for (h=0;h<height;h++)
242 tbuf = startline;
243 *tbuf = 0;
244 for (w=0;w<bmp->bitmap.bmWidth;w++)
246 if ((w%8) == 0)
247 *tbuf = 0;
248 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
249 if ((w&7) == 7) ++tbuf;
251 startline += bmp->bitmap.bmWidthBytes;
253 break;
254 case 4:
255 for (h=0;h<height;h++)
257 tbuf = startline;
258 for (w=0;w<bmp->bitmap.bmWidth;w++)
260 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
261 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
263 startline += bmp->bitmap.bmWidthBytes;
265 break;
266 case 8:
267 for (h=0;h<height;h++)
269 tbuf = startline;
270 for (w=0;w<bmp->bitmap.bmWidth;w++)
271 *tbuf++ = XGetPixel(image,w,h);
272 startline += bmp->bitmap.bmWidthBytes;
274 break;
275 case 15:
276 case 16:
277 for (h=0;h<height;h++)
279 tbuf = startline;
280 for (w=0;w<bmp->bitmap.bmWidth;w++)
282 long pixel = XGetPixel(image,w,h);
284 *tbuf++ = pixel & 0xff;
285 *tbuf++ = (pixel>>8) & 0xff;
287 startline += bmp->bitmap.bmWidthBytes;
289 break;
290 case 24:
291 for (h=0;h<height;h++)
293 tbuf = startline;
294 for (w=0;w<bmp->bitmap.bmWidth;w++)
296 long pixel = XGetPixel(image,w,h);
298 *tbuf++ = pixel & 0xff;
299 *tbuf++ = (pixel>> 8) & 0xff;
300 *tbuf++ = (pixel>>16) & 0xff;
302 startline += bmp->bitmap.bmWidthBytes;
304 break;
306 case 32:
307 for (h=0;h<height;h++)
309 tbuf = startline;
310 for (w=0;w<bmp->bitmap.bmWidth;w++)
312 long pixel = XGetPixel(image,w,h);
314 *tbuf++ = pixel & 0xff;
315 *tbuf++ = (pixel>> 8) & 0xff;
316 *tbuf++ = (pixel>>16) & 0xff;
317 *tbuf++ = (pixel>>24) & 0xff;
319 startline += bmp->bitmap.bmWidthBytes;
321 break;
322 default:
323 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
325 XDestroyImage( image );
326 wine_tsx11_unlock();
327 return count;
332 /******************************************************************************
333 * X11DRV_SetBitmapBits
335 * RETURNS
336 * Success: Number of bytes used in setting the bitmap bits
337 * Failure: 0
339 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
341 LONG height;
342 XImage *image;
343 LPBYTE sbuf, startline;
344 int w, h;
346 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
348 height = count / bmp->bitmap.bmWidthBytes;
350 wine_tsx11_lock();
351 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
352 bmp->bitmap.bmWidth, height, 32, 0 );
353 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
355 WARN("No memory to create image data.\n");
356 XDestroyImage( image );
357 wine_tsx11_unlock();
358 return 0;
361 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
363 startline = bits;
365 switch (bmp->bitmap.bmBitsPixel)
367 case 1:
368 for (h=0;h<height;h++)
370 sbuf = startline;
371 for (w=0;w<bmp->bitmap.bmWidth;w++)
373 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
374 if ((w&7) == 7)
375 sbuf++;
377 startline += bmp->bitmap.bmWidthBytes;
379 break;
380 case 4:
381 for (h=0;h<height;h++)
383 sbuf = startline;
384 for (w=0;w<bmp->bitmap.bmWidth;w++)
386 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
387 else XPutPixel( image, w, h, *sbuf++ & 0xf );
389 startline += bmp->bitmap.bmWidthBytes;
391 break;
392 case 8:
393 for (h=0;h<height;h++)
395 sbuf = startline;
396 for (w=0;w<bmp->bitmap.bmWidth;w++)
397 XPutPixel(image,w,h,*sbuf++);
398 startline += bmp->bitmap.bmWidthBytes;
400 break;
401 case 15:
402 case 16:
403 for (h=0;h<height;h++)
405 sbuf = startline;
406 for (w=0;w<bmp->bitmap.bmWidth;w++)
408 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
409 sbuf+=2;
411 startline += bmp->bitmap.bmWidthBytes;
413 break;
414 case 24:
415 for (h=0;h<height;h++)
417 sbuf = startline;
418 for (w=0;w<bmp->bitmap.bmWidth;w++)
420 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
421 sbuf += 3;
423 startline += bmp->bitmap.bmWidthBytes;
425 break;
426 case 32:
427 for (h=0;h<height;h++)
429 sbuf = startline;
430 for (w=0;w<bmp->bitmap.bmWidth;w++)
432 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
433 sbuf += 4;
435 startline += bmp->bitmap.bmWidthBytes;
437 break;
438 default:
439 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
442 XPutImage( gdi_display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
443 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
444 XDestroyImage( image ); /* frees image->data too */
445 wine_tsx11_unlock();
446 return count;
449 /***********************************************************************
450 * X11DRV_BitmapBits
452 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
454 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
455 LONG ret;
456 if(!bmp) {
457 WARN("Bad bitmap handle %08x\n", hbitmap);
458 return FALSE;
461 if(flags == DDB_GET)
462 ret = X11DRV_GetBitmapBits(bmp, bits, count);
463 else if(flags == DDB_SET)
464 ret = X11DRV_SetBitmapBits(bmp, bits, count);
465 else {
466 ERR("Unknown flags value %d\n", flags);
467 ret = 0;
469 GDI_ReleaseObj( hbitmap );
470 return ret;
473 /***********************************************************************
474 * X11DRV_BITMAP_DeleteObject
476 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap )
478 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
479 if (bmp)
481 TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
482 bmp->physBitmap = NULL;
483 bmp->funcs = NULL;
484 GDI_ReleaseObj( hbitmap );
486 return TRUE;
489 /**************************************************************************
490 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
492 * Allocates an HBITMAP which references the Pixmap passed in.
493 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
494 * calling DeleteObject on this will free the Pixmap as well.
496 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
498 HBITMAP hBmp = 0;
499 BITMAPOBJ *pBmp = NULL;
500 Window root;
501 int x,y; /* Unused */
502 unsigned border_width; /* Unused */
503 unsigned int depth, width, height;
505 /* Get the Pixmap dimensions and bit depth */
506 if ( 0 == TSXGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
507 &border_width, &depth) )
508 goto END;
510 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
511 width, height, depth);
514 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
515 * and make it a container for the pixmap passed.
517 hBmp = CreateBitmap( width, height, 1, depth, NULL );
519 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
521 pBmp->funcs = X11DRV_DC_Funcs;
522 pBmp->physBitmap = (void *)pixmap;
523 GDI_ReleaseObj( hBmp );
525 END:
526 TRACE("\tReturning HBITMAP %x\n", hBmp);
527 return hBmp;
531 /**************************************************************************
532 * X11DRV_BITMAP_CreateBitmapFromPixmap
534 * Allocates an HBITMAP and copies the Pixmap data into it.
535 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
537 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
539 HBITMAP hBmp = 0, hBmpCopy = 0;
540 BITMAPOBJ *pBmp = NULL;
541 unsigned int width, height;
543 /* Allocate an HBITMAP which references the Pixmap passed to us */
544 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
545 if (!hBmp)
547 TRACE("\tCould not create bitmap header for Pixmap\n");
548 goto END;
551 /* Get the bitmap dimensions */
552 width = pBmp->bitmap.bmWidth;
553 height = pBmp->bitmap.bmHeight;
555 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
557 /* We can now get rid of the HBITMAP wrapper we created earlier.
558 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
560 if (!bDeletePixmap)
562 /* Manually clear the bitmap internals to prevent the Pixmap
563 * from being deleted by DeleteObject.
565 pBmp->physBitmap = NULL;
566 pBmp->funcs = NULL;
568 DeleteObject(hBmp);
570 END:
571 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
572 return hBmpCopy;
576 /**************************************************************************
577 * X11DRV_BITMAP_CreatePixmapFromBitmap
579 * Creates a Pixmap from a bitmap
581 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
583 HGLOBAL hPackedDIB = 0;
584 Pixmap pixmap = 0;
587 * Create a packed DIB from the bitmap passed to us.
588 * A packed DIB contains a BITMAPINFO structure followed immediately by
589 * an optional color palette and the pixel data.
591 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
593 /* Create a Pixmap from the packed DIB */
594 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
596 /* Free the temporary packed DIB */
597 GlobalFree(hPackedDIB);
599 return pixmap;
603 /***********************************************************************
604 * X11DRV_BITMAP_Pixmap
606 * This function exists solely for x11 driver of the window system.
608 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
610 Pixmap pixmap;
611 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
612 if (bmp) {
613 pixmap = (Pixmap)bmp->physBitmap;
614 GDI_ReleaseObj( hbitmap );
616 else {
617 ERR("handle %08x returned no obj\n", hbitmap);
618 pixmap = 0;
620 return pixmap;