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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/winbase16.h"
30 #include "gdi_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
36 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, HDC hdc );
37 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
38 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
39 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj );
41 static const struct gdi_obj_funcs bitmap_funcs =
43 BITMAP_SelectObject, /* pSelectObject */
44 BITMAP_GetObject16, /* pGetObject16 */
45 BITMAP_GetObject, /* pGetObjectA */
46 BITMAP_GetObject, /* pGetObjectW */
47 NULL, /* pUnrealizeObject */
48 BITMAP_DeleteObject /* pDeleteObject */
51 /***********************************************************************
52 * BITMAP_GetWidthBytes
54 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
57 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
62 return 2 * ((bmWidth+15) >> 4);
65 bmWidth *= 3; /* fall through */
67 return bmWidth + (bmWidth & 1);
77 return 2 * ((bmWidth+3) >> 2);
80 WARN("Unknown depth %d, please report.\n", bpp );
86 /******************************************************************************
87 * CreateBitmap [GDI32.@]
89 * Creates a bitmap with the specified info.
92 * width [I] bitmap width
93 * height [I] bitmap height
94 * planes [I] Number of color planes
95 * bpp [I] Number of bits to identify a color
96 * bits [I] Pointer to array containing color data
99 * Success: Handle to bitmap
102 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
103 UINT bpp, LPCVOID bits )
109 bm.bmHeight = height;
110 bm.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
111 bm.bmPlanes = planes;
112 bm.bmBitsPixel = bpp;
113 bm.bmBits = (LPVOID)bits;
115 return CreateBitmapIndirect( &bm );
118 /******************************************************************************
119 * CreateCompatibleBitmap [GDI32.@]
121 * Creates a bitmap compatible with the DC.
124 * hdc [I] Handle to device context
125 * width [I] Width of bitmap
126 * height [I] Height of bitmap
129 * Success: Handle to bitmap
132 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
137 TRACE("(%p,%d,%d) =\n", hdc, width, height);
139 if (!(dc = DC_GetDCPtr(hdc))) return 0;
141 if (GDIMAGIC( dc->header.wMagic ) != MEMORY_DC_MAGIC)
143 hbmpRet = CreateBitmap(width, height,
144 GetDeviceCaps(hdc, PLANES),
145 GetDeviceCaps(hdc, BITSPIXEL),
150 BITMAPOBJ *bmp = GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC );
154 /* A device-dependent bitmap is selected in the DC */
155 hbmpRet = CreateBitmap(width, height,
156 bmp->bitmap.bmPlanes,
157 bmp->bitmap.bmBitsPixel,
162 /* A DIB section is selected in the DC */
166 /* Allocate memory for a BITMAPINFOHEADER structure and a
167 color table. The maximum number of colors in a color table
168 is 256 which corresponds to a bitmap with depth 8.
169 Bitmaps with higher depths don't have color tables. */
170 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
174 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
175 bi->bmiHeader.biWidth = width;
176 bi->bmiHeader.biHeight = height;
177 bi->bmiHeader.biPlanes = bmp->dib->dsBmih.biPlanes;
178 bi->bmiHeader.biBitCount = bmp->dib->dsBmih.biBitCount;
179 bi->bmiHeader.biCompression = bmp->dib->dsBmih.biCompression;
180 bi->bmiHeader.biSizeImage = 0;
181 bi->bmiHeader.biXPelsPerMeter = bmp->dib->dsBmih.biXPelsPerMeter;
182 bi->bmiHeader.biYPelsPerMeter = bmp->dib->dsBmih.biYPelsPerMeter;
183 bi->bmiHeader.biClrUsed = bmp->dib->dsBmih.biClrUsed;
184 bi->bmiHeader.biClrImportant = bmp->dib->dsBmih.biClrImportant;
186 if (bi->bmiHeader.biCompression == BI_BITFIELDS)
188 /* Copy the color masks */
189 CopyMemory(bi->bmiColors, bmp->dib->dsBitfields, 3 * sizeof(DWORD));
191 else if (bi->bmiHeader.biBitCount <= 8)
193 /* Copy the color table */
194 GetDIBColorTable(hdc, 0, 256, bi->bmiColors);
197 hbmpRet = CreateDIBSection(hdc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
198 HeapFree(GetProcessHeap(), 0, bi);
201 GDI_ReleaseObj(dc->hBitmap);
203 DC_ReleaseDCPtr( dc );
205 TRACE("\t\t%p\n", hbmpRet);
210 /******************************************************************************
211 * CreateBitmapIndirect [GDI32.@]
213 * Creates a bitmap with the specified info.
216 * bmp [I] Pointer to the bitmap info describing the bitmap
219 * Success: Handle to bitmap
220 * Failure: NULL. Use GetLastError() to determine the cause.
223 * If a width or height of 0 are given, a 1x1 monochrome bitmap is returned.
225 HBITMAP WINAPI CreateBitmapIndirect( const BITMAP *bmp )
231 if (!bmp || bmp->bmType)
233 SetLastError( ERROR_INVALID_PARAMETER );
237 if (bmp->bmWidth > 0x7ffffff || bmp->bmHeight > 0x7ffffff)
239 SetLastError( ERROR_INVALID_PARAMETER );
245 if (!bm.bmWidth || !bm.bmHeight)
247 return GetStockObject( DEFAULT_BITMAP );
252 bm.bmHeight = -bm.bmHeight;
254 bm.bmWidth = -bm.bmWidth;
257 if (bm.bmPlanes != 1)
259 FIXME("planes = %d\n", bm.bmPlanes);
260 SetLastError( ERROR_INVALID_PARAMETER );
264 /* Windows only uses 1, 4, 8, 16, 24 and 32 bpp */
265 if(bm.bmBitsPixel == 1) bm.bmBitsPixel = 1;
266 else if(bm.bmBitsPixel <= 4) bm.bmBitsPixel = 4;
267 else if(bm.bmBitsPixel <= 8) bm.bmBitsPixel = 8;
268 else if(bm.bmBitsPixel <= 16) bm.bmBitsPixel = 16;
269 else if(bm.bmBitsPixel <= 24) bm.bmBitsPixel = 24;
270 else if(bm.bmBitsPixel <= 32) bm.bmBitsPixel = 32;
272 WARN("Invalid bmBitsPixel %d, returning ERROR_INVALID_PARAMETER\n", bm.bmBitsPixel);
273 SetLastError(ERROR_INVALID_PARAMETER);
277 /* Windows ignores the provided bm.bmWidthBytes */
278 bm.bmWidthBytes = BITMAP_GetWidthBytes( bm.bmWidth, bm.bmBitsPixel );
279 /* XP doesn't allow to create bitmaps larger than 128 Mb */
280 if (bm.bmHeight * bm.bmWidthBytes > 128 * 1024 * 1024)
282 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
286 /* Create the BITMAPOBJ */
287 bmpobj = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC,
288 (HGDIOBJ *)&hbitmap, &bitmap_funcs );
292 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
296 TRACE("%dx%d, %d colors returning %p\n", bm.bmWidth, bm.bmHeight,
297 1 << (bm.bmPlanes * bm.bmBitsPixel), hbitmap);
302 bmpobj->bitmap.bmBits = NULL;
303 bmpobj->funcs = NULL;
305 bmpobj->segptr_bits = 0;
306 bmpobj->color_table = NULL;
307 bmpobj->nb_colors = 0;
310 SetBitmapBits( hbitmap, bm.bmHeight * bm.bmWidthBytes, bm.bmBits );
312 GDI_ReleaseObj( hbitmap );
317 /***********************************************************************
318 * GetBitmapBits [GDI32.@]
320 * Copies bitmap bits of bitmap to buffer.
323 * Success: Number of bytes copied
326 LONG WINAPI GetBitmapBits(
327 HBITMAP hbitmap, /* [in] Handle to bitmap */
328 LONG count, /* [in] Number of bytes to copy */
329 LPVOID bits) /* [out] Pointer to buffer to receive bits */
331 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
336 if (bmp->dib) /* simply copy the bits from the DIB */
338 DIBSECTION *dib = bmp->dib;
339 const char *src = dib->dsBm.bmBits;
340 INT width_bytes = BITMAP_GetWidthBytes(dib->dsBm.bmWidth, dib->dsBm.bmBitsPixel);
341 DWORD max = width_bytes * bmp->bitmap.bmHeight;
349 if (count > max) count = max;
352 /* GetBitmapBits returns not 32-bit aligned data */
354 if (bmp->dib->dsBmih.biHeight >= 0) /* not top-down, need to flip contents vertically */
356 src += dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
359 src -= dib->dsBm.bmWidthBytes;
360 memcpy( bits, src, min( count, width_bytes ) );
361 bits = (char *)bits + width_bytes;
362 count -= width_bytes;
369 memcpy( bits, src, min( count, width_bytes ) );
370 src += dib->dsBm.bmWidthBytes;
371 bits = (char *)bits + width_bytes;
372 count -= width_bytes;
378 /* If the bits vector is null, the function should return the read size */
381 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
386 WARN("(%d): Negative number of bytes passed???\n", count );
390 /* Only get entire lines */
391 height = count / bmp->bitmap.bmWidthBytes;
392 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
393 count = height * bmp->bitmap.bmWidthBytes;
396 WARN("Less than one entire line requested\n");
402 TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
403 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
404 1 << bmp->bitmap.bmBitsPixel, height );
406 if(bmp->funcs && bmp->funcs->pGetBitmapBits)
408 TRACE("Calling device specific BitmapBits\n");
409 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
412 if(!bmp->bitmap.bmBits) {
413 TRACE("Bitmap is empty\n");
414 memset(bits, 0, count);
417 memcpy(bits, bmp->bitmap.bmBits, count);
423 GDI_ReleaseObj( hbitmap );
428 /******************************************************************************
429 * SetBitmapBits [GDI32.@]
431 * Sets bits of color data for a bitmap.
434 * Success: Number of bytes used in setting the bitmap bits
437 LONG WINAPI SetBitmapBits(
438 HBITMAP hbitmap, /* [in] Handle to bitmap */
439 LONG count, /* [in] Number of bytes in bitmap array */
440 LPCVOID bits) /* [in] Address of array with bitmap bits */
442 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
445 if ((!bmp) || (!bits))
449 WARN("(%d): Negative number of bytes passed???\n", count );
453 if (bmp->dib) /* simply copy the bits into the DIB */
455 DIBSECTION *dib = bmp->dib;
456 char *dest = dib->dsBm.bmBits;
457 DWORD max = dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
458 if (count > max) count = max;
461 if (bmp->dib->dsBmih.biHeight >= 0) /* not top-down, need to flip contents vertically */
463 dest += dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
466 dest -= dib->dsBm.bmWidthBytes;
467 memcpy( dest, bits, min( count, dib->dsBm.bmWidthBytes ) );
468 bits = (const char *)bits + dib->dsBm.bmWidthBytes;
469 count -= dib->dsBm.bmWidthBytes;
472 else memcpy( dest, bits, count );
474 GDI_ReleaseObj( hbitmap );
478 /* Only get entire lines */
479 height = count / bmp->bitmap.bmWidthBytes;
480 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
481 count = height * bmp->bitmap.bmWidthBytes;
483 TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
484 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
485 1 << bmp->bitmap.bmBitsPixel, height );
487 if(bmp->funcs && bmp->funcs->pSetBitmapBits) {
489 TRACE("Calling device specific BitmapBits\n");
490 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
493 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
494 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
495 if(!bmp->bitmap.bmBits) {
496 WARN("Unable to allocate bit buffer\n");
499 memcpy(bmp->bitmap.bmBits, bits, count);
504 GDI_ReleaseObj( hbitmap );
508 /**********************************************************************
512 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
514 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
522 res = CreateBitmapIndirect(&bm);
525 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
527 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
528 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
529 HeapFree( GetProcessHeap(), 0, buf );
532 GDI_ReleaseObj( hbitmap );
537 /***********************************************************************
540 * Set the type of DC that owns the bitmap. This is used when the
541 * bitmap is selected into a device to initialize the bitmap function
544 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
549 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
550 if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
552 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
555 if (!bitmap->funcs) /* not owned by a DC yet */
557 if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap,
558 bitmap->bitmap.bmBits );
559 if (ret) bitmap->funcs = dc->funcs;
561 else if (bitmap->funcs != dc->funcs)
563 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
566 GDI_ReleaseObj( hbitmap );
571 /***********************************************************************
572 * BITMAP_SelectObject
574 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, HDC hdc )
580 if (!(bitmap = GDI_GetObjPtr( handle, BITMAP_MAGIC ))) return 0;
582 if (!(dc = get_dc_ptr( hdc )))
584 GDI_ReleaseObj( handle );
587 if (GetObjectType( hdc ) != OBJ_MEMDC)
593 if (handle == dc->hBitmap) goto done; /* nothing to do */
595 if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
597 WARN( "Bitmap already selected in another DC\n" );
602 if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
608 if (dc->funcs->pSelectBitmap) handle = dc->funcs->pSelectBitmap( dc->physDev, handle );
612 dc->hBitmap = handle;
613 GDI_inc_ref_count( handle );
615 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
617 GDI_dec_ref_count( ret );
622 GDI_ReleaseObj( handle );
623 release_dc_ptr( dc );
628 /***********************************************************************
629 * BITMAP_DeleteObject
631 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
633 BITMAPOBJ * bmp = obj;
635 if (bmp->funcs && bmp->funcs->pDeleteBitmap)
636 bmp->funcs->pDeleteBitmap( handle );
638 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
642 DIBSECTION *dib = bmp->dib;
644 if (dib->dsBm.bmBits)
648 SYSTEM_INFO SystemInfo;
649 GetSystemInfo( &SystemInfo );
650 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
651 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
653 else if (!dib->dsOffset)
654 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
656 HeapFree(GetProcessHeap(), 0, dib);
658 if (bmp->segptr_bits)
659 { /* free its selector array */
660 WORD sel = SELECTOROF(bmp->segptr_bits);
661 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
664 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
666 HeapFree(GetProcessHeap(), 0, bmp->color_table);
668 return GDI_FreeObject( handle, obj );
672 /***********************************************************************
675 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
677 BITMAPOBJ *bmp = obj;
681 if ( count <= sizeof(BITMAP16) )
683 BITMAP *bmp32 = &bmp->dib->dsBm;
685 bmp16.bmType = bmp32->bmType;
686 bmp16.bmWidth = bmp32->bmWidth;
687 bmp16.bmHeight = bmp32->bmHeight;
688 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
689 bmp16.bmPlanes = bmp32->bmPlanes;
690 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
691 bmp16.bmBits = (SEGPTR)0;
692 memcpy( buffer, &bmp16, count );
697 FIXME("not implemented for DIBs: count %d\n", count);
704 bmp16.bmType = bmp->bitmap.bmType;
705 bmp16.bmWidth = bmp->bitmap.bmWidth;
706 bmp16.bmHeight = bmp->bitmap.bmHeight;
707 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
708 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
709 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
710 bmp16.bmBits = (SEGPTR)0;
711 if (count < sizeof(bmp16)) return 0;
712 memcpy( buffer, &bmp16, sizeof(bmp16) );
713 return sizeof(bmp16);
718 /***********************************************************************
721 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
723 BITMAPOBJ *bmp = obj;
725 if( !buffer ) return sizeof(BITMAP);
726 if (count < sizeof(BITMAP)) return 0;
730 if (count >= sizeof(DIBSECTION))
732 memcpy( buffer, bmp->dib, sizeof(DIBSECTION) );
733 return sizeof(DIBSECTION);
735 else /* if (count >= sizeof(BITMAP)) */
737 DIBSECTION *dib = bmp->dib;
738 memcpy( buffer, &dib->dsBm, sizeof(BITMAP) );
739 return sizeof(BITMAP);
744 memcpy( buffer, &bmp->bitmap, sizeof(BITMAP) );
745 ((BITMAP *) buffer)->bmBits = NULL;
746 return sizeof(BITMAP);
751 /******************************************************************************
752 * CreateDiscardableBitmap [GDI32.@]
754 * Creates a discardable bitmap.
757 * Success: Handle to bitmap
760 HBITMAP WINAPI CreateDiscardableBitmap(
761 HDC hdc, /* [in] Handle to device context */
762 INT width, /* [in] Bitmap width */
763 INT height) /* [in] Bitmap height */
765 return CreateCompatibleBitmap( hdc, width, height );
769 /******************************************************************************
770 * GetBitmapDimensionEx [GDI32.@]
772 * Retrieves dimensions of a bitmap.
778 BOOL WINAPI GetBitmapDimensionEx(
779 HBITMAP hbitmap, /* [in] Handle to bitmap */
780 LPSIZE size) /* [out] Address of struct receiving dimensions */
782 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
783 if (!bmp) return FALSE;
785 GDI_ReleaseObj( hbitmap );
790 /******************************************************************************
791 * SetBitmapDimensionEx [GDI32.@]
793 * Assigns dimensions to a bitmap.
794 * MSDN says that this function will fail if hbitmap is a handle created by
795 * CreateDIBSection, but that's not true on Windows 2000.
801 BOOL WINAPI SetBitmapDimensionEx(
802 HBITMAP hbitmap, /* [in] Handle to bitmap */
803 INT x, /* [in] Bitmap width */
804 INT y, /* [in] Bitmap height */
805 LPSIZE prevSize) /* [out] Address of structure for orig dims */
807 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
808 if (!bmp) return FALSE;
809 if (prevSize) *prevSize = bmp->size;
812 GDI_ReleaseObj( hbitmap );