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