Adapted to separation between KERNEL and USER.
[wine] / objects / bitmap.c
1 /*
2  * GDI bitmap objects
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1998 Huw D M Davies
6  */
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include "gdi.h"
11 #include "dc.h"
12 #include "bitmap.h"
13 #include "heap.h"
14 #include "global.h"
15 #include "sysmetrics.h"
16 #include "cursoricon.h"
17 #include "color.h"
18 #include "debug.h"
19
20
21 /***********************************************************************
22  *           BITMAP_GetPadding
23  *
24  * Return number of bytes to pad a scanline of 16-bit aligned Windows DDB data.
25  */
26 INT32 BITMAP_GetPadding( int bmWidth, int bpp )
27 {
28     INT32 pad;
29
30     switch (bpp) 
31     {
32     case 1:
33         pad = ((bmWidth-1) & 8) ? 0 : 1;
34         break;
35
36     case 8:
37         pad = (2 - (bmWidth & 1)) & 1;
38         break;
39
40     case 24:
41         pad = (bmWidth*3) & 1;
42         break;
43
44     case 32:
45     case 16:
46     case 15:
47         pad = 0; /* we have 16bit alignment already */
48         break;
49
50     case 4:
51         if (!(bmWidth & 3)) pad = 0;
52         else pad = ((4 - (bmWidth & 3)) + 1) / 2;
53         break;
54
55     default:
56         WARN(bitmap,"Unknown depth %d, please report.\n", bpp );
57         return -1;
58     }
59     return pad;
60 }
61
62 /***********************************************************************
63  *           BITMAP_GetWidthBytes
64  *
65  * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
66  * data.
67  */
68 INT32 BITMAP_GetWidthBytes( INT32 bmWidth, INT32 bpp )
69 {
70     switch(bpp)
71     {
72     case 1:
73         return 2 * ((bmWidth+15) >> 4);
74
75     case 24:
76         bmWidth *= 3; /* fall through */
77     case 8:
78         return bmWidth + (bmWidth & 1);
79
80     case 32:
81         return bmWidth * 4;
82
83     case 16:
84     case 15:
85         return bmWidth * 2;
86
87     case 4:
88         return 2 * ((bmWidth+3) >> 2);
89
90     default:
91         WARN(bitmap,"Unknown depth %d, please report.\n", bpp );
92     }
93     return -1;
94 }
95
96 /***********************************************************************
97  *           CreateUserBitmap16    (GDI.407)
98  */
99 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
100                                      UINT16 bpp, LPCVOID bits )
101 {
102     return CreateBitmap16( width, height, planes, bpp, bits );
103 }
104
105 /***********************************************************************
106  *           CreateUserDiscardableBitmap16    (GDI.409)
107  */
108 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy, 
109                                                 INT16 width, INT16 height )
110 {
111     return CreateUserBitmap16( width, height, 1, screenDepth, NULL );
112 }
113
114
115 /***********************************************************************
116  *           CreateBitmap16    (GDI.48)
117  */
118 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
119                                  UINT16 bpp, LPCVOID bits )
120 {
121     return CreateBitmap32( width, height, planes, bpp, bits );
122 }
123
124
125 /******************************************************************************
126  * CreateBitmap32 [GDI32.25]  Creates a bitmap with the specified info
127  *
128  * PARAMS
129  *    width  [I] bitmap width
130  *    height [I] bitmap height
131  *    planes [I] Number of color planes
132  *    bpp    [I] Number of bits to identify a color
133  *    bits   [I] Pointer to array containing color data
134  *
135  * RETURNS
136  *    Success: Handle to bitmap
137  *    Failure: 0
138  */
139 HBITMAP32 WINAPI CreateBitmap32( INT32 width, INT32 height, UINT32 planes,
140                                  UINT32 bpp, LPCVOID bits )
141 {
142     BITMAPOBJ *bmp;
143     HBITMAP32 hbitmap;
144
145     planes = (BYTE)planes;
146     bpp    = (BYTE)bpp;
147
148
149       /* Check parameters */
150     if (!height || !width) return 0;
151     if (planes != 1) {
152         FIXME(bitmap, "planes = %d\n", planes);
153         return 0;
154     }
155     if (height < 0) height = -height;
156     if (width < 0) width = -width;
157
158       /* Create the BITMAPOBJ */
159     hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
160     if (!hbitmap) return 0;
161
162     TRACE(bitmap, "%dx%d, %d colors returning %08x\n", width, height,
163           1 << (planes*bpp), hbitmap);
164
165     bmp = (BITMAPOBJ *) GDI_HEAP_LOCK( hbitmap );
166
167     bmp->size.cx = 0;
168     bmp->size.cy = 0;
169     bmp->bitmap.bmType = 0;
170     bmp->bitmap.bmWidth = width;
171     bmp->bitmap.bmHeight = height;
172     bmp->bitmap.bmPlanes = planes;
173     bmp->bitmap.bmBitsPixel = bpp;
174     bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
175     bmp->bitmap.bmBits = NULL;
176
177     bmp->DDBitmap = NULL;
178     bmp->dib = NULL;
179
180     if (bits) /* Set bitmap bits */
181         SetBitmapBits32( hbitmap, height * bmp->bitmap.bmWidthBytes,
182                          bits );
183     GDI_HEAP_UNLOCK( hbitmap );
184     return hbitmap;
185 }
186
187
188 /***********************************************************************
189  *           CreateCompatibleBitmap16    (GDI.51)
190  */
191 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
192 {
193     return CreateCompatibleBitmap32( hdc, width, height );
194 }
195
196
197 /******************************************************************************
198  * CreateCompatibleBitmap32 [GDI32.30]  Creates a bitmap compatible with the DC
199  *
200  * PARAMS
201  *    hdc    [I] Handle to device context
202  *    width  [I] Width of bitmap
203  *    height [I] Height of bitmap
204  *
205  * RETURNS
206  *    Success: Handle to bitmap
207  *    Failure: 0
208  */
209 HBITMAP32 WINAPI CreateCompatibleBitmap32( HDC32 hdc, INT32 width, INT32 height)
210 {
211     HBITMAP32 hbmpRet = 0;
212     DC *dc;
213
214     TRACE(bitmap, "(%04x,%d,%d) = \n", hdc, width, height );
215     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
216     if ((width >0x1000) || (height > 0x1000)) {
217         FIXME(bitmap,"got bad width %d or height %d, please look for reason\n",
218               width, height );
219         return 0;
220     }
221     hbmpRet = CreateBitmap32( width, height, 1, dc->w.bitsPerPixel, NULL );
222
223     if(dc->funcs->pCreateBitmap)
224         dc->funcs->pCreateBitmap( hbmpRet );
225
226     TRACE(bitmap,"\t\t%04x\n", hbmpRet);
227     return hbmpRet;
228 }
229
230
231 /***********************************************************************
232  *           CreateBitmapIndirect16    (GDI.49)
233  */
234 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
235 {
236     return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
237                            bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
238 }
239
240
241 /******************************************************************************
242  * CreateBitmapIndirect32 [GDI32.26]  Creates a bitmap with the specifies info
243  *
244  * RETURNS
245  *    Success: Handle to bitmap
246  *    Failure: NULL
247  */
248 HBITMAP32 WINAPI CreateBitmapIndirect32(
249     const BITMAP32 * bmp) /* [in] Pointer to the bitmap data */
250 {
251     return CreateBitmap32( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
252                            bmp->bmBitsPixel, bmp->bmBits );
253 }
254
255
256 /***********************************************************************
257  *           GetBitmapBits16    (GDI.74)
258  */
259 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
260 {
261     return GetBitmapBits32( hbitmap, count, buffer );
262 }
263
264
265 /***********************************************************************
266  * GetBitmapBits32 [GDI32.143]  Copies bitmap bits of bitmap to buffer
267  * 
268  * RETURNS
269  *    Success: Number of bytes copied
270  *    Failure: 0
271  */
272 LONG WINAPI GetBitmapBits32(
273     HBITMAP32 hbitmap, /* [in]  Handle to bitmap */
274     LONG count,        /* [in]  Number of bytes to copy */
275     LPVOID bits)       /* [out] Pointer to buffer to receive bits */
276 {
277     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
278     LONG height, ret;
279     
280     if (!bmp) return 0;
281
282     if (count < 0) {
283         WARN(bitmap, "(%ld): Negative number of bytes passed???\n", count );
284         count = -count;
285     }
286
287     /* Only get entire lines */
288     height = count / bmp->bitmap.bmWidthBytes;
289     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
290     count = height * bmp->bitmap.bmWidthBytes;
291
292     TRACE(bitmap, "(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
293             hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
294             1 << bmp->bitmap.bmBitsPixel, height );
295
296     if(bmp->DDBitmap) { 
297
298         TRACE(bitmap, "Calling device specific BitmapBits\n");
299         if(bmp->DDBitmap->funcs->pBitmapBits)
300             ret = bmp->DDBitmap->funcs->pBitmapBits(hbitmap, bits, count,
301                                                     DDB_GET);
302         else {
303             ERR(bitmap, "BitmapBits == NULL??\n");
304             ret = 0;
305         }       
306
307     } else {
308
309         if(!bmp->bitmap.bmBits) {
310             WARN(bitmap, "Bitmap is empty\n");
311             ret = 0;
312         } else {
313             memcpy(bits, bmp->bitmap.bmBits, count);
314             ret = count;
315         }
316
317     }
318
319     GDI_HEAP_UNLOCK( hbitmap );
320     return ret;
321 }
322
323
324 /***********************************************************************
325  *           SetBitmapBits16    (GDI.106)
326  */
327 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
328 {
329     return SetBitmapBits32( hbitmap, count, buffer );
330 }
331
332
333 /******************************************************************************
334  * SetBitmapBits32 [GDI32.303]  Sets bits of color data for a bitmap
335  *
336  * RETURNS
337  *    Success: Number of bytes used in setting the bitmap bits
338  *    Failure: 0
339  */
340 LONG WINAPI SetBitmapBits32(
341     HBITMAP32 hbitmap, /* [in] Handle to bitmap */
342     LONG count,        /* [in] Number of bytes in bitmap array */
343     LPCVOID bits)      /* [in] Address of array with bitmap bits */
344 {
345     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
346     LONG height, ret;
347     
348     if (!bmp) return 0;
349
350     if (count < 0) {
351         WARN(bitmap, "(%ld): Negative number of bytes passed???\n", count );
352         count = -count;
353     }
354
355     /* Only get entire lines */
356     height = count / bmp->bitmap.bmWidthBytes;
357     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
358     count = height * bmp->bitmap.bmWidthBytes;
359
360     TRACE(bitmap, "(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
361             hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
362             1 << bmp->bitmap.bmBitsPixel, height );
363
364     if(bmp->DDBitmap) {
365
366         TRACE(bitmap, "Calling device specific BitmapBits\n");
367         if(bmp->DDBitmap->funcs->pBitmapBits)
368             ret = bmp->DDBitmap->funcs->pBitmapBits(hbitmap, (void *) bits,
369                                                     count, DDB_SET);
370         else {
371             ERR(bitmap, "BitmapBits == NULL??\n");
372             ret = 0;
373         }
374         
375     } else {
376
377         if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
378             bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
379         if(!bmp->bitmap.bmBits) {
380             WARN(bitmap, "Unable to allocate bit buffer\n");
381             ret = 0;
382         } else {
383             memcpy(bmp->bitmap.bmBits, bits, count);
384             ret = count;
385         }
386     }
387
388     GDI_HEAP_UNLOCK( hbitmap );
389     return ret;
390 }
391
392 /***********************************************************************
393  * LoadImage16 [USER.389]
394  *
395  */
396 HANDLE16 WINAPI LoadImage16( HINSTANCE16 hinst, LPCSTR name, UINT16 type,
397                              INT16 desiredx, INT16 desiredy, UINT16 loadflags)
398 {
399         if (HIWORD(name)) {
400             TRACE(resource,"(0x%04x,%s,%d,%d,%d,0x%08x)\n",
401                 hinst,(char *)PTR_SEG_TO_LIN(name),type,desiredx,desiredy,loadflags);
402         } else {
403             TRACE(resource,"LoadImage16(0x%04x,%p,%d,%d,%d,0x%08x)\n",
404                 hinst,name,type,desiredx,desiredy,loadflags);
405         }
406         switch (type) {
407         case IMAGE_BITMAP:
408                 return LoadBitmap16(hinst,(SEGPTR)name);
409         case IMAGE_ICON:
410                 return LoadIcon16(hinst,(SEGPTR)name);
411         case IMAGE_CURSOR:
412                 return LoadCursor16(hinst,(SEGPTR)name);
413         }
414         return 0;
415         
416 }
417
418 /**********************************************************************
419  *          LoadImage32A    (USER32.365)
420  * 
421  * FIXME: implementation lacks some features, see LR_ defines in windows.h
422  */
423
424 HANDLE32 WINAPI LoadImage32A( HINSTANCE32 hinst, LPCSTR name, UINT32 type,
425                               INT32 desiredx, INT32 desiredy, UINT32 loadflags)
426 {
427     HANDLE32 res;
428     LPWSTR u_name;
429
430     if (HIWORD(name)) u_name = HEAP_strdupAtoW(GetProcessHeap(), 0, name);
431     else u_name=(LPWSTR)name;
432     res = LoadImage32W(hinst, u_name, type, desiredx, desiredy, loadflags);
433     if (HIWORD(name)) HeapFree(GetProcessHeap(), 0, u_name);
434     return res;
435 }
436
437
438 /******************************************************************************
439  * LoadImage32W [USER32.366]  Loads an icon, cursor, or bitmap
440  *
441  * PARAMS
442  *    hinst     [I] Handle of instance that contains image
443  *    name      [I] Name of image
444  *    type      [I] Type of image
445  *    desiredx  [I] Desired width
446  *    desiredy  [I] Desired height
447  *    loadflags [I] Load flags
448  *
449  * RETURNS
450  *    Success: Handle to newly loaded image
451  *    Failure: NULL
452  *
453  * FIXME: Implementation lacks some features, see LR_ defines in windows.h
454  */
455 HANDLE32 WINAPI LoadImage32W( HINSTANCE32 hinst, LPCWSTR name, UINT32 type,
456                 INT32 desiredx, INT32 desiredy, UINT32 loadflags )
457 {
458         if (HIWORD(name)) {
459                 TRACE(resource,"(0x%04x,%p,%d,%d,%d,0x%08x)\n",
460                         hinst,name,type,desiredx,desiredy,loadflags
461                 );
462         } else {
463                 TRACE(resource,"(0x%04x,%p,%d,%d,%d,0x%08x)\n",
464                         hinst,name,type,desiredx,desiredy,loadflags
465                 );
466         }
467     if (loadflags & LR_DEFAULTSIZE)
468       if (type == IMAGE_ICON) {
469         if (!desiredx) desiredx = SYSMETRICS_CXICON;
470         if (!desiredy) desiredy = SYSMETRICS_CYICON;
471       } else if (type == IMAGE_CURSOR) {
472         if (!desiredx) desiredx = SYSMETRICS_CXCURSOR;
473         if (!desiredy) desiredy = SYSMETRICS_CYCURSOR;
474       }
475     if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
476     switch (type) {
477         case IMAGE_BITMAP:
478             return BITMAP_LoadBitmap32W(hinst, name, loadflags);
479         case IMAGE_ICON:
480             return CURSORICON_Load32(hinst, name, desiredx, desiredy,
481               MIN(16, COLOR_GetSystemPaletteSize()), FALSE, loadflags);
482         case IMAGE_CURSOR:
483             return CURSORICON_Load32(hinst, name, desiredx, desiredy, 1, TRUE,
484               loadflags);
485     }
486     return 0;
487 }
488
489
490 /**********************************************************************
491  *              BITMAP_CopyBitmap
492  *
493  */
494 HBITMAP32 BITMAP_CopyBitmap(HBITMAP32 hbitmap)
495 {
496     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
497     HBITMAP32 res = 0;
498     BITMAP32 bm;
499
500     if(!bmp) return 0;
501
502     bm = bmp->bitmap;
503     bm.bmBits = NULL;
504     res = CreateBitmapIndirect32(&bm);
505
506     if(res) {
507         char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
508                                bm.bmHeight );
509         GetBitmapBits32 (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
510         SetBitmapBits32 (res, bm.bmWidthBytes * bm.bmHeight, buf);
511         HeapFree( GetProcessHeap(), 0, buf );
512     }
513
514     GDI_HEAP_UNLOCK( hbitmap );
515     return res;
516 }
517
518
519 /******************************************************************************
520  * CopyImage32 [USER32.61]  Creates new image and copies attributes to it
521  *
522  * PARAMS
523  *    hnd      [I] Handle to image to copy
524  *    type     [I] Type of image to copy
525  *    desiredx [I] Desired width of new image
526  *    desiredy [I] Desired height of new image
527  *    flags    [I] Copy flags
528  *
529  * RETURNS
530  *    Success: Handle to newly created image
531  *    Failure: NULL
532  *
533  * FIXME: implementation still lacks nearly all features, see LR_*
534  * defines in windows.h
535  */
536 HICON32 WINAPI CopyImage32( HANDLE32 hnd, UINT32 type, INT32 desiredx,
537                              INT32 desiredy, UINT32 flags )
538 {
539     switch (type)
540     {
541         case IMAGE_BITMAP:
542                 return BITMAP_CopyBitmap(hnd);
543         case IMAGE_ICON:
544                 return CopyIcon32(hnd);
545         case IMAGE_CURSOR:
546                 return CopyCursor32(hnd);
547     }
548     return 0;
549 }
550
551 /**********************************************************************
552  *          LoadBitmap16    (USER.175)
553  *
554  * NOTES
555  *    Can this call LoadBitmap32?
556  */
557 HBITMAP16 WINAPI LoadBitmap16( HINSTANCE16 instance, SEGPTR name )
558 {
559     HBITMAP32 hbitmap = 0;
560     HDC32 hdc;
561     HRSRC16 hRsrc;
562     HGLOBAL16 handle;
563     BITMAPINFO *info;
564
565     if (HIWORD(name))
566     {
567         char *str = (char *)PTR_SEG_TO_LIN( name );
568         TRACE(bitmap, "(%04x,'%s')\n", instance, str );
569         if (str[0] == '#') name = (SEGPTR)(DWORD)(WORD)atoi( str + 1 );
570     }
571     else
572         TRACE(bitmap, "(%04x,%04x)\n",
573                         instance, LOWORD(name) );
574
575     if (!instance)  /* OEM bitmap */
576     {
577         if (HIWORD((int)name)) return 0;
578         return OBM_LoadBitmap( LOWORD((int)name) );
579     }
580
581     if (!(hRsrc = FindResource16( instance, name, RT_BITMAP16 ))) return 0;
582     if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
583
584     info = (BITMAPINFO *)LockResource16( handle );
585     if ((hdc = GetDC32(0)) != 0)
586     {
587         char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
588         hbitmap = CreateDIBitmap32( hdc, &info->bmiHeader, CBM_INIT,
589                                     bits, info, DIB_RGB_COLORS );
590         ReleaseDC32( 0, hdc );
591     }
592     FreeResource16( handle );
593     return hbitmap;
594 }
595
596
597 /**********************************************************************
598  *       BITMAP_LoadBitmap32W
599  */
600 HBITMAP32 BITMAP_LoadBitmap32W(HINSTANCE32 instance,LPCWSTR name, 
601                                UINT32 loadflags)
602 {
603     HBITMAP32 hbitmap = 0;
604     HDC32 hdc;
605     HRSRC32 hRsrc;
606     HGLOBAL32 handle;
607     char *ptr = NULL;
608     BITMAPINFO *info, *fix_info=NULL;
609     HGLOBAL32 hFix;
610     int size;
611
612     if (!(loadflags & LR_LOADFROMFILE)) {
613       if (!instance)  /* OEM bitmap */
614       {
615           if (HIWORD((int)name)) return 0;
616           return OBM_LoadBitmap( LOWORD((int)name) );
617       }
618
619       if (!(hRsrc = FindResource32W( instance, name, RT_BITMAP32W ))) return 0;
620       if (!(handle = LoadResource32( instance, hRsrc ))) return 0;
621
622       if ((info = (BITMAPINFO *)LockResource32( handle )) == NULL) return 0;
623     }
624     else
625     {
626         if (!(ptr = (char *)VIRTUAL_MapFileW( name ))) return 0;
627         info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
628     }
629     size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
630     if ((hFix = GlobalAlloc32(0, size))) fix_info=GlobalLock32(hFix);
631     if (fix_info) {
632       BYTE pix;
633
634       memcpy(fix_info, info, size);
635       pix = *((LPBYTE)info+DIB_BitmapInfoSize(info, DIB_RGB_COLORS));
636       DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
637       if ((hdc = GetDC32(0)) != 0) {
638         if (loadflags & LR_CREATEDIBSECTION)
639           hbitmap = CreateDIBSection32(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
640         else {
641           char *bits = (char *)info + size;;
642           hbitmap = CreateDIBitmap32( hdc, &fix_info->bmiHeader, CBM_INIT,
643                                       bits, fix_info, DIB_RGB_COLORS );
644         }
645         ReleaseDC32( 0, hdc );
646       }
647       GlobalUnlock32(hFix);
648       GlobalFree32(hFix);
649     }
650     if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
651     return hbitmap;
652 }
653
654
655 /******************************************************************************
656  * LoadBitmap32W [USER32.358]  Loads bitmap from the executable file
657  *
658  * RETURNS
659  *    Success: Handle to specified bitmap
660  *    Failure: NULL
661  */
662 HBITMAP32 WINAPI LoadBitmap32W(
663     HINSTANCE32 instance, /* [in] Handle to application instance */
664     LPCWSTR name)         /* [in] Address of bitmap resource name */
665 {
666     return BITMAP_LoadBitmap32W(instance, name, 0);
667 }
668
669
670 /**********************************************************************
671  *          LoadBitmap32A   (USER32.357)
672  */
673 HBITMAP32 WINAPI LoadBitmap32A( HINSTANCE32 instance, LPCSTR name )
674 {
675     HBITMAP32 res;
676     if (!HIWORD(name)) res = LoadBitmap32W( instance, (LPWSTR)name );
677     else
678     {
679         LPWSTR uni = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
680         res = LoadBitmap32W( instance, uni );
681         HeapFree( GetProcessHeap(), 0, uni );
682     }
683     return res;
684 }
685
686
687 /***********************************************************************
688  *           BITMAP_DeleteObject
689  */
690 BOOL32 BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
691 {
692     if( bmp->DDBitmap ) {
693         if( bmp->DDBitmap->funcs->pDeleteObject )
694             bmp->DDBitmap->funcs->pDeleteObject( hbitmap );
695     }
696
697     if( bmp->bitmap.bmBits )
698         HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
699
700     DIB_DeleteDIBSection( bmp );
701
702     return GDI_FreeObject( hbitmap );
703 }
704
705         
706 /***********************************************************************
707  *           BITMAP_GetObject16
708  */
709 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
710 {
711     if (bmp->dib)
712     {
713         if ( count <= sizeof(BITMAP16) )
714         {
715             BITMAP32 *bmp32 = &bmp->dib->dibSection.dsBm;
716             BITMAP16 bmp16;
717             bmp16.bmType       = bmp32->bmType;
718             bmp16.bmWidth      = bmp32->bmWidth;
719             bmp16.bmHeight     = bmp32->bmHeight;
720             bmp16.bmWidthBytes = bmp32->bmWidthBytes;
721             bmp16.bmPlanes     = bmp32->bmPlanes;
722             bmp16.bmBitsPixel  = bmp32->bmBitsPixel;
723             bmp16.bmBits       = (SEGPTR)0;
724             memcpy( buffer, &bmp16, count );
725             return count;
726         }
727         else
728         {
729             FIXME(bitmap, "not implemented for DIBs: count %d\n", count);
730             return 0;
731         }
732     }
733     else
734     {
735         BITMAP16 bmp16;
736         bmp16.bmType       = bmp->bitmap.bmType;
737         bmp16.bmWidth      = bmp->bitmap.bmWidth;
738         bmp16.bmHeight     = bmp->bitmap.bmHeight;
739         bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
740         bmp16.bmPlanes     = bmp->bitmap.bmPlanes;
741         bmp16.bmBitsPixel  = bmp->bitmap.bmBitsPixel;
742         bmp16.bmBits       = (SEGPTR)0;
743         if (count > sizeof(bmp16)) count = sizeof(bmp16);
744         memcpy( buffer, &bmp16, count );
745         return count;
746     }
747 }
748     
749
750 /***********************************************************************
751  *           BITMAP_GetObject32
752  */
753 INT32 BITMAP_GetObject32( BITMAPOBJ * bmp, INT32 count, LPVOID buffer )
754 {
755     if (bmp->dib)
756     {
757         if (count < sizeof(DIBSECTION))
758         {
759             if (count > sizeof(BITMAP32)) count = sizeof(BITMAP32);
760         }
761         else
762         {
763             if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
764         }
765
766         memcpy( buffer, &bmp->dib->dibSection, count );
767         return count;
768     }
769     else
770     {
771         if (count > sizeof(BITMAP32)) count = sizeof(BITMAP32);
772         memcpy( buffer, &bmp->bitmap, count );
773         return count;
774     }
775 }
776     
777
778 /***********************************************************************
779  *           CreateDiscardableBitmap16    (GDI.156)
780  */
781 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
782                                             INT16 height )
783 {
784     return CreateCompatibleBitmap16( hdc, width, height );
785 }
786
787
788 /******************************************************************************
789  * CreateDiscardableBitmap32 [GDI32.38]  Creates a discardable bitmap
790  *
791  * RETURNS
792  *    Success: Handle to bitmap
793  *    Failure: NULL
794  */
795 HBITMAP32 WINAPI CreateDiscardableBitmap32(
796     HDC32 hdc,    /* [in] Handle to device context */
797     INT32 width,  /* [in] Bitmap width */
798     INT32 height) /* [in] Bitmap height */
799 {
800     return CreateCompatibleBitmap32( hdc, width, height );
801 }
802
803
804 /***********************************************************************
805  *           GetBitmapDimensionEx16    (GDI.468)
806  *
807  * NOTES
808  *    Can this call GetBitmapDimensionEx32?
809  */
810 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
811 {
812     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
813     if (!bmp) return FALSE;
814     CONV_SIZE32TO16( &bmp->size, size );
815     GDI_HEAP_UNLOCK( hbitmap );
816     return TRUE;
817 }
818
819
820 /******************************************************************************
821  * GetBitmapDimensionEx32 [GDI32.144]  Retrieves dimensions of a bitmap
822  *
823  * RETURNS
824  *    Success: TRUE
825  *    Failure: FALSE
826  */
827 BOOL32 WINAPI GetBitmapDimensionEx32(
828     HBITMAP32 hbitmap, /* [in]  Handle to bitmap */
829     LPSIZE32 size)     /* [out] Address of struct receiving dimensions */
830 {
831     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
832     if (!bmp) return FALSE;
833     *size = bmp->size;
834     GDI_HEAP_UNLOCK( hbitmap );
835     return TRUE;
836 }
837
838
839 /***********************************************************************
840  *           GetBitmapDimension    (GDI.162)
841  */
842 DWORD WINAPI GetBitmapDimension( HBITMAP16 hbitmap )
843 {
844     SIZE16 size;
845     if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
846     return MAKELONG( size.cx, size.cy );
847 }
848
849
850 /***********************************************************************
851  *           SetBitmapDimensionEx16    (GDI.478)
852  */
853 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
854                                       LPSIZE16 prevSize )
855 {
856     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
857     if (!bmp) return FALSE;
858     if (prevSize) CONV_SIZE32TO16( &bmp->size, prevSize );
859     bmp->size.cx = x;
860     bmp->size.cy = y;
861     GDI_HEAP_UNLOCK( hbitmap );
862     return TRUE;
863 }
864
865
866 /******************************************************************************
867  * SetBitmapDimensionEx32 [GDI32.304]  Assignes dimensions to a bitmap
868  *
869  * RETURNS
870  *    Success: TRUE
871  *    Failure: FALSE
872  */
873 BOOL32 WINAPI SetBitmapDimensionEx32(
874     HBITMAP32 hbitmap, /* [in]  Handle to bitmap */
875     INT32 x,           /* [in]  Bitmap width */
876     INT32 y,           /* [in]  Bitmap height */
877     LPSIZE32 prevSize) /* [out] Address of structure for orig dims */
878 {
879     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
880     if (!bmp) return FALSE;
881     if (prevSize) *prevSize = bmp->size;
882     bmp->size.cx = x;
883     bmp->size.cy = y;
884     GDI_HEAP_UNLOCK( hbitmap );
885     return TRUE;
886 }
887
888
889 /***********************************************************************
890  *           SetBitmapDimension    (GDI.163)
891  */
892 DWORD WINAPI SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
893 {
894     SIZE16 size;
895     if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
896     return MAKELONG( size.cx, size.cy );
897 }
898