Release 960728
[wine/multimedia.git] / objects / bitmap.c
blobf609f25c41dd848ef74f2fcfacf9d55ffe66cbf9
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 BOOL 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 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
134 hbmpRet = CreateBitmap( 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 CreateBitmap( 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 CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
156 bmp->bmBitsPixel, bmp->bmBits );
160 /***********************************************************************
161 * GetBitmapBits (GDI.74) (GDI32.143)
163 LONG GetBitmapBits( HBITMAP32 hbitmap, LONG count, LPVOID buffer )
165 BITMAPOBJ * bmp;
166 LONG height;
167 XImage * image;
169 /* KLUDGE! */
170 if (count < 0) {
171 fprintf(stderr, "Negative number of bytes (%ld) passed to GetBitmapBits???\n", count );
172 count = -count;
174 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
175 if (!bmp) return 0;
177 /* Only get entire lines */
178 height = count / bmp->bitmap.bmWidthBytes;
179 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
180 dprintf_bitmap(stddeb, "GetBitmapBits: %dx%d %d colors %p fetched height: %ld\n",
181 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
182 1 << bmp->bitmap.bmBitsPixel, buffer, height );
183 if (!height) return 0;
185 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
186 CallTo32_LargeStack( (int(*)())XGetSubImage, 11,
187 display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
188 height, AllPlanes, ZPixmap, image, 0, 0 );
189 image->data = NULL;
190 XDestroyImage( image );
191 return height * bmp->bitmap.bmWidthBytes;
195 /***********************************************************************
196 * SetBitmapBits (GDI.106) (GDI32.303)
198 LONG SetBitmapBits( HBITMAP32 hbitmap, LONG count, LPCVOID buffer )
200 BITMAPOBJ * bmp;
201 LONG height;
202 XImage * image;
204 /* KLUDGE! */
205 if (count < 0) {
206 fprintf(stderr, "Negative number of bytes (%ld) passed to SetBitmapBits???\n", count );
207 count = -count;
209 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
210 if (!bmp) return 0;
212 dprintf_bitmap(stddeb, "SetBitmapBits: %dx%d %d colors %p\n",
213 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
214 1 << bmp->bitmap.bmBitsPixel, buffer );
216 /* Only set entire lines */
217 height = count / bmp->bitmap.bmWidthBytes;
218 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
219 if (!height) return 0;
221 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, (LPVOID)buffer ))) return 0;
222 CallTo32_LargeStack( XPutImage, 10,
223 display, bmp->pixmap, BITMAP_GC(bmp), image, 0, 0,
224 0, 0, bmp->bitmap.bmWidth, height );
225 image->data = NULL;
226 XDestroyImage( image );
227 return height * bmp->bitmap.bmWidthBytes;
230 /**********************************************************************
231 * LoadImageA (USER32.364)
232 * FIXME: implementation still lacks nearly all features, see LR_*
233 * defines in windows.h
236 HANDLE32 LoadImage32A(
237 HINSTANCE32 hinst,LPCSTR name,UINT32 type,INT32 desiredx,
238 INT32 desiredy,UINT32 loadflags
240 if (HIWORD(name)) {
241 dprintf_resource(stddeb,"LoadImage32A(0x%04x,%s,%d,%d,%d,0x%08x)\n",
242 hinst,name,type,desiredx,desiredy,loadflags
244 } else {
245 dprintf_resource(stddeb,"LoadImage32A(0x%04x,%p,%d,%d,%d,0x%08x)\n",
246 hinst,name,type,desiredx,desiredy,loadflags
249 switch (type) {
250 case IMAGE_BITMAP:
251 return LoadBitmap32A(hinst,name);
252 case IMAGE_ICON:
253 return LoadIcon32A(hinst,name);
254 case IMAGE_CURSOR:
255 return LoadCursor32A(hinst,name);
257 return 0;
260 /**********************************************************************
261 * LoadBitmap16 (USER.175)
263 HBITMAP16 LoadBitmap16( HINSTANCE16 instance, SEGPTR name )
265 HBITMAP16 hbitmap = 0;
266 HDC hdc;
267 HRSRC16 hRsrc;
268 HGLOBAL16 handle;
269 BITMAPINFO *info;
271 if (HIWORD(name))
273 char *str = (char *)PTR_SEG_TO_LIN( name );
274 dprintf_bitmap( stddeb, "LoadBitmap16(%04x,'%s')\n", instance, str );
275 if (str[0] == '#') name = (SEGPTR)(DWORD)(WORD)atoi( str + 1 );
277 else
278 dprintf_bitmap( stddeb, "LoadBitmap16(%04x,%04x)\n",
279 instance, LOWORD(name) );
281 if (!instance) /* OEM bitmap */
283 if (HIWORD((int)name)) return 0;
284 return OBM_LoadBitmap( LOWORD((int)name) );
287 if (!(hRsrc = FindResource16( instance, name, RT_BITMAP ))) return 0;
288 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
290 info = (BITMAPINFO *)LockResource16( handle );
291 if ((hdc = GetDC(0)) != 0)
293 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
294 hbitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
295 bits, info, DIB_RGB_COLORS );
296 ReleaseDC( 0, hdc );
298 FreeResource16( handle );
299 return hbitmap;
302 /**********************************************************************
303 * LoadBitmap32W (USER32.357)
305 HBITMAP32 LoadBitmap32W( HINSTANCE32 instance, LPCWSTR name )
307 HBITMAP32 hbitmap = 0;
308 HDC hdc;
309 HRSRC32 hRsrc;
310 HGLOBAL32 handle;
311 BITMAPINFO *info;
313 if (!instance) /* OEM bitmap */
315 if (HIWORD((int)name)) return 0;
316 return OBM_LoadBitmap( LOWORD((int)name) );
319 if (!(hRsrc = FindResource32W( instance, name,
320 (LPWSTR)RT_BITMAP ))) return 0;
321 if (!(handle = LoadResource32( instance, hRsrc ))) return 0;
323 info = (BITMAPINFO *)LockResource32( handle );
324 if ((hdc = GetDC(0)) != 0)
326 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
327 hbitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
328 bits, info, DIB_RGB_COLORS );
329 ReleaseDC( 0, hdc );
331 return hbitmap;
335 /**********************************************************************
336 * LoadBitmap32A (USER32.356)
338 HBITMAP32 LoadBitmap32A( HINSTANCE32 instance, LPCSTR name )
340 HBITMAP32 res;
341 if (!HIWORD(name)) res = LoadBitmap32W(instance,(LPWSTR)name);
342 else
344 LPWSTR uni = STRING32_DupAnsiToUni(name);
345 res = LoadBitmap32W(instance,uni);
346 free(uni);
348 return res;
352 /***********************************************************************
353 * BITMAP_DeleteObject
355 BOOL BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bitmap )
357 XFreePixmap( display, bitmap->pixmap );
358 return GDI_FreeObject( hbitmap );
362 /***********************************************************************
363 * BITMAP_GetObject16
365 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
367 if (count > sizeof(bmp->bitmap)) count = sizeof(bmp->bitmap);
368 memcpy( buffer, &bmp->bitmap, count );
369 return count;
373 /***********************************************************************
374 * BITMAP_GetObject32
376 INT32 BITMAP_GetObject32( BITMAPOBJ * bmp, INT32 count, LPVOID buffer )
378 BITMAP32 bmp32;
379 bmp32.bmType = bmp->bitmap.bmType;
380 bmp32.bmWidth = bmp->bitmap.bmWidth;
381 bmp32.bmHeight = bmp->bitmap.bmHeight;
382 bmp32.bmWidthBytes = bmp->bitmap.bmWidthBytes;
383 bmp32.bmPlanes = bmp->bitmap.bmPlanes;
384 bmp32.bmBitsPixel = bmp->bitmap.bmBitsPixel;
385 bmp32.bmBits = NULL;
386 if (count > sizeof(bmp32)) count = sizeof(bmp32);
387 memcpy( buffer, &bmp32, count );
388 return count;
392 /***********************************************************************
393 * BITMAP_SelectObject
395 HBITMAP BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
396 BITMAPOBJ * bmp )
398 HRGN hrgn;
399 HBITMAP prevHandle = dc->w.hBitmap;
401 if (!(dc->w.flags & DC_MEMORY)) return 0;
402 hrgn = CreateRectRgn( 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
403 if (!hrgn) return 0;
405 DeleteObject( dc->w.hVisRgn );
406 dc->w.hVisRgn = hrgn;
407 dc->u.x.drawable = bmp->pixmap;
408 dc->w.hBitmap = hbitmap;
410 /* Change GC depth if needed */
412 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
414 XFreeGC( display, dc->u.x.gc );
415 dc->u.x.gc = XCreateGC( display, dc->u.x.drawable, 0, NULL );
416 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
417 DC_InitDC( dc );
419 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
420 return prevHandle;
423 /***********************************************************************
424 * CreateDiscardableBitmap (GDI.156) (GDI32.38)
426 HBITMAP16 CreateDiscardableBitmap( HDC32 hdc, INT32 width, INT32 height )
428 dprintf_bitmap(stddeb,"CreateDiscardableBitmap(%04x, %d, %d); "
429 "// call CreateCompatibleBitmap() for now!\n",
430 hdc, width, height);
431 return CreateCompatibleBitmap(hdc, width, height);
435 /***********************************************************************
436 * GetBitmapDimensionEx16 (GDI.468)
438 BOOL16 GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
440 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
441 if (!bmp) return FALSE;
442 *size = bmp->size;
443 return TRUE;
447 /***********************************************************************
448 * GetBitmapDimensionEx32 (GDI32.144)
450 BOOL32 GetBitmapDimensionEx32( HBITMAP32 hbitmap, LPSIZE32 size )
452 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
453 if (!bmp) return FALSE;
454 size->cx = (INT32)bmp->size.cx;
455 size->cy = (INT32)bmp->size.cy;
456 return TRUE;
460 /***********************************************************************
461 * GetBitmapDimension (GDI.162)
463 DWORD GetBitmapDimension( HBITMAP16 hbitmap )
465 SIZE16 size;
466 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
467 return MAKELONG( size.cx, size.cy );
471 /***********************************************************************
472 * SetBitmapDimensionEx16 (GDI.478)
474 BOOL16 SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
475 LPSIZE16 prevSize )
477 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
478 if (!bmp) return FALSE;
479 if (prevSize) *prevSize = bmp->size;
480 bmp->size.cx = x;
481 bmp->size.cy = y;
482 return TRUE;
486 /***********************************************************************
487 * SetBitmapDimensionEx32 (GDI32.304)
489 BOOL32 SetBitmapDimensionEx32( HBITMAP32 hbitmap, INT32 x, INT32 y,
490 LPSIZE32 prevSize )
492 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
493 if (!bmp) return FALSE;
494 if (prevSize) CONV_SIZE16TO32( &bmp->size, prevSize );
495 bmp->size.cx = (INT16)x;
496 bmp->size.cy = (INT16)y;
497 return TRUE;
501 /***********************************************************************
502 * SetBitmapDimension (GDI.163)
504 DWORD SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
506 SIZE16 size;
507 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
508 return MAKELONG( size.cx, size.cy );