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 LONG nulldrv_GetBitmapBits( HBITMAP bitmap, void *bits, LONG size )
55 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
57 if (bmp->bitmap.bmBits) memcpy( bits, bmp->bitmap.bmBits, size );
58 else memset( bits, 0, size );
59 GDI_ReleaseObj( bitmap );
63 LONG nulldrv_SetBitmapBits( HBITMAP bitmap, const void *bits, LONG size )
65 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
67 if (!bmp->bitmap.bmBits)
69 LONG total = bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes; /* alloc enough for entire bitmap */
70 if (!(bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, total )))
72 GDI_ReleaseObj( bitmap );
75 if (size < total) memset( (char *)bmp->bitmap.bmBits + size, 0, total - size );
77 memcpy( bmp->bitmap.bmBits, bits, size );
78 GDI_ReleaseObj( bitmap );
82 INT nulldrv_SetDIBits( PHYSDEV dev, HBITMAP bitmap, UINT start, UINT lines,
83 const void *bits, const BITMAPINFO *info, UINT coloruse )
85 /* FIXME: transfer bits to bmp->bitmap.bmBits */
89 /***********************************************************************
90 * BITMAP_GetWidthBytes
92 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
95 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
100 return 2 * ((bmWidth+15) >> 4);
103 bmWidth *= 3; /* fall through */
105 return bmWidth + (bmWidth & 1);
115 return 2 * ((bmWidth+3) >> 2);
118 WARN("Unknown depth %d, please report.\n", bpp );
124 /******************************************************************************
125 * CreateBitmap [GDI32.@]
127 * Creates a bitmap with the specified info.
130 * width [I] bitmap width
131 * height [I] bitmap height
132 * planes [I] Number of color planes
133 * bpp [I] Number of bits to identify a color
134 * bits [I] Pointer to array containing color data
137 * Success: Handle to bitmap
140 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
141 UINT bpp, LPCVOID bits )
147 bm.bmHeight = height;
148 bm.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
149 bm.bmPlanes = planes;
150 bm.bmBitsPixel = bpp;
151 bm.bmBits = (LPVOID)bits;
153 return CreateBitmapIndirect( &bm );
156 /******************************************************************************
157 * CreateCompatibleBitmap [GDI32.@]
159 * Creates a bitmap compatible with the DC.
162 * hdc [I] Handle to device context
163 * width [I] Width of bitmap
164 * height [I] Height of bitmap
167 * Success: Handle to bitmap
170 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
174 TRACE("(%p,%d,%d) =\n", hdc, width, height);
176 if (GetObjectType( hdc ) != OBJ_MEMDC)
178 hbmpRet = CreateBitmap(width, height,
179 GetDeviceCaps(hdc, PLANES),
180 GetDeviceCaps(hdc, BITSPIXEL),
186 HBITMAP bitmap = GetCurrentObject( hdc, OBJ_BITMAP );
187 INT size = GetObjectW( bitmap, sizeof(dib), &dib );
191 if (size == sizeof(BITMAP))
193 /* A device-dependent bitmap is selected in the DC */
194 hbmpRet = CreateBitmap(width, height,
196 dib.dsBm.bmBitsPixel,
201 /* A DIB section is selected in the DC */
205 /* Allocate memory for a BITMAPINFOHEADER structure and a
206 color table. The maximum number of colors in a color table
207 is 256 which corresponds to a bitmap with depth 8.
208 Bitmaps with higher depths don't have color tables. */
209 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
213 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
214 bi->bmiHeader.biWidth = width;
215 bi->bmiHeader.biHeight = height;
216 bi->bmiHeader.biPlanes = dib.dsBmih.biPlanes;
217 bi->bmiHeader.biBitCount = dib.dsBmih.biBitCount;
218 bi->bmiHeader.biCompression = dib.dsBmih.biCompression;
219 bi->bmiHeader.biSizeImage = 0;
220 bi->bmiHeader.biXPelsPerMeter = dib.dsBmih.biXPelsPerMeter;
221 bi->bmiHeader.biYPelsPerMeter = dib.dsBmih.biYPelsPerMeter;
222 bi->bmiHeader.biClrUsed = dib.dsBmih.biClrUsed;
223 bi->bmiHeader.biClrImportant = dib.dsBmih.biClrImportant;
225 if (bi->bmiHeader.biCompression == BI_BITFIELDS)
227 /* Copy the color masks */
228 CopyMemory(bi->bmiColors, dib.dsBitfields, 3 * sizeof(DWORD));
230 else if (bi->bmiHeader.biBitCount <= 8)
232 /* Copy the color table */
233 GetDIBColorTable(hdc, 0, 256, bi->bmiColors);
236 hbmpRet = CreateDIBSection(hdc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
237 HeapFree(GetProcessHeap(), 0, bi);
242 TRACE("\t\t%p\n", hbmpRet);
247 /******************************************************************************
248 * CreateBitmapIndirect [GDI32.@]
250 * Creates a bitmap with the specified info.
253 * bmp [I] Pointer to the bitmap info describing the bitmap
256 * Success: Handle to bitmap
257 * Failure: NULL. Use GetLastError() to determine the cause.
260 * If a width or height of 0 are given, a 1x1 monochrome bitmap is returned.
262 HBITMAP WINAPI CreateBitmapIndirect( const BITMAP *bmp )
268 if (!bmp || bmp->bmType)
270 SetLastError( ERROR_INVALID_PARAMETER );
274 if (bmp->bmWidth > 0x7ffffff || bmp->bmHeight > 0x7ffffff)
276 SetLastError( ERROR_INVALID_PARAMETER );
282 if (!bm.bmWidth || !bm.bmHeight)
284 return GetStockObject( DEFAULT_BITMAP );
289 bm.bmHeight = -bm.bmHeight;
291 bm.bmWidth = -bm.bmWidth;
294 if (bm.bmPlanes != 1)
296 FIXME("planes = %d\n", bm.bmPlanes);
297 SetLastError( ERROR_INVALID_PARAMETER );
301 /* Windows only uses 1, 4, 8, 16, 24 and 32 bpp */
302 if(bm.bmBitsPixel == 1) bm.bmBitsPixel = 1;
303 else if(bm.bmBitsPixel <= 4) bm.bmBitsPixel = 4;
304 else if(bm.bmBitsPixel <= 8) bm.bmBitsPixel = 8;
305 else if(bm.bmBitsPixel <= 16) bm.bmBitsPixel = 16;
306 else if(bm.bmBitsPixel <= 24) bm.bmBitsPixel = 24;
307 else if(bm.bmBitsPixel <= 32) bm.bmBitsPixel = 32;
309 WARN("Invalid bmBitsPixel %d, returning ERROR_INVALID_PARAMETER\n", bm.bmBitsPixel);
310 SetLastError(ERROR_INVALID_PARAMETER);
314 /* Windows ignores the provided bm.bmWidthBytes */
315 bm.bmWidthBytes = BITMAP_GetWidthBytes( bm.bmWidth, bm.bmBitsPixel );
316 /* XP doesn't allow to create bitmaps larger than 128 Mb */
317 if (bm.bmHeight > 128 * 1024 * 1024 / bm.bmWidthBytes)
319 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
323 /* Create the BITMAPOBJ */
324 if (!(bmpobj = HeapAlloc( GetProcessHeap(), 0, sizeof(*bmpobj) )))
326 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
333 bmpobj->bitmap.bmBits = NULL;
334 bmpobj->funcs = &null_driver;
336 bmpobj->color_table = NULL;
337 bmpobj->nb_colors = 0;
339 if (!(hbitmap = alloc_gdi_handle( &bmpobj->header, OBJ_BITMAP, &bitmap_funcs )))
341 HeapFree( GetProcessHeap(), 0, bmpobj );
346 SetBitmapBits( hbitmap, bm.bmHeight * bm.bmWidthBytes, bm.bmBits );
348 TRACE("%dx%d, %d colors returning %p\n", bm.bmWidth, bm.bmHeight,
349 1 << (bm.bmPlanes * bm.bmBitsPixel), hbitmap);
355 /***********************************************************************
356 * GetBitmapBits [GDI32.@]
358 * Copies bitmap bits of bitmap to buffer.
361 * Success: Number of bytes copied
364 LONG WINAPI GetBitmapBits(
365 HBITMAP hbitmap, /* [in] Handle to bitmap */
366 LONG count, /* [in] Number of bytes to copy */
367 LPVOID bits) /* [out] Pointer to buffer to receive bits */
369 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
374 if (bmp->dib) /* simply copy the bits from the DIB */
376 DIBSECTION *dib = bmp->dib;
377 const char *src = dib->dsBm.bmBits;
378 INT width_bytes = BITMAP_GetWidthBytes(dib->dsBm.bmWidth, dib->dsBm.bmBitsPixel);
379 LONG max = width_bytes * bmp->bitmap.bmHeight;
387 if (count > max) count = max;
390 /* GetBitmapBits returns not 32-bit aligned data */
392 if (bmp->dib->dsBmih.biHeight >= 0) /* not top-down, need to flip contents vertically */
394 src += dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
397 src -= dib->dsBm.bmWidthBytes;
398 memcpy( bits, src, min( count, width_bytes ) );
399 bits = (char *)bits + width_bytes;
400 count -= width_bytes;
407 memcpy( bits, src, min( count, width_bytes ) );
408 src += dib->dsBm.bmWidthBytes;
409 bits = (char *)bits + width_bytes;
410 count -= width_bytes;
416 /* If the bits vector is null, the function should return the read size */
419 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
424 WARN("(%d): Negative number of bytes passed???\n", count );
428 /* Only get entire lines */
429 height = count / bmp->bitmap.bmWidthBytes;
430 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
431 count = height * bmp->bitmap.bmWidthBytes;
434 WARN("Less than one entire line requested\n");
440 TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
441 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
442 1 << bmp->bitmap.bmBitsPixel, height );
444 ret = bmp->funcs->pGetBitmapBits( hbitmap, bits, count );
446 GDI_ReleaseObj( hbitmap );
451 /******************************************************************************
452 * SetBitmapBits [GDI32.@]
454 * Sets bits of color data for a bitmap.
457 * Success: Number of bytes used in setting the bitmap bits
460 LONG WINAPI SetBitmapBits(
461 HBITMAP hbitmap, /* [in] Handle to bitmap */
462 LONG count, /* [in] Number of bytes in bitmap array */
463 LPCVOID bits) /* [in] Address of array with bitmap bits */
470 bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
474 WARN("(%d): Negative number of bytes passed???\n", count );
478 if (bmp->dib) /* simply copy the bits into the DIB */
480 DIBSECTION *dib = bmp->dib;
481 char *dest = dib->dsBm.bmBits;
482 LONG max = dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
483 if (count > max) count = max;
486 if (bmp->dib->dsBmih.biHeight >= 0) /* not top-down, need to flip contents vertically */
488 dest += dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
491 dest -= dib->dsBm.bmWidthBytes;
492 memcpy( dest, bits, min( count, dib->dsBm.bmWidthBytes ) );
493 bits = (const char *)bits + dib->dsBm.bmWidthBytes;
494 count -= dib->dsBm.bmWidthBytes;
497 else memcpy( dest, bits, count );
499 GDI_ReleaseObj( hbitmap );
503 /* Only get entire lines */
504 height = count / bmp->bitmap.bmWidthBytes;
505 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
506 count = height * bmp->bitmap.bmWidthBytes;
508 TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
509 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
510 1 << bmp->bitmap.bmBitsPixel, height );
512 ret = bmp->funcs->pSetBitmapBits( hbitmap, bits, count );
513 GDI_ReleaseObj( hbitmap );
517 /**********************************************************************
521 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
525 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
535 GDI_ReleaseObj( hbitmap );
536 dc = CreateCompatibleDC( NULL );
539 if (!(bi = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
544 bi->bmiHeader = dib.dsBmih;
546 /* Get the color table or the color masks */
547 GetDIBits( dc, hbitmap, 0, 0, NULL, bi, DIB_RGB_COLORS );
548 bi->bmiHeader.biHeight = dib.dsBmih.biHeight;
550 res = CreateDIBSection( dc, bi, DIB_RGB_COLORS, &bits, NULL, 0 );
551 if (res) SetDIBits( dc, res, 0, dib.dsBm.bmHeight, dib.dsBm.bmBits, bi, DIB_RGB_COLORS );
552 HeapFree( GetProcessHeap(), 0, bi );
556 dib.dsBm = bmp->bitmap;
557 dib.dsBm.bmBits = NULL;
558 GDI_ReleaseObj( hbitmap );
560 res = CreateBitmapIndirect( &dib.dsBm );
562 char *buf = HeapAlloc( GetProcessHeap(), 0, dib.dsBm.bmWidthBytes * dib.dsBm.bmHeight );
563 GetBitmapBits (hbitmap, dib.dsBm.bmWidthBytes * dib.dsBm.bmHeight, buf);
564 SetBitmapBits (res, dib.dsBm.bmWidthBytes * dib.dsBm.bmHeight, buf);
565 HeapFree( GetProcessHeap(), 0, buf );
571 /***********************************************************************
574 * Set the type of DC that owns the bitmap. This is used when the
575 * bitmap is selected into a device to initialize the bitmap function
578 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, PHYSDEV physdev )
583 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
584 if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
586 if (!(bitmap = GDI_GetObjPtr( hbitmap, OBJ_BITMAP ))) return FALSE;
588 if (!bitmap->dib && bitmap->funcs != physdev->funcs)
590 /* we can only change from the null driver to some other driver */
591 if (bitmap->funcs == &null_driver)
593 if (physdev->funcs->pCreateBitmap)
595 ret = physdev->funcs->pCreateBitmap( physdev, hbitmap, bitmap->bitmap.bmBits );
596 if (ret) bitmap->funcs = physdev->funcs;
600 WARN( "Trying to select bitmap %p in DC that doesn't support it\n", hbitmap );
606 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
610 GDI_ReleaseObj( hbitmap );
615 /***********************************************************************
616 * BITMAP_SelectObject
618 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, HDC hdc )
623 PHYSDEV physdev = NULL, old_physdev = NULL;
625 if (!(dc = get_dc_ptr( hdc ))) return 0;
627 if (GetObjectType( hdc ) != OBJ_MEMDC)
633 if (handle == dc->hBitmap) goto done; /* nothing to do */
635 if (!(bitmap = GDI_GetObjPtr( handle, OBJ_BITMAP )))
641 if (bitmap->header.selcount && (handle != GetStockObject(DEFAULT_BITMAP)))
643 WARN( "Bitmap already selected in another DC\n" );
644 GDI_ReleaseObj( handle );
649 old_physdev = GET_DC_PHYSDEV( dc, pSelectBitmap );
650 if(old_physdev == &dc->dibdrv.dev)
651 pop_dc_driver( dc, old_physdev );
655 physdev = &dc->dibdrv.dev;
656 push_dc_driver( dc, physdev, physdev->funcs );
659 physdev = GET_DC_PHYSDEV( dc, pSelectBitmap );
661 if (!BITMAP_SetOwnerDC( handle, physdev ))
663 GDI_ReleaseObj( handle );
667 if (!physdev->funcs->pSelectBitmap( physdev, handle ))
669 GDI_ReleaseObj( handle );
674 dc->hBitmap = handle;
675 GDI_inc_ref_count( handle );
677 dc->vis_rect.left = 0;
678 dc->vis_rect.top = 0;
679 dc->vis_rect.right = bitmap->bitmap.bmWidth;
680 dc->vis_rect.bottom = bitmap->bitmap.bmHeight;
681 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
682 GDI_ReleaseObj( handle );
684 GDI_dec_ref_count( ret );
690 if(physdev == &dc->dibdrv.dev) pop_dc_driver( dc, physdev );
691 if(old_physdev == &dc->dibdrv.dev) push_dc_driver( dc, old_physdev, old_physdev->funcs );
693 release_dc_ptr( dc );
698 /***********************************************************************
699 * BITMAP_DeleteObject
701 static BOOL BITMAP_DeleteObject( HGDIOBJ handle )
703 const DC_FUNCTIONS *funcs;
704 BITMAPOBJ *bmp = GDI_GetObjPtr( handle, OBJ_BITMAP );
706 if (!bmp) return FALSE;
708 GDI_ReleaseObj( handle );
710 funcs->pDeleteBitmap( handle );
712 if (!(bmp = free_gdi_handle( handle ))) return FALSE;
714 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
718 DIBSECTION *dib = bmp->dib;
720 if (dib->dsBm.bmBits)
724 SYSTEM_INFO SystemInfo;
725 GetSystemInfo( &SystemInfo );
726 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
727 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
729 else if (!dib->dsOffset)
730 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
732 HeapFree(GetProcessHeap(), 0, dib);
733 HeapFree(GetProcessHeap(), 0, bmp->color_table);
735 return HeapFree( GetProcessHeap(), 0, bmp );
739 /***********************************************************************
742 static INT BITMAP_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
745 BITMAPOBJ *bmp = GDI_GetObjPtr( handle, OBJ_BITMAP );
749 if (!buffer) ret = sizeof(BITMAP);
750 else if (count < sizeof(BITMAP)) ret = 0;
753 if (count >= sizeof(DIBSECTION))
755 DIBSECTION *dib = buffer;
757 dib->dsBmih.biHeight = abs( dib->dsBmih.biHeight );
758 ret = sizeof(DIBSECTION);
760 else /* if (count >= sizeof(BITMAP)) */
762 DIBSECTION *dib = bmp->dib;
763 memcpy( buffer, &dib->dsBm, sizeof(BITMAP) );
764 ret = sizeof(BITMAP);
769 memcpy( buffer, &bmp->bitmap, sizeof(BITMAP) );
770 ((BITMAP *) buffer)->bmBits = NULL;
771 ret = sizeof(BITMAP);
773 GDI_ReleaseObj( handle );
778 /******************************************************************************
779 * CreateDiscardableBitmap [GDI32.@]
781 * Creates a discardable bitmap.
784 * Success: Handle to bitmap
787 HBITMAP WINAPI CreateDiscardableBitmap(
788 HDC hdc, /* [in] Handle to device context */
789 INT width, /* [in] Bitmap width */
790 INT height) /* [in] Bitmap height */
792 return CreateCompatibleBitmap( hdc, width, height );
796 /******************************************************************************
797 * GetBitmapDimensionEx [GDI32.@]
799 * Retrieves dimensions of a bitmap.
805 BOOL WINAPI GetBitmapDimensionEx(
806 HBITMAP hbitmap, /* [in] Handle to bitmap */
807 LPSIZE size) /* [out] Address of struct receiving dimensions */
809 BITMAPOBJ * bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
810 if (!bmp) return FALSE;
812 GDI_ReleaseObj( hbitmap );
817 /******************************************************************************
818 * SetBitmapDimensionEx [GDI32.@]
820 * Assigns dimensions to a bitmap.
821 * MSDN says that this function will fail if hbitmap is a handle created by
822 * CreateDIBSection, but that's not true on Windows 2000.
828 BOOL WINAPI SetBitmapDimensionEx(
829 HBITMAP hbitmap, /* [in] Handle to bitmap */
830 INT x, /* [in] Bitmap width */
831 INT y, /* [in] Bitmap height */
832 LPSIZE prevSize) /* [out] Address of structure for orig dims */
834 BITMAPOBJ * bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
835 if (!bmp) return FALSE;
836 if (prevSize) *prevSize = bmp->size;
839 GDI_ReleaseObj( hbitmap );