4 * Copyright 1993 Alexandre Julliard
11 #include "wine/winbase16.h"
17 #include "cursoricon.h"
18 #include "debugtools.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
34 INT
BITMAP_GetWidthBytes( INT bmWidth
, INT bpp
)
39 return 2 * ((bmWidth
+15) >> 4);
42 bmWidth
*= 3; /* fall through */
44 return bmWidth
+ (bmWidth
& 1);
54 return 2 * ((bmWidth
+3) >> 2);
57 WARN("Unknown depth %d, please report.\n", bpp
);
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 * CreateBitmap [GDI32.25] Creates a bitmap with the specified info
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
102 * Success: Handle to bitmap
105 HBITMAP WINAPI
CreateBitmap( INT width
, INT height
, UINT planes
,
106 UINT bpp
, LPCVOID bits
)
111 planes
= (BYTE
)planes
;
115 /* Check parameters */
116 if (!height
|| !width
) return 0;
118 FIXME("planes = %d\n", planes
);
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
);
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
;
144 bmp
->physBitmap
= NULL
;
147 if (bits
) /* Set bitmap bits */
148 SetBitmapBits( hbitmap
, height
* bmp
->bitmap
.bmWidthBytes
,
150 GDI_HEAP_UNLOCK( 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
168 * hdc [I] Handle to device context
169 * width [I] Width of bitmap
170 * height [I] Height of bitmap
173 * Success: Handle to bitmap
176 HBITMAP WINAPI
CreateCompatibleBitmap( HDC hdc
, INT width
, INT height
)
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",
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
);
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
);
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
215 * Success: Handle to bitmap
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
239 * Success: Number of bytes copied
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
);
252 /* If the bits vector is null, the function should return the read size */
254 return bmp
->bitmap
.bmWidthBytes
* bmp
->bitmap
.bmHeight
;
257 WARN("(%ld): Negative number of bytes passed???\n", 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
;
267 WARN("Less then one entire line requested\n");
268 GDI_HEAP_UNLOCK( hbitmap
);
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
);
279 TRACE("Calling device specific BitmapBits\n");
280 if(bmp
->funcs
->pBitmapBits
)
281 ret
= bmp
->funcs
->pBitmapBits(hbitmap
, bits
, count
, DDB_GET
);
283 ERR("BitmapBits == NULL??\n");
289 if(!bmp
->bitmap
.bmBits
) {
290 WARN("Bitmap is empty\n");
293 memcpy(bits
, bmp
->bitmap
.bmBits
, count
);
299 GDI_HEAP_UNLOCK( hbitmap
);
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
317 * Success: Number of bytes used in setting the bitmap bits
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
);
328 if ((!bmp
) || (!bits
))
332 WARN("(%ld): Negative number of bytes passed???\n", 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
);
347 TRACE("Calling device specific BitmapBits\n");
348 if(bmp
->funcs
->pBitmapBits
)
349 ret
= bmp
->funcs
->pBitmapBits(hbitmap
, (void *) bits
, count
, DDB_SET
);
351 ERR("BitmapBits == NULL??\n");
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");
363 memcpy(bmp
->bitmap
.bmBits
, bits
, count
);
368 GDI_HEAP_UNLOCK( hbitmap
);
372 /**********************************************************************
376 HBITMAP
BITMAP_CopyBitmap(HBITMAP hbitmap
)
378 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
386 res
= CreateBitmapIndirect(&bm
);
389 char *buf
= HeapAlloc( GetProcessHeap(), 0, bm
.bmWidthBytes
*
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
);
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 /***********************************************************************
420 INT16
BITMAP_GetObject16( BITMAPOBJ
* bmp
, INT16 count
, LPVOID buffer
)
424 if ( count
<= sizeof(BITMAP16
) )
426 BITMAP
*bmp32
= &bmp
->dib
->dsBm
;
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
);
440 FIXME("not implemented for DIBs: count %d\n", count
);
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
);
461 /***********************************************************************
464 INT
BITMAP_GetObject( BITMAPOBJ
* bmp
, INT count
, LPVOID buffer
)
468 if (count
< sizeof(DIBSECTION
))
470 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
474 if (count
> sizeof(DIBSECTION
)) count
= sizeof(DIBSECTION
);
477 memcpy( buffer
, bmp
->dib
, count
);
482 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
483 memcpy( buffer
, &bmp
->bitmap
, count
);
489 /***********************************************************************
490 * CreateDiscardableBitmap16 (GDI.156)
492 HBITMAP16 WINAPI
CreateDiscardableBitmap16( HDC16 hdc
, INT16 width
,
495 return CreateCompatibleBitmap16( hdc
, width
, height
);
499 /******************************************************************************
500 * CreateDiscardableBitmap [GDI32.38] Creates a discardable bitmap
503 * Success: Handle to bitmap
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)
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
);
531 /******************************************************************************
532 * GetBitmapDimensionEx [GDI32.144] Retrieves dimensions of a bitmap
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
;
545 GDI_HEAP_UNLOCK( hbitmap
);
550 /***********************************************************************
551 * GetBitmapDimension (GDI.162)
553 DWORD WINAPI
GetBitmapDimension16( HBITMAP16 hbitmap
)
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
,
567 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
568 if (!bmp
) return FALSE
;
569 if (prevSize
) CONV_SIZE32TO16( &bmp
->size
, prevSize
);
572 GDI_HEAP_UNLOCK( hbitmap
);
577 /******************************************************************************
578 * SetBitmapDimensionEx [GDI32.304] Assignes dimensions to a bitmap
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
;
595 GDI_HEAP_UNLOCK( hbitmap
);
600 /***********************************************************************
601 * SetBitmapDimension (GDI.163)
603 DWORD WINAPI
SetBitmapDimension16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
)
606 if (!SetBitmapDimensionEx16( hbitmap
, x
, y
, &size
)) return 0;
607 return MAKELONG( size
.cx
, size
.cy
);