2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
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
31 #include "wine/debug.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 /***********************************************************************
47 BOOL X11DRV_BITMAP_Init(void)
51 /* Create the necessary GCs */
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 );
76 /***********************************************************************
77 * SelectBitmap (X11DRV.@)
79 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
84 if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
88 if(!X11DRV_CreateBitmap(hbitmap))
90 GDI_ReleaseObj( hbitmap );
95 if(bmp->funcs != dc->funcs) {
96 WARN("Trying to select non-X11 DDB into an X11 dc\n");
97 GDI_ReleaseObj( hbitmap );
101 physDev->drawable = (Pixmap)bmp->physBitmap;
103 /* Change GC depth if needed */
105 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
108 XFreeGC( gdi_display, physDev->gc );
109 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
110 XSetGraphicsExposures( gdi_display, physDev->gc, False );
111 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
112 XFlush( gdi_display );
115 GDI_ReleaseObj( hbitmap );
120 /****************************************************************************
122 * X11DRV_CreateBitmap
124 * Create a device dependent X11 bitmap
126 * Returns TRUE on success else FALSE
130 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
132 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
135 WARN("Bad bitmap handle %08x\n", hbitmap);
139 /* Check parameters */
140 if (bmp->bitmap.bmPlanes != 1)
142 GDI_ReleaseObj( hbitmap );
145 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
147 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
148 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
149 GDI_ReleaseObj( hbitmap );
153 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
154 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
156 /* Create the pixmap */
157 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
158 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
159 bmp->bitmap.bmBitsPixel)))
161 WARN("Can't create Pixmap\n");
162 GDI_ReleaseObj( hbitmap );
165 bmp->funcs = X11DRV_DC_Funcs;
167 if (bmp->bitmap.bmBits) /* Set bitmap bits */
168 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
169 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
172 GDI_ReleaseObj( hbitmap );
177 /***********************************************************************
178 * X11DRV_GetBitmapBits
181 * Success: Number of bytes copied
184 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
186 LONG old_height, height;
188 LPBYTE tbuf, startline;
191 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
195 /* Hack: change the bitmap height temporarily to avoid */
196 /* getting unnecessary bitmap rows. */
198 old_height = bmp->bitmap.bmHeight;
199 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
201 image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
202 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
203 AllPlanes, ZPixmap );
204 bmp->bitmap.bmHeight = old_height;
206 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
209 switch (bmp->bitmap.bmBitsPixel)
212 for (h=0;h<height;h++)
216 for (w=0;w<bmp->bitmap.bmWidth;w++)
220 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
221 if ((w&7) == 7) ++tbuf;
223 startline += bmp->bitmap.bmWidthBytes;
227 for (h=0;h<height;h++)
230 for (w=0;w<bmp->bitmap.bmWidth;w++)
232 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
233 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
235 startline += bmp->bitmap.bmWidthBytes;
239 for (h=0;h<height;h++)
242 for (w=0;w<bmp->bitmap.bmWidth;w++)
243 *tbuf++ = XGetPixel(image,w,h);
244 startline += bmp->bitmap.bmWidthBytes;
249 for (h=0;h<height;h++)
252 for (w=0;w<bmp->bitmap.bmWidth;w++)
254 long pixel = XGetPixel(image,w,h);
256 *tbuf++ = pixel & 0xff;
257 *tbuf++ = (pixel>>8) & 0xff;
259 startline += bmp->bitmap.bmWidthBytes;
263 for (h=0;h<height;h++)
266 for (w=0;w<bmp->bitmap.bmWidth;w++)
268 long pixel = XGetPixel(image,w,h);
270 *tbuf++ = pixel & 0xff;
271 *tbuf++ = (pixel>> 8) & 0xff;
272 *tbuf++ = (pixel>>16) & 0xff;
274 startline += bmp->bitmap.bmWidthBytes;
279 for (h=0;h<height;h++)
282 for (w=0;w<bmp->bitmap.bmWidth;w++)
284 long pixel = XGetPixel(image,w,h);
286 *tbuf++ = pixel & 0xff;
287 *tbuf++ = (pixel>> 8) & 0xff;
288 *tbuf++ = (pixel>>16) & 0xff;
289 *tbuf++ = (pixel>>24) & 0xff;
291 startline += bmp->bitmap.bmWidthBytes;
295 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
297 XDestroyImage( image );
304 /******************************************************************************
305 * X11DRV_SetBitmapBits
308 * Success: Number of bytes used in setting the bitmap bits
311 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
315 LPBYTE sbuf, startline;
318 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
320 height = count / bmp->bitmap.bmWidthBytes;
323 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
324 bmp->bitmap.bmWidth, height, 32, 0 );
325 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
327 WARN("No memory to create image data.\n");
328 XDestroyImage( image );
333 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
337 switch (bmp->bitmap.bmBitsPixel)
340 for (h=0;h<height;h++)
343 for (w=0;w<bmp->bitmap.bmWidth;w++)
345 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
349 startline += bmp->bitmap.bmWidthBytes;
353 for (h=0;h<height;h++)
356 for (w=0;w<bmp->bitmap.bmWidth;w++)
358 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
359 else XPutPixel( image, w, h, *sbuf++ & 0xf );
361 startline += bmp->bitmap.bmWidthBytes;
365 for (h=0;h<height;h++)
368 for (w=0;w<bmp->bitmap.bmWidth;w++)
369 XPutPixel(image,w,h,*sbuf++);
370 startline += bmp->bitmap.bmWidthBytes;
375 for (h=0;h<height;h++)
378 for (w=0;w<bmp->bitmap.bmWidth;w++)
380 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
383 startline += bmp->bitmap.bmWidthBytes;
387 for (h=0;h<height;h++)
390 for (w=0;w<bmp->bitmap.bmWidth;w++)
392 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
395 startline += bmp->bitmap.bmWidthBytes;
399 for (h=0;h<height;h++)
402 for (w=0;w<bmp->bitmap.bmWidth;w++)
404 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
407 startline += bmp->bitmap.bmWidthBytes;
411 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
414 XPutImage( gdi_display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
415 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
416 XDestroyImage( image ); /* frees image->data too */
421 /***********************************************************************
424 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
426 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
429 WARN("Bad bitmap handle %08x\n", hbitmap);
434 ret = X11DRV_GetBitmapBits(bmp, bits, count);
435 else if(flags == DDB_SET)
436 ret = X11DRV_SetBitmapBits(bmp, bits, count);
438 ERR("Unknown flags value %d\n", flags);
441 GDI_ReleaseObj( hbitmap );
445 /***********************************************************************
446 * X11DRV_BITMAP_DeleteObject
448 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap )
450 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
453 TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
454 bmp->physBitmap = NULL;
456 if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
457 GDI_ReleaseObj( hbitmap );
462 /**************************************************************************
463 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
465 * Allocates an HBITMAP which references the Pixmap passed in.
466 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
467 * calling DeleteObject on this will free the Pixmap as well.
469 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
472 BITMAPOBJ *pBmp = NULL;
474 int x,y; /* Unused */
475 unsigned border_width; /* Unused */
476 unsigned int depth, width, height;
478 /* Get the Pixmap dimensions and bit depth */
479 if ( 0 == TSXGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
480 &border_width, &depth) )
483 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
484 width, height, depth);
487 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
488 * and make it a container for the pixmap passed.
490 hBmp = CreateBitmap( width, height, 1, depth, NULL );
492 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
494 pBmp->funcs = X11DRV_DC_Funcs;
495 pBmp->physBitmap = (void *)pixmap;
496 GDI_ReleaseObj( hBmp );
499 TRACE("\tReturning HBITMAP %x\n", hBmp);
504 /**************************************************************************
505 * X11DRV_BITMAP_CreateBitmapFromPixmap
507 * Allocates an HBITMAP and copies the Pixmap data into it.
508 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
510 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
512 HBITMAP hBmp = 0, hBmpCopy = 0;
513 BITMAPOBJ *pBmp = NULL;
514 unsigned int width, height;
516 /* Allocate an HBITMAP which references the Pixmap passed to us */
517 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
520 TRACE("\tCould not create bitmap header for Pixmap\n");
524 /* Get the bitmap dimensions */
525 width = pBmp->bitmap.bmWidth;
526 height = pBmp->bitmap.bmHeight;
528 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
530 /* We can now get rid of the HBITMAP wrapper we created earlier.
531 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
535 /* Manually clear the bitmap internals to prevent the Pixmap
536 * from being deleted by DeleteObject.
538 pBmp->physBitmap = NULL;
544 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
549 /**************************************************************************
550 * X11DRV_BITMAP_CreatePixmapFromBitmap
552 * Creates a Pixmap from a bitmap
554 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
556 HGLOBAL hPackedDIB = 0;
560 * Create a packed DIB from the bitmap passed to us.
561 * A packed DIB contains a BITMAPINFO structure followed immediately by
562 * an optional color palette and the pixel data.
564 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
566 /* Create a Pixmap from the packed DIB */
567 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
569 /* Free the temporary packed DIB */
570 GlobalFree(hPackedDIB);
576 /***********************************************************************
577 * X11DRV_BITMAP_Pixmap
579 * This function exists solely for x11 driver of the window system.
581 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
584 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
586 pixmap = (Pixmap)bmp->physBitmap;
587 GDI_ReleaseObj( hbitmap );
590 ERR("handle %08x returned no obj\n", hbitmap);