Fixed other bugs within MMIO implementation. Now, it's possible to
[wine] / objects / bitmap.c
1 /*
2  * GDI bitmap objects
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1998 Huw D M Davies
6  */
7
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "wine/winbase16.h"
12 #include "gdi.h"
13 #include "dc.h"
14 #include "bitmap.h"
15 #include "heap.h"
16 #include "global.h"
17 #include "cursoricon.h"
18 #include "debugtools.h"
19 #include "monitor.h"
20 #include "wine/winuser16.h"
21
22 DEFAULT_DEBUG_CHANNEL(bitmap)
23 DECLARE_DEBUG_CHANNEL(resource)
24
25 BITMAP_DRIVER *BITMAP_Driver = NULL;
26
27
28 /***********************************************************************
29  *           BITMAP_GetWidthBytes
30  *
31  * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
32  * data.
33  */
34 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
35 {
36     switch(bpp)
37     {
38     case 1:
39         return 2 * ((bmWidth+15) >> 4);
40
41     case 24:
42         bmWidth *= 3; /* fall through */
43     case 8:
44         return bmWidth + (bmWidth & 1);
45
46     case 32:
47         return bmWidth * 4;
48
49     case 16:
50     case 15:
51         return bmWidth * 2;
52
53     case 4:
54         return 2 * ((bmWidth+3) >> 2);
55
56     default:
57         WARN("Unknown depth %d, please report.\n", bpp );
58     }
59     return -1;
60 }
61
62 /***********************************************************************
63  *           CreateUserBitmap16    (GDI.407)
64  */
65 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
66                                      UINT16 bpp, LPCVOID bits )
67 {
68     return CreateBitmap16( width, height, planes, bpp, bits );
69 }
70
71 /***********************************************************************
72  *           CreateUserDiscardableBitmap16    (GDI.409)
73  */
74 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy, 
75                                                 INT16 width, INT16 height )
76 {
77     return CreateUserBitmap16( width, height, 1, MONITOR_GetDepth(&MONITOR_PrimaryMonitor), NULL );
78 }
79
80
81 /***********************************************************************
82  *           CreateBitmap16    (GDI.48)
83  */
84 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
85                                  UINT16 bpp, LPCVOID bits )
86 {
87     return CreateBitmap( width, height, planes, bpp, bits );
88 }
89
90
91 /******************************************************************************
92  * CreateBitmap [GDI32.25]  Creates a bitmap with the specified info
93  *
94  * PARAMS
95  *    width  [I] bitmap width
96  *    height [I] bitmap height
97  *    planes [I] Number of color planes
98  *    bpp    [I] Number of bits to identify a color
99  *    bits   [I] Pointer to array containing color data
100  *
101  * RETURNS
102  *    Success: Handle to bitmap
103  *    Failure: 0
104  */
105 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
106                                  UINT bpp, LPCVOID bits )
107 {
108     BITMAPOBJ *bmp;
109     HBITMAP hbitmap;
110
111     planes = (BYTE)planes;
112     bpp    = (BYTE)bpp;
113
114
115       /* Check parameters */
116     if (!height || !width) return 0;
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     hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
126     if (!hbitmap) return 0;
127
128     TRACE("%dx%d, %d colors returning %08x\n", width, height,
129           1 << (planes*bpp), hbitmap);
130
131     bmp = (BITMAPOBJ *) GDI_HEAP_LOCK( hbitmap );
132
133     bmp->size.cx = 0;
134     bmp->size.cy = 0;
135     bmp->bitmap.bmType = 0;
136     bmp->bitmap.bmWidth = width;
137     bmp->bitmap.bmHeight = height;
138     bmp->bitmap.bmPlanes = planes;
139     bmp->bitmap.bmBitsPixel = bpp;
140     bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
141     bmp->bitmap.bmBits = NULL;
142
143     bmp->funcs = NULL;
144     bmp->physBitmap = NULL;
145     bmp->dib = NULL;
146
147     if (bits) /* Set bitmap bits */
148         SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
149                          bits );
150     GDI_HEAP_UNLOCK( hbitmap );
151     return hbitmap;
152 }
153
154
155 /***********************************************************************
156  *           CreateCompatibleBitmap16    (GDI.51)
157  */
158 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
159 {
160     return CreateCompatibleBitmap( hdc, width, height );
161 }
162
163
164 /******************************************************************************
165  * CreateCompatibleBitmap [GDI32.30]  Creates a bitmap compatible with the DC
166  *
167  * PARAMS
168  *    hdc    [I] Handle to device context
169  *    width  [I] Width of bitmap
170  *    height [I] Height of bitmap
171  *
172  * RETURNS
173  *    Success: Handle to bitmap
174  *    Failure: 0
175  */
176 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
177 {
178     HBITMAP hbmpRet = 0;
179     DC *dc;
180
181     TRACE("(%04x,%d,%d) = \n", hdc, width, height );
182     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
183     if ((width >= 0x10000) || (height >= 0x10000)) {
184         FIXME("got bad width %d or height %d, please look for reason\n",
185               width, height );
186     } else {
187         /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
188         if (!width || !height) 
189            hbmpRet = CreateBitmap( 1, 1, 1, 1, NULL );
190         else 
191            hbmpRet = CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
192         if(dc->funcs->pCreateBitmap)
193             dc->funcs->pCreateBitmap( hbmpRet );
194     }
195     TRACE("\t\t%04x\n", hbmpRet);
196     GDI_HEAP_UNLOCK(hdc);
197     return hbmpRet;
198 }
199
200
201 /***********************************************************************
202  *           CreateBitmapIndirect16    (GDI.49)
203  */
204 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
205 {
206     return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
207                            bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
208 }
209
210
211 /******************************************************************************
212  * CreateBitmapIndirect [GDI32.26]  Creates a bitmap with the specifies info
213  *
214  * RETURNS
215  *    Success: Handle to bitmap
216  *    Failure: NULL
217  */
218 HBITMAP WINAPI CreateBitmapIndirect(
219     const BITMAP * bmp) /* [in] Pointer to the bitmap data */
220 {
221     return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
222                            bmp->bmBitsPixel, bmp->bmBits );
223 }
224
225
226 /***********************************************************************
227  *           GetBitmapBits16    (GDI.74)
228  */
229 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
230 {
231     return GetBitmapBits( hbitmap, count, buffer );
232 }
233
234
235 /***********************************************************************
236  * GetBitmapBits [GDI32.143]  Copies bitmap bits of bitmap to buffer
237  * 
238  * RETURNS
239  *    Success: Number of bytes copied
240  *    Failure: 0
241  */
242 LONG WINAPI GetBitmapBits(
243     HBITMAP hbitmap, /* [in]  Handle to bitmap */
244     LONG count,        /* [in]  Number of bytes to copy */
245     LPVOID bits)       /* [out] Pointer to buffer to receive bits */
246 {
247     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
248     LONG height, ret;
249     
250     if (!bmp) return 0;
251     
252     /* If the bits vector is null, the function should return the read size */
253     if(bits == NULL)
254         return bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
255
256     if (count < 0) {
257         WARN("(%ld): Negative number of bytes passed???\n", count );
258         count = -count;
259     }
260
261     /* Only get entire lines */
262     height = count / bmp->bitmap.bmWidthBytes;
263     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
264     count = height * bmp->bitmap.bmWidthBytes;
265     if (count == 0)
266       {
267         WARN("Less then one entire line requested\n");
268         GDI_HEAP_UNLOCK( hbitmap );
269         return 0;
270       }
271
272
273     TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
274           hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
275           1 << bmp->bitmap.bmBitsPixel, height );
276
277     if(bmp->funcs) { 
278
279         TRACE("Calling device specific BitmapBits\n");
280         if(bmp->funcs->pBitmapBits)
281             ret = bmp->funcs->pBitmapBits(hbitmap, bits, count, DDB_GET);
282         else {
283             ERR("BitmapBits == NULL??\n");
284             ret = 0;
285         }       
286
287     } else {
288
289         if(!bmp->bitmap.bmBits) {
290             WARN("Bitmap is empty\n");
291             ret = 0;
292         } else {
293             memcpy(bits, bmp->bitmap.bmBits, count);
294             ret = count;
295         }
296
297     }
298
299     GDI_HEAP_UNLOCK( hbitmap );
300     return ret;
301 }
302
303
304 /***********************************************************************
305  *           SetBitmapBits16    (GDI.106)
306  */
307 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
308 {
309     return SetBitmapBits( hbitmap, count, buffer );
310 }
311
312
313 /******************************************************************************
314  * SetBitmapBits [GDI32.303]  Sets bits of color data for a bitmap
315  *
316  * RETURNS
317  *    Success: Number of bytes used in setting the bitmap bits
318  *    Failure: 0
319  */
320 LONG WINAPI SetBitmapBits(
321     HBITMAP hbitmap, /* [in] Handle to bitmap */
322     LONG count,        /* [in] Number of bytes in bitmap array */
323     LPCVOID bits)      /* [in] Address of array with bitmap bits */
324 {
325     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
326     LONG height, ret;
327     
328     if ((!bmp) || (!bits))
329         return 0;
330
331     if (count < 0) {
332         WARN("(%ld): Negative number of bytes passed???\n", count );
333         count = -count;
334     }
335
336     /* Only get entire lines */
337     height = count / bmp->bitmap.bmWidthBytes;
338     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
339     count = height * bmp->bitmap.bmWidthBytes;
340
341     TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
342           hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
343           1 << bmp->bitmap.bmBitsPixel, height );
344
345     if(bmp->funcs) {
346
347         TRACE("Calling device specific BitmapBits\n");
348         if(bmp->funcs->pBitmapBits)
349             ret = bmp->funcs->pBitmapBits(hbitmap, (void *) bits, count, DDB_SET);
350         else {
351             ERR("BitmapBits == NULL??\n");
352             ret = 0;
353         }
354         
355     } else {
356
357         if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
358             bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
359         if(!bmp->bitmap.bmBits) {
360             WARN("Unable to allocate bit buffer\n");
361             ret = 0;
362         } else {
363             memcpy(bmp->bitmap.bmBits, bits, count);
364             ret = count;
365         }
366     }
367
368     GDI_HEAP_UNLOCK( hbitmap );
369     return ret;
370 }
371
372 /**********************************************************************
373  *              BITMAP_CopyBitmap
374  *
375  */
376 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
377 {
378     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
379     HBITMAP res = 0;
380     BITMAP bm;
381
382     if(!bmp) return 0;
383
384     bm = bmp->bitmap;
385     bm.bmBits = NULL;
386     res = CreateBitmapIndirect(&bm);
387
388     if(res) {
389         char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
390                                bm.bmHeight );
391         GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
392         SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
393         HeapFree( GetProcessHeap(), 0, buf );
394     }
395
396     GDI_HEAP_UNLOCK( hbitmap );
397     return res;
398 }
399
400 /***********************************************************************
401  *           BITMAP_DeleteObject
402  */
403 BOOL BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
404 {
405     if (bmp->funcs && bmp->funcs->pDeleteObject)
406         bmp->funcs->pDeleteObject( hbitmap );
407
408     if( bmp->bitmap.bmBits )
409         HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
410
411     DIB_DeleteDIBSection( bmp );
412
413     return GDI_FreeObject( hbitmap );
414 }
415
416         
417 /***********************************************************************
418  *           BITMAP_GetObject16
419  */
420 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
421 {
422     if (bmp->dib)
423     {
424         if ( count <= sizeof(BITMAP16) )
425         {
426             BITMAP *bmp32 = &bmp->dib->dsBm;
427             BITMAP16 bmp16;
428             bmp16.bmType       = bmp32->bmType;
429             bmp16.bmWidth      = bmp32->bmWidth;
430             bmp16.bmHeight     = bmp32->bmHeight;
431             bmp16.bmWidthBytes = bmp32->bmWidthBytes;
432             bmp16.bmPlanes     = bmp32->bmPlanes;
433             bmp16.bmBitsPixel  = bmp32->bmBitsPixel;
434             bmp16.bmBits       = (SEGPTR)0;
435             memcpy( buffer, &bmp16, count );
436             return count;
437         }
438         else
439         {
440             FIXME("not implemented for DIBs: count %d\n", count);
441             return 0;
442         }
443     }
444     else
445     {
446         BITMAP16 bmp16;
447         bmp16.bmType       = bmp->bitmap.bmType;
448         bmp16.bmWidth      = bmp->bitmap.bmWidth;
449         bmp16.bmHeight     = bmp->bitmap.bmHeight;
450         bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
451         bmp16.bmPlanes     = bmp->bitmap.bmPlanes;
452         bmp16.bmBitsPixel  = bmp->bitmap.bmBitsPixel;
453         bmp16.bmBits       = (SEGPTR)0;
454         if (count > sizeof(bmp16)) count = sizeof(bmp16);
455         memcpy( buffer, &bmp16, count );
456         return count;
457     }
458 }
459     
460
461 /***********************************************************************
462  *           BITMAP_GetObject
463  */
464 INT BITMAP_GetObject( BITMAPOBJ * bmp, INT count, LPVOID buffer )
465 {
466     if (bmp->dib)
467     {
468         if (count < sizeof(DIBSECTION))
469         {
470             if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
471         }
472         else
473         {
474             if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
475         }
476
477         memcpy( buffer, bmp->dib, count );
478         return count;
479     }
480     else
481     {
482         if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
483         memcpy( buffer, &bmp->bitmap, count );
484         return count;
485     }
486 }
487     
488
489 /***********************************************************************
490  *           CreateDiscardableBitmap16    (GDI.156)
491  */
492 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
493                                             INT16 height )
494 {
495     return CreateCompatibleBitmap16( hdc, width, height );
496 }
497
498
499 /******************************************************************************
500  * CreateDiscardableBitmap [GDI32.38]  Creates a discardable bitmap
501  *
502  * RETURNS
503  *    Success: Handle to bitmap
504  *    Failure: NULL
505  */
506 HBITMAP WINAPI CreateDiscardableBitmap(
507     HDC hdc,    /* [in] Handle to device context */
508     INT width,  /* [in] Bitmap width */
509     INT height) /* [in] Bitmap height */
510 {
511     return CreateCompatibleBitmap( hdc, width, height );
512 }
513
514
515 /***********************************************************************
516  *           GetBitmapDimensionEx16    (GDI.468)
517  *
518  * NOTES
519  *    Can this call GetBitmapDimensionEx?
520  */
521 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
522 {
523     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
524     if (!bmp) return FALSE;
525     CONV_SIZE32TO16( &bmp->size, size );
526     GDI_HEAP_UNLOCK( hbitmap );
527     return TRUE;
528 }
529
530
531 /******************************************************************************
532  * GetBitmapDimensionEx [GDI32.144]  Retrieves dimensions of a bitmap
533  *
534  * RETURNS
535  *    Success: TRUE
536  *    Failure: FALSE
537  */
538 BOOL WINAPI GetBitmapDimensionEx(
539     HBITMAP hbitmap, /* [in]  Handle to bitmap */
540     LPSIZE size)     /* [out] Address of struct receiving dimensions */
541 {
542     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
543     if (!bmp) return FALSE;
544     *size = bmp->size;
545     GDI_HEAP_UNLOCK( hbitmap );
546     return TRUE;
547 }
548
549
550 /***********************************************************************
551  *           GetBitmapDimension    (GDI.162)
552  */
553 DWORD WINAPI GetBitmapDimension16( HBITMAP16 hbitmap )
554 {
555     SIZE16 size;
556     if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
557     return MAKELONG( size.cx, size.cy );
558 }
559
560
561 /***********************************************************************
562  *           SetBitmapDimensionEx16    (GDI.478)
563  */
564 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
565                                       LPSIZE16 prevSize )
566 {
567     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
568     if (!bmp) return FALSE;
569     if (prevSize) CONV_SIZE32TO16( &bmp->size, prevSize );
570     bmp->size.cx = x;
571     bmp->size.cy = y;
572     GDI_HEAP_UNLOCK( hbitmap );
573     return TRUE;
574 }
575
576
577 /******************************************************************************
578  * SetBitmapDimensionEx [GDI32.304]  Assignes dimensions to a bitmap
579  *
580  * RETURNS
581  *    Success: TRUE
582  *    Failure: FALSE
583  */
584 BOOL WINAPI SetBitmapDimensionEx(
585     HBITMAP hbitmap, /* [in]  Handle to bitmap */
586     INT x,           /* [in]  Bitmap width */
587     INT y,           /* [in]  Bitmap height */
588     LPSIZE prevSize) /* [out] Address of structure for orig dims */
589 {
590     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
591     if (!bmp) return FALSE;
592     if (prevSize) *prevSize = bmp->size;
593     bmp->size.cx = x;
594     bmp->size.cy = y;
595     GDI_HEAP_UNLOCK( hbitmap );
596     return TRUE;
597 }
598
599
600 /***********************************************************************
601  *           SetBitmapDimension    (GDI.163)
602  */
603 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
604 {
605     SIZE16 size;
606     if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
607     return MAKELONG( size.cx, size.cy );
608 }
609