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 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc );
36 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
37 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
38 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj );
40 static const struct gdi_obj_funcs bitmap_funcs =
42 BITMAP_SelectObject, /* pSelectObject */
43 BITMAP_GetObject16, /* pGetObject16 */
44 BITMAP_GetObject, /* pGetObjectA */
45 BITMAP_GetObject, /* pGetObjectW */
46 NULL, /* pUnrealizeObject */
47 BITMAP_DeleteObject /* pDeleteObject */
50 /***********************************************************************
51 * BITMAP_GetWidthBytes
53 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
56 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
61 return 2 * ((bmWidth+15) >> 4);
64 bmWidth *= 3; /* fall through */
66 return bmWidth + (bmWidth & 1);
76 return 2 * ((bmWidth+3) >> 2);
79 WARN("Unknown depth %d, please report.\n", bpp );
84 /***********************************************************************
85 * CreateUserBitmap (GDI.407)
87 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
88 UINT16 bpp, LPCVOID bits )
90 return CreateBitmap16( width, height, planes, bpp, bits );
93 /***********************************************************************
94 * CreateUserDiscardableBitmap (GDI.409)
96 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy,
97 INT16 width, INT16 height )
99 HDC hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
100 HBITMAP16 ret = CreateCompatibleBitmap16( hdc, width, height );
106 /***********************************************************************
107 * CreateBitmap (GDI.48)
109 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
110 UINT16 bpp, LPCVOID bits )
112 return CreateBitmap( width, height, planes, bpp, bits );
116 /******************************************************************************
117 * CreateBitmap [GDI32.@] Creates a bitmap with the specified info
120 * width [I] bitmap width
121 * height [I] bitmap height
122 * planes [I] Number of color planes
123 * bpp [I] Number of bits to identify a color
124 * bits [I] Pointer to array containing color data
127 * Success: Handle to bitmap
130 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
131 UINT bpp, LPCVOID bits )
136 planes = (BYTE)planes;
140 /* Check parameters */
141 if (!height || !width)
149 FIXME("planes = %d\n", planes);
152 if (height < 0) height = -height;
153 if (width < 0) width = -width;
155 /* Create the BITMAPOBJ */
156 if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC, &hbitmap, &bitmap_funcs )))
159 TRACE("%dx%d, %d colors returning %08x\n", width, height,
160 1 << (planes*bpp), hbitmap);
164 bmp->bitmap.bmType = 0;
165 bmp->bitmap.bmWidth = width;
166 bmp->bitmap.bmHeight = height;
167 bmp->bitmap.bmPlanes = planes;
168 bmp->bitmap.bmBitsPixel = bpp;
169 bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
170 bmp->bitmap.bmBits = NULL;
173 bmp->physBitmap = NULL;
175 bmp->segptr_bits = 0;
177 if (bits) /* Set bitmap bits */
178 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
180 GDI_ReleaseObj( hbitmap );
185 /***********************************************************************
186 * CreateCompatibleBitmap (GDI.51)
188 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
190 return CreateCompatibleBitmap( hdc, width, height );
194 /******************************************************************************
195 * CreateCompatibleBitmap [GDI32.@] Creates a bitmap compatible with the DC
198 * hdc [I] Handle to device context
199 * width [I] Width of bitmap
200 * height [I] Height of bitmap
203 * Success: Handle to bitmap
206 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
211 TRACE("(%04x,%d,%d) = \n", hdc, width, height );
212 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
213 if ((width >= 0x10000) || (height >= 0x10000)) {
214 FIXME("got bad width %d or height %d, please look for reason\n",
217 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
218 if (!width || !height)
219 hbmpRet = CreateBitmap( 1, 1, 1, 1, NULL );
221 hbmpRet = CreateBitmap( width, height, 1, dc->bitsPerPixel, NULL );
223 if (!BITMAP_SetOwnerDC( hbmpRet, dc ))
225 DeleteObject( hbmpRet );
229 TRACE("\t\t%04x\n", hbmpRet);
235 /***********************************************************************
236 * CreateBitmapIndirect (GDI.49)
238 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
240 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
241 bmp->bmBitsPixel, MapSL( bmp->bmBits ) );
245 /******************************************************************************
246 * CreateBitmapIndirect [GDI32.@] Creates a bitmap with the specifies info
249 * Success: Handle to bitmap
252 HBITMAP WINAPI CreateBitmapIndirect(
253 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
255 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
256 bmp->bmBitsPixel, bmp->bmBits );
260 /***********************************************************************
261 * GetBitmapBits (GDI.74)
263 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
265 return GetBitmapBits( hbitmap, count, buffer );
269 /***********************************************************************
270 * GetBitmapBits [GDI32.@] Copies bitmap bits of bitmap to buffer
273 * Success: Number of bytes copied
276 LONG WINAPI GetBitmapBits(
277 HBITMAP hbitmap, /* [in] Handle to bitmap */
278 LONG count, /* [in] Number of bytes to copy */
279 LPVOID bits) /* [out] Pointer to buffer to receive bits */
281 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
286 /* If the bits vector is null, the function should return the read size */
289 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
294 WARN("(%ld): Negative number of bytes passed???\n", count );
298 /* Only get entire lines */
299 height = count / bmp->bitmap.bmWidthBytes;
300 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
301 count = height * bmp->bitmap.bmWidthBytes;
304 WARN("Less than one entire line requested\n");
310 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
311 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
312 1 << bmp->bitmap.bmBitsPixel, height );
316 TRACE("Calling device specific BitmapBits\n");
317 if(bmp->funcs->pGetBitmapBits)
318 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
321 memset( bits, 0, count );
326 if(!bmp->bitmap.bmBits) {
327 WARN("Bitmap is empty\n");
330 memcpy(bits, bmp->bitmap.bmBits, count);
336 GDI_ReleaseObj( hbitmap );
341 /***********************************************************************
342 * SetBitmapBits (GDI.106)
344 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
346 return SetBitmapBits( hbitmap, count, buffer );
350 /******************************************************************************
351 * SetBitmapBits [GDI32.@] Sets bits of color data for a bitmap
354 * Success: Number of bytes used in setting the bitmap bits
357 LONG WINAPI SetBitmapBits(
358 HBITMAP hbitmap, /* [in] Handle to bitmap */
359 LONG count, /* [in] Number of bytes in bitmap array */
360 LPCVOID bits) /* [in] Address of array with bitmap bits */
362 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
365 if ((!bmp) || (!bits))
369 WARN("(%ld): Negative number of bytes passed???\n", count );
373 /* Only get entire lines */
374 height = count / bmp->bitmap.bmWidthBytes;
375 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
376 count = height * bmp->bitmap.bmWidthBytes;
378 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
379 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
380 1 << bmp->bitmap.bmBitsPixel, height );
384 TRACE("Calling device specific BitmapBits\n");
385 if(bmp->funcs->pSetBitmapBits)
386 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
391 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
392 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
393 if(!bmp->bitmap.bmBits) {
394 WARN("Unable to allocate bit buffer\n");
397 memcpy(bmp->bitmap.bmBits, bits, count);
402 GDI_ReleaseObj( hbitmap );
406 /**********************************************************************
410 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
412 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
420 res = CreateBitmapIndirect(&bm);
423 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
425 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
426 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
427 HeapFree( GetProcessHeap(), 0, buf );
430 GDI_ReleaseObj( hbitmap );
435 /***********************************************************************
438 * Set the type of DC that owns the bitmap. This is used when the
439 * bitmap is selected into a device to initialize the bitmap function
442 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
447 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
448 if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
450 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
453 if (!bitmap->funcs) /* not owned by a DC yet */
455 if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap );
456 if (ret) bitmap->funcs = dc->funcs;
458 else if (bitmap->funcs != dc->funcs)
460 FIXME( "Trying to select bitmap %x in different DC type\n", hbitmap );
463 GDI_ReleaseObj( hbitmap );
468 /***********************************************************************
469 * BITMAP_SelectObject
471 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
473 HGDIOBJ ret = handle;
474 BITMAPOBJ *bitmap = obj;
475 DC *dc = DC_GetDCPtr( hdc );
478 if (!(dc->flags & DC_MEMORY))
480 GDI_ReleaseObj( hdc );
483 if (handle == dc->hBitmap) goto done; /* nothing to do */
485 if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
487 WARN( "Bitmap already selected in another DC\n" );
488 GDI_ReleaseObj( hdc );
492 if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
494 GDI_ReleaseObj( hdc );
498 if (dc->funcs->pSelectBitmap) ret = dc->funcs->pSelectBitmap( dc->physDev, handle );
503 dc->totalExtent.left = 0;
504 dc->totalExtent.top = 0;
505 dc->totalExtent.right = bitmap->bitmap.bmWidth;
506 dc->totalExtent.bottom = bitmap->bitmap.bmHeight;
507 dc->flags &= ~DC_DIRTY;
508 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
509 CLIPPING_UpdateGCRegion( dc );
511 if (dc->bitsPerPixel != bitmap->bitmap.bmBitsPixel)
513 /* depth changed, reinitialize the DC */
514 dc->bitsPerPixel = bitmap->bitmap.bmBitsPixel;
519 GDI_ReleaseObj( hdc );
524 /***********************************************************************
525 * BITMAP_DeleteObject
527 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
529 BITMAPOBJ * bmp = obj;
531 if (bmp->funcs && bmp->funcs->pDeleteBitmap)
532 bmp->funcs->pDeleteBitmap( handle );
534 if( bmp->bitmap.bmBits )
535 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
539 DIBSECTION *dib = bmp->dib;
541 if (dib->dsBm.bmBits)
545 SYSTEM_INFO SystemInfo;
546 GetSystemInfo( &SystemInfo );
547 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
548 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
550 else if (!dib->dsOffset)
551 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
553 HeapFree(GetProcessHeap(), 0, dib);
555 if (bmp->segptr_bits)
556 { /* free its selector array */
557 WORD sel = SELECTOROF(bmp->segptr_bits);
558 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
561 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
564 return GDI_FreeObject( handle, obj );
568 /***********************************************************************
571 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
573 BITMAPOBJ *bmp = obj;
577 if ( count <= sizeof(BITMAP16) )
579 BITMAP *bmp32 = &bmp->dib->dsBm;
581 bmp16.bmType = bmp32->bmType;
582 bmp16.bmWidth = bmp32->bmWidth;
583 bmp16.bmHeight = bmp32->bmHeight;
584 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
585 bmp16.bmPlanes = bmp32->bmPlanes;
586 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
587 bmp16.bmBits = (SEGPTR)0;
588 memcpy( buffer, &bmp16, count );
593 FIXME("not implemented for DIBs: count %d\n", count);
600 bmp16.bmType = bmp->bitmap.bmType;
601 bmp16.bmWidth = bmp->bitmap.bmWidth;
602 bmp16.bmHeight = bmp->bitmap.bmHeight;
603 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
604 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
605 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
606 bmp16.bmBits = (SEGPTR)0;
607 if (count > sizeof(bmp16)) count = sizeof(bmp16);
608 memcpy( buffer, &bmp16, count );
614 /***********************************************************************
617 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
619 BITMAPOBJ *bmp = obj;
623 if (count < sizeof(DIBSECTION))
625 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
629 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
632 memcpy( buffer, bmp->dib, count );
637 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
638 memcpy( buffer, &bmp->bitmap, count );
644 /***********************************************************************
645 * CreateDiscardableBitmap (GDI.156)
647 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
650 return CreateCompatibleBitmap16( hdc, width, height );
654 /******************************************************************************
655 * CreateDiscardableBitmap [GDI32.@] Creates a discardable bitmap
658 * Success: Handle to bitmap
661 HBITMAP WINAPI CreateDiscardableBitmap(
662 HDC hdc, /* [in] Handle to device context */
663 INT width, /* [in] Bitmap width */
664 INT height) /* [in] Bitmap height */
666 return CreateCompatibleBitmap( hdc, width, height );
670 /***********************************************************************
671 * GetBitmapDimensionEx (GDI.468)
674 * Can this call GetBitmapDimensionEx?
676 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
678 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
679 if (!bmp) return FALSE;
680 size->cx = bmp->size.cx;
681 size->cy = bmp->size.cy;
682 GDI_ReleaseObj( hbitmap );
687 /******************************************************************************
688 * GetBitmapDimensionEx [GDI32.@] Retrieves dimensions of a bitmap
694 BOOL WINAPI GetBitmapDimensionEx(
695 HBITMAP hbitmap, /* [in] Handle to bitmap */
696 LPSIZE size) /* [out] Address of struct receiving dimensions */
698 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
699 if (!bmp) return FALSE;
701 GDI_ReleaseObj( hbitmap );
706 /***********************************************************************
707 * GetBitmapDimension (GDI.162)
709 DWORD WINAPI GetBitmapDimension16( HBITMAP16 hbitmap )
712 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
713 return MAKELONG( size.cx, size.cy );
717 /***********************************************************************
718 * SetBitmapDimensionEx (GDI.478)
720 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
723 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
724 if (!bmp) return FALSE;
727 prevSize->cx = bmp->size.cx;
728 prevSize->cy = bmp->size.cy;
732 GDI_ReleaseObj( hbitmap );
737 /******************************************************************************
738 * SetBitmapDimensionEx [GDI32.@] Assignes dimensions to a bitmap
744 BOOL WINAPI SetBitmapDimensionEx(
745 HBITMAP hbitmap, /* [in] Handle to bitmap */
746 INT x, /* [in] Bitmap width */
747 INT y, /* [in] Bitmap height */
748 LPSIZE prevSize) /* [out] Address of structure for orig dims */
750 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
751 if (!bmp) return FALSE;
752 if (prevSize) *prevSize = bmp->size;
755 GDI_ReleaseObj( hbitmap );
760 /***********************************************************************
761 * SetBitmapDimension (GDI.163)
763 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
766 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
767 return MAKELONG( size.cx, size.cy );