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"
26 #include "wine/winuser16.h"
28 #include "gdi_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
34 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc );
35 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
36 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
37 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj );
39 static const struct gdi_obj_funcs bitmap_funcs =
41 BITMAP_SelectObject, /* pSelectObject */
42 BITMAP_GetObject16, /* pGetObject16 */
43 BITMAP_GetObject, /* pGetObjectA */
44 BITMAP_GetObject, /* pGetObjectW */
45 NULL, /* pUnrealizeObject */
46 BITMAP_DeleteObject /* pDeleteObject */
49 /***********************************************************************
50 * BITMAP_GetWidthBytes
52 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
55 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
60 return 2 * ((bmWidth+15) >> 4);
63 bmWidth *= 3; /* fall through */
65 return bmWidth + (bmWidth & 1);
75 return 2 * ((bmWidth+3) >> 2);
78 WARN("Unknown depth %d, please report.\n", bpp );
84 /******************************************************************************
85 * CreateBitmap [GDI32.@]
87 * Creates a bitmap with the specified info.
90 * width [I] bitmap width
91 * height [I] bitmap height
92 * planes [I] Number of color planes
93 * bpp [I] Number of bits to identify a color
94 * bits [I] Pointer to array containing color data
97 * Success: Handle to bitmap
100 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
101 UINT bpp, LPCVOID bits )
107 bm.bmHeight = height;
108 bm.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
109 bm.bmPlanes = planes;
110 bm.bmBitsPixel = bpp;
111 bm.bmBits = (LPVOID)bits;
113 return CreateBitmapIndirect( &bm );
116 /******************************************************************************
117 * CreateCompatibleBitmap [GDI32.@]
119 * Creates a bitmap compatible with the DC.
122 * hdc [I] Handle to device context
123 * width [I] Width of bitmap
124 * height [I] Height of bitmap
127 * Success: Handle to bitmap
130 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
135 TRACE("(%p,%d,%d) = \n", hdc, width, height);
137 if ((width >= 0x10000) || (height >= 0x10000))
139 FIXME("got bad width %d or height %d, please look for reason\n",
144 if (!(dc = DC_GetDCPtr(hdc))) return 0;
146 if (GDIMAGIC( dc->header.wMagic ) != MEMORY_DC_MAGIC)
148 hbmpRet = CreateBitmap(width, height,
149 GetDeviceCaps(hdc, PLANES),
150 GetDeviceCaps(hdc, BITSPIXEL),
155 BITMAPOBJ *bmp = GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC );
159 /* A device-dependent bitmap is selected in the DC */
160 hbmpRet = CreateBitmap(width, height,
161 bmp->bitmap.bmPlanes,
162 bmp->bitmap.bmBitsPixel,
167 /* A DIB section is selected in the DC */
171 /* Allocate memory for a BITMAPINFOHEADER structure and a
172 color table. The maximum number of colors in a color table
173 is 256 which corresponds to a bitmap with depth 8.
174 Bitmaps with higher depths don't have color tables. */
175 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
179 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
180 bi->bmiHeader.biWidth = width;
181 bi->bmiHeader.biHeight = height;
182 bi->bmiHeader.biPlanes = bmp->dib->dsBmih.biPlanes;
183 bi->bmiHeader.biBitCount = bmp->dib->dsBmih.biBitCount;
184 bi->bmiHeader.biCompression = bmp->dib->dsBmih.biCompression;
185 bi->bmiHeader.biSizeImage = 0;
186 bi->bmiHeader.biXPelsPerMeter = bmp->dib->dsBmih.biXPelsPerMeter;
187 bi->bmiHeader.biYPelsPerMeter = bmp->dib->dsBmih.biYPelsPerMeter;
188 bi->bmiHeader.biClrUsed = bmp->dib->dsBmih.biClrUsed;
189 bi->bmiHeader.biClrImportant = bmp->dib->dsBmih.biClrImportant;
191 if (bi->bmiHeader.biCompression == BI_BITFIELDS)
193 /* Copy the color masks */
194 CopyMemory(bi->bmiColors, bmp->dib->dsBitfields, 3 * sizeof(DWORD));
196 else if (bi->bmiHeader.biBitCount <= 8)
198 /* Copy the color table */
199 GetDIBColorTable(hdc, 0, 256, bi->bmiColors);
202 hbmpRet = CreateDIBSection(hdc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
203 HeapFree(GetProcessHeap(), 0, bi);
206 GDI_ReleaseObj(dc->hBitmap);
211 TRACE("\t\t%p\n", hbmpRet);
216 /******************************************************************************
217 * CreateBitmapIndirect [GDI32.@]
219 * Creates a bitmap with the specified info.
222 * bmp [I] Pointer to the bitmap info describing the bitmap
225 * Success: Handle to bitmap
226 * Failure: NULL. Use GetLastError() to determine the cause.
229 * If a width or height of 0 are given, a 1x1 monochrome bitmap is returned.
231 HBITMAP WINAPI CreateBitmapIndirect( const BITMAP *bmp )
237 if (!bmp || bmp->bmType || bmp->bmPlanes != 1)
239 if (bmp && bmp->bmPlanes != 1)
240 FIXME("planes = %d\n", bmp->bmPlanes);
241 SetLastError( ERROR_INVALID_PARAMETER );
247 if (!bm.bmWidth || !bm.bmHeight)
249 bm.bmWidth = bm.bmHeight = 1;
250 bm.bmPlanes = bm.bmBitsPixel = 1;
257 bm.bmHeight = -bm.bmHeight;
259 bm.bmWidth = -bm.bmWidth;
262 /* Create the BITMAPOBJ */
263 bmpobj = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC,
264 (HGDIOBJ *)&hbitmap, &bitmap_funcs );
268 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
272 TRACE("%dx%d, %d colors returning %p\n", bm.bmWidth, bm.bmHeight,
273 1 << (bm.bmPlanes * bm.bmBitsPixel), hbitmap);
278 bmpobj->bitmap.bmBits = NULL;
279 bmpobj->funcs = NULL;
280 bmpobj->physBitmap = NULL;
282 bmpobj->segptr_bits = 0;
285 SetBitmapBits( hbitmap, bm.bmHeight * bm.bmWidthBytes, bm.bmBits );
287 GDI_ReleaseObj( hbitmap );
292 /***********************************************************************
293 * GetBitmapBits [GDI32.@]
295 * Copies bitmap bits of bitmap to buffer.
298 * Success: Number of bytes copied
301 LONG WINAPI GetBitmapBits(
302 HBITMAP hbitmap, /* [in] Handle to bitmap */
303 LONG count, /* [in] Number of bytes to copy */
304 LPVOID bits) /* [out] Pointer to buffer to receive bits */
306 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
311 /* If the bits vector is null, the function should return the read size */
314 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
319 WARN("(%ld): Negative number of bytes passed???\n", count );
323 /* Only get entire lines */
324 height = count / bmp->bitmap.bmWidthBytes;
325 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
326 count = height * bmp->bitmap.bmWidthBytes;
329 WARN("Less than one entire line requested\n");
335 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
336 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
337 1 << bmp->bitmap.bmBitsPixel, height );
339 if(bmp->funcs && bmp->funcs->pGetBitmapBits)
341 TRACE("Calling device specific BitmapBits\n");
342 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
345 if(!bmp->bitmap.bmBits) {
346 WARN("Bitmap is empty\n");
349 memcpy(bits, bmp->bitmap.bmBits, count);
355 GDI_ReleaseObj( hbitmap );
360 /******************************************************************************
361 * SetBitmapBits [GDI32.@]
363 * Sets bits of color data for a bitmap.
366 * Success: Number of bytes used in setting the bitmap bits
369 LONG WINAPI SetBitmapBits(
370 HBITMAP hbitmap, /* [in] Handle to bitmap */
371 LONG count, /* [in] Number of bytes in bitmap array */
372 LPCVOID bits) /* [in] Address of array with bitmap bits */
374 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
377 if ((!bmp) || (!bits))
381 WARN("(%ld): Negative number of bytes passed???\n", count );
385 /* Only get entire lines */
386 height = count / bmp->bitmap.bmWidthBytes;
387 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
388 count = height * bmp->bitmap.bmWidthBytes;
390 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
391 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
392 1 << bmp->bitmap.bmBitsPixel, height );
394 if(bmp->funcs && bmp->funcs->pSetBitmapBits) {
396 TRACE("Calling device specific BitmapBits\n");
397 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
400 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
401 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
402 if(!bmp->bitmap.bmBits) {
403 WARN("Unable to allocate bit buffer\n");
406 memcpy(bmp->bitmap.bmBits, bits, count);
411 GDI_ReleaseObj( hbitmap );
415 /**********************************************************************
419 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
421 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
429 res = CreateBitmapIndirect(&bm);
432 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
434 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
435 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
436 HeapFree( GetProcessHeap(), 0, buf );
439 GDI_ReleaseObj( hbitmap );
444 /***********************************************************************
447 * Set the type of DC that owns the bitmap. This is used when the
448 * bitmap is selected into a device to initialize the bitmap function
451 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
456 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
457 if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
459 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
462 if (!bitmap->funcs) /* not owned by a DC yet */
464 if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap );
465 if (ret) bitmap->funcs = dc->funcs;
467 else if (bitmap->funcs != dc->funcs)
469 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
472 GDI_ReleaseObj( hbitmap );
477 /***********************************************************************
478 * BITMAP_SelectObject
480 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
483 BITMAPOBJ *bitmap = obj;
484 DC *dc = DC_GetDCPtr( hdc );
487 if (GetObjectType( hdc ) != OBJ_MEMDC)
489 GDI_ReleaseObj( hdc );
493 if (handle == dc->hBitmap) goto done; /* nothing to do */
495 if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
497 WARN( "Bitmap already selected in another DC\n" );
498 GDI_ReleaseObj( hdc );
502 if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
504 GDI_ReleaseObj( hdc );
508 if (dc->funcs->pSelectBitmap) handle = dc->funcs->pSelectBitmap( dc->physDev, handle );
512 dc->hBitmap = handle;
513 dc->flags &= ~DC_DIRTY;
514 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
520 GDI_ReleaseObj( hdc );
525 /***********************************************************************
526 * BITMAP_DeleteObject
528 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
530 BITMAPOBJ * bmp = obj;
532 if (bmp->funcs && bmp->funcs->pDeleteBitmap)
533 bmp->funcs->pDeleteBitmap( handle );
535 if( bmp->bitmap.bmBits )
536 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
540 DIBSECTION *dib = bmp->dib;
542 if (dib->dsBm.bmBits)
546 SYSTEM_INFO SystemInfo;
547 GetSystemInfo( &SystemInfo );
548 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
549 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
551 else if (!dib->dsOffset)
552 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
554 HeapFree(GetProcessHeap(), 0, dib);
556 if (bmp->segptr_bits)
557 { /* free its selector array */
558 WORD sel = SELECTOROF(bmp->segptr_bits);
559 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
562 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
565 return GDI_FreeObject( handle, obj );
569 /***********************************************************************
572 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
574 BITMAPOBJ *bmp = obj;
578 if ( count <= sizeof(BITMAP16) )
580 BITMAP *bmp32 = &bmp->dib->dsBm;
582 bmp16.bmType = bmp32->bmType;
583 bmp16.bmWidth = bmp32->bmWidth;
584 bmp16.bmHeight = bmp32->bmHeight;
585 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
586 bmp16.bmPlanes = bmp32->bmPlanes;
587 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
588 bmp16.bmBits = (SEGPTR)0;
589 memcpy( buffer, &bmp16, count );
594 FIXME("not implemented for DIBs: count %d\n", count);
601 bmp16.bmType = bmp->bitmap.bmType;
602 bmp16.bmWidth = bmp->bitmap.bmWidth;
603 bmp16.bmHeight = bmp->bitmap.bmHeight;
604 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
605 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
606 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
607 bmp16.bmBits = (SEGPTR)0;
608 if (count > sizeof(bmp16)) count = sizeof(bmp16);
609 memcpy( buffer, &bmp16, count );
615 /***********************************************************************
618 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
620 BITMAPOBJ *bmp = obj;
625 return sizeof(DIBSECTION);
626 if (count < sizeof(DIBSECTION))
628 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
632 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
635 memcpy( buffer, bmp->dib, count );
641 return sizeof(BITMAP);
642 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
643 memcpy( buffer, &bmp->bitmap, count );
649 /******************************************************************************
650 * CreateDiscardableBitmap [GDI32.@]
652 * Creates a discardable bitmap.
655 * Success: Handle to bitmap
658 HBITMAP WINAPI CreateDiscardableBitmap(
659 HDC hdc, /* [in] Handle to device context */
660 INT width, /* [in] Bitmap width */
661 INT height) /* [in] Bitmap height */
663 return CreateCompatibleBitmap( hdc, width, height );
667 /******************************************************************************
668 * GetBitmapDimensionEx [GDI32.@]
670 * Retrieves dimensions of a bitmap.
676 BOOL WINAPI GetBitmapDimensionEx(
677 HBITMAP hbitmap, /* [in] Handle to bitmap */
678 LPSIZE size) /* [out] Address of struct receiving dimensions */
680 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
681 if (!bmp) return FALSE;
683 GDI_ReleaseObj( hbitmap );
688 /******************************************************************************
689 * SetBitmapDimensionEx [GDI32.@]
691 * Assigns dimensions to a bitmap.
692 * MSDN says that this function will fail if hbitmap is a handle created by
693 * CreateDIBSection, but that's not true on Windows 2000.
699 BOOL WINAPI SetBitmapDimensionEx(
700 HBITMAP hbitmap, /* [in] Handle to bitmap */
701 INT x, /* [in] Bitmap width */
702 INT y, /* [in] Bitmap height */
703 LPSIZE prevSize) /* [out] Address of structure for orig dims */
705 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
706 if (!bmp) return FALSE;
707 if (prevSize) *prevSize = bmp->size;
710 GDI_ReleaseObj( hbitmap );