Fixed aliasing problems (do not use (POINT*)&rect constructs).
[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 static 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 /******************************************************************************
86  * CreateBitmap [GDI32.@]  Creates a bitmap with the specified info
87  *
88  * PARAMS
89  *    width  [I] bitmap width
90  *    height [I] bitmap height
91  *    planes [I] Number of color planes
92  *    bpp    [I] Number of bits to identify a color
93  *    bits   [I] Pointer to array containing color data
94  *
95  * RETURNS
96  *    Success: Handle to bitmap
97  *    Failure: 0
98  */
99 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
100                                  UINT bpp, LPCVOID bits )
101 {
102     BITMAPOBJ *bmp;
103     HBITMAP hbitmap;
104
105     planes = (BYTE)planes;
106     bpp    = (BYTE)bpp;
107
108
109       /* Check parameters */
110     if (!height || !width)
111     {
112         height = 1;
113         width  = 1;
114         planes = 1;
115         bpp    = 1;
116     }
117     if (planes != 1) {
118         FIXME("planes = %d\n", planes);
119         return 0;
120     }
121     if (height < 0) height = -height;
122     if (width < 0) width = -width;
123
124       /* Create the BITMAPOBJ */
125     if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC,
126                                  (HGDIOBJ *)&hbitmap, &bitmap_funcs )))
127         return 0;
128
129     TRACE("%dx%d, %d colors returning %08x\n", width, height,
130           1 << (planes*bpp), hbitmap);
131
132     bmp->size.cx = 0;
133     bmp->size.cy = 0;
134     bmp->bitmap.bmType = 0;
135     bmp->bitmap.bmWidth = width;
136     bmp->bitmap.bmHeight = height;
137     bmp->bitmap.bmPlanes = planes;
138     bmp->bitmap.bmBitsPixel = bpp;
139     bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
140     bmp->bitmap.bmBits = NULL;
141
142     bmp->funcs = NULL;
143     bmp->physBitmap = NULL;
144     bmp->dib = NULL;
145     bmp->segptr_bits = 0;
146
147     if (bits) /* Set bitmap bits */
148         SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
149                          bits );
150     GDI_ReleaseObj( hbitmap );
151     return hbitmap;
152 }
153
154
155 /******************************************************************************
156  * CreateCompatibleBitmap [GDI32.@]  Creates a bitmap compatible with the DC
157  *
158  * PARAMS
159  *    hdc    [I] Handle to device context
160  *    width  [I] Width of bitmap
161  *    height [I] Height of bitmap
162  *
163  * RETURNS
164  *    Success: Handle to bitmap
165  *    Failure: 0
166  */
167 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
168 {
169     HBITMAP hbmpRet = 0;
170     DC *dc;
171
172     TRACE("(%04x,%d,%d) = \n", hdc, width, height );
173     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
174     if ((width >= 0x10000) || (height >= 0x10000)) {
175         FIXME("got bad width %d or height %d, please look for reason\n",
176               width, height );
177     } else {
178         /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
179         if (!width || !height)
180            hbmpRet = CreateBitmap( 1, 1, 1, 1, NULL );
181         else
182            hbmpRet = CreateBitmap( width, height, 1, dc->bitsPerPixel, NULL );
183
184         if (!BITMAP_SetOwnerDC( hbmpRet, dc ))
185         {
186             DeleteObject( hbmpRet );
187             hbmpRet = 0;
188         }
189     }
190     TRACE("\t\t%04x\n", hbmpRet);
191     GDI_ReleaseObj(hdc);
192     return hbmpRet;
193 }
194
195
196 /******************************************************************************
197  * CreateBitmapIndirect [GDI32.@]  Creates a bitmap with the specifies info
198  *
199  * RETURNS
200  *    Success: Handle to bitmap
201  *    Failure: NULL
202  */
203 HBITMAP WINAPI CreateBitmapIndirect(
204     const BITMAP * bmp) /* [in] Pointer to the bitmap data */
205 {
206     return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
207                            bmp->bmBitsPixel, bmp->bmBits );
208 }
209
210
211 /***********************************************************************
212  * GetBitmapBits [GDI32.@]  Copies bitmap bits of bitmap to buffer
213  *
214  * RETURNS
215  *    Success: Number of bytes copied
216  *    Failure: 0
217  */
218 LONG WINAPI GetBitmapBits(
219     HBITMAP hbitmap, /* [in]  Handle to bitmap */
220     LONG count,        /* [in]  Number of bytes to copy */
221     LPVOID bits)       /* [out] Pointer to buffer to receive bits */
222 {
223     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
224     LONG height, ret;
225
226     if (!bmp) return 0;
227
228     /* If the bits vector is null, the function should return the read size */
229     if(bits == NULL)
230     {
231         ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
232         goto done;
233     }
234
235     if (count < 0) {
236         WARN("(%ld): Negative number of bytes passed???\n", count );
237         count = -count;
238     }
239
240     /* Only get entire lines */
241     height = count / bmp->bitmap.bmWidthBytes;
242     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
243     count = height * bmp->bitmap.bmWidthBytes;
244     if (count == 0)
245       {
246         WARN("Less than one entire line requested\n");
247         ret = 0;
248         goto done;
249       }
250
251
252     TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
253           hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
254           1 << bmp->bitmap.bmBitsPixel, height );
255
256     if(bmp->funcs)
257     {
258         TRACE("Calling device specific BitmapBits\n");
259         if(bmp->funcs->pGetBitmapBits)
260             ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
261         else
262         {
263             memset( bits, 0, count );
264             ret = count;
265         }
266     } else {
267
268         if(!bmp->bitmap.bmBits) {
269             WARN("Bitmap is empty\n");
270             ret = 0;
271         } else {
272             memcpy(bits, bmp->bitmap.bmBits, count);
273             ret = count;
274         }
275
276     }
277  done:
278     GDI_ReleaseObj( hbitmap );
279     return ret;
280 }
281
282
283 /******************************************************************************
284  * SetBitmapBits [GDI32.@]  Sets bits of color data for a bitmap
285  *
286  * RETURNS
287  *    Success: Number of bytes used in setting the bitmap bits
288  *    Failure: 0
289  */
290 LONG WINAPI SetBitmapBits(
291     HBITMAP hbitmap, /* [in] Handle to bitmap */
292     LONG count,        /* [in] Number of bytes in bitmap array */
293     LPCVOID bits)      /* [in] Address of array with bitmap bits */
294 {
295     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
296     LONG height, ret;
297
298     if ((!bmp) || (!bits))
299         return 0;
300
301     if (count < 0) {
302         WARN("(%ld): Negative number of bytes passed???\n", count );
303         count = -count;
304     }
305
306     /* Only get entire lines */
307     height = count / bmp->bitmap.bmWidthBytes;
308     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
309     count = height * bmp->bitmap.bmWidthBytes;
310
311     TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
312           hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
313           1 << bmp->bitmap.bmBitsPixel, height );
314
315     if(bmp->funcs) {
316
317         TRACE("Calling device specific BitmapBits\n");
318         if(bmp->funcs->pSetBitmapBits)
319             ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
320         else
321             ret = 0;
322     } else {
323
324         if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
325             bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
326         if(!bmp->bitmap.bmBits) {
327             WARN("Unable to allocate bit buffer\n");
328             ret = 0;
329         } else {
330             memcpy(bmp->bitmap.bmBits, bits, count);
331             ret = count;
332         }
333     }
334
335     GDI_ReleaseObj( hbitmap );
336     return ret;
337 }
338
339 /**********************************************************************
340  *              BITMAP_CopyBitmap
341  *
342  */
343 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
344 {
345     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
346     HBITMAP res = 0;
347     BITMAP bm;
348
349     if(!bmp) return 0;
350
351     bm = bmp->bitmap;
352     bm.bmBits = NULL;
353     res = CreateBitmapIndirect(&bm);
354
355     if(res) {
356         char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
357                                bm.bmHeight );
358         GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
359         SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
360         HeapFree( GetProcessHeap(), 0, buf );
361     }
362
363     GDI_ReleaseObj( hbitmap );
364     return res;
365 }
366
367
368 /***********************************************************************
369  *           BITMAP_SetOwnerDC
370  *
371  * Set the type of DC that owns the bitmap. This is used when the
372  * bitmap is selected into a device to initialize the bitmap function
373  * table.
374  */
375 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
376 {
377     BITMAPOBJ *bitmap;
378     BOOL ret;
379
380     /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
381     if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
382
383     if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
384
385     ret = TRUE;
386     if (!bitmap->funcs)  /* not owned by a DC yet */
387     {
388         if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap );
389         if (ret) bitmap->funcs = dc->funcs;
390     }
391     else if (bitmap->funcs != dc->funcs)
392     {
393         FIXME( "Trying to select bitmap %x in different DC type\n", hbitmap );
394         ret = FALSE;
395     }
396     GDI_ReleaseObj( hbitmap );
397     return ret;
398 }
399
400
401 /***********************************************************************
402  *           BITMAP_SelectObject
403  */
404 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
405 {
406     HGDIOBJ ret;
407     BITMAPOBJ *bitmap = obj;
408     DC *dc = DC_GetDCPtr( hdc );
409
410     if (!dc) return 0;
411     if (!(dc->flags & DC_MEMORY))
412     {
413         GDI_ReleaseObj( hdc );
414         return 0;
415     }
416     ret = dc->hBitmap;
417     if (handle == dc->hBitmap) goto done;  /* nothing to do */
418
419     if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
420     {
421         WARN( "Bitmap already selected in another DC\n" );
422         GDI_ReleaseObj( hdc );
423         return 0;
424     }
425
426     if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
427     {
428         GDI_ReleaseObj( hdc );
429         return 0;
430     }
431
432     if (dc->funcs->pSelectBitmap) handle = dc->funcs->pSelectBitmap( dc->physDev, handle );
433
434     if (handle)
435     {
436         dc->hBitmap            = handle;
437         dc->totalExtent.left   = 0;
438         dc->totalExtent.top    = 0;
439         dc->totalExtent.right  = bitmap->bitmap.bmWidth;
440         dc->totalExtent.bottom = bitmap->bitmap.bmHeight;
441         dc->flags &= ~DC_DIRTY;
442         SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
443         CLIPPING_UpdateGCRegion( dc );
444
445         if (dc->bitsPerPixel != bitmap->bitmap.bmBitsPixel)
446         {
447             /* depth changed, reinitialize the DC */
448             dc->bitsPerPixel = bitmap->bitmap.bmBitsPixel;
449             DC_InitDC( dc );
450         }
451     }
452     else ret = 0;
453
454  done:
455     GDI_ReleaseObj( hdc );
456     return ret;
457 }
458
459
460 /***********************************************************************
461  *           BITMAP_DeleteObject
462  */
463 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
464 {
465     BITMAPOBJ * bmp = obj;
466
467     if (bmp->funcs && bmp->funcs->pDeleteBitmap)
468         bmp->funcs->pDeleteBitmap( handle );
469
470     if( bmp->bitmap.bmBits )
471         HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
472
473     if (bmp->dib)
474     {
475         DIBSECTION *dib = bmp->dib;
476
477         if (dib->dsBm.bmBits)
478         {
479             if (dib->dshSection)
480             {
481                 SYSTEM_INFO SystemInfo;
482                 GetSystemInfo( &SystemInfo );
483                 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
484                                  (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
485             }
486             else if (!dib->dsOffset)
487                 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
488         }
489         HeapFree(GetProcessHeap(), 0, dib);
490         bmp->dib = NULL;
491         if (bmp->segptr_bits)
492         { /* free its selector array */
493             WORD sel = SELECTOROF(bmp->segptr_bits);
494             WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
495             int i;
496
497             for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
498         }
499     }
500     return GDI_FreeObject( handle, obj );
501 }
502
503
504 /***********************************************************************
505  *           BITMAP_GetObject16
506  */
507 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
508 {
509     BITMAPOBJ *bmp = obj;
510
511     if (bmp->dib)
512     {
513         if ( count <= sizeof(BITMAP16) )
514         {
515             BITMAP *bmp32 = &bmp->dib->dsBm;
516             BITMAP16 bmp16;
517             bmp16.bmType       = bmp32->bmType;
518             bmp16.bmWidth      = bmp32->bmWidth;
519             bmp16.bmHeight     = bmp32->bmHeight;
520             bmp16.bmWidthBytes = bmp32->bmWidthBytes;
521             bmp16.bmPlanes     = bmp32->bmPlanes;
522             bmp16.bmBitsPixel  = bmp32->bmBitsPixel;
523             bmp16.bmBits       = (SEGPTR)0;
524             memcpy( buffer, &bmp16, count );
525             return count;
526         }
527         else
528         {
529             FIXME("not implemented for DIBs: count %d\n", count);
530             return 0;
531         }
532     }
533     else
534     {
535         BITMAP16 bmp16;
536         bmp16.bmType       = bmp->bitmap.bmType;
537         bmp16.bmWidth      = bmp->bitmap.bmWidth;
538         bmp16.bmHeight     = bmp->bitmap.bmHeight;
539         bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
540         bmp16.bmPlanes     = bmp->bitmap.bmPlanes;
541         bmp16.bmBitsPixel  = bmp->bitmap.bmBitsPixel;
542         bmp16.bmBits       = (SEGPTR)0;
543         if (count > sizeof(bmp16)) count = sizeof(bmp16);
544         memcpy( buffer, &bmp16, count );
545         return count;
546     }
547 }
548
549
550 /***********************************************************************
551  *           BITMAP_GetObject
552  */
553 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
554 {
555     BITMAPOBJ *bmp = obj;
556
557     if (bmp->dib)
558     {
559         if (count < sizeof(DIBSECTION))
560         {
561             if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
562         }
563         else
564         {
565             if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
566         }
567
568         memcpy( buffer, bmp->dib, count );
569         return count;
570     }
571     else
572     {
573         if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
574         memcpy( buffer, &bmp->bitmap, count );
575         return count;
576     }
577 }
578
579
580 /******************************************************************************
581  * CreateDiscardableBitmap [GDI32.@]  Creates a discardable bitmap
582  *
583  * RETURNS
584  *    Success: Handle to bitmap
585  *    Failure: NULL
586  */
587 HBITMAP WINAPI CreateDiscardableBitmap(
588     HDC hdc,    /* [in] Handle to device context */
589     INT width,  /* [in] Bitmap width */
590     INT height) /* [in] Bitmap height */
591 {
592     return CreateCompatibleBitmap( hdc, width, height );
593 }
594
595
596 /******************************************************************************
597  * GetBitmapDimensionEx [GDI32.@]  Retrieves dimensions of a bitmap
598  *
599  * RETURNS
600  *    Success: TRUE
601  *    Failure: FALSE
602  */
603 BOOL WINAPI GetBitmapDimensionEx(
604     HBITMAP hbitmap, /* [in]  Handle to bitmap */
605     LPSIZE size)     /* [out] Address of struct receiving dimensions */
606 {
607     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
608     if (!bmp) return FALSE;
609     *size = bmp->size;
610     GDI_ReleaseObj( hbitmap );
611     return TRUE;
612 }
613
614
615 /******************************************************************************
616  * SetBitmapDimensionEx [GDI32.@]  Assignes dimensions to a bitmap
617  *
618  * RETURNS
619  *    Success: TRUE
620  *    Failure: FALSE
621  */
622 BOOL WINAPI SetBitmapDimensionEx(
623     HBITMAP hbitmap, /* [in]  Handle to bitmap */
624     INT x,           /* [in]  Bitmap width */
625     INT y,           /* [in]  Bitmap height */
626     LPSIZE prevSize) /* [out] Address of structure for orig dims */
627 {
628     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
629     if (!bmp) return FALSE;
630     if (prevSize) *prevSize = bmp->size;
631     bmp->size.cx = x;
632     bmp->size.cy = y;
633     GDI_ReleaseObj( hbitmap );
634     return TRUE;
635 }