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
30 #include "wine/debug.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 /***********************************************************************
47 BOOL
X11DRV_BITMAP_Init(void)
51 /* Create the necessary GCs */
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
);
73 /***********************************************************************
74 * SelectBitmap (X11DRV.@)
76 HBITMAP
X11DRV_SelectBitmap( X11DRV_PDEVICE
*physDev
, HBITMAP hbitmap
)
81 if (!(bmp
= GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
))) return 0;
83 if (hbitmap
== GetStockObject(DEFAULT_BITMAP
))
84 physDev
->drawable
= BITMAP_stock_pixmap
;
86 physDev
->drawable
= (Pixmap
)bmp
->physBitmap
;
88 /* Change GC depth if needed */
90 if (dc
->bitsPerPixel
!= bmp
->bitmap
.bmBitsPixel
)
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
);
100 GDI_ReleaseObj( 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
);
117 WARN("Bad bitmap handle %08x\n", hbitmap
);
121 /* Check parameters */
122 if (bmp
->bitmap
.bmPlanes
!= 1)
124 GDI_ReleaseObj( hbitmap
);
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
);
134 if (hbitmap
== GetStockObject(DEFAULT_BITMAP
))
136 ERR( "called for stock bitmap, please report\n" );
137 GDI_ReleaseObj( hbitmap
);
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
);
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
);
163 /***********************************************************************
164 * GetBitmapBits (X11DRV.@)
167 * Success: Number of bytes copied
170 LONG
X11DRV_GetBitmapBits( HBITMAP hbitmap
, void *buffer
, LONG count
)
172 BITMAPOBJ
*bmp
= GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
173 LONG old_height
, height
;
175 LPBYTE tbuf
, startline
;
179 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp
, buffer
, count
);
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 */
197 switch (bmp
->bitmap
.bmBitsPixel
)
200 for (h
=0;h
<height
;h
++)
204 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
208 *tbuf
|= XGetPixel(image
,w
,h
)<<(7-(w
&7));
209 if ((w
&7) == 7) ++tbuf
;
211 startline
+= bmp
->bitmap
.bmWidthBytes
;
215 for (h
=0;h
<height
;h
++)
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
;
227 for (h
=0;h
<height
;h
++)
230 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
231 *tbuf
++ = XGetPixel(image
,w
,h
);
232 startline
+= bmp
->bitmap
.bmWidthBytes
;
237 for (h
=0;h
<height
;h
++)
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
;
251 for (h
=0;h
<height
;h
++)
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
;
267 for (h
=0;h
<height
;h
++)
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
;
283 FIXME("Unhandled bits:%d\n", bmp
->bitmap
.bmBitsPixel
);
285 XDestroyImage( image
);
287 GDI_ReleaseObj( hbitmap
);
293 /******************************************************************************
294 * SetBitmapBits (X11DRV.@)
297 * Success: Number of bytes used in setting the bitmap bits
300 LONG
X11DRV_SetBitmapBits( HBITMAP hbitmap
, const void *bits
, LONG count
)
302 BITMAPOBJ
*bmp
= GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
305 const BYTE
*sbuf
, *startline
;
309 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp
, bits
, count
);
311 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
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
);
321 GDI_ReleaseObj( hbitmap
);
325 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
329 switch (bmp
->bitmap
.bmBitsPixel
)
332 for (h
=0;h
<height
;h
++)
335 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
337 XPutPixel(image
,w
,h
,(sbuf
[0]>>(7-(w
&7))) & 1);
341 startline
+= bmp
->bitmap
.bmWidthBytes
;
345 for (h
=0;h
<height
;h
++)
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
;
357 for (h
=0;h
<height
;h
++)
360 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
361 XPutPixel(image
,w
,h
,*sbuf
++);
362 startline
+= bmp
->bitmap
.bmWidthBytes
;
367 for (h
=0;h
<height
;h
++)
370 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
372 XPutPixel(image
,w
,h
,sbuf
[1]*256+sbuf
[0]);
375 startline
+= bmp
->bitmap
.bmWidthBytes
;
379 for (h
=0;h
<height
;h
++)
382 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
384 XPutPixel(image
,w
,h
,(sbuf
[2]<<16)+(sbuf
[1]<<8)+sbuf
[0]);
387 startline
+= bmp
->bitmap
.bmWidthBytes
;
391 for (h
=0;h
<height
;h
++)
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]);
399 startline
+= bmp
->bitmap
.bmWidthBytes
;
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 */
410 GDI_ReleaseObj( hbitmap
);
414 /***********************************************************************
415 * DeleteBitmap (X11DRV.@)
417 BOOL
X11DRV_DeleteBitmap( HBITMAP hbitmap
)
419 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
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
);
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
)
440 BITMAPOBJ
*pBmp
= NULL
;
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
) )
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
);
467 TRACE("\tReturning HBITMAP %x\n", 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
);
488 TRACE("\tCould not create bitmap header for Pixmap\n");
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.
503 /* Manually clear the bitmap internals to prevent the Pixmap
504 * from being deleted by DeleteObject.
506 pBmp
->physBitmap
= NULL
;
512 TRACE("\tReturning HBITMAP %x\n", 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;
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
);
544 /***********************************************************************
545 * X11DRV_BITMAP_Pixmap
547 * This function exists solely for x11 driver of the window system.
549 Pixmap
X11DRV_BITMAP_Pixmap(HBITMAP hbitmap
)
552 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
554 pixmap
= (Pixmap
)bmp
->physBitmap
;
555 GDI_ReleaseObj( hbitmap
);
558 ERR("handle %08x returned no obj\n", hbitmap
);