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