4 * Copyright 1993 Alexandre Julliard
11 #include "wine/winbase16.h"
16 #include "debugtools.h"
17 #include "wine/winuser16.h"
19 DEFAULT_DEBUG_CHANNEL(bitmap
);
21 BITMAP_DRIVER
*BITMAP_Driver
= NULL
;
24 /***********************************************************************
25 * BITMAP_GetWidthBytes
27 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
30 INT
BITMAP_GetWidthBytes( INT bmWidth
, INT bpp
)
35 return 2 * ((bmWidth
+15) >> 4);
38 bmWidth
*= 3; /* fall through */
40 return bmWidth
+ (bmWidth
& 1);
50 return 2 * ((bmWidth
+3) >> 2);
53 WARN("Unknown depth %d, please report.\n", bpp
);
58 /***********************************************************************
59 * CreateUserBitmap16 (GDI.407)
61 HBITMAP16 WINAPI
CreateUserBitmap16( INT16 width
, INT16 height
, UINT16 planes
,
62 UINT16 bpp
, LPCVOID bits
)
64 return CreateBitmap16( width
, height
, planes
, bpp
, bits
);
67 /***********************************************************************
68 * CreateUserDiscardableBitmap16 (GDI.409)
70 HBITMAP16 WINAPI
CreateUserDiscardableBitmap16( WORD dummy
,
71 INT16 width
, INT16 height
)
73 HDC hdc
= CreateDCA( "DISPLAY", NULL
, NULL
, NULL
);
74 HBITMAP16 ret
= CreateCompatibleBitmap16( hdc
, width
, height
);
80 /***********************************************************************
81 * CreateBitmap16 (GDI.48)
83 HBITMAP16 WINAPI
CreateBitmap16( INT16 width
, INT16 height
, UINT16 planes
,
84 UINT16 bpp
, LPCVOID bits
)
86 return CreateBitmap( width
, height
, planes
, bpp
, bits
);
90 /******************************************************************************
91 * CreateBitmap [GDI32.25] Creates a bitmap with the specified info
94 * width [I] bitmap width
95 * height [I] bitmap height
96 * planes [I] Number of color planes
97 * bpp [I] Number of bits to identify a color
98 * bits [I] Pointer to array containing color data
101 * Success: Handle to bitmap
104 HBITMAP WINAPI
CreateBitmap( INT width
, INT height
, UINT planes
,
105 UINT bpp
, LPCVOID bits
)
110 planes
= (BYTE
)planes
;
114 /* Check parameters */
115 if (!height
|| !width
) return 0;
117 FIXME("planes = %d\n", planes
);
120 if (height
< 0) height
= -height
;
121 if (width
< 0) width
= -width
;
123 /* Create the BITMAPOBJ */
124 if (!(bmp
= GDI_AllocObject( sizeof(BITMAPOBJ
), BITMAP_MAGIC
, &hbitmap
))) return 0;
126 TRACE("%dx%d, %d colors returning %08x\n", width
, height
,
127 1 << (planes
*bpp
), hbitmap
);
131 bmp
->bitmap
.bmType
= 0;
132 bmp
->bitmap
.bmWidth
= width
;
133 bmp
->bitmap
.bmHeight
= height
;
134 bmp
->bitmap
.bmPlanes
= planes
;
135 bmp
->bitmap
.bmBitsPixel
= bpp
;
136 bmp
->bitmap
.bmWidthBytes
= BITMAP_GetWidthBytes( width
, bpp
);
137 bmp
->bitmap
.bmBits
= NULL
;
140 bmp
->physBitmap
= NULL
;
143 if (bits
) /* Set bitmap bits */
144 SetBitmapBits( hbitmap
, height
* bmp
->bitmap
.bmWidthBytes
,
146 GDI_ReleaseObj( hbitmap
);
151 /***********************************************************************
152 * CreateCompatibleBitmap16 (GDI.51)
154 HBITMAP16 WINAPI
CreateCompatibleBitmap16(HDC16 hdc
, INT16 width
, INT16 height
)
156 return CreateCompatibleBitmap( hdc
, width
, height
);
160 /******************************************************************************
161 * CreateCompatibleBitmap [GDI32.30] Creates a bitmap compatible with the DC
164 * hdc [I] Handle to device context
165 * width [I] Width of bitmap
166 * height [I] Height of bitmap
169 * Success: Handle to bitmap
172 HBITMAP WINAPI
CreateCompatibleBitmap( HDC hdc
, INT width
, INT height
)
177 TRACE("(%04x,%d,%d) = \n", hdc
, width
, height
);
178 if (!(dc
= DC_GetDCPtr( hdc
))) return 0;
179 if ((width
>= 0x10000) || (height
>= 0x10000)) {
180 FIXME("got bad width %d or height %d, please look for reason\n",
183 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
184 if (!width
|| !height
)
185 hbmpRet
= CreateBitmap( 1, 1, 1, 1, NULL
);
187 hbmpRet
= CreateBitmap( width
, height
, 1, dc
->bitsPerPixel
, NULL
);
188 if(dc
->funcs
->pCreateBitmap
)
189 dc
->funcs
->pCreateBitmap( hbmpRet
);
191 TRACE("\t\t%04x\n", hbmpRet
);
197 /***********************************************************************
198 * CreateBitmapIndirect16 (GDI.49)
200 HBITMAP16 WINAPI
CreateBitmapIndirect16( const BITMAP16
* bmp
)
202 return CreateBitmap16( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
203 bmp
->bmBitsPixel
, PTR_SEG_TO_LIN( bmp
->bmBits
) );
207 /******************************************************************************
208 * CreateBitmapIndirect [GDI32.26] Creates a bitmap with the specifies info
211 * Success: Handle to bitmap
214 HBITMAP WINAPI
CreateBitmapIndirect(
215 const BITMAP
* bmp
) /* [in] Pointer to the bitmap data */
217 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
218 bmp
->bmBitsPixel
, bmp
->bmBits
);
222 /***********************************************************************
223 * GetBitmapBits16 (GDI.74)
225 LONG WINAPI
GetBitmapBits16( HBITMAP16 hbitmap
, LONG count
, LPVOID buffer
)
227 return GetBitmapBits( hbitmap
, count
, buffer
);
231 /***********************************************************************
232 * GetBitmapBits [GDI32.143] Copies bitmap bits of bitmap to buffer
235 * Success: Number of bytes copied
238 LONG WINAPI
GetBitmapBits(
239 HBITMAP hbitmap
, /* [in] Handle to bitmap */
240 LONG count
, /* [in] Number of bytes to copy */
241 LPVOID bits
) /* [out] Pointer to buffer to receive bits */
243 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
248 /* If the bits vector is null, the function should return the read size */
251 ret
= 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");
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
->funcs
->pBitmapBits
)
280 ret
= bmp
->funcs
->pBitmapBits(hbitmap
, bits
, count
, DDB_GET
);
282 ERR("BitmapBits == NULL??\n");
288 if(!bmp
->bitmap
.bmBits
) {
289 WARN("Bitmap is empty\n");
292 memcpy(bits
, bmp
->bitmap
.bmBits
, count
);
298 GDI_ReleaseObj( hbitmap
);
303 /***********************************************************************
304 * SetBitmapBits16 (GDI.106)
306 LONG WINAPI
SetBitmapBits16( HBITMAP16 hbitmap
, LONG count
, LPCVOID buffer
)
308 return SetBitmapBits( hbitmap
, count
, buffer
);
312 /******************************************************************************
313 * SetBitmapBits [GDI32.303] Sets bits of color data for a bitmap
316 * Success: Number of bytes used in setting the bitmap bits
319 LONG WINAPI
SetBitmapBits(
320 HBITMAP hbitmap
, /* [in] Handle to bitmap */
321 LONG count
, /* [in] Number of bytes in bitmap array */
322 LPCVOID bits
) /* [in] Address of array with bitmap bits */
324 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
327 if ((!bmp
) || (!bits
))
331 WARN("(%ld): Negative number of bytes passed???\n", count
);
335 /* Only get entire lines */
336 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
337 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
338 count
= height
* bmp
->bitmap
.bmWidthBytes
;
340 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
341 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
342 1 << bmp
->bitmap
.bmBitsPixel
, height
);
346 TRACE("Calling device specific BitmapBits\n");
347 if(bmp
->funcs
->pBitmapBits
)
348 ret
= bmp
->funcs
->pBitmapBits(hbitmap
, (void *) bits
, count
, DDB_SET
);
350 ERR("BitmapBits == NULL??\n");
356 if(!bmp
->bitmap
.bmBits
) /* Alloc enough for entire bitmap */
357 bmp
->bitmap
.bmBits
= HeapAlloc( GetProcessHeap(), 0, count
);
358 if(!bmp
->bitmap
.bmBits
) {
359 WARN("Unable to allocate bit buffer\n");
362 memcpy(bmp
->bitmap
.bmBits
, bits
, count
);
367 GDI_ReleaseObj( hbitmap
);
371 /**********************************************************************
375 HBITMAP
BITMAP_CopyBitmap(HBITMAP hbitmap
)
377 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
385 res
= CreateBitmapIndirect(&bm
);
388 char *buf
= HeapAlloc( GetProcessHeap(), 0, bm
.bmWidthBytes
*
390 GetBitmapBits (hbitmap
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
391 SetBitmapBits (res
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
392 HeapFree( GetProcessHeap(), 0, buf
);
395 GDI_ReleaseObj( hbitmap
);
399 /***********************************************************************
400 * BITMAP_DeleteObject
402 BOOL
BITMAP_DeleteObject( HBITMAP16 hbitmap
, BITMAPOBJ
* bmp
)
404 if (bmp
->funcs
&& bmp
->funcs
->pDeleteObject
)
405 bmp
->funcs
->pDeleteObject( hbitmap
);
407 if( bmp
->bitmap
.bmBits
)
408 HeapFree( GetProcessHeap(), 0, bmp
->bitmap
.bmBits
);
410 DIB_DeleteDIBSection( bmp
);
412 return GDI_FreeObject( hbitmap
, bmp
);
416 /***********************************************************************
419 INT16
BITMAP_GetObject16( BITMAPOBJ
* bmp
, INT16 count
, LPVOID buffer
)
423 if ( count
<= sizeof(BITMAP16
) )
425 BITMAP
*bmp32
= &bmp
->dib
->dsBm
;
427 bmp16
.bmType
= bmp32
->bmType
;
428 bmp16
.bmWidth
= bmp32
->bmWidth
;
429 bmp16
.bmHeight
= bmp32
->bmHeight
;
430 bmp16
.bmWidthBytes
= bmp32
->bmWidthBytes
;
431 bmp16
.bmPlanes
= bmp32
->bmPlanes
;
432 bmp16
.bmBitsPixel
= bmp32
->bmBitsPixel
;
433 bmp16
.bmBits
= (SEGPTR
)0;
434 memcpy( buffer
, &bmp16
, count
);
439 FIXME("not implemented for DIBs: count %d\n", count
);
446 bmp16
.bmType
= bmp
->bitmap
.bmType
;
447 bmp16
.bmWidth
= bmp
->bitmap
.bmWidth
;
448 bmp16
.bmHeight
= bmp
->bitmap
.bmHeight
;
449 bmp16
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
450 bmp16
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
451 bmp16
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
452 bmp16
.bmBits
= (SEGPTR
)0;
453 if (count
> sizeof(bmp16
)) count
= sizeof(bmp16
);
454 memcpy( buffer
, &bmp16
, count
);
460 /***********************************************************************
463 INT
BITMAP_GetObject( BITMAPOBJ
* bmp
, INT count
, LPVOID buffer
)
467 if (count
< sizeof(DIBSECTION
))
469 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
473 if (count
> sizeof(DIBSECTION
)) count
= sizeof(DIBSECTION
);
476 memcpy( buffer
, bmp
->dib
, count
);
481 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
482 memcpy( buffer
, &bmp
->bitmap
, count
);
488 /***********************************************************************
489 * CreateDiscardableBitmap16 (GDI.156)
491 HBITMAP16 WINAPI
CreateDiscardableBitmap16( HDC16 hdc
, INT16 width
,
494 return CreateCompatibleBitmap16( hdc
, width
, height
);
498 /******************************************************************************
499 * CreateDiscardableBitmap [GDI32.38] Creates a discardable bitmap
502 * Success: Handle to bitmap
505 HBITMAP WINAPI
CreateDiscardableBitmap(
506 HDC hdc
, /* [in] Handle to device context */
507 INT width
, /* [in] Bitmap width */
508 INT height
) /* [in] Bitmap height */
510 return CreateCompatibleBitmap( hdc
, width
, height
);
514 /***********************************************************************
515 * GetBitmapDimensionEx16 (GDI.468)
518 * Can this call GetBitmapDimensionEx?
520 BOOL16 WINAPI
GetBitmapDimensionEx16( HBITMAP16 hbitmap
, LPSIZE16 size
)
522 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
523 if (!bmp
) return FALSE
;
524 CONV_SIZE32TO16( &bmp
->size
, size
);
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
;
568 if (prevSize
) CONV_SIZE32TO16( &bmp
->size
, prevSize
);
571 GDI_ReleaseObj( hbitmap
);
576 /******************************************************************************
577 * SetBitmapDimensionEx [GDI32.304] Assignes dimensions to a bitmap
583 BOOL WINAPI
SetBitmapDimensionEx(
584 HBITMAP hbitmap
, /* [in] Handle to bitmap */
585 INT x
, /* [in] Bitmap width */
586 INT y
, /* [in] Bitmap height */
587 LPSIZE prevSize
) /* [out] Address of structure for orig dims */
589 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
590 if (!bmp
) return FALSE
;
591 if (prevSize
) *prevSize
= bmp
->size
;
594 GDI_ReleaseObj( hbitmap
);
599 /***********************************************************************
600 * SetBitmapDimension (GDI.163)
602 DWORD WINAPI
SetBitmapDimension16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
)
605 if (!SetBitmapDimensionEx16( hbitmap
, x
, y
, &size
)) return 0;
606 return MAKELONG( size
.cx
, size
.cy
);