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 )
57 int height, width_bytes;
59 if (!hbitmap) return ERROR_NOT_SUPPORTED;
60 if (!(bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP ))) return ERROR_INVALID_HANDLE;
62 info->bmiHeader.biSize = sizeof(info->bmiHeader);
63 info->bmiHeader.biPlanes = 1;
64 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
65 info->bmiHeader.biCompression = BI_RGB;
66 info->bmiHeader.biXPelsPerMeter = 0;
67 info->bmiHeader.biYPelsPerMeter = 0;
68 info->bmiHeader.biClrUsed = 0;
69 info->bmiHeader.biClrImportant = 0;
70 if (bmp->bitmap.bmBitsPixel == 16)
72 DWORD *masks = (DWORD *)info->bmiColors;
76 info->bmiHeader.biCompression = BI_BITFIELDS;
80 height = src->visrect.bottom - src->visrect.top;
81 width_bytes = get_dib_stride( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
82 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
83 info->bmiHeader.biHeight = -height;
84 info->bmiHeader.biSizeImage = height * width_bytes;
86 /* make the source rectangle relative to the returned bits */
87 src->y -= src->visrect.top;
88 offset_rect( &src->visrect, 0, -src->visrect.top );
90 if (bmp->bitmap.bmBits && bmp->bitmap.bmWidthBytes == width_bytes)
92 bits->ptr = (char *)bmp->bitmap.bmBits + src->visrect.top * width_bytes;
93 bits->is_copy = FALSE;
98 bits->ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
100 bits->free = free_heap_bits;
101 if (bmp->bitmap.bmBits)
103 /* fixup the line alignment */
104 char *src = bmp->bitmap.bmBits, *dst = bits->ptr;
105 for ( ; height > 0; height--, src += bmp->bitmap.bmWidthBytes, dst += width_bytes)
107 memcpy( dst, src, bmp->bitmap.bmWidthBytes );
108 memset( dst + bmp->bitmap.bmWidthBytes, 0, width_bytes - bmp->bitmap.bmWidthBytes );
111 else memset( bits->ptr, 0, info->bmiHeader.biSizeImage );
114 GDI_ReleaseObj( hbitmap );
115 return ERROR_SUCCESS;
118 DWORD nulldrv_PutImage( PHYSDEV dev, HBITMAP hbitmap, HRGN clip, BITMAPINFO *info,
119 const struct gdi_image_bits *bits, struct bitblt_coords *src,
120 struct bitblt_coords *dst, DWORD rop )
124 if (!hbitmap) return ERROR_SUCCESS;
125 if (!(bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP ))) return ERROR_INVALID_HANDLE;
127 if (info->bmiHeader.biPlanes != 1) goto update_format;
128 if (info->bmiHeader.biBitCount != bmp->bitmap.bmBitsPixel) goto update_format;
129 /* FIXME: check color masks */
133 int i, width_bytes = get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
134 unsigned char *dst_bits, *src_bits;
136 if ((src->width != dst->width) || (src->height != dst->height))
138 GDI_ReleaseObj( hbitmap );
139 return ERROR_TRANSFORM_NOT_SUPPORTED;
141 if (src->visrect.left > 0 || src->visrect.right < bmp->bitmap.bmWidth ||
142 dst->visrect.left > 0 || dst->visrect.right < bmp->bitmap.bmWidth)
144 FIXME( "setting partial rows not supported\n" );
145 GDI_ReleaseObj( hbitmap );
146 return ERROR_NOT_SUPPORTED;
150 FIXME( "clip region not supported\n" );
151 GDI_ReleaseObj( hbitmap );
152 return ERROR_NOT_SUPPORTED;
155 if (!bmp->bitmap.bmBits &&
156 !(bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0,
157 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes )))
159 GDI_ReleaseObj( hbitmap );
160 return ERROR_OUTOFMEMORY;
162 src_bits = bits->ptr;
163 if (info->bmiHeader.biHeight > 0)
165 src_bits += (info->bmiHeader.biHeight - 1 - src->visrect.top) * width_bytes;
166 width_bytes = -width_bytes;
169 src_bits += src->visrect.top * width_bytes;
171 dst_bits = (unsigned char *)bmp->bitmap.bmBits + dst->visrect.top * bmp->bitmap.bmWidthBytes;
172 if (width_bytes != bmp->bitmap.bmWidthBytes)
174 for (i = 0; i < dst->visrect.bottom - dst->visrect.top; i++)
176 memcpy( dst_bits, src_bits, min( abs(width_bytes), bmp->bitmap.bmWidthBytes ));
177 src_bits += width_bytes;
178 dst_bits += bmp->bitmap.bmWidthBytes;
181 else memcpy( dst_bits, src_bits, width_bytes * (dst->visrect.bottom - dst->visrect.top) );
183 GDI_ReleaseObj( hbitmap );
184 return ERROR_SUCCESS;
187 info->bmiHeader.biPlanes = 1;
188 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
189 if (info->bmiHeader.biHeight > 0) info->bmiHeader.biHeight = -info->bmiHeader.biHeight;
190 GDI_ReleaseObj( hbitmap );
191 return ERROR_BAD_FORMAT;
195 /******************************************************************************
196 * CreateBitmap [GDI32.@]
198 * Creates a bitmap with the specified info.
201 * width [I] bitmap width
202 * height [I] bitmap height
203 * planes [I] Number of color planes
204 * bpp [I] Number of bits to identify a color
205 * bits [I] Pointer to array containing color data
208 * Success: Handle to bitmap
211 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
212 UINT bpp, LPCVOID bits )
218 bm.bmHeight = height;
219 bm.bmWidthBytes = get_bitmap_stride( width, bpp );
220 bm.bmPlanes = planes;
221 bm.bmBitsPixel = bpp;
222 bm.bmBits = (LPVOID)bits;
224 return CreateBitmapIndirect( &bm );
227 /******************************************************************************
228 * CreateCompatibleBitmap [GDI32.@]
230 * Creates a bitmap compatible with the DC.
233 * hdc [I] Handle to device context
234 * width [I] Width of bitmap
235 * height [I] Height of bitmap
238 * Success: Handle to bitmap
241 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
245 TRACE("(%p,%d,%d) =\n", hdc, width, height);
247 if (GetObjectType( hdc ) != OBJ_MEMDC)
249 hbmpRet = CreateBitmap(width, height,
250 GetDeviceCaps(hdc, PLANES),
251 GetDeviceCaps(hdc, BITSPIXEL),
257 HBITMAP bitmap = GetCurrentObject( hdc, OBJ_BITMAP );
258 INT size = GetObjectW( bitmap, sizeof(dib), &dib );
262 if (size == sizeof(BITMAP))
264 /* A device-dependent bitmap is selected in the DC */
265 hbmpRet = CreateBitmap(width, height,
267 dib.dsBm.bmBitsPixel,
272 /* A DIB section is selected in the DC */
276 /* Allocate memory for a BITMAPINFOHEADER structure and a
277 color table. The maximum number of colors in a color table
278 is 256 which corresponds to a bitmap with depth 8.
279 Bitmaps with higher depths don't have color tables. */
280 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
284 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
285 bi->bmiHeader.biWidth = width;
286 bi->bmiHeader.biHeight = height;
287 bi->bmiHeader.biPlanes = dib.dsBmih.biPlanes;
288 bi->bmiHeader.biBitCount = dib.dsBmih.biBitCount;
289 bi->bmiHeader.biCompression = dib.dsBmih.biCompression;
290 bi->bmiHeader.biSizeImage = 0;
291 bi->bmiHeader.biXPelsPerMeter = dib.dsBmih.biXPelsPerMeter;
292 bi->bmiHeader.biYPelsPerMeter = dib.dsBmih.biYPelsPerMeter;
293 bi->bmiHeader.biClrUsed = dib.dsBmih.biClrUsed;
294 bi->bmiHeader.biClrImportant = dib.dsBmih.biClrImportant;
296 if (bi->bmiHeader.biCompression == BI_BITFIELDS)
298 /* Copy the color masks */
299 CopyMemory(bi->bmiColors, dib.dsBitfields, 3 * sizeof(DWORD));
301 else if (bi->bmiHeader.biBitCount <= 8)
303 /* Copy the color table */
304 GetDIBColorTable(hdc, 0, 256, bi->bmiColors);
307 hbmpRet = CreateDIBSection(hdc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
308 HeapFree(GetProcessHeap(), 0, bi);
313 TRACE("\t\t%p\n", hbmpRet);
318 /******************************************************************************
319 * CreateBitmapIndirect [GDI32.@]
321 * Creates a bitmap with the specified info.
324 * bmp [I] Pointer to the bitmap info describing the bitmap
327 * Success: Handle to bitmap
328 * Failure: NULL. Use GetLastError() to determine the cause.
331 * If a width or height of 0 are given, a 1x1 monochrome bitmap is returned.
333 HBITMAP WINAPI CreateBitmapIndirect( const BITMAP *bmp )
339 if (!bmp || bmp->bmType)
341 SetLastError( ERROR_INVALID_PARAMETER );
345 if (bmp->bmWidth > 0x7ffffff || bmp->bmHeight > 0x7ffffff)
347 SetLastError( ERROR_INVALID_PARAMETER );
353 if (!bm.bmWidth || !bm.bmHeight)
355 return GetStockObject( DEFAULT_BITMAP );
360 bm.bmHeight = -bm.bmHeight;
362 bm.bmWidth = -bm.bmWidth;
365 if (bm.bmPlanes != 1)
367 FIXME("planes = %d\n", bm.bmPlanes);
368 SetLastError( ERROR_INVALID_PARAMETER );
372 /* Windows only uses 1, 4, 8, 16, 24 and 32 bpp */
373 if(bm.bmBitsPixel == 1) bm.bmBitsPixel = 1;
374 else if(bm.bmBitsPixel <= 4) bm.bmBitsPixel = 4;
375 else if(bm.bmBitsPixel <= 8) bm.bmBitsPixel = 8;
376 else if(bm.bmBitsPixel <= 16) bm.bmBitsPixel = 16;
377 else if(bm.bmBitsPixel <= 24) bm.bmBitsPixel = 24;
378 else if(bm.bmBitsPixel <= 32) bm.bmBitsPixel = 32;
380 WARN("Invalid bmBitsPixel %d, returning ERROR_INVALID_PARAMETER\n", bm.bmBitsPixel);
381 SetLastError(ERROR_INVALID_PARAMETER);
385 /* Windows ignores the provided bm.bmWidthBytes */
386 bm.bmWidthBytes = get_bitmap_stride( bm.bmWidth, bm.bmBitsPixel );
387 /* XP doesn't allow to create bitmaps larger than 128 Mb */
388 if (bm.bmHeight > 128 * 1024 * 1024 / bm.bmWidthBytes)
390 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
394 /* Create the BITMAPOBJ */
395 if (!(bmpobj = HeapAlloc( GetProcessHeap(), 0, sizeof(*bmpobj) )))
397 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
404 bmpobj->bitmap.bmBits = NULL;
405 bmpobj->funcs = &null_driver;
407 bmpobj->color_table = NULL;
408 bmpobj->nb_colors = 0;
410 if (!(hbitmap = alloc_gdi_handle( &bmpobj->header, OBJ_BITMAP, &bitmap_funcs )))
412 HeapFree( GetProcessHeap(), 0, bmpobj );
417 SetBitmapBits( hbitmap, bm.bmHeight * bm.bmWidthBytes, bm.bmBits );
419 TRACE("%dx%d, %d colors returning %p\n", bm.bmWidth, bm.bmHeight,
420 1 << (bm.bmPlanes * bm.bmBitsPixel), hbitmap);
426 /* convenience wrapper for GetImage to retrieve the full contents of a bitmap */
427 BOOL get_bitmap_image( HBITMAP hbitmap, BITMAPINFO *info, struct gdi_image_bits *bits )
429 struct bitblt_coords src;
431 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
435 src.visrect.left = src.x = 0;
436 src.visrect.top = src.y = 0;
437 src.visrect.right = src.width = bmp->bitmap.bmWidth;
438 src.visrect.bottom = src.height = bmp->bitmap.bmHeight;
439 ret = !bmp->funcs->pGetImage( NULL, hbitmap, info, bits, &src );
440 GDI_ReleaseObj( hbitmap );
446 /***********************************************************************
447 * GetBitmapBits [GDI32.@]
449 * Copies bitmap bits of bitmap to buffer.
452 * Success: Number of bytes copied
455 LONG WINAPI GetBitmapBits(
456 HBITMAP hbitmap, /* [in] Handle to bitmap */
457 LONG count, /* [in] Number of bytes to copy */
458 LPVOID bits) /* [out] Pointer to buffer to receive bits */
460 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
461 BITMAPINFO *info = (BITMAPINFO *)buffer;
462 struct gdi_image_bits src_bits;
463 struct bitblt_coords src;
464 int dst_stride, max, ret;
465 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
466 const struct gdi_dc_funcs *funcs;
469 funcs = get_bitmap_funcs( bmp );
471 if (bmp->dib) dst_stride = get_bitmap_stride( bmp->dib->dsBmih.biWidth, bmp->dib->dsBmih.biBitCount );
472 else dst_stride = get_bitmap_stride( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
474 ret = max = dst_stride * bmp->bitmap.bmHeight;
475 if (!bits) goto done;
476 if (count > max) count = max;
479 src.visrect.left = 0;
480 src.visrect.right = bmp->bitmap.bmWidth;
482 src.visrect.bottom = (count + dst_stride - 1) / dst_stride;
484 src.width = src.visrect.right - src.visrect.left;
485 src.height = src.visrect.bottom - src.visrect.top;
487 if (!funcs->pGetImage( NULL, hbitmap, info, &src_bits, &src ))
489 const char *src_ptr = src_bits.ptr;
490 int src_stride = get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
492 /* GetBitmapBits returns 16-bit aligned data */
494 if (info->bmiHeader.biHeight > 0)
496 src_ptr += (info->bmiHeader.biHeight - 1) * src_stride;
497 src_stride = -src_stride;
499 src_ptr += src.visrect.top * src_stride;
501 if (src_stride == dst_stride) memcpy( bits, src_ptr, count );
502 else while (count > 0)
504 memcpy( bits, src_ptr, min( count, dst_stride ) );
505 src_ptr += src_stride;
506 bits = (char *)bits + dst_stride;
509 if (src_bits.free) src_bits.free( &src_bits );
514 GDI_ReleaseObj( hbitmap );
519 /******************************************************************************
520 * SetBitmapBits [GDI32.@]
522 * Sets bits of color data for a bitmap.
525 * Success: Number of bytes used in setting the bitmap bits
528 LONG WINAPI SetBitmapBits(
529 HBITMAP hbitmap, /* [in] Handle to bitmap */
530 LONG count, /* [in] Number of bytes in bitmap array */
531 LPCVOID bits) /* [in] Address of array with bitmap bits */
533 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
534 BITMAPINFO *info = (BITMAPINFO *)buffer;
537 int i, src_stride, dst_stride;
538 struct bitblt_coords src, dst;
539 struct gdi_image_bits src_bits;
541 const struct gdi_dc_funcs *funcs;
545 bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
548 funcs = get_bitmap_funcs( bmp );
551 WARN("(%d): Negative number of bytes passed???\n", count );
555 if (bmp->dib) src_stride = get_bitmap_stride( bmp->dib->dsBmih.biWidth, bmp->dib->dsBmih.biBitCount );
556 else src_stride = get_bitmap_stride( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
557 count = min( count, src_stride * bmp->bitmap.bmHeight );
559 dst_stride = get_dib_stride( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
561 src.visrect.left = src.x = 0;
562 src.visrect.top = src.y = 0;
563 src.visrect.right = src.width = bmp->bitmap.bmWidth;
564 src.visrect.bottom = src.height = (count + src_stride - 1 ) / src_stride;
567 if (count % src_stride)
570 int extra_pixels = ((count % src_stride) << 3) / bmp->bitmap.bmBitsPixel;
572 if ((count % src_stride << 3) % bmp->bitmap.bmBitsPixel)
573 FIXME( "Unhandled partial pixel\n" );
574 clip = CreateRectRgn( src.visrect.left, src.visrect.top,
575 src.visrect.right, src.visrect.bottom - 1 );
576 last_row = CreateRectRgn( src.visrect.left, src.visrect.bottom - 1,
577 src.visrect.left + extra_pixels, src.visrect.bottom );
578 CombineRgn( clip, clip, last_row, RGN_OR );
579 DeleteObject( last_row );
582 TRACE("(%p, %d, %p) %dx%d %d bpp fetched height: %d\n",
583 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
584 bmp->bitmap.bmBitsPixel, src.height );
586 if (src_stride == dst_stride)
588 src_bits.ptr = (void *)bits;
589 src_bits.is_copy = FALSE;
590 src_bits.free = NULL;
594 if (!(src_bits.ptr = HeapAlloc( GetProcessHeap(), 0, dst.height * dst_stride )))
596 GDI_ReleaseObj( hbitmap );
599 src_bits.is_copy = TRUE;
600 src_bits.free = free_heap_bits;
601 for (i = 0; i < count / src_stride; i++)
602 memcpy( (char *)src_bits.ptr + i * dst_stride, (char *)bits + i * src_stride, src_stride );
603 if (count % src_stride)
604 memcpy( (char *)src_bits.ptr + i * dst_stride, (char *)bits + i * src_stride, count % src_stride );
607 /* query the color info */
608 info->bmiHeader.biSize = sizeof(info->bmiHeader);
609 info->bmiHeader.biPlanes = 1;
610 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
611 info->bmiHeader.biCompression = BI_RGB;
612 info->bmiHeader.biXPelsPerMeter = 0;
613 info->bmiHeader.biYPelsPerMeter = 0;
614 info->bmiHeader.biClrUsed = 0;
615 info->bmiHeader.biClrImportant = 0;
616 info->bmiHeader.biWidth = 0;
617 info->bmiHeader.biHeight = 0;
618 info->bmiHeader.biSizeImage = 0;
619 err = funcs->pPutImage( NULL, hbitmap, 0, info, NULL, NULL, NULL, SRCCOPY );
621 if (!err || err == ERROR_BAD_FORMAT)
623 info->bmiHeader.biPlanes = 1;
624 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
625 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
626 info->bmiHeader.biHeight = -dst.height;
627 info->bmiHeader.biSizeImage = dst.height * dst_stride;
628 err = funcs->pPutImage( NULL, hbitmap, clip, info, &src_bits, &src, &dst, SRCCOPY );
632 if (clip) DeleteObject( clip );
633 if (src_bits.free) src_bits.free( &src_bits );
634 GDI_ReleaseObj( hbitmap );
638 /**********************************************************************
642 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
646 BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
656 GDI_ReleaseObj( hbitmap );
657 dc = CreateCompatibleDC( NULL );
660 if (!(bi = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
665 bi->bmiHeader = dib.dsBmih;
667 /* Get the color table or the color masks */
668 GetDIBits( dc, hbitmap, 0, 0, NULL, bi, DIB_RGB_COLORS );
669 bi->bmiHeader.biHeight = dib.dsBmih.biHeight;
671 res = CreateDIBSection( dc, bi, DIB_RGB_COLORS, &bits, NULL, 0 );
672 if (res) SetDIBits( dc, res, 0, dib.dsBm.bmHeight, dib.dsBm.bmBits, bi, DIB_RGB_COLORS );
673 HeapFree( GetProcessHeap(), 0, bi );
677 dib.dsBm = bmp->bitmap;
678 dib.dsBm.bmBits = NULL;
679 GDI_ReleaseObj( hbitmap );
681 res = CreateBitmapIndirect( &dib.dsBm );
683 char *buf = HeapAlloc( GetProcessHeap(), 0, dib.dsBm.bmWidthBytes * dib.dsBm.bmHeight );
684 GetBitmapBits (hbitmap, dib.dsBm.bmWidthBytes * dib.dsBm.bmHeight, buf);
685 SetBitmapBits (res, dib.dsBm.bmWidthBytes * dib.dsBm.bmHeight, buf);
686 HeapFree( GetProcessHeap(), 0, buf );
692 static void set_initial_bitmap_bits( HBITMAP hbitmap, BITMAPOBJ *bmp )
694 if (!bmp->bitmap.bmBits) return;
695 SetBitmapBits( hbitmap, bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes, bmp->bitmap.bmBits );
698 /***********************************************************************
701 * Set the type of DC that owns the bitmap. This is used when the
702 * bitmap is selected into a device to initialize the bitmap function
705 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, PHYSDEV physdev )
710 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
711 if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
713 if (!(bitmap = GDI_GetObjPtr( hbitmap, OBJ_BITMAP ))) return FALSE;
715 if (!bitmap->dib && bitmap->funcs != physdev->funcs)
717 /* we can only change from the null driver to some other driver */
718 if (bitmap->funcs == &null_driver)
720 if (physdev->funcs->pCreateBitmap)
722 ret = physdev->funcs->pCreateBitmap( physdev, hbitmap );
725 bitmap->funcs = physdev->funcs;
726 set_initial_bitmap_bits( hbitmap, bitmap );
731 WARN( "Trying to select bitmap %p in DC that doesn't support it\n", hbitmap );
737 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
741 GDI_ReleaseObj( hbitmap );
746 /***********************************************************************
747 * BITMAP_SelectObject
749 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, HDC hdc )
754 PHYSDEV physdev = NULL, old_physdev = NULL;
756 if (!(dc = get_dc_ptr( hdc ))) return 0;
758 if (GetObjectType( hdc ) != OBJ_MEMDC)
764 if (handle == dc->hBitmap) goto done; /* nothing to do */
766 if (!(bitmap = GDI_GetObjPtr( handle, OBJ_BITMAP )))
772 if (bitmap->header.selcount && (handle != GetStockObject(DEFAULT_BITMAP)))
774 WARN( "Bitmap already selected in another DC\n" );
775 GDI_ReleaseObj( handle );
780 old_physdev = GET_DC_PHYSDEV( dc, pSelectBitmap );
781 if(old_physdev == &dc->dibdrv.dev)
782 pop_dc_driver( dc, old_physdev );
786 physdev = &dc->dibdrv.dev;
787 push_dc_driver( dc, physdev, physdev->funcs );
790 physdev = GET_DC_PHYSDEV( dc, pSelectBitmap );
792 if (!BITMAP_SetOwnerDC( handle, physdev ))
794 GDI_ReleaseObj( handle );
798 if (!physdev->funcs->pSelectBitmap( physdev, handle ))
800 GDI_ReleaseObj( handle );
805 dc->hBitmap = handle;
806 GDI_inc_ref_count( handle );
808 dc->vis_rect.left = 0;
809 dc->vis_rect.top = 0;
810 dc->vis_rect.right = bitmap->bitmap.bmWidth;
811 dc->vis_rect.bottom = bitmap->bitmap.bmHeight;
812 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
813 GDI_ReleaseObj( handle );
815 GDI_dec_ref_count( ret );
821 if(physdev == &dc->dibdrv.dev) pop_dc_driver( dc, physdev );
822 if(old_physdev == &dc->dibdrv.dev) push_dc_driver( dc, old_physdev, old_physdev->funcs );
824 release_dc_ptr( dc );
829 /***********************************************************************
830 * BITMAP_DeleteObject
832 static BOOL BITMAP_DeleteObject( HGDIOBJ handle )
834 const DC_FUNCTIONS *funcs;
835 BITMAPOBJ *bmp = GDI_GetObjPtr( handle, OBJ_BITMAP );
837 if (!bmp) return FALSE;
839 GDI_ReleaseObj( handle );
841 funcs->pDeleteBitmap( handle );
843 if (!(bmp = free_gdi_handle( handle ))) return FALSE;
845 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
849 DIBSECTION *dib = bmp->dib;
851 if (dib->dsBm.bmBits)
855 SYSTEM_INFO SystemInfo;
856 GetSystemInfo( &SystemInfo );
857 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
858 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
860 else if (!dib->dsOffset)
861 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
863 HeapFree(GetProcessHeap(), 0, dib);
864 HeapFree(GetProcessHeap(), 0, bmp->color_table);
866 return HeapFree( GetProcessHeap(), 0, bmp );
870 /***********************************************************************
873 static INT BITMAP_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
876 BITMAPOBJ *bmp = GDI_GetObjPtr( handle, OBJ_BITMAP );
880 if (!buffer) ret = sizeof(BITMAP);
881 else if (count < sizeof(BITMAP)) ret = 0;
884 if (count >= sizeof(DIBSECTION))
886 DIBSECTION *dib = buffer;
888 dib->dsBmih.biHeight = abs( dib->dsBmih.biHeight );
889 ret = sizeof(DIBSECTION);
891 else /* if (count >= sizeof(BITMAP)) */
893 DIBSECTION *dib = bmp->dib;
894 memcpy( buffer, &dib->dsBm, sizeof(BITMAP) );
895 ret = sizeof(BITMAP);
900 memcpy( buffer, &bmp->bitmap, sizeof(BITMAP) );
901 ((BITMAP *) buffer)->bmBits = NULL;
902 ret = sizeof(BITMAP);
904 GDI_ReleaseObj( handle );
909 /******************************************************************************
910 * CreateDiscardableBitmap [GDI32.@]
912 * Creates a discardable bitmap.
915 * Success: Handle to bitmap
918 HBITMAP WINAPI CreateDiscardableBitmap(
919 HDC hdc, /* [in] Handle to device context */
920 INT width, /* [in] Bitmap width */
921 INT height) /* [in] Bitmap height */
923 return CreateCompatibleBitmap( hdc, width, height );
927 /******************************************************************************
928 * GetBitmapDimensionEx [GDI32.@]
930 * Retrieves dimensions of a bitmap.
936 BOOL WINAPI GetBitmapDimensionEx(
937 HBITMAP hbitmap, /* [in] Handle to bitmap */
938 LPSIZE size) /* [out] Address of struct receiving dimensions */
940 BITMAPOBJ * bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
941 if (!bmp) return FALSE;
943 GDI_ReleaseObj( hbitmap );
948 /******************************************************************************
949 * SetBitmapDimensionEx [GDI32.@]
951 * Assigns dimensions to a bitmap.
952 * MSDN says that this function will fail if hbitmap is a handle created by
953 * CreateDIBSection, but that's not true on Windows 2000.
959 BOOL WINAPI SetBitmapDimensionEx(
960 HBITMAP hbitmap, /* [in] Handle to bitmap */
961 INT x, /* [in] Bitmap width */
962 INT y, /* [in] Bitmap height */
963 LPSIZE prevSize) /* [out] Address of structure for orig dims */
965 BITMAPOBJ * bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
966 if (!bmp) return FALSE;
967 if (prevSize) *prevSize = bmp->size;
970 GDI_ReleaseObj( hbitmap );