2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
10 #ifndef X_DISPLAY_MISSING
22 #include "debugtools.h"
28 #include "wine/winuser16.h"
30 DEFAULT_DEBUG_CHANNEL(x11drv
);
32 /* GCs used for B&W and color bitmap operations */
33 GC BITMAP_monoGC
= 0, BITMAP_colorGC
= 0;
36 /***********************************************************************
39 BOOL
X11DRV_BITMAP_Init(void)
43 /* Create the necessary GCs */
45 if ((tmpPixmap
= TSXCreatePixmap(display
,
46 X11DRV_GetXRootWindow(),
50 BITMAP_monoGC
= TSXCreateGC( display
, tmpPixmap
, 0, NULL
);
51 TSXSetGraphicsExposures( display
, BITMAP_monoGC
, False
);
52 TSXFreePixmap( display
, tmpPixmap
);
55 if (X11DRV_GetDepth() != 1)
57 if ((tmpPixmap
= TSXCreatePixmap(display
, X11DRV_GetXRootWindow(),
58 1, 1, X11DRV_GetDepth())))
60 BITMAP_colorGC
= TSXCreateGC( display
, tmpPixmap
, 0, NULL
);
61 TSXSetGraphicsExposures( display
, BITMAP_colorGC
, False
);
62 TSXFreePixmap( display
, tmpPixmap
);
68 /***********************************************************************
69 * X11DRV_BITMAP_SelectObject
71 HBITMAP
X11DRV_BITMAP_SelectObject( DC
* dc
, HBITMAP hbitmap
,
75 HBITMAP prevHandle
= dc
->w
.hBitmap
;
76 X11DRV_PHYSBITMAP
*pbitmap
;
77 X11DRV_PDEVICE
*physDev
= (X11DRV_PDEVICE
*)dc
->physDev
;
80 if (!(dc
->w
.flags
& DC_MEMORY
)) return 0;
83 if(!X11DRV_CreateBitmap(hbitmap
))
86 if(bmp
->DDBitmap
->funcs
!= dc
->funcs
) {
87 WARN("Trying to select non-X11 DDB into an X11 dc\n");
91 pbitmap
= bmp
->DDBitmap
->physBitmap
;
93 dc
->w
.totalExtent
.left
= 0;
94 dc
->w
.totalExtent
.top
= 0;
95 dc
->w
.totalExtent
.right
= bmp
->bitmap
.bmWidth
;
96 dc
->w
.totalExtent
.bottom
= bmp
->bitmap
.bmHeight
;
99 SetRectRgn( dc
->w
.hVisRgn
, 0, 0,
100 bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
);
103 hrgn
= CreateRectRgn(0, 0, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
);
105 dc
->w
.hVisRgn
= hrgn
;
108 physDev
->drawable
= pbitmap
->pixmap
;
109 dc
->w
.hBitmap
= hbitmap
;
111 /* Change GC depth if needed */
113 if (dc
->w
.bitsPerPixel
!= bmp
->bitmap
.bmBitsPixel
)
115 TSXFreeGC( display
, physDev
->gc
);
116 physDev
->gc
= TSXCreateGC( display
, physDev
->drawable
, 0, NULL
);
117 TSXSetGraphicsExposures( display
, physDev
->gc
, False
);
118 dc
->w
.bitsPerPixel
= bmp
->bitmap
.bmBitsPixel
;
121 else CLIPPING_UpdateGCRegion( dc
); /* Just update GC clip region */
126 /***********************************************************************
129 * Wrapper to call XPutImage with CALL_LARGE_STACK.
132 struct XPutImage_descr
140 static int XPutImage_wrapper( const struct XPutImage_descr
*descr
)
142 return XPutImage( display
,
143 ((X11DRV_PHYSBITMAP
*)descr
->bmp
->DDBitmap
->physBitmap
)->pixmap
,
144 BITMAP_GC(descr
->bmp
),
145 descr
->image
, 0, 0, 0, 0, descr
->width
, descr
->height
);
149 /***************************************************************************
153 * Allocate DDBitmap and physBitmap
156 X11DRV_PHYSBITMAP
*X11DRV_AllocBitmap( BITMAPOBJ
*bmp
)
158 X11DRV_PHYSBITMAP
*pbitmap
;
160 if(!(bmp
->DDBitmap
= HeapAlloc(GetProcessHeap(), 0, sizeof(DDBITMAP
)))) {
161 WARN("Can't alloc DDBITMAP\n");
165 if(!(pbitmap
= HeapAlloc(GetProcessHeap(), 0,sizeof(X11DRV_PHYSBITMAP
)))) {
166 WARN("Can't alloc X11DRV_PHYSBITMAP\n");
167 HeapFree(GetProcessHeap(), 0, bmp
->DDBitmap
);
171 bmp
->DDBitmap
->physBitmap
= pbitmap
;
172 bmp
->DDBitmap
->funcs
= DRIVER_FindDriver( "DISPLAY" );
177 /****************************************************************************
179 * X11DRV_CreateBitmap
181 * Create a device dependent X11 bitmap
183 * Returns TRUE on success else FALSE
187 BOOL
X11DRV_CreateBitmap( HBITMAP hbitmap
)
189 X11DRV_PHYSBITMAP
*pbitmap
;
190 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
193 WARN("Bad bitmap handle %08x\n", hbitmap
);
197 /* Check parameters */
198 if (bmp
->bitmap
.bmPlanes
!= 1) return 0;
199 if ((bmp
->bitmap
.bmBitsPixel
!= 1) &&
200 (bmp
->bitmap
.bmBitsPixel
!= X11DRV_GetDepth()))
202 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
203 bmp
->bitmap
.bmPlanes
, bmp
->bitmap
.bmBitsPixel
);
204 GDI_HEAP_UNLOCK( hbitmap
);
208 TRACE("(%08x) %dx%d %d bpp\n", hbitmap
, bmp
->bitmap
.bmWidth
,
209 bmp
->bitmap
.bmHeight
, bmp
->bitmap
.bmBitsPixel
);
211 pbitmap
= X11DRV_AllocBitmap( bmp
);
212 if(!pbitmap
) return FALSE
;
214 /* Create the pixmap */
215 pbitmap
->pixmap
= TSXCreatePixmap(display
, X11DRV_GetXRootWindow(), bmp
->bitmap
.bmWidth
,
216 bmp
->bitmap
.bmHeight
, bmp
->bitmap
.bmBitsPixel
);
217 if (!pbitmap
->pixmap
) {
218 WARN("Can't create Pixmap\n");
219 HeapFree(GetProcessHeap(), 0, bmp
->DDBitmap
->physBitmap
);
220 HeapFree(GetProcessHeap(), 0, bmp
->DDBitmap
);
221 GDI_HEAP_UNLOCK( hbitmap
);
225 if (bmp
->bitmap
.bmBits
) /* Set bitmap bits */
226 X11DRV_BitmapBits( hbitmap
, bmp
->bitmap
.bmBits
,
227 bmp
->bitmap
.bmHeight
* bmp
->bitmap
.bmWidthBytes
,
230 GDI_HEAP_UNLOCK( hbitmap
);
235 /***********************************************************************
236 * X11DRV_BITMAP_GetXImage
238 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
240 XImage
*X11DRV_BITMAP_GetXImage( const BITMAPOBJ
*bmp
)
242 return XGetImage( display
,
243 ((X11DRV_PHYSBITMAP
*)bmp
->DDBitmap
->physBitmap
)->pixmap
,
244 0, 0, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
245 AllPlanes
, ZPixmap
);
249 /***********************************************************************
250 * X11DRV_GetBitmapBits
253 * Success: Number of bytes copied
256 static LONG
X11DRV_GetBitmapBits(BITMAPOBJ
*bmp
, void *buffer
, LONG count
)
258 LONG old_height
, height
;
260 LPBYTE tbuf
, startline
;
263 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp
, buffer
, count
);
265 EnterCriticalSection( &X11DRV_CritSection
);
267 /* Hack: change the bitmap height temporarily to avoid */
268 /* getting unnecessary bitmap rows. */
270 old_height
= bmp
->bitmap
.bmHeight
;
271 height
= bmp
->bitmap
.bmHeight
= count
/ bmp
->bitmap
.bmWidthBytes
;
273 image
= (XImage
*)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage
, bmp
);
275 bmp
->bitmap
.bmHeight
= old_height
;
277 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
280 switch (bmp
->bitmap
.bmBitsPixel
)
283 for (h
=0;h
<height
;h
++)
287 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
291 *tbuf
|= XGetPixel(image
,w
,h
)<<(7-(w
&7));
292 if ((w
&7) == 7) ++tbuf
;
294 startline
+= bmp
->bitmap
.bmWidthBytes
;
298 for (h
=0;h
<height
;h
++)
301 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
303 if (!(w
& 1)) *tbuf
= XGetPixel( image
, w
, h
) << 4;
304 else *tbuf
++ |= XGetPixel( image
, w
, h
) & 0x0f;
306 startline
+= bmp
->bitmap
.bmWidthBytes
;
310 for (h
=0;h
<height
;h
++)
313 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
314 *tbuf
++ = XGetPixel(image
,w
,h
);
315 startline
+= bmp
->bitmap
.bmWidthBytes
;
320 for (h
=0;h
<height
;h
++)
323 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
325 long pixel
= XGetPixel(image
,w
,h
);
327 *tbuf
++ = pixel
& 0xff;
328 *tbuf
++ = (pixel
>>8) & 0xff;
330 startline
+= bmp
->bitmap
.bmWidthBytes
;
334 for (h
=0;h
<height
;h
++)
337 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
339 long pixel
= XGetPixel(image
,w
,h
);
341 *tbuf
++ = pixel
& 0xff;
342 *tbuf
++ = (pixel
>> 8) & 0xff;
343 *tbuf
++ = (pixel
>>16) & 0xff;
345 startline
+= bmp
->bitmap
.bmWidthBytes
;
350 for (h
=0;h
<height
;h
++)
353 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
355 long pixel
= XGetPixel(image
,w
,h
);
357 *tbuf
++ = pixel
& 0xff;
358 *tbuf
++ = (pixel
>> 8) & 0xff;
359 *tbuf
++ = (pixel
>>16) & 0xff;
360 *tbuf
++ = (pixel
>>24) & 0xff;
362 startline
+= bmp
->bitmap
.bmWidthBytes
;
366 FIXME("Unhandled bits:%d\n", bmp
->bitmap
.bmBitsPixel
);
368 XDestroyImage( image
);
369 LeaveCriticalSection( &X11DRV_CritSection
);
376 /******************************************************************************
377 * X11DRV_SetBitmapBits
380 * Success: Number of bytes used in setting the bitmap bits
383 static LONG
X11DRV_SetBitmapBits(BITMAPOBJ
*bmp
, void *bits
, LONG count
)
385 struct XPutImage_descr descr
;
388 LPBYTE sbuf
, startline
;
391 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp
, bits
, count
);
393 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
395 EnterCriticalSection( &X11DRV_CritSection
);
396 image
= XCreateImage( display
, X11DRV_GetVisual(), bmp
->bitmap
.bmBitsPixel
, ZPixmap
, 0, NULL
,
397 bmp
->bitmap
.bmWidth
, height
, 32, 0 );
398 image
->data
= (LPBYTE
)xmalloc(image
->bytes_per_line
* height
);
400 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
404 switch (bmp
->bitmap
.bmBitsPixel
)
407 for (h
=0;h
<height
;h
++)
410 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
412 XPutPixel(image
,w
,h
,(sbuf
[0]>>(7-(w
&7))) & 1);
416 startline
+= bmp
->bitmap
.bmWidthBytes
;
420 for (h
=0;h
<height
;h
++)
423 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
425 if (!(w
& 1)) XPutPixel( image
, w
, h
, *sbuf
>> 4 );
426 else XPutPixel( image
, w
, h
, *sbuf
++ & 0xf );
428 startline
+= bmp
->bitmap
.bmWidthBytes
;
432 for (h
=0;h
<height
;h
++)
435 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
436 XPutPixel(image
,w
,h
,*sbuf
++);
437 startline
+= bmp
->bitmap
.bmWidthBytes
;
442 for (h
=0;h
<height
;h
++)
445 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
447 XPutPixel(image
,w
,h
,sbuf
[1]*256+sbuf
[0]);
450 startline
+= bmp
->bitmap
.bmWidthBytes
;
454 for (h
=0;h
<height
;h
++)
457 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
459 XPutPixel(image
,w
,h
,(sbuf
[2]<<16)+(sbuf
[1]<<8)+sbuf
[0]);
462 startline
+= bmp
->bitmap
.bmWidthBytes
;
466 for (h
=0;h
<height
;h
++)
469 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
471 XPutPixel(image
,w
,h
,(sbuf
[3]<<24)+(sbuf
[2]<<16)+(sbuf
[1]<<8)+sbuf
[0]);
474 startline
+= bmp
->bitmap
.bmWidthBytes
;
478 FIXME("Unhandled bits:%d\n", bmp
->bitmap
.bmBitsPixel
);
484 descr
.width
= bmp
->bitmap
.bmWidth
;
485 descr
.height
= height
;
487 CALL_LARGE_STACK( XPutImage_wrapper
, &descr
);
488 XDestroyImage( image
); /* frees image->data too */
489 LeaveCriticalSection( &X11DRV_CritSection
);
494 /***********************************************************************
497 LONG
X11DRV_BitmapBits(HBITMAP hbitmap
, void *bits
, LONG count
, WORD flags
)
499 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
502 WARN("Bad bitmap handle %08x\n", hbitmap
);
507 ret
= X11DRV_GetBitmapBits(bmp
, bits
, count
);
508 else if(flags
== DDB_SET
)
509 ret
= X11DRV_SetBitmapBits(bmp
, bits
, count
);
511 ERR("Unknown flags value %d\n", flags
);
515 GDI_HEAP_UNLOCK( hbitmap
);
519 /***********************************************************************
520 * X11DRV_BITMAP_DeleteObject
522 BOOL
X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap
, BITMAPOBJ
* bmp
)
524 X11DRV_PHYSBITMAP
*pbitmap
= bmp
->DDBitmap
->physBitmap
;
526 TSXFreePixmap( display
, pbitmap
->pixmap
);
528 HeapFree( GetProcessHeap(), 0, bmp
->DDBitmap
->physBitmap
);
529 HeapFree( GetProcessHeap(), 0, bmp
->DDBitmap
);
530 bmp
->DDBitmap
= NULL
;
535 /**************************************************************************
536 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
538 * Allocates an HBITMAP which references the Pixmap passed in.
539 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
540 * calling DeleteObject on this will free the Pixmap as well.
542 HBITMAP
X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap
)
545 BITMAPOBJ
*pBmp
= NULL
;
546 X11DRV_PHYSBITMAP
*pPhysBmp
= NULL
;
548 int x
,y
; /* Unused */
549 unsigned border_width
; /* Unused */
550 unsigned int depth
, width
, height
;
552 /* Get the Pixmap dimensions and bit depth */
553 if ( 0 == TSXGetGeometry(display
, pixmap
, &root
, &x
, &y
, &width
, &height
,
554 &border_width
, &depth
) )
557 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
558 width
, height
, depth
);
561 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
562 * and make it a container for the pixmap passed.
564 hBmp
= CreateBitmap( width
, height
, 1, depth
, NULL
);
566 /* Allocate DDBitmap and physBitmap structures in BITMAPOBJ.
567 * The hBmp is just a filled in BITMAPOBJ header at this point.
569 pBmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hBmp
, BITMAP_MAGIC
);
570 pPhysBmp
= X11DRV_AllocBitmap( pBmp
);
578 /* Point to our Pixmap in the physical bitmap structure */
579 pPhysBmp
->pixmap
= pixmap
;
582 TRACE("\tReturning HBITMAP %x\n", hBmp
);
587 /**************************************************************************
588 * X11DRV_BITMAP_CreateBitmapFromPixmap
590 * Allocates an HBITMAP and copies the Pixmap data into it.
591 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
593 HBITMAP
X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap
, BOOL bDeletePixmap
)
595 HBITMAP hBmp
= 0, hBmpCopy
= 0;
596 BITMAPOBJ
*pBmp
= NULL
;
597 unsigned int width
, height
;
599 /* Allocate an HBITMAP which references the Pixmap passed to us */
600 hBmp
= X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap
);
603 TRACE("\tCould not create bitmap header for Pixmap\n");
607 /* Get the bitmap dimensions */
608 width
= pBmp
->bitmap
.bmWidth
;
609 height
= pBmp
->bitmap
.bmHeight
;
611 hBmpCopy
= CopyImage(hBmp
, IMAGE_BITMAP
, width
, height
, LR_CREATEDIBSECTION
);
613 /* We can now get rid of the HBITMAP wrapper we created earlier.
614 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
618 /* Manually free the DDBitmap internals to prevent the Pixmap
619 * from being deleted by DeleteObject.
621 pBmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hBmp
, BITMAP_MAGIC
);
622 HeapFree( GetProcessHeap(), 0, pBmp
->DDBitmap
->physBitmap
);
623 HeapFree( GetProcessHeap(), 0, pBmp
->DDBitmap
);
624 pBmp
->DDBitmap
= NULL
;
629 TRACE("\tReturning HBITMAP %x\n", hBmpCopy
);
634 /**************************************************************************
635 * X11DRV_BITMAP_CreatePixmapFromBitmap
637 * Creates a Pixmap from a bitmap
639 Pixmap
X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp
, HDC hdc
)
641 HGLOBAL hPackedDIB
= NULL
;
642 Pixmap pixmap
= NULL
;
645 * Create a packed DIB from the bitmap passed to us.
646 * A packed DIB contains a BITMAPINFO structure followed immediately by
647 * an optional color palette and the pixel data.
649 hPackedDIB
= DIB_CreateDIBFromBitmap(hdc
, hBmp
);
651 /* Create a Pixmap from the packed DIB */
652 pixmap
= X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB
, hdc
);
654 /* Free the temporary packed DIB */
655 GlobalFree(hPackedDIB
);
661 /***********************************************************************
662 * X11DRV_BITMAP_Pixmap
664 * This function exists solely for x11 driver of the window system.
666 BOOL
X11DRV_BITMAP_Pixmap(HBITMAP hbitmap
)
668 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
669 return ((X11DRV_PHYSBITMAP
*)(bmp
->DDBitmap
->physBitmap
))->pixmap
;
672 #endif /* !defined(X_DISPLAY_MISSING) */