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 * null driver fallback implementations
53 DWORD nulldrv_GetImage( PHYSDEV dev, HBITMAP hbitmap, BITMAPINFO *info,
54 struct gdi_image_bits *bits, struct bitblt_coords *src )
56 if (!hbitmap) return ERROR_NOT_SUPPORTED;
57 return dib_driver.pGetImage( 0, hbitmap, info, bits, src );
60 DWORD nulldrv_PutImage( PHYSDEV dev, HBITMAP hbitmap, HRGN clip, BITMAPINFO *info,
61 const struct gdi_image_bits *bits, struct bitblt_coords *src,
62 struct bitblt_coords *dst, DWORD rop )
64 if (!hbitmap) return ERROR_SUCCESS;
65 return dib_driver.pPutImage( NULL, hbitmap, clip, info, bits, src, dst, rop );
69 /******************************************************************************
70 * CreateBitmap [GDI32.@]
72 * Creates a bitmap with the specified info.
75 * width [I] bitmap width
76 * height [I] bitmap height
77 * planes [I] Number of color planes
78 * bpp [I] Number of bits to identify a color
79 * bits [I] Pointer to array containing color data
82 * Success: Handle to bitmap
85 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
86 UINT bpp, LPCVOID bits )
93 bm.bmWidthBytes = get_bitmap_stride( width, bpp );
96 bm.bmBits = (LPVOID)bits;
98 return CreateBitmapIndirect( &bm );
101 /******************************************************************************
102 * CreateCompatibleBitmap [GDI32.@]
104 * Creates a bitmap compatible with the DC.
107 * hdc [I] Handle to device context
108 * width [I] Width of bitmap
109 * height [I] Height of bitmap
112 * Success: Handle to bitmap
115 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
117 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
118 BITMAPINFO *bi = (BITMAPINFO *)buffer;
121 TRACE("(%p,%d,%d)\n", hdc, width, height);
123 if (GetObjectType( hdc ) != OBJ_MEMDC)
124 return CreateBitmap( width, height,
125 GetDeviceCaps(hdc, PLANES), GetDeviceCaps(hdc, BITSPIXEL), NULL );
127 switch (GetObjectW( GetCurrentObject( hdc, OBJ_BITMAP ), sizeof(dib), &dib ))
129 case sizeof(BITMAP): /* A device-dependent bitmap is selected in the DC */
130 return CreateBitmap( width, height, dib.dsBm.bmPlanes, dib.dsBm.bmBitsPixel, NULL );
132 case sizeof(DIBSECTION): /* A DIB section is selected in the DC */
133 bi->bmiHeader = dib.dsBmih;
134 bi->bmiHeader.biWidth = width;
135 bi->bmiHeader.biHeight = height;
136 if (dib.dsBmih.biCompression == BI_BITFIELDS) /* copy the color masks */
137 memcpy(bi->bmiColors, dib.dsBitfields, sizeof(dib.dsBitfields));
138 else if (dib.dsBmih.biBitCount <= 8) /* copy the color table */
139 GetDIBColorTable(hdc, 0, 256, bi->bmiColors);
140 return CreateDIBSection( hdc, bi, DIB_RGB_COLORS, NULL, NULL, 0 );
148 /******************************************************************************
149 * CreateBitmapIndirect [GDI32.@]
151 * Creates a bitmap with the specified info.
154 * bmp [I] Pointer to the bitmap info describing the bitmap
157 * Success: Handle to bitmap
158 * Failure: NULL. Use GetLastError() to determine the cause.
161 * If a width or height of 0 are given, a 1x1 monochrome bitmap is returned.
163 HBITMAP WINAPI CreateBitmapIndirect( const BITMAP *bmp )
169 if (!bmp || bmp->bmType)
171 SetLastError( ERROR_INVALID_PARAMETER );
175 if (bmp->bmWidth > 0x7ffffff || bmp->bmHeight > 0x7ffffff)
177 SetLastError( ERROR_INVALID_PARAMETER );
183 if (!bm.bmWidth || !bm.bmHeight)
185 return GetStockObject( DEFAULT_BITMAP );
190 bm.bmHeight = -bm.bmHeight;
192 bm.bmWidth = -bm.bmWidth;
195 if (bm.bmPlanes != 1)
197 FIXME("planes = %d\n", bm.bmPlanes);
198 SetLastError( ERROR_INVALID_PARAMETER );
202 /* Windows only uses 1, 4, 8, 16, 24 and 32 bpp */
203 if(bm.bmBitsPixel == 1) bm.bmBitsPixel = 1;
204 else if(bm.bmBitsPixel <= 4) bm.bmBitsPixel = 4;
205 else if(bm.bmBitsPixel <= 8) bm.bmBitsPixel = 8;
206 else if(bm.bmBitsPixel <= 16) bm.bmBitsPixel = 16;
207 else if(bm.bmBitsPixel <= 24) bm.bmBitsPixel = 24;
208 else if(bm.bmBitsPixel <= 32) bm.bmBitsPixel = 32;
210 WARN("Invalid bmBitsPixel %d, returning ERROR_INVALID_PARAMETER\n", bm.bmBitsPixel);
211 SetLastError(ERROR_INVALID_PARAMETER);
215 /* Windows ignores the provided bm.bmWidthBytes */
216 bm.bmWidthBytes = get_bitmap_stride( bm.bmWidth, bm.bmBitsPixel );
217 /* XP doesn't allow to create bitmaps larger than 128 Mb */
218 if (bm.bmHeight > 128 * 1024 * 1024 / bm.bmWidthBytes)
220 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
224 /* Create the BITMAPOBJ */
225 if (!(bmpobj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*bmpobj) )))
227 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
231 bmpobj->dib.dsBm = bm;
232 bmpobj->dib.dsBm.bmBits = NULL;
233 bmpobj->funcs = &null_driver;
235 if (!(hbitmap = alloc_gdi_handle( &bmpobj->header, OBJ_BITMAP, &bitmap_funcs )))
237 HeapFree( GetProcessHeap(), 0, bmpobj );
242 SetBitmapBits( hbitmap, bm.bmHeight * bm.bmWidthBytes, bm.bmBits );
244 TRACE("%dx%d, bpp %d planes %d: returning %p\n", bm.bmWidth, bm.bmHeight,
245 bm.bmBitsPixel, bm.bmPlanes, hbitmap);
251 /***********************************************************************
252 * GetBitmapBits [GDI32.@]
254 * Copies bitmap bits of bitmap to buffer.
257 * Success: Number of bytes copied
260 LONG WINAPI GetBitmapBits(
261 HBITMAP hbitmap, /* [in] Handle to bitmap */
262 LONG count, /* [in] Number of bytes to copy */
263 LPVOID bits) /* [out] Pointer to buffer to receive bits */
265 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
266 BITMAPINFO *info = (BITMAPINFO *)buffer;
267 struct gdi_image_bits src_bits;
268 struct bitblt_coords src;
269 int dst_stride, max, ret;
270 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
274 dst_stride = get_bitmap_stride( bmp->dib.dsBm.bmWidth, bmp->dib.dsBm.bmBitsPixel );
275 ret = max = dst_stride * bmp->dib.dsBm.bmHeight;
276 if (!bits) goto done;
277 if (count > max) count = max;
280 src.visrect.left = 0;
281 src.visrect.right = bmp->dib.dsBm.bmWidth;
283 src.visrect.bottom = (count + dst_stride - 1) / dst_stride;
285 src.width = src.visrect.right - src.visrect.left;
286 src.height = src.visrect.bottom - src.visrect.top;
288 if (!bmp->funcs->pGetImage( NULL, hbitmap, info, &src_bits, &src ))
290 const char *src_ptr = src_bits.ptr;
291 int src_stride = get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
293 /* GetBitmapBits returns 16-bit aligned data */
295 if (info->bmiHeader.biHeight > 0)
297 src_ptr += (info->bmiHeader.biHeight - 1) * src_stride;
298 src_stride = -src_stride;
300 src_ptr += src.visrect.top * src_stride;
302 if (src_stride == dst_stride) memcpy( bits, src_ptr, count );
303 else while (count > 0)
305 memcpy( bits, src_ptr, min( count, dst_stride ) );
306 src_ptr += src_stride;
307 bits = (char *)bits + dst_stride;
310 if (src_bits.free) src_bits.free( &src_bits );
315 GDI_ReleaseObj( hbitmap );
320 /******************************************************************************
321 * SetBitmapBits [GDI32.@]
323 * Sets bits of color data for a bitmap.
326 * Success: Number of bytes used in setting the bitmap bits
329 LONG WINAPI SetBitmapBits(
330 HBITMAP hbitmap, /* [in] Handle to bitmap */
331 LONG count, /* [in] Number of bytes in bitmap array */
332 LPCVOID bits) /* [in] Address of array with bitmap bits */
334 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
335 BITMAPINFO *info = (BITMAPINFO *)buffer;
338 int i, src_stride, dst_stride;
339 struct bitblt_coords src, dst;
340 struct gdi_image_bits src_bits;
345 bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
349 WARN("(%d): Negative number of bytes passed???\n", count );
353 src_stride = get_bitmap_stride( bmp->dib.dsBm.bmWidth, bmp->dib.dsBm.bmBitsPixel );
354 count = min( count, src_stride * bmp->dib.dsBm.bmHeight );
356 dst_stride = get_dib_stride( bmp->dib.dsBm.bmWidth, bmp->dib.dsBm.bmBitsPixel );
358 src.visrect.left = src.x = 0;
359 src.visrect.top = src.y = 0;
360 src.visrect.right = src.width = bmp->dib.dsBm.bmWidth;
361 src.visrect.bottom = src.height = (count + src_stride - 1 ) / src_stride;
364 if (count % src_stride)
367 int extra_pixels = ((count % src_stride) << 3) / bmp->dib.dsBm.bmBitsPixel;
369 if ((count % src_stride << 3) % bmp->dib.dsBm.bmBitsPixel)
370 FIXME( "Unhandled partial pixel\n" );
371 clip = CreateRectRgn( src.visrect.left, src.visrect.top,
372 src.visrect.right, src.visrect.bottom - 1 );
373 last_row = CreateRectRgn( src.visrect.left, src.visrect.bottom - 1,
374 src.visrect.left + extra_pixels, src.visrect.bottom );
375 CombineRgn( clip, clip, last_row, RGN_OR );
376 DeleteObject( last_row );
379 TRACE("(%p, %d, %p) %dx%d %d bpp fetched height: %d\n",
380 hbitmap, count, bits, bmp->dib.dsBm.bmWidth, bmp->dib.dsBm.bmHeight,
381 bmp->dib.dsBm.bmBitsPixel, src.height );
383 if (src_stride == dst_stride)
385 src_bits.ptr = (void *)bits;
386 src_bits.is_copy = FALSE;
387 src_bits.free = NULL;
391 if (!(src_bits.ptr = HeapAlloc( GetProcessHeap(), 0, dst.height * dst_stride )))
393 GDI_ReleaseObj( hbitmap );
396 src_bits.is_copy = TRUE;
397 src_bits.free = free_heap_bits;
398 for (i = 0; i < count / src_stride; i++)
399 memcpy( (char *)src_bits.ptr + i * dst_stride, (char *)bits + i * src_stride, src_stride );
400 if (count % src_stride)
401 memcpy( (char *)src_bits.ptr + i * dst_stride, (char *)bits + i * src_stride, count % src_stride );
404 /* query the color info */
405 info->bmiHeader.biSize = sizeof(info->bmiHeader);
406 info->bmiHeader.biPlanes = 1;
407 info->bmiHeader.biBitCount = bmp->dib.dsBm.bmBitsPixel;
408 info->bmiHeader.biCompression = BI_RGB;
409 info->bmiHeader.biXPelsPerMeter = 0;
410 info->bmiHeader.biYPelsPerMeter = 0;
411 info->bmiHeader.biClrUsed = 0;
412 info->bmiHeader.biClrImportant = 0;
413 info->bmiHeader.biWidth = 0;
414 info->bmiHeader.biHeight = 0;
415 info->bmiHeader.biSizeImage = 0;
416 err = bmp->funcs->pPutImage( NULL, hbitmap, 0, info, NULL, NULL, NULL, SRCCOPY );
418 if (!err || err == ERROR_BAD_FORMAT)
420 info->bmiHeader.biWidth = bmp->dib.dsBm.bmWidth;
421 info->bmiHeader.biHeight = -dst.height;
422 info->bmiHeader.biSizeImage = dst.height * dst_stride;
423 err = bmp->funcs->pPutImage( NULL, hbitmap, clip, info, &src_bits, &src, &dst, SRCCOPY );
427 if (clip) DeleteObject( clip );
428 if (src_bits.free) src_bits.free( &src_bits );
429 GDI_ReleaseObj( hbitmap );
434 static void set_initial_bitmap_bits( HBITMAP hbitmap, BITMAPOBJ *bmp )
436 char src_bmibuf[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
437 BITMAPINFO *src_info = (BITMAPINFO *)src_bmibuf;
438 char dst_bmibuf[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
439 BITMAPINFO *dst_info = (BITMAPINFO *)dst_bmibuf;
441 struct gdi_image_bits bits;
442 struct bitblt_coords src, dst;
444 if (!bmp->dib.dsBm.bmBits) return;
445 if (bmp->funcs->pPutImage == nulldrv_PutImage) return;
447 get_ddb_bitmapinfo( bmp, src_info );
449 bits.ptr = bmp->dib.dsBm.bmBits;
450 bits.is_copy = FALSE;
456 src.width = bmp->dib.dsBm.bmWidth;
457 src.height = bmp->dib.dsBm.bmHeight;
458 src.visrect.left = 0;
460 src.visrect.right = bmp->dib.dsBm.bmWidth;
461 src.visrect.bottom = bmp->dib.dsBm.bmHeight;
464 copy_bitmapinfo( dst_info, src_info );
466 err = bmp->funcs->pPutImage( NULL, hbitmap, 0, dst_info, &bits, &src, &dst, 0 );
467 if (err == ERROR_BAD_FORMAT)
469 err = convert_bits( src_info, &src, dst_info, &bits, FALSE );
470 if (!err) err = bmp->funcs->pPutImage( NULL, hbitmap, 0, dst_info, &bits, &src, &dst, 0 );
471 if (bits.free) bits.free( &bits );
476 /***********************************************************************
477 * BITMAP_SelectObject
479 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, HDC hdc )
484 PHYSDEV physdev = NULL, old_physdev = NULL, createdev;
486 if (!(dc = get_dc_ptr( hdc ))) return 0;
488 if (GetObjectType( hdc ) != OBJ_MEMDC)
494 if (handle == dc->hBitmap) goto done; /* nothing to do */
496 if (!(bitmap = GDI_GetObjPtr( handle, OBJ_BITMAP )))
502 if (bitmap->header.selcount && (handle != GetStockObject(DEFAULT_BITMAP)))
504 WARN( "Bitmap already selected in another DC\n" );
505 GDI_ReleaseObj( handle );
510 if (bitmap->dib.dsBm.bmBitsPixel != 1 &&
511 bitmap->dib.dsBm.bmBitsPixel != GetDeviceCaps( hdc, BITSPIXEL ))
513 WARN( "Wrong format bitmap %u bpp\n", bitmap->dib.dsBm.bmBitsPixel );
514 GDI_ReleaseObj( handle );
519 if (dc->dibdrv) old_physdev = pop_dc_driver( dc, dc->dibdrv );
521 if (bitmap->dib.dsBm.bmBitsPixel > 1)
523 physdev = GET_DC_PHYSDEV( dc, pSelectBitmap );
524 createdev = GET_DC_PHYSDEV( dc, pCreateBitmap );
526 else physdev = createdev = &dc->nulldrv; /* force use of the DIB engine for 1-bpp */
528 if (physdev->funcs == &null_driver)
530 physdev = dc->dibdrv;
531 if (physdev) push_dc_driver( &dc->physDev, physdev, physdev->funcs );
534 if (!dib_driver.pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL )) goto done;
535 dc->dibdrv = physdev = dc->physDev;
539 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
540 if (handle != GetStockObject(DEFAULT_BITMAP) && bitmap->funcs != createdev->funcs)
542 /* we can only change from the null driver to some other driver */
543 if (bitmap->funcs != &null_driver)
545 FIXME( "Trying to select bitmap %p in different DC type\n", handle );
546 GDI_ReleaseObj( handle );
550 if (createdev->funcs != &null_driver)
552 if (!createdev->funcs->pCreateBitmap( createdev, handle ))
554 GDI_ReleaseObj( handle );
558 bitmap->funcs = createdev->funcs;
559 set_initial_bitmap_bits( handle, bitmap );
561 else bitmap->funcs = &dib_driver; /* use the DIB driver to emulate DDB support */
564 if (!physdev->funcs->pSelectBitmap( physdev, handle ))
566 GDI_ReleaseObj( handle );
571 dc->hBitmap = handle;
572 GDI_inc_ref_count( handle );
574 dc->vis_rect.left = 0;
575 dc->vis_rect.top = 0;
576 dc->vis_rect.right = bitmap->dib.dsBm.bmWidth;
577 dc->vis_rect.bottom = bitmap->dib.dsBm.bmHeight;
578 dc->device_rect = dc->vis_rect;
579 GDI_ReleaseObj( handle );
581 GDI_dec_ref_count( ret );
587 if (physdev && physdev == dc->dibdrv) pop_dc_driver( dc, dc->dibdrv );
588 if (old_physdev) push_dc_driver( &dc->physDev, old_physdev, old_physdev->funcs );
590 release_dc_ptr( dc );
595 /***********************************************************************
596 * BITMAP_DeleteObject
598 static BOOL BITMAP_DeleteObject( HGDIOBJ handle )
600 const struct gdi_dc_funcs *funcs;
601 BITMAPOBJ *bmp = GDI_GetObjPtr( handle, OBJ_BITMAP );
603 if (!bmp) return FALSE;
605 GDI_ReleaseObj( handle );
607 funcs->pDeleteBitmap( handle );
609 if (!(bmp = free_gdi_handle( handle ))) return FALSE;
611 HeapFree( GetProcessHeap(), 0, bmp->dib.dsBm.bmBits );
612 return HeapFree( GetProcessHeap(), 0, bmp );
616 /***********************************************************************
619 static INT BITMAP_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
622 BITMAPOBJ *bmp = GDI_GetObjPtr( handle, OBJ_BITMAP );
626 if (!buffer) ret = sizeof(BITMAP);
627 else if (count >= sizeof(BITMAP))
629 BITMAP *bitmap = buffer;
630 *bitmap = bmp->dib.dsBm;
631 bitmap->bmBits = NULL;
632 ret = sizeof(BITMAP);
634 GDI_ReleaseObj( handle );
639 /******************************************************************************
640 * CreateDiscardableBitmap [GDI32.@]
642 * Creates a discardable bitmap.
645 * Success: Handle to bitmap
648 HBITMAP WINAPI CreateDiscardableBitmap(
649 HDC hdc, /* [in] Handle to device context */
650 INT width, /* [in] Bitmap width */
651 INT height) /* [in] Bitmap height */
653 return CreateCompatibleBitmap( hdc, width, height );
657 /******************************************************************************
658 * GetBitmapDimensionEx [GDI32.@]
660 * Retrieves dimensions of a bitmap.
666 BOOL WINAPI GetBitmapDimensionEx(
667 HBITMAP hbitmap, /* [in] Handle to bitmap */
668 LPSIZE size) /* [out] Address of struct receiving dimensions */
670 BITMAPOBJ * bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
671 if (!bmp) return FALSE;
673 GDI_ReleaseObj( hbitmap );
678 /******************************************************************************
679 * SetBitmapDimensionEx [GDI32.@]
681 * Assigns dimensions to a bitmap.
682 * MSDN says that this function will fail if hbitmap is a handle created by
683 * CreateDIBSection, but that's not true on Windows 2000.
689 BOOL WINAPI SetBitmapDimensionEx(
690 HBITMAP hbitmap, /* [in] Handle to bitmap */
691 INT x, /* [in] Bitmap width */
692 INT y, /* [in] Bitmap height */
693 LPSIZE prevSize) /* [out] Address of structure for orig dims */
695 BITMAPOBJ * bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
696 if (!bmp) return FALSE;
697 if (prevSize) *prevSize = bmp->size;
700 GDI_ReleaseObj( hbitmap );