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