Set selector 0000H base to 0xf0000 until the first 64K are
[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 "debugtools.h"
18 #include "wine/winuser16.h"
19
20 DEFAULT_DEBUG_CHANNEL(bitmap);
21
22 BITMAP_DRIVER *BITMAP_Driver = NULL;
23
24
25 /***********************************************************************
26  *           BITMAP_GetWidthBytes
27  *
28  * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
29  * data.
30  */
31 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
32 {
33     switch(bpp)
34     {
35     case 1:
36         return 2 * ((bmWidth+15) >> 4);
37
38     case 24:
39         bmWidth *= 3; /* fall through */
40     case 8:
41         return bmWidth + (bmWidth & 1);
42
43     case 32:
44         return bmWidth * 4;
45
46     case 16:
47     case 15:
48         return bmWidth * 2;
49
50     case 4:
51         return 2 * ((bmWidth+3) >> 2);
52
53     default:
54         WARN("Unknown depth %d, please report.\n", bpp );
55     }
56     return -1;
57 }
58
59 /***********************************************************************
60  *           CreateUserBitmap16    (GDI.407)
61  */
62 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
63                                      UINT16 bpp, LPCVOID bits )
64 {
65     return CreateBitmap16( width, height, planes, bpp, bits );
66 }
67
68 /***********************************************************************
69  *           CreateUserDiscardableBitmap16    (GDI.409)
70  */
71 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy, 
72                                                 INT16 width, INT16 height )
73 {
74     HDC hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
75     HBITMAP16 ret = CreateCompatibleBitmap16( hdc, width, height );
76     DeleteDC( hdc );
77     return ret;
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     if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC, &hbitmap ))) return 0;
126
127     TRACE("%dx%d, %d colors returning %08x\n", width, height,
128           1 << (planes*bpp), hbitmap);
129
130     bmp->size.cx = 0;
131     bmp->size.cy = 0;
132     bmp->bitmap.bmType = 0;
133     bmp->bitmap.bmWidth = width;
134     bmp->bitmap.bmHeight = height;
135     bmp->bitmap.bmPlanes = planes;
136     bmp->bitmap.bmBitsPixel = bpp;
137     bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
138     bmp->bitmap.bmBits = NULL;
139
140     bmp->funcs = NULL;
141     bmp->physBitmap = NULL;
142     bmp->dib = NULL;
143
144     if (bits) /* Set bitmap bits */
145         SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
146                          bits );
147     GDI_ReleaseObj( hbitmap );
148     return hbitmap;
149 }
150
151
152 /***********************************************************************
153  *           CreateCompatibleBitmap16    (GDI.51)
154  */
155 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
156 {
157     return CreateCompatibleBitmap( hdc, width, height );
158 }
159
160
161 /******************************************************************************
162  * CreateCompatibleBitmap [GDI32.30]  Creates a bitmap compatible with the DC
163  *
164  * PARAMS
165  *    hdc    [I] Handle to device context
166  *    width  [I] Width of bitmap
167  *    height [I] Height of bitmap
168  *
169  * RETURNS
170  *    Success: Handle to bitmap
171  *    Failure: 0
172  */
173 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
174 {
175     HBITMAP hbmpRet = 0;
176     DC *dc;
177
178     TRACE("(%04x,%d,%d) = \n", hdc, width, height );
179     if (!(dc = DC_GetDCPtr( hdc ))) return 0;
180     if ((width >= 0x10000) || (height >= 0x10000)) {
181         FIXME("got bad width %d or height %d, please look for reason\n",
182               width, height );
183     } else {
184         /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
185         if (!width || !height) 
186            hbmpRet = CreateBitmap( 1, 1, 1, 1, NULL );
187         else 
188            hbmpRet = CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
189         if(dc->funcs->pCreateBitmap)
190             dc->funcs->pCreateBitmap( hbmpRet );
191     }
192     TRACE("\t\t%04x\n", hbmpRet);
193     GDI_ReleaseObj(hdc);
194     return hbmpRet;
195 }
196
197
198 /***********************************************************************
199  *           CreateBitmapIndirect16    (GDI.49)
200  */
201 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
202 {
203     return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
204                            bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
205 }
206
207
208 /******************************************************************************
209  * CreateBitmapIndirect [GDI32.26]  Creates a bitmap with the specifies info
210  *
211  * RETURNS
212  *    Success: Handle to bitmap
213  *    Failure: NULL
214  */
215 HBITMAP WINAPI CreateBitmapIndirect(
216     const BITMAP * bmp) /* [in] Pointer to the bitmap data */
217 {
218     return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
219                            bmp->bmBitsPixel, bmp->bmBits );
220 }
221
222
223 /***********************************************************************
224  *           GetBitmapBits16    (GDI.74)
225  */
226 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
227 {
228     return GetBitmapBits( hbitmap, count, buffer );
229 }
230
231
232 /***********************************************************************
233  * GetBitmapBits [GDI32.143]  Copies bitmap bits of bitmap to buffer
234  * 
235  * RETURNS
236  *    Success: Number of bytes copied
237  *    Failure: 0
238  */
239 LONG WINAPI GetBitmapBits(
240     HBITMAP hbitmap, /* [in]  Handle to bitmap */
241     LONG count,        /* [in]  Number of bytes to copy */
242     LPVOID bits)       /* [out] Pointer to buffer to receive bits */
243 {
244     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
245     LONG height, ret;
246     
247     if (!bmp) return 0;
248     
249     /* If the bits vector is null, the function should return the read size */
250     if(bits == NULL)
251     {
252         ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
253         goto done;
254     }
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         ret = 0;
269         goto done;
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  done:
299     GDI_ReleaseObj( 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_ReleaseObj( 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_ReleaseObj( 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, bmp );
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_ReleaseObj( 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_ReleaseObj( 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_ReleaseObj( 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_ReleaseObj( 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