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