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 * X11DRV_BITMAP_SelectObject
79 HBITMAP
X11DRV_BITMAP_SelectObject( DC
* dc
, HBITMAP hbitmap
)
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
);
99 if(!X11DRV_CreateBitmap(hbitmap
))
101 GDI_ReleaseObj( hbitmap
);
106 if(bmp
->funcs
!= dc
->funcs
) {
107 WARN("Trying to select non-X11 DDB into an X11 dc\n");
108 GDI_ReleaseObj( hbitmap
);
112 if (!(hrgn
= CreateRectRgn(0, 0, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
)))
114 GDI_ReleaseObj( hbitmap
);
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
)
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
);
140 dc
->bitsPerPixel
= bmp
->bitmap
.bmBitsPixel
;
143 GDI_ReleaseObj( hbitmap
);
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
);
163 WARN("Bad bitmap handle %08x\n", hbitmap
);
167 /* Check parameters */
168 if (bmp
->bitmap
.bmPlanes
!= 1)
170 GDI_ReleaseObj( hbitmap
);
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
);
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
);
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
,
200 GDI_ReleaseObj( hbitmap
);
205 /***********************************************************************
206 * X11DRV_GetBitmapBits
209 * Success: Number of bytes copied
212 static LONG
X11DRV_GetBitmapBits(BITMAPOBJ
*bmp
, void *buffer
, LONG count
)
214 LONG old_height
, height
;
216 LPBYTE tbuf
, startline
;
219 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp
, buffer
, count
);
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 */
237 switch (bmp
->bitmap
.bmBitsPixel
)
240 for (h
=0;h
<height
;h
++)
244 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
248 *tbuf
|= XGetPixel(image
,w
,h
)<<(7-(w
&7));
249 if ((w
&7) == 7) ++tbuf
;
251 startline
+= bmp
->bitmap
.bmWidthBytes
;
255 for (h
=0;h
<height
;h
++)
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
;
267 for (h
=0;h
<height
;h
++)
270 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
271 *tbuf
++ = XGetPixel(image
,w
,h
);
272 startline
+= bmp
->bitmap
.bmWidthBytes
;
277 for (h
=0;h
<height
;h
++)
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
;
291 for (h
=0;h
<height
;h
++)
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
;
307 for (h
=0;h
<height
;h
++)
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
;
323 FIXME("Unhandled bits:%d\n", bmp
->bitmap
.bmBitsPixel
);
325 XDestroyImage( image
);
332 /******************************************************************************
333 * X11DRV_SetBitmapBits
336 * Success: Number of bytes used in setting the bitmap bits
339 static LONG
X11DRV_SetBitmapBits(BITMAPOBJ
*bmp
, void *bits
, LONG count
)
343 LPBYTE sbuf
, startline
;
346 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp
, bits
, count
);
348 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
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
);
361 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
365 switch (bmp
->bitmap
.bmBitsPixel
)
368 for (h
=0;h
<height
;h
++)
371 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
373 XPutPixel(image
,w
,h
,(sbuf
[0]>>(7-(w
&7))) & 1);
377 startline
+= bmp
->bitmap
.bmWidthBytes
;
381 for (h
=0;h
<height
;h
++)
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
;
393 for (h
=0;h
<height
;h
++)
396 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
397 XPutPixel(image
,w
,h
,*sbuf
++);
398 startline
+= bmp
->bitmap
.bmWidthBytes
;
403 for (h
=0;h
<height
;h
++)
406 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
408 XPutPixel(image
,w
,h
,sbuf
[1]*256+sbuf
[0]);
411 startline
+= bmp
->bitmap
.bmWidthBytes
;
415 for (h
=0;h
<height
;h
++)
418 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
420 XPutPixel(image
,w
,h
,(sbuf
[2]<<16)+(sbuf
[1]<<8)+sbuf
[0]);
423 startline
+= bmp
->bitmap
.bmWidthBytes
;
427 for (h
=0;h
<height
;h
++)
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]);
435 startline
+= bmp
->bitmap
.bmWidthBytes
;
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 */
449 /***********************************************************************
452 LONG
X11DRV_BitmapBits(HBITMAP hbitmap
, void *bits
, LONG count
, WORD flags
)
454 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
457 WARN("Bad bitmap handle %08x\n", hbitmap
);
462 ret
= X11DRV_GetBitmapBits(bmp
, bits
, count
);
463 else if(flags
== DDB_SET
)
464 ret
= X11DRV_SetBitmapBits(bmp
, bits
, count
);
466 ERR("Unknown flags value %d\n", flags
);
469 GDI_ReleaseObj( hbitmap
);
473 /***********************************************************************
474 * X11DRV_BITMAP_DeleteObject
476 BOOL
X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap
)
478 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
481 TSXFreePixmap( gdi_display
, (Pixmap
)bmp
->physBitmap
);
482 bmp
->physBitmap
= NULL
;
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
);