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