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