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