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