4 * Copyright 1993 Alexandre Julliard
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.
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.
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
25 #include "wine/winbase16.h"
28 #include "selectors.h"
29 #include "wine/debug.h"
30 #include "wine/winuser16.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
35 /***********************************************************************
36 * BITMAP_GetWidthBytes
38 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
41 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
46 return 2 * ((bmWidth+15) >> 4);
49 bmWidth *= 3; /* fall through */
51 return bmWidth + (bmWidth & 1);
61 return 2 * ((bmWidth+3) >> 2);
64 WARN("Unknown depth %d, please report.\n", bpp );
69 /***********************************************************************
70 * CreateUserBitmap (GDI.407)
72 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
73 UINT16 bpp, LPCVOID bits )
75 return CreateBitmap16( width, height, planes, bpp, bits );
78 /***********************************************************************
79 * CreateUserDiscardableBitmap (GDI.409)
81 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy,
82 INT16 width, INT16 height )
84 HDC hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
85 HBITMAP16 ret = CreateCompatibleBitmap16( hdc, width, height );
91 /***********************************************************************
92 * CreateBitmap (GDI.48)
94 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
95 UINT16 bpp, LPCVOID bits )
97 return CreateBitmap( width, height, planes, bpp, bits );
101 /******************************************************************************
102 * CreateBitmap [GDI32.@] Creates a bitmap with the specified info
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
112 * Success: Handle to bitmap
115 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
116 UINT bpp, LPCVOID bits )
121 planes = (BYTE)planes;
125 /* Check parameters */
126 if (!height || !width)
134 FIXME("planes = %d\n", planes);
137 if (height < 0) height = -height;
138 if (width < 0) width = -width;
140 /* Create the BITMAPOBJ */
141 if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC, &hbitmap ))) return 0;
143 TRACE("%dx%d, %d colors returning %08x\n", width, height,
144 1 << (planes*bpp), hbitmap);
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;
157 bmp->physBitmap = NULL;
159 bmp->segptr_bits = 0;
161 if (bits) /* Set bitmap bits */
162 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
164 GDI_ReleaseObj( hbitmap );
169 /***********************************************************************
170 * CreateCompatibleBitmap (GDI.51)
172 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
174 return CreateCompatibleBitmap( hdc, width, height );
178 /******************************************************************************
179 * CreateCompatibleBitmap [GDI32.@] Creates a bitmap compatible with the DC
182 * hdc [I] Handle to device context
183 * width [I] Width of bitmap
184 * height [I] Height of bitmap
187 * Success: Handle to bitmap
190 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
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",
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 );
205 hbmpRet = CreateBitmap( width, height, 1, dc->bitsPerPixel, NULL );
206 if(dc->funcs->pCreateBitmap)
207 dc->funcs->pCreateBitmap( hbmpRet );
209 TRACE("\t\t%04x\n", hbmpRet);
215 /***********************************************************************
216 * CreateBitmapIndirect (GDI.49)
218 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
220 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
221 bmp->bmBitsPixel, MapSL( bmp->bmBits ) );
225 /******************************************************************************
226 * CreateBitmapIndirect [GDI32.@] Creates a bitmap with the specifies info
229 * Success: Handle to bitmap
232 HBITMAP WINAPI CreateBitmapIndirect(
233 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
235 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
236 bmp->bmBitsPixel, bmp->bmBits );
240 /***********************************************************************
241 * GetBitmapBits (GDI.74)
243 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
245 return GetBitmapBits( hbitmap, count, buffer );
249 /***********************************************************************
250 * GetBitmapBits [GDI32.@] Copies bitmap bits of bitmap to buffer
253 * Success: Number of bytes copied
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 */
261 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
266 /* If the bits vector is null, the function should return the read size */
269 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
274 WARN("(%ld): Negative number of bytes passed???\n", count );
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;
284 WARN("Less than one entire line requested\n");
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 );
296 TRACE("Calling device specific BitmapBits\n");
297 if(bmp->funcs->pBitmapBits)
298 ret = bmp->funcs->pBitmapBits(hbitmap, bits, count, DDB_GET);
300 ERR("BitmapBits == NULL??\n");
306 if(!bmp->bitmap.bmBits) {
307 WARN("Bitmap is empty\n");
310 memcpy(bits, bmp->bitmap.bmBits, count);
316 GDI_ReleaseObj( hbitmap );
321 /***********************************************************************
322 * SetBitmapBits (GDI.106)
324 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
326 return SetBitmapBits( hbitmap, count, buffer );
330 /******************************************************************************
331 * SetBitmapBits [GDI32.@] Sets bits of color data for a bitmap
334 * Success: Number of bytes used in setting the bitmap bits
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 */
342 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
345 if ((!bmp) || (!bits))
349 WARN("(%ld): Negative number of bytes passed???\n", count );
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;
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 );
364 TRACE("Calling device specific BitmapBits\n");
365 if(bmp->funcs->pBitmapBits)
366 ret = bmp->funcs->pBitmapBits(hbitmap, (void *) bits, count, DDB_SET);
368 ERR("BitmapBits == NULL??\n");
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");
380 memcpy(bmp->bitmap.bmBits, bits, count);
385 GDI_ReleaseObj( hbitmap );
389 /**********************************************************************
393 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
395 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
403 res = CreateBitmapIndirect(&bm);
406 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
408 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
409 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
410 HeapFree( GetProcessHeap(), 0, buf );
413 GDI_ReleaseObj( hbitmap );
417 /***********************************************************************
418 * BITMAP_DeleteObject
420 BOOL BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
422 if (bmp->funcs && bmp->funcs->pDeleteObject)
423 bmp->funcs->pDeleteObject( hbitmap );
425 if( bmp->bitmap.bmBits )
426 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
430 DIBSECTION *dib = bmp->dib;
432 if (dib->dsBm.bmBits)
436 SYSTEM_INFO SystemInfo;
437 GetSystemInfo( &SystemInfo );
438 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
439 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
441 else if (!dib->dsOffset)
442 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
444 HeapFree(GetProcessHeap(), 0, dib);
446 if (bmp->segptr_bits) SELECTOR_FreeBlock( SELECTOROF(bmp->segptr_bits) );
448 return GDI_FreeObject( hbitmap, bmp );
452 /***********************************************************************
455 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
459 if ( count <= sizeof(BITMAP16) )
461 BITMAP *bmp32 = &bmp->dib->dsBm;
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 );
475 FIXME("not implemented for DIBs: count %d\n", count);
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 );
496 /***********************************************************************
499 INT BITMAP_GetObject( BITMAPOBJ * bmp, INT count, LPVOID buffer )
503 if (count < sizeof(DIBSECTION))
505 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
509 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
512 memcpy( buffer, bmp->dib, count );
517 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
518 memcpy( buffer, &bmp->bitmap, count );
524 /***********************************************************************
525 * CreateDiscardableBitmap (GDI.156)
527 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
530 return CreateCompatibleBitmap16( hdc, width, height );
534 /******************************************************************************
535 * CreateDiscardableBitmap [GDI32.@] Creates a discardable bitmap
538 * Success: Handle to bitmap
541 HBITMAP WINAPI CreateDiscardableBitmap(
542 HDC hdc, /* [in] Handle to device context */
543 INT width, /* [in] Bitmap width */
544 INT height) /* [in] Bitmap height */
546 return CreateCompatibleBitmap( hdc, width, height );
550 /***********************************************************************
551 * GetBitmapDimensionEx (GDI.468)
554 * Can this call GetBitmapDimensionEx?
556 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
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 );
567 /******************************************************************************
568 * GetBitmapDimensionEx [GDI32.@] Retrieves dimensions of a bitmap
574 BOOL WINAPI GetBitmapDimensionEx(
575 HBITMAP hbitmap, /* [in] Handle to bitmap */
576 LPSIZE size) /* [out] Address of struct receiving dimensions */
578 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
579 if (!bmp) return FALSE;
581 GDI_ReleaseObj( hbitmap );
586 /***********************************************************************
587 * GetBitmapDimension (GDI.162)
589 DWORD WINAPI GetBitmapDimension16( HBITMAP16 hbitmap )
592 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
593 return MAKELONG( size.cx, size.cy );
597 /***********************************************************************
598 * SetBitmapDimensionEx (GDI.478)
600 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
603 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
604 if (!bmp) return FALSE;
607 prevSize->cx = bmp->size.cx;
608 prevSize->cy = bmp->size.cy;
612 GDI_ReleaseObj( hbitmap );
617 /******************************************************************************
618 * SetBitmapDimensionEx [GDI32.@] Assignes dimensions to a bitmap
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 */
630 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
631 if (!bmp) return FALSE;
632 if (prevSize) *prevSize = bmp->size;
635 GDI_ReleaseObj( hbitmap );
640 /***********************************************************************
641 * SetBitmapDimension (GDI.163)
643 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
646 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
647 return MAKELONG( size.cx, size.cy );