Release 961215
[wine.git] / objects / bitmap.c
blob0c535f5724b102344367d183880003e762f5dec1
1 /*
2 * GDI bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
11 #include "gdi.h"
12 #include "callback.h"
13 #include "dc.h"
14 #include "bitmap.h"
15 #include "heap.h"
16 #include "string32.h"
17 #include "stddebug.h"
18 #include "debug.h"
20 #ifdef PRELIMINARY_WING16_SUPPORT
21 #include <sys/types.h>
22 #include <sys/ipc.h>
23 #include <sys/shm.h>
24 #endif
26 /* GCs used for B&W and color bitmap operations */
27 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
29 extern void CLIPPING_UpdateGCRegion( DC * dc ); /* objects/clipping.c */
31 /***********************************************************************
32 * BITMAP_Init
34 BOOL32 BITMAP_Init(void)
36 Pixmap tmpPixmap;
38 /* Create the necessary GCs */
40 if ((tmpPixmap = XCreatePixmap( display, rootWindow, 1, 1, 1 )))
42 BITMAP_monoGC = XCreateGC( display, tmpPixmap, 0, NULL );
43 XSetGraphicsExposures( display, BITMAP_monoGC, False );
44 XFreePixmap( display, tmpPixmap );
47 if (screenDepth != 1)
49 if ((tmpPixmap = XCreatePixmap(display, rootWindow, 1,1,screenDepth)))
51 BITMAP_colorGC = XCreateGC( display, tmpPixmap, 0, NULL );
52 XSetGraphicsExposures( display, BITMAP_colorGC, False );
53 XFreePixmap( display, tmpPixmap );
56 return TRUE;
60 /***********************************************************************
61 * BITMAP_BmpToImage
63 * Create an XImage pointing to the bitmap data.
65 static XImage *BITMAP_BmpToImage( BITMAP16 * bmp, LPVOID bmpData )
67 extern void _XInitImageFuncPtrs( XImage* );
68 XImage * image;
70 image = XCreateImage( display, DefaultVisualOfScreen(screen),
71 bmp->bmBitsPixel, ZPixmap, 0, bmpData,
72 bmp->bmWidth, bmp->bmHeight, 16, bmp->bmWidthBytes );
73 if (!image) return 0;
74 image->byte_order = MSBFirst;
75 image->bitmap_bit_order = MSBFirst;
76 image->bitmap_unit = 16;
77 _XInitImageFuncPtrs(image);
78 return image;
82 /***********************************************************************
83 * CreateBitmap (GDI.48) (GDI32.25)
85 HBITMAP16 CreateBitmap( INT32 width, INT32 height, UINT32 planes,
86 UINT32 bpp, LPCVOID bits )
88 BITMAPOBJ * bmpObjPtr;
89 HBITMAP16 hbitmap;
91 dprintf_gdi( stddeb, "CreateBitmap: %dx%d, %d colors\n",
92 width, height, 1 << (planes*bpp) );
94 /* Check parameters */
95 if (!height || !width || planes != 1) return 0;
96 if ((bpp != 1) && (bpp != screenDepth)) return 0;
97 if (height < 0) height = -height;
98 if (width < 0) width = -width;
100 /* Create the BITMAPOBJ */
101 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
102 if (!hbitmap) return 0;
103 bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LIN_ADDR( hbitmap );
105 bmpObjPtr->size.cx = 0;
106 bmpObjPtr->size.cy = 0;
107 bmpObjPtr->bitmap.bmType = 0;
108 bmpObjPtr->bitmap.bmWidth = (INT16)width;
109 bmpObjPtr->bitmap.bmHeight = (INT16)height;
110 bmpObjPtr->bitmap.bmPlanes = (BYTE)planes;
111 bmpObjPtr->bitmap.bmBitsPixel = (BYTE)bpp;
112 bmpObjPtr->bitmap.bmWidthBytes = (INT16)BITMAP_WIDTH_BYTES( width, bpp );
113 bmpObjPtr->bitmap.bmBits = NULL;
115 /* Create the pixmap */
116 bmpObjPtr->pixmap = XCreatePixmap(display, rootWindow, width, height, bpp);
117 if (!bmpObjPtr->pixmap)
119 GDI_HEAP_FREE( hbitmap );
120 hbitmap = 0;
122 else if (bits) /* Set bitmap bits */
123 SetBitmapBits( hbitmap, height * bmpObjPtr->bitmap.bmWidthBytes, bits);
124 return hbitmap;
128 /***********************************************************************
129 * CreateCompatibleBitmap (GDI.51) (GDI32.30)
131 HBITMAP16 CreateCompatibleBitmap( HDC32 hdc, INT32 width, INT32 height )
133 HBITMAP16 hbmpRet = 0;
134 DC *dc;
136 dprintf_gdi( stddeb, "CreateCompatibleBitmap(%04x,%d,%d) = \n",
137 hdc, width, height );
138 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
139 if (!dc)
141 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
142 if (!dc) return 0;
144 hbmpRet = CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
145 dprintf_gdi(stddeb,"\t\t%04x\n", hbmpRet);
146 return hbmpRet;
150 /***********************************************************************
151 * CreateBitmapIndirect16 (GDI.49)
153 HBITMAP16 CreateBitmapIndirect16( const BITMAP16 * bmp )
155 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
156 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
160 /***********************************************************************
161 * CreateBitmapIndirect32 (GDI32.26)
163 HBITMAP32 CreateBitmapIndirect32( const BITMAP32 * bmp )
165 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
166 bmp->bmBitsPixel, bmp->bmBits );
170 /***********************************************************************
171 * GetBitmapBits (GDI.74) (GDI32.143)
173 LONG GetBitmapBits( HBITMAP32 hbitmap, LONG count, LPVOID buffer )
175 BITMAPOBJ * bmp;
176 LONG height;
177 XImage * image;
179 /* KLUDGE! */
180 if (count < 0) {
181 fprintf(stderr, "Negative number of bytes (%ld) passed to GetBitmapBits???\n", count );
182 count = -count;
184 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
185 if (!bmp) return 0;
187 /* Only get entire lines */
188 height = count / bmp->bitmap.bmWidthBytes;
189 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
190 dprintf_bitmap(stddeb, "GetBitmapBits: %dx%d %d colors %p fetched height: %ld\n",
191 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
192 1 << bmp->bitmap.bmBitsPixel, buffer, height );
193 if (!height) return 0;
195 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
196 CallTo32_LargeStack( (int(*)())XGetSubImage, 11,
197 display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
198 height, AllPlanes, ZPixmap, image, 0, 0 );
199 image->data = NULL;
200 XDestroyImage( image );
201 return height * bmp->bitmap.bmWidthBytes;
205 /***********************************************************************
206 * SetBitmapBits (GDI.106) (GDI32.303)
208 LONG SetBitmapBits( HBITMAP32 hbitmap, LONG count, LPCVOID buffer )
210 BITMAPOBJ * bmp;
211 LONG height;
212 XImage * image;
214 /* KLUDGE! */
215 if (count < 0) {
216 fprintf(stderr, "Negative number of bytes (%ld) passed to SetBitmapBits???\n", count );
217 count = -count;
219 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
220 if (!bmp) return 0;
222 dprintf_bitmap(stddeb, "SetBitmapBits: %dx%d %d colors %p\n",
223 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
224 1 << bmp->bitmap.bmBitsPixel, buffer );
226 /* Only set entire lines */
227 height = count / bmp->bitmap.bmWidthBytes;
228 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
229 if (!height) return 0;
231 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, (LPVOID)buffer ))) return 0;
232 CallTo32_LargeStack( XPutImage, 10,
233 display, bmp->pixmap, BITMAP_GC(bmp), image, 0, 0,
234 0, 0, bmp->bitmap.bmWidth, height );
235 image->data = NULL;
236 XDestroyImage( image );
237 return height * bmp->bitmap.bmWidthBytes;
240 /**********************************************************************
241 * LoadImageA (USER32.364)
242 * FIXME: implementation still lacks nearly all features, see LR_*
243 * defines in windows.h
246 HANDLE32 LoadImage32A(
247 HINSTANCE32 hinst,LPCSTR name,UINT32 type,INT32 desiredx,
248 INT32 desiredy,UINT32 loadflags
250 if (HIWORD(name)) {
251 dprintf_resource(stddeb,"LoadImage32A(0x%04x,%s,%d,%d,%d,0x%08x)\n",
252 hinst,name,type,desiredx,desiredy,loadflags
254 } else {
255 dprintf_resource(stddeb,"LoadImage32A(0x%04x,%p,%d,%d,%d,0x%08x)\n",
256 hinst,name,type,desiredx,desiredy,loadflags
259 switch (type) {
260 case IMAGE_BITMAP:
261 return LoadBitmap32A(hinst,name);
262 case IMAGE_ICON:
263 return LoadIcon32A(hinst,name);
264 case IMAGE_CURSOR:
265 return LoadCursor32A(hinst,name);
267 return 0;
270 /**********************************************************************
271 * CopyImage32 (USER32.60)
273 * FIXME: implementation still lacks nearly all features, see LR_*
274 * defines in windows.h
276 HANDLE32 CopyImage32( HANDLE32 hnd, UINT32 type, INT32 desiredx,
277 INT32 desiredy, UINT32 flags )
279 switch (type)
281 case IMAGE_BITMAP:
282 return hnd; /* FIXME ... need to copy here */
283 case IMAGE_ICON:
284 return CopyIcon32(hnd);
285 case IMAGE_CURSOR:
286 return CopyCursor32(hnd);
288 return 0;
292 /**********************************************************************
293 * LoadBitmap16 (USER.175)
295 HBITMAP16 LoadBitmap16( HINSTANCE16 instance, SEGPTR name )
297 HBITMAP16 hbitmap = 0;
298 HDC32 hdc;
299 HRSRC16 hRsrc;
300 HGLOBAL16 handle;
301 BITMAPINFO *info;
303 if (HIWORD(name))
305 char *str = (char *)PTR_SEG_TO_LIN( name );
306 dprintf_bitmap( stddeb, "LoadBitmap16(%04x,'%s')\n", instance, str );
307 if (str[0] == '#') name = (SEGPTR)(DWORD)(WORD)atoi( str + 1 );
309 else
310 dprintf_bitmap( stddeb, "LoadBitmap16(%04x,%04x)\n",
311 instance, LOWORD(name) );
313 if (!instance) /* OEM bitmap */
315 if (HIWORD((int)name)) return 0;
316 return OBM_LoadBitmap( LOWORD((int)name) );
319 if (!(hRsrc = FindResource16( instance, name, RT_BITMAP ))) return 0;
320 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
322 info = (BITMAPINFO *)LockResource16( handle );
323 if ((hdc = GetDC32(0)) != 0)
325 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
326 hbitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
327 bits, info, DIB_RGB_COLORS );
328 ReleaseDC32( 0, hdc );
330 FreeResource16( handle );
331 return hbitmap;
334 /**********************************************************************
335 * LoadBitmap32W (USER32.357)
337 HBITMAP32 LoadBitmap32W( HINSTANCE32 instance, LPCWSTR name )
339 HBITMAP32 hbitmap = 0;
340 HDC32 hdc;
341 HRSRC32 hRsrc;
342 HGLOBAL32 handle;
343 BITMAPINFO *info;
345 if (!instance) /* OEM bitmap */
347 if (HIWORD((int)name)) return 0;
348 return OBM_LoadBitmap( LOWORD((int)name) );
351 if (!(hRsrc = FindResource32W( instance, name,
352 (LPWSTR)RT_BITMAP ))) return 0;
353 if (!(handle = LoadResource32( instance, hRsrc ))) return 0;
355 info = (BITMAPINFO *)LockResource32( handle );
356 if ((hdc = GetDC32(0)) != 0)
358 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
359 hbitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
360 bits, info, DIB_RGB_COLORS );
361 ReleaseDC32( 0, hdc );
363 return hbitmap;
367 /**********************************************************************
368 * LoadBitmap32A (USER32.356)
370 HBITMAP32 LoadBitmap32A( HINSTANCE32 instance, LPCSTR name )
372 HBITMAP32 res;
373 if (!HIWORD(name)) res = LoadBitmap32W(instance,(LPWSTR)name);
374 else
376 LPWSTR uni = STRING32_DupAnsiToUni(name);
377 res = LoadBitmap32W(instance,uni);
378 free(uni);
380 return res;
384 /***********************************************************************
385 * BITMAP_DeleteObject
387 BOOL32 BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
389 #ifdef PRELIMINARY_WING16_SUPPORT
390 if( bmp->bitmap.bmBits )
391 XShmDetach( display, (XShmSegmentInfo*)bmp->bitmap.bmBits );
392 #endif
394 XFreePixmap( display, bmp->pixmap );
395 #ifdef PRELIMINARY_WING16_SUPPORT
396 if( bmp->bitmap.bmBits )
398 __ShmBitmapCtl* p = (__ShmBitmapCtl*)bmp->bitmap.bmBits;
399 WORD sel = HIWORD(p->bits);
400 unsigned long l, limit = GetSelectorLimit(sel);
402 for( l = 0; l < limit; l += 0x10000, sel += __AHINCR )
403 FreeSelector(sel);
404 shmctl(p->si.shmid, IPC_RMID, NULL);
405 shmdt(p->si.shmaddr); /* already marked for destruction */
407 #endif
408 return GDI_FreeObject( hbitmap );
412 /***********************************************************************
413 * BITMAP_GetObject16
415 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
417 if (count > sizeof(bmp->bitmap)) count = sizeof(bmp->bitmap);
418 memcpy( buffer, &bmp->bitmap, count );
419 return count;
423 /***********************************************************************
424 * BITMAP_GetObject32
426 INT32 BITMAP_GetObject32( BITMAPOBJ * bmp, INT32 count, LPVOID buffer )
428 BITMAP32 bmp32;
429 bmp32.bmType = bmp->bitmap.bmType;
430 bmp32.bmWidth = bmp->bitmap.bmWidth;
431 bmp32.bmHeight = bmp->bitmap.bmHeight;
432 bmp32.bmWidthBytes = bmp->bitmap.bmWidthBytes;
433 bmp32.bmPlanes = bmp->bitmap.bmPlanes;
434 bmp32.bmBitsPixel = bmp->bitmap.bmBitsPixel;
435 bmp32.bmBits = NULL;
436 if (count > sizeof(bmp32)) count = sizeof(bmp32);
437 memcpy( buffer, &bmp32, count );
438 return count;
442 /***********************************************************************
443 * BITMAP_SelectObject
445 HBITMAP16 BITMAP_SelectObject( DC * dc, HBITMAP16 hbitmap,
446 BITMAPOBJ * bmp )
448 HRGN32 hrgn;
449 HBITMAP16 prevHandle = dc->w.hBitmap;
451 if (!(dc->w.flags & DC_MEMORY)) return 0;
453 if (dc->w.hVisRgn)
454 SetRectRgn(dc->w.hVisRgn, 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
455 else
457 hrgn = CreateRectRgn32(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
458 if (!hrgn) return 0;
459 dc->w.hVisRgn = hrgn;
462 dc->u.x.drawable = bmp->pixmap;
463 dc->w.hBitmap = hbitmap;
465 /* Change GC depth if needed */
467 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
469 XFreeGC( display, dc->u.x.gc );
470 dc->u.x.gc = XCreateGC( display, dc->u.x.drawable, 0, NULL );
471 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
472 DC_InitDC( dc );
474 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
475 return prevHandle;
478 /***********************************************************************
479 * CreateDiscardableBitmap (GDI.156) (GDI32.38)
481 HBITMAP16 CreateDiscardableBitmap( HDC32 hdc, INT32 width, INT32 height )
483 dprintf_bitmap(stddeb,"CreateDiscardableBitmap(%04x, %d, %d); "
484 "// call CreateCompatibleBitmap() for now!\n",
485 hdc, width, height);
486 return CreateCompatibleBitmap(hdc, width, height);
490 /***********************************************************************
491 * GetBitmapDimensionEx16 (GDI.468)
493 BOOL16 GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
495 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
496 if (!bmp) return FALSE;
497 *size = bmp->size;
498 return TRUE;
502 /***********************************************************************
503 * GetBitmapDimensionEx32 (GDI32.144)
505 BOOL32 GetBitmapDimensionEx32( HBITMAP32 hbitmap, LPSIZE32 size )
507 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
508 if (!bmp) return FALSE;
509 size->cx = (INT32)bmp->size.cx;
510 size->cy = (INT32)bmp->size.cy;
511 return TRUE;
515 /***********************************************************************
516 * GetBitmapDimension (GDI.162)
518 DWORD GetBitmapDimension( HBITMAP16 hbitmap )
520 SIZE16 size;
521 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
522 return MAKELONG( size.cx, size.cy );
526 /***********************************************************************
527 * SetBitmapDimensionEx16 (GDI.478)
529 BOOL16 SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
530 LPSIZE16 prevSize )
532 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
533 if (!bmp) return FALSE;
534 if (prevSize) *prevSize = bmp->size;
535 bmp->size.cx = x;
536 bmp->size.cy = y;
537 return TRUE;
541 /***********************************************************************
542 * SetBitmapDimensionEx32 (GDI32.304)
544 BOOL32 SetBitmapDimensionEx32( HBITMAP32 hbitmap, INT32 x, INT32 y,
545 LPSIZE32 prevSize )
547 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
548 if (!bmp) return FALSE;
549 if (prevSize) CONV_SIZE16TO32( &bmp->size, prevSize );
550 bmp->size.cx = (INT16)x;
551 bmp->size.cy = (INT16)y;
552 return TRUE;
556 /***********************************************************************
557 * SetBitmapDimension (GDI.163)
559 DWORD SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
561 SIZE16 size;
562 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
563 return MAKELONG( size.cx, size.cy );