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
27 #include "wine/debug.h"
31 #include "wine/winuser16.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(x11drv
);
35 /* GCs used for B&W and color bitmap operations */
36 GC BITMAP_monoGC
= 0, BITMAP_colorGC
= 0;
37 Pixmap BITMAP_stock_pixmap
; /* pixmap for the default stock bitmap */
39 /***********************************************************************
42 void X11DRV_BITMAP_Init(void)
46 /* Create the necessary GCs */
49 BITMAP_stock_pixmap
= XCreatePixmap( gdi_display
, root_window
, 1, 1, 1 );
50 BITMAP_monoGC
= XCreateGC( gdi_display
, BITMAP_stock_pixmap
, 0, NULL
);
51 XSetGraphicsExposures( gdi_display
, BITMAP_monoGC
, False
);
52 XSetSubwindowMode( gdi_display
, BITMAP_monoGC
, IncludeInferiors
);
54 if (screen_depth
!= 1)
56 if ((tmpPixmap
= XCreatePixmap( gdi_display
, root_window
, 1, 1, screen_depth
)))
58 BITMAP_colorGC
= XCreateGC( gdi_display
, tmpPixmap
, 0, NULL
);
59 XSetGraphicsExposures( gdi_display
, BITMAP_colorGC
, False
);
60 XSetSubwindowMode( gdi_display
, BITMAP_colorGC
, IncludeInferiors
);
61 XFreePixmap( gdi_display
, tmpPixmap
);
67 /***********************************************************************
68 * SelectBitmap (X11DRV.@)
70 HBITMAP
X11DRV_SelectBitmap( X11DRV_PDEVICE
*physDev
, HBITMAP hbitmap
)
74 if (!(bmp
= GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
))) return 0;
77 X11DRV_XRender_UpdateDrawable( physDev
);
79 if (hbitmap
== GetStockObject(DEFAULT_BITMAP
))
80 physDev
->drawable
= BITMAP_stock_pixmap
;
82 physDev
->drawable
= (Pixmap
)bmp
->physBitmap
;
84 /* Change GC depth if needed */
86 if (physDev
->depth
!= bmp
->bitmap
.bmBitsPixel
)
88 physDev
->depth
= bmp
->bitmap
.bmBitsPixel
;
90 XFreeGC( gdi_display
, physDev
->gc
);
91 physDev
->gc
= XCreateGC( gdi_display
, physDev
->drawable
, 0, NULL
);
92 XSetGraphicsExposures( gdi_display
, physDev
->gc
, False
);
93 XSetSubwindowMode( gdi_display
, physDev
->gc
, IncludeInferiors
);
94 XFlush( gdi_display
);
97 GDI_ReleaseObj( hbitmap
);
102 /****************************************************************************
103 * CreateBitmap (X11DRV.@)
105 * Create a device dependent X11 bitmap
107 * Returns TRUE on success else FALSE
109 BOOL
X11DRV_CreateBitmap( X11DRV_PDEVICE
*physDev
, HBITMAP hbitmap
)
111 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
114 WARN("Bad bitmap handle %p\n", hbitmap
);
118 /* Check parameters */
119 if (bmp
->bitmap
.bmPlanes
!= 1)
121 GDI_ReleaseObj( hbitmap
);
124 if ((bmp
->bitmap
.bmBitsPixel
!= 1) && (bmp
->bitmap
.bmBitsPixel
!= screen_depth
))
126 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
127 bmp
->bitmap
.bmPlanes
, bmp
->bitmap
.bmBitsPixel
);
128 GDI_ReleaseObj( hbitmap
);
131 if (hbitmap
== GetStockObject(DEFAULT_BITMAP
))
133 ERR( "called for stock bitmap, please report\n" );
134 GDI_ReleaseObj( hbitmap
);
138 TRACE("(%p) %dx%d %d bpp\n", hbitmap
, bmp
->bitmap
.bmWidth
,
139 bmp
->bitmap
.bmHeight
, bmp
->bitmap
.bmBitsPixel
);
141 /* Create the pixmap */
143 bmp
->physBitmap
= (void *)XCreatePixmap(gdi_display
, root_window
,
144 bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
145 bmp
->bitmap
.bmBitsPixel
);
147 if (!bmp
->physBitmap
)
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
);
423 if (bmp
->physBitmap
) XFreePixmap( gdi_display
, (Pixmap
)bmp
->physBitmap
);
424 bmp
->physBitmap
= NULL
;
426 if (bmp
->dib
) X11DRV_DIB_DeleteDIBSection( bmp
);
427 GDI_ReleaseObj( hbitmap
);
432 /**************************************************************************
433 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
435 * Allocates an HBITMAP which references the Pixmap passed in.
436 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
437 * calling DeleteObject on this will free the Pixmap as well.
439 HBITMAP
X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(HDC hdc
, Pixmap pixmap
)
442 HBITMAP hBmp
= 0, old
;
443 BITMAPOBJ
*pBmp
= NULL
;
445 int x
,y
; /* Unused */
446 unsigned border_width
; /* Unused */
447 unsigned int depth
, width
, height
;
449 /* Get the Pixmap dimensions and bit depth */
451 if (!XGetGeometry(gdi_display
, pixmap
, &root
, &x
, &y
, &width
, &height
,
452 &border_width
, &depth
)) depth
= 0;
454 if (!depth
) goto END
;
456 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
457 width
, height
, depth
);
460 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
461 * and make it a container for the pixmap passed.
463 hBmp
= CreateBitmap( width
, height
, 1, depth
, NULL
);
465 /* force bitmap to be owned by a screen DC */
466 hdcMem
= CreateCompatibleDC( hdc
);
467 old
= SelectObject( hdcMem
, hBmp
);
469 pBmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hBmp
, BITMAP_MAGIC
);
471 if (pBmp
->physBitmap
) XFreePixmap( gdi_display
, (Pixmap
)pBmp
->physBitmap
);
472 pBmp
->physBitmap
= (void *)pixmap
;
473 GDI_ReleaseObj( hBmp
);
475 SelectObject( hdcMem
, old
);
479 TRACE("\tReturning HBITMAP %p\n", hBmp
);
484 /***********************************************************************
485 * X11DRV_BITMAP_Pixmap
487 * This function exists solely for x11 driver of the window system.
489 Pixmap
X11DRV_BITMAP_Pixmap(HBITMAP hbitmap
)
492 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
494 pixmap
= (Pixmap
)bmp
->physBitmap
;
495 GDI_ReleaseObj( hbitmap
);
498 ERR("handle %p returned no obj\n", hbitmap
);