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 "wine/debug.h"
29 #include "wine/winuser16.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(bitmap
);
34 static HGDIOBJ
BITMAP_SelectObject( HGDIOBJ handle
, void *obj
, HDC hdc
);
35 static INT
BITMAP_GetObject16( HGDIOBJ handle
, void *obj
, INT count
, LPVOID buffer
);
36 static INT
BITMAP_GetObject( HGDIOBJ handle
, void *obj
, INT count
, LPVOID buffer
);
37 static BOOL
BITMAP_DeleteObject( HGDIOBJ handle
, void *obj
);
39 static const struct gdi_obj_funcs bitmap_funcs
=
41 BITMAP_SelectObject
, /* pSelectObject */
42 BITMAP_GetObject16
, /* pGetObject16 */
43 BITMAP_GetObject
, /* pGetObjectA */
44 BITMAP_GetObject
, /* pGetObjectW */
45 NULL
, /* pUnrealizeObject */
46 BITMAP_DeleteObject
/* pDeleteObject */
49 /***********************************************************************
50 * BITMAP_GetWidthBytes
52 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
55 static INT
BITMAP_GetWidthBytes( INT bmWidth
, INT bpp
)
60 return 2 * ((bmWidth
+15) >> 4);
63 bmWidth
*= 3; /* fall through */
65 return bmWidth
+ (bmWidth
& 1);
75 return 2 * ((bmWidth
+3) >> 2);
78 WARN("Unknown depth %d, please report.\n", bpp
);
84 /******************************************************************************
85 * CreateBitmap [GDI32.@] Creates a bitmap with the specified info
88 * width [I] bitmap width
89 * height [I] bitmap height
90 * planes [I] Number of color planes
91 * bpp [I] Number of bits to identify a color
92 * bits [I] Pointer to array containing color data
95 * Success: Handle to bitmap
98 HBITMAP WINAPI
CreateBitmap( INT width
, INT height
, UINT planes
,
99 UINT bpp
, LPCVOID bits
)
104 planes
= (BYTE
)planes
;
108 /* Check parameters */
109 if (!height
|| !width
)
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
,
125 (HGDIOBJ
*)&hbitmap
, &bitmap_funcs
)))
128 TRACE("%dx%d, %d colors returning %p\n", width
, height
, 1 << (planes
*bpp
), hbitmap
);
132 bmp
->bitmap
.bmType
= 0;
133 bmp
->bitmap
.bmWidth
= width
;
134 bmp
->bitmap
.bmHeight
= height
;
135 bmp
->bitmap
.bmPlanes
= planes
;
136 bmp
->bitmap
.bmBitsPixel
= bpp
;
137 bmp
->bitmap
.bmWidthBytes
= BITMAP_GetWidthBytes( width
, bpp
);
138 bmp
->bitmap
.bmBits
= NULL
;
141 bmp
->physBitmap
= NULL
;
143 bmp
->segptr_bits
= 0;
145 if (bits
) /* Set bitmap bits */
146 SetBitmapBits( hbitmap
, height
* bmp
->bitmap
.bmWidthBytes
,
148 GDI_ReleaseObj( hbitmap
);
153 /******************************************************************************
154 * CreateCompatibleBitmap [GDI32.@] Creates a bitmap compatible with the DC
157 * hdc [I] Handle to device context
158 * width [I] Width of bitmap
159 * height [I] Height of bitmap
162 * Success: Handle to bitmap
165 HBITMAP WINAPI
CreateCompatibleBitmap( HDC hdc
, INT width
, INT height
)
170 TRACE("(%p,%d,%d) = \n", hdc
, width
, height
);
171 if (!(dc
= DC_GetDCPtr( hdc
))) return 0;
172 if ((width
>= 0x10000) || (height
>= 0x10000)) {
173 FIXME("got bad width %d or height %d, please look for reason\n",
176 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
177 if (!width
|| !height
)
178 hbmpRet
= CreateBitmap( 1, 1, 1, 1, NULL
);
180 hbmpRet
= CreateBitmap( width
, height
, 1, dc
->bitsPerPixel
, NULL
);
182 TRACE("\t\t%p\n", hbmpRet
);
188 /******************************************************************************
189 * CreateBitmapIndirect [GDI32.@] Creates a bitmap with the specifies info
192 * Success: Handle to bitmap
195 HBITMAP WINAPI
CreateBitmapIndirect(
196 const BITMAP
* bmp
) /* [in] Pointer to the bitmap data */
198 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
199 bmp
->bmBitsPixel
, bmp
->bmBits
);
203 /***********************************************************************
204 * GetBitmapBits [GDI32.@] Copies bitmap bits of bitmap to buffer
207 * Success: Number of bytes copied
210 LONG WINAPI
GetBitmapBits(
211 HBITMAP hbitmap
, /* [in] Handle to bitmap */
212 LONG count
, /* [in] Number of bytes to copy */
213 LPVOID bits
) /* [out] Pointer to buffer to receive bits */
215 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
220 /* If the bits vector is null, the function should return the read size */
223 ret
= bmp
->bitmap
.bmWidthBytes
* bmp
->bitmap
.bmHeight
;
228 WARN("(%ld): Negative number of bytes passed???\n", count
);
232 /* Only get entire lines */
233 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
234 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
235 count
= height
* bmp
->bitmap
.bmWidthBytes
;
238 WARN("Less than one entire line requested\n");
244 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
245 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
246 1 << bmp
->bitmap
.bmBitsPixel
, height
);
250 TRACE("Calling device specific BitmapBits\n");
251 if(bmp
->funcs
->pGetBitmapBits
)
252 ret
= bmp
->funcs
->pGetBitmapBits(hbitmap
, bits
, count
);
255 memset( bits
, 0, count
);
260 if(!bmp
->bitmap
.bmBits
) {
261 WARN("Bitmap is empty\n");
264 memcpy(bits
, bmp
->bitmap
.bmBits
, count
);
270 GDI_ReleaseObj( hbitmap
);
275 /******************************************************************************
276 * SetBitmapBits [GDI32.@] Sets bits of color data for a bitmap
279 * Success: Number of bytes used in setting the bitmap bits
282 LONG WINAPI
SetBitmapBits(
283 HBITMAP hbitmap
, /* [in] Handle to bitmap */
284 LONG count
, /* [in] Number of bytes in bitmap array */
285 LPCVOID bits
) /* [in] Address of array with bitmap bits */
287 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
290 if ((!bmp
) || (!bits
))
294 WARN("(%ld): Negative number of bytes passed???\n", count
);
298 /* Only get entire lines */
299 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
300 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
301 count
= height
* bmp
->bitmap
.bmWidthBytes
;
303 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
304 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
305 1 << bmp
->bitmap
.bmBitsPixel
, height
);
309 TRACE("Calling device specific BitmapBits\n");
310 if(bmp
->funcs
->pSetBitmapBits
)
311 ret
= bmp
->funcs
->pSetBitmapBits(hbitmap
, bits
, count
);
316 if(!bmp
->bitmap
.bmBits
) /* Alloc enough for entire bitmap */
317 bmp
->bitmap
.bmBits
= HeapAlloc( GetProcessHeap(), 0, count
);
318 if(!bmp
->bitmap
.bmBits
) {
319 WARN("Unable to allocate bit buffer\n");
322 memcpy(bmp
->bitmap
.bmBits
, bits
, count
);
327 GDI_ReleaseObj( hbitmap
);
331 /**********************************************************************
335 HBITMAP
BITMAP_CopyBitmap(HBITMAP hbitmap
)
337 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
345 res
= CreateBitmapIndirect(&bm
);
348 char *buf
= HeapAlloc( GetProcessHeap(), 0, bm
.bmWidthBytes
*
350 GetBitmapBits (hbitmap
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
351 SetBitmapBits (res
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
352 HeapFree( GetProcessHeap(), 0, buf
);
355 GDI_ReleaseObj( hbitmap
);
360 /***********************************************************************
363 * Set the type of DC that owns the bitmap. This is used when the
364 * bitmap is selected into a device to initialize the bitmap function
367 BOOL
BITMAP_SetOwnerDC( HBITMAP hbitmap
, DC
*dc
)
372 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
373 if (hbitmap
== GetStockObject(DEFAULT_BITMAP
)) return TRUE
;
375 if (!(bitmap
= GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
))) return FALSE
;
378 if (!bitmap
->funcs
) /* not owned by a DC yet */
380 if (dc
->funcs
->pCreateBitmap
) ret
= dc
->funcs
->pCreateBitmap( dc
->physDev
, hbitmap
);
381 if (ret
) bitmap
->funcs
= dc
->funcs
;
383 else if (bitmap
->funcs
!= dc
->funcs
)
385 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap
);
388 GDI_ReleaseObj( hbitmap
);
393 /***********************************************************************
394 * BITMAP_SelectObject
396 static HGDIOBJ
BITMAP_SelectObject( HGDIOBJ handle
, void *obj
, HDC hdc
)
399 BITMAPOBJ
*bitmap
= obj
;
400 DC
*dc
= DC_GetDCPtr( hdc
);
403 if (GetObjectType( hdc
) != OBJ_MEMDC
)
405 GDI_ReleaseObj( hdc
);
409 if (handle
== dc
->hBitmap
) goto done
; /* nothing to do */
411 if (bitmap
->header
.dwCount
&& (handle
!= GetStockObject(DEFAULT_BITMAP
)))
413 WARN( "Bitmap already selected in another DC\n" );
414 GDI_ReleaseObj( hdc
);
418 if (!bitmap
->funcs
&& !BITMAP_SetOwnerDC( handle
, dc
))
420 GDI_ReleaseObj( hdc
);
424 if (dc
->funcs
->pSelectBitmap
) handle
= dc
->funcs
->pSelectBitmap( dc
->physDev
, handle
);
428 dc
->hBitmap
= handle
;
429 dc
->totalExtent
.left
= 0;
430 dc
->totalExtent
.top
= 0;
431 dc
->totalExtent
.right
= bitmap
->bitmap
.bmWidth
;
432 dc
->totalExtent
.bottom
= bitmap
->bitmap
.bmHeight
;
433 dc
->flags
&= ~DC_DIRTY
;
434 SetRectRgn( dc
->hVisRgn
, 0, 0, bitmap
->bitmap
.bmWidth
, bitmap
->bitmap
.bmHeight
);
435 CLIPPING_UpdateGCRegion( dc
);
437 if (dc
->bitsPerPixel
!= bitmap
->bitmap
.bmBitsPixel
)
439 /* depth changed, reinitialize the DC */
440 dc
->bitsPerPixel
= bitmap
->bitmap
.bmBitsPixel
;
447 GDI_ReleaseObj( hdc
);
452 /***********************************************************************
453 * BITMAP_DeleteObject
455 static BOOL
BITMAP_DeleteObject( HGDIOBJ handle
, void *obj
)
457 BITMAPOBJ
* bmp
= obj
;
459 if (bmp
->funcs
&& bmp
->funcs
->pDeleteBitmap
)
460 bmp
->funcs
->pDeleteBitmap( handle
);
462 if( bmp
->bitmap
.bmBits
)
463 HeapFree( GetProcessHeap(), 0, bmp
->bitmap
.bmBits
);
467 DIBSECTION
*dib
= bmp
->dib
;
469 if (dib
->dsBm
.bmBits
)
473 SYSTEM_INFO SystemInfo
;
474 GetSystemInfo( &SystemInfo
);
475 UnmapViewOfFile( (char *)dib
->dsBm
.bmBits
-
476 (dib
->dsOffset
% SystemInfo
.dwAllocationGranularity
) );
478 else if (!dib
->dsOffset
)
479 VirtualFree(dib
->dsBm
.bmBits
, 0L, MEM_RELEASE
);
481 HeapFree(GetProcessHeap(), 0, dib
);
483 if (bmp
->segptr_bits
)
484 { /* free its selector array */
485 WORD sel
= SELECTOROF(bmp
->segptr_bits
);
486 WORD count
= (GetSelectorLimit16(sel
) / 0x10000) + 1;
489 for (i
= 0; i
< count
; i
++) FreeSelector16(sel
+ (i
<< __AHSHIFT
));
492 return GDI_FreeObject( handle
, obj
);
496 /***********************************************************************
499 static INT
BITMAP_GetObject16( HGDIOBJ handle
, void *obj
, INT count
, LPVOID buffer
)
501 BITMAPOBJ
*bmp
= obj
;
505 if ( count
<= sizeof(BITMAP16
) )
507 BITMAP
*bmp32
= &bmp
->dib
->dsBm
;
509 bmp16
.bmType
= bmp32
->bmType
;
510 bmp16
.bmWidth
= bmp32
->bmWidth
;
511 bmp16
.bmHeight
= bmp32
->bmHeight
;
512 bmp16
.bmWidthBytes
= bmp32
->bmWidthBytes
;
513 bmp16
.bmPlanes
= bmp32
->bmPlanes
;
514 bmp16
.bmBitsPixel
= bmp32
->bmBitsPixel
;
515 bmp16
.bmBits
= (SEGPTR
)0;
516 memcpy( buffer
, &bmp16
, count
);
521 FIXME("not implemented for DIBs: count %d\n", count
);
528 bmp16
.bmType
= bmp
->bitmap
.bmType
;
529 bmp16
.bmWidth
= bmp
->bitmap
.bmWidth
;
530 bmp16
.bmHeight
= bmp
->bitmap
.bmHeight
;
531 bmp16
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
532 bmp16
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
533 bmp16
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
534 bmp16
.bmBits
= (SEGPTR
)0;
535 if (count
> sizeof(bmp16
)) count
= sizeof(bmp16
);
536 memcpy( buffer
, &bmp16
, count
);
542 /***********************************************************************
545 static INT
BITMAP_GetObject( HGDIOBJ handle
, void *obj
, INT count
, LPVOID buffer
)
547 BITMAPOBJ
*bmp
= obj
;
552 return sizeof(DIBSECTION
);
553 if (count
< sizeof(DIBSECTION
))
555 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
559 if (count
> sizeof(DIBSECTION
)) count
= sizeof(DIBSECTION
);
562 memcpy( buffer
, bmp
->dib
, count
);
568 return sizeof(BITMAP
);
569 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
570 memcpy( buffer
, &bmp
->bitmap
, count
);
576 /******************************************************************************
577 * CreateDiscardableBitmap [GDI32.@] Creates a discardable bitmap
580 * Success: Handle to bitmap
583 HBITMAP WINAPI
CreateDiscardableBitmap(
584 HDC hdc
, /* [in] Handle to device context */
585 INT width
, /* [in] Bitmap width */
586 INT height
) /* [in] Bitmap height */
588 return CreateCompatibleBitmap( hdc
, width
, height
);
592 /******************************************************************************
593 * GetBitmapDimensionEx [GDI32.@] Retrieves dimensions of a bitmap
599 BOOL WINAPI
GetBitmapDimensionEx(
600 HBITMAP hbitmap
, /* [in] Handle to bitmap */
601 LPSIZE size
) /* [out] Address of struct receiving dimensions */
603 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
604 if (!bmp
) return FALSE
;
606 GDI_ReleaseObj( hbitmap
);
611 /******************************************************************************
612 * SetBitmapDimensionEx [GDI32.@] Assignes dimensions to a bitmap
618 BOOL WINAPI
SetBitmapDimensionEx(
619 HBITMAP hbitmap
, /* [in] Handle to bitmap */
620 INT x
, /* [in] Bitmap width */
621 INT y
, /* [in] Bitmap height */
622 LPSIZE prevSize
) /* [out] Address of structure for orig dims */
624 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
625 if (!bmp
) return FALSE
;
626 if (prevSize
) *prevSize
= bmp
->size
;
629 GDI_ReleaseObj( hbitmap
);