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