If we are looking for an existing pixmap format, we do not need the
[wine.git] / objects / bitmap.c
blob4b8de05e519f90c8a60ffba9e7f0e7d65979aa5c
1 /*
2 * GDI bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 * 1998 Huw D M Davies
6 */
8 #include <stdlib.h>
9 #include <string.h>
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"
21 DEFAULT_DEBUG_CHANNEL(bitmap);
23 BITMAP_DRIVER *BITMAP_Driver = NULL;
26 /***********************************************************************
27 * BITMAP_GetWidthBytes
29 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
30 * data.
32 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
34 switch(bpp)
36 case 1:
37 return 2 * ((bmWidth+15) >> 4);
39 case 24:
40 bmWidth *= 3; /* fall through */
41 case 8:
42 return bmWidth + (bmWidth & 1);
44 case 32:
45 return bmWidth * 4;
47 case 16:
48 case 15:
49 return bmWidth * 2;
51 case 4:
52 return 2 * ((bmWidth+3) >> 2);
54 default:
55 WARN("Unknown depth %d, please report.\n", bpp );
57 return -1;
60 /***********************************************************************
61 * CreateUserBitmap16 (GDI.407)
63 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
64 UINT16 bpp, LPCVOID bits )
66 return CreateBitmap16( width, height, planes, bpp, bits );
69 /***********************************************************************
70 * CreateUserDiscardableBitmap16 (GDI.409)
72 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy,
73 INT16 width, INT16 height )
75 HDC hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
76 HBITMAP16 ret = CreateCompatibleBitmap16( hdc, width, height );
77 DeleteDC( hdc );
78 return ret;
82 /***********************************************************************
83 * CreateBitmap16 (GDI.48)
85 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
86 UINT16 bpp, LPCVOID bits )
88 return CreateBitmap( width, height, planes, bpp, bits );
92 /******************************************************************************
93 * CreateBitmap [GDI32.25] Creates a bitmap with the specified info
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
102 * RETURNS
103 * Success: Handle to bitmap
104 * Failure: 0
106 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
107 UINT bpp, LPCVOID bits )
109 BITMAPOBJ *bmp;
110 HBITMAP hbitmap;
112 planes = (BYTE)planes;
113 bpp = (BYTE)bpp;
116 /* Check parameters */
117 if (!height || !width) return 0;
118 if (planes != 1) {
119 FIXME("planes = %d\n", planes);
120 return 0;
122 if (height < 0) height = -height;
123 if (width < 0) width = -width;
125 /* Create the BITMAPOBJ */
126 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
127 if (!hbitmap) return 0;
129 TRACE("%dx%d, %d colors returning %08x\n", width, height,
130 1 << (planes*bpp), hbitmap);
132 bmp = (BITMAPOBJ *) GDI_HEAP_LOCK( hbitmap );
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;
144 bmp->funcs = NULL;
145 bmp->physBitmap = NULL;
146 bmp->dib = NULL;
148 if (bits) /* Set bitmap bits */
149 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
150 bits );
151 GDI_HEAP_UNLOCK( hbitmap );
152 return hbitmap;
156 /***********************************************************************
157 * CreateCompatibleBitmap16 (GDI.51)
159 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
161 return CreateCompatibleBitmap( hdc, width, height );
165 /******************************************************************************
166 * CreateCompatibleBitmap [GDI32.30] Creates a bitmap compatible with the DC
168 * PARAMS
169 * hdc [I] Handle to device context
170 * width [I] Width of bitmap
171 * height [I] Height of bitmap
173 * RETURNS
174 * Success: Handle to bitmap
175 * Failure: 0
177 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
179 HBITMAP hbmpRet = 0;
180 DC *dc;
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 );
196 TRACE("\t\t%04x\n", hbmpRet);
197 GDI_HEAP_UNLOCK(hdc);
198 return hbmpRet;
202 /***********************************************************************
203 * CreateBitmapIndirect16 (GDI.49)
205 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
207 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
208 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
212 /******************************************************************************
213 * CreateBitmapIndirect [GDI32.26] Creates a bitmap with the specifies info
215 * RETURNS
216 * Success: Handle to bitmap
217 * Failure: NULL
219 HBITMAP WINAPI CreateBitmapIndirect(
220 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
222 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
223 bmp->bmBitsPixel, bmp->bmBits );
227 /***********************************************************************
228 * GetBitmapBits16 (GDI.74)
230 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
232 return GetBitmapBits( hbitmap, count, buffer );
236 /***********************************************************************
237 * GetBitmapBits [GDI32.143] Copies bitmap bits of bitmap to buffer
239 * RETURNS
240 * Success: Number of bytes copied
241 * Failure: 0
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 */
248 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
249 LONG height, ret;
251 if (!bmp) return 0;
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;
257 if (count < 0) {
258 WARN("(%ld): Negative number of bytes passed???\n", count );
259 count = -count;
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)
268 WARN("Less then one entire line requested\n");
269 GDI_HEAP_UNLOCK( hbitmap );
270 return 0;
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 );
278 if(bmp->funcs) {
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;
288 } else {
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;
300 GDI_HEAP_UNLOCK( hbitmap );
301 return ret;
305 /***********************************************************************
306 * SetBitmapBits16 (GDI.106)
308 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
310 return SetBitmapBits( hbitmap, count, buffer );
314 /******************************************************************************
315 * SetBitmapBits [GDI32.303] Sets bits of color data for a bitmap
317 * RETURNS
318 * Success: Number of bytes used in setting the bitmap bits
319 * Failure: 0
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 */
326 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
327 LONG height, ret;
329 if ((!bmp) || (!bits))
330 return 0;
332 if (count < 0) {
333 WARN("(%ld): Negative number of bytes passed???\n", count );
334 count = -count;
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;
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 );
346 if(bmp->funcs) {
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;
356 } else {
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;
369 GDI_HEAP_UNLOCK( hbitmap );
370 return ret;
373 /**********************************************************************
374 * BITMAP_CopyBitmap
377 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
379 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
380 HBITMAP res = 0;
381 BITMAP bm;
383 if(!bmp) return 0;
385 bm = bmp->bitmap;
386 bm.bmBits = NULL;
387 res = CreateBitmapIndirect(&bm);
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 );
397 GDI_HEAP_UNLOCK( hbitmap );
398 return res;
401 /***********************************************************************
402 * BITMAP_DeleteObject
404 BOOL BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
406 if (bmp->funcs && bmp->funcs->pDeleteObject)
407 bmp->funcs->pDeleteObject( hbitmap );
409 if( bmp->bitmap.bmBits )
410 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
412 DIB_DeleteDIBSection( bmp );
414 return GDI_FreeObject( hbitmap );
418 /***********************************************************************
419 * BITMAP_GetObject16
421 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
423 if (bmp->dib)
425 if ( count <= sizeof(BITMAP16) )
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;
439 else
441 FIXME("not implemented for DIBs: count %d\n", count);
442 return 0;
445 else
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;
462 /***********************************************************************
463 * BITMAP_GetObject
465 INT BITMAP_GetObject( BITMAPOBJ * bmp, INT count, LPVOID buffer )
467 if (bmp->dib)
469 if (count < sizeof(DIBSECTION))
471 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
473 else
475 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
478 memcpy( buffer, bmp->dib, count );
479 return count;
481 else
483 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
484 memcpy( buffer, &bmp->bitmap, count );
485 return count;
490 /***********************************************************************
491 * CreateDiscardableBitmap16 (GDI.156)
493 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
494 INT16 height )
496 return CreateCompatibleBitmap16( hdc, width, height );
500 /******************************************************************************
501 * CreateDiscardableBitmap [GDI32.38] Creates a discardable bitmap
503 * RETURNS
504 * Success: Handle to bitmap
505 * Failure: NULL
507 HBITMAP WINAPI CreateDiscardableBitmap(
508 HDC hdc, /* [in] Handle to device context */
509 INT width, /* [in] Bitmap width */
510 INT height) /* [in] Bitmap height */
512 return CreateCompatibleBitmap( hdc, width, height );
516 /***********************************************************************
517 * GetBitmapDimensionEx16 (GDI.468)
519 * NOTES
520 * Can this call GetBitmapDimensionEx?
522 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
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;
532 /******************************************************************************
533 * GetBitmapDimensionEx [GDI32.144] Retrieves dimensions of a bitmap
535 * RETURNS
536 * Success: TRUE
537 * Failure: FALSE
539 BOOL WINAPI GetBitmapDimensionEx(
540 HBITMAP hbitmap, /* [in] Handle to bitmap */
541 LPSIZE size) /* [out] Address of struct receiving dimensions */
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;
551 /***********************************************************************
552 * GetBitmapDimension (GDI.162)
554 DWORD WINAPI GetBitmapDimension16( HBITMAP16 hbitmap )
556 SIZE16 size;
557 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
558 return MAKELONG( size.cx, size.cy );
562 /***********************************************************************
563 * SetBitmapDimensionEx16 (GDI.478)
565 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
566 LPSIZE16 prevSize )
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;
578 /******************************************************************************
579 * SetBitmapDimensionEx [GDI32.304] Assignes dimensions to a bitmap
581 * RETURNS
582 * Success: TRUE
583 * Failure: FALSE
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 */
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;
601 /***********************************************************************
602 * SetBitmapDimension (GDI.163)
604 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
606 SIZE16 size;
607 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
608 return MAKELONG( size.cx, size.cy );