kernel32: Update Italian translation.
[wine/testsucceed.git] / dlls / winex11.drv / bitmap.c
blob4683fd9a9ca209df6404f6fb3f2cfab8059736c2
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "windef.h"
27 #include "wingdi.h"
28 #include "wine/debug.h"
29 #include "x11drv.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
33 /* GCs used for B&W and color bitmap operations */
34 static GC bitmap_gc[32];
35 X_PHYSBITMAP BITMAP_stock_phys_bitmap = { 0 }; /* phys bitmap for the default stock bitmap */
37 static XContext bitmap_context; /* X context to associate a phys bitmap to a handle */
39 GC get_bitmap_gc(int depth)
41 if(depth < 1 || depth > 32)
42 return 0;
44 return bitmap_gc[depth-1];
47 /***********************************************************************
48 * X11DRV_BITMAP_Init
50 void X11DRV_BITMAP_Init(void)
52 int depth_count, index, i;
53 int *depth_list;
54 Pixmap tmpPixmap;
56 wine_tsx11_lock();
57 bitmap_context = XUniqueContext();
58 BITMAP_stock_phys_bitmap.pixmap_depth = 1;
59 BITMAP_stock_phys_bitmap.pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
60 bitmap_gc[0] = XCreateGC( gdi_display, BITMAP_stock_phys_bitmap.pixmap, 0, NULL );
61 XSetGraphicsExposures( gdi_display, bitmap_gc[0], False );
62 XSetSubwindowMode( gdi_display, bitmap_gc[0], IncludeInferiors );
64 /* Create a GC for all available depths. GCs at depths other than 1-bit/screen_depth are for use
65 * in combination with XRender which allows us to create dibsections at more depths.
67 depth_list = XListDepths(gdi_display, DefaultScreen(gdi_display), &depth_count);
68 for (i = 0; i < depth_count; i++)
70 index = depth_list[i] - 1;
71 if (bitmap_gc[index]) continue;
72 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, depth_list[i])))
74 if ((bitmap_gc[index] = XCreateGC( gdi_display, tmpPixmap, 0, NULL )))
76 XSetGraphicsExposures( gdi_display, bitmap_gc[index], False );
77 XSetSubwindowMode( gdi_display, bitmap_gc[index], IncludeInferiors );
79 XFreePixmap( gdi_display, tmpPixmap );
82 XFree( depth_list );
84 wine_tsx11_unlock();
87 /***********************************************************************
88 * SelectBitmap (X11DRV.@)
90 HBITMAP CDECL X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
92 X_PHYSBITMAP *physBitmap;
93 BITMAP bitmap;
95 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
97 if(physDev->xrender)
98 X11DRV_XRender_UpdateDrawable( physDev );
100 if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap) physBitmap = &BITMAP_stock_phys_bitmap;
101 else if (!(physBitmap = X11DRV_get_phys_bitmap( hbitmap ))) return 0;
103 physDev->bitmap = physBitmap;
104 physDev->drawable = physBitmap->pixmap;
105 physDev->color_shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
106 SetRect( &physDev->drawable_rect, 0, 0, bitmap.bmWidth, bitmap.bmHeight );
107 physDev->dc_rect = physDev->drawable_rect;
109 /* Change GC depth if needed */
111 if (physDev->depth != physBitmap->pixmap_depth)
113 physDev->depth = physBitmap->pixmap_depth;
114 wine_tsx11_lock();
115 XFreeGC( gdi_display, physDev->gc );
116 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
117 XSetGraphicsExposures( gdi_display, physDev->gc, False );
118 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
119 XFlush( gdi_display );
120 wine_tsx11_unlock();
123 return hbitmap;
127 /****************************************************************************
128 * CreateBitmap (X11DRV.@)
130 * Create a device dependent X11 bitmap
132 * Returns TRUE on success else FALSE
134 BOOL CDECL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, LPVOID bmBits )
136 X_PHYSBITMAP *physBitmap;
137 BITMAP bitmap;
139 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
141 /* Check parameters */
142 if (bitmap.bmPlanes != 1) return FALSE;
144 /* check if bpp is compatible with screen depth */
145 if (!((bitmap.bmBitsPixel == 1) || (bitmap.bmBitsPixel == screen_bpp)))
147 WARN("Trying to make bitmap with planes=%d, bpp=%d\n",
148 bitmap.bmPlanes, bitmap.bmBitsPixel);
149 return FALSE;
151 if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
153 ERR( "called for stock bitmap, please report\n" );
154 return FALSE;
157 TRACE("(%p) %dx%d %d bpp\n", hbitmap, bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
159 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return FALSE;
161 if (!X11DRV_XRender_SetPhysBitmapDepth( physBitmap, bitmap.bmBitsPixel, NULL ))
163 if(bitmap.bmBitsPixel == 1)
165 physBitmap->pixmap_depth = 1;
166 physBitmap->trueColor = FALSE;
168 else
170 physBitmap->pixmap_depth = screen_depth;
171 physBitmap->pixmap_color_shifts = X11DRV_PALETTE_default_shifts;
172 physBitmap->trueColor = (visual->class == TrueColor || visual->class == DirectColor);
176 wine_tsx11_lock();
177 /* Create the pixmap */
178 physBitmap->pixmap = XCreatePixmap(gdi_display, root_window,
179 bitmap.bmWidth, bitmap.bmHeight, physBitmap->pixmap_depth);
180 wine_tsx11_unlock();
181 if (!physBitmap->pixmap)
183 WARN("Can't create Pixmap\n");
184 HeapFree( GetProcessHeap(), 0, physBitmap );
185 return FALSE;
188 if (bmBits) /* Set bitmap bits */
190 X11DRV_SetBitmapBits( hbitmap, bmBits, bitmap.bmHeight * bitmap.bmWidthBytes );
192 else /* else clear the bitmap */
194 GC gc = get_bitmap_gc(physBitmap->pixmap_depth);
195 wine_tsx11_lock();
196 XSetFunction( gdi_display, gc, GXclear );
197 XFillRectangle( gdi_display, physBitmap->pixmap, gc, 0, 0,
198 bitmap.bmWidth, bitmap.bmHeight );
199 XSetFunction( gdi_display, gc, GXcopy );
200 wine_tsx11_unlock();
202 return TRUE;
206 /***********************************************************************
207 * GetBitmapBits (X11DRV.@)
209 * RETURNS
210 * Success: Number of bytes copied
211 * Failure: 0
213 LONG CDECL X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
215 BITMAP bitmap;
216 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
217 LONG height;
218 XImage *image;
219 LPBYTE tbuf, startline;
220 int h, w;
222 if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
224 TRACE("(bmp=%p, buffer=%p, count=0x%x)\n", hbitmap, buffer, count);
226 wine_tsx11_lock();
227 height = count / bitmap.bmWidthBytes;
228 image = XGetImage( gdi_display, physBitmap->pixmap, 0, 0,
229 bitmap.bmWidth, height, AllPlanes, ZPixmap );
231 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
233 startline = buffer;
234 switch (bitmap.bmBitsPixel)
236 case 1:
237 for (h=0;h<height;h++)
239 tbuf = startline;
240 *tbuf = 0;
241 for (w=0;w<bitmap.bmWidth;w++)
243 if ((w%8) == 0)
244 *tbuf = 0;
245 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
246 if ((w&7) == 7) ++tbuf;
248 startline += bitmap.bmWidthBytes;
250 break;
251 case 4:
252 for (h=0;h<height;h++)
254 tbuf = startline;
255 for (w=0;w<bitmap.bmWidth;w++)
257 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
258 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
260 startline += bitmap.bmWidthBytes;
262 break;
263 case 8:
264 for (h=0;h<height;h++)
266 tbuf = startline;
267 for (w=0;w<bitmap.bmWidth;w++)
268 *tbuf++ = XGetPixel(image,w,h);
269 startline += bitmap.bmWidthBytes;
271 break;
272 case 15:
273 case 16:
274 for (h=0;h<height;h++)
276 tbuf = startline;
277 for (w=0;w<bitmap.bmWidth;w++)
279 long pixel = XGetPixel(image,w,h);
281 *tbuf++ = pixel & 0xff;
282 *tbuf++ = (pixel>>8) & 0xff;
284 startline += bitmap.bmWidthBytes;
286 break;
287 case 24:
288 for (h=0;h<height;h++)
290 tbuf = startline;
291 for (w=0;w<bitmap.bmWidth;w++)
293 long pixel = XGetPixel(image,w,h);
295 *tbuf++ = pixel & 0xff;
296 *tbuf++ = (pixel>> 8) & 0xff;
297 *tbuf++ = (pixel>>16) & 0xff;
299 startline += bitmap.bmWidthBytes;
301 break;
303 case 32:
304 for (h=0;h<height;h++)
306 tbuf = startline;
307 for (w=0;w<bitmap.bmWidth;w++)
309 long pixel = XGetPixel(image,w,h);
311 *tbuf++ = pixel & 0xff;
312 *tbuf++ = (pixel>> 8) & 0xff;
313 *tbuf++ = (pixel>>16) & 0xff;
314 *tbuf++ = (pixel>>24) & 0xff;
316 startline += bitmap.bmWidthBytes;
318 break;
319 default:
320 FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
322 XDestroyImage( image );
323 wine_tsx11_unlock();
324 return count;
329 /******************************************************************************
330 * SetBitmapBits (X11DRV.@)
332 * RETURNS
333 * Success: Number of bytes used in setting the bitmap bits
334 * Failure: 0
336 LONG CDECL X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
338 BITMAP bitmap;
339 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
340 LONG height;
341 XImage *image;
342 const BYTE *sbuf, *startline;
343 int w, h;
345 if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
347 TRACE("(bmp=%p, bits=%p, count=0x%x)\n", hbitmap, bits, count);
349 height = count / bitmap.bmWidthBytes;
351 wine_tsx11_lock();
352 image = XCreateImage( gdi_display, visual, physBitmap->pixmap_depth, ZPixmap, 0, NULL,
353 bitmap.bmWidth, height, 32, 0 );
354 if (!(image->data = HeapAlloc( GetProcessHeap(), 0, image->bytes_per_line * height )))
356 WARN("No memory to create image data.\n");
357 XDestroyImage( image );
358 wine_tsx11_unlock();
359 return 0;
362 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
364 startline = bits;
366 switch (bitmap.bmBitsPixel)
368 case 1:
369 for (h=0;h<height;h++)
371 sbuf = startline;
372 for (w=0;w<bitmap.bmWidth;w++)
374 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
375 if ((w&7) == 7)
376 sbuf++;
378 startline += bitmap.bmWidthBytes;
380 break;
381 case 4:
382 for (h=0;h<height;h++)
384 sbuf = startline;
385 for (w=0;w<bitmap.bmWidth;w++)
387 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
388 else XPutPixel( image, w, h, *sbuf++ & 0xf );
390 startline += bitmap.bmWidthBytes;
392 break;
393 case 8:
394 for (h=0;h<height;h++)
396 sbuf = startline;
397 for (w=0;w<bitmap.bmWidth;w++)
398 XPutPixel(image,w,h,*sbuf++);
399 startline += bitmap.bmWidthBytes;
401 break;
402 case 15:
403 case 16:
404 for (h=0;h<height;h++)
406 sbuf = startline;
407 for (w=0;w<bitmap.bmWidth;w++)
409 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
410 sbuf+=2;
412 startline += bitmap.bmWidthBytes;
414 break;
415 case 24:
416 for (h=0;h<height;h++)
418 sbuf = startline;
419 for (w=0;w<bitmap.bmWidth;w++)
421 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
422 sbuf += 3;
424 startline += bitmap.bmWidthBytes;
426 break;
427 case 32:
428 for (h=0;h<height;h++)
430 sbuf = startline;
431 for (w=0;w<bitmap.bmWidth;w++)
433 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
434 sbuf += 4;
436 startline += bitmap.bmWidthBytes;
438 break;
439 default:
440 FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
443 XPutImage( gdi_display, physBitmap->pixmap, get_bitmap_gc(physBitmap->pixmap_depth),
444 image, 0, 0, 0, 0, bitmap.bmWidth, height );
445 HeapFree( GetProcessHeap(), 0, image->data );
446 image->data = NULL;
447 XDestroyImage( image );
448 wine_tsx11_unlock();
449 return count;
452 /***********************************************************************
453 * DeleteBitmap (X11DRV.@)
455 BOOL CDECL X11DRV_DeleteBitmap( HBITMAP hbitmap )
457 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
459 if (physBitmap)
461 DIBSECTION dib;
463 if (GetObjectW( hbitmap, sizeof(dib), &dib ) == sizeof(dib))
464 X11DRV_DIB_DeleteDIBSection( physBitmap, &dib );
466 if (physBitmap->glxpixmap)
467 destroy_glxpixmap( gdi_display, physBitmap->glxpixmap );
468 wine_tsx11_lock();
469 if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
470 XDeleteContext( gdi_display, (XID)hbitmap, bitmap_context );
471 wine_tsx11_unlock();
472 HeapFree( GetProcessHeap(), 0, physBitmap );
474 return TRUE;
478 /***********************************************************************
479 * X11DRV_get_phys_bitmap
481 * Retrieve the X physical bitmap info.
483 X_PHYSBITMAP *X11DRV_get_phys_bitmap( HBITMAP hbitmap )
485 X_PHYSBITMAP *ret;
487 wine_tsx11_lock();
488 if (XFindContext( gdi_display, (XID)hbitmap, bitmap_context, (char **)&ret )) ret = NULL;
489 wine_tsx11_unlock();
490 return ret;
494 /***********************************************************************
495 * X11DRV_init_phys_bitmap
497 * Initialize the X physical bitmap info.
499 X_PHYSBITMAP *X11DRV_init_phys_bitmap( HBITMAP hbitmap )
501 X_PHYSBITMAP *ret;
503 if ((ret = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret) )) != NULL)
505 ret->hbitmap = hbitmap;
506 wine_tsx11_lock();
507 XSaveContext( gdi_display, (XID)hbitmap, bitmap_context, (char *)ret );
508 wine_tsx11_unlock();
510 return ret;
514 /***********************************************************************
515 * X11DRV_get_pixmap
517 * Retrieve the pixmap associated to a bitmap.
519 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
521 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
523 if (!physBitmap) return 0;
524 return physBitmap->pixmap;