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 */
49 /******************************************************************************
50 * CreateBitmap [GDI32.@]
52 * Creates a bitmap with the specified info.
55 * width [I] bitmap width
56 * height [I] bitmap height
57 * planes [I] Number of color planes
58 * bpp [I] Number of bits to identify a color
59 * bits [I] Pointer to array containing color data
62 * Success: Handle to bitmap
65 HBITMAP WINAPI
CreateBitmap( INT width
, INT height
, UINT planes
,
66 UINT bpp
, LPCVOID bits
)
73 bm
.bmWidthBytes
= get_bitmap_stride( width
, bpp
);
76 bm
.bmBits
= (LPVOID
)bits
;
78 return CreateBitmapIndirect( &bm
);
81 /******************************************************************************
82 * CreateCompatibleBitmap [GDI32.@]
84 * Creates a bitmap compatible with the DC.
87 * hdc [I] Handle to device context
88 * width [I] Width of bitmap
89 * height [I] Height of bitmap
92 * Success: Handle to bitmap
95 HBITMAP WINAPI
CreateCompatibleBitmap( HDC hdc
, INT width
, INT height
)
97 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
98 BITMAPINFO
*bi
= (BITMAPINFO
*)buffer
;
101 TRACE("(%p,%d,%d)\n", hdc
, width
, height
);
103 if (GetObjectType( hdc
) != OBJ_MEMDC
)
104 return CreateBitmap( width
, height
,
105 GetDeviceCaps(hdc
, PLANES
), GetDeviceCaps(hdc
, BITSPIXEL
), NULL
);
107 switch (GetObjectW( GetCurrentObject( hdc
, OBJ_BITMAP
), sizeof(dib
), &dib
))
109 case sizeof(BITMAP
): /* A device-dependent bitmap is selected in the DC */
110 return CreateBitmap( width
, height
, dib
.dsBm
.bmPlanes
, dib
.dsBm
.bmBitsPixel
, NULL
);
112 case sizeof(DIBSECTION
): /* A DIB section is selected in the DC */
113 bi
->bmiHeader
= dib
.dsBmih
;
114 bi
->bmiHeader
.biWidth
= width
;
115 bi
->bmiHeader
.biHeight
= height
;
116 if (dib
.dsBmih
.biCompression
== BI_BITFIELDS
) /* copy the color masks */
117 memcpy(bi
->bmiColors
, dib
.dsBitfields
, sizeof(dib
.dsBitfields
));
118 else if (dib
.dsBmih
.biBitCount
<= 8) /* copy the color table */
119 GetDIBColorTable(hdc
, 0, 256, bi
->bmiColors
);
120 return CreateDIBSection( hdc
, bi
, DIB_RGB_COLORS
, NULL
, NULL
, 0 );
128 /******************************************************************************
129 * CreateBitmapIndirect [GDI32.@]
131 * Creates a bitmap with the specified info.
134 * bmp [I] Pointer to the bitmap info describing the bitmap
137 * Success: Handle to bitmap
138 * Failure: NULL. Use GetLastError() to determine the cause.
141 * If a width or height of 0 is given, a 1x1 monochrome bitmap is returned.
143 HBITMAP WINAPI
CreateBitmapIndirect( const BITMAP
*bmp
)
149 if (!bmp
|| bmp
->bmType
)
151 SetLastError( ERROR_INVALID_PARAMETER
);
155 if (bmp
->bmWidth
> 0x7ffffff || bmp
->bmHeight
> 0x7ffffff)
157 SetLastError( ERROR_INVALID_PARAMETER
);
163 if (!bm
.bmWidth
|| !bm
.bmHeight
)
165 return GetStockObject( DEFAULT_BITMAP
);
170 bm
.bmHeight
= -bm
.bmHeight
;
172 bm
.bmWidth
= -bm
.bmWidth
;
175 if (bm
.bmPlanes
!= 1)
177 FIXME("planes = %d\n", bm
.bmPlanes
);
178 SetLastError( ERROR_INVALID_PARAMETER
);
182 /* Windows only uses 1, 4, 8, 16, 24 and 32 bpp */
183 if(bm
.bmBitsPixel
== 1) bm
.bmBitsPixel
= 1;
184 else if(bm
.bmBitsPixel
<= 4) bm
.bmBitsPixel
= 4;
185 else if(bm
.bmBitsPixel
<= 8) bm
.bmBitsPixel
= 8;
186 else if(bm
.bmBitsPixel
<= 16) bm
.bmBitsPixel
= 16;
187 else if(bm
.bmBitsPixel
<= 24) bm
.bmBitsPixel
= 24;
188 else if(bm
.bmBitsPixel
<= 32) bm
.bmBitsPixel
= 32;
190 WARN("Invalid bmBitsPixel %d, returning ERROR_INVALID_PARAMETER\n", bm
.bmBitsPixel
);
191 SetLastError(ERROR_INVALID_PARAMETER
);
195 /* Windows ignores the provided bm.bmWidthBytes */
196 bm
.bmWidthBytes
= get_bitmap_stride( bm
.bmWidth
, bm
.bmBitsPixel
);
197 /* XP doesn't allow to create bitmaps larger than 128 Mb */
198 if (bm
.bmHeight
> 128 * 1024 * 1024 / bm
.bmWidthBytes
)
200 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
204 /* Create the BITMAPOBJ */
205 if (!(bmpobj
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*bmpobj
) )))
207 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
211 bmpobj
->dib
.dsBm
= bm
;
212 bmpobj
->dib
.dsBm
.bmBits
= NULL
;
214 if (!(hbitmap
= alloc_gdi_handle( bmpobj
, OBJ_BITMAP
, &bitmap_funcs
)))
216 HeapFree( GetProcessHeap(), 0, bmpobj
);
221 SetBitmapBits( hbitmap
, bm
.bmHeight
* bm
.bmWidthBytes
, bm
.bmBits
);
223 TRACE("%dx%d, bpp %d planes %d: returning %p\n", bm
.bmWidth
, bm
.bmHeight
,
224 bm
.bmBitsPixel
, bm
.bmPlanes
, hbitmap
);
230 /***********************************************************************
231 * GetBitmapBits [GDI32.@]
233 * Copies bitmap bits of bitmap to buffer.
236 * Success: Number of bytes copied
239 LONG WINAPI
GetBitmapBits(
240 HBITMAP hbitmap
, /* [in] Handle to bitmap */
241 LONG count
, /* [in] Number of bytes to copy */
242 LPVOID bits
) /* [out] Pointer to buffer to receive bits */
244 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
245 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
246 struct gdi_image_bits src_bits
;
247 struct bitblt_coords src
;
248 int dst_stride
, max
, ret
;
249 BITMAPOBJ
*bmp
= GDI_GetObjPtr( hbitmap
, OBJ_BITMAP
);
253 dst_stride
= get_bitmap_stride( bmp
->dib
.dsBm
.bmWidth
, bmp
->dib
.dsBm
.bmBitsPixel
);
254 ret
= max
= dst_stride
* bmp
->dib
.dsBm
.bmHeight
;
255 if (!bits
) goto done
;
256 if (count
> max
) count
= max
;
259 src
.visrect
.left
= 0;
260 src
.visrect
.right
= bmp
->dib
.dsBm
.bmWidth
;
262 src
.visrect
.bottom
= (count
+ dst_stride
- 1) / dst_stride
;
264 src
.width
= src
.visrect
.right
- src
.visrect
.left
;
265 src
.height
= src
.visrect
.bottom
- src
.visrect
.top
;
267 if (!get_image_from_bitmap( bmp
, info
, &src_bits
, &src
))
269 const char *src_ptr
= src_bits
.ptr
;
270 int src_stride
= get_dib_stride( info
->bmiHeader
.biWidth
, info
->bmiHeader
.biBitCount
);
272 /* GetBitmapBits returns 16-bit aligned data */
274 if (info
->bmiHeader
.biHeight
> 0)
276 src_ptr
+= (info
->bmiHeader
.biHeight
- 1) * src_stride
;
277 src_stride
= -src_stride
;
279 src_ptr
+= src
.visrect
.top
* src_stride
;
281 if (src_stride
== dst_stride
) memcpy( bits
, src_ptr
, count
);
282 else while (count
> 0)
284 memcpy( bits
, src_ptr
, min( count
, dst_stride
) );
285 src_ptr
+= src_stride
;
286 bits
= (char *)bits
+ dst_stride
;
289 if (src_bits
.free
) src_bits
.free( &src_bits
);
294 GDI_ReleaseObj( hbitmap
);
299 /******************************************************************************
300 * SetBitmapBits [GDI32.@]
302 * Sets bits of color data for a bitmap.
305 * Success: Number of bytes used in setting the bitmap bits
308 LONG WINAPI
SetBitmapBits(
309 HBITMAP hbitmap
, /* [in] Handle to bitmap */
310 LONG count
, /* [in] Number of bytes in bitmap array */
311 LPCVOID bits
) /* [in] Address of array with bitmap bits */
313 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
314 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
317 int i
, src_stride
, dst_stride
;
318 struct bitblt_coords src
, dst
;
319 struct gdi_image_bits src_bits
;
324 bmp
= GDI_GetObjPtr( hbitmap
, OBJ_BITMAP
);
328 WARN("(%d): Negative number of bytes passed???\n", count
);
332 src_stride
= get_bitmap_stride( bmp
->dib
.dsBm
.bmWidth
, bmp
->dib
.dsBm
.bmBitsPixel
);
333 count
= min( count
, src_stride
* bmp
->dib
.dsBm
.bmHeight
);
335 dst_stride
= get_dib_stride( bmp
->dib
.dsBm
.bmWidth
, bmp
->dib
.dsBm
.bmBitsPixel
);
337 src
.visrect
.left
= src
.x
= 0;
338 src
.visrect
.top
= src
.y
= 0;
339 src
.visrect
.right
= src
.width
= bmp
->dib
.dsBm
.bmWidth
;
340 src
.visrect
.bottom
= src
.height
= (count
+ src_stride
- 1 ) / src_stride
;
343 if (count
% src_stride
)
346 int extra_pixels
= ((count
% src_stride
) << 3) / bmp
->dib
.dsBm
.bmBitsPixel
;
348 if ((count
% src_stride
<< 3) % bmp
->dib
.dsBm
.bmBitsPixel
)
349 FIXME( "Unhandled partial pixel\n" );
350 clip
= CreateRectRgn( src
.visrect
.left
, src
.visrect
.top
,
351 src
.visrect
.right
, src
.visrect
.bottom
- 1 );
352 last_row
= CreateRectRgn( src
.visrect
.left
, src
.visrect
.bottom
- 1,
353 src
.visrect
.left
+ extra_pixels
, src
.visrect
.bottom
);
354 CombineRgn( clip
, clip
, last_row
, RGN_OR
);
355 DeleteObject( last_row
);
358 TRACE("(%p, %d, %p) %dx%d %d bpp fetched height: %d\n",
359 hbitmap
, count
, bits
, bmp
->dib
.dsBm
.bmWidth
, bmp
->dib
.dsBm
.bmHeight
,
360 bmp
->dib
.dsBm
.bmBitsPixel
, src
.height
);
362 if (src_stride
== dst_stride
)
364 src_bits
.ptr
= (void *)bits
;
365 src_bits
.is_copy
= FALSE
;
366 src_bits
.free
= NULL
;
370 if (!(src_bits
.ptr
= HeapAlloc( GetProcessHeap(), 0, dst
.height
* dst_stride
)))
372 GDI_ReleaseObj( hbitmap
);
375 src_bits
.is_copy
= TRUE
;
376 src_bits
.free
= free_heap_bits
;
377 for (i
= 0; i
< count
/ src_stride
; i
++)
378 memcpy( (char *)src_bits
.ptr
+ i
* dst_stride
, (char *)bits
+ i
* src_stride
, src_stride
);
379 if (count
% src_stride
)
380 memcpy( (char *)src_bits
.ptr
+ i
* dst_stride
, (char *)bits
+ i
* src_stride
, count
% src_stride
);
383 /* query the color info */
384 info
->bmiHeader
.biSize
= sizeof(info
->bmiHeader
);
385 info
->bmiHeader
.biPlanes
= 1;
386 info
->bmiHeader
.biBitCount
= bmp
->dib
.dsBm
.bmBitsPixel
;
387 info
->bmiHeader
.biCompression
= BI_RGB
;
388 info
->bmiHeader
.biXPelsPerMeter
= 0;
389 info
->bmiHeader
.biYPelsPerMeter
= 0;
390 info
->bmiHeader
.biClrUsed
= 0;
391 info
->bmiHeader
.biClrImportant
= 0;
392 info
->bmiHeader
.biWidth
= 0;
393 info
->bmiHeader
.biHeight
= 0;
394 info
->bmiHeader
.biSizeImage
= 0;
395 err
= put_image_into_bitmap( bmp
, 0, info
, NULL
, NULL
, NULL
);
397 if (!err
|| err
== ERROR_BAD_FORMAT
)
399 info
->bmiHeader
.biWidth
= bmp
->dib
.dsBm
.bmWidth
;
400 info
->bmiHeader
.biHeight
= -dst
.height
;
401 info
->bmiHeader
.biSizeImage
= dst
.height
* dst_stride
;
402 err
= put_image_into_bitmap( bmp
, clip
, info
, &src_bits
, &src
, &dst
);
406 if (clip
) DeleteObject( clip
);
407 if (src_bits
.free
) src_bits
.free( &src_bits
);
408 GDI_ReleaseObj( hbitmap
);
413 /***********************************************************************
414 * BITMAP_SelectObject
416 static HGDIOBJ
BITMAP_SelectObject( HGDIOBJ handle
, HDC hdc
)
423 if (!(dc
= get_dc_ptr( hdc
))) return 0;
425 if (GetObjectType( hdc
) != OBJ_MEMDC
)
431 if (handle
== dc
->hBitmap
) goto done
; /* nothing to do */
433 if (!(bitmap
= GDI_GetObjPtr( handle
, OBJ_BITMAP
)))
439 if (handle
!= GetStockObject(DEFAULT_BITMAP
) && GDI_get_ref_count( handle
))
441 WARN( "Bitmap already selected in another DC\n" );
442 GDI_ReleaseObj( handle
);
447 if (bitmap
->dib
.dsBm
.bmBitsPixel
!= 1 &&
448 bitmap
->dib
.dsBm
.bmBitsPixel
!= GetDeviceCaps( hdc
, BITSPIXEL
))
450 WARN( "Wrong format bitmap %u bpp\n", bitmap
->dib
.dsBm
.bmBitsPixel
);
451 GDI_ReleaseObj( handle
);
456 physdev
= GET_DC_PHYSDEV( dc
, pSelectBitmap
);
457 if (!physdev
->funcs
->pSelectBitmap( physdev
, handle
))
459 GDI_ReleaseObj( handle
);
464 dc
->hBitmap
= handle
;
465 GDI_inc_ref_count( handle
);
467 dc
->vis_rect
.left
= 0;
468 dc
->vis_rect
.top
= 0;
469 dc
->vis_rect
.right
= bitmap
->dib
.dsBm
.bmWidth
;
470 dc
->vis_rect
.bottom
= bitmap
->dib
.dsBm
.bmHeight
;
471 dc
->device_rect
= dc
->vis_rect
;
472 GDI_ReleaseObj( handle
);
474 GDI_dec_ref_count( ret
);
478 release_dc_ptr( dc
);
483 /***********************************************************************
484 * BITMAP_DeleteObject
486 static BOOL
BITMAP_DeleteObject( HGDIOBJ handle
)
488 BITMAPOBJ
*bmp
= free_gdi_handle( handle
);
490 if (!bmp
) return FALSE
;
491 HeapFree( GetProcessHeap(), 0, bmp
->dib
.dsBm
.bmBits
);
492 return HeapFree( GetProcessHeap(), 0, bmp
);
496 /***********************************************************************
499 static INT
BITMAP_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
)
502 BITMAPOBJ
*bmp
= GDI_GetObjPtr( handle
, OBJ_BITMAP
);
506 if (!buffer
) ret
= sizeof(BITMAP
);
507 else if (count
>= sizeof(BITMAP
))
509 BITMAP
*bitmap
= buffer
;
510 *bitmap
= bmp
->dib
.dsBm
;
511 bitmap
->bmBits
= NULL
;
512 ret
= sizeof(BITMAP
);
514 GDI_ReleaseObj( handle
);
519 /******************************************************************************
520 * CreateDiscardableBitmap [GDI32.@]
522 * Creates a discardable bitmap.
525 * Success: Handle to bitmap
528 HBITMAP WINAPI
CreateDiscardableBitmap(
529 HDC hdc
, /* [in] Handle to device context */
530 INT width
, /* [in] Bitmap width */
531 INT height
) /* [in] Bitmap height */
533 return CreateCompatibleBitmap( hdc
, width
, height
);
537 /******************************************************************************
538 * GetBitmapDimensionEx [GDI32.@]
540 * Retrieves dimensions of a bitmap.
546 BOOL WINAPI
GetBitmapDimensionEx(
547 HBITMAP hbitmap
, /* [in] Handle to bitmap */
548 LPSIZE size
) /* [out] Address of struct receiving dimensions */
550 BITMAPOBJ
* bmp
= GDI_GetObjPtr( hbitmap
, OBJ_BITMAP
);
551 if (!bmp
) return FALSE
;
553 GDI_ReleaseObj( hbitmap
);
558 /******************************************************************************
559 * SetBitmapDimensionEx [GDI32.@]
561 * Assigns dimensions to a bitmap.
562 * MSDN says that this function will fail if hbitmap is a handle created by
563 * CreateDIBSection, but that's not true on Windows 2000.
569 BOOL WINAPI
SetBitmapDimensionEx(
570 HBITMAP hbitmap
, /* [in] Handle to bitmap */
571 INT x
, /* [in] Bitmap width */
572 INT y
, /* [in] Bitmap height */
573 LPSIZE prevSize
) /* [out] Address of structure for orig dims */
575 BITMAPOBJ
* bmp
= GDI_GetObjPtr( hbitmap
, OBJ_BITMAP
);
576 if (!bmp
) return FALSE
;
577 if (prevSize
) *prevSize
= bmp
->size
;
580 GDI_ReleaseObj( hbitmap
);