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