2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
9 #ifndef X_DISPLAY_MISSING
26 #include "wine/winuser16.h"
28 /* GCs used for B&W and color bitmap operations */
29 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
32 /***********************************************************************
35 BOOL X11DRV_BITMAP_Init(void)
39 /* Create the necessary GCs */
41 if ((tmpPixmap = TSXCreatePixmap(display,
42 X11DRV_GetXRootWindow(),
46 BITMAP_monoGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
47 TSXSetGraphicsExposures( display, BITMAP_monoGC, False );
48 TSXFreePixmap( display, tmpPixmap );
51 if (MONITOR_GetDepth(&MONITOR_PrimaryMonitor) != 1)
53 if ((tmpPixmap = TSXCreatePixmap(display,
54 X11DRV_GetXRootWindow(),
56 MONITOR_GetDepth(&MONITOR_PrimaryMonitor))))
58 BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
59 TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
60 TSXFreePixmap( display, tmpPixmap );
66 /***********************************************************************
67 * X11DRV_BITMAP_SelectObject
69 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
73 HBITMAP prevHandle = dc->w.hBitmap;
74 X11DRV_PHYSBITMAP *pbitmap;
75 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
78 if (!(dc->w.flags & DC_MEMORY)) return 0;
81 if(!X11DRV_CreateBitmap(hbitmap))
84 if(bmp->DDBitmap->funcs != dc->funcs) {
85 WARN(x11drv, "Trying to select non-X11 DDB into an X11 dc\n");
89 pbitmap = bmp->DDBitmap->physBitmap;
91 dc->w.totalExtent.left = 0;
92 dc->w.totalExtent.top = 0;
93 dc->w.totalExtent.right = bmp->bitmap.bmWidth;
94 dc->w.totalExtent.bottom = bmp->bitmap.bmHeight;
97 SetRectRgn( dc->w.hVisRgn, 0, 0,
98 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
101 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
103 dc->w.hVisRgn = hrgn;
106 physDev->drawable = pbitmap->pixmap;
107 dc->w.hBitmap = hbitmap;
109 /* Change GC depth if needed */
111 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
113 TSXFreeGC( display, physDev->gc );
114 physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
115 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
118 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
123 /***********************************************************************
126 * Wrapper to call XPutImage with CALL_LARGE_STACK.
129 struct XPutImage_descr
137 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
139 return XPutImage( display,
140 ((X11DRV_PHYSBITMAP *)descr->bmp->DDBitmap->physBitmap)->pixmap,
141 BITMAP_GC(descr->bmp),
142 descr->image, 0, 0, 0, 0, descr->width, descr->height );
146 /***************************************************************************
150 * Allocate DDBitmap and physBitmap
153 X11DRV_PHYSBITMAP *X11DRV_AllocBitmap( BITMAPOBJ *bmp )
155 X11DRV_PHYSBITMAP *pbitmap;
157 if(!(bmp->DDBitmap = HeapAlloc(GetProcessHeap(), 0, sizeof(DDBITMAP)))) {
158 WARN(x11drv, "Can't alloc DDBITMAP\n");
162 if(!(pbitmap = HeapAlloc(GetProcessHeap(), 0,sizeof(X11DRV_PHYSBITMAP)))) {
163 WARN(x11drv, "Can't alloc X11DRV_PHYSBITMAP\n");
164 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
168 bmp->DDBitmap->physBitmap = pbitmap;
169 bmp->DDBitmap->funcs = DRIVER_FindDriver( "DISPLAY" );
174 /****************************************************************************
176 * X11DRV_CreateBitmap
178 * Create a device dependent X11 bitmap
180 * Returns TRUE on success else FALSE
184 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
186 X11DRV_PHYSBITMAP *pbitmap;
187 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
190 WARN(x11drv, "Bad bitmap handle %08x\n", hbitmap);
194 /* Check parameters */
195 if (bmp->bitmap.bmPlanes != 1) return 0;
196 if ((bmp->bitmap.bmBitsPixel != 1) &&
197 (bmp->bitmap.bmBitsPixel != MONITOR_GetDepth(&MONITOR_PrimaryMonitor))) {
198 ERR(x11drv, "Trying to make bitmap with planes=%d, bpp=%d\n",
199 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
200 GDI_HEAP_UNLOCK( hbitmap );
204 TRACE(x11drv, "(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
205 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
207 pbitmap = X11DRV_AllocBitmap( bmp );
208 if(!pbitmap) return FALSE;
210 /* Create the pixmap */
211 pbitmap->pixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(), bmp->bitmap.bmWidth,
212 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
213 if (!pbitmap->pixmap) {
214 WARN(x11drv, "Can't create Pixmap\n");
215 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap->physBitmap);
216 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
217 GDI_HEAP_UNLOCK( hbitmap );
221 if (bmp->bitmap.bmBits) /* Set bitmap bits */
222 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
223 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
226 GDI_HEAP_UNLOCK( hbitmap );
231 /***********************************************************************
232 * X11DRV_BITMAP_GetXImage
234 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
236 XImage *X11DRV_BITMAP_GetXImage( const BITMAPOBJ *bmp )
238 return XGetImage( display,
239 ((X11DRV_PHYSBITMAP *)bmp->DDBitmap->physBitmap)->pixmap,
240 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
241 AllPlanes, ZPixmap );
245 /***********************************************************************
246 * X11DRV_GetBitmapBits
249 * Success: Number of bytes copied
252 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
254 LONG old_height, height;
259 TRACE(x11drv, "(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
261 pad = BITMAP_GetPadding(bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel);
266 EnterCriticalSection( &X11DRV_CritSection );
268 /* Hack: change the bitmap height temporarily to avoid */
269 /* getting unnecessary bitmap rows. */
271 old_height = bmp->bitmap.bmHeight;
272 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
274 image = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
276 bmp->bitmap.bmHeight = old_height;
278 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
281 switch (bmp->bitmap.bmBitsPixel)
284 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;
298 for (h=0;h<height;h++)
300 for (w=0;w<bmp->bitmap.bmWidth;w++)
302 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
303 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
309 for (h=0;h<height;h++)
311 for (w=0;w<bmp->bitmap.bmWidth;w++)
312 *tbuf++ = XGetPixel(image,w,h);
318 for (h=0;h<height;h++)
320 for (w=0;w<bmp->bitmap.bmWidth;w++)
322 long pixel = XGetPixel(image,w,h);
324 *tbuf++ = pixel & 0xff;
325 *tbuf++ = (pixel>>8) & 0xff;
330 for (h=0;h<height;h++)
332 for (w=0;w<bmp->bitmap.bmWidth;w++)
334 long pixel = XGetPixel(image,w,h);
336 *tbuf++ = pixel & 0xff;
337 *tbuf++ = (pixel>> 8) & 0xff;
338 *tbuf++ = (pixel>>16) & 0xff;
345 for (h=0;h<height;h++)
347 for (w=0;w<bmp->bitmap.bmWidth;w++)
349 long pixel = XGetPixel(image,w,h);
351 *tbuf++ = pixel & 0xff;
352 *tbuf++ = (pixel>> 8) & 0xff;
353 *tbuf++ = (pixel>>16) & 0xff;
354 *tbuf++ = (pixel>>24) & 0xff;
360 FIXME(x11drv, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
362 XDestroyImage( image );
363 LeaveCriticalSection( &X11DRV_CritSection );
370 /******************************************************************************
371 * X11DRV_SetBitmapBits
374 * Success: Number of bytes used in setting the bitmap bits
377 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
379 struct XPutImage_descr descr;
385 TRACE(x11drv, "(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
387 pad = BITMAP_GetPadding(bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel);
394 height = count / bmp->bitmap.bmWidthBytes;
396 EnterCriticalSection( &X11DRV_CritSection );
397 image = XCreateImage( display, DefaultVisualOfScreen(X11DRV_GetXScreen()),
398 bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
399 bmp->bitmap.bmWidth, height, 32, 0 );
400 image->data = (LPBYTE)xmalloc(image->bytes_per_line * height);
402 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
404 switch (bmp->bitmap.bmBitsPixel)
407 for (h=0;h<height;h++)
409 for (w=0;w<bmp->bitmap.bmWidth;w++)
411 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
419 for (h=0;h<height;h++)
421 for (w=0;w<bmp->bitmap.bmWidth;w++)
423 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
424 else XPutPixel( image, w, h, *sbuf++ & 0xf );
430 for (h=0;h<height;h++)
432 for (w=0;w<bmp->bitmap.bmWidth;w++)
433 XPutPixel(image,w,h,*sbuf++);
439 for (h=0;h<height;h++)
441 for (w=0;w<bmp->bitmap.bmWidth;w++)
443 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
449 for (h=0;h<height;h++)
451 for (w=0;w<bmp->bitmap.bmWidth;w++)
453 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
460 for (h=0;h<height;h++)
462 for (w=0;w<bmp->bitmap.bmWidth;w++)
464 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
471 FIXME(x11drv, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
477 descr.width = bmp->bitmap.bmWidth;
478 descr.height = height;
480 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
481 XDestroyImage( image ); /* frees image->data too */
482 LeaveCriticalSection( &X11DRV_CritSection );
487 /***********************************************************************
490 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
492 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
495 WARN(x11drv, "Bad bitmap handle %08x\n", hbitmap);
500 ret = X11DRV_GetBitmapBits(bmp, bits, count);
501 else if(flags == DDB_SET)
502 ret = X11DRV_SetBitmapBits(bmp, bits, count);
504 ERR(x11drv, "Unknown flags value %d\n", flags);
508 GDI_HEAP_UNLOCK( hbitmap );
512 /***********************************************************************
513 * X11DRV_BITMAP_DeleteObject
515 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
517 X11DRV_PHYSBITMAP *pbitmap = bmp->DDBitmap->physBitmap;
519 TSXFreePixmap( display, pbitmap->pixmap );
521 HeapFree( GetProcessHeap(), 0, bmp->DDBitmap->physBitmap );
522 HeapFree( GetProcessHeap(), 0, bmp->DDBitmap );
523 bmp->DDBitmap = NULL;
528 #endif /* !defined(X_DISPLAY_MISSING) */