4 * Copyright 1993 Alexandre Julliard
11 #include "wine/winbase16.h"
17 #include "cursoricon.h"
18 #include "debugtools.h"
19 #include "wine/winuser16.h"
21 DEFAULT_DEBUG_CHANNEL(bitmap
);
23 BITMAP_DRIVER
*BITMAP_Driver
= NULL
;
26 /***********************************************************************
27 * BITMAP_GetWidthBytes
29 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
32 INT
BITMAP_GetWidthBytes( INT bmWidth
, INT bpp
)
37 return 2 * ((bmWidth
+15) >> 4);
40 bmWidth
*= 3; /* fall through */
42 return bmWidth
+ (bmWidth
& 1);
52 return 2 * ((bmWidth
+3) >> 2);
55 WARN("Unknown depth %d, please report.\n", bpp
);
60 /***********************************************************************
61 * CreateUserBitmap16 (GDI.407)
63 HBITMAP16 WINAPI
CreateUserBitmap16( INT16 width
, INT16 height
, UINT16 planes
,
64 UINT16 bpp
, LPCVOID bits
)
66 return CreateBitmap16( width
, height
, planes
, bpp
, bits
);
69 /***********************************************************************
70 * CreateUserDiscardableBitmap16 (GDI.409)
72 HBITMAP16 WINAPI
CreateUserDiscardableBitmap16( WORD dummy
,
73 INT16 width
, INT16 height
)
75 HDC hdc
= CreateDCA( "DISPLAY", NULL
, NULL
, NULL
);
76 HBITMAP16 ret
= CreateCompatibleBitmap16( hdc
, width
, height
);
82 /***********************************************************************
83 * CreateBitmap16 (GDI.48)
85 HBITMAP16 WINAPI
CreateBitmap16( INT16 width
, INT16 height
, UINT16 planes
,
86 UINT16 bpp
, LPCVOID bits
)
88 return CreateBitmap( width
, height
, planes
, bpp
, bits
);
92 /******************************************************************************
93 * CreateBitmap [GDI32.25] Creates a bitmap with the specified info
96 * width [I] bitmap width
97 * height [I] bitmap height
98 * planes [I] Number of color planes
99 * bpp [I] Number of bits to identify a color
100 * bits [I] Pointer to array containing color data
103 * Success: Handle to bitmap
106 HBITMAP WINAPI
CreateBitmap( INT width
, INT height
, UINT planes
,
107 UINT bpp
, LPCVOID bits
)
112 planes
= (BYTE
)planes
;
116 /* Check parameters */
117 if (!height
|| !width
) return 0;
119 FIXME("planes = %d\n", planes
);
122 if (height
< 0) height
= -height
;
123 if (width
< 0) width
= -width
;
125 /* Create the BITMAPOBJ */
126 hbitmap
= GDI_AllocObject( sizeof(BITMAPOBJ
), BITMAP_MAGIC
);
127 if (!hbitmap
) return 0;
129 TRACE("%dx%d, %d colors returning %08x\n", width
, height
,
130 1 << (planes
*bpp
), hbitmap
);
132 bmp
= (BITMAPOBJ
*) GDI_HEAP_LOCK( hbitmap
);
136 bmp
->bitmap
.bmType
= 0;
137 bmp
->bitmap
.bmWidth
= width
;
138 bmp
->bitmap
.bmHeight
= height
;
139 bmp
->bitmap
.bmPlanes
= planes
;
140 bmp
->bitmap
.bmBitsPixel
= bpp
;
141 bmp
->bitmap
.bmWidthBytes
= BITMAP_GetWidthBytes( width
, bpp
);
142 bmp
->bitmap
.bmBits
= NULL
;
145 bmp
->physBitmap
= NULL
;
148 if (bits
) /* Set bitmap bits */
149 SetBitmapBits( hbitmap
, height
* bmp
->bitmap
.bmWidthBytes
,
151 GDI_HEAP_UNLOCK( hbitmap
);
156 /***********************************************************************
157 * CreateCompatibleBitmap16 (GDI.51)
159 HBITMAP16 WINAPI
CreateCompatibleBitmap16(HDC16 hdc
, INT16 width
, INT16 height
)
161 return CreateCompatibleBitmap( hdc
, width
, height
);
165 /******************************************************************************
166 * CreateCompatibleBitmap [GDI32.30] Creates a bitmap compatible with the DC
169 * hdc [I] Handle to device context
170 * width [I] Width of bitmap
171 * height [I] Height of bitmap
174 * Success: Handle to bitmap
177 HBITMAP WINAPI
CreateCompatibleBitmap( HDC hdc
, INT width
, INT height
)
182 TRACE("(%04x,%d,%d) = \n", hdc
, width
, height
);
183 if (!(dc
= DC_GetDCPtr( hdc
))) return 0;
184 if ((width
>= 0x10000) || (height
>= 0x10000)) {
185 FIXME("got bad width %d or height %d, please look for reason\n",
188 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
189 if (!width
|| !height
)
190 hbmpRet
= CreateBitmap( 1, 1, 1, 1, NULL
);
192 hbmpRet
= CreateBitmap( width
, height
, 1, dc
->w
.bitsPerPixel
, NULL
);
193 if(dc
->funcs
->pCreateBitmap
)
194 dc
->funcs
->pCreateBitmap( hbmpRet
);
196 TRACE("\t\t%04x\n", hbmpRet
);
197 GDI_HEAP_UNLOCK(hdc
);
202 /***********************************************************************
203 * CreateBitmapIndirect16 (GDI.49)
205 HBITMAP16 WINAPI
CreateBitmapIndirect16( const BITMAP16
* bmp
)
207 return CreateBitmap16( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
208 bmp
->bmBitsPixel
, PTR_SEG_TO_LIN( bmp
->bmBits
) );
212 /******************************************************************************
213 * CreateBitmapIndirect [GDI32.26] Creates a bitmap with the specifies info
216 * Success: Handle to bitmap
219 HBITMAP WINAPI
CreateBitmapIndirect(
220 const BITMAP
* bmp
) /* [in] Pointer to the bitmap data */
222 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
223 bmp
->bmBitsPixel
, bmp
->bmBits
);
227 /***********************************************************************
228 * GetBitmapBits16 (GDI.74)
230 LONG WINAPI
GetBitmapBits16( HBITMAP16 hbitmap
, LONG count
, LPVOID buffer
)
232 return GetBitmapBits( hbitmap
, count
, buffer
);
236 /***********************************************************************
237 * GetBitmapBits [GDI32.143] Copies bitmap bits of bitmap to buffer
240 * Success: Number of bytes copied
243 LONG WINAPI
GetBitmapBits(
244 HBITMAP hbitmap
, /* [in] Handle to bitmap */
245 LONG count
, /* [in] Number of bytes to copy */
246 LPVOID bits
) /* [out] Pointer to buffer to receive bits */
248 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
253 /* If the bits vector is null, the function should return the read size */
255 return bmp
->bitmap
.bmWidthBytes
* bmp
->bitmap
.bmHeight
;
258 WARN("(%ld): Negative number of bytes passed???\n", count
);
262 /* Only get entire lines */
263 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
264 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
265 count
= height
* bmp
->bitmap
.bmWidthBytes
;
268 WARN("Less then one entire line requested\n");
269 GDI_HEAP_UNLOCK( hbitmap
);
274 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
275 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
276 1 << bmp
->bitmap
.bmBitsPixel
, height
);
280 TRACE("Calling device specific BitmapBits\n");
281 if(bmp
->funcs
->pBitmapBits
)
282 ret
= bmp
->funcs
->pBitmapBits(hbitmap
, bits
, count
, DDB_GET
);
284 ERR("BitmapBits == NULL??\n");
290 if(!bmp
->bitmap
.bmBits
) {
291 WARN("Bitmap is empty\n");
294 memcpy(bits
, bmp
->bitmap
.bmBits
, count
);
300 GDI_HEAP_UNLOCK( hbitmap
);
305 /***********************************************************************
306 * SetBitmapBits16 (GDI.106)
308 LONG WINAPI
SetBitmapBits16( HBITMAP16 hbitmap
, LONG count
, LPCVOID buffer
)
310 return SetBitmapBits( hbitmap
, count
, buffer
);
314 /******************************************************************************
315 * SetBitmapBits [GDI32.303] Sets bits of color data for a bitmap
318 * Success: Number of bytes used in setting the bitmap bits
321 LONG WINAPI
SetBitmapBits(
322 HBITMAP hbitmap
, /* [in] Handle to bitmap */
323 LONG count
, /* [in] Number of bytes in bitmap array */
324 LPCVOID bits
) /* [in] Address of array with bitmap bits */
326 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
329 if ((!bmp
) || (!bits
))
333 WARN("(%ld): Negative number of bytes passed???\n", count
);
337 /* Only get entire lines */
338 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
339 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
340 count
= height
* bmp
->bitmap
.bmWidthBytes
;
342 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
343 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
344 1 << bmp
->bitmap
.bmBitsPixel
, height
);
348 TRACE("Calling device specific BitmapBits\n");
349 if(bmp
->funcs
->pBitmapBits
)
350 ret
= bmp
->funcs
->pBitmapBits(hbitmap
, (void *) bits
, count
, DDB_SET
);
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
->funcs
&& bmp
->funcs
->pDeleteObject
)
407 bmp
->funcs
->pDeleteObject( hbitmap
);
409 if( bmp
->bitmap
.bmBits
)
410 HeapFree( GetProcessHeap(), 0, bmp
->bitmap
.bmBits
);
412 DIB_DeleteDIBSection( bmp
);
414 return GDI_FreeObject( hbitmap
);
418 /***********************************************************************
421 INT16
BITMAP_GetObject16( BITMAPOBJ
* bmp
, INT16 count
, LPVOID buffer
)
425 if ( count
<= sizeof(BITMAP16
) )
427 BITMAP
*bmp32
= &bmp
->dib
->dsBm
;
429 bmp16
.bmType
= bmp32
->bmType
;
430 bmp16
.bmWidth
= bmp32
->bmWidth
;
431 bmp16
.bmHeight
= bmp32
->bmHeight
;
432 bmp16
.bmWidthBytes
= bmp32
->bmWidthBytes
;
433 bmp16
.bmPlanes
= bmp32
->bmPlanes
;
434 bmp16
.bmBitsPixel
= bmp32
->bmBitsPixel
;
435 bmp16
.bmBits
= (SEGPTR
)0;
436 memcpy( buffer
, &bmp16
, count
);
441 FIXME("not implemented for DIBs: count %d\n", count
);
448 bmp16
.bmType
= bmp
->bitmap
.bmType
;
449 bmp16
.bmWidth
= bmp
->bitmap
.bmWidth
;
450 bmp16
.bmHeight
= bmp
->bitmap
.bmHeight
;
451 bmp16
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
452 bmp16
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
453 bmp16
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
454 bmp16
.bmBits
= (SEGPTR
)0;
455 if (count
> sizeof(bmp16
)) count
= sizeof(bmp16
);
456 memcpy( buffer
, &bmp16
, count
);
462 /***********************************************************************
465 INT
BITMAP_GetObject( BITMAPOBJ
* bmp
, INT count
, LPVOID buffer
)
469 if (count
< sizeof(DIBSECTION
))
471 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
475 if (count
> sizeof(DIBSECTION
)) count
= sizeof(DIBSECTION
);
478 memcpy( buffer
, bmp
->dib
, count
);
483 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
484 memcpy( buffer
, &bmp
->bitmap
, count
);
490 /***********************************************************************
491 * CreateDiscardableBitmap16 (GDI.156)
493 HBITMAP16 WINAPI
CreateDiscardableBitmap16( HDC16 hdc
, INT16 width
,
496 return CreateCompatibleBitmap16( hdc
, width
, height
);
500 /******************************************************************************
501 * CreateDiscardableBitmap [GDI32.38] Creates a discardable bitmap
504 * Success: Handle to bitmap
507 HBITMAP WINAPI
CreateDiscardableBitmap(
508 HDC hdc
, /* [in] Handle to device context */
509 INT width
, /* [in] Bitmap width */
510 INT height
) /* [in] Bitmap height */
512 return CreateCompatibleBitmap( hdc
, width
, height
);
516 /***********************************************************************
517 * GetBitmapDimensionEx16 (GDI.468)
520 * Can this call GetBitmapDimensionEx?
522 BOOL16 WINAPI
GetBitmapDimensionEx16( HBITMAP16 hbitmap
, LPSIZE16 size
)
524 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
525 if (!bmp
) return FALSE
;
526 CONV_SIZE32TO16( &bmp
->size
, size
);
527 GDI_HEAP_UNLOCK( hbitmap
);
532 /******************************************************************************
533 * GetBitmapDimensionEx [GDI32.144] Retrieves dimensions of a bitmap
539 BOOL WINAPI
GetBitmapDimensionEx(
540 HBITMAP hbitmap
, /* [in] Handle to bitmap */
541 LPSIZE size
) /* [out] Address of struct receiving dimensions */
543 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
544 if (!bmp
) return FALSE
;
546 GDI_HEAP_UNLOCK( hbitmap
);
551 /***********************************************************************
552 * GetBitmapDimension (GDI.162)
554 DWORD WINAPI
GetBitmapDimension16( HBITMAP16 hbitmap
)
557 if (!GetBitmapDimensionEx16( hbitmap
, &size
)) return 0;
558 return MAKELONG( size
.cx
, size
.cy
);
562 /***********************************************************************
563 * SetBitmapDimensionEx16 (GDI.478)
565 BOOL16 WINAPI
SetBitmapDimensionEx16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
,
568 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
569 if (!bmp
) return FALSE
;
570 if (prevSize
) CONV_SIZE32TO16( &bmp
->size
, prevSize
);
573 GDI_HEAP_UNLOCK( hbitmap
);
578 /******************************************************************************
579 * SetBitmapDimensionEx [GDI32.304] Assignes dimensions to a bitmap
585 BOOL WINAPI
SetBitmapDimensionEx(
586 HBITMAP hbitmap
, /* [in] Handle to bitmap */
587 INT x
, /* [in] Bitmap width */
588 INT y
, /* [in] Bitmap height */
589 LPSIZE prevSize
) /* [out] Address of structure for orig dims */
591 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
592 if (!bmp
) return FALSE
;
593 if (prevSize
) *prevSize
= bmp
->size
;
596 GDI_HEAP_UNLOCK( hbitmap
);
601 /***********************************************************************
602 * SetBitmapDimension (GDI.163)
604 DWORD WINAPI
SetBitmapDimension16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
)
607 if (!SetBitmapDimensionEx16( hbitmap
, x
, y
, &size
)) return 0;
608 return MAKELONG( size
.cx
, size
.cy
);