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 )
85 if (!(dc->flags & DC_MEMORY)) return 0;
86 if (hbitmap == dc->hBitmap) return hbitmap; /* nothing to do */
87 if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
89 if (bmp->header.dwCount && (hbitmap != GetStockObject(DEFAULT_BITMAP)))
91 WARN( "Bitmap already selected in another DC\n" );
92 GDI_ReleaseObj( hbitmap );
98 if(!X11DRV_CreateBitmap(hbitmap))
100 GDI_ReleaseObj( hbitmap );
105 if(bmp->funcs != dc->funcs) {
106 WARN("Trying to select non-X11 DDB into an X11 dc\n");
107 GDI_ReleaseObj( hbitmap );
111 if (!(hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight)))
113 GDI_ReleaseObj( hbitmap );
117 dc->totalExtent.left = 0;
118 dc->totalExtent.top = 0;
119 dc->totalExtent.right = bmp->bitmap.bmWidth;
120 dc->totalExtent.bottom = bmp->bitmap.bmHeight;
122 physDev->drawable = (Pixmap)bmp->physBitmap;
123 dc->hBitmap = hbitmap;
125 SelectVisRgn16( dc->hSelf, hrgn );
126 DeleteObject( hrgn );
128 /* Change GC depth if needed */
130 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
133 XFreeGC( gdi_display, physDev->gc );
134 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
135 XSetGraphicsExposures( gdi_display, physDev->gc, False );
136 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
137 XFlush( gdi_display );
139 dc->bitsPerPixel = bmp->bitmap.bmBitsPixel;
142 GDI_ReleaseObj( hbitmap );
147 /****************************************************************************
149 * X11DRV_CreateBitmap
151 * Create a device dependent X11 bitmap
153 * Returns TRUE on success else FALSE
157 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
159 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
162 WARN("Bad bitmap handle %08x\n", hbitmap);
166 /* Check parameters */
167 if (bmp->bitmap.bmPlanes != 1)
169 GDI_ReleaseObj( hbitmap );
172 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
174 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
175 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
176 GDI_ReleaseObj( hbitmap );
180 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
181 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
183 /* Create the pixmap */
184 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(gdi_display, root_window,
185 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
186 bmp->bitmap.bmBitsPixel)))
188 WARN("Can't create Pixmap\n");
189 GDI_ReleaseObj( hbitmap );
192 bmp->funcs = X11DRV_DC_Funcs;
194 if (bmp->bitmap.bmBits) /* Set bitmap bits */
195 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
196 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
199 GDI_ReleaseObj( hbitmap );
204 /***********************************************************************
205 * X11DRV_GetBitmapBits
208 * Success: Number of bytes copied
211 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
213 LONG old_height, height;
215 LPBYTE tbuf, startline;
218 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
222 /* Hack: change the bitmap height temporarily to avoid */
223 /* getting unnecessary bitmap rows. */
225 old_height = bmp->bitmap.bmHeight;
226 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
228 image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
229 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
230 AllPlanes, ZPixmap );
231 bmp->bitmap.bmHeight = old_height;
233 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
236 switch (bmp->bitmap.bmBitsPixel)
239 for (h=0;h<height;h++)
243 for (w=0;w<bmp->bitmap.bmWidth;w++)
247 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
248 if ((w&7) == 7) ++tbuf;
250 startline += bmp->bitmap.bmWidthBytes;
254 for (h=0;h<height;h++)
257 for (w=0;w<bmp->bitmap.bmWidth;w++)
259 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
260 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
262 startline += bmp->bitmap.bmWidthBytes;
266 for (h=0;h<height;h++)
269 for (w=0;w<bmp->bitmap.bmWidth;w++)
270 *tbuf++ = XGetPixel(image,w,h);
271 startline += bmp->bitmap.bmWidthBytes;
276 for (h=0;h<height;h++)
279 for (w=0;w<bmp->bitmap.bmWidth;w++)
281 long pixel = XGetPixel(image,w,h);
283 *tbuf++ = pixel & 0xff;
284 *tbuf++ = (pixel>>8) & 0xff;
286 startline += bmp->bitmap.bmWidthBytes;
290 for (h=0;h<height;h++)
293 for (w=0;w<bmp->bitmap.bmWidth;w++)
295 long pixel = XGetPixel(image,w,h);
297 *tbuf++ = pixel & 0xff;
298 *tbuf++ = (pixel>> 8) & 0xff;
299 *tbuf++ = (pixel>>16) & 0xff;
301 startline += bmp->bitmap.bmWidthBytes;
306 for (h=0;h<height;h++)
309 for (w=0;w<bmp->bitmap.bmWidth;w++)
311 long pixel = XGetPixel(image,w,h);
313 *tbuf++ = pixel & 0xff;
314 *tbuf++ = (pixel>> 8) & 0xff;
315 *tbuf++ = (pixel>>16) & 0xff;
316 *tbuf++ = (pixel>>24) & 0xff;
318 startline += bmp->bitmap.bmWidthBytes;
322 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
324 XDestroyImage( image );
331 /******************************************************************************
332 * X11DRV_SetBitmapBits
335 * Success: Number of bytes used in setting the bitmap bits
338 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
342 LPBYTE sbuf, startline;
345 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
347 height = count / bmp->bitmap.bmWidthBytes;
350 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
351 bmp->bitmap.bmWidth, height, 32, 0 );
352 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
354 WARN("No memory to create image data.\n");
355 XDestroyImage( image );
360 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
364 switch (bmp->bitmap.bmBitsPixel)
367 for (h=0;h<height;h++)
370 for (w=0;w<bmp->bitmap.bmWidth;w++)
372 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
376 startline += bmp->bitmap.bmWidthBytes;
380 for (h=0;h<height;h++)
383 for (w=0;w<bmp->bitmap.bmWidth;w++)
385 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
386 else XPutPixel( image, w, h, *sbuf++ & 0xf );
388 startline += bmp->bitmap.bmWidthBytes;
392 for (h=0;h<height;h++)
395 for (w=0;w<bmp->bitmap.bmWidth;w++)
396 XPutPixel(image,w,h,*sbuf++);
397 startline += bmp->bitmap.bmWidthBytes;
402 for (h=0;h<height;h++)
405 for (w=0;w<bmp->bitmap.bmWidth;w++)
407 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
410 startline += bmp->bitmap.bmWidthBytes;
414 for (h=0;h<height;h++)
417 for (w=0;w<bmp->bitmap.bmWidth;w++)
419 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
422 startline += bmp->bitmap.bmWidthBytes;
426 for (h=0;h<height;h++)
429 for (w=0;w<bmp->bitmap.bmWidth;w++)
431 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
434 startline += bmp->bitmap.bmWidthBytes;
438 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
441 XPutImage( gdi_display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
442 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
443 XDestroyImage( image ); /* frees image->data too */
448 /***********************************************************************
451 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
453 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
456 WARN("Bad bitmap handle %08x\n", hbitmap);
461 ret = X11DRV_GetBitmapBits(bmp, bits, count);
462 else if(flags == DDB_SET)
463 ret = X11DRV_SetBitmapBits(bmp, bits, count);
465 ERR("Unknown flags value %d\n", flags);
468 GDI_ReleaseObj( hbitmap );
472 /***********************************************************************
473 * X11DRV_BITMAP_DeleteObject
475 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap )
477 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
480 TSXFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
481 bmp->physBitmap = NULL;
483 if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
484 GDI_ReleaseObj( hbitmap );
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)
499 BITMAPOBJ *pBmp = NULL;
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) )
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 );
526 TRACE("\tReturning HBITMAP %x\n", 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);
547 TRACE("\tCould not create bitmap header for Pixmap\n");
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.
562 /* Manually clear the bitmap internals to prevent the Pixmap
563 * from being deleted by DeleteObject.
565 pBmp->physBitmap = NULL;
571 TRACE("\tReturning HBITMAP %x\n", 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;
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);
603 /***********************************************************************
604 * X11DRV_BITMAP_Pixmap
606 * This function exists solely for x11 driver of the window system.
608 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
611 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
613 pixmap = (Pixmap)bmp->physBitmap;
614 GDI_ReleaseObj( hbitmap );
617 ERR("handle %08x returned no obj\n", hbitmap);