Removed some unnecessary #includes and dll dependencies.
[wine/multimedia.git] / objects / bitmap.c
blobcc1aa5c26eeb3de9c630f24aad619ce7f434bc4e
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 "dc.h"
14 #include "bitmap.h"
15 #include "heap.h"
16 #include "global.h"
17 #include "debugtools.h"
18 #include "wine/winuser16.h"
20 DEFAULT_DEBUG_CHANNEL(bitmap);
22 BITMAP_DRIVER *BITMAP_Driver = NULL;
25 /***********************************************************************
26 * BITMAP_GetWidthBytes
28 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
29 * data.
31 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
33 switch(bpp)
35 case 1:
36 return 2 * ((bmWidth+15) >> 4);
38 case 24:
39 bmWidth *= 3; /* fall through */
40 case 8:
41 return bmWidth + (bmWidth & 1);
43 case 32:
44 return bmWidth * 4;
46 case 16:
47 case 15:
48 return bmWidth * 2;
50 case 4:
51 return 2 * ((bmWidth+3) >> 2);
53 default:
54 WARN("Unknown depth %d, please report.\n", bpp );
56 return -1;
59 /***********************************************************************
60 * CreateUserBitmap16 (GDI.407)
62 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
63 UINT16 bpp, LPCVOID bits )
65 return CreateBitmap16( width, height, planes, bpp, bits );
68 /***********************************************************************
69 * CreateUserDiscardableBitmap16 (GDI.409)
71 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy,
72 INT16 width, INT16 height )
74 HDC hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
75 HBITMAP16 ret = CreateCompatibleBitmap16( hdc, width, height );
76 DeleteDC( hdc );
77 return ret;
81 /***********************************************************************
82 * CreateBitmap16 (GDI.48)
84 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
85 UINT16 bpp, LPCVOID bits )
87 return CreateBitmap( width, height, planes, bpp, bits );
91 /******************************************************************************
92 * CreateBitmap [GDI32.25] Creates a bitmap with the specified info
94 * PARAMS
95 * width [I] bitmap width
96 * height [I] bitmap height
97 * planes [I] Number of color planes
98 * bpp [I] Number of bits to identify a color
99 * bits [I] Pointer to array containing color data
101 * RETURNS
102 * Success: Handle to bitmap
103 * Failure: 0
105 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
106 UINT bpp, LPCVOID bits )
108 BITMAPOBJ *bmp;
109 HBITMAP hbitmap;
111 planes = (BYTE)planes;
112 bpp = (BYTE)bpp;
115 /* Check parameters */
116 if (!height || !width) return 0;
117 if (planes != 1) {
118 FIXME("planes = %d\n", planes);
119 return 0;
121 if (height < 0) height = -height;
122 if (width < 0) width = -width;
124 /* Create the BITMAPOBJ */
125 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
126 if (!hbitmap) return 0;
128 TRACE("%dx%d, %d colors returning %08x\n", width, height,
129 1 << (planes*bpp), hbitmap);
131 bmp = (BITMAPOBJ *) GDI_HEAP_LOCK( hbitmap );
133 bmp->size.cx = 0;
134 bmp->size.cy = 0;
135 bmp->bitmap.bmType = 0;
136 bmp->bitmap.bmWidth = width;
137 bmp->bitmap.bmHeight = height;
138 bmp->bitmap.bmPlanes = planes;
139 bmp->bitmap.bmBitsPixel = bpp;
140 bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
141 bmp->bitmap.bmBits = NULL;
143 bmp->funcs = NULL;
144 bmp->physBitmap = NULL;
145 bmp->dib = NULL;
147 if (bits) /* Set bitmap bits */
148 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
149 bits );
150 GDI_HEAP_UNLOCK( hbitmap );
151 return hbitmap;
155 /***********************************************************************
156 * CreateCompatibleBitmap16 (GDI.51)
158 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
160 return CreateCompatibleBitmap( hdc, width, height );
164 /******************************************************************************
165 * CreateCompatibleBitmap [GDI32.30] Creates a bitmap compatible with the DC
167 * PARAMS
168 * hdc [I] Handle to device context
169 * width [I] Width of bitmap
170 * height [I] Height of bitmap
172 * RETURNS
173 * Success: Handle to bitmap
174 * Failure: 0
176 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
178 HBITMAP hbmpRet = 0;
179 DC *dc;
181 TRACE("(%04x,%d,%d) = \n", hdc, width, height );
182 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
183 if ((width >= 0x10000) || (height >= 0x10000)) {
184 FIXME("got bad width %d or height %d, please look for reason\n",
185 width, height );
186 } else {
187 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
188 if (!width || !height)
189 hbmpRet = CreateBitmap( 1, 1, 1, 1, NULL );
190 else
191 hbmpRet = CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
192 if(dc->funcs->pCreateBitmap)
193 dc->funcs->pCreateBitmap( hbmpRet );
195 TRACE("\t\t%04x\n", hbmpRet);
196 GDI_HEAP_UNLOCK(hdc);
197 return hbmpRet;
201 /***********************************************************************
202 * CreateBitmapIndirect16 (GDI.49)
204 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
206 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
207 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
211 /******************************************************************************
212 * CreateBitmapIndirect [GDI32.26] Creates a bitmap with the specifies info
214 * RETURNS
215 * Success: Handle to bitmap
216 * Failure: NULL
218 HBITMAP WINAPI CreateBitmapIndirect(
219 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
221 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
222 bmp->bmBitsPixel, bmp->bmBits );
226 /***********************************************************************
227 * GetBitmapBits16 (GDI.74)
229 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
231 return GetBitmapBits( hbitmap, count, buffer );
235 /***********************************************************************
236 * GetBitmapBits [GDI32.143] Copies bitmap bits of bitmap to buffer
238 * RETURNS
239 * Success: Number of bytes copied
240 * Failure: 0
242 LONG WINAPI GetBitmapBits(
243 HBITMAP hbitmap, /* [in] Handle to bitmap */
244 LONG count, /* [in] Number of bytes to copy */
245 LPVOID bits) /* [out] Pointer to buffer to receive bits */
247 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
248 LONG height, ret;
250 if (!bmp) return 0;
252 /* If the bits vector is null, the function should return the read size */
253 if(bits == NULL)
254 return bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
256 if (count < 0) {
257 WARN("(%ld): Negative number of bytes passed???\n", count );
258 count = -count;
261 /* Only get entire lines */
262 height = count / bmp->bitmap.bmWidthBytes;
263 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
264 count = height * bmp->bitmap.bmWidthBytes;
265 if (count == 0)
267 WARN("Less then one entire line requested\n");
268 GDI_HEAP_UNLOCK( hbitmap );
269 return 0;
273 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
274 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
275 1 << bmp->bitmap.bmBitsPixel, height );
277 if(bmp->funcs) {
279 TRACE("Calling device specific BitmapBits\n");
280 if(bmp->funcs->pBitmapBits)
281 ret = bmp->funcs->pBitmapBits(hbitmap, bits, count, DDB_GET);
282 else {
283 ERR("BitmapBits == NULL??\n");
284 ret = 0;
287 } else {
289 if(!bmp->bitmap.bmBits) {
290 WARN("Bitmap is empty\n");
291 ret = 0;
292 } else {
293 memcpy(bits, bmp->bitmap.bmBits, count);
294 ret = count;
299 GDI_HEAP_UNLOCK( hbitmap );
300 return ret;
304 /***********************************************************************
305 * SetBitmapBits16 (GDI.106)
307 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
309 return SetBitmapBits( hbitmap, count, buffer );
313 /******************************************************************************
314 * SetBitmapBits [GDI32.303] Sets bits of color data for a bitmap
316 * RETURNS
317 * Success: Number of bytes used in setting the bitmap bits
318 * Failure: 0
320 LONG WINAPI SetBitmapBits(
321 HBITMAP hbitmap, /* [in] Handle to bitmap */
322 LONG count, /* [in] Number of bytes in bitmap array */
323 LPCVOID bits) /* [in] Address of array with bitmap bits */
325 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
326 LONG height, ret;
328 if ((!bmp) || (!bits))
329 return 0;
331 if (count < 0) {
332 WARN("(%ld): Negative number of bytes passed???\n", count );
333 count = -count;
336 /* Only get entire lines */
337 height = count / bmp->bitmap.bmWidthBytes;
338 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
339 count = height * bmp->bitmap.bmWidthBytes;
341 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
342 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
343 1 << bmp->bitmap.bmBitsPixel, height );
345 if(bmp->funcs) {
347 TRACE("Calling device specific BitmapBits\n");
348 if(bmp->funcs->pBitmapBits)
349 ret = bmp->funcs->pBitmapBits(hbitmap, (void *) bits, count, DDB_SET);
350 else {
351 ERR("BitmapBits == NULL??\n");
352 ret = 0;
355 } else {
357 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
358 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
359 if(!bmp->bitmap.bmBits) {
360 WARN("Unable to allocate bit buffer\n");
361 ret = 0;
362 } else {
363 memcpy(bmp->bitmap.bmBits, bits, count);
364 ret = count;
368 GDI_HEAP_UNLOCK( hbitmap );
369 return ret;
372 /**********************************************************************
373 * BITMAP_CopyBitmap
376 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
378 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
379 HBITMAP res = 0;
380 BITMAP bm;
382 if(!bmp) return 0;
384 bm = bmp->bitmap;
385 bm.bmBits = NULL;
386 res = CreateBitmapIndirect(&bm);
388 if(res) {
389 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
390 bm.bmHeight );
391 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
392 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
393 HeapFree( GetProcessHeap(), 0, buf );
396 GDI_HEAP_UNLOCK( hbitmap );
397 return res;
400 /***********************************************************************
401 * BITMAP_DeleteObject
403 BOOL BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
405 if (bmp->funcs && bmp->funcs->pDeleteObject)
406 bmp->funcs->pDeleteObject( hbitmap );
408 if( bmp->bitmap.bmBits )
409 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
411 DIB_DeleteDIBSection( bmp );
413 return GDI_FreeObject( hbitmap );
417 /***********************************************************************
418 * BITMAP_GetObject16
420 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
422 if (bmp->dib)
424 if ( count <= sizeof(BITMAP16) )
426 BITMAP *bmp32 = &bmp->dib->dsBm;
427 BITMAP16 bmp16;
428 bmp16.bmType = bmp32->bmType;
429 bmp16.bmWidth = bmp32->bmWidth;
430 bmp16.bmHeight = bmp32->bmHeight;
431 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
432 bmp16.bmPlanes = bmp32->bmPlanes;
433 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
434 bmp16.bmBits = (SEGPTR)0;
435 memcpy( buffer, &bmp16, count );
436 return count;
438 else
440 FIXME("not implemented for DIBs: count %d\n", count);
441 return 0;
444 else
446 BITMAP16 bmp16;
447 bmp16.bmType = bmp->bitmap.bmType;
448 bmp16.bmWidth = bmp->bitmap.bmWidth;
449 bmp16.bmHeight = bmp->bitmap.bmHeight;
450 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
451 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
452 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
453 bmp16.bmBits = (SEGPTR)0;
454 if (count > sizeof(bmp16)) count = sizeof(bmp16);
455 memcpy( buffer, &bmp16, count );
456 return count;
461 /***********************************************************************
462 * BITMAP_GetObject
464 INT BITMAP_GetObject( BITMAPOBJ * bmp, INT count, LPVOID buffer )
466 if (bmp->dib)
468 if (count < sizeof(DIBSECTION))
470 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
472 else
474 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
477 memcpy( buffer, bmp->dib, count );
478 return count;
480 else
482 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
483 memcpy( buffer, &bmp->bitmap, count );
484 return count;
489 /***********************************************************************
490 * CreateDiscardableBitmap16 (GDI.156)
492 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
493 INT16 height )
495 return CreateCompatibleBitmap16( hdc, width, height );
499 /******************************************************************************
500 * CreateDiscardableBitmap [GDI32.38] Creates a discardable bitmap
502 * RETURNS
503 * Success: Handle to bitmap
504 * Failure: NULL
506 HBITMAP WINAPI CreateDiscardableBitmap(
507 HDC hdc, /* [in] Handle to device context */
508 INT width, /* [in] Bitmap width */
509 INT height) /* [in] Bitmap height */
511 return CreateCompatibleBitmap( hdc, width, height );
515 /***********************************************************************
516 * GetBitmapDimensionEx16 (GDI.468)
518 * NOTES
519 * Can this call GetBitmapDimensionEx?
521 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
523 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
524 if (!bmp) return FALSE;
525 CONV_SIZE32TO16( &bmp->size, size );
526 GDI_HEAP_UNLOCK( hbitmap );
527 return TRUE;
531 /******************************************************************************
532 * GetBitmapDimensionEx [GDI32.144] 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_HEAP_UNLOCK( 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) CONV_SIZE32TO16( &bmp->size, prevSize );
570 bmp->size.cx = x;
571 bmp->size.cy = y;
572 GDI_HEAP_UNLOCK( hbitmap );
573 return TRUE;
577 /******************************************************************************
578 * SetBitmapDimensionEx [GDI32.304] Assignes dimensions to a bitmap
580 * RETURNS
581 * Success: TRUE
582 * Failure: FALSE
584 BOOL WINAPI SetBitmapDimensionEx(
585 HBITMAP hbitmap, /* [in] Handle to bitmap */
586 INT x, /* [in] Bitmap width */
587 INT y, /* [in] Bitmap height */
588 LPSIZE prevSize) /* [out] Address of structure for orig dims */
590 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
591 if (!bmp) return FALSE;
592 if (prevSize) *prevSize = bmp->size;
593 bmp->size.cx = x;
594 bmp->size.cy = y;
595 GDI_HEAP_UNLOCK( hbitmap );
596 return TRUE;
600 /***********************************************************************
601 * SetBitmapDimension (GDI.163)
603 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
605 SIZE16 size;
606 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
607 return MAKELONG( size.cx, size.cy );