Use libwine and libwine_unicode directly from their build directory
[wine/multimedia.git] / objects / bitmap.c
blob77278b980902b1ebed12620b08ae73a76de6a71e
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 "global.h"
16 #include "debugtools.h"
17 #include "wine/winuser16.h"
19 DEFAULT_DEBUG_CHANNEL(bitmap);
21 BITMAP_DRIVER *BITMAP_Driver = NULL;
24 /***********************************************************************
25 * BITMAP_GetWidthBytes
27 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
28 * data.
30 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
32 switch(bpp)
34 case 1:
35 return 2 * ((bmWidth+15) >> 4);
37 case 24:
38 bmWidth *= 3; /* fall through */
39 case 8:
40 return bmWidth + (bmWidth & 1);
42 case 32:
43 return bmWidth * 4;
45 case 16:
46 case 15:
47 return bmWidth * 2;
49 case 4:
50 return 2 * ((bmWidth+3) >> 2);
52 default:
53 WARN("Unknown depth %d, please report.\n", bpp );
55 return -1;
58 /***********************************************************************
59 * CreateUserBitmap16 (GDI.407)
61 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
62 UINT16 bpp, LPCVOID bits )
64 return CreateBitmap16( width, height, planes, bpp, bits );
67 /***********************************************************************
68 * CreateUserDiscardableBitmap16 (GDI.409)
70 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy,
71 INT16 width, INT16 height )
73 HDC hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
74 HBITMAP16 ret = CreateCompatibleBitmap16( hdc, width, height );
75 DeleteDC( hdc );
76 return ret;
80 /***********************************************************************
81 * CreateBitmap16 (GDI.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.25] 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.30] 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, PTR_SEG_TO_LIN( bmp->bmBits ) );
207 /******************************************************************************
208 * CreateBitmapIndirect [GDI32.26] 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.143] 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 then 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.303] 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.38] 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 CONV_SIZE32TO16( &bmp->size, size );
525 GDI_ReleaseObj( hbitmap );
526 return TRUE;
530 /******************************************************************************
531 * GetBitmapDimensionEx [GDI32.144] Retrieves dimensions of a bitmap
533 * RETURNS
534 * Success: TRUE
535 * Failure: FALSE
537 BOOL WINAPI GetBitmapDimensionEx(
538 HBITMAP hbitmap, /* [in] Handle to bitmap */
539 LPSIZE size) /* [out] Address of struct receiving dimensions */
541 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
542 if (!bmp) return FALSE;
543 *size = bmp->size;
544 GDI_ReleaseObj( hbitmap );
545 return TRUE;
549 /***********************************************************************
550 * GetBitmapDimension (GDI.162)
552 DWORD WINAPI GetBitmapDimension16( HBITMAP16 hbitmap )
554 SIZE16 size;
555 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
556 return MAKELONG( size.cx, size.cy );
560 /***********************************************************************
561 * SetBitmapDimensionEx16 (GDI.478)
563 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
564 LPSIZE16 prevSize )
566 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
567 if (!bmp) return FALSE;
568 if (prevSize) CONV_SIZE32TO16( &bmp->size, prevSize );
569 bmp->size.cx = x;
570 bmp->size.cy = y;
571 GDI_ReleaseObj( hbitmap );
572 return TRUE;
576 /******************************************************************************
577 * SetBitmapDimensionEx [GDI32.304] Assignes dimensions to a bitmap
579 * RETURNS
580 * Success: TRUE
581 * Failure: FALSE
583 BOOL WINAPI SetBitmapDimensionEx(
584 HBITMAP hbitmap, /* [in] Handle to bitmap */
585 INT x, /* [in] Bitmap width */
586 INT y, /* [in] Bitmap height */
587 LPSIZE prevSize) /* [out] Address of structure for orig dims */
589 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
590 if (!bmp) return FALSE;
591 if (prevSize) *prevSize = bmp->size;
592 bmp->size.cx = x;
593 bmp->size.cy = y;
594 GDI_ReleaseObj( hbitmap );
595 return TRUE;
599 /***********************************************************************
600 * SetBitmapDimension (GDI.163)
602 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
604 SIZE16 size;
605 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
606 return MAKELONG( size.cx, size.cy );