4 * Copyright 1993 Alexandre Julliard
11 #include "wine/winbase16.h"
17 #include "debugtools.h"
18 #include "wine/winuser16.h"
20 DEFAULT_DEBUG_CHANNEL(bitmap);
22 BITMAP_DRIVER *BITMAP_Driver = NULL;
25 /***********************************************************************
26 * BITMAP_GetWidthBytes
28 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
31 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
36 return 2 * ((bmWidth+15) >> 4);
39 bmWidth *= 3; /* fall through */
41 return bmWidth + (bmWidth & 1);
51 return 2 * ((bmWidth+3) >> 2);
54 WARN("Unknown depth %d, please report.\n", bpp );
59 /***********************************************************************
60 * CreateUserBitmap16 (GDI.407)
62 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
63 UINT16 bpp, LPCVOID bits )
65 return CreateBitmap16( width, height, planes, bpp, bits );
68 /***********************************************************************
69 * CreateUserDiscardableBitmap16 (GDI.409)
71 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy,
72 INT16 width, INT16 height )
74 HDC hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
75 HBITMAP16 ret = CreateCompatibleBitmap16( hdc, width, height );
81 /***********************************************************************
82 * CreateBitmap16 (GDI.48)
84 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
85 UINT16 bpp, LPCVOID bits )
87 return CreateBitmap( width, height, planes, bpp, bits );
91 /******************************************************************************
92 * CreateBitmap [GDI32.25] Creates a bitmap with the specified info
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
102 * Success: Handle to bitmap
105 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
106 UINT bpp, LPCVOID bits )
111 planes = (BYTE)planes;
115 /* Check parameters */
116 if (!height || !width) return 0;
118 FIXME("planes = %d\n", planes);
121 if (height < 0) height = -height;
122 if (width < 0) width = -width;
124 /* Create the BITMAPOBJ */
125 if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC, &hbitmap ))) return 0;
127 TRACE("%dx%d, %d colors returning %08x\n", width, height,
128 1 << (planes*bpp), hbitmap);
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;
141 bmp->physBitmap = NULL;
144 if (bits) /* Set bitmap bits */
145 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
147 GDI_ReleaseObj( hbitmap );
152 /***********************************************************************
153 * CreateCompatibleBitmap16 (GDI.51)
155 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
157 return CreateCompatibleBitmap( hdc, width, height );
161 /******************************************************************************
162 * CreateCompatibleBitmap [GDI32.30] Creates a bitmap compatible with the DC
165 * hdc [I] Handle to device context
166 * width [I] Width of bitmap
167 * height [I] Height of bitmap
170 * Success: Handle to bitmap
173 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
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",
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 );
188 hbmpRet = CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
189 if(dc->funcs->pCreateBitmap)
190 dc->funcs->pCreateBitmap( hbmpRet );
192 TRACE("\t\t%04x\n", hbmpRet);
198 /***********************************************************************
199 * CreateBitmapIndirect16 (GDI.49)
201 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
203 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
204 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
208 /******************************************************************************
209 * CreateBitmapIndirect [GDI32.26] Creates a bitmap with the specifies info
212 * Success: Handle to bitmap
215 HBITMAP WINAPI CreateBitmapIndirect(
216 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
218 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
219 bmp->bmBitsPixel, bmp->bmBits );
223 /***********************************************************************
224 * GetBitmapBits16 (GDI.74)
226 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
228 return GetBitmapBits( hbitmap, count, buffer );
232 /***********************************************************************
233 * GetBitmapBits [GDI32.143] Copies bitmap bits of bitmap to buffer
236 * Success: Number of bytes copied
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 */
244 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
249 /* If the bits vector is null, the function should return the read size */
252 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
257 WARN("(%ld): Negative number of bytes passed???\n", count );
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;
267 WARN("Less then one entire line requested\n");
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 );
279 TRACE("Calling device specific BitmapBits\n");
280 if(bmp->funcs->pBitmapBits)
281 ret = bmp->funcs->pBitmapBits(hbitmap, bits, count, DDB_GET);
283 ERR("BitmapBits == NULL??\n");
289 if(!bmp->bitmap.bmBits) {
290 WARN("Bitmap is empty\n");
293 memcpy(bits, bmp->bitmap.bmBits, count);
299 GDI_ReleaseObj( hbitmap );
304 /***********************************************************************
305 * SetBitmapBits16 (GDI.106)
307 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
309 return SetBitmapBits( hbitmap, count, buffer );
313 /******************************************************************************
314 * SetBitmapBits [GDI32.303] Sets bits of color data for a bitmap
317 * Success: Number of bytes used in setting the bitmap bits
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 */
325 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
328 if ((!bmp) || (!bits))
332 WARN("(%ld): Negative number of bytes passed???\n", count );
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;
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 );
347 TRACE("Calling device specific BitmapBits\n");
348 if(bmp->funcs->pBitmapBits)
349 ret = bmp->funcs->pBitmapBits(hbitmap, (void *) bits, count, DDB_SET);
351 ERR("BitmapBits == NULL??\n");
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");
363 memcpy(bmp->bitmap.bmBits, bits, count);
368 GDI_ReleaseObj( hbitmap );
372 /**********************************************************************
376 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
378 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
386 res = CreateBitmapIndirect(&bm);
389 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
391 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
392 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
393 HeapFree( GetProcessHeap(), 0, buf );
396 GDI_ReleaseObj( hbitmap );
400 /***********************************************************************
401 * BITMAP_DeleteObject
403 BOOL BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
405 if (bmp->funcs && bmp->funcs->pDeleteObject)
406 bmp->funcs->pDeleteObject( hbitmap );
408 if( bmp->bitmap.bmBits )
409 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
411 DIB_DeleteDIBSection( bmp );
413 return GDI_FreeObject( hbitmap, bmp );
417 /***********************************************************************
420 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
424 if ( count <= sizeof(BITMAP16) )
426 BITMAP *bmp32 = &bmp->dib->dsBm;
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 );
440 FIXME("not implemented for DIBs: count %d\n", count);
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 );
461 /***********************************************************************
464 INT BITMAP_GetObject( BITMAPOBJ * bmp, INT count, LPVOID buffer )
468 if (count < sizeof(DIBSECTION))
470 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
474 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
477 memcpy( buffer, bmp->dib, count );
482 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
483 memcpy( buffer, &bmp->bitmap, count );
489 /***********************************************************************
490 * CreateDiscardableBitmap16 (GDI.156)
492 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
495 return CreateCompatibleBitmap16( hdc, width, height );
499 /******************************************************************************
500 * CreateDiscardableBitmap [GDI32.38] Creates a discardable bitmap
503 * Success: Handle to bitmap
506 HBITMAP WINAPI CreateDiscardableBitmap(
507 HDC hdc, /* [in] Handle to device context */
508 INT width, /* [in] Bitmap width */
509 INT height) /* [in] Bitmap height */
511 return CreateCompatibleBitmap( hdc, width, height );
515 /***********************************************************************
516 * GetBitmapDimensionEx16 (GDI.468)
519 * Can this call GetBitmapDimensionEx?
521 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
523 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
524 if (!bmp) return FALSE;
525 CONV_SIZE32TO16( &bmp->size, size );
526 GDI_ReleaseObj( hbitmap );
531 /******************************************************************************
532 * GetBitmapDimensionEx [GDI32.144] Retrieves dimensions of a bitmap
538 BOOL WINAPI GetBitmapDimensionEx(
539 HBITMAP hbitmap, /* [in] Handle to bitmap */
540 LPSIZE size) /* [out] Address of struct receiving dimensions */
542 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
543 if (!bmp) return FALSE;
545 GDI_ReleaseObj( hbitmap );
550 /***********************************************************************
551 * GetBitmapDimension (GDI.162)
553 DWORD WINAPI GetBitmapDimension16( HBITMAP16 hbitmap )
556 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
557 return MAKELONG( size.cx, size.cy );
561 /***********************************************************************
562 * SetBitmapDimensionEx16 (GDI.478)
564 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
567 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
568 if (!bmp) return FALSE;
569 if (prevSize) CONV_SIZE32TO16( &bmp->size, prevSize );
572 GDI_ReleaseObj( hbitmap );
577 /******************************************************************************
578 * SetBitmapDimensionEx [GDI32.304] Assignes dimensions to a bitmap
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 */
590 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
591 if (!bmp) return FALSE;
592 if (prevSize) *prevSize = bmp->size;
595 GDI_ReleaseObj( hbitmap );
600 /***********************************************************************
601 * SetBitmapDimension (GDI.163)
603 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
606 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
607 return MAKELONG( size.cx, size.cy );