Release 960928
[wine/multimedia.git] / objects / bitmap.c
blob53237a818e3d1fde6678cd89a88765686db4df3c
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 /* GCs used for B&W and color bitmap operations */
21 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
23 extern void CLIPPING_UpdateGCRegion( DC * dc ); /* objects/clipping.c */
25 /***********************************************************************
26 * BITMAP_Init
28 BOOL32 BITMAP_Init(void)
30 Pixmap tmpPixmap;
32 /* Create the necessary GCs */
34 if ((tmpPixmap = XCreatePixmap( display, rootWindow, 1, 1, 1 )))
36 BITMAP_monoGC = XCreateGC( display, tmpPixmap, 0, NULL );
37 XSetGraphicsExposures( display, BITMAP_monoGC, False );
38 XFreePixmap( display, tmpPixmap );
41 if (screenDepth != 1)
43 if ((tmpPixmap = XCreatePixmap(display, rootWindow, 1,1,screenDepth)))
45 BITMAP_colorGC = XCreateGC( display, tmpPixmap, 0, NULL );
46 XSetGraphicsExposures( display, BITMAP_colorGC, False );
47 XFreePixmap( display, tmpPixmap );
50 return TRUE;
54 /***********************************************************************
55 * BITMAP_BmpToImage
57 * Create an XImage pointing to the bitmap data.
59 static XImage *BITMAP_BmpToImage( BITMAP16 * bmp, LPVOID bmpData )
61 extern void _XInitImageFuncPtrs( XImage* );
62 XImage * image;
64 image = XCreateImage( display, DefaultVisualOfScreen(screen),
65 bmp->bmBitsPixel, ZPixmap, 0, bmpData,
66 bmp->bmWidth, bmp->bmHeight, 16, bmp->bmWidthBytes );
67 if (!image) return 0;
68 image->byte_order = MSBFirst;
69 image->bitmap_bit_order = MSBFirst;
70 image->bitmap_unit = 16;
71 _XInitImageFuncPtrs(image);
72 return image;
76 /***********************************************************************
77 * CreateBitmap (GDI.48) (GDI32.25)
79 HBITMAP16 CreateBitmap( INT32 width, INT32 height, UINT32 planes,
80 UINT32 bpp, LPCVOID bits )
82 BITMAPOBJ * bmpObjPtr;
83 HBITMAP16 hbitmap;
85 dprintf_gdi( stddeb, "CreateBitmap: %dx%d, %d colors\n",
86 width, height, 1 << (planes*bpp) );
88 /* Check parameters */
89 if (!height || !width || planes != 1) return 0;
90 if ((bpp != 1) && (bpp != screenDepth)) return 0;
91 if (height < 0) height = -height;
92 if (width < 0) width = -width;
94 /* Create the BITMAPOBJ */
95 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
96 if (!hbitmap) return 0;
97 bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LIN_ADDR( hbitmap );
99 bmpObjPtr->size.cx = 0;
100 bmpObjPtr->size.cy = 0;
101 bmpObjPtr->bitmap.bmType = 0;
102 bmpObjPtr->bitmap.bmWidth = (INT16)width;
103 bmpObjPtr->bitmap.bmHeight = (INT16)height;
104 bmpObjPtr->bitmap.bmPlanes = (BYTE)planes;
105 bmpObjPtr->bitmap.bmBitsPixel = (BYTE)bpp;
106 bmpObjPtr->bitmap.bmWidthBytes = (INT16)BITMAP_WIDTH_BYTES( width, bpp );
107 bmpObjPtr->bitmap.bmBits = NULL;
109 /* Create the pixmap */
110 bmpObjPtr->pixmap = XCreatePixmap(display, rootWindow, width, height, bpp);
111 if (!bmpObjPtr->pixmap)
113 GDI_HEAP_FREE( hbitmap );
114 hbitmap = 0;
116 else if (bits) /* Set bitmap bits */
117 SetBitmapBits( hbitmap, height * bmpObjPtr->bitmap.bmWidthBytes, bits);
118 return hbitmap;
122 /***********************************************************************
123 * CreateCompatibleBitmap (GDI.51) (GDI32.30)
125 HBITMAP16 CreateCompatibleBitmap( HDC32 hdc, INT32 width, INT32 height )
127 HBITMAP hbmpRet = 0;
128 DC *dc;
130 dprintf_gdi( stddeb, "CreateCompatibleBitmap(%04x,%d,%d) = \n",
131 hdc, width, height );
132 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
133 if (!dc)
135 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
136 if (!dc) return 0;
138 hbmpRet = CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
139 dprintf_gdi(stddeb,"\t\t%04x\n", hbmpRet);
140 return hbmpRet;
144 /***********************************************************************
145 * CreateBitmapIndirect16 (GDI.49)
147 HBITMAP16 CreateBitmapIndirect16( const BITMAP16 * bmp )
149 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
150 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
154 /***********************************************************************
155 * CreateBitmapIndirect32 (GDI32.26)
157 HBITMAP32 CreateBitmapIndirect32( const BITMAP32 * bmp )
159 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
160 bmp->bmBitsPixel, bmp->bmBits );
164 /***********************************************************************
165 * GetBitmapBits (GDI.74) (GDI32.143)
167 LONG GetBitmapBits( HBITMAP32 hbitmap, LONG count, LPVOID buffer )
169 BITMAPOBJ * bmp;
170 LONG height;
171 XImage * image;
173 /* KLUDGE! */
174 if (count < 0) {
175 fprintf(stderr, "Negative number of bytes (%ld) passed to GetBitmapBits???\n", count );
176 count = -count;
178 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
179 if (!bmp) return 0;
181 /* Only get entire lines */
182 height = count / bmp->bitmap.bmWidthBytes;
183 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
184 dprintf_bitmap(stddeb, "GetBitmapBits: %dx%d %d colors %p fetched height: %ld\n",
185 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
186 1 << bmp->bitmap.bmBitsPixel, buffer, height );
187 if (!height) return 0;
189 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
190 CallTo32_LargeStack( (int(*)())XGetSubImage, 11,
191 display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
192 height, AllPlanes, ZPixmap, image, 0, 0 );
193 image->data = NULL;
194 XDestroyImage( image );
195 return height * bmp->bitmap.bmWidthBytes;
199 /***********************************************************************
200 * SetBitmapBits (GDI.106) (GDI32.303)
202 LONG SetBitmapBits( HBITMAP32 hbitmap, LONG count, LPCVOID buffer )
204 BITMAPOBJ * bmp;
205 LONG height;
206 XImage * image;
208 /* KLUDGE! */
209 if (count < 0) {
210 fprintf(stderr, "Negative number of bytes (%ld) passed to SetBitmapBits???\n", count );
211 count = -count;
213 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
214 if (!bmp) return 0;
216 dprintf_bitmap(stddeb, "SetBitmapBits: %dx%d %d colors %p\n",
217 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
218 1 << bmp->bitmap.bmBitsPixel, buffer );
220 /* Only set entire lines */
221 height = count / bmp->bitmap.bmWidthBytes;
222 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
223 if (!height) return 0;
225 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, (LPVOID)buffer ))) return 0;
226 CallTo32_LargeStack( XPutImage, 10,
227 display, bmp->pixmap, BITMAP_GC(bmp), image, 0, 0,
228 0, 0, bmp->bitmap.bmWidth, height );
229 image->data = NULL;
230 XDestroyImage( image );
231 return height * bmp->bitmap.bmWidthBytes;
234 /**********************************************************************
235 * LoadImageA (USER32.364)
236 * FIXME: implementation still lacks nearly all features, see LR_*
237 * defines in windows.h
240 HANDLE32 LoadImage32A(
241 HINSTANCE32 hinst,LPCSTR name,UINT32 type,INT32 desiredx,
242 INT32 desiredy,UINT32 loadflags
244 if (HIWORD(name)) {
245 dprintf_resource(stddeb,"LoadImage32A(0x%04x,%s,%d,%d,%d,0x%08x)\n",
246 hinst,name,type,desiredx,desiredy,loadflags
248 } else {
249 dprintf_resource(stddeb,"LoadImage32A(0x%04x,%p,%d,%d,%d,0x%08x)\n",
250 hinst,name,type,desiredx,desiredy,loadflags
253 switch (type) {
254 case IMAGE_BITMAP:
255 return LoadBitmap32A(hinst,name);
256 case IMAGE_ICON:
257 return LoadIcon32A(hinst,name);
258 case IMAGE_CURSOR:
259 return LoadCursor32A(hinst,name);
261 return 0;
264 /**********************************************************************
265 * CopyImage32 (USER32.60)
267 * FIXME: implementation still lacks nearly all features, see LR_*
268 * defines in windows.h
270 HANDLE32 CopyImage32( HANDLE32 hnd, UINT32 type, INT32 desiredx,
271 INT32 desiredy, UINT32 flags )
273 switch (type)
275 case IMAGE_BITMAP:
276 return hnd; /* FIXME ... need to copy here */
277 case IMAGE_ICON:
278 return CopyIcon32(hnd);
279 case IMAGE_CURSOR:
280 return CopyCursor32(hnd);
282 return 0;
286 /**********************************************************************
287 * LoadBitmap16 (USER.175)
289 HBITMAP16 LoadBitmap16( HINSTANCE16 instance, SEGPTR name )
291 HBITMAP16 hbitmap = 0;
292 HDC32 hdc;
293 HRSRC16 hRsrc;
294 HGLOBAL16 handle;
295 BITMAPINFO *info;
297 if (HIWORD(name))
299 char *str = (char *)PTR_SEG_TO_LIN( name );
300 dprintf_bitmap( stddeb, "LoadBitmap16(%04x,'%s')\n", instance, str );
301 if (str[0] == '#') name = (SEGPTR)(DWORD)(WORD)atoi( str + 1 );
303 else
304 dprintf_bitmap( stddeb, "LoadBitmap16(%04x,%04x)\n",
305 instance, LOWORD(name) );
307 if (!instance) /* OEM bitmap */
309 if (HIWORD((int)name)) return 0;
310 return OBM_LoadBitmap( LOWORD((int)name) );
313 if (!(hRsrc = FindResource16( instance, name, RT_BITMAP ))) return 0;
314 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
316 info = (BITMAPINFO *)LockResource16( handle );
317 if ((hdc = GetDC32(0)) != 0)
319 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
320 hbitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
321 bits, info, DIB_RGB_COLORS );
322 ReleaseDC32( 0, hdc );
324 FreeResource16( handle );
325 return hbitmap;
328 /**********************************************************************
329 * LoadBitmap32W (USER32.357)
331 HBITMAP32 LoadBitmap32W( HINSTANCE32 instance, LPCWSTR name )
333 HBITMAP32 hbitmap = 0;
334 HDC32 hdc;
335 HRSRC32 hRsrc;
336 HGLOBAL32 handle;
337 BITMAPINFO *info;
339 if (!instance) /* OEM bitmap */
341 if (HIWORD((int)name)) return 0;
342 return OBM_LoadBitmap( LOWORD((int)name) );
345 if (!(hRsrc = FindResource32W( instance, name,
346 (LPWSTR)RT_BITMAP ))) return 0;
347 if (!(handle = LoadResource32( instance, hRsrc ))) return 0;
349 info = (BITMAPINFO *)LockResource32( handle );
350 if ((hdc = GetDC32(0)) != 0)
352 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
353 hbitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
354 bits, info, DIB_RGB_COLORS );
355 ReleaseDC32( 0, hdc );
357 return hbitmap;
361 /**********************************************************************
362 * LoadBitmap32A (USER32.356)
364 HBITMAP32 LoadBitmap32A( HINSTANCE32 instance, LPCSTR name )
366 HBITMAP32 res;
367 if (!HIWORD(name)) res = LoadBitmap32W(instance,(LPWSTR)name);
368 else
370 LPWSTR uni = STRING32_DupAnsiToUni(name);
371 res = LoadBitmap32W(instance,uni);
372 free(uni);
374 return res;
378 /***********************************************************************
379 * BITMAP_DeleteObject
381 BOOL32 BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bitmap )
383 XFreePixmap( display, bitmap->pixmap );
384 return GDI_FreeObject( hbitmap );
388 /***********************************************************************
389 * BITMAP_GetObject16
391 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
393 if (count > sizeof(bmp->bitmap)) count = sizeof(bmp->bitmap);
394 memcpy( buffer, &bmp->bitmap, count );
395 return count;
399 /***********************************************************************
400 * BITMAP_GetObject32
402 INT32 BITMAP_GetObject32( BITMAPOBJ * bmp, INT32 count, LPVOID buffer )
404 BITMAP32 bmp32;
405 bmp32.bmType = bmp->bitmap.bmType;
406 bmp32.bmWidth = bmp->bitmap.bmWidth;
407 bmp32.bmHeight = bmp->bitmap.bmHeight;
408 bmp32.bmWidthBytes = bmp->bitmap.bmWidthBytes;
409 bmp32.bmPlanes = bmp->bitmap.bmPlanes;
410 bmp32.bmBitsPixel = bmp->bitmap.bmBitsPixel;
411 bmp32.bmBits = NULL;
412 if (count > sizeof(bmp32)) count = sizeof(bmp32);
413 memcpy( buffer, &bmp32, count );
414 return count;
418 /***********************************************************************
419 * BITMAP_SelectObject
421 HBITMAP16 BITMAP_SelectObject( DC * dc, HBITMAP16 hbitmap,
422 BITMAPOBJ * bmp )
424 HRGN hrgn;
425 HBITMAP prevHandle = dc->w.hBitmap;
427 if (!(dc->w.flags & DC_MEMORY)) return 0;
429 if (dc->w.hVisRgn)
430 SetRectRgn(dc->w.hVisRgn, 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
431 else
433 hrgn = CreateRectRgn( 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
434 if (!hrgn) return 0;
435 dc->w.hVisRgn = hrgn;
438 dc->u.x.drawable = bmp->pixmap;
439 dc->w.hBitmap = hbitmap;
441 /* Change GC depth if needed */
443 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
445 XFreeGC( display, dc->u.x.gc );
446 dc->u.x.gc = XCreateGC( display, dc->u.x.drawable, 0, NULL );
447 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
448 DC_InitDC( dc );
450 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
451 return prevHandle;
454 /***********************************************************************
455 * CreateDiscardableBitmap (GDI.156) (GDI32.38)
457 HBITMAP16 CreateDiscardableBitmap( HDC32 hdc, INT32 width, INT32 height )
459 dprintf_bitmap(stddeb,"CreateDiscardableBitmap(%04x, %d, %d); "
460 "// call CreateCompatibleBitmap() for now!\n",
461 hdc, width, height);
462 return CreateCompatibleBitmap(hdc, width, height);
466 /***********************************************************************
467 * GetBitmapDimensionEx16 (GDI.468)
469 BOOL16 GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
471 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
472 if (!bmp) return FALSE;
473 *size = bmp->size;
474 return TRUE;
478 /***********************************************************************
479 * GetBitmapDimensionEx32 (GDI32.144)
481 BOOL32 GetBitmapDimensionEx32( HBITMAP32 hbitmap, LPSIZE32 size )
483 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
484 if (!bmp) return FALSE;
485 size->cx = (INT32)bmp->size.cx;
486 size->cy = (INT32)bmp->size.cy;
487 return TRUE;
491 /***********************************************************************
492 * GetBitmapDimension (GDI.162)
494 DWORD GetBitmapDimension( HBITMAP16 hbitmap )
496 SIZE16 size;
497 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
498 return MAKELONG( size.cx, size.cy );
502 /***********************************************************************
503 * SetBitmapDimensionEx16 (GDI.478)
505 BOOL16 SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
506 LPSIZE16 prevSize )
508 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
509 if (!bmp) return FALSE;
510 if (prevSize) *prevSize = bmp->size;
511 bmp->size.cx = x;
512 bmp->size.cy = y;
513 return TRUE;
517 /***********************************************************************
518 * SetBitmapDimensionEx32 (GDI32.304)
520 BOOL32 SetBitmapDimensionEx32( HBITMAP32 hbitmap, INT32 x, INT32 y,
521 LPSIZE32 prevSize )
523 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
524 if (!bmp) return FALSE;
525 if (prevSize) CONV_SIZE16TO32( &bmp->size, prevSize );
526 bmp->size.cx = (INT16)x;
527 bmp->size.cy = (INT16)y;
528 return TRUE;
532 /***********************************************************************
533 * SetBitmapDimension (GDI.163)
535 DWORD SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
537 SIZE16 size;
538 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
539 return MAKELONG( size.cx, size.cy );