4 * Copyright 1993 Alexandre Julliard
11 #include "wine/winbase16.h"
15 #include "debugtools.h"
16 #include "wine/winuser16.h"
18 DEFAULT_DEBUG_CHANNEL(bitmap
);
20 BITMAP_DRIVER
*BITMAP_Driver
= NULL
;
23 /***********************************************************************
24 * BITMAP_GetWidthBytes
26 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
29 INT
BITMAP_GetWidthBytes( INT bmWidth
, INT bpp
)
34 return 2 * ((bmWidth
+15) >> 4);
37 bmWidth
*= 3; /* fall through */
39 return bmWidth
+ (bmWidth
& 1);
49 return 2 * ((bmWidth
+3) >> 2);
52 WARN("Unknown depth %d, please report.\n", bpp
);
57 /***********************************************************************
58 * CreateUserBitmap16 (GDI.407)
60 HBITMAP16 WINAPI
CreateUserBitmap16( INT16 width
, INT16 height
, UINT16 planes
,
61 UINT16 bpp
, LPCVOID bits
)
63 return CreateBitmap16( width
, height
, planes
, bpp
, bits
);
66 /***********************************************************************
67 * CreateUserDiscardableBitmap16 (GDI.409)
69 HBITMAP16 WINAPI
CreateUserDiscardableBitmap16( WORD dummy
,
70 INT16 width
, INT16 height
)
72 HDC hdc
= CreateDCA( "DISPLAY", NULL
, NULL
, NULL
);
73 HBITMAP16 ret
= CreateCompatibleBitmap16( hdc
, width
, height
);
79 /***********************************************************************
80 * CreateBitmap16 (GDI.48)
82 HBITMAP16 WINAPI
CreateBitmap16( INT16 width
, INT16 height
, UINT16 planes
,
83 UINT16 bpp
, LPCVOID bits
)
85 return CreateBitmap( width
, height
, planes
, bpp
, bits
);
89 /******************************************************************************
90 * CreateBitmap [GDI32.25] Creates a bitmap with the specified info
93 * width [I] bitmap width
94 * height [I] bitmap height
95 * planes [I] Number of color planes
96 * bpp [I] Number of bits to identify a color
97 * bits [I] Pointer to array containing color data
100 * Success: Handle to bitmap
103 HBITMAP WINAPI
CreateBitmap( INT width
, INT height
, UINT planes
,
104 UINT bpp
, LPCVOID bits
)
109 planes
= (BYTE
)planes
;
113 /* Check parameters */
114 if (!height
|| !width
) return 0;
116 FIXME("planes = %d\n", planes
);
119 if (height
< 0) height
= -height
;
120 if (width
< 0) width
= -width
;
122 /* Create the BITMAPOBJ */
123 if (!(bmp
= GDI_AllocObject( sizeof(BITMAPOBJ
), BITMAP_MAGIC
, &hbitmap
))) return 0;
125 TRACE("%dx%d, %d colors returning %08x\n", width
, height
,
126 1 << (planes
*bpp
), hbitmap
);
130 bmp
->bitmap
.bmType
= 0;
131 bmp
->bitmap
.bmWidth
= width
;
132 bmp
->bitmap
.bmHeight
= height
;
133 bmp
->bitmap
.bmPlanes
= planes
;
134 bmp
->bitmap
.bmBitsPixel
= bpp
;
135 bmp
->bitmap
.bmWidthBytes
= BITMAP_GetWidthBytes( width
, bpp
);
136 bmp
->bitmap
.bmBits
= NULL
;
139 bmp
->physBitmap
= NULL
;
142 if (bits
) /* Set bitmap bits */
143 SetBitmapBits( hbitmap
, height
* bmp
->bitmap
.bmWidthBytes
,
145 GDI_ReleaseObj( hbitmap
);
150 /***********************************************************************
151 * CreateCompatibleBitmap16 (GDI.51)
153 HBITMAP16 WINAPI
CreateCompatibleBitmap16(HDC16 hdc
, INT16 width
, INT16 height
)
155 return CreateCompatibleBitmap( hdc
, width
, height
);
159 /******************************************************************************
160 * CreateCompatibleBitmap [GDI32.30] Creates a bitmap compatible with the DC
163 * hdc [I] Handle to device context
164 * width [I] Width of bitmap
165 * height [I] Height of bitmap
168 * Success: Handle to bitmap
171 HBITMAP WINAPI
CreateCompatibleBitmap( HDC hdc
, INT width
, INT height
)
176 TRACE("(%04x,%d,%d) = \n", hdc
, width
, height
);
177 if (!(dc
= DC_GetDCPtr( hdc
))) return 0;
178 if ((width
>= 0x10000) || (height
>= 0x10000)) {
179 FIXME("got bad width %d or height %d, please look for reason\n",
182 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
183 if (!width
|| !height
)
184 hbmpRet
= CreateBitmap( 1, 1, 1, 1, NULL
);
186 hbmpRet
= CreateBitmap( width
, height
, 1, dc
->bitsPerPixel
, NULL
);
187 if(dc
->funcs
->pCreateBitmap
)
188 dc
->funcs
->pCreateBitmap( hbmpRet
);
190 TRACE("\t\t%04x\n", hbmpRet
);
196 /***********************************************************************
197 * CreateBitmapIndirect16 (GDI.49)
199 HBITMAP16 WINAPI
CreateBitmapIndirect16( const BITMAP16
* bmp
)
201 return CreateBitmap16( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
202 bmp
->bmBitsPixel
, MapSL( bmp
->bmBits
) );
206 /******************************************************************************
207 * CreateBitmapIndirect [GDI32.26] Creates a bitmap with the specifies info
210 * Success: Handle to bitmap
213 HBITMAP WINAPI
CreateBitmapIndirect(
214 const BITMAP
* bmp
) /* [in] Pointer to the bitmap data */
216 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
217 bmp
->bmBitsPixel
, bmp
->bmBits
);
221 /***********************************************************************
222 * GetBitmapBits16 (GDI.74)
224 LONG WINAPI
GetBitmapBits16( HBITMAP16 hbitmap
, LONG count
, LPVOID buffer
)
226 return GetBitmapBits( hbitmap
, count
, buffer
);
230 /***********************************************************************
231 * GetBitmapBits [GDI32.143] Copies bitmap bits of bitmap to buffer
234 * Success: Number of bytes copied
237 LONG WINAPI
GetBitmapBits(
238 HBITMAP hbitmap
, /* [in] Handle to bitmap */
239 LONG count
, /* [in] Number of bytes to copy */
240 LPVOID bits
) /* [out] Pointer to buffer to receive bits */
242 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
247 /* If the bits vector is null, the function should return the read size */
250 ret
= bmp
->bitmap
.bmWidthBytes
* bmp
->bitmap
.bmHeight
;
255 WARN("(%ld): Negative number of bytes passed???\n", count
);
259 /* Only get entire lines */
260 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
261 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
262 count
= height
* bmp
->bitmap
.bmWidthBytes
;
265 WARN("Less than one entire line requested\n");
271 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
272 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
273 1 << bmp
->bitmap
.bmBitsPixel
, height
);
277 TRACE("Calling device specific BitmapBits\n");
278 if(bmp
->funcs
->pBitmapBits
)
279 ret
= bmp
->funcs
->pBitmapBits(hbitmap
, bits
, count
, DDB_GET
);
281 ERR("BitmapBits == NULL??\n");
287 if(!bmp
->bitmap
.bmBits
) {
288 WARN("Bitmap is empty\n");
291 memcpy(bits
, bmp
->bitmap
.bmBits
, count
);
297 GDI_ReleaseObj( hbitmap
);
302 /***********************************************************************
303 * SetBitmapBits16 (GDI.106)
305 LONG WINAPI
SetBitmapBits16( HBITMAP16 hbitmap
, LONG count
, LPCVOID buffer
)
307 return SetBitmapBits( hbitmap
, count
, buffer
);
311 /******************************************************************************
312 * SetBitmapBits [GDI32.303] Sets bits of color data for a bitmap
315 * Success: Number of bytes used in setting the bitmap bits
318 LONG WINAPI
SetBitmapBits(
319 HBITMAP hbitmap
, /* [in] Handle to bitmap */
320 LONG count
, /* [in] Number of bytes in bitmap array */
321 LPCVOID bits
) /* [in] Address of array with bitmap bits */
323 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
326 if ((!bmp
) || (!bits
))
330 WARN("(%ld): Negative number of bytes passed???\n", count
);
334 /* Only get entire lines */
335 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
336 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
337 count
= height
* bmp
->bitmap
.bmWidthBytes
;
339 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
340 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
341 1 << bmp
->bitmap
.bmBitsPixel
, height
);
345 TRACE("Calling device specific BitmapBits\n");
346 if(bmp
->funcs
->pBitmapBits
)
347 ret
= bmp
->funcs
->pBitmapBits(hbitmap
, (void *) bits
, count
, DDB_SET
);
349 ERR("BitmapBits == NULL??\n");
355 if(!bmp
->bitmap
.bmBits
) /* Alloc enough for entire bitmap */
356 bmp
->bitmap
.bmBits
= HeapAlloc( GetProcessHeap(), 0, count
);
357 if(!bmp
->bitmap
.bmBits
) {
358 WARN("Unable to allocate bit buffer\n");
361 memcpy(bmp
->bitmap
.bmBits
, bits
, count
);
366 GDI_ReleaseObj( hbitmap
);
370 /**********************************************************************
374 HBITMAP
BITMAP_CopyBitmap(HBITMAP hbitmap
)
376 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
384 res
= CreateBitmapIndirect(&bm
);
387 char *buf
= HeapAlloc( GetProcessHeap(), 0, bm
.bmWidthBytes
*
389 GetBitmapBits (hbitmap
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
390 SetBitmapBits (res
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
391 HeapFree( GetProcessHeap(), 0, buf
);
394 GDI_ReleaseObj( hbitmap
);
398 /***********************************************************************
399 * BITMAP_DeleteObject
401 BOOL
BITMAP_DeleteObject( HBITMAP16 hbitmap
, BITMAPOBJ
* bmp
)
403 if (bmp
->funcs
&& bmp
->funcs
->pDeleteObject
)
404 bmp
->funcs
->pDeleteObject( hbitmap
);
406 if( bmp
->bitmap
.bmBits
)
407 HeapFree( GetProcessHeap(), 0, bmp
->bitmap
.bmBits
);
409 DIB_DeleteDIBSection( bmp
);
411 return GDI_FreeObject( hbitmap
, bmp
);
415 /***********************************************************************
418 INT16
BITMAP_GetObject16( BITMAPOBJ
* bmp
, INT16 count
, LPVOID buffer
)
422 if ( count
<= sizeof(BITMAP16
) )
424 BITMAP
*bmp32
= &bmp
->dib
->dsBm
;
426 bmp16
.bmType
= bmp32
->bmType
;
427 bmp16
.bmWidth
= bmp32
->bmWidth
;
428 bmp16
.bmHeight
= bmp32
->bmHeight
;
429 bmp16
.bmWidthBytes
= bmp32
->bmWidthBytes
;
430 bmp16
.bmPlanes
= bmp32
->bmPlanes
;
431 bmp16
.bmBitsPixel
= bmp32
->bmBitsPixel
;
432 bmp16
.bmBits
= (SEGPTR
)0;
433 memcpy( buffer
, &bmp16
, count
);
438 FIXME("not implemented for DIBs: count %d\n", count
);
445 bmp16
.bmType
= bmp
->bitmap
.bmType
;
446 bmp16
.bmWidth
= bmp
->bitmap
.bmWidth
;
447 bmp16
.bmHeight
= bmp
->bitmap
.bmHeight
;
448 bmp16
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
449 bmp16
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
450 bmp16
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
451 bmp16
.bmBits
= (SEGPTR
)0;
452 if (count
> sizeof(bmp16
)) count
= sizeof(bmp16
);
453 memcpy( buffer
, &bmp16
, count
);
459 /***********************************************************************
462 INT
BITMAP_GetObject( BITMAPOBJ
* bmp
, INT count
, LPVOID buffer
)
466 if (count
< sizeof(DIBSECTION
))
468 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
472 if (count
> sizeof(DIBSECTION
)) count
= sizeof(DIBSECTION
);
475 memcpy( buffer
, bmp
->dib
, count
);
480 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
481 memcpy( buffer
, &bmp
->bitmap
, count
);
487 /***********************************************************************
488 * CreateDiscardableBitmap16 (GDI.156)
490 HBITMAP16 WINAPI
CreateDiscardableBitmap16( HDC16 hdc
, INT16 width
,
493 return CreateCompatibleBitmap16( hdc
, width
, height
);
497 /******************************************************************************
498 * CreateDiscardableBitmap [GDI32.38] Creates a discardable bitmap
501 * Success: Handle to bitmap
504 HBITMAP WINAPI
CreateDiscardableBitmap(
505 HDC hdc
, /* [in] Handle to device context */
506 INT width
, /* [in] Bitmap width */
507 INT height
) /* [in] Bitmap height */
509 return CreateCompatibleBitmap( hdc
, width
, height
);
513 /***********************************************************************
514 * GetBitmapDimensionEx16 (GDI.468)
517 * Can this call GetBitmapDimensionEx?
519 BOOL16 WINAPI
GetBitmapDimensionEx16( HBITMAP16 hbitmap
, LPSIZE16 size
)
521 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
522 if (!bmp
) return FALSE
;
523 size
->cx
= bmp
->size
.cx
;
524 size
->cy
= bmp
->size
.cy
;
525 GDI_ReleaseObj( hbitmap
);
530 /******************************************************************************
531 * GetBitmapDimensionEx [GDI32.144] Retrieves dimensions of a bitmap
537 BOOL WINAPI
GetBitmapDimensionEx(
538 HBITMAP hbitmap
, /* [in] Handle to bitmap */
539 LPSIZE size
) /* [out] Address of struct receiving dimensions */
541 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
542 if (!bmp
) return FALSE
;
544 GDI_ReleaseObj( hbitmap
);
549 /***********************************************************************
550 * GetBitmapDimension (GDI.162)
552 DWORD WINAPI
GetBitmapDimension16( HBITMAP16 hbitmap
)
555 if (!GetBitmapDimensionEx16( hbitmap
, &size
)) return 0;
556 return MAKELONG( size
.cx
, size
.cy
);
560 /***********************************************************************
561 * SetBitmapDimensionEx16 (GDI.478)
563 BOOL16 WINAPI
SetBitmapDimensionEx16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
,
566 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
567 if (!bmp
) return FALSE
;
570 prevSize
->cx
= bmp
->size
.cx
;
571 prevSize
->cy
= bmp
->size
.cy
;
575 GDI_ReleaseObj( hbitmap
);
580 /******************************************************************************
581 * SetBitmapDimensionEx [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_ReleaseObj( 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
);