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