Release 20000326.
[wine/gsoc-2012-control.git] / objects / bitmap.c
blob8f74c7f6fd7603d5f7adc005ceebdbb328e738fa
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 "cursoricon.h"
18 #include "debugtools.h"
19 #include "monitor.h"
20 #include "wine/winuser16.h"
22 DEFAULT_DEBUG_CHANNEL(bitmap)
23 DECLARE_DEBUG_CHANNEL(resource)
25 BITMAP_DRIVER *BITMAP_Driver = NULL;
28 /***********************************************************************
29 * BITMAP_GetWidthBytes
31 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
32 * data.
34 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
36 switch(bpp)
38 case 1:
39 return 2 * ((bmWidth+15) >> 4);
41 case 24:
42 bmWidth *= 3; /* fall through */
43 case 8:
44 return bmWidth + (bmWidth & 1);
46 case 32:
47 return bmWidth * 4;
49 case 16:
50 case 15:
51 return bmWidth * 2;
53 case 4:
54 return 2 * ((bmWidth+3) >> 2);
56 default:
57 WARN("Unknown depth %d, please report.\n", bpp );
59 return -1;
62 /***********************************************************************
63 * CreateUserBitmap16 (GDI.407)
65 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
66 UINT16 bpp, LPCVOID bits )
68 return CreateBitmap16( width, height, planes, bpp, bits );
71 /***********************************************************************
72 * CreateUserDiscardableBitmap16 (GDI.409)
74 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy,
75 INT16 width, INT16 height )
77 return CreateUserBitmap16( width, height, 1, MONITOR_GetDepth(&MONITOR_PrimaryMonitor), NULL );
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 * CreateBitmap32 [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->DDBitmap = NULL;
144 bmp->dib = NULL;
146 if (bits) /* Set bitmap bits */
147 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
148 bits );
149 GDI_HEAP_UNLOCK( hbitmap );
150 return hbitmap;
154 /***********************************************************************
155 * CreateCompatibleBitmap16 (GDI.51)
157 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
159 return CreateCompatibleBitmap( hdc, width, height );
163 /******************************************************************************
164 * CreateCompatibleBitmap32 [GDI32.30] Creates a bitmap compatible with the DC
166 * PARAMS
167 * hdc [I] Handle to device context
168 * width [I] Width of bitmap
169 * height [I] Height of bitmap
171 * RETURNS
172 * Success: Handle to bitmap
173 * Failure: 0
175 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
177 HBITMAP hbmpRet = 0;
178 DC *dc;
180 TRACE("(%04x,%d,%d) = \n", hdc, width, height );
181 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
182 if ((width >= 0x10000) || (height >= 0x10000)) {
183 FIXME("got bad width %d or height %d, please look for reason\n",
184 width, height );
185 } else {
186 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
187 if (!width || !height)
188 hbmpRet = CreateBitmap( 1, 1, 1, 1, NULL );
189 else
190 hbmpRet = CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
191 if(dc->funcs->pCreateBitmap)
192 dc->funcs->pCreateBitmap( hbmpRet );
194 TRACE("\t\t%04x\n", hbmpRet);
195 GDI_HEAP_UNLOCK(hdc);
196 return hbmpRet;
200 /***********************************************************************
201 * CreateBitmapIndirect16 (GDI.49)
203 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
205 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
206 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
210 /******************************************************************************
211 * CreateBitmapIndirect32 [GDI32.26] Creates a bitmap with the specifies info
213 * RETURNS
214 * Success: Handle to bitmap
215 * Failure: NULL
217 HBITMAP WINAPI CreateBitmapIndirect(
218 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
220 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
221 bmp->bmBitsPixel, bmp->bmBits );
225 /***********************************************************************
226 * GetBitmapBits16 (GDI.74)
228 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
230 return GetBitmapBits( hbitmap, count, buffer );
234 /***********************************************************************
235 * GetBitmapBits32 [GDI32.143] Copies bitmap bits of bitmap to buffer
237 * RETURNS
238 * Success: Number of bytes copied
239 * Failure: 0
241 LONG WINAPI GetBitmapBits(
242 HBITMAP hbitmap, /* [in] Handle to bitmap */
243 LONG count, /* [in] Number of bytes to copy */
244 LPVOID bits) /* [out] Pointer to buffer to receive bits */
246 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
247 LONG height, ret;
249 if (!bmp) return 0;
251 /* If the bits vector is null, the function should return the read size */
252 if(bits == NULL)
253 return bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
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 GDI_HEAP_UNLOCK( hbitmap );
268 return 0;
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->DDBitmap) {
278 TRACE("Calling device specific BitmapBits\n");
279 if(bmp->DDBitmap->funcs->pBitmapBits)
280 ret = bmp->DDBitmap->funcs->pBitmapBits(hbitmap, bits, count,
281 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 * SetBitmapBits32 [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->DDBitmap) {
347 TRACE("Calling device specific BitmapBits\n");
348 if(bmp->DDBitmap->funcs->pBitmapBits)
349 ret = bmp->DDBitmap->funcs->pBitmapBits(hbitmap, (void *) bits,
350 count, DDB_SET);
351 else {
352 ERR("BitmapBits == NULL??\n");
353 ret = 0;
356 } else {
358 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
359 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
360 if(!bmp->bitmap.bmBits) {
361 WARN("Unable to allocate bit buffer\n");
362 ret = 0;
363 } else {
364 memcpy(bmp->bitmap.bmBits, bits, count);
365 ret = count;
369 GDI_HEAP_UNLOCK( hbitmap );
370 return ret;
373 /**********************************************************************
374 * BITMAP_CopyBitmap
377 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
379 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
380 HBITMAP res = 0;
381 BITMAP bm;
383 if(!bmp) return 0;
385 bm = bmp->bitmap;
386 bm.bmBits = NULL;
387 res = CreateBitmapIndirect(&bm);
389 if(res) {
390 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
391 bm.bmHeight );
392 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
393 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
394 HeapFree( GetProcessHeap(), 0, buf );
397 GDI_HEAP_UNLOCK( hbitmap );
398 return res;
401 /***********************************************************************
402 * BITMAP_DeleteObject
404 BOOL BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
406 if( bmp->DDBitmap ) {
407 if( bmp->DDBitmap->funcs->pDeleteObject )
408 bmp->DDBitmap->funcs->pDeleteObject( hbitmap );
411 if( bmp->bitmap.bmBits )
412 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
414 DIB_DeleteDIBSection( bmp );
416 return GDI_FreeObject( hbitmap );
420 /***********************************************************************
421 * BITMAP_GetObject16
423 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
425 if (bmp->dib)
427 if ( count <= sizeof(BITMAP16) )
429 BITMAP *bmp32 = &bmp->dib->dsBm;
430 BITMAP16 bmp16;
431 bmp16.bmType = bmp32->bmType;
432 bmp16.bmWidth = bmp32->bmWidth;
433 bmp16.bmHeight = bmp32->bmHeight;
434 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
435 bmp16.bmPlanes = bmp32->bmPlanes;
436 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
437 bmp16.bmBits = (SEGPTR)0;
438 memcpy( buffer, &bmp16, count );
439 return count;
441 else
443 FIXME("not implemented for DIBs: count %d\n", count);
444 return 0;
447 else
449 BITMAP16 bmp16;
450 bmp16.bmType = bmp->bitmap.bmType;
451 bmp16.bmWidth = bmp->bitmap.bmWidth;
452 bmp16.bmHeight = bmp->bitmap.bmHeight;
453 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
454 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
455 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
456 bmp16.bmBits = (SEGPTR)0;
457 if (count > sizeof(bmp16)) count = sizeof(bmp16);
458 memcpy( buffer, &bmp16, count );
459 return count;
464 /***********************************************************************
465 * BITMAP_GetObject32
467 INT BITMAP_GetObject( BITMAPOBJ * bmp, INT count, LPVOID buffer )
469 if (bmp->dib)
471 if (count < sizeof(DIBSECTION))
473 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
475 else
477 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
480 memcpy( buffer, bmp->dib, count );
481 return count;
483 else
485 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
486 memcpy( buffer, &bmp->bitmap, count );
487 return count;
492 /***********************************************************************
493 * CreateDiscardableBitmap16 (GDI.156)
495 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
496 INT16 height )
498 return CreateCompatibleBitmap16( hdc, width, height );
502 /******************************************************************************
503 * CreateDiscardableBitmap32 [GDI32.38] Creates a discardable bitmap
505 * RETURNS
506 * Success: Handle to bitmap
507 * Failure: NULL
509 HBITMAP WINAPI CreateDiscardableBitmap(
510 HDC hdc, /* [in] Handle to device context */
511 INT width, /* [in] Bitmap width */
512 INT height) /* [in] Bitmap height */
514 return CreateCompatibleBitmap( hdc, width, height );
518 /***********************************************************************
519 * GetBitmapDimensionEx16 (GDI.468)
521 * NOTES
522 * Can this call GetBitmapDimensionEx32?
524 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
526 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
527 if (!bmp) return FALSE;
528 CONV_SIZE32TO16( &bmp->size, size );
529 GDI_HEAP_UNLOCK( hbitmap );
530 return TRUE;
534 /******************************************************************************
535 * GetBitmapDimensionEx32 [GDI32.144] Retrieves dimensions of a bitmap
537 * RETURNS
538 * Success: TRUE
539 * Failure: FALSE
541 BOOL WINAPI GetBitmapDimensionEx(
542 HBITMAP hbitmap, /* [in] Handle to bitmap */
543 LPSIZE size) /* [out] Address of struct receiving dimensions */
545 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
546 if (!bmp) return FALSE;
547 *size = bmp->size;
548 GDI_HEAP_UNLOCK( hbitmap );
549 return TRUE;
553 /***********************************************************************
554 * GetBitmapDimension (GDI.162)
556 DWORD WINAPI GetBitmapDimension16( HBITMAP16 hbitmap )
558 SIZE16 size;
559 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
560 return MAKELONG( size.cx, size.cy );
564 /***********************************************************************
565 * SetBitmapDimensionEx16 (GDI.478)
567 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
568 LPSIZE16 prevSize )
570 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
571 if (!bmp) return FALSE;
572 if (prevSize) CONV_SIZE32TO16( &bmp->size, prevSize );
573 bmp->size.cx = x;
574 bmp->size.cy = y;
575 GDI_HEAP_UNLOCK( hbitmap );
576 return TRUE;
580 /******************************************************************************
581 * SetBitmapDimensionEx32 [GDI32.304] Assignes dimensions to a bitmap
583 * RETURNS
584 * Success: TRUE
585 * Failure: FALSE
587 BOOL WINAPI SetBitmapDimensionEx(
588 HBITMAP hbitmap, /* [in] Handle to bitmap */
589 INT x, /* [in] Bitmap width */
590 INT y, /* [in] Bitmap height */
591 LPSIZE prevSize) /* [out] Address of structure for orig dims */
593 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
594 if (!bmp) return FALSE;
595 if (prevSize) *prevSize = bmp->size;
596 bmp->size.cx = x;
597 bmp->size.cy = y;
598 GDI_HEAP_UNLOCK( hbitmap );
599 return TRUE;
603 /***********************************************************************
604 * SetBitmapDimension (GDI.163)
606 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
608 SIZE16 size;
609 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
610 return MAKELONG( size.cx, size.cy );