gdi32: Disable the freetype mmap sharing on Mac OS.
[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 || bmp->bmPlanes != 1)
240     {
241         if (bmp && bmp->bmPlanes != 1)
242             FIXME("planes = %d\n", bmp->bmPlanes);
243         SetLastError( ERROR_INVALID_PARAMETER );
244         return NULL;
245     }
246
247     bm = *bmp;
248
249     if (!bm.bmWidth || !bm.bmHeight)
250     {
251         bm.bmWidth = bm.bmHeight = 1;
252         bm.bmPlanes = bm.bmBitsPixel = 1;
253         bm.bmWidthBytes = 2;
254         bm.bmBits = NULL;
255     }
256     else
257     {
258         if (bm.bmHeight < 0)
259             bm.bmHeight = -bm.bmHeight;
260         if (bm.bmWidth < 0)
261             bm.bmWidth = -bm.bmWidth;
262     }
263
264       /* Create the BITMAPOBJ */
265     bmpobj = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC,
266                               (HGDIOBJ *)&hbitmap, &bitmap_funcs );
267
268     if (!bmpobj)
269     {
270         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
271         return NULL;
272     }
273
274     TRACE("%dx%d, %d colors returning %p\n", bm.bmWidth, bm.bmHeight,
275           1 << (bm.bmPlanes * bm.bmBitsPixel), hbitmap);
276
277     bmpobj->size.cx = 0;
278     bmpobj->size.cy = 0;
279     bmpobj->bitmap = bm;
280     bmpobj->bitmap.bmBits = NULL;
281     bmpobj->funcs = NULL;
282     bmpobj->dib = NULL;
283     bmpobj->segptr_bits = 0;
284     bmpobj->color_table = NULL;
285     bmpobj->nb_colors = 0;
286
287     if (bm.bmBits)
288         SetBitmapBits( hbitmap, bm.bmHeight * bm.bmWidthBytes, bm.bmBits );
289
290     GDI_ReleaseObj( hbitmap );
291     return hbitmap;
292 }
293
294
295 /***********************************************************************
296  * GetBitmapBits [GDI32.@]
297  *
298  * Copies bitmap bits of bitmap to buffer.
299  *
300  * RETURNS
301  *    Success: Number of bytes copied
302  *    Failure: 0
303  */
304 LONG WINAPI GetBitmapBits(
305     HBITMAP hbitmap, /* [in]  Handle to bitmap */
306     LONG count,        /* [in]  Number of bytes to copy */
307     LPVOID bits)       /* [out] Pointer to buffer to receive bits */
308 {
309     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
310     LONG height, ret;
311
312     if (!bmp) return 0;
313
314     if (bmp->dib)  /* simply copy the bits from the DIB */
315     {
316         DIBSECTION *dib = bmp->dib;
317         const char *src = dib->dsBm.bmBits;
318         INT width_bytes = BITMAP_GetWidthBytes(dib->dsBm.bmWidth, dib->dsBm.bmBitsPixel);
319         DWORD max = width_bytes * bmp->bitmap.bmHeight;
320
321         if (!bits)
322         {
323             ret = max;
324             goto done;
325         }
326
327         if (count > max) count = max;
328         ret = count;
329
330         /* GetBitmapBits returns not 32-bit aligned data */
331
332         if (bmp->dib->dsBmih.biHeight >= 0)  /* not top-down, need to flip contents vertically */
333         {
334             src += dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
335             while (count > 0)
336             {
337                 src -= dib->dsBm.bmWidthBytes;
338                 memcpy( bits, src, min( count, width_bytes ) );
339                 bits = (char *)bits + width_bytes;
340                 count -= width_bytes;
341             }
342         }
343         else
344         {
345             while (count > 0)
346             {
347                 memcpy( bits, src, min( count, width_bytes ) );
348                 src += dib->dsBm.bmWidthBytes;
349                 bits = (char *)bits + width_bytes;
350                 count -= width_bytes;
351             }
352         }
353         goto done;
354     }
355
356     /* If the bits vector is null, the function should return the read size */
357     if(bits == NULL)
358     {
359         ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
360         goto done;
361     }
362
363     if (count < 0) {
364         WARN("(%d): Negative number of bytes passed???\n", count );
365         count = -count;
366     }
367
368     /* Only get entire lines */
369     height = count / bmp->bitmap.bmWidthBytes;
370     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
371     count = height * bmp->bitmap.bmWidthBytes;
372     if (count == 0)
373       {
374         WARN("Less than one entire line requested\n");
375         ret = 0;
376         goto done;
377       }
378
379
380     TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
381           hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
382           1 << bmp->bitmap.bmBitsPixel, height );
383
384     if(bmp->funcs && bmp->funcs->pGetBitmapBits)
385     {
386         TRACE("Calling device specific BitmapBits\n");
387         ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
388     } else {
389
390         if(!bmp->bitmap.bmBits) {
391             TRACE("Bitmap is empty\n");
392             memset(bits, 0, count);
393             ret = count;
394         } else {
395             memcpy(bits, bmp->bitmap.bmBits, count);
396             ret = count;
397         }
398
399     }
400  done:
401     GDI_ReleaseObj( hbitmap );
402     return ret;
403 }
404
405
406 /******************************************************************************
407  * SetBitmapBits [GDI32.@]
408  *
409  * Sets bits of color data for a bitmap.
410  *
411  * RETURNS
412  *    Success: Number of bytes used in setting the bitmap bits
413  *    Failure: 0
414  */
415 LONG WINAPI SetBitmapBits(
416     HBITMAP hbitmap, /* [in] Handle to bitmap */
417     LONG count,        /* [in] Number of bytes in bitmap array */
418     LPCVOID bits)      /* [in] Address of array with bitmap bits */
419 {
420     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
421     LONG height, ret;
422
423     if ((!bmp) || (!bits))
424         return 0;
425
426     if (count < 0) {
427         WARN("(%d): Negative number of bytes passed???\n", count );
428         count = -count;
429     }
430
431     if (bmp->dib)  /* simply copy the bits into the DIB */
432     {
433         DIBSECTION *dib = bmp->dib;
434         char *dest = dib->dsBm.bmBits;
435         DWORD max = dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
436         if (count > max) count = max;
437         ret = count;
438
439         if (bmp->dib->dsBmih.biHeight >= 0)  /* not top-down, need to flip contents vertically */
440         {
441             dest += dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
442             while (count > 0)
443             {
444                 dest -= dib->dsBm.bmWidthBytes;
445                 memcpy( dest, bits, min( count, dib->dsBm.bmWidthBytes ) );
446                 bits = (const char *)bits + dib->dsBm.bmWidthBytes;
447                 count -= dib->dsBm.bmWidthBytes;
448             }
449         }
450         else memcpy( dest, bits, count );
451
452         GDI_ReleaseObj( hbitmap );
453         return ret;
454     }
455
456     /* Only get entire lines */
457     height = count / bmp->bitmap.bmWidthBytes;
458     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
459     count = height * bmp->bitmap.bmWidthBytes;
460
461     TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
462           hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
463           1 << bmp->bitmap.bmBitsPixel, height );
464
465     if(bmp->funcs && bmp->funcs->pSetBitmapBits) {
466
467         TRACE("Calling device specific BitmapBits\n");
468         ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
469     } else {
470
471         if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
472             bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
473         if(!bmp->bitmap.bmBits) {
474             WARN("Unable to allocate bit buffer\n");
475             ret = 0;
476         } else {
477             memcpy(bmp->bitmap.bmBits, bits, count);
478             ret = count;
479         }
480     }
481
482     GDI_ReleaseObj( hbitmap );
483     return ret;
484 }
485
486 /**********************************************************************
487  *              BITMAP_CopyBitmap
488  *
489  */
490 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
491 {
492     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
493     HBITMAP res = 0;
494     BITMAP bm;
495
496     if(!bmp) return 0;
497
498     bm = bmp->bitmap;
499     bm.bmBits = NULL;
500     res = CreateBitmapIndirect(&bm);
501
502     if(res) {
503         char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
504                                bm.bmHeight );
505         GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
506         SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
507         HeapFree( GetProcessHeap(), 0, buf );
508     }
509
510     GDI_ReleaseObj( hbitmap );
511     return res;
512 }
513
514
515 /***********************************************************************
516  *           BITMAP_SetOwnerDC
517  *
518  * Set the type of DC that owns the bitmap. This is used when the
519  * bitmap is selected into a device to initialize the bitmap function
520  * table.
521  */
522 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
523 {
524     BITMAPOBJ *bitmap;
525     BOOL ret;
526
527     /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
528     if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
529
530     if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
531
532     ret = TRUE;
533     if (!bitmap->funcs)  /* not owned by a DC yet */
534     {
535         if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap,
536                                                                       bitmap->bitmap.bmBits );
537         if (ret) bitmap->funcs = dc->funcs;
538     }
539     else if (bitmap->funcs != dc->funcs)
540     {
541         FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
542         ret = FALSE;
543     }
544     GDI_ReleaseObj( hbitmap );
545     return ret;
546 }
547
548
549 /***********************************************************************
550  *           BITMAP_SelectObject
551  */
552 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
553 {
554     HGDIOBJ ret;
555     BITMAPOBJ *bitmap = obj;
556     DC *dc = DC_GetDCPtr( hdc );
557
558     if (!dc) return 0;
559     if (GetObjectType( hdc ) != OBJ_MEMDC)
560     {
561         GDI_ReleaseObj( hdc );
562         return 0;
563     }
564     ret = dc->hBitmap;
565     if (handle == dc->hBitmap) goto done;  /* nothing to do */
566
567     if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
568     {
569         WARN( "Bitmap already selected in another DC\n" );
570         GDI_ReleaseObj( hdc );
571         return 0;
572     }
573
574     if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
575     {
576         GDI_ReleaseObj( hdc );
577         return 0;
578     }
579
580     if (dc->funcs->pSelectBitmap) handle = dc->funcs->pSelectBitmap( dc->physDev, handle );
581
582     if (handle)
583     {
584         dc->hBitmap = handle;
585         dc->flags &= ~DC_DIRTY;
586         SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
587         DC_InitDC( dc );
588     }
589     else ret = 0;
590
591  done:
592     GDI_ReleaseObj( hdc );
593     return ret;
594 }
595
596
597 /***********************************************************************
598  *           BITMAP_DeleteObject
599  */
600 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
601 {
602     BITMAPOBJ * bmp = obj;
603
604     if (bmp->funcs && bmp->funcs->pDeleteBitmap)
605         bmp->funcs->pDeleteBitmap( handle );
606
607     HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
608
609     if (bmp->dib)
610     {
611         DIBSECTION *dib = bmp->dib;
612
613         if (dib->dsBm.bmBits)
614         {
615             if (dib->dshSection)
616             {
617                 SYSTEM_INFO SystemInfo;
618                 GetSystemInfo( &SystemInfo );
619                 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
620                                  (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
621             }
622             else if (!dib->dsOffset)
623                 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
624         }
625         HeapFree(GetProcessHeap(), 0, dib);
626         bmp->dib = NULL;
627         if (bmp->segptr_bits)
628         { /* free its selector array */
629             WORD sel = SELECTOROF(bmp->segptr_bits);
630             WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
631             int i;
632
633             for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
634         }
635         HeapFree(GetProcessHeap(), 0, bmp->color_table);
636     }
637     return GDI_FreeObject( handle, obj );
638 }
639
640
641 /***********************************************************************
642  *           BITMAP_GetObject16
643  */
644 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
645 {
646     BITMAPOBJ *bmp = obj;
647
648     if (bmp->dib)
649     {
650         if ( count <= sizeof(BITMAP16) )
651         {
652             BITMAP *bmp32 = &bmp->dib->dsBm;
653             BITMAP16 bmp16;
654             bmp16.bmType       = bmp32->bmType;
655             bmp16.bmWidth      = bmp32->bmWidth;
656             bmp16.bmHeight     = bmp32->bmHeight;
657             bmp16.bmWidthBytes = bmp32->bmWidthBytes;
658             bmp16.bmPlanes     = bmp32->bmPlanes;
659             bmp16.bmBitsPixel  = bmp32->bmBitsPixel;
660             bmp16.bmBits       = (SEGPTR)0;
661             memcpy( buffer, &bmp16, count );
662             return count;
663         }
664         else
665         {
666             FIXME("not implemented for DIBs: count %d\n", count);
667             return 0;
668         }
669     }
670     else
671     {
672         BITMAP16 bmp16;
673         bmp16.bmType       = bmp->bitmap.bmType;
674         bmp16.bmWidth      = bmp->bitmap.bmWidth;
675         bmp16.bmHeight     = bmp->bitmap.bmHeight;
676         bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
677         bmp16.bmPlanes     = bmp->bitmap.bmPlanes;
678         bmp16.bmBitsPixel  = bmp->bitmap.bmBitsPixel;
679         bmp16.bmBits       = (SEGPTR)0;
680         if (count < sizeof(bmp16)) return 0;
681         memcpy( buffer, &bmp16, sizeof(bmp16) );
682         return sizeof(bmp16);
683     }
684 }
685
686
687 /***********************************************************************
688  *           BITMAP_GetObject
689  */
690 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
691 {
692     BITMAPOBJ *bmp = obj;
693
694     if( !buffer ) return sizeof(BITMAP);
695     if (count < sizeof(BITMAP)) return 0;
696
697     if (bmp->dib)
698     {
699         if (count >= sizeof(DIBSECTION))
700         {
701             memcpy( buffer, bmp->dib, sizeof(DIBSECTION) );
702             return sizeof(DIBSECTION);
703         }
704         else /* if (count >= sizeof(BITMAP)) */
705         {
706             DIBSECTION *dib = bmp->dib;
707             memcpy( buffer, &dib->dsBm, sizeof(BITMAP) );
708             return sizeof(BITMAP);
709         }
710     }
711     else
712     {
713         memcpy( buffer, &bmp->bitmap, sizeof(BITMAP) );
714         ((BITMAP *) buffer)->bmBits = NULL;
715         return sizeof(BITMAP);
716     }
717 }
718
719
720 /******************************************************************************
721  * CreateDiscardableBitmap [GDI32.@]
722  *
723  * Creates a discardable bitmap.
724  *
725  * RETURNS
726  *    Success: Handle to bitmap
727  *    Failure: NULL
728  */
729 HBITMAP WINAPI CreateDiscardableBitmap(
730     HDC hdc,    /* [in] Handle to device context */
731     INT width,  /* [in] Bitmap width */
732     INT height) /* [in] Bitmap height */
733 {
734     return CreateCompatibleBitmap( hdc, width, height );
735 }
736
737
738 /******************************************************************************
739  * GetBitmapDimensionEx [GDI32.@]
740  *
741  * Retrieves dimensions of a bitmap.
742  *
743  * RETURNS
744  *    Success: TRUE
745  *    Failure: FALSE
746  */
747 BOOL WINAPI GetBitmapDimensionEx(
748     HBITMAP hbitmap, /* [in]  Handle to bitmap */
749     LPSIZE size)     /* [out] Address of struct receiving dimensions */
750 {
751     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
752     if (!bmp) return FALSE;
753     *size = bmp->size;
754     GDI_ReleaseObj( hbitmap );
755     return TRUE;
756 }
757
758
759 /******************************************************************************
760  * SetBitmapDimensionEx [GDI32.@]
761  *
762  * Assigns dimensions to a bitmap.
763  * MSDN says that this function will fail if hbitmap is a handle created by
764  * CreateDIBSection, but that's not true on Windows 2000.
765  *
766  * RETURNS
767  *    Success: TRUE
768  *    Failure: FALSE
769  */
770 BOOL WINAPI SetBitmapDimensionEx(
771     HBITMAP hbitmap, /* [in]  Handle to bitmap */
772     INT x,           /* [in]  Bitmap width */
773     INT y,           /* [in]  Bitmap height */
774     LPSIZE prevSize) /* [out] Address of structure for orig dims */
775 {
776     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
777     if (!bmp) return FALSE;
778     if (prevSize) *prevSize = bmp->size;
779     bmp->size.cx = x;
780     bmp->size.cy = y;
781     GDI_ReleaseObj( hbitmap );
782     return TRUE;
783 }