Replace some 16-bit calls by their 32-bit equivalents.
[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
11 #include "wine/winbase16.h"
12 #include "gdi.h"
13 #include "bitmap.h"
14 #include "heap.h"
15 #include "debugtools.h"
16 #include "wine/winuser16.h"
17
18 DEFAULT_DEBUG_CHANNEL(bitmap);
19
20 BITMAP_DRIVER *BITMAP_Driver = NULL;
21
22
23 /***********************************************************************
24  *           BITMAP_GetWidthBytes
25  *
26  * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
27  * data.
28  */
29 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
30 {
31     switch(bpp)
32     {
33     case 1:
34         return 2 * ((bmWidth+15) >> 4);
35
36     case 24:
37         bmWidth *= 3; /* fall through */
38     case 8:
39         return bmWidth + (bmWidth & 1);
40
41     case 32:
42         return bmWidth * 4;
43
44     case 16:
45     case 15:
46         return bmWidth * 2;
47
48     case 4:
49         return 2 * ((bmWidth+3) >> 2);
50
51     default:
52         WARN("Unknown depth %d, please report.\n", bpp );
53     }
54     return -1;
55 }
56
57 /***********************************************************************
58  *           CreateUserBitmap16    (GDI.407)
59  */
60 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
61                                      UINT16 bpp, LPCVOID bits )
62 {
63     return CreateBitmap16( width, height, planes, bpp, bits );
64 }
65
66 /***********************************************************************
67  *           CreateUserDiscardableBitmap16    (GDI.409)
68  */
69 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy, 
70                                                 INT16 width, INT16 height )
71 {
72     HDC hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
73     HBITMAP16 ret = CreateCompatibleBitmap16( hdc, width, height );
74     DeleteDC( hdc );
75     return ret;
76 }
77
78
79 /***********************************************************************
80  *           CreateBitmap16    (GDI.48)
81  *           CreateBitmap16    (DISPLAY.48)
82  */
83 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
84                                  UINT16 bpp, LPCVOID bits )
85 {
86     return CreateBitmap( width, height, planes, bpp, bits );
87 }
88
89
90 /******************************************************************************
91  * CreateBitmap [GDI32.@]  Creates a bitmap with the specified info
92  *
93  * PARAMS
94  *    width  [I] bitmap width
95  *    height [I] bitmap height
96  *    planes [I] Number of color planes
97  *    bpp    [I] Number of bits to identify a color
98  *    bits   [I] Pointer to array containing color data
99  *
100  * RETURNS
101  *    Success: Handle to bitmap
102  *    Failure: 0
103  */
104 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
105                                  UINT bpp, LPCVOID bits )
106 {
107     BITMAPOBJ *bmp;
108     HBITMAP hbitmap;
109
110     planes = (BYTE)planes;
111     bpp    = (BYTE)bpp;
112
113
114       /* Check parameters */
115     if (!height || !width) return 0;
116     if (planes != 1) {
117         FIXME("planes = %d\n", planes);
118         return 0;
119     }
120     if (height < 0) height = -height;
121     if (width < 0) width = -width;
122
123       /* Create the BITMAPOBJ */
124     if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC, &hbitmap ))) return 0;
125
126     TRACE("%dx%d, %d colors returning %08x\n", width, height,
127           1 << (planes*bpp), hbitmap);
128
129     bmp->size.cx = 0;
130     bmp->size.cy = 0;
131     bmp->bitmap.bmType = 0;
132     bmp->bitmap.bmWidth = width;
133     bmp->bitmap.bmHeight = height;
134     bmp->bitmap.bmPlanes = planes;
135     bmp->bitmap.bmBitsPixel = bpp;
136     bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
137     bmp->bitmap.bmBits = NULL;
138
139     bmp->funcs = NULL;
140     bmp->physBitmap = NULL;
141     bmp->dib = NULL;
142
143     if (bits) /* Set bitmap bits */
144         SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
145                          bits );
146     GDI_ReleaseObj( hbitmap );
147     return hbitmap;
148 }
149
150
151 /***********************************************************************
152  *           CreateCompatibleBitmap16    (GDI.51)
153  */
154 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
155 {
156     return CreateCompatibleBitmap( hdc, width, height );
157 }
158
159
160 /******************************************************************************
161  * CreateCompatibleBitmap [GDI32.@]  Creates a bitmap compatible with the DC
162  *
163  * PARAMS
164  *    hdc    [I] Handle to device context
165  *    width  [I] Width of bitmap
166  *    height [I] Height of bitmap
167  *
168  * RETURNS
169  *    Success: Handle to bitmap
170  *    Failure: 0
171  */
172 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
173 {
174     HBITMAP hbmpRet = 0;
175     DC *dc;
176
177     TRACE("(%04x,%d,%d) = \n", hdc, width, height );
178     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
179     if ((width >= 0x10000) || (height >= 0x10000)) {
180         FIXME("got bad width %d or height %d, please look for reason\n",
181               width, height );
182     } else {
183         /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
184         if (!width || !height) 
185            hbmpRet = CreateBitmap( 1, 1, 1, 1, NULL );
186         else 
187            hbmpRet = CreateBitmap( width, height, 1, dc->bitsPerPixel, NULL );
188         if(dc->funcs->pCreateBitmap)
189             dc->funcs->pCreateBitmap( hbmpRet );
190     }
191     TRACE("\t\t%04x\n", hbmpRet);
192     GDI_ReleaseObj(hdc);
193     return hbmpRet;
194 }
195
196
197 /***********************************************************************
198  *           CreateBitmapIndirect16    (GDI.49)
199  */
200 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
201 {
202     return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
203                            bmp->bmBitsPixel, MapSL( bmp->bmBits ) );
204 }
205
206
207 /******************************************************************************
208  * CreateBitmapIndirect [GDI32.@]  Creates a bitmap with the specifies info
209  *
210  * RETURNS
211  *    Success: Handle to bitmap
212  *    Failure: NULL
213  */
214 HBITMAP WINAPI CreateBitmapIndirect(
215     const BITMAP * bmp) /* [in] Pointer to the bitmap data */
216 {
217     return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
218                            bmp->bmBitsPixel, bmp->bmBits );
219 }
220
221
222 /***********************************************************************
223  *           GetBitmapBits16    (GDI.74)
224  */
225 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
226 {
227     return GetBitmapBits( hbitmap, count, buffer );
228 }
229
230
231 /***********************************************************************
232  * GetBitmapBits [GDI32.@]  Copies bitmap bits of bitmap to buffer
233  * 
234  * RETURNS
235  *    Success: Number of bytes copied
236  *    Failure: 0
237  */
238 LONG WINAPI GetBitmapBits(
239     HBITMAP hbitmap, /* [in]  Handle to bitmap */
240     LONG count,        /* [in]  Number of bytes to copy */
241     LPVOID bits)       /* [out] Pointer to buffer to receive bits */
242 {
243     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
244     LONG height, ret;
245     
246     if (!bmp) return 0;
247     
248     /* If the bits vector is null, the function should return the read size */
249     if(bits == NULL)
250     {
251         ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
252         goto done;
253     }
254
255     if (count < 0) {
256         WARN("(%ld): Negative number of bytes passed???\n", count );
257         count = -count;
258     }
259
260     /* Only get entire lines */
261     height = count / bmp->bitmap.bmWidthBytes;
262     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
263     count = height * bmp->bitmap.bmWidthBytes;
264     if (count == 0)
265       {
266         WARN("Less than one entire line requested\n");
267         ret = 0;
268         goto done;
269       }
270
271
272     TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
273           hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
274           1 << bmp->bitmap.bmBitsPixel, height );
275
276     if(bmp->funcs) { 
277
278         TRACE("Calling device specific BitmapBits\n");
279         if(bmp->funcs->pBitmapBits)
280             ret = bmp->funcs->pBitmapBits(hbitmap, bits, count, DDB_GET);
281         else {
282             ERR("BitmapBits == NULL??\n");
283             ret = 0;
284         }       
285
286     } else {
287
288         if(!bmp->bitmap.bmBits) {
289             WARN("Bitmap is empty\n");
290             ret = 0;
291         } else {
292             memcpy(bits, bmp->bitmap.bmBits, count);
293             ret = count;
294         }
295
296     }
297  done:
298     GDI_ReleaseObj( hbitmap );
299     return ret;
300 }
301
302
303 /***********************************************************************
304  *           SetBitmapBits16    (GDI.106)
305  */
306 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
307 {
308     return SetBitmapBits( hbitmap, count, buffer );
309 }
310
311
312 /******************************************************************************
313  * SetBitmapBits [GDI32.@]  Sets bits of color data for a bitmap
314  *
315  * RETURNS
316  *    Success: Number of bytes used in setting the bitmap bits
317  *    Failure: 0
318  */
319 LONG WINAPI SetBitmapBits(
320     HBITMAP hbitmap, /* [in] Handle to bitmap */
321     LONG count,        /* [in] Number of bytes in bitmap array */
322     LPCVOID bits)      /* [in] Address of array with bitmap bits */
323 {
324     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
325     LONG height, ret;
326     
327     if ((!bmp) || (!bits))
328         return 0;
329
330     if (count < 0) {
331         WARN("(%ld): Negative number of bytes passed???\n", count );
332         count = -count;
333     }
334
335     /* Only get entire lines */
336     height = count / bmp->bitmap.bmWidthBytes;
337     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
338     count = height * bmp->bitmap.bmWidthBytes;
339
340     TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
341           hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
342           1 << bmp->bitmap.bmBitsPixel, height );
343
344     if(bmp->funcs) {
345
346         TRACE("Calling device specific BitmapBits\n");
347         if(bmp->funcs->pBitmapBits)
348             ret = bmp->funcs->pBitmapBits(hbitmap, (void *) bits, count, DDB_SET);
349         else {
350             ERR("BitmapBits == NULL??\n");
351             ret = 0;
352         }
353         
354     } else {
355
356         if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
357             bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
358         if(!bmp->bitmap.bmBits) {
359             WARN("Unable to allocate bit buffer\n");
360             ret = 0;
361         } else {
362             memcpy(bmp->bitmap.bmBits, bits, count);
363             ret = count;
364         }
365     }
366
367     GDI_ReleaseObj( hbitmap );
368     return ret;
369 }
370
371 /**********************************************************************
372  *              BITMAP_CopyBitmap
373  *
374  */
375 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
376 {
377     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
378     HBITMAP res = 0;
379     BITMAP bm;
380
381     if(!bmp) return 0;
382
383     bm = bmp->bitmap;
384     bm.bmBits = NULL;
385     res = CreateBitmapIndirect(&bm);
386
387     if(res) {
388         char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
389                                bm.bmHeight );
390         GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
391         SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
392         HeapFree( GetProcessHeap(), 0, buf );
393     }
394
395     GDI_ReleaseObj( hbitmap );
396     return res;
397 }
398
399 /***********************************************************************
400  *           BITMAP_DeleteObject
401  */
402 BOOL BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
403 {
404     if (bmp->funcs && bmp->funcs->pDeleteObject)
405         bmp->funcs->pDeleteObject( hbitmap );
406
407     if( bmp->bitmap.bmBits )
408         HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
409
410     DIB_DeleteDIBSection( bmp );
411
412     return GDI_FreeObject( hbitmap, bmp );
413 }
414
415         
416 /***********************************************************************
417  *           BITMAP_GetObject16
418  */
419 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
420 {
421     if (bmp->dib)
422     {
423         if ( count <= sizeof(BITMAP16) )
424         {
425             BITMAP *bmp32 = &bmp->dib->dsBm;
426             BITMAP16 bmp16;
427             bmp16.bmType       = bmp32->bmType;
428             bmp16.bmWidth      = bmp32->bmWidth;
429             bmp16.bmHeight     = bmp32->bmHeight;
430             bmp16.bmWidthBytes = bmp32->bmWidthBytes;
431             bmp16.bmPlanes     = bmp32->bmPlanes;
432             bmp16.bmBitsPixel  = bmp32->bmBitsPixel;
433             bmp16.bmBits       = (SEGPTR)0;
434             memcpy( buffer, &bmp16, count );
435             return count;
436         }
437         else
438         {
439             FIXME("not implemented for DIBs: count %d\n", count);
440             return 0;
441         }
442     }
443     else
444     {
445         BITMAP16 bmp16;
446         bmp16.bmType       = bmp->bitmap.bmType;
447         bmp16.bmWidth      = bmp->bitmap.bmWidth;
448         bmp16.bmHeight     = bmp->bitmap.bmHeight;
449         bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
450         bmp16.bmPlanes     = bmp->bitmap.bmPlanes;
451         bmp16.bmBitsPixel  = bmp->bitmap.bmBitsPixel;
452         bmp16.bmBits       = (SEGPTR)0;
453         if (count > sizeof(bmp16)) count = sizeof(bmp16);
454         memcpy( buffer, &bmp16, count );
455         return count;
456     }
457 }
458     
459
460 /***********************************************************************
461  *           BITMAP_GetObject
462  */
463 INT BITMAP_GetObject( BITMAPOBJ * bmp, INT count, LPVOID buffer )
464 {
465     if (bmp->dib)
466     {
467         if (count < sizeof(DIBSECTION))
468         {
469             if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
470         }
471         else
472         {
473             if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
474         }
475
476         memcpy( buffer, bmp->dib, count );
477         return count;
478     }
479     else
480     {
481         if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
482         memcpy( buffer, &bmp->bitmap, count );
483         return count;
484     }
485 }
486     
487
488 /***********************************************************************
489  *           CreateDiscardableBitmap16    (GDI.156)
490  */
491 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
492                                             INT16 height )
493 {
494     return CreateCompatibleBitmap16( hdc, width, height );
495 }
496
497
498 /******************************************************************************
499  * CreateDiscardableBitmap [GDI32.@]  Creates a discardable bitmap
500  *
501  * RETURNS
502  *    Success: Handle to bitmap
503  *    Failure: NULL
504  */
505 HBITMAP WINAPI CreateDiscardableBitmap(
506     HDC hdc,    /* [in] Handle to device context */
507     INT width,  /* [in] Bitmap width */
508     INT height) /* [in] Bitmap height */
509 {
510     return CreateCompatibleBitmap( hdc, width, height );
511 }
512
513
514 /***********************************************************************
515  *           GetBitmapDimensionEx16    (GDI.468)
516  *
517  * NOTES
518  *    Can this call GetBitmapDimensionEx?
519  */
520 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
521 {
522     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
523     if (!bmp) return FALSE;
524     size->cx = bmp->size.cx;
525     size->cy = bmp->size.cy;
526     GDI_ReleaseObj( hbitmap );
527     return TRUE;
528 }
529
530
531 /******************************************************************************
532  * GetBitmapDimensionEx [GDI32.@]  Retrieves dimensions of a bitmap
533  *
534  * RETURNS
535  *    Success: TRUE
536  *    Failure: FALSE
537  */
538 BOOL WINAPI GetBitmapDimensionEx(
539     HBITMAP hbitmap, /* [in]  Handle to bitmap */
540     LPSIZE size)     /* [out] Address of struct receiving dimensions */
541 {
542     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
543     if (!bmp) return FALSE;
544     *size = bmp->size;
545     GDI_ReleaseObj( hbitmap );
546     return TRUE;
547 }
548
549
550 /***********************************************************************
551  *           GetBitmapDimension    (GDI.162)
552  */
553 DWORD WINAPI GetBitmapDimension16( HBITMAP16 hbitmap )
554 {
555     SIZE16 size;
556     if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
557     return MAKELONG( size.cx, size.cy );
558 }
559
560
561 /***********************************************************************
562  *           SetBitmapDimensionEx16    (GDI.478)
563  */
564 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
565                                       LPSIZE16 prevSize )
566 {
567     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
568     if (!bmp) return FALSE;
569     if (prevSize)
570     {
571         prevSize->cx = bmp->size.cx;
572         prevSize->cy = bmp->size.cy;
573     }
574     bmp->size.cx = x;
575     bmp->size.cy = y;
576     GDI_ReleaseObj( hbitmap );
577     return TRUE;
578 }
579
580
581 /******************************************************************************
582  * SetBitmapDimensionEx [GDI32.@]  Assignes dimensions to a bitmap
583  *
584  * RETURNS
585  *    Success: TRUE
586  *    Failure: FALSE
587  */
588 BOOL WINAPI SetBitmapDimensionEx(
589     HBITMAP hbitmap, /* [in]  Handle to bitmap */
590     INT x,           /* [in]  Bitmap width */
591     INT y,           /* [in]  Bitmap height */
592     LPSIZE prevSize) /* [out] Address of structure for orig dims */
593 {
594     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
595     if (!bmp) return FALSE;
596     if (prevSize) *prevSize = bmp->size;
597     bmp->size.cx = x;
598     bmp->size.cy = y;
599     GDI_ReleaseObj( hbitmap );
600     return TRUE;
601 }
602
603
604 /***********************************************************************
605  *           SetBitmapDimension    (GDI.163)
606  */
607 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
608 {
609     SIZE16 size;
610     if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
611     return MAKELONG( size.cx, size.cy );
612 }
613