Added first version of the Perl regression testing framework.
[wine/multimedia.git] / objects / bitmap.c
blobf6734f286fd8e4c0baa0dc5bb8721e8f5fdafb96
1 /*
2 * GDI bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 * 1998 Huw D M Davies
6 */
8 #include <stdlib.h>
9 #include <string.h>
11 #include "wine/winbase16.h"
12 #include "gdi.h"
13 #include "bitmap.h"
14 #include "heap.h"
15 #include "debugtools.h"
16 #include "wine/winuser16.h"
18 DEFAULT_DEBUG_CHANNEL(bitmap);
20 BITMAP_DRIVER *BITMAP_Driver = NULL;
23 /***********************************************************************
24 * BITMAP_GetWidthBytes
26 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
27 * data.
29 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
31 switch(bpp)
33 case 1:
34 return 2 * ((bmWidth+15) >> 4);
36 case 24:
37 bmWidth *= 3; /* fall through */
38 case 8:
39 return bmWidth + (bmWidth & 1);
41 case 32:
42 return bmWidth * 4;
44 case 16:
45 case 15:
46 return bmWidth * 2;
48 case 4:
49 return 2 * ((bmWidth+3) >> 2);
51 default:
52 WARN("Unknown depth %d, please report.\n", bpp );
54 return -1;
57 /***********************************************************************
58 * CreateUserBitmap16 (GDI.407)
60 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
61 UINT16 bpp, LPCVOID bits )
63 return CreateBitmap16( width, height, planes, bpp, bits );
66 /***********************************************************************
67 * CreateUserDiscardableBitmap16 (GDI.409)
69 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy,
70 INT16 width, INT16 height )
72 HDC hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
73 HBITMAP16 ret = CreateCompatibleBitmap16( hdc, width, height );
74 DeleteDC( hdc );
75 return ret;
79 /***********************************************************************
80 * CreateBitmap16 (GDI.48)
81 * CreateBitmap16 (DISPLAY.48)
83 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
84 UINT16 bpp, LPCVOID bits )
86 return CreateBitmap( width, height, planes, bpp, bits );
90 /******************************************************************************
91 * CreateBitmap [GDI32.@] Creates a bitmap with the specified info
93 * PARAMS
94 * width [I] bitmap width
95 * height [I] bitmap height
96 * planes [I] Number of color planes
97 * bpp [I] Number of bits to identify a color
98 * bits [I] Pointer to array containing color data
100 * RETURNS
101 * Success: Handle to bitmap
102 * Failure: 0
104 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
105 UINT bpp, LPCVOID bits )
107 BITMAPOBJ *bmp;
108 HBITMAP hbitmap;
110 planes = (BYTE)planes;
111 bpp = (BYTE)bpp;
114 /* Check parameters */
115 if (!height || !width) return 0;
116 if (planes != 1) {
117 FIXME("planes = %d\n", planes);
118 return 0;
120 if (height < 0) height = -height;
121 if (width < 0) width = -width;
123 /* Create the BITMAPOBJ */
124 if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC, &hbitmap ))) return 0;
126 TRACE("%dx%d, %d colors returning %08x\n", width, height,
127 1 << (planes*bpp), hbitmap);
129 bmp->size.cx = 0;
130 bmp->size.cy = 0;
131 bmp->bitmap.bmType = 0;
132 bmp->bitmap.bmWidth = width;
133 bmp->bitmap.bmHeight = height;
134 bmp->bitmap.bmPlanes = planes;
135 bmp->bitmap.bmBitsPixel = bpp;
136 bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
137 bmp->bitmap.bmBits = NULL;
139 bmp->funcs = NULL;
140 bmp->physBitmap = NULL;
141 bmp->dib = NULL;
143 if (bits) /* Set bitmap bits */
144 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
145 bits );
146 GDI_ReleaseObj( hbitmap );
147 return hbitmap;
151 /***********************************************************************
152 * CreateCompatibleBitmap16 (GDI.51)
154 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
156 return CreateCompatibleBitmap( hdc, width, height );
160 /******************************************************************************
161 * CreateCompatibleBitmap [GDI32.@] Creates a bitmap compatible with the DC
163 * PARAMS
164 * hdc [I] Handle to device context
165 * width [I] Width of bitmap
166 * height [I] Height of bitmap
168 * RETURNS
169 * Success: Handle to bitmap
170 * Failure: 0
172 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
174 HBITMAP hbmpRet = 0;
175 DC *dc;
177 TRACE("(%04x,%d,%d) = \n", hdc, width, height );
178 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
179 if ((width >= 0x10000) || (height >= 0x10000)) {
180 FIXME("got bad width %d or height %d, please look for reason\n",
181 width, height );
182 } else {
183 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
184 if (!width || !height)
185 hbmpRet = CreateBitmap( 1, 1, 1, 1, NULL );
186 else
187 hbmpRet = CreateBitmap( width, height, 1, dc->bitsPerPixel, NULL );
188 if(dc->funcs->pCreateBitmap)
189 dc->funcs->pCreateBitmap( hbmpRet );
191 TRACE("\t\t%04x\n", hbmpRet);
192 GDI_ReleaseObj(hdc);
193 return hbmpRet;
197 /***********************************************************************
198 * CreateBitmapIndirect16 (GDI.49)
200 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
202 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
203 bmp->bmBitsPixel, MapSL( bmp->bmBits ) );
207 /******************************************************************************
208 * CreateBitmapIndirect [GDI32.@] Creates a bitmap with the specifies info
210 * RETURNS
211 * Success: Handle to bitmap
212 * Failure: NULL
214 HBITMAP WINAPI CreateBitmapIndirect(
215 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
217 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
218 bmp->bmBitsPixel, bmp->bmBits );
222 /***********************************************************************
223 * GetBitmapBits16 (GDI.74)
225 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
227 return GetBitmapBits( hbitmap, count, buffer );
231 /***********************************************************************
232 * GetBitmapBits [GDI32.@] Copies bitmap bits of bitmap to buffer
234 * RETURNS
235 * Success: Number of bytes copied
236 * Failure: 0
238 LONG WINAPI GetBitmapBits(
239 HBITMAP hbitmap, /* [in] Handle to bitmap */
240 LONG count, /* [in] Number of bytes to copy */
241 LPVOID bits) /* [out] Pointer to buffer to receive bits */
243 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
244 LONG height, ret;
246 if (!bmp) return 0;
248 /* If the bits vector is null, the function should return the read size */
249 if(bits == NULL)
251 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
252 goto done;
255 if (count < 0) {
256 WARN("(%ld): Negative number of bytes passed???\n", count );
257 count = -count;
260 /* Only get entire lines */
261 height = count / bmp->bitmap.bmWidthBytes;
262 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
263 count = height * bmp->bitmap.bmWidthBytes;
264 if (count == 0)
266 WARN("Less than one entire line requested\n");
267 ret = 0;
268 goto done;
272 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
273 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
274 1 << bmp->bitmap.bmBitsPixel, height );
276 if(bmp->funcs) {
278 TRACE("Calling device specific BitmapBits\n");
279 if(bmp->funcs->pBitmapBits)
280 ret = bmp->funcs->pBitmapBits(hbitmap, bits, count, DDB_GET);
281 else {
282 ERR("BitmapBits == NULL??\n");
283 ret = 0;
286 } else {
288 if(!bmp->bitmap.bmBits) {
289 WARN("Bitmap is empty\n");
290 ret = 0;
291 } else {
292 memcpy(bits, bmp->bitmap.bmBits, count);
293 ret = count;
297 done:
298 GDI_ReleaseObj( hbitmap );
299 return ret;
303 /***********************************************************************
304 * SetBitmapBits16 (GDI.106)
306 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
308 return SetBitmapBits( hbitmap, count, buffer );
312 /******************************************************************************
313 * SetBitmapBits [GDI32.@] Sets bits of color data for a bitmap
315 * RETURNS
316 * Success: Number of bytes used in setting the bitmap bits
317 * Failure: 0
319 LONG WINAPI SetBitmapBits(
320 HBITMAP hbitmap, /* [in] Handle to bitmap */
321 LONG count, /* [in] Number of bytes in bitmap array */
322 LPCVOID bits) /* [in] Address of array with bitmap bits */
324 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
325 LONG height, ret;
327 if ((!bmp) || (!bits))
328 return 0;
330 if (count < 0) {
331 WARN("(%ld): Negative number of bytes passed???\n", count );
332 count = -count;
335 /* Only get entire lines */
336 height = count / bmp->bitmap.bmWidthBytes;
337 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
338 count = height * bmp->bitmap.bmWidthBytes;
340 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
341 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
342 1 << bmp->bitmap.bmBitsPixel, height );
344 if(bmp->funcs) {
346 TRACE("Calling device specific BitmapBits\n");
347 if(bmp->funcs->pBitmapBits)
348 ret = bmp->funcs->pBitmapBits(hbitmap, (void *) bits, count, DDB_SET);
349 else {
350 ERR("BitmapBits == NULL??\n");
351 ret = 0;
354 } else {
356 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
357 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
358 if(!bmp->bitmap.bmBits) {
359 WARN("Unable to allocate bit buffer\n");
360 ret = 0;
361 } else {
362 memcpy(bmp->bitmap.bmBits, bits, count);
363 ret = count;
367 GDI_ReleaseObj( hbitmap );
368 return ret;
371 /**********************************************************************
372 * BITMAP_CopyBitmap
375 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
377 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
378 HBITMAP res = 0;
379 BITMAP bm;
381 if(!bmp) return 0;
383 bm = bmp->bitmap;
384 bm.bmBits = NULL;
385 res = CreateBitmapIndirect(&bm);
387 if(res) {
388 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
389 bm.bmHeight );
390 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
391 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
392 HeapFree( GetProcessHeap(), 0, buf );
395 GDI_ReleaseObj( hbitmap );
396 return res;
399 /***********************************************************************
400 * BITMAP_DeleteObject
402 BOOL BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
404 if (bmp->funcs && bmp->funcs->pDeleteObject)
405 bmp->funcs->pDeleteObject( hbitmap );
407 if( bmp->bitmap.bmBits )
408 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
410 DIB_DeleteDIBSection( bmp );
412 return GDI_FreeObject( hbitmap, bmp );
416 /***********************************************************************
417 * BITMAP_GetObject16
419 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
421 if (bmp->dib)
423 if ( count <= sizeof(BITMAP16) )
425 BITMAP *bmp32 = &bmp->dib->dsBm;
426 BITMAP16 bmp16;
427 bmp16.bmType = bmp32->bmType;
428 bmp16.bmWidth = bmp32->bmWidth;
429 bmp16.bmHeight = bmp32->bmHeight;
430 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
431 bmp16.bmPlanes = bmp32->bmPlanes;
432 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
433 bmp16.bmBits = (SEGPTR)0;
434 memcpy( buffer, &bmp16, count );
435 return count;
437 else
439 FIXME("not implemented for DIBs: count %d\n", count);
440 return 0;
443 else
445 BITMAP16 bmp16;
446 bmp16.bmType = bmp->bitmap.bmType;
447 bmp16.bmWidth = bmp->bitmap.bmWidth;
448 bmp16.bmHeight = bmp->bitmap.bmHeight;
449 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
450 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
451 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
452 bmp16.bmBits = (SEGPTR)0;
453 if (count > sizeof(bmp16)) count = sizeof(bmp16);
454 memcpy( buffer, &bmp16, count );
455 return count;
460 /***********************************************************************
461 * BITMAP_GetObject
463 INT BITMAP_GetObject( BITMAPOBJ * bmp, INT count, LPVOID buffer )
465 if (bmp->dib)
467 if (count < sizeof(DIBSECTION))
469 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
471 else
473 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
476 memcpy( buffer, bmp->dib, count );
477 return count;
479 else
481 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
482 memcpy( buffer, &bmp->bitmap, count );
483 return count;
488 /***********************************************************************
489 * CreateDiscardableBitmap16 (GDI.156)
491 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
492 INT16 height )
494 return CreateCompatibleBitmap16( hdc, width, height );
498 /******************************************************************************
499 * CreateDiscardableBitmap [GDI32.@] Creates a discardable bitmap
501 * RETURNS
502 * Success: Handle to bitmap
503 * Failure: NULL
505 HBITMAP WINAPI CreateDiscardableBitmap(
506 HDC hdc, /* [in] Handle to device context */
507 INT width, /* [in] Bitmap width */
508 INT height) /* [in] Bitmap height */
510 return CreateCompatibleBitmap( hdc, width, height );
514 /***********************************************************************
515 * GetBitmapDimensionEx16 (GDI.468)
517 * NOTES
518 * Can this call GetBitmapDimensionEx?
520 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
522 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
523 if (!bmp) return FALSE;
524 size->cx = bmp->size.cx;
525 size->cy = bmp->size.cy;
526 GDI_ReleaseObj( hbitmap );
527 return TRUE;
531 /******************************************************************************
532 * GetBitmapDimensionEx [GDI32.@] Retrieves dimensions of a bitmap
534 * RETURNS
535 * Success: TRUE
536 * Failure: FALSE
538 BOOL WINAPI GetBitmapDimensionEx(
539 HBITMAP hbitmap, /* [in] Handle to bitmap */
540 LPSIZE size) /* [out] Address of struct receiving dimensions */
542 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
543 if (!bmp) return FALSE;
544 *size = bmp->size;
545 GDI_ReleaseObj( hbitmap );
546 return TRUE;
550 /***********************************************************************
551 * GetBitmapDimension (GDI.162)
553 DWORD WINAPI GetBitmapDimension16( HBITMAP16 hbitmap )
555 SIZE16 size;
556 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
557 return MAKELONG( size.cx, size.cy );
561 /***********************************************************************
562 * SetBitmapDimensionEx16 (GDI.478)
564 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
565 LPSIZE16 prevSize )
567 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
568 if (!bmp) return FALSE;
569 if (prevSize)
571 prevSize->cx = bmp->size.cx;
572 prevSize->cy = bmp->size.cy;
574 bmp->size.cx = x;
575 bmp->size.cy = y;
576 GDI_ReleaseObj( hbitmap );
577 return TRUE;
581 /******************************************************************************
582 * SetBitmapDimensionEx [GDI32.@] Assignes dimensions to a bitmap
584 * RETURNS
585 * Success: TRUE
586 * Failure: FALSE
588 BOOL WINAPI SetBitmapDimensionEx(
589 HBITMAP hbitmap, /* [in] Handle to bitmap */
590 INT x, /* [in] Bitmap width */
591 INT y, /* [in] Bitmap height */
592 LPSIZE prevSize) /* [out] Address of structure for orig dims */
594 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
595 if (!bmp) return FALSE;
596 if (prevSize) *prevSize = bmp->size;
597 bmp->size.cx = x;
598 bmp->size.cy = y;
599 GDI_ReleaseObj( hbitmap );
600 return TRUE;
604 /***********************************************************************
605 * SetBitmapDimension (GDI.163)
607 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
609 SIZE16 size;
610 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
611 return MAKELONG( size.cx, size.cy );