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