4 * Copyright 1993 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/winbase16.h"
28 #include "selectors.h"
29 #include "wine/debug.h"
30 #include "wine/winuser16.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(bitmap
);
35 /***********************************************************************
36 * BITMAP_GetWidthBytes
38 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
41 INT
BITMAP_GetWidthBytes( INT bmWidth
, INT bpp
)
46 return 2 * ((bmWidth
+15) >> 4);
49 bmWidth
*= 3; /* fall through */
51 return bmWidth
+ (bmWidth
& 1);
61 return 2 * ((bmWidth
+3) >> 2);
64 WARN("Unknown depth %d, please report.\n", bpp
);
69 /***********************************************************************
70 * CreateUserBitmap (GDI.407)
72 HBITMAP16 WINAPI
CreateUserBitmap16( INT16 width
, INT16 height
, UINT16 planes
,
73 UINT16 bpp
, LPCVOID bits
)
75 return CreateBitmap16( width
, height
, planes
, bpp
, bits
);
78 /***********************************************************************
79 * CreateUserDiscardableBitmap (GDI.409)
81 HBITMAP16 WINAPI
CreateUserDiscardableBitmap16( WORD dummy
,
82 INT16 width
, INT16 height
)
84 HDC hdc
= CreateDCA( "DISPLAY", NULL
, NULL
, NULL
);
85 HBITMAP16 ret
= CreateCompatibleBitmap16( hdc
, width
, height
);
91 /***********************************************************************
92 * CreateBitmap (GDI.48)
94 HBITMAP16 WINAPI
CreateBitmap16( INT16 width
, INT16 height
, UINT16 planes
,
95 UINT16 bpp
, LPCVOID bits
)
97 return CreateBitmap( width
, height
, planes
, bpp
, bits
);
101 /******************************************************************************
102 * CreateBitmap [GDI32.@] Creates a bitmap with the specified info
105 * width [I] bitmap width
106 * height [I] bitmap height
107 * planes [I] Number of color planes
108 * bpp [I] Number of bits to identify a color
109 * bits [I] Pointer to array containing color data
112 * Success: Handle to bitmap
115 HBITMAP WINAPI
CreateBitmap( INT width
, INT height
, UINT planes
,
116 UINT bpp
, LPCVOID bits
)
121 planes
= (BYTE
)planes
;
125 /* Check parameters */
126 if (!height
|| !width
)
134 FIXME("planes = %d\n", planes
);
137 if (height
< 0) height
= -height
;
138 if (width
< 0) width
= -width
;
140 /* Create the BITMAPOBJ */
141 if (!(bmp
= GDI_AllocObject( sizeof(BITMAPOBJ
), BITMAP_MAGIC
, &hbitmap
))) return 0;
143 TRACE("%dx%d, %d colors returning %08x\n", width
, height
,
144 1 << (planes
*bpp
), hbitmap
);
148 bmp
->bitmap
.bmType
= 0;
149 bmp
->bitmap
.bmWidth
= width
;
150 bmp
->bitmap
.bmHeight
= height
;
151 bmp
->bitmap
.bmPlanes
= planes
;
152 bmp
->bitmap
.bmBitsPixel
= bpp
;
153 bmp
->bitmap
.bmWidthBytes
= BITMAP_GetWidthBytes( width
, bpp
);
154 bmp
->bitmap
.bmBits
= NULL
;
157 bmp
->physBitmap
= NULL
;
159 bmp
->segptr_bits
= 0;
161 if (bits
) /* Set bitmap bits */
162 SetBitmapBits( hbitmap
, height
* bmp
->bitmap
.bmWidthBytes
,
164 GDI_ReleaseObj( hbitmap
);
169 /***********************************************************************
170 * CreateCompatibleBitmap (GDI.51)
172 HBITMAP16 WINAPI
CreateCompatibleBitmap16(HDC16 hdc
, INT16 width
, INT16 height
)
174 return CreateCompatibleBitmap( hdc
, width
, height
);
178 /******************************************************************************
179 * CreateCompatibleBitmap [GDI32.@] Creates a bitmap compatible with the DC
182 * hdc [I] Handle to device context
183 * width [I] Width of bitmap
184 * height [I] Height of bitmap
187 * Success: Handle to bitmap
190 HBITMAP WINAPI
CreateCompatibleBitmap( HDC hdc
, INT width
, INT height
)
195 TRACE("(%04x,%d,%d) = \n", hdc
, width
, height
);
196 if (!(dc
= DC_GetDCPtr( hdc
))) return 0;
197 if ((width
>= 0x10000) || (height
>= 0x10000)) {
198 FIXME("got bad width %d or height %d, please look for reason\n",
201 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
202 if (!width
|| !height
)
203 hbmpRet
= CreateBitmap( 1, 1, 1, 1, NULL
);
205 hbmpRet
= CreateBitmap( width
, height
, 1, dc
->bitsPerPixel
, NULL
);
206 if(dc
->funcs
->pCreateBitmap
)
207 dc
->funcs
->pCreateBitmap( hbmpRet
);
209 TRACE("\t\t%04x\n", hbmpRet
);
215 /***********************************************************************
216 * CreateBitmapIndirect (GDI.49)
218 HBITMAP16 WINAPI
CreateBitmapIndirect16( const BITMAP16
* bmp
)
220 return CreateBitmap16( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
221 bmp
->bmBitsPixel
, MapSL( bmp
->bmBits
) );
225 /******************************************************************************
226 * CreateBitmapIndirect [GDI32.@] Creates a bitmap with the specifies info
229 * Success: Handle to bitmap
232 HBITMAP WINAPI
CreateBitmapIndirect(
233 const BITMAP
* bmp
) /* [in] Pointer to the bitmap data */
235 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
236 bmp
->bmBitsPixel
, bmp
->bmBits
);
240 /***********************************************************************
241 * GetBitmapBits (GDI.74)
243 LONG WINAPI
GetBitmapBits16( HBITMAP16 hbitmap
, LONG count
, LPVOID buffer
)
245 return GetBitmapBits( hbitmap
, count
, buffer
);
249 /***********************************************************************
250 * GetBitmapBits [GDI32.@] Copies bitmap bits of bitmap to buffer
253 * Success: Number of bytes copied
256 LONG WINAPI
GetBitmapBits(
257 HBITMAP hbitmap
, /* [in] Handle to bitmap */
258 LONG count
, /* [in] Number of bytes to copy */
259 LPVOID bits
) /* [out] Pointer to buffer to receive bits */
261 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
266 /* If the bits vector is null, the function should return the read size */
269 ret
= bmp
->bitmap
.bmWidthBytes
* bmp
->bitmap
.bmHeight
;
274 WARN("(%ld): Negative number of bytes passed???\n", count
);
278 /* Only get entire lines */
279 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
280 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
281 count
= height
* bmp
->bitmap
.bmWidthBytes
;
284 WARN("Less than one entire line requested\n");
290 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
291 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
292 1 << bmp
->bitmap
.bmBitsPixel
, height
);
296 TRACE("Calling device specific BitmapBits\n");
297 if(bmp
->funcs
->pBitmapBits
)
298 ret
= bmp
->funcs
->pBitmapBits(hbitmap
, bits
, count
, DDB_GET
);
300 ERR("BitmapBits == NULL??\n");
306 if(!bmp
->bitmap
.bmBits
) {
307 WARN("Bitmap is empty\n");
310 memcpy(bits
, bmp
->bitmap
.bmBits
, count
);
316 GDI_ReleaseObj( hbitmap
);
321 /***********************************************************************
322 * SetBitmapBits (GDI.106)
324 LONG WINAPI
SetBitmapBits16( HBITMAP16 hbitmap
, LONG count
, LPCVOID buffer
)
326 return SetBitmapBits( hbitmap
, count
, buffer
);
330 /******************************************************************************
331 * SetBitmapBits [GDI32.@] Sets bits of color data for a bitmap
334 * Success: Number of bytes used in setting the bitmap bits
337 LONG WINAPI
SetBitmapBits(
338 HBITMAP hbitmap
, /* [in] Handle to bitmap */
339 LONG count
, /* [in] Number of bytes in bitmap array */
340 LPCVOID bits
) /* [in] Address of array with bitmap bits */
342 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
345 if ((!bmp
) || (!bits
))
349 WARN("(%ld): Negative number of bytes passed???\n", count
);
353 /* Only get entire lines */
354 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
355 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
356 count
= height
* bmp
->bitmap
.bmWidthBytes
;
358 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
359 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
360 1 << bmp
->bitmap
.bmBitsPixel
, height
);
364 TRACE("Calling device specific BitmapBits\n");
365 if(bmp
->funcs
->pBitmapBits
)
366 ret
= bmp
->funcs
->pBitmapBits(hbitmap
, (void *) bits
, count
, DDB_SET
);
368 ERR("BitmapBits == NULL??\n");
374 if(!bmp
->bitmap
.bmBits
) /* Alloc enough for entire bitmap */
375 bmp
->bitmap
.bmBits
= HeapAlloc( GetProcessHeap(), 0, count
);
376 if(!bmp
->bitmap
.bmBits
) {
377 WARN("Unable to allocate bit buffer\n");
380 memcpy(bmp
->bitmap
.bmBits
, bits
, count
);
385 GDI_ReleaseObj( hbitmap
);
389 /**********************************************************************
393 HBITMAP
BITMAP_CopyBitmap(HBITMAP hbitmap
)
395 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
403 res
= CreateBitmapIndirect(&bm
);
406 char *buf
= HeapAlloc( GetProcessHeap(), 0, bm
.bmWidthBytes
*
408 GetBitmapBits (hbitmap
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
409 SetBitmapBits (res
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
410 HeapFree( GetProcessHeap(), 0, buf
);
413 GDI_ReleaseObj( hbitmap
);
417 /***********************************************************************
418 * BITMAP_DeleteObject
420 BOOL
BITMAP_DeleteObject( HBITMAP16 hbitmap
, BITMAPOBJ
* bmp
)
422 if (bmp
->funcs
&& bmp
->funcs
->pDeleteObject
)
423 bmp
->funcs
->pDeleteObject( hbitmap
);
425 if( bmp
->bitmap
.bmBits
)
426 HeapFree( GetProcessHeap(), 0, bmp
->bitmap
.bmBits
);
430 DIBSECTION
*dib
= bmp
->dib
;
432 if (dib
->dsBm
.bmBits
)
436 SYSTEM_INFO SystemInfo
;
437 GetSystemInfo( &SystemInfo
);
438 UnmapViewOfFile( (char *)dib
->dsBm
.bmBits
-
439 (dib
->dsOffset
% SystemInfo
.dwAllocationGranularity
) );
441 else if (!dib
->dsOffset
)
442 VirtualFree(dib
->dsBm
.bmBits
, 0L, MEM_RELEASE
);
444 HeapFree(GetProcessHeap(), 0, dib
);
446 if (bmp
->segptr_bits
)
447 { /* free its selector array */
448 WORD sel
= SELECTOROF(bmp
->segptr_bits
);
449 WORD count
= (GetSelectorLimit16(sel
) / 0x10000) + 1;
452 for (i
= 0; i
< count
; i
++) FreeSelector16(sel
+ (i
<< __AHSHIFT
));
455 return GDI_FreeObject( hbitmap
, bmp
);
459 /***********************************************************************
462 INT16
BITMAP_GetObject16( BITMAPOBJ
* bmp
, INT16 count
, LPVOID buffer
)
466 if ( count
<= sizeof(BITMAP16
) )
468 BITMAP
*bmp32
= &bmp
->dib
->dsBm
;
470 bmp16
.bmType
= bmp32
->bmType
;
471 bmp16
.bmWidth
= bmp32
->bmWidth
;
472 bmp16
.bmHeight
= bmp32
->bmHeight
;
473 bmp16
.bmWidthBytes
= bmp32
->bmWidthBytes
;
474 bmp16
.bmPlanes
= bmp32
->bmPlanes
;
475 bmp16
.bmBitsPixel
= bmp32
->bmBitsPixel
;
476 bmp16
.bmBits
= (SEGPTR
)0;
477 memcpy( buffer
, &bmp16
, count
);
482 FIXME("not implemented for DIBs: count %d\n", count
);
489 bmp16
.bmType
= bmp
->bitmap
.bmType
;
490 bmp16
.bmWidth
= bmp
->bitmap
.bmWidth
;
491 bmp16
.bmHeight
= bmp
->bitmap
.bmHeight
;
492 bmp16
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
493 bmp16
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
494 bmp16
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
495 bmp16
.bmBits
= (SEGPTR
)0;
496 if (count
> sizeof(bmp16
)) count
= sizeof(bmp16
);
497 memcpy( buffer
, &bmp16
, count
);
503 /***********************************************************************
506 INT
BITMAP_GetObject( BITMAPOBJ
* bmp
, INT count
, LPVOID buffer
)
510 if (count
< sizeof(DIBSECTION
))
512 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
516 if (count
> sizeof(DIBSECTION
)) count
= sizeof(DIBSECTION
);
519 memcpy( buffer
, bmp
->dib
, count
);
524 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
525 memcpy( buffer
, &bmp
->bitmap
, count
);
531 /***********************************************************************
532 * CreateDiscardableBitmap (GDI.156)
534 HBITMAP16 WINAPI
CreateDiscardableBitmap16( HDC16 hdc
, INT16 width
,
537 return CreateCompatibleBitmap16( hdc
, width
, height
);
541 /******************************************************************************
542 * CreateDiscardableBitmap [GDI32.@] Creates a discardable bitmap
545 * Success: Handle to bitmap
548 HBITMAP WINAPI
CreateDiscardableBitmap(
549 HDC hdc
, /* [in] Handle to device context */
550 INT width
, /* [in] Bitmap width */
551 INT height
) /* [in] Bitmap height */
553 return CreateCompatibleBitmap( hdc
, width
, height
);
557 /***********************************************************************
558 * GetBitmapDimensionEx (GDI.468)
561 * Can this call GetBitmapDimensionEx?
563 BOOL16 WINAPI
GetBitmapDimensionEx16( HBITMAP16 hbitmap
, LPSIZE16 size
)
565 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
566 if (!bmp
) return FALSE
;
567 size
->cx
= bmp
->size
.cx
;
568 size
->cy
= bmp
->size
.cy
;
569 GDI_ReleaseObj( hbitmap
);
574 /******************************************************************************
575 * GetBitmapDimensionEx [GDI32.@] Retrieves dimensions of a bitmap
581 BOOL WINAPI
GetBitmapDimensionEx(
582 HBITMAP hbitmap
, /* [in] Handle to bitmap */
583 LPSIZE size
) /* [out] Address of struct receiving dimensions */
585 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
586 if (!bmp
) return FALSE
;
588 GDI_ReleaseObj( hbitmap
);
593 /***********************************************************************
594 * GetBitmapDimension (GDI.162)
596 DWORD WINAPI
GetBitmapDimension16( HBITMAP16 hbitmap
)
599 if (!GetBitmapDimensionEx16( hbitmap
, &size
)) return 0;
600 return MAKELONG( size
.cx
, size
.cy
);
604 /***********************************************************************
605 * SetBitmapDimensionEx (GDI.478)
607 BOOL16 WINAPI
SetBitmapDimensionEx16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
,
610 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
611 if (!bmp
) return FALSE
;
614 prevSize
->cx
= bmp
->size
.cx
;
615 prevSize
->cy
= bmp
->size
.cy
;
619 GDI_ReleaseObj( hbitmap
);
624 /******************************************************************************
625 * SetBitmapDimensionEx [GDI32.@] Assignes dimensions to a bitmap
631 BOOL WINAPI
SetBitmapDimensionEx(
632 HBITMAP hbitmap
, /* [in] Handle to bitmap */
633 INT x
, /* [in] Bitmap width */
634 INT y
, /* [in] Bitmap height */
635 LPSIZE prevSize
) /* [out] Address of structure for orig dims */
637 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
638 if (!bmp
) return FALSE
;
639 if (prevSize
) *prevSize
= bmp
->size
;
642 GDI_ReleaseObj( hbitmap
);
647 /***********************************************************************
648 * SetBitmapDimension (GDI.163)
650 DWORD WINAPI
SetBitmapDimension16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
)
653 if (!SetBitmapDimensionEx16( hbitmap
, x
, y
, &size
)) return 0;
654 return MAKELONG( size
.cx
, size
.cy
);