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