gdi32: Get rid of the GetBitmapBits driver entry point.
[wine] / dlls / gdi32 / bitmap.c
1 /*
2  * GDI bitmap objects
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1998 Huw D M Davies
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "gdi_private.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
33
34
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 );
38
39 static const struct gdi_obj_funcs bitmap_funcs =
40 {
41     BITMAP_SelectObject,  /* pSelectObject */
42     BITMAP_GetObject,     /* pGetObjectA */
43     BITMAP_GetObject,     /* pGetObjectW */
44     NULL,                 /* pUnrealizeObject */
45     BITMAP_DeleteObject   /* pDeleteObject */
46 };
47
48
49 /***********************************************************************
50  *           null driver fallback implementations
51  */
52
53 LONG nulldrv_SetBitmapBits( HBITMAP bitmap, const void *bits, LONG size )
54 {
55     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
56
57     if (!bmp->bitmap.bmBits)
58     {
59         LONG total = bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes;  /* alloc enough for entire bitmap */
60         if (!(bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, total )))
61         {
62             GDI_ReleaseObj( bitmap );
63             return 0;
64         }
65         if (size < total) memset( (char *)bmp->bitmap.bmBits + size, 0, total - size );
66     }
67     memcpy( bmp->bitmap.bmBits, bits, size );
68     GDI_ReleaseObj( bitmap );
69     return size;
70 }
71
72 DWORD nulldrv_GetImage( PHYSDEV dev, HBITMAP hbitmap, BITMAPINFO *info,
73                         struct gdi_image_bits *bits, struct bitblt_coords *src )
74 {
75     BITMAPOBJ *bmp;
76     int height, width_bytes;
77
78     if (!hbitmap) return ERROR_NOT_SUPPORTED;
79     if (!(bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP ))) return ERROR_INVALID_HANDLE;
80
81     info->bmiHeader.biSize          = sizeof(info->bmiHeader);
82     info->bmiHeader.biPlanes        = 1;
83     info->bmiHeader.biBitCount      = bmp->bitmap.bmBitsPixel;
84     info->bmiHeader.biCompression   = BI_RGB;
85     info->bmiHeader.biXPelsPerMeter = 0;
86     info->bmiHeader.biYPelsPerMeter = 0;
87     info->bmiHeader.biClrUsed       = 0;
88     info->bmiHeader.biClrImportant  = 0;
89     if (!bits) goto done;
90
91     height = src->visrect.bottom - src->visrect.top;
92     width_bytes = get_dib_stride( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
93     info->bmiHeader.biWidth     = bmp->bitmap.bmWidth;
94     info->bmiHeader.biHeight    = -height;
95     info->bmiHeader.biSizeImage = height * width_bytes;
96
97     /* make the source rectangle relative to the returned bits */
98     src->y -= src->visrect.top;
99     offset_rect( &src->visrect, 0, -src->visrect.top );
100
101     if (bmp->bitmap.bmBits && bmp->bitmap.bmWidthBytes == width_bytes)
102     {
103         bits->ptr = (char *)bmp->bitmap.bmBits + src->visrect.top * width_bytes;
104         bits->is_copy = FALSE;
105         bits->free = NULL;
106     }
107     else
108     {
109         bits->ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
110         bits->is_copy = TRUE;
111         bits->free = free_heap_bits;
112         if (bmp->bitmap.bmBits)
113         {
114             /* fixup the line alignment */
115             char *src = bmp->bitmap.bmBits, *dst = bits->ptr;
116             for ( ; height > 0; height--, src += bmp->bitmap.bmWidthBytes, dst += width_bytes)
117             {
118                 memcpy( dst, src, bmp->bitmap.bmWidthBytes );
119                 memset( dst + bmp->bitmap.bmWidthBytes, 0, width_bytes - bmp->bitmap.bmWidthBytes );
120             }
121         }
122         else memset( bits->ptr, 0, info->bmiHeader.biSizeImage );
123     }
124 done:
125     GDI_ReleaseObj( hbitmap );
126     return ERROR_SUCCESS;
127 }
128
129
130 /******************************************************************************
131  * CreateBitmap [GDI32.@]
132  *
133  * Creates a bitmap with the specified info.
134  *
135  * PARAMS
136  *    width  [I] bitmap width
137  *    height [I] bitmap height
138  *    planes [I] Number of color planes
139  *    bpp    [I] Number of bits to identify a color
140  *    bits   [I] Pointer to array containing color data
141  *
142  * RETURNS
143  *    Success: Handle to bitmap
144  *    Failure: 0
145  */
146 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
147                              UINT bpp, LPCVOID bits )
148 {
149     BITMAP bm;
150
151     bm.bmType = 0;
152     bm.bmWidth = width;
153     bm.bmHeight = height;
154     bm.bmWidthBytes = get_bitmap_stride( width, bpp );
155     bm.bmPlanes = planes;
156     bm.bmBitsPixel = bpp;
157     bm.bmBits = (LPVOID)bits;
158
159     return CreateBitmapIndirect( &bm );
160 }
161
162 /******************************************************************************
163  * CreateCompatibleBitmap [GDI32.@]
164  *
165  * Creates a bitmap compatible with the DC.
166  *
167  * PARAMS
168  *    hdc    [I] Handle to device context
169  *    width  [I] Width of bitmap
170  *    height [I] Height of bitmap
171  *
172  * RETURNS
173  *    Success: Handle to bitmap
174  *    Failure: 0
175  */
176 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
177 {
178     HBITMAP hbmpRet = 0;
179
180     TRACE("(%p,%d,%d) =\n", hdc, width, height);
181
182     if (GetObjectType( hdc ) != OBJ_MEMDC)
183     {
184         hbmpRet = CreateBitmap(width, height,
185                                GetDeviceCaps(hdc, PLANES),
186                                GetDeviceCaps(hdc, BITSPIXEL),
187                                NULL);
188     }
189     else  /* Memory DC */
190     {
191         DIBSECTION dib;
192         HBITMAP bitmap = GetCurrentObject( hdc, OBJ_BITMAP );
193         INT size = GetObjectW( bitmap, sizeof(dib), &dib );
194
195         if (!size) return 0;
196
197         if (size == sizeof(BITMAP))
198         {
199             /* A device-dependent bitmap is selected in the DC */
200             hbmpRet = CreateBitmap(width, height,
201                                    dib.dsBm.bmPlanes,
202                                    dib.dsBm.bmBitsPixel,
203                                    NULL);
204         }
205         else
206         {
207             /* A DIB section is selected in the DC */
208             BITMAPINFO *bi;
209             void *bits;
210
211             /* Allocate memory for a BITMAPINFOHEADER structure and a
212                color table. The maximum number of colors in a color table
213                is 256 which corresponds to a bitmap with depth 8.
214                Bitmaps with higher depths don't have color tables. */
215             bi = HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
216
217             if (bi)
218             {
219                 bi->bmiHeader.biSize          = sizeof(bi->bmiHeader);
220                 bi->bmiHeader.biWidth         = width;
221                 bi->bmiHeader.biHeight        = height;
222                 bi->bmiHeader.biPlanes        = dib.dsBmih.biPlanes;
223                 bi->bmiHeader.biBitCount      = dib.dsBmih.biBitCount;
224                 bi->bmiHeader.biCompression   = dib.dsBmih.biCompression;
225                 bi->bmiHeader.biSizeImage     = 0;
226                 bi->bmiHeader.biXPelsPerMeter = dib.dsBmih.biXPelsPerMeter;
227                 bi->bmiHeader.biYPelsPerMeter = dib.dsBmih.biYPelsPerMeter;
228                 bi->bmiHeader.biClrUsed       = dib.dsBmih.biClrUsed;
229                 bi->bmiHeader.biClrImportant  = dib.dsBmih.biClrImportant;
230
231                 if (bi->bmiHeader.biCompression == BI_BITFIELDS)
232                 {
233                     /* Copy the color masks */
234                     CopyMemory(bi->bmiColors, dib.dsBitfields, 3 * sizeof(DWORD));
235                 }
236                 else if (bi->bmiHeader.biBitCount <= 8)
237                 {
238                     /* Copy the color table */
239                     GetDIBColorTable(hdc, 0, 256, bi->bmiColors);
240                 }
241
242                 hbmpRet = CreateDIBSection(hdc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
243                 HeapFree(GetProcessHeap(), 0, bi);
244             }
245         }
246     }
247
248     TRACE("\t\t%p\n", hbmpRet);
249     return hbmpRet;
250 }
251
252
253 /******************************************************************************
254  * CreateBitmapIndirect [GDI32.@]
255  *
256  * Creates a bitmap with the specified info.
257  *
258  * PARAMS
259  *  bmp [I] Pointer to the bitmap info describing the bitmap
260  *
261  * RETURNS
262  *    Success: Handle to bitmap
263  *    Failure: NULL. Use GetLastError() to determine the cause.
264  *
265  * NOTES
266  *  If a width or height of 0 are given, a 1x1 monochrome bitmap is returned.
267  */
268 HBITMAP WINAPI CreateBitmapIndirect( const BITMAP *bmp )
269 {
270     BITMAP bm;
271     BITMAPOBJ *bmpobj;
272     HBITMAP hbitmap;
273
274     if (!bmp || bmp->bmType)
275     {
276         SetLastError( ERROR_INVALID_PARAMETER );
277         return NULL;
278     }
279
280     if (bmp->bmWidth > 0x7ffffff || bmp->bmHeight > 0x7ffffff)
281     {
282         SetLastError( ERROR_INVALID_PARAMETER );
283         return 0;
284     }
285
286     bm = *bmp;
287
288     if (!bm.bmWidth || !bm.bmHeight)
289     {
290         return GetStockObject( DEFAULT_BITMAP );
291     }
292     else
293     {
294         if (bm.bmHeight < 0)
295             bm.bmHeight = -bm.bmHeight;
296         if (bm.bmWidth < 0)
297             bm.bmWidth = -bm.bmWidth;
298     }
299
300     if (bm.bmPlanes != 1)
301     {
302         FIXME("planes = %d\n", bm.bmPlanes);
303         SetLastError( ERROR_INVALID_PARAMETER );
304         return NULL;
305     }
306
307     /* Windows only uses 1, 4, 8, 16, 24 and 32 bpp */
308     if(bm.bmBitsPixel == 1)         bm.bmBitsPixel = 1;
309     else if(bm.bmBitsPixel <= 4)    bm.bmBitsPixel = 4;
310     else if(bm.bmBitsPixel <= 8)    bm.bmBitsPixel = 8;
311     else if(bm.bmBitsPixel <= 16)   bm.bmBitsPixel = 16;
312     else if(bm.bmBitsPixel <= 24)   bm.bmBitsPixel = 24;
313     else if(bm.bmBitsPixel <= 32)   bm.bmBitsPixel = 32;
314     else {
315         WARN("Invalid bmBitsPixel %d, returning ERROR_INVALID_PARAMETER\n", bm.bmBitsPixel);
316         SetLastError(ERROR_INVALID_PARAMETER);
317         return NULL;
318     }
319
320     /* Windows ignores the provided bm.bmWidthBytes */
321     bm.bmWidthBytes = get_bitmap_stride( bm.bmWidth, bm.bmBitsPixel );
322     /* XP doesn't allow to create bitmaps larger than 128 Mb */
323     if (bm.bmHeight > 128 * 1024 * 1024 / bm.bmWidthBytes)
324     {
325         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
326         return 0;
327     }
328
329     /* Create the BITMAPOBJ */
330     if (!(bmpobj = HeapAlloc( GetProcessHeap(), 0, sizeof(*bmpobj) )))
331     {
332         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
333         return 0;
334     }
335
336     bmpobj->size.cx = 0;
337     bmpobj->size.cy = 0;
338     bmpobj->bitmap = bm;
339     bmpobj->bitmap.bmBits = NULL;
340     bmpobj->funcs = &null_driver;
341     bmpobj->dib = NULL;
342     bmpobj->color_table = NULL;
343     bmpobj->nb_colors = 0;
344
345     if (!(hbitmap = alloc_gdi_handle( &bmpobj->header, OBJ_BITMAP, &bitmap_funcs )))
346     {
347         HeapFree( GetProcessHeap(), 0, bmpobj );
348         return 0;
349     }
350
351     if (bm.bmBits)
352         SetBitmapBits( hbitmap, bm.bmHeight * bm.bmWidthBytes, bm.bmBits );
353
354     TRACE("%dx%d, %d colors returning %p\n", bm.bmWidth, bm.bmHeight,
355           1 << (bm.bmPlanes * bm.bmBitsPixel), hbitmap);
356
357     return hbitmap;
358 }
359
360
361 /***********************************************************************
362  * GetBitmapBits [GDI32.@]
363  *
364  * Copies bitmap bits of bitmap to buffer.
365  *
366  * RETURNS
367  *    Success: Number of bytes copied
368  *    Failure: 0
369  */
370 LONG WINAPI GetBitmapBits(
371     HBITMAP hbitmap, /* [in]  Handle to bitmap */
372     LONG count,        /* [in]  Number of bytes to copy */
373     LPVOID bits)       /* [out] Pointer to buffer to receive bits */
374 {
375     char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
376     BITMAPINFO *info = (BITMAPINFO *)buffer;
377     struct gdi_image_bits src_bits;
378     struct bitblt_coords src;
379     int dst_stride, max, ret;
380     BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
381
382     if (!bmp) return 0;
383
384     if (bmp->dib) dst_stride = get_bitmap_stride( bmp->dib->dsBmih.biWidth, bmp->dib->dsBmih.biBitCount );
385     else dst_stride = get_bitmap_stride( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
386
387     ret = max = dst_stride * bmp->bitmap.bmHeight;
388     if (!bits) goto done;
389     if (count > max) count = max;
390     ret = count;
391
392     src.visrect.left = 0;
393     src.visrect.right = bmp->bitmap.bmWidth;
394     src.visrect.top = 0;
395     src.visrect.bottom = (count + dst_stride - 1) / dst_stride;
396     src.x = src.y = 0;
397     src.width = src.visrect.right - src.visrect.left;
398     src.height = src.visrect.bottom - src.visrect.top;
399
400     if (!bmp->funcs->pGetImage( NULL, hbitmap, info, &src_bits, &src ))
401     {
402         const char *src_ptr = src_bits.ptr;
403         int src_stride = get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
404
405         /* GetBitmapBits returns 16-bit aligned data */
406
407         if (info->bmiHeader.biHeight > 0)
408         {
409             src_ptr += (info->bmiHeader.biHeight - 1) * src_stride;
410             src_stride = -src_stride;
411         }
412         src_ptr += src.visrect.top * src_stride;
413
414         if (src_stride == dst_stride) memcpy( bits, src_ptr, count );
415         else while (count > 0)
416         {
417             memcpy( bits, src_ptr, min( count, dst_stride ) );
418             src_ptr += src_stride;
419             bits = (char *)bits + dst_stride;
420             count -= dst_stride;
421         }
422         if (src_bits.free) src_bits.free( &src_bits );
423     }
424     else ret = 0;
425
426 done:
427     GDI_ReleaseObj( hbitmap );
428     return ret;
429 }
430
431
432 /******************************************************************************
433  * SetBitmapBits [GDI32.@]
434  *
435  * Sets bits of color data for a bitmap.
436  *
437  * RETURNS
438  *    Success: Number of bytes used in setting the bitmap bits
439  *    Failure: 0
440  */
441 LONG WINAPI SetBitmapBits(
442     HBITMAP hbitmap, /* [in] Handle to bitmap */
443     LONG count,        /* [in] Number of bytes in bitmap array */
444     LPCVOID bits)      /* [in] Address of array with bitmap bits */
445 {
446     BITMAPOBJ *bmp;
447     LONG height, ret;
448
449     if (!bits) return 0;
450
451     bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
452     if (!bmp) return 0;
453
454     if (count < 0) {
455         WARN("(%d): Negative number of bytes passed???\n", count );
456         count = -count;
457     }
458
459     if (bmp->dib)  /* simply copy the bits into the DIB */
460     {
461         DIBSECTION *dib = bmp->dib;
462         char *dest = dib->dsBm.bmBits;
463         LONG max = dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
464         if (count > max) count = max;
465         ret = count;
466
467         if (bmp->dib->dsBmih.biHeight >= 0)  /* not top-down, need to flip contents vertically */
468         {
469             dest += dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
470             while (count > 0)
471             {
472                 dest -= dib->dsBm.bmWidthBytes;
473                 memcpy( dest, bits, min( count, dib->dsBm.bmWidthBytes ) );
474                 bits = (const char *)bits + dib->dsBm.bmWidthBytes;
475                 count -= dib->dsBm.bmWidthBytes;
476             }
477         }
478         else memcpy( dest, bits, count );
479
480         GDI_ReleaseObj( hbitmap );
481         return ret;
482     }
483
484     /* Only get entire lines */
485     height = count / bmp->bitmap.bmWidthBytes;
486     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
487     count = height * bmp->bitmap.bmWidthBytes;
488
489     TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
490           hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
491           1 << bmp->bitmap.bmBitsPixel, height );
492
493     ret = bmp->funcs->pSetBitmapBits( hbitmap, bits, count );
494     GDI_ReleaseObj( hbitmap );
495     return ret;
496 }
497
498 /**********************************************************************
499  *              BITMAP_CopyBitmap
500  *
501  */
502 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
503 {
504     HBITMAP res;
505     DIBSECTION dib;
506     BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
507
508     if (!bmp) return 0;
509     if (bmp->dib)
510     {
511         void *bits;
512         BITMAPINFO *bi;
513         HDC dc;
514
515         dib = *bmp->dib;
516         GDI_ReleaseObj( hbitmap );
517         dc = CreateCompatibleDC( NULL );
518
519         if (!dc) return 0;
520         if (!(bi = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
521         {
522             DeleteDC( dc );
523             return 0;
524         }
525         bi->bmiHeader = dib.dsBmih;
526
527         /* Get the color table or the color masks */
528         GetDIBits( dc, hbitmap, 0, 0, NULL, bi, DIB_RGB_COLORS );
529         bi->bmiHeader.biHeight = dib.dsBmih.biHeight;
530
531         res = CreateDIBSection( dc, bi, DIB_RGB_COLORS, &bits, NULL, 0 );
532         if (res) SetDIBits( dc, res, 0, dib.dsBm.bmHeight, dib.dsBm.bmBits, bi, DIB_RGB_COLORS );
533         HeapFree( GetProcessHeap(), 0, bi );
534         DeleteDC( dc );
535         return res;
536     }
537     dib.dsBm = bmp->bitmap;
538     dib.dsBm.bmBits = NULL;
539     GDI_ReleaseObj( hbitmap );
540
541     res = CreateBitmapIndirect( &dib.dsBm );
542     if(res) {
543         char *buf = HeapAlloc( GetProcessHeap(), 0, dib.dsBm.bmWidthBytes * dib.dsBm.bmHeight );
544         GetBitmapBits (hbitmap, dib.dsBm.bmWidthBytes * dib.dsBm.bmHeight, buf);
545         SetBitmapBits (res, dib.dsBm.bmWidthBytes * dib.dsBm.bmHeight, buf);
546         HeapFree( GetProcessHeap(), 0, buf );
547     }
548     return res;
549 }
550
551
552 /***********************************************************************
553  *           BITMAP_SetOwnerDC
554  *
555  * Set the type of DC that owns the bitmap. This is used when the
556  * bitmap is selected into a device to initialize the bitmap function
557  * table.
558  */
559 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, PHYSDEV physdev )
560 {
561     BITMAPOBJ *bitmap;
562     BOOL ret = TRUE;
563
564     /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
565     if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
566
567     if (!(bitmap = GDI_GetObjPtr( hbitmap, OBJ_BITMAP ))) return FALSE;
568
569     if (!bitmap->dib && bitmap->funcs != physdev->funcs)
570     {
571         /* we can only change from the null driver to some other driver */
572         if (bitmap->funcs == &null_driver)
573         {
574             if (physdev->funcs->pCreateBitmap)
575             {
576                 ret = physdev->funcs->pCreateBitmap( physdev, hbitmap, bitmap->bitmap.bmBits );
577                 if (ret) bitmap->funcs = physdev->funcs;
578             }
579             else
580             {
581                 WARN( "Trying to select bitmap %p in DC that doesn't support it\n", hbitmap );
582                 ret = FALSE;
583             }
584         }
585         else
586         {
587             FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
588             ret = FALSE;
589         }
590     }
591     GDI_ReleaseObj( hbitmap );
592     return ret;
593 }
594
595
596 /***********************************************************************
597  *           BITMAP_SelectObject
598  */
599 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, HDC hdc )
600 {
601     HGDIOBJ ret;
602     BITMAPOBJ *bitmap;
603     DC *dc;
604     PHYSDEV physdev = NULL, old_physdev = NULL;
605
606     if (!(dc = get_dc_ptr( hdc ))) return 0;
607
608     if (GetObjectType( hdc ) != OBJ_MEMDC)
609     {
610         ret = 0;
611         goto done;
612     }
613     ret = dc->hBitmap;
614     if (handle == dc->hBitmap) goto done;  /* nothing to do */
615
616     if (!(bitmap = GDI_GetObjPtr( handle, OBJ_BITMAP )))
617     {
618         ret = 0;
619         goto done;
620     }
621
622     if (bitmap->header.selcount && (handle != GetStockObject(DEFAULT_BITMAP)))
623     {
624         WARN( "Bitmap already selected in another DC\n" );
625         GDI_ReleaseObj( handle );
626         ret = 0;
627         goto done;
628     }
629
630     old_physdev = GET_DC_PHYSDEV( dc, pSelectBitmap );
631     if(old_physdev == &dc->dibdrv.dev)
632         pop_dc_driver( dc, old_physdev );
633
634     if(bitmap->dib)
635     {
636         physdev = &dc->dibdrv.dev;
637         push_dc_driver( dc, physdev, physdev->funcs );
638     }
639     else
640         physdev = GET_DC_PHYSDEV( dc, pSelectBitmap );
641
642     if (!BITMAP_SetOwnerDC( handle, physdev ))
643     {
644         GDI_ReleaseObj( handle );
645         ret = 0;
646         goto done;
647     }
648     if (!physdev->funcs->pSelectBitmap( physdev, handle ))
649     {
650         GDI_ReleaseObj( handle );
651         ret = 0;
652     }
653     else
654     {
655         dc->hBitmap = handle;
656         GDI_inc_ref_count( handle );
657         dc->dirty = 0;
658         dc->vis_rect.left   = 0;
659         dc->vis_rect.top    = 0;
660         dc->vis_rect.right  = bitmap->bitmap.bmWidth;
661         dc->vis_rect.bottom = bitmap->bitmap.bmHeight;
662         SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
663         GDI_ReleaseObj( handle );
664         DC_InitDC( dc );
665         GDI_dec_ref_count( ret );
666     }
667
668  done:
669     if(!ret)
670     {
671         if(physdev == &dc->dibdrv.dev) pop_dc_driver( dc, physdev );
672         if(old_physdev == &dc->dibdrv.dev) push_dc_driver( dc, old_physdev, old_physdev->funcs );
673     }
674     release_dc_ptr( dc );
675     return ret;
676 }
677
678
679 /***********************************************************************
680  *           BITMAP_DeleteObject
681  */
682 static BOOL BITMAP_DeleteObject( HGDIOBJ handle )
683 {
684     const DC_FUNCTIONS *funcs;
685     BITMAPOBJ *bmp = GDI_GetObjPtr( handle, OBJ_BITMAP );
686
687     if (!bmp) return FALSE;
688     funcs = bmp->funcs;
689     GDI_ReleaseObj( handle );
690
691     funcs->pDeleteBitmap( handle );
692
693     if (!(bmp = free_gdi_handle( handle ))) return FALSE;
694
695     HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
696
697     if (bmp->dib)
698     {
699         DIBSECTION *dib = bmp->dib;
700
701         if (dib->dsBm.bmBits)
702         {
703             if (dib->dshSection)
704             {
705                 SYSTEM_INFO SystemInfo;
706                 GetSystemInfo( &SystemInfo );
707                 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
708                                  (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
709             }
710             else if (!dib->dsOffset)
711                 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
712         }
713         HeapFree(GetProcessHeap(), 0, dib);
714         HeapFree(GetProcessHeap(), 0, bmp->color_table);
715     }
716     return HeapFree( GetProcessHeap(), 0, bmp );
717 }
718
719
720 /***********************************************************************
721  *           BITMAP_GetObject
722  */
723 static INT BITMAP_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
724 {
725     INT ret;
726     BITMAPOBJ *bmp = GDI_GetObjPtr( handle, OBJ_BITMAP );
727
728     if (!bmp) return 0;
729
730     if (!buffer) ret = sizeof(BITMAP);
731     else if (count < sizeof(BITMAP)) ret = 0;
732     else if (bmp->dib)
733     {
734         if (count >= sizeof(DIBSECTION))
735         {
736             DIBSECTION *dib = buffer;
737             *dib = *bmp->dib;
738             dib->dsBmih.biHeight = abs( dib->dsBmih.biHeight );
739             ret = sizeof(DIBSECTION);
740         }
741         else /* if (count >= sizeof(BITMAP)) */
742         {
743             DIBSECTION *dib = bmp->dib;
744             memcpy( buffer, &dib->dsBm, sizeof(BITMAP) );
745             ret = sizeof(BITMAP);
746         }
747     }
748     else
749     {
750         memcpy( buffer, &bmp->bitmap, sizeof(BITMAP) );
751         ((BITMAP *) buffer)->bmBits = NULL;
752         ret = sizeof(BITMAP);
753     }
754     GDI_ReleaseObj( handle );
755     return ret;
756 }
757
758
759 /******************************************************************************
760  * CreateDiscardableBitmap [GDI32.@]
761  *
762  * Creates a discardable bitmap.
763  *
764  * RETURNS
765  *    Success: Handle to bitmap
766  *    Failure: NULL
767  */
768 HBITMAP WINAPI CreateDiscardableBitmap(
769     HDC hdc,    /* [in] Handle to device context */
770     INT width,  /* [in] Bitmap width */
771     INT height) /* [in] Bitmap height */
772 {
773     return CreateCompatibleBitmap( hdc, width, height );
774 }
775
776
777 /******************************************************************************
778  * GetBitmapDimensionEx [GDI32.@]
779  *
780  * Retrieves dimensions of a bitmap.
781  *
782  * RETURNS
783  *    Success: TRUE
784  *    Failure: FALSE
785  */
786 BOOL WINAPI GetBitmapDimensionEx(
787     HBITMAP hbitmap, /* [in]  Handle to bitmap */
788     LPSIZE size)     /* [out] Address of struct receiving dimensions */
789 {
790     BITMAPOBJ * bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
791     if (!bmp) return FALSE;
792     *size = bmp->size;
793     GDI_ReleaseObj( hbitmap );
794     return TRUE;
795 }
796
797
798 /******************************************************************************
799  * SetBitmapDimensionEx [GDI32.@]
800  *
801  * Assigns dimensions to a bitmap.
802  * MSDN says that this function will fail if hbitmap is a handle created by
803  * CreateDIBSection, but that's not true on Windows 2000.
804  *
805  * RETURNS
806  *    Success: TRUE
807  *    Failure: FALSE
808  */
809 BOOL WINAPI SetBitmapDimensionEx(
810     HBITMAP hbitmap, /* [in]  Handle to bitmap */
811     INT x,           /* [in]  Bitmap width */
812     INT y,           /* [in]  Bitmap height */
813     LPSIZE prevSize) /* [out] Address of structure for orig dims */
814 {
815     BITMAPOBJ * bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
816     if (!bmp) return FALSE;
817     if (prevSize) *prevSize = bmp->size;
818     bmp->size.cx = x;
819     bmp->size.cy = y;
820     GDI_ReleaseObj( hbitmap );
821     return TRUE;
822 }