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