Release 960611
[wine/multimedia.git] / objects / bitmap.c
blobf54cdc7a287a7605142ff7d0d5bfd36debfa6e96
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 "stddebug.h"
16 #include "debug.h"
18 /* GCs used for B&W and color bitmap operations */
19 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
21 extern void CLIPPING_UpdateGCRegion( DC * dc ); /* objects/clipping.c */
23 /***********************************************************************
24 * BITMAP_Init
26 BOOL BITMAP_Init(void)
28 Pixmap tmpPixmap;
30 /* Create the necessary GCs */
32 if ((tmpPixmap = XCreatePixmap( display, rootWindow, 1, 1, 1 )))
34 BITMAP_monoGC = XCreateGC( display, tmpPixmap, 0, NULL );
35 XSetGraphicsExposures( display, BITMAP_monoGC, False );
36 XFreePixmap( display, tmpPixmap );
39 if (screenDepth != 1)
41 if ((tmpPixmap = XCreatePixmap(display, rootWindow, 1,1,screenDepth)))
43 BITMAP_colorGC = XCreateGC( display, tmpPixmap, 0, NULL );
44 XSetGraphicsExposures( display, BITMAP_colorGC, False );
45 XFreePixmap( display, tmpPixmap );
48 return TRUE;
52 /***********************************************************************
53 * BITMAP_BmpToImage
55 * Create an XImage pointing to the bitmap data.
57 static XImage *BITMAP_BmpToImage( BITMAP16 * bmp, LPVOID bmpData )
59 extern void _XInitImageFuncPtrs( XImage* );
60 XImage * image;
62 image = XCreateImage( display, DefaultVisualOfScreen(screen),
63 bmp->bmBitsPixel, ZPixmap, 0, bmpData,
64 bmp->bmWidth, bmp->bmHeight, 16, bmp->bmWidthBytes );
65 if (!image) return 0;
66 image->byte_order = MSBFirst;
67 image->bitmap_bit_order = MSBFirst;
68 image->bitmap_unit = 16;
69 _XInitImageFuncPtrs(image);
70 return image;
74 /***********************************************************************
75 * CreateBitmap (GDI.48) (GDI32.25)
77 HBITMAP16 CreateBitmap( INT32 width, INT32 height, UINT32 planes,
78 UINT32 bpp, LPCVOID bits )
80 BITMAPOBJ * bmpObjPtr;
81 HBITMAP16 hbitmap;
83 dprintf_gdi( stddeb, "CreateBitmap: %dx%d, %d colors\n",
84 width, height, 1 << (planes*bpp) );
86 /* Check parameters */
87 if (!height || !width || planes != 1) return 0;
88 if ((bpp != 1) && (bpp != screenDepth)) return 0;
89 if (height < 0) height = -height;
90 if (width < 0) width = -width;
92 /* Create the BITMAPOBJ */
93 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
94 if (!hbitmap) return 0;
95 bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LIN_ADDR( hbitmap );
97 bmpObjPtr->size.cx = 0;
98 bmpObjPtr->size.cy = 0;
99 bmpObjPtr->bitmap.bmType = 0;
100 bmpObjPtr->bitmap.bmWidth = (INT16)width;
101 bmpObjPtr->bitmap.bmHeight = (INT16)height;
102 bmpObjPtr->bitmap.bmPlanes = (BYTE)planes;
103 bmpObjPtr->bitmap.bmBitsPixel = (BYTE)bpp;
104 bmpObjPtr->bitmap.bmWidthBytes = (INT16)BITMAP_WIDTH_BYTES( width, bpp );
105 bmpObjPtr->bitmap.bmBits = NULL;
107 /* Create the pixmap */
108 bmpObjPtr->pixmap = XCreatePixmap(display, rootWindow, width, height, bpp);
109 if (!bmpObjPtr->pixmap)
111 GDI_HEAP_FREE( hbitmap );
112 hbitmap = 0;
114 else if (bits) /* Set bitmap bits */
115 SetBitmapBits( hbitmap, height * bmpObjPtr->bitmap.bmWidthBytes, bits);
116 return hbitmap;
120 /***********************************************************************
121 * CreateCompatibleBitmap (GDI.51) (GDI32.30)
123 HBITMAP16 CreateCompatibleBitmap( HDC32 hdc, INT32 width, INT32 height )
125 HBITMAP hbmpRet = 0;
126 DC *dc;
128 dprintf_gdi( stddeb, "CreateCompatibleBitmap(%04x,%d,%d) = \n",
129 hdc, width, height );
130 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
132 hbmpRet = CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
133 dprintf_gdi(stddeb,"\t\t%04x\n", hbmpRet);
134 return hbmpRet;
138 /***********************************************************************
139 * CreateBitmapIndirect16 (GDI.49)
141 HBITMAP16 CreateBitmapIndirect16( const BITMAP16 * bmp )
143 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
144 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
148 /***********************************************************************
149 * CreateBitmapIndirect32 (GDI32.26)
151 HBITMAP32 CreateBitmapIndirect32( const BITMAP32 * bmp )
153 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
154 bmp->bmBitsPixel, bmp->bmBits );
158 /***********************************************************************
159 * GetBitmapBits (GDI.74) (GDI32.143)
161 LONG GetBitmapBits( HBITMAP32 hbitmap, LONG count, LPVOID buffer )
163 BITMAPOBJ * bmp;
164 LONG height;
165 XImage * image;
167 /* KLUDGE! */
168 if (count < 0) {
169 fprintf(stderr, "Negative number of bytes (%ld) passed to GetBitmapBits???\n", count );
170 count = -count;
172 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
173 if (!bmp) return 0;
175 /* Only get entire lines */
176 height = count / bmp->bitmap.bmWidthBytes;
177 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
178 dprintf_bitmap(stddeb, "GetBitmapBits: %dx%d %d colors %p fetched height: %ld\n",
179 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
180 1 << bmp->bitmap.bmBitsPixel, buffer, height );
181 if (!height) return 0;
183 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
184 CallTo32_LargeStack( (int(*)())XGetSubImage, 11,
185 display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
186 height, AllPlanes, ZPixmap, image, 0, 0 );
187 image->data = NULL;
188 XDestroyImage( image );
189 return height * bmp->bitmap.bmWidthBytes;
193 /***********************************************************************
194 * SetBitmapBits (GDI.106) (GDI32.303)
196 LONG SetBitmapBits( HBITMAP32 hbitmap, LONG count, LPCVOID buffer )
198 BITMAPOBJ * bmp;
199 LONG height;
200 XImage * image;
202 /* KLUDGE! */
203 if (count < 0) {
204 fprintf(stderr, "Negative number of bytes (%ld) passed to SetBitmapBits???\n", count );
205 count = -count;
207 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
208 if (!bmp) return 0;
210 dprintf_bitmap(stddeb, "SetBitmapBits: %dx%d %d colors %p\n",
211 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
212 1 << bmp->bitmap.bmBitsPixel, buffer );
214 /* Only set entire lines */
215 height = count / bmp->bitmap.bmWidthBytes;
216 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
217 if (!height) return 0;
219 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, (LPVOID)buffer ))) return 0;
220 CallTo32_LargeStack( XPutImage, 10,
221 display, bmp->pixmap, BITMAP_GC(bmp), image, 0, 0,
222 0, 0, bmp->bitmap.bmWidth, height );
223 image->data = NULL;
224 XDestroyImage( image );
225 return height * bmp->bitmap.bmWidthBytes;
229 /**********************************************************************
230 * LoadBitmap (USER.175)
232 HBITMAP LoadBitmap( HANDLE instance, SEGPTR name )
234 HBITMAP hbitmap = 0;
235 HDC hdc;
236 HRSRC hRsrc;
237 HGLOBAL handle;
238 BITMAPINFO *info;
240 if (HIWORD(name))
242 char *str = (char *)PTR_SEG_TO_LIN( name );
243 dprintf_bitmap( stddeb, "LoadBitmap(%04x,'%s')\n", instance, str );
244 if (str[0] == '#') name = (SEGPTR)(DWORD)(WORD)atoi( str + 1 );
246 else
247 dprintf_bitmap( stddeb, "LoadBitmap(%04x,%04x)\n",
248 instance, LOWORD(name) );
250 if (!instance) /* OEM bitmap */
252 if (HIWORD((int)name)) return 0;
253 return OBM_LoadBitmap( LOWORD((int)name) );
256 if (!(hRsrc = FindResource( instance, name, RT_BITMAP ))) return 0;
257 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
259 info = (BITMAPINFO *)LockResource( handle );
260 if ((hdc = GetDC(0)) != 0)
262 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
263 hbitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
264 bits, info, DIB_RGB_COLORS );
265 ReleaseDC( 0, hdc );
267 FreeResource( handle );
268 return hbitmap;
272 /***********************************************************************
273 * BITMAP_DeleteObject
275 BOOL BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bitmap )
277 XFreePixmap( display, bitmap->pixmap );
278 return GDI_FreeObject( hbitmap );
282 /***********************************************************************
283 * BITMAP_GetObject16
285 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
287 if (count > sizeof(bmp->bitmap)) count = sizeof(bmp->bitmap);
288 memcpy( buffer, &bmp->bitmap, count );
289 return count;
293 /***********************************************************************
294 * BITMAP_GetObject32
296 INT32 BITMAP_GetObject32( BITMAPOBJ * bmp, INT32 count, LPVOID buffer )
298 BITMAP32 bmp32;
299 bmp32.bmType = bmp->bitmap.bmType;
300 bmp32.bmWidth = bmp->bitmap.bmWidth;
301 bmp32.bmHeight = bmp->bitmap.bmHeight;
302 bmp32.bmWidthBytes = bmp->bitmap.bmWidthBytes;
303 bmp32.bmPlanes = bmp->bitmap.bmPlanes;
304 bmp32.bmBitsPixel = bmp->bitmap.bmBitsPixel;
305 bmp32.bmBits = NULL;
306 if (count > sizeof(bmp32)) count = sizeof(bmp32);
307 memcpy( buffer, &bmp32, count );
308 return count;
312 /***********************************************************************
313 * BITMAP_SelectObject
315 HBITMAP BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
316 BITMAPOBJ * bmp )
318 HRGN hrgn;
319 HBITMAP prevHandle = dc->w.hBitmap;
321 if (!(dc->w.flags & DC_MEMORY)) return 0;
322 hrgn = CreateRectRgn( 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
323 if (!hrgn) return 0;
325 DeleteObject( dc->w.hVisRgn );
326 dc->w.hVisRgn = hrgn;
327 dc->u.x.drawable = bmp->pixmap;
328 dc->w.hBitmap = hbitmap;
330 /* Change GC depth if needed */
332 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
334 XFreeGC( display, dc->u.x.gc );
335 dc->u.x.gc = XCreateGC( display, dc->u.x.drawable, 0, NULL );
336 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
337 DC_InitDC( dc );
339 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
340 return prevHandle;
343 /***********************************************************************
344 * CreateDiscardableBitmap (GDI.156) (GDI32.38)
346 HBITMAP16 CreateDiscardableBitmap( HDC32 hdc, INT32 width, INT32 height )
348 dprintf_bitmap(stddeb,"CreateDiscardableBitmap(%04x, %d, %d); "
349 "// call CreateCompatibleBitmap() for now!\n",
350 hdc, width, height);
351 return CreateCompatibleBitmap(hdc, width, height);
355 /***********************************************************************
356 * GetBitmapDimensionEx16 (GDI.468)
358 BOOL16 GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
360 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
361 if (!bmp) return FALSE;
362 *size = bmp->size;
363 return TRUE;
367 /***********************************************************************
368 * GetBitmapDimensionEx32 (GDI32.144)
370 BOOL32 GetBitmapDimensionEx32( HBITMAP32 hbitmap, LPSIZE32 size )
372 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
373 if (!bmp) return FALSE;
374 size->cx = (INT32)bmp->size.cx;
375 size->cy = (INT32)bmp->size.cy;
376 return TRUE;
380 /***********************************************************************
381 * GetBitmapDimension (GDI.162)
383 DWORD GetBitmapDimension( HBITMAP16 hbitmap )
385 SIZE16 size;
386 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
387 return MAKELONG( size.cx, size.cy );
391 /***********************************************************************
392 * SetBitmapDimensionEx16 (GDI.478)
394 BOOL16 SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
395 LPSIZE16 prevSize )
397 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
398 if (!bmp) return FALSE;
399 if (prevSize) *prevSize = bmp->size;
400 bmp->size.cx = x;
401 bmp->size.cy = y;
402 return TRUE;
406 /***********************************************************************
407 * SetBitmapDimensionEx32 (GDI32.304)
409 BOOL32 SetBitmapDimensionEx32( HBITMAP32 hbitmap, INT32 x, INT32 y,
410 LPSIZE32 prevSize )
412 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
413 if (!bmp) return FALSE;
414 if (prevSize) CONV_SIZE16TO32( &bmp->size, prevSize );
415 bmp->size.cx = (INT16)x;
416 bmp->size.cy = (INT16)y;
417 return TRUE;
421 /***********************************************************************
422 * SetBitmapDimension (GDI.163)
424 DWORD SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
426 SIZE16 size;
427 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
428 return MAKELONG( size.cx, size.cy );