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 * CreateBitmap32 [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
;
143 bmp
->DDBitmap
= NULL
;
146 if (bits
) /* Set bitmap bits */
147 SetBitmapBits( hbitmap
, height
* bmp
->bitmap
.bmWidthBytes
,
149 GDI_HEAP_UNLOCK( 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
167 * hdc [I] Handle to device context
168 * width [I] Width of bitmap
169 * height [I] Height of bitmap
172 * Success: Handle to bitmap
175 HBITMAP WINAPI
CreateCompatibleBitmap( HDC hdc
, INT width
, INT height
)
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",
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
);
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
);
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
214 * Success: Handle to bitmap
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
238 * Success: Number of bytes copied
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
);
251 /* If the bits vector is null, the function should return the read size */
253 return bmp
->bitmap
.bmWidthBytes
* bmp
->bitmap
.bmHeight
;
256 WARN("(%ld): Negative number of bytes passed???\n", 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
;
266 WARN("Less then one entire line requested\n");
267 GDI_HEAP_UNLOCK( hbitmap
);
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
);
278 TRACE("Calling device specific BitmapBits\n");
279 if(bmp
->DDBitmap
->funcs
->pBitmapBits
)
280 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hbitmap
, bits
, count
,
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 * SetBitmapBits32 [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
->DDBitmap
->funcs
->pBitmapBits
)
349 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hbitmap
, (void *) bits
,
352 ERR("BitmapBits == NULL??\n");
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");
364 memcpy(bmp
->bitmap
.bmBits
, bits
, count
);
369 GDI_HEAP_UNLOCK( hbitmap
);
373 /**********************************************************************
377 HBITMAP
BITMAP_CopyBitmap(HBITMAP hbitmap
)
379 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
387 res
= CreateBitmapIndirect(&bm
);
390 char *buf
= HeapAlloc( GetProcessHeap(), 0, bm
.bmWidthBytes
*
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
);
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 /***********************************************************************
423 INT16
BITMAP_GetObject16( BITMAPOBJ
* bmp
, INT16 count
, LPVOID buffer
)
427 if ( count
<= sizeof(BITMAP16
) )
429 BITMAP
*bmp32
= &bmp
->dib
->dsBm
;
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
);
443 FIXME("not implemented for DIBs: count %d\n", count
);
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
);
464 /***********************************************************************
467 INT
BITMAP_GetObject( BITMAPOBJ
* bmp
, INT count
, LPVOID buffer
)
471 if (count
< sizeof(DIBSECTION
))
473 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
477 if (count
> sizeof(DIBSECTION
)) count
= sizeof(DIBSECTION
);
480 memcpy( buffer
, bmp
->dib
, count
);
485 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
486 memcpy( buffer
, &bmp
->bitmap
, count
);
492 /***********************************************************************
493 * CreateDiscardableBitmap16 (GDI.156)
495 HBITMAP16 WINAPI
CreateDiscardableBitmap16( HDC16 hdc
, INT16 width
,
498 return CreateCompatibleBitmap16( hdc
, width
, height
);
502 /******************************************************************************
503 * CreateDiscardableBitmap32 [GDI32.38] Creates a discardable bitmap
506 * Success: Handle to bitmap
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)
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
);
534 /******************************************************************************
535 * GetBitmapDimensionEx32 [GDI32.144] Retrieves dimensions of a bitmap
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
;
548 GDI_HEAP_UNLOCK( hbitmap
);
553 /***********************************************************************
554 * GetBitmapDimension (GDI.162)
556 DWORD WINAPI
GetBitmapDimension16( HBITMAP16 hbitmap
)
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
,
570 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
571 if (!bmp
) return FALSE
;
572 if (prevSize
) CONV_SIZE32TO16( &bmp
->size
, prevSize
);
575 GDI_HEAP_UNLOCK( hbitmap
);
580 /******************************************************************************
581 * SetBitmapDimensionEx32 [GDI32.304] Assignes dimensions to a bitmap
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
;
598 GDI_HEAP_UNLOCK( hbitmap
);
603 /***********************************************************************
604 * SetBitmapDimension (GDI.163)
606 DWORD WINAPI
SetBitmapDimension16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
)
609 if (!SetBitmapDimensionEx16( hbitmap
, x
, y
, &size
)) return 0;
610 return MAKELONG( size
.cx
, size
.cy
);