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 "wine/debug.h"
29 #include "wine/winuser16.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
33 BITMAP_DRIVER *BITMAP_Driver = NULL;
36 /***********************************************************************
37 * BITMAP_GetWidthBytes
39 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
42 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
47 return 2 * ((bmWidth+15) >> 4);
50 bmWidth *= 3; /* fall through */
52 return bmWidth + (bmWidth & 1);
62 return 2 * ((bmWidth+3) >> 2);
65 WARN("Unknown depth %d, please report.\n", bpp );
70 /***********************************************************************
71 * CreateUserBitmap (GDI.407)
73 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
74 UINT16 bpp, LPCVOID bits )
76 return CreateBitmap16( width, height, planes, bpp, bits );
79 /***********************************************************************
80 * CreateUserDiscardableBitmap (GDI.409)
82 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy,
83 INT16 width, INT16 height )
85 HDC hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
86 HBITMAP16 ret = CreateCompatibleBitmap16( hdc, width, height );
92 /***********************************************************************
93 * CreateBitmap (GDI.48)
95 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
96 UINT16 bpp, LPCVOID bits )
98 return CreateBitmap( width, height, planes, bpp, bits );
102 /******************************************************************************
103 * CreateBitmap [GDI32.@] Creates a bitmap with the specified info
106 * width [I] bitmap width
107 * height [I] bitmap height
108 * planes [I] Number of color planes
109 * bpp [I] Number of bits to identify a color
110 * bits [I] Pointer to array containing color data
113 * Success: Handle to bitmap
116 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
117 UINT bpp, LPCVOID bits )
122 planes = (BYTE)planes;
126 /* Check parameters */
127 if (!height || !width)
135 FIXME("planes = %d\n", planes);
138 if (height < 0) height = -height;
139 if (width < 0) width = -width;
141 /* Create the BITMAPOBJ */
142 if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC, &hbitmap ))) return 0;
144 TRACE("%dx%d, %d colors returning %08x\n", width, height,
145 1 << (planes*bpp), hbitmap);
149 bmp->bitmap.bmType = 0;
150 bmp->bitmap.bmWidth = width;
151 bmp->bitmap.bmHeight = height;
152 bmp->bitmap.bmPlanes = planes;
153 bmp->bitmap.bmBitsPixel = bpp;
154 bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
155 bmp->bitmap.bmBits = NULL;
158 bmp->physBitmap = NULL;
160 bmp->segptr_bits = 0;
162 if (bits) /* Set bitmap bits */
163 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
165 GDI_ReleaseObj( hbitmap );
170 /***********************************************************************
171 * CreateCompatibleBitmap (GDI.51)
173 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
175 return CreateCompatibleBitmap( hdc, width, height );
179 /******************************************************************************
180 * CreateCompatibleBitmap [GDI32.@] Creates a bitmap compatible with the DC
183 * hdc [I] Handle to device context
184 * width [I] Width of bitmap
185 * height [I] Height of bitmap
188 * Success: Handle to bitmap
191 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
196 TRACE("(%04x,%d,%d) = \n", hdc, width, height );
197 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
198 if ((width >= 0x10000) || (height >= 0x10000)) {
199 FIXME("got bad width %d or height %d, please look for reason\n",
202 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
203 if (!width || !height)
204 hbmpRet = CreateBitmap( 1, 1, 1, 1, NULL );
206 hbmpRet = CreateBitmap( width, height, 1, dc->bitsPerPixel, NULL );
207 if(dc->funcs->pCreateBitmap)
208 dc->funcs->pCreateBitmap( hbmpRet );
210 TRACE("\t\t%04x\n", hbmpRet);
216 /***********************************************************************
217 * CreateBitmapIndirect (GDI.49)
219 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
221 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
222 bmp->bmBitsPixel, MapSL( bmp->bmBits ) );
226 /******************************************************************************
227 * CreateBitmapIndirect [GDI32.@] Creates a bitmap with the specifies info
230 * Success: Handle to bitmap
233 HBITMAP WINAPI CreateBitmapIndirect(
234 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
236 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
237 bmp->bmBitsPixel, bmp->bmBits );
241 /***********************************************************************
242 * GetBitmapBits (GDI.74)
244 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
246 return GetBitmapBits( hbitmap, count, buffer );
250 /***********************************************************************
251 * GetBitmapBits [GDI32.@] Copies bitmap bits of bitmap to buffer
254 * Success: Number of bytes copied
257 LONG WINAPI GetBitmapBits(
258 HBITMAP hbitmap, /* [in] Handle to bitmap */
259 LONG count, /* [in] Number of bytes to copy */
260 LPVOID bits) /* [out] Pointer to buffer to receive bits */
262 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
267 /* If the bits vector is null, the function should return the read size */
270 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
275 WARN("(%ld): Negative number of bytes passed???\n", count );
279 /* Only get entire lines */
280 height = count / bmp->bitmap.bmWidthBytes;
281 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
282 count = height * bmp->bitmap.bmWidthBytes;
285 WARN("Less than one entire line requested\n");
291 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
292 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
293 1 << bmp->bitmap.bmBitsPixel, height );
297 TRACE("Calling device specific BitmapBits\n");
298 if(bmp->funcs->pBitmapBits)
299 ret = bmp->funcs->pBitmapBits(hbitmap, bits, count, DDB_GET);
301 ERR("BitmapBits == NULL??\n");
307 if(!bmp->bitmap.bmBits) {
308 WARN("Bitmap is empty\n");
311 memcpy(bits, bmp->bitmap.bmBits, count);
317 GDI_ReleaseObj( hbitmap );
322 /***********************************************************************
323 * SetBitmapBits (GDI.106)
325 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
327 return SetBitmapBits( hbitmap, count, buffer );
331 /******************************************************************************
332 * SetBitmapBits [GDI32.@] Sets bits of color data for a bitmap
335 * Success: Number of bytes used in setting the bitmap bits
338 LONG WINAPI SetBitmapBits(
339 HBITMAP hbitmap, /* [in] Handle to bitmap */
340 LONG count, /* [in] Number of bytes in bitmap array */
341 LPCVOID bits) /* [in] Address of array with bitmap bits */
343 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
346 if ((!bmp) || (!bits))
350 WARN("(%ld): Negative number of bytes passed???\n", count );
354 /* Only get entire lines */
355 height = count / bmp->bitmap.bmWidthBytes;
356 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
357 count = height * bmp->bitmap.bmWidthBytes;
359 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
360 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
361 1 << bmp->bitmap.bmBitsPixel, height );
365 TRACE("Calling device specific BitmapBits\n");
366 if(bmp->funcs->pBitmapBits)
367 ret = bmp->funcs->pBitmapBits(hbitmap, (void *) bits, count, DDB_SET);
369 ERR("BitmapBits == NULL??\n");
375 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
376 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
377 if(!bmp->bitmap.bmBits) {
378 WARN("Unable to allocate bit buffer\n");
381 memcpy(bmp->bitmap.bmBits, bits, count);
386 GDI_ReleaseObj( hbitmap );
390 /**********************************************************************
394 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
396 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
404 res = CreateBitmapIndirect(&bm);
407 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
409 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
410 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
411 HeapFree( GetProcessHeap(), 0, buf );
414 GDI_ReleaseObj( hbitmap );
418 /***********************************************************************
419 * BITMAP_DeleteObject
421 BOOL BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
423 if (bmp->funcs && bmp->funcs->pDeleteObject)
424 bmp->funcs->pDeleteObject( hbitmap );
426 if( bmp->bitmap.bmBits )
427 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
429 DIB_DeleteDIBSection( bmp );
431 return GDI_FreeObject( hbitmap, bmp );
435 /***********************************************************************
438 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
442 if ( count <= sizeof(BITMAP16) )
444 BITMAP *bmp32 = &bmp->dib->dsBm;
446 bmp16.bmType = bmp32->bmType;
447 bmp16.bmWidth = bmp32->bmWidth;
448 bmp16.bmHeight = bmp32->bmHeight;
449 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
450 bmp16.bmPlanes = bmp32->bmPlanes;
451 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
452 bmp16.bmBits = (SEGPTR)0;
453 memcpy( buffer, &bmp16, count );
458 FIXME("not implemented for DIBs: count %d\n", count);
465 bmp16.bmType = bmp->bitmap.bmType;
466 bmp16.bmWidth = bmp->bitmap.bmWidth;
467 bmp16.bmHeight = bmp->bitmap.bmHeight;
468 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
469 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
470 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
471 bmp16.bmBits = (SEGPTR)0;
472 if (count > sizeof(bmp16)) count = sizeof(bmp16);
473 memcpy( buffer, &bmp16, count );
479 /***********************************************************************
482 INT BITMAP_GetObject( BITMAPOBJ * bmp, INT count, LPVOID buffer )
486 if (count < sizeof(DIBSECTION))
488 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
492 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
495 memcpy( buffer, bmp->dib, count );
500 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
501 memcpy( buffer, &bmp->bitmap, count );
507 /***********************************************************************
508 * CreateDiscardableBitmap (GDI.156)
510 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
513 return CreateCompatibleBitmap16( hdc, width, height );
517 /******************************************************************************
518 * CreateDiscardableBitmap [GDI32.@] Creates a discardable bitmap
521 * Success: Handle to bitmap
524 HBITMAP WINAPI CreateDiscardableBitmap(
525 HDC hdc, /* [in] Handle to device context */
526 INT width, /* [in] Bitmap width */
527 INT height) /* [in] Bitmap height */
529 return CreateCompatibleBitmap( hdc, width, height );
533 /***********************************************************************
534 * GetBitmapDimensionEx (GDI.468)
537 * Can this call GetBitmapDimensionEx?
539 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
541 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
542 if (!bmp) return FALSE;
543 size->cx = bmp->size.cx;
544 size->cy = bmp->size.cy;
545 GDI_ReleaseObj( hbitmap );
550 /******************************************************************************
551 * GetBitmapDimensionEx [GDI32.@] Retrieves dimensions of a bitmap
557 BOOL WINAPI GetBitmapDimensionEx(
558 HBITMAP hbitmap, /* [in] Handle to bitmap */
559 LPSIZE size) /* [out] Address of struct receiving dimensions */
561 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
562 if (!bmp) return FALSE;
564 GDI_ReleaseObj( hbitmap );
569 /***********************************************************************
570 * GetBitmapDimension (GDI.162)
572 DWORD WINAPI GetBitmapDimension16( HBITMAP16 hbitmap )
575 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
576 return MAKELONG( size.cx, size.cy );
580 /***********************************************************************
581 * SetBitmapDimensionEx (GDI.478)
583 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
586 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
587 if (!bmp) return FALSE;
590 prevSize->cx = bmp->size.cx;
591 prevSize->cy = bmp->size.cy;
595 GDI_ReleaseObj( hbitmap );
600 /******************************************************************************
601 * SetBitmapDimensionEx [GDI32.@] Assignes dimensions to a bitmap
607 BOOL WINAPI SetBitmapDimensionEx(
608 HBITMAP hbitmap, /* [in] Handle to bitmap */
609 INT x, /* [in] Bitmap width */
610 INT y, /* [in] Bitmap height */
611 LPSIZE prevSize) /* [out] Address of structure for orig dims */
613 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
614 if (!bmp) return FALSE;
615 if (prevSize) *prevSize = bmp->size;
618 GDI_ReleaseObj( hbitmap );
623 /***********************************************************************
624 * SetBitmapDimension (GDI.163)
626 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
629 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
630 return MAKELONG( size.cx, size.cy );