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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "gdi_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(bitmap
);
35 static HGDIOBJ
BITMAP_SelectObject( HGDIOBJ handle
, HDC hdc
);
36 static INT
BITMAP_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
);
37 static BOOL
BITMAP_DeleteObject( HGDIOBJ handle
);
39 static const struct gdi_obj_funcs bitmap_funcs
=
41 BITMAP_SelectObject
, /* pSelectObject */
42 BITMAP_GetObject
, /* pGetObjectA */
43 BITMAP_GetObject
, /* pGetObjectW */
44 NULL
, /* pUnrealizeObject */
45 BITMAP_DeleteObject
/* pDeleteObject */
48 /***********************************************************************
49 * BITMAP_GetWidthBytes
51 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
54 INT
BITMAP_GetWidthBytes( INT bmWidth
, INT bpp
)
59 return 2 * ((bmWidth
+15) >> 4);
62 bmWidth
*= 3; /* fall through */
64 return bmWidth
+ (bmWidth
& 1);
74 return 2 * ((bmWidth
+3) >> 2);
77 WARN("Unknown depth %d, please report.\n", bpp
);
83 /******************************************************************************
84 * CreateBitmap [GDI32.@]
86 * Creates a bitmap with the specified info.
89 * width [I] bitmap width
90 * height [I] bitmap height
91 * planes [I] Number of color planes
92 * bpp [I] Number of bits to identify a color
93 * bits [I] Pointer to array containing color data
96 * Success: Handle to bitmap
99 HBITMAP WINAPI
CreateBitmap( INT width
, INT height
, UINT planes
,
100 UINT bpp
, LPCVOID bits
)
106 bm
.bmHeight
= height
;
107 bm
.bmWidthBytes
= BITMAP_GetWidthBytes( width
, bpp
);
108 bm
.bmPlanes
= planes
;
109 bm
.bmBitsPixel
= bpp
;
110 bm
.bmBits
= (LPVOID
)bits
;
112 return CreateBitmapIndirect( &bm
);
115 /******************************************************************************
116 * CreateCompatibleBitmap [GDI32.@]
118 * Creates a bitmap compatible with the DC.
121 * hdc [I] Handle to device context
122 * width [I] Width of bitmap
123 * height [I] Height of bitmap
126 * Success: Handle to bitmap
129 HBITMAP WINAPI
CreateCompatibleBitmap( HDC hdc
, INT width
, INT height
)
133 TRACE("(%p,%d,%d) =\n", hdc
, width
, height
);
135 if (GetObjectType( hdc
) != OBJ_MEMDC
)
137 hbmpRet
= CreateBitmap(width
, height
,
138 GetDeviceCaps(hdc
, PLANES
),
139 GetDeviceCaps(hdc
, BITSPIXEL
),
145 HBITMAP bitmap
= GetCurrentObject( hdc
, OBJ_BITMAP
);
146 INT size
= GetObjectW( bitmap
, sizeof(dib
), &dib
);
150 if (size
== sizeof(BITMAP
))
152 /* A device-dependent bitmap is selected in the DC */
153 hbmpRet
= CreateBitmap(width
, height
,
155 dib
.dsBm
.bmBitsPixel
,
160 /* A DIB section is selected in the DC */
164 /* Allocate memory for a BITMAPINFOHEADER structure and a
165 color table. The maximum number of colors in a color table
166 is 256 which corresponds to a bitmap with depth 8.
167 Bitmaps with higher depths don't have color tables. */
168 bi
= HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
172 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
173 bi
->bmiHeader
.biWidth
= width
;
174 bi
->bmiHeader
.biHeight
= height
;
175 bi
->bmiHeader
.biPlanes
= dib
.dsBmih
.biPlanes
;
176 bi
->bmiHeader
.biBitCount
= dib
.dsBmih
.biBitCount
;
177 bi
->bmiHeader
.biCompression
= dib
.dsBmih
.biCompression
;
178 bi
->bmiHeader
.biSizeImage
= 0;
179 bi
->bmiHeader
.biXPelsPerMeter
= dib
.dsBmih
.biXPelsPerMeter
;
180 bi
->bmiHeader
.biYPelsPerMeter
= dib
.dsBmih
.biYPelsPerMeter
;
181 bi
->bmiHeader
.biClrUsed
= dib
.dsBmih
.biClrUsed
;
182 bi
->bmiHeader
.biClrImportant
= dib
.dsBmih
.biClrImportant
;
184 if (bi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
186 /* Copy the color masks */
187 CopyMemory(bi
->bmiColors
, dib
.dsBitfields
, 3 * sizeof(DWORD
));
189 else if (bi
->bmiHeader
.biBitCount
<= 8)
191 /* Copy the color table */
192 GetDIBColorTable(hdc
, 0, 256, bi
->bmiColors
);
195 hbmpRet
= CreateDIBSection(hdc
, bi
, DIB_RGB_COLORS
, &bits
, NULL
, 0);
196 HeapFree(GetProcessHeap(), 0, bi
);
201 TRACE("\t\t%p\n", hbmpRet
);
206 /******************************************************************************
207 * CreateBitmapIndirect [GDI32.@]
209 * Creates a bitmap with the specified info.
212 * bmp [I] Pointer to the bitmap info describing the bitmap
215 * Success: Handle to bitmap
216 * Failure: NULL. Use GetLastError() to determine the cause.
219 * If a width or height of 0 are given, a 1x1 monochrome bitmap is returned.
221 HBITMAP WINAPI
CreateBitmapIndirect( const BITMAP
*bmp
)
227 if (!bmp
|| bmp
->bmType
)
229 SetLastError( ERROR_INVALID_PARAMETER
);
233 if (bmp
->bmWidth
> 0x7ffffff || bmp
->bmHeight
> 0x7ffffff)
235 SetLastError( ERROR_INVALID_PARAMETER
);
241 if (!bm
.bmWidth
|| !bm
.bmHeight
)
243 return GetStockObject( DEFAULT_BITMAP
);
248 bm
.bmHeight
= -bm
.bmHeight
;
250 bm
.bmWidth
= -bm
.bmWidth
;
253 if (bm
.bmPlanes
!= 1)
255 FIXME("planes = %d\n", bm
.bmPlanes
);
256 SetLastError( ERROR_INVALID_PARAMETER
);
260 /* Windows only uses 1, 4, 8, 16, 24 and 32 bpp */
261 if(bm
.bmBitsPixel
== 1) bm
.bmBitsPixel
= 1;
262 else if(bm
.bmBitsPixel
<= 4) bm
.bmBitsPixel
= 4;
263 else if(bm
.bmBitsPixel
<= 8) bm
.bmBitsPixel
= 8;
264 else if(bm
.bmBitsPixel
<= 16) bm
.bmBitsPixel
= 16;
265 else if(bm
.bmBitsPixel
<= 24) bm
.bmBitsPixel
= 24;
266 else if(bm
.bmBitsPixel
<= 32) bm
.bmBitsPixel
= 32;
268 WARN("Invalid bmBitsPixel %d, returning ERROR_INVALID_PARAMETER\n", bm
.bmBitsPixel
);
269 SetLastError(ERROR_INVALID_PARAMETER
);
273 /* Windows ignores the provided bm.bmWidthBytes */
274 bm
.bmWidthBytes
= BITMAP_GetWidthBytes( bm
.bmWidth
, bm
.bmBitsPixel
);
275 /* XP doesn't allow to create bitmaps larger than 128 Mb */
276 if (bm
.bmHeight
> 128 * 1024 * 1024 / bm
.bmWidthBytes
)
278 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
282 /* Create the BITMAPOBJ */
283 if (!(bmpobj
= HeapAlloc( GetProcessHeap(), 0, sizeof(*bmpobj
) )))
285 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
292 bmpobj
->bitmap
.bmBits
= NULL
;
293 bmpobj
->funcs
= NULL
;
295 bmpobj
->color_table
= NULL
;
296 bmpobj
->nb_colors
= 0;
298 if (!(hbitmap
= alloc_gdi_handle( &bmpobj
->header
, OBJ_BITMAP
, &bitmap_funcs
)))
300 HeapFree( GetProcessHeap(), 0, bmpobj
);
305 SetBitmapBits( hbitmap
, bm
.bmHeight
* bm
.bmWidthBytes
, bm
.bmBits
);
307 TRACE("%dx%d, %d colors returning %p\n", bm
.bmWidth
, bm
.bmHeight
,
308 1 << (bm
.bmPlanes
* bm
.bmBitsPixel
), hbitmap
);
314 /***********************************************************************
315 * GetBitmapBits [GDI32.@]
317 * Copies bitmap bits of bitmap to buffer.
320 * Success: Number of bytes copied
323 LONG WINAPI
GetBitmapBits(
324 HBITMAP hbitmap
, /* [in] Handle to bitmap */
325 LONG count
, /* [in] Number of bytes to copy */
326 LPVOID bits
) /* [out] Pointer to buffer to receive bits */
328 BITMAPOBJ
*bmp
= GDI_GetObjPtr( hbitmap
, OBJ_BITMAP
);
333 if (bmp
->dib
) /* simply copy the bits from the DIB */
335 DIBSECTION
*dib
= bmp
->dib
;
336 const char *src
= dib
->dsBm
.bmBits
;
337 INT width_bytes
= BITMAP_GetWidthBytes(dib
->dsBm
.bmWidth
, dib
->dsBm
.bmBitsPixel
);
338 LONG max
= width_bytes
* bmp
->bitmap
.bmHeight
;
346 if (count
> max
) count
= max
;
349 /* GetBitmapBits returns not 32-bit aligned data */
351 if (bmp
->dib
->dsBmih
.biHeight
>= 0) /* not top-down, need to flip contents vertically */
353 src
+= dib
->dsBm
.bmWidthBytes
* dib
->dsBm
.bmHeight
;
356 src
-= dib
->dsBm
.bmWidthBytes
;
357 memcpy( bits
, src
, min( count
, width_bytes
) );
358 bits
= (char *)bits
+ width_bytes
;
359 count
-= width_bytes
;
366 memcpy( bits
, src
, min( count
, width_bytes
) );
367 src
+= dib
->dsBm
.bmWidthBytes
;
368 bits
= (char *)bits
+ width_bytes
;
369 count
-= width_bytes
;
375 /* If the bits vector is null, the function should return the read size */
378 ret
= bmp
->bitmap
.bmWidthBytes
* bmp
->bitmap
.bmHeight
;
383 WARN("(%d): Negative number of bytes passed???\n", count
);
387 /* Only get entire lines */
388 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
389 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
390 count
= height
* bmp
->bitmap
.bmWidthBytes
;
393 WARN("Less than one entire line requested\n");
399 TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
400 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
401 1 << bmp
->bitmap
.bmBitsPixel
, height
);
403 if(bmp
->funcs
&& bmp
->funcs
->pGetBitmapBits
)
405 TRACE("Calling device specific BitmapBits\n");
406 ret
= bmp
->funcs
->pGetBitmapBits(hbitmap
, bits
, count
);
409 if(!bmp
->bitmap
.bmBits
) {
410 TRACE("Bitmap is empty\n");
411 memset(bits
, 0, count
);
414 memcpy(bits
, bmp
->bitmap
.bmBits
, count
);
420 GDI_ReleaseObj( hbitmap
);
425 /******************************************************************************
426 * SetBitmapBits [GDI32.@]
428 * Sets bits of color data for a bitmap.
431 * Success: Number of bytes used in setting the bitmap bits
434 LONG WINAPI
SetBitmapBits(
435 HBITMAP hbitmap
, /* [in] Handle to bitmap */
436 LONG count
, /* [in] Number of bytes in bitmap array */
437 LPCVOID bits
) /* [in] Address of array with bitmap bits */
444 bmp
= GDI_GetObjPtr( hbitmap
, OBJ_BITMAP
);
448 WARN("(%d): Negative number of bytes passed???\n", count
);
452 if (bmp
->dib
) /* simply copy the bits into the DIB */
454 DIBSECTION
*dib
= bmp
->dib
;
455 char *dest
= dib
->dsBm
.bmBits
;
456 LONG max
= dib
->dsBm
.bmWidthBytes
* dib
->dsBm
.bmHeight
;
457 if (count
> max
) count
= max
;
460 if (bmp
->dib
->dsBmih
.biHeight
>= 0) /* not top-down, need to flip contents vertically */
462 dest
+= dib
->dsBm
.bmWidthBytes
* dib
->dsBm
.bmHeight
;
465 dest
-= dib
->dsBm
.bmWidthBytes
;
466 memcpy( dest
, bits
, min( count
, dib
->dsBm
.bmWidthBytes
) );
467 bits
= (const char *)bits
+ dib
->dsBm
.bmWidthBytes
;
468 count
-= dib
->dsBm
.bmWidthBytes
;
471 else memcpy( dest
, bits
, count
);
473 GDI_ReleaseObj( hbitmap
);
477 /* Only get entire lines */
478 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
479 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
480 count
= height
* bmp
->bitmap
.bmWidthBytes
;
482 TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
483 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
484 1 << bmp
->bitmap
.bmBitsPixel
, height
);
486 if(bmp
->funcs
&& bmp
->funcs
->pSetBitmapBits
) {
488 TRACE("Calling device specific BitmapBits\n");
489 ret
= bmp
->funcs
->pSetBitmapBits(hbitmap
, bits
, count
);
492 if(!bmp
->bitmap
.bmBits
) /* Alloc enough for entire bitmap */
493 bmp
->bitmap
.bmBits
= HeapAlloc( GetProcessHeap(), 0, count
);
494 if(!bmp
->bitmap
.bmBits
) {
495 WARN("Unable to allocate bit buffer\n");
498 memcpy(bmp
->bitmap
.bmBits
, bits
, count
);
503 GDI_ReleaseObj( hbitmap
);
507 /**********************************************************************
511 HBITMAP
BITMAP_CopyBitmap(HBITMAP hbitmap
)
517 if (!(size
= GetObjectW( hbitmap
, sizeof(dib
), &dib
))) return 0;
519 if (size
== sizeof(DIBSECTION
))
523 HDC dc
= CreateCompatibleDC( NULL
);
526 if (!(bi
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] ))))
531 bi
->bmiHeader
= dib
.dsBmih
;
533 /* Get the color table or the color masks */
534 GetDIBits( dc
, hbitmap
, 0, 0, NULL
, bi
, DIB_RGB_COLORS
);
536 res
= CreateDIBSection( dc
, bi
, DIB_RGB_COLORS
, &bits
, NULL
, 0 );
537 if (res
) SetDIBits( dc
, res
, 0, dib
.dsBm
.bmHeight
, dib
.dsBm
.bmBits
, bi
, DIB_RGB_COLORS
);
538 HeapFree( GetProcessHeap(), 0, bi
);
543 res
= CreateBitmapIndirect( &dib
.dsBm
);
545 char *buf
= HeapAlloc( GetProcessHeap(), 0, dib
.dsBm
.bmWidthBytes
* dib
.dsBm
.bmHeight
);
546 GetBitmapBits (hbitmap
, dib
.dsBm
.bmWidthBytes
* dib
.dsBm
.bmHeight
, buf
);
547 SetBitmapBits (res
, dib
.dsBm
.bmWidthBytes
* dib
.dsBm
.bmHeight
, buf
);
548 HeapFree( GetProcessHeap(), 0, buf
);
554 /***********************************************************************
557 * Set the type of DC that owns the bitmap. This is used when the
558 * bitmap is selected into a device to initialize the bitmap function
561 BOOL
BITMAP_SetOwnerDC( HBITMAP hbitmap
, DC
*dc
)
566 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
567 if (hbitmap
== GetStockObject(DEFAULT_BITMAP
)) return TRUE
;
569 if (!(bitmap
= GDI_GetObjPtr( hbitmap
, OBJ_BITMAP
))) return FALSE
;
572 if (!bitmap
->funcs
) /* not owned by a DC yet */
574 if (dc
->funcs
->pCreateBitmap
) ret
= dc
->funcs
->pCreateBitmap( dc
->physDev
, hbitmap
,
575 bitmap
->bitmap
.bmBits
);
576 if (ret
) bitmap
->funcs
= dc
->funcs
;
578 else if (bitmap
->funcs
!= dc
->funcs
)
580 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap
);
583 GDI_ReleaseObj( hbitmap
);
588 /***********************************************************************
589 * BITMAP_SelectObject
591 static HGDIOBJ
BITMAP_SelectObject( HGDIOBJ handle
, HDC hdc
)
597 if (!(dc
= get_dc_ptr( hdc
))) return 0;
599 if (GetObjectType( hdc
) != OBJ_MEMDC
)
605 if (handle
== dc
->hBitmap
) goto done
; /* nothing to do */
607 if (!(bitmap
= GDI_GetObjPtr( handle
, OBJ_BITMAP
)))
613 if (bitmap
->header
.selcount
&& (handle
!= GetStockObject(DEFAULT_BITMAP
)))
615 WARN( "Bitmap already selected in another DC\n" );
616 GDI_ReleaseObj( handle
);
621 if (!bitmap
->funcs
&& !BITMAP_SetOwnerDC( handle
, dc
))
623 GDI_ReleaseObj( handle
);
628 if (dc
->funcs
->pSelectBitmap
&& !dc
->funcs
->pSelectBitmap( dc
->physDev
, handle
))
630 GDI_ReleaseObj( handle
);
635 dc
->hBitmap
= handle
;
636 GDI_inc_ref_count( handle
);
638 dc
->vis_rect
.left
= 0;
639 dc
->vis_rect
.top
= 0;
640 dc
->vis_rect
.right
= bitmap
->bitmap
.bmWidth
;
641 dc
->vis_rect
.bottom
= bitmap
->bitmap
.bmHeight
;
642 SetRectRgn( dc
->hVisRgn
, 0, 0, bitmap
->bitmap
.bmWidth
, bitmap
->bitmap
.bmHeight
);
643 GDI_ReleaseObj( handle
);
645 GDI_dec_ref_count( ret
);
649 release_dc_ptr( dc
);
654 /***********************************************************************
655 * BITMAP_DeleteObject
657 static BOOL
BITMAP_DeleteObject( HGDIOBJ handle
)
659 const DC_FUNCTIONS
*funcs
;
660 BITMAPOBJ
*bmp
= GDI_GetObjPtr( handle
, OBJ_BITMAP
);
662 if (!bmp
) return FALSE
;
664 GDI_ReleaseObj( handle
);
666 if (funcs
&& funcs
->pDeleteBitmap
) funcs
->pDeleteBitmap( handle
);
668 if (!(bmp
= free_gdi_handle( handle
))) return FALSE
;
670 HeapFree( GetProcessHeap(), 0, bmp
->bitmap
.bmBits
);
674 DIBSECTION
*dib
= bmp
->dib
;
676 if (dib
->dsBm
.bmBits
)
680 SYSTEM_INFO SystemInfo
;
681 GetSystemInfo( &SystemInfo
);
682 UnmapViewOfFile( (char *)dib
->dsBm
.bmBits
-
683 (dib
->dsOffset
% SystemInfo
.dwAllocationGranularity
) );
685 else if (!dib
->dsOffset
)
686 VirtualFree(dib
->dsBm
.bmBits
, 0L, MEM_RELEASE
);
688 HeapFree(GetProcessHeap(), 0, dib
);
689 HeapFree(GetProcessHeap(), 0, bmp
->color_table
);
691 return HeapFree( GetProcessHeap(), 0, bmp
);
695 /***********************************************************************
698 static INT
BITMAP_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
)
701 BITMAPOBJ
*bmp
= GDI_GetObjPtr( handle
, OBJ_BITMAP
);
705 if (!buffer
) ret
= sizeof(BITMAP
);
706 else if (count
< sizeof(BITMAP
)) ret
= 0;
709 if (count
>= sizeof(DIBSECTION
))
711 DIBSECTION
*dib
= buffer
;
713 dib
->dsBmih
.biHeight
= abs( dib
->dsBmih
.biHeight
);
714 ret
= sizeof(DIBSECTION
);
716 else /* if (count >= sizeof(BITMAP)) */
718 DIBSECTION
*dib
= bmp
->dib
;
719 memcpy( buffer
, &dib
->dsBm
, sizeof(BITMAP
) );
720 ret
= sizeof(BITMAP
);
725 memcpy( buffer
, &bmp
->bitmap
, sizeof(BITMAP
) );
726 ((BITMAP
*) buffer
)->bmBits
= NULL
;
727 ret
= sizeof(BITMAP
);
729 GDI_ReleaseObj( handle
);
734 /******************************************************************************
735 * CreateDiscardableBitmap [GDI32.@]
737 * Creates a discardable bitmap.
740 * Success: Handle to bitmap
743 HBITMAP WINAPI
CreateDiscardableBitmap(
744 HDC hdc
, /* [in] Handle to device context */
745 INT width
, /* [in] Bitmap width */
746 INT height
) /* [in] Bitmap height */
748 return CreateCompatibleBitmap( hdc
, width
, height
);
752 /******************************************************************************
753 * GetBitmapDimensionEx [GDI32.@]
755 * Retrieves dimensions of a bitmap.
761 BOOL WINAPI
GetBitmapDimensionEx(
762 HBITMAP hbitmap
, /* [in] Handle to bitmap */
763 LPSIZE size
) /* [out] Address of struct receiving dimensions */
765 BITMAPOBJ
* bmp
= GDI_GetObjPtr( hbitmap
, OBJ_BITMAP
);
766 if (!bmp
) return FALSE
;
768 GDI_ReleaseObj( hbitmap
);
773 /******************************************************************************
774 * SetBitmapDimensionEx [GDI32.@]
776 * Assigns dimensions to a bitmap.
777 * MSDN says that this function will fail if hbitmap is a handle created by
778 * CreateDIBSection, but that's not true on Windows 2000.
784 BOOL WINAPI
SetBitmapDimensionEx(
785 HBITMAP hbitmap
, /* [in] Handle to bitmap */
786 INT x
, /* [in] Bitmap width */
787 INT y
, /* [in] Bitmap height */
788 LPSIZE prevSize
) /* [out] Address of structure for orig dims */
790 BITMAPOBJ
* bmp
= GDI_GetObjPtr( hbitmap
, OBJ_BITMAP
);
791 if (!bmp
) return FALSE
;
792 if (prevSize
) *prevSize
= bmp
->size
;
795 GDI_ReleaseObj( hbitmap
);