Release 20040408.
[wine.git] / dlls / x11drv / bitmap.c
blob84eb0090a577cbef9e0127cd9e17c8fb31654102
1 /*
2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 * 1999 Noel Borthwick
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
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "gdi.h"
27 #include "wine/debug.h"
28 #include "x11drv.h"
29 #include "wingdi.h"
30 #include "windef.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 /***********************************************************************
40 * X11DRV_BITMAP_Init
42 BOOL X11DRV_BITMAP_Init(void)
44 Pixmap tmpPixmap;
46 /* Create the necessary GCs */
48 wine_tsx11_lock();
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 );
64 wine_tsx11_unlock();
65 return TRUE;
68 /***********************************************************************
69 * SelectBitmap (X11DRV.@)
71 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
73 BITMAPOBJ *bmp;
75 if (!(bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
77 if(physDev->xrender)
78 X11DRV_XRender_UpdateDrawable( physDev );
80 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
81 physDev->drawable = BITMAP_stock_pixmap;
82 else
83 physDev->drawable = (Pixmap)bmp->physBitmap;
85 /* Change GC depth if needed */
87 if (physDev->depth != bmp->bitmap.bmBitsPixel)
89 physDev->depth = bmp->bitmap.bmBitsPixel;
90 wine_tsx11_lock();
91 XFreeGC( gdi_display, physDev->gc );
92 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
93 XSetGraphicsExposures( gdi_display, physDev->gc, False );
94 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
95 XFlush( gdi_display );
96 wine_tsx11_unlock();
98 GDI_ReleaseObj( hbitmap );
99 return hbitmap;
103 /****************************************************************************
104 * CreateBitmap (X11DRV.@)
106 * Create a device dependent X11 bitmap
108 * Returns TRUE on success else FALSE
110 BOOL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
112 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
114 if(!bmp) {
115 WARN("Bad bitmap handle %p\n", hbitmap);
116 return FALSE;
119 /* Check parameters */
120 if (bmp->bitmap.bmPlanes != 1)
122 GDI_ReleaseObj( hbitmap );
123 return 0;
125 if ((bmp->bitmap.bmBitsPixel != 1) && (bmp->bitmap.bmBitsPixel != screen_depth))
127 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
128 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
129 GDI_ReleaseObj( hbitmap );
130 return FALSE;
132 if (hbitmap == GetStockObject(DEFAULT_BITMAP))
134 ERR( "called for stock bitmap, please report\n" );
135 GDI_ReleaseObj( hbitmap );
136 return FALSE;
139 TRACE("(%p) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
140 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
142 /* Create the pixmap */
143 wine_tsx11_lock();
144 bmp->physBitmap = (void *)XCreatePixmap(gdi_display, root_window,
145 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
146 bmp->bitmap.bmBitsPixel);
147 wine_tsx11_unlock();
148 if (!bmp->physBitmap)
150 WARN("Can't create Pixmap\n");
151 GDI_ReleaseObj( hbitmap );
152 return FALSE;
155 if (bmp->bitmap.bmBits) /* Set bitmap bits */
156 X11DRV_SetBitmapBits( hbitmap, bmp->bitmap.bmBits,
157 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes );
159 GDI_ReleaseObj( hbitmap );
160 return TRUE;
164 /***********************************************************************
165 * GetBitmapBits (X11DRV.@)
167 * RETURNS
168 * Success: Number of bytes copied
169 * Failure: 0
171 LONG X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
173 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
174 LONG old_height, height;
175 XImage *image;
176 LPBYTE tbuf, startline;
177 int h, w;
179 if (!bmp) return 0;
180 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
182 wine_tsx11_lock();
184 /* Hack: change the bitmap height temporarily to avoid */
185 /* getting unnecessary bitmap rows. */
187 old_height = bmp->bitmap.bmHeight;
188 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
190 image = XGetImage( gdi_display, (Pixmap)bmp->physBitmap,
191 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
192 AllPlanes, ZPixmap );
193 bmp->bitmap.bmHeight = old_height;
195 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
197 startline = buffer;
198 switch (bmp->bitmap.bmBitsPixel)
200 case 1:
201 for (h=0;h<height;h++)
203 tbuf = startline;
204 *tbuf = 0;
205 for (w=0;w<bmp->bitmap.bmWidth;w++)
207 if ((w%8) == 0)
208 *tbuf = 0;
209 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
210 if ((w&7) == 7) ++tbuf;
212 startline += bmp->bitmap.bmWidthBytes;
214 break;
215 case 4:
216 for (h=0;h<height;h++)
218 tbuf = startline;
219 for (w=0;w<bmp->bitmap.bmWidth;w++)
221 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
222 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
224 startline += bmp->bitmap.bmWidthBytes;
226 break;
227 case 8:
228 for (h=0;h<height;h++)
230 tbuf = startline;
231 for (w=0;w<bmp->bitmap.bmWidth;w++)
232 *tbuf++ = XGetPixel(image,w,h);
233 startline += bmp->bitmap.bmWidthBytes;
235 break;
236 case 15:
237 case 16:
238 for (h=0;h<height;h++)
240 tbuf = startline;
241 for (w=0;w<bmp->bitmap.bmWidth;w++)
243 long pixel = XGetPixel(image,w,h);
245 *tbuf++ = pixel & 0xff;
246 *tbuf++ = (pixel>>8) & 0xff;
248 startline += bmp->bitmap.bmWidthBytes;
250 break;
251 case 24:
252 for (h=0;h<height;h++)
254 tbuf = startline;
255 for (w=0;w<bmp->bitmap.bmWidth;w++)
257 long pixel = XGetPixel(image,w,h);
259 *tbuf++ = pixel & 0xff;
260 *tbuf++ = (pixel>> 8) & 0xff;
261 *tbuf++ = (pixel>>16) & 0xff;
263 startline += bmp->bitmap.bmWidthBytes;
265 break;
267 case 32:
268 for (h=0;h<height;h++)
270 tbuf = startline;
271 for (w=0;w<bmp->bitmap.bmWidth;w++)
273 long pixel = XGetPixel(image,w,h);
275 *tbuf++ = pixel & 0xff;
276 *tbuf++ = (pixel>> 8) & 0xff;
277 *tbuf++ = (pixel>>16) & 0xff;
278 *tbuf++ = (pixel>>24) & 0xff;
280 startline += bmp->bitmap.bmWidthBytes;
282 break;
283 default:
284 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
286 XDestroyImage( image );
287 wine_tsx11_unlock();
288 GDI_ReleaseObj( hbitmap );
289 return count;
294 /******************************************************************************
295 * SetBitmapBits (X11DRV.@)
297 * RETURNS
298 * Success: Number of bytes used in setting the bitmap bits
299 * Failure: 0
301 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
303 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
304 LONG height;
305 XImage *image;
306 const BYTE *sbuf, *startline;
307 int w, h;
309 if (!bmp) return 0;
310 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
312 height = count / bmp->bitmap.bmWidthBytes;
314 wine_tsx11_lock();
315 image = XCreateImage( gdi_display, visual, bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
316 bmp->bitmap.bmWidth, height, 32, 0 );
317 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
319 WARN("No memory to create image data.\n");
320 XDestroyImage( image );
321 wine_tsx11_unlock();
322 GDI_ReleaseObj( hbitmap );
323 return 0;
326 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
328 startline = bits;
330 switch (bmp->bitmap.bmBitsPixel)
332 case 1:
333 for (h=0;h<height;h++)
335 sbuf = startline;
336 for (w=0;w<bmp->bitmap.bmWidth;w++)
338 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
339 if ((w&7) == 7)
340 sbuf++;
342 startline += bmp->bitmap.bmWidthBytes;
344 break;
345 case 4:
346 for (h=0;h<height;h++)
348 sbuf = startline;
349 for (w=0;w<bmp->bitmap.bmWidth;w++)
351 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
352 else XPutPixel( image, w, h, *sbuf++ & 0xf );
354 startline += bmp->bitmap.bmWidthBytes;
356 break;
357 case 8:
358 for (h=0;h<height;h++)
360 sbuf = startline;
361 for (w=0;w<bmp->bitmap.bmWidth;w++)
362 XPutPixel(image,w,h,*sbuf++);
363 startline += bmp->bitmap.bmWidthBytes;
365 break;
366 case 15:
367 case 16:
368 for (h=0;h<height;h++)
370 sbuf = startline;
371 for (w=0;w<bmp->bitmap.bmWidth;w++)
373 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
374 sbuf+=2;
376 startline += bmp->bitmap.bmWidthBytes;
378 break;
379 case 24:
380 for (h=0;h<height;h++)
382 sbuf = startline;
383 for (w=0;w<bmp->bitmap.bmWidth;w++)
385 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
386 sbuf += 3;
388 startline += bmp->bitmap.bmWidthBytes;
390 break;
391 case 32:
392 for (h=0;h<height;h++)
394 sbuf = startline;
395 for (w=0;w<bmp->bitmap.bmWidth;w++)
397 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
398 sbuf += 4;
400 startline += bmp->bitmap.bmWidthBytes;
402 break;
403 default:
404 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
407 XPutImage( gdi_display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
408 image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
409 XDestroyImage( image ); /* frees image->data too */
410 wine_tsx11_unlock();
411 GDI_ReleaseObj( hbitmap );
412 return count;
415 /***********************************************************************
416 * DeleteBitmap (X11DRV.@)
418 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
420 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
421 if (bmp)
423 wine_tsx11_lock();
424 if (bmp->physBitmap) XFreePixmap( gdi_display, (Pixmap)bmp->physBitmap );
425 bmp->physBitmap = NULL;
426 wine_tsx11_unlock();
427 if (bmp->dib) X11DRV_DIB_DeleteDIBSection( bmp );
428 GDI_ReleaseObj( hbitmap );
430 return TRUE;
433 /**************************************************************************
434 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
436 * Allocates an HBITMAP which references the Pixmap passed in.
437 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
438 * calling DeleteObject on this will free the Pixmap as well.
440 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(HDC hdc, Pixmap pixmap)
442 HDC hdcMem;
443 HBITMAP hBmp = 0, old;
444 BITMAPOBJ *pBmp = NULL;
445 Window root;
446 int x,y; /* Unused */
447 unsigned border_width; /* Unused */
448 unsigned int depth, width, height;
450 /* Get the Pixmap dimensions and bit depth */
451 wine_tsx11_lock();
452 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
453 &border_width, &depth)) depth = 0;
454 wine_tsx11_unlock();
455 if (!depth) goto END;
457 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
458 width, height, depth);
461 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
462 * and make it a container for the pixmap passed.
464 hBmp = CreateBitmap( width, height, 1, depth, NULL );
466 /* force bitmap to be owned by a screen DC */
467 hdcMem = CreateCompatibleDC( hdc );
468 old = SelectObject( hdcMem, hBmp );
470 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
472 if (pBmp->physBitmap) XFreePixmap( gdi_display, (Pixmap)pBmp->physBitmap );
473 pBmp->physBitmap = (void *)pixmap;
474 GDI_ReleaseObj( hBmp );
476 SelectObject( hdcMem, old );
477 DeleteDC( hdcMem );
479 END:
480 TRACE("\tReturning HBITMAP %p\n", hBmp);
481 return hBmp;
485 /***********************************************************************
486 * X11DRV_BITMAP_Pixmap
488 * This function exists solely for x11 driver of the window system.
490 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
492 Pixmap pixmap;
493 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
494 if (bmp) {
495 pixmap = (Pixmap)bmp->physBitmap;
496 GDI_ReleaseObj( hbitmap );
498 else {
499 ERR("handle %p returned no obj\n", hbitmap);
500 pixmap = 0;
502 return pixmap;