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