Release 970305
[wine/multimedia.git] / objects / bitmap.c
blob77147f760b74c3b5fcdbff6a96307ea6091686d6
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 "stddebug.h"
17 #include "debug.h"
19 #ifdef PRELIMINARY_WING16_SUPPORT
20 #include <sys/types.h>
21 #include <sys/ipc.h>
22 #include <sys/shm.h>
23 #endif
25 /* GCs used for B&W and color bitmap operations */
26 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
28 extern void CLIPPING_UpdateGCRegion( DC * dc ); /* objects/clipping.c */
32 /***********************************************************************
33 * BITMAP_BmpToImage
35 * Create an XImage pointing to the bitmap data.
37 static XImage *BITMAP_BmpToImage( BITMAP16 * bmp, LPVOID bmpData )
39 extern void _XInitImageFuncPtrs( XImage* );
40 XImage * image;
42 image = XCreateImage( display, DefaultVisualOfScreen(screen),
43 bmp->bmBitsPixel, ZPixmap, 0, bmpData,
44 bmp->bmWidth, bmp->bmHeight, 16, bmp->bmWidthBytes );
45 if (!image) return 0;
46 image->byte_order = MSBFirst;
47 image->bitmap_bit_order = MSBFirst;
48 image->bitmap_unit = 16;
49 _XInitImageFuncPtrs(image);
50 return image;
54 /***********************************************************************
55 * CreateBitmap16 (GDI.48)
57 HBITMAP16 CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
58 UINT16 bpp, LPCVOID bits )
60 return CreateBitmap32( width, height, planes, bpp, bits );
64 /***********************************************************************
65 * CreateBitmap32 (GDI32.25)
67 HBITMAP32 CreateBitmap32( INT32 width, INT32 height, UINT32 planes,
68 UINT32 bpp, LPCVOID bits )
70 BITMAPOBJ * bmpObjPtr;
71 HBITMAP32 hbitmap;
73 planes = (BYTE)planes;
74 bpp = (BYTE)bpp;
76 dprintf_gdi( stddeb, "CreateBitmap: %dx%d, %d colors\n",
77 width, height, 1 << (planes*bpp) );
79 /* Check parameters */
80 if (!height || !width || planes != 1) return 0;
81 if ((bpp != 1) && (bpp != screenDepth)) return 0;
82 if (height < 0) height = -height;
83 if (width < 0) width = -width;
85 /* Create the BITMAPOBJ */
86 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
87 if (!hbitmap) return 0;
88 bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LIN_ADDR( hbitmap );
90 bmpObjPtr->size.cx = 0;
91 bmpObjPtr->size.cy = 0;
92 bmpObjPtr->bitmap.bmType = 0;
93 bmpObjPtr->bitmap.bmWidth = (INT16)width;
94 bmpObjPtr->bitmap.bmHeight = (INT16)height;
95 bmpObjPtr->bitmap.bmPlanes = (BYTE)planes;
96 bmpObjPtr->bitmap.bmBitsPixel = (BYTE)bpp;
97 bmpObjPtr->bitmap.bmWidthBytes = (INT16)BITMAP_WIDTH_BYTES( width, bpp );
98 bmpObjPtr->bitmap.bmBits = NULL;
100 /* Create the pixmap */
101 bmpObjPtr->pixmap = XCreatePixmap(display, rootWindow, width, height, bpp);
102 if (!bmpObjPtr->pixmap)
104 GDI_HEAP_FREE( hbitmap );
105 hbitmap = 0;
107 else if (bits) /* Set bitmap bits */
108 SetBitmapBits32( hbitmap, height * bmpObjPtr->bitmap.bmWidthBytes,
109 bits );
110 return hbitmap;
114 /***********************************************************************
115 * CreateCompatibleBitmap16 (GDI.51)
117 HBITMAP16 CreateCompatibleBitmap16( HDC16 hdc, INT16 width, INT16 height )
119 return CreateCompatibleBitmap32( hdc, width, height );
123 /***********************************************************************
124 * CreateCompatibleBitmap32 (GDI32.30)
126 HBITMAP32 CreateCompatibleBitmap32( HDC32 hdc, INT32 width, INT32 height )
128 HBITMAP32 hbmpRet = 0;
129 DC *dc;
131 dprintf_gdi( stddeb, "CreateCompatibleBitmap(%04x,%d,%d) = \n",
132 hdc, width, height );
133 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
134 hbmpRet = CreateBitmap32( width, height, 1, dc->w.bitsPerPixel, NULL );
135 dprintf_gdi(stddeb,"\t\t%04x\n", hbmpRet);
136 return hbmpRet;
140 /***********************************************************************
141 * CreateBitmapIndirect16 (GDI.49)
143 HBITMAP16 CreateBitmapIndirect16( const BITMAP16 * bmp )
145 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
146 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
150 /***********************************************************************
151 * CreateBitmapIndirect32 (GDI32.26)
153 HBITMAP32 CreateBitmapIndirect32( const BITMAP32 * bmp )
155 return CreateBitmap32( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
156 bmp->bmBitsPixel, bmp->bmBits );
160 /***********************************************************************
161 * GetBitmapBits16 (GDI.74)
163 LONG GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
165 return GetBitmapBits32( hbitmap, count, buffer );
169 /***********************************************************************
170 * GetBitmapBits32 (GDI32.143)
172 LONG GetBitmapBits32( HBITMAP32 hbitmap, LONG count, LPVOID buffer )
174 BITMAPOBJ * bmp;
175 LONG height;
176 XImage * image;
178 /* KLUDGE! */
179 if (count < 0) {
180 fprintf(stderr, "Negative number of bytes (%ld) passed to GetBitmapBits???\n", count );
181 count = -count;
183 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
184 if (!bmp) return 0;
186 /* Only get entire lines */
187 height = count / bmp->bitmap.bmWidthBytes;
188 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
189 dprintf_bitmap(stddeb, "GetBitmapBits: %dx%d %d colors %p fetched height: %ld\n",
190 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
191 1 << bmp->bitmap.bmBitsPixel, buffer, height );
192 if (!height) return 0;
194 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
195 CallTo32_LargeStack( (int(*)())XGetSubImage, 11,
196 display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
197 height, AllPlanes, ZPixmap, image, 0, 0 );
198 image->data = NULL;
199 XDestroyImage( image );
200 return height * bmp->bitmap.bmWidthBytes;
204 /***********************************************************************
205 * SetBitmapBits16 (GDI.106)
207 LONG SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
209 return SetBitmapBits32( hbitmap, count, buffer );
213 /***********************************************************************
214 * SetBitmapBits32 (GDI32.303)
216 LONG SetBitmapBits32( HBITMAP32 hbitmap, LONG count, LPCVOID buffer )
218 BITMAPOBJ * bmp;
219 LONG height;
220 XImage * image;
222 /* KLUDGE! */
223 if (count < 0) {
224 fprintf(stderr, "Negative number of bytes (%ld) passed to SetBitmapBits???\n", count );
225 count = -count;
227 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
228 if (!bmp) return 0;
230 dprintf_bitmap(stddeb, "SetBitmapBits: %dx%d %d colors %p\n",
231 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
232 1 << bmp->bitmap.bmBitsPixel, buffer );
234 /* Only set entire lines */
235 height = count / bmp->bitmap.bmWidthBytes;
236 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
237 if (!height) return 0;
239 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, (LPVOID)buffer ))) return 0;
240 CallTo32_LargeStack( XPutImage, 10,
241 display, bmp->pixmap, BITMAP_GC(bmp), image, 0, 0,
242 0, 0, bmp->bitmap.bmWidth, height );
243 image->data = NULL;
244 XDestroyImage( image );
245 return height * bmp->bitmap.bmWidthBytes;
248 /**********************************************************************
249 * LoadImageA (USER32.364)
250 * FIXME: implementation still lacks nearly all features, see LR_*
251 * defines in windows.h
254 HANDLE32 LoadImage32A(
255 HINSTANCE32 hinst,LPCSTR name,UINT32 type,INT32 desiredx,
256 INT32 desiredy,UINT32 loadflags
258 if (HIWORD(name)) {
259 dprintf_resource(stddeb,"LoadImage32A(0x%04x,%s,%d,%d,%d,0x%08x)\n",
260 hinst,name,type,desiredx,desiredy,loadflags
262 } else {
263 dprintf_resource(stddeb,"LoadImage32A(0x%04x,%p,%d,%d,%d,0x%08x)\n",
264 hinst,name,type,desiredx,desiredy,loadflags
267 switch (type) {
268 case IMAGE_BITMAP:
269 return LoadBitmap32A(hinst,name);
270 case IMAGE_ICON:
271 return LoadIcon32A(hinst,name);
272 case IMAGE_CURSOR:
273 return LoadCursor32A(hinst,name);
275 return 0;
278 /**********************************************************************
279 * CopyImage32 (USER32.60)
281 * FIXME: implementation still lacks nearly all features, see LR_*
282 * defines in windows.h
284 HANDLE32 CopyImage32( HANDLE32 hnd, UINT32 type, INT32 desiredx,
285 INT32 desiredy, UINT32 flags )
287 switch (type)
289 case IMAGE_BITMAP:
290 return hnd; /* FIXME ... need to copy here */
291 case IMAGE_ICON:
292 return CopyIcon32(hnd);
293 case IMAGE_CURSOR:
294 return CopyCursor32(hnd);
296 return 0;
300 /**********************************************************************
301 * LoadBitmap16 (USER.175)
303 HBITMAP16 LoadBitmap16( HINSTANCE16 instance, SEGPTR name )
305 HBITMAP32 hbitmap = 0;
306 HDC32 hdc;
307 HRSRC16 hRsrc;
308 HGLOBAL16 handle;
309 BITMAPINFO *info;
311 if (HIWORD(name))
313 char *str = (char *)PTR_SEG_TO_LIN( name );
314 dprintf_bitmap( stddeb, "LoadBitmap16(%04x,'%s')\n", instance, str );
315 if (str[0] == '#') name = (SEGPTR)(DWORD)(WORD)atoi( str + 1 );
317 else
318 dprintf_bitmap( stddeb, "LoadBitmap16(%04x,%04x)\n",
319 instance, LOWORD(name) );
321 if (!instance) /* OEM bitmap */
323 if (HIWORD((int)name)) return 0;
324 return OBM_LoadBitmap( LOWORD((int)name) );
327 if (!(hRsrc = FindResource16( instance, name, RT_BITMAP ))) return 0;
328 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
330 info = (BITMAPINFO *)LockResource16( handle );
331 if ((hdc = GetDC32(0)) != 0)
333 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
334 hbitmap = CreateDIBitmap32( hdc, &info->bmiHeader, CBM_INIT,
335 bits, info, DIB_RGB_COLORS );
336 ReleaseDC32( 0, hdc );
338 FreeResource16( handle );
339 return hbitmap;
342 /**********************************************************************
343 * LoadBitmap32W (USER32.357)
345 HBITMAP32 LoadBitmap32W( HINSTANCE32 instance, LPCWSTR name )
347 HBITMAP32 hbitmap = 0;
348 HDC32 hdc;
349 HRSRC32 hRsrc;
350 HGLOBAL32 handle;
351 BITMAPINFO *info;
353 if (!instance) /* OEM bitmap */
355 if (HIWORD((int)name)) return 0;
356 return OBM_LoadBitmap( LOWORD((int)name) );
359 if (!(hRsrc = FindResource32W( instance, name,
360 (LPWSTR)RT_BITMAP ))) return 0;
361 if (!(handle = LoadResource32( instance, hRsrc ))) return 0;
363 info = (BITMAPINFO *)LockResource32( handle );
364 if ((hdc = GetDC32(0)) != 0)
366 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
367 hbitmap = CreateDIBitmap32( hdc, &info->bmiHeader, CBM_INIT,
368 bits, info, DIB_RGB_COLORS );
369 ReleaseDC32( 0, hdc );
371 return hbitmap;
375 /**********************************************************************
376 * LoadBitmap32A (USER32.356)
378 HBITMAP32 LoadBitmap32A( HINSTANCE32 instance, LPCSTR name )
380 HBITMAP32 res;
381 if (!HIWORD(name)) res = LoadBitmap32W( instance, (LPWSTR)name );
382 else
384 LPWSTR uni = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
385 res = LoadBitmap32W( instance, uni );
386 HeapFree( GetProcessHeap(), 0, uni );
388 return res;
392 /***********************************************************************
393 * BITMAP_DeleteObject
395 BOOL32 BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
397 #ifdef PRELIMINARY_WING16_SUPPORT
398 if( bmp->bitmap.bmBits )
399 XShmDetach( display, (XShmSegmentInfo*)bmp->bitmap.bmBits );
400 #endif
402 XFreePixmap( display, bmp->pixmap );
403 #ifdef PRELIMINARY_WING16_SUPPORT
404 if( bmp->bitmap.bmBits )
406 __ShmBitmapCtl* p = (__ShmBitmapCtl*)bmp->bitmap.bmBits;
407 WORD sel = HIWORD(p->bits);
408 unsigned long l, limit = GetSelectorLimit(sel);
410 for( l = 0; l < limit; l += 0x10000, sel += __AHINCR )
411 FreeSelector(sel);
412 shmctl(p->si.shmid, IPC_RMID, NULL);
413 shmdt(p->si.shmaddr); /* already marked for destruction */
415 #endif
416 return GDI_FreeObject( hbitmap );
420 /***********************************************************************
421 * BITMAP_GetObject16
423 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
425 if (count > sizeof(bmp->bitmap)) count = sizeof(bmp->bitmap);
426 memcpy( buffer, &bmp->bitmap, count );
427 return count;
431 /***********************************************************************
432 * BITMAP_GetObject32
434 INT32 BITMAP_GetObject32( BITMAPOBJ * bmp, INT32 count, LPVOID buffer )
436 BITMAP32 bmp32;
437 bmp32.bmType = bmp->bitmap.bmType;
438 bmp32.bmWidth = bmp->bitmap.bmWidth;
439 bmp32.bmHeight = bmp->bitmap.bmHeight;
440 bmp32.bmWidthBytes = bmp->bitmap.bmWidthBytes;
441 bmp32.bmPlanes = bmp->bitmap.bmPlanes;
442 bmp32.bmBitsPixel = bmp->bitmap.bmBitsPixel;
443 bmp32.bmBits = NULL;
444 if (count > sizeof(bmp32)) count = sizeof(bmp32);
445 memcpy( buffer, &bmp32, count );
446 return count;
451 /***********************************************************************
452 * CreateDiscardableBitmap16 (GDI.156)
454 HBITMAP16 CreateDiscardableBitmap16( HDC16 hdc, INT16 width, INT16 height )
456 return CreateCompatibleBitmap16( hdc, width, height );
460 /***********************************************************************
461 * CreateDiscardableBitmap32 (GDI32.38)
463 HBITMAP32 CreateDiscardableBitmap32( HDC32 hdc, INT32 width, INT32 height )
465 return CreateCompatibleBitmap32( hdc, width, height );
469 /***********************************************************************
470 * GetBitmapDimensionEx16 (GDI.468)
472 BOOL16 GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
474 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
475 if (!bmp) return FALSE;
476 *size = bmp->size;
477 return TRUE;
481 /***********************************************************************
482 * GetBitmapDimensionEx32 (GDI32.144)
484 BOOL32 GetBitmapDimensionEx32( HBITMAP32 hbitmap, LPSIZE32 size )
486 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
487 if (!bmp) return FALSE;
488 size->cx = (INT32)bmp->size.cx;
489 size->cy = (INT32)bmp->size.cy;
490 return TRUE;
494 /***********************************************************************
495 * GetBitmapDimension (GDI.162)
497 DWORD GetBitmapDimension( HBITMAP16 hbitmap )
499 SIZE16 size;
500 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
501 return MAKELONG( size.cx, size.cy );
505 /***********************************************************************
506 * SetBitmapDimensionEx16 (GDI.478)
508 BOOL16 SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
509 LPSIZE16 prevSize )
511 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
512 if (!bmp) return FALSE;
513 if (prevSize) *prevSize = bmp->size;
514 bmp->size.cx = x;
515 bmp->size.cy = y;
516 return TRUE;
520 /***********************************************************************
521 * SetBitmapDimensionEx32 (GDI32.304)
523 BOOL32 SetBitmapDimensionEx32( HBITMAP32 hbitmap, INT32 x, INT32 y,
524 LPSIZE32 prevSize )
526 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
527 if (!bmp) return FALSE;
528 if (prevSize) CONV_SIZE16TO32( &bmp->size, prevSize );
529 bmp->size.cx = (INT16)x;
530 bmp->size.cy = (INT16)y;
531 return TRUE;
535 /***********************************************************************
536 * SetBitmapDimension (GDI.163)
538 DWORD SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
540 SIZE16 size;
541 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
542 return MAKELONG( size.cx, size.cy );