gdi32: Avoid locking in BITMAP_CopyBitmap.
[wine.git] / dlls / gdi32 / bitmap.c
blobc46892014fae52571258d3c754536abf65fa589e
1 /*
2 * GDI bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 * 1998 Huw D M Davies
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
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
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
55 * data.
57 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
59 switch(bpp)
61 case 1:
62 return 2 * ((bmWidth+15) >> 4);
64 case 24:
65 bmWidth *= 3; /* fall through */
66 case 8:
67 return bmWidth + (bmWidth & 1);
69 case 32:
70 return bmWidth * 4;
72 case 16:
73 case 15:
74 return bmWidth * 2;
76 case 4:
77 return 2 * ((bmWidth+3) >> 2);
79 default:
80 WARN("Unknown depth %d, please report.\n", bpp );
82 return -1;
86 /******************************************************************************
87 * CreateBitmap [GDI32.@]
89 * Creates a bitmap with the specified info.
91 * PARAMS
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
98 * RETURNS
99 * Success: Handle to bitmap
100 * Failure: 0
102 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
103 UINT bpp, LPCVOID bits )
105 BITMAP bm;
107 bm.bmType = 0;
108 bm.bmWidth = width;
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.
123 * PARAMS
124 * hdc [I] Handle to device context
125 * width [I] Width of bitmap
126 * height [I] Height of bitmap
128 * RETURNS
129 * Success: Handle to bitmap
130 * Failure: 0
132 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
134 HBITMAP hbmpRet = 0;
136 TRACE("(%p,%d,%d) =\n", hdc, width, height);
138 if (GetObjectType( hdc ) != OBJ_MEMDC)
140 hbmpRet = CreateBitmap(width, height,
141 GetDeviceCaps(hdc, PLANES),
142 GetDeviceCaps(hdc, BITSPIXEL),
143 NULL);
145 else /* Memory DC */
147 DIBSECTION dib;
148 HBITMAP bitmap = GetCurrentObject( hdc, OBJ_BITMAP );
149 INT size = GetObjectW( bitmap, sizeof(dib), &dib );
151 if (!size) return 0;
153 if (size == sizeof(BITMAP))
155 /* A device-dependent bitmap is selected in the DC */
156 hbmpRet = CreateBitmap(width, height,
157 dib.dsBm.bmPlanes,
158 dib.dsBm.bmBitsPixel,
159 NULL);
161 else
163 /* A DIB section is selected in the DC */
164 BITMAPINFO *bi;
165 void *bits;
167 /* Allocate memory for a BITMAPINFOHEADER structure and a
168 color table. The maximum number of colors in a color table
169 is 256 which corresponds to a bitmap with depth 8.
170 Bitmaps with higher depths don't have color tables. */
171 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
173 if (bi)
175 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
176 bi->bmiHeader.biWidth = width;
177 bi->bmiHeader.biHeight = height;
178 bi->bmiHeader.biPlanes = dib.dsBmih.biPlanes;
179 bi->bmiHeader.biBitCount = dib.dsBmih.biBitCount;
180 bi->bmiHeader.biCompression = dib.dsBmih.biCompression;
181 bi->bmiHeader.biSizeImage = 0;
182 bi->bmiHeader.biXPelsPerMeter = dib.dsBmih.biXPelsPerMeter;
183 bi->bmiHeader.biYPelsPerMeter = dib.dsBmih.biYPelsPerMeter;
184 bi->bmiHeader.biClrUsed = dib.dsBmih.biClrUsed;
185 bi->bmiHeader.biClrImportant = dib.dsBmih.biClrImportant;
187 if (bi->bmiHeader.biCompression == BI_BITFIELDS)
189 /* Copy the color masks */
190 CopyMemory(bi->bmiColors, dib.dsBitfields, 3 * sizeof(DWORD));
192 else if (bi->bmiHeader.biBitCount <= 8)
194 /* Copy the color table */
195 GetDIBColorTable(hdc, 0, 256, bi->bmiColors);
198 hbmpRet = CreateDIBSection(hdc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
199 HeapFree(GetProcessHeap(), 0, bi);
204 TRACE("\t\t%p\n", hbmpRet);
205 return hbmpRet;
209 /******************************************************************************
210 * CreateBitmapIndirect [GDI32.@]
212 * Creates a bitmap with the specified info.
214 * PARAMS
215 * bmp [I] Pointer to the bitmap info describing the bitmap
217 * RETURNS
218 * Success: Handle to bitmap
219 * Failure: NULL. Use GetLastError() to determine the cause.
221 * NOTES
222 * If a width or height of 0 are given, a 1x1 monochrome bitmap is returned.
224 HBITMAP WINAPI CreateBitmapIndirect( const BITMAP *bmp )
226 BITMAP bm;
227 BITMAPOBJ *bmpobj;
228 HBITMAP hbitmap;
230 if (!bmp || bmp->bmType)
232 SetLastError( ERROR_INVALID_PARAMETER );
233 return NULL;
236 if (bmp->bmWidth > 0x7ffffff || bmp->bmHeight > 0x7ffffff)
238 SetLastError( ERROR_INVALID_PARAMETER );
239 return 0;
242 bm = *bmp;
244 if (!bm.bmWidth || !bm.bmHeight)
246 return GetStockObject( DEFAULT_BITMAP );
248 else
250 if (bm.bmHeight < 0)
251 bm.bmHeight = -bm.bmHeight;
252 if (bm.bmWidth < 0)
253 bm.bmWidth = -bm.bmWidth;
256 if (bm.bmPlanes != 1)
258 FIXME("planes = %d\n", bm.bmPlanes);
259 SetLastError( ERROR_INVALID_PARAMETER );
260 return NULL;
263 /* Windows only uses 1, 4, 8, 16, 24 and 32 bpp */
264 if(bm.bmBitsPixel == 1) bm.bmBitsPixel = 1;
265 else if(bm.bmBitsPixel <= 4) bm.bmBitsPixel = 4;
266 else if(bm.bmBitsPixel <= 8) bm.bmBitsPixel = 8;
267 else if(bm.bmBitsPixel <= 16) bm.bmBitsPixel = 16;
268 else if(bm.bmBitsPixel <= 24) bm.bmBitsPixel = 24;
269 else if(bm.bmBitsPixel <= 32) bm.bmBitsPixel = 32;
270 else {
271 WARN("Invalid bmBitsPixel %d, returning ERROR_INVALID_PARAMETER\n", bm.bmBitsPixel);
272 SetLastError(ERROR_INVALID_PARAMETER);
273 return NULL;
276 /* Windows ignores the provided bm.bmWidthBytes */
277 bm.bmWidthBytes = BITMAP_GetWidthBytes( bm.bmWidth, bm.bmBitsPixel );
278 /* XP doesn't allow to create bitmaps larger than 128 Mb */
279 if (bm.bmHeight * bm.bmWidthBytes > 128 * 1024 * 1024)
281 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
282 return 0;
285 /* Create the BITMAPOBJ */
286 bmpobj = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC,
287 (HGDIOBJ *)&hbitmap, &bitmap_funcs );
289 if (!bmpobj)
291 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
292 return NULL;
295 TRACE("%dx%d, %d colors returning %p\n", bm.bmWidth, bm.bmHeight,
296 1 << (bm.bmPlanes * bm.bmBitsPixel), hbitmap);
298 bmpobj->size.cx = 0;
299 bmpobj->size.cy = 0;
300 bmpobj->bitmap = bm;
301 bmpobj->bitmap.bmBits = NULL;
302 bmpobj->funcs = NULL;
303 bmpobj->dib = NULL;
304 bmpobj->segptr_bits = 0;
305 bmpobj->color_table = NULL;
306 bmpobj->nb_colors = 0;
308 if (bm.bmBits)
309 SetBitmapBits( hbitmap, bm.bmHeight * bm.bmWidthBytes, bm.bmBits );
311 GDI_ReleaseObj( hbitmap );
312 return hbitmap;
316 /***********************************************************************
317 * GetBitmapBits [GDI32.@]
319 * Copies bitmap bits of bitmap to buffer.
321 * RETURNS
322 * Success: Number of bytes copied
323 * Failure: 0
325 LONG WINAPI GetBitmapBits(
326 HBITMAP hbitmap, /* [in] Handle to bitmap */
327 LONG count, /* [in] Number of bytes to copy */
328 LPVOID bits) /* [out] Pointer to buffer to receive bits */
330 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
331 LONG height, ret;
333 if (!bmp) return 0;
335 if (bmp->dib) /* simply copy the bits from the DIB */
337 DIBSECTION *dib = bmp->dib;
338 const char *src = dib->dsBm.bmBits;
339 INT width_bytes = BITMAP_GetWidthBytes(dib->dsBm.bmWidth, dib->dsBm.bmBitsPixel);
340 DWORD max = width_bytes * bmp->bitmap.bmHeight;
342 if (!bits)
344 ret = max;
345 goto done;
348 if (count > max) count = max;
349 ret = count;
351 /* GetBitmapBits returns not 32-bit aligned data */
353 if (bmp->dib->dsBmih.biHeight >= 0) /* not top-down, need to flip contents vertically */
355 src += dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
356 while (count > 0)
358 src -= dib->dsBm.bmWidthBytes;
359 memcpy( bits, src, min( count, width_bytes ) );
360 bits = (char *)bits + width_bytes;
361 count -= width_bytes;
364 else
366 while (count > 0)
368 memcpy( bits, src, min( count, width_bytes ) );
369 src += dib->dsBm.bmWidthBytes;
370 bits = (char *)bits + width_bytes;
371 count -= width_bytes;
374 goto done;
377 /* If the bits vector is null, the function should return the read size */
378 if(bits == NULL)
380 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
381 goto done;
384 if (count < 0) {
385 WARN("(%d): Negative number of bytes passed???\n", count );
386 count = -count;
389 /* Only get entire lines */
390 height = count / bmp->bitmap.bmWidthBytes;
391 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
392 count = height * bmp->bitmap.bmWidthBytes;
393 if (count == 0)
395 WARN("Less than one entire line requested\n");
396 ret = 0;
397 goto done;
401 TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
402 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
403 1 << bmp->bitmap.bmBitsPixel, height );
405 if(bmp->funcs && bmp->funcs->pGetBitmapBits)
407 TRACE("Calling device specific BitmapBits\n");
408 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
409 } else {
411 if(!bmp->bitmap.bmBits) {
412 TRACE("Bitmap is empty\n");
413 memset(bits, 0, count);
414 ret = count;
415 } else {
416 memcpy(bits, bmp->bitmap.bmBits, count);
417 ret = count;
421 done:
422 GDI_ReleaseObj( hbitmap );
423 return ret;
427 /******************************************************************************
428 * SetBitmapBits [GDI32.@]
430 * Sets bits of color data for a bitmap.
432 * RETURNS
433 * Success: Number of bytes used in setting the bitmap bits
434 * Failure: 0
436 LONG WINAPI SetBitmapBits(
437 HBITMAP hbitmap, /* [in] Handle to bitmap */
438 LONG count, /* [in] Number of bytes in bitmap array */
439 LPCVOID bits) /* [in] Address of array with bitmap bits */
441 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
442 LONG height, ret;
444 if ((!bmp) || (!bits))
445 return 0;
447 if (count < 0) {
448 WARN("(%d): Negative number of bytes passed???\n", count );
449 count = -count;
452 if (bmp->dib) /* simply copy the bits into the DIB */
454 DIBSECTION *dib = bmp->dib;
455 char *dest = dib->dsBm.bmBits;
456 DWORD max = dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
457 if (count > max) count = max;
458 ret = count;
460 if (bmp->dib->dsBmih.biHeight >= 0) /* not top-down, need to flip contents vertically */
462 dest += dib->dsBm.bmWidthBytes * dib->dsBm.bmHeight;
463 while (count > 0)
465 dest -= dib->dsBm.bmWidthBytes;
466 memcpy( dest, bits, min( count, dib->dsBm.bmWidthBytes ) );
467 bits = (const char *)bits + dib->dsBm.bmWidthBytes;
468 count -= dib->dsBm.bmWidthBytes;
471 else memcpy( dest, bits, count );
473 GDI_ReleaseObj( hbitmap );
474 return ret;
477 /* Only get entire lines */
478 height = count / bmp->bitmap.bmWidthBytes;
479 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
480 count = height * bmp->bitmap.bmWidthBytes;
482 TRACE("(%p, %d, %p) %dx%d %d colors fetched height: %d\n",
483 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
484 1 << bmp->bitmap.bmBitsPixel, height );
486 if(bmp->funcs && bmp->funcs->pSetBitmapBits) {
488 TRACE("Calling device specific BitmapBits\n");
489 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
490 } else {
492 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
493 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
494 if(!bmp->bitmap.bmBits) {
495 WARN("Unable to allocate bit buffer\n");
496 ret = 0;
497 } else {
498 memcpy(bmp->bitmap.bmBits, bits, count);
499 ret = count;
503 GDI_ReleaseObj( hbitmap );
504 return ret;
507 /**********************************************************************
508 * BITMAP_CopyBitmap
511 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
513 HBITMAP res = 0;
514 BITMAP bm;
516 if (!GetObjectW( hbitmap, sizeof(bm), &bm )) return 0;
517 res = CreateBitmapIndirect(&bm);
519 if(res) {
520 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
521 bm.bmHeight );
522 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
523 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
524 HeapFree( GetProcessHeap(), 0, buf );
526 return res;
530 /***********************************************************************
531 * BITMAP_SetOwnerDC
533 * Set the type of DC that owns the bitmap. This is used when the
534 * bitmap is selected into a device to initialize the bitmap function
535 * table.
537 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
539 BITMAPOBJ *bitmap;
540 BOOL ret;
542 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
543 if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
545 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
547 ret = TRUE;
548 if (!bitmap->funcs) /* not owned by a DC yet */
550 if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap,
551 bitmap->bitmap.bmBits );
552 if (ret) bitmap->funcs = dc->funcs;
554 else if (bitmap->funcs != dc->funcs)
556 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
557 ret = FALSE;
559 GDI_ReleaseObj( hbitmap );
560 return ret;
564 /***********************************************************************
565 * BITMAP_SelectObject
567 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, HDC hdc )
569 HGDIOBJ ret;
570 BITMAPOBJ *bitmap;
571 DC *dc;
573 if (!(dc = get_dc_ptr( hdc ))) return 0;
575 if (GetObjectType( hdc ) != OBJ_MEMDC)
577 ret = 0;
578 goto done;
580 ret = dc->hBitmap;
581 if (handle == dc->hBitmap) goto done; /* nothing to do */
583 if (!(bitmap = GDI_GetObjPtr( handle, BITMAP_MAGIC )))
585 ret = 0;
586 goto done;
589 if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
591 WARN( "Bitmap already selected in another DC\n" );
592 GDI_ReleaseObj( handle );
593 ret = 0;
594 goto done;
597 if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
599 GDI_ReleaseObj( handle );
600 ret = 0;
601 goto done;
604 if (dc->funcs->pSelectBitmap && !dc->funcs->pSelectBitmap( dc->physDev, handle ))
606 GDI_ReleaseObj( handle );
607 ret = 0;
609 else
611 dc->hBitmap = handle;
612 GDI_inc_ref_count( handle );
613 dc->dirty = 0;
614 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
615 GDI_ReleaseObj( handle );
616 DC_InitDC( dc );
617 GDI_dec_ref_count( ret );
620 done:
621 release_dc_ptr( dc );
622 return ret;
626 /***********************************************************************
627 * BITMAP_DeleteObject
629 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
631 BITMAPOBJ * bmp = obj;
633 if (bmp->funcs && bmp->funcs->pDeleteBitmap)
634 bmp->funcs->pDeleteBitmap( handle );
636 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
638 if (bmp->dib)
640 DIBSECTION *dib = bmp->dib;
642 if (dib->dsBm.bmBits)
644 if (dib->dshSection)
646 SYSTEM_INFO SystemInfo;
647 GetSystemInfo( &SystemInfo );
648 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
649 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
651 else if (!dib->dsOffset)
652 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
654 HeapFree(GetProcessHeap(), 0, dib);
655 bmp->dib = NULL;
656 if (bmp->segptr_bits)
657 { /* free its selector array */
658 WORD sel = SELECTOROF(bmp->segptr_bits);
659 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
660 int i;
662 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
664 HeapFree(GetProcessHeap(), 0, bmp->color_table);
666 return GDI_FreeObject( handle, obj );
670 /***********************************************************************
671 * BITMAP_GetObject16
673 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
675 BITMAPOBJ *bmp = obj;
677 if (bmp->dib)
679 if ( count <= sizeof(BITMAP16) )
681 BITMAP *bmp32 = &bmp->dib->dsBm;
682 BITMAP16 bmp16;
683 bmp16.bmType = bmp32->bmType;
684 bmp16.bmWidth = bmp32->bmWidth;
685 bmp16.bmHeight = bmp32->bmHeight;
686 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
687 bmp16.bmPlanes = bmp32->bmPlanes;
688 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
689 bmp16.bmBits = (SEGPTR)0;
690 memcpy( buffer, &bmp16, count );
691 return count;
693 else
695 FIXME("not implemented for DIBs: count %d\n", count);
696 return 0;
699 else
701 BITMAP16 bmp16;
702 bmp16.bmType = bmp->bitmap.bmType;
703 bmp16.bmWidth = bmp->bitmap.bmWidth;
704 bmp16.bmHeight = bmp->bitmap.bmHeight;
705 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
706 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
707 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
708 bmp16.bmBits = (SEGPTR)0;
709 if (count < sizeof(bmp16)) return 0;
710 memcpy( buffer, &bmp16, sizeof(bmp16) );
711 return sizeof(bmp16);
716 /***********************************************************************
717 * BITMAP_GetObject
719 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
721 BITMAPOBJ *bmp = obj;
723 if( !buffer ) return sizeof(BITMAP);
724 if (count < sizeof(BITMAP)) return 0;
726 if (bmp->dib)
728 if (count >= sizeof(DIBSECTION))
730 memcpy( buffer, bmp->dib, sizeof(DIBSECTION) );
731 return sizeof(DIBSECTION);
733 else /* if (count >= sizeof(BITMAP)) */
735 DIBSECTION *dib = bmp->dib;
736 memcpy( buffer, &dib->dsBm, sizeof(BITMAP) );
737 return sizeof(BITMAP);
740 else
742 memcpy( buffer, &bmp->bitmap, sizeof(BITMAP) );
743 ((BITMAP *) buffer)->bmBits = NULL;
744 return sizeof(BITMAP);
749 /******************************************************************************
750 * CreateDiscardableBitmap [GDI32.@]
752 * Creates a discardable bitmap.
754 * RETURNS
755 * Success: Handle to bitmap
756 * Failure: NULL
758 HBITMAP WINAPI CreateDiscardableBitmap(
759 HDC hdc, /* [in] Handle to device context */
760 INT width, /* [in] Bitmap width */
761 INT height) /* [in] Bitmap height */
763 return CreateCompatibleBitmap( hdc, width, height );
767 /******************************************************************************
768 * GetBitmapDimensionEx [GDI32.@]
770 * Retrieves dimensions of a bitmap.
772 * RETURNS
773 * Success: TRUE
774 * Failure: FALSE
776 BOOL WINAPI GetBitmapDimensionEx(
777 HBITMAP hbitmap, /* [in] Handle to bitmap */
778 LPSIZE size) /* [out] Address of struct receiving dimensions */
780 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
781 if (!bmp) return FALSE;
782 *size = bmp->size;
783 GDI_ReleaseObj( hbitmap );
784 return TRUE;
788 /******************************************************************************
789 * SetBitmapDimensionEx [GDI32.@]
791 * Assigns dimensions to a bitmap.
792 * MSDN says that this function will fail if hbitmap is a handle created by
793 * CreateDIBSection, but that's not true on Windows 2000.
795 * RETURNS
796 * Success: TRUE
797 * Failure: FALSE
799 BOOL WINAPI SetBitmapDimensionEx(
800 HBITMAP hbitmap, /* [in] Handle to bitmap */
801 INT x, /* [in] Bitmap width */
802 INT y, /* [in] Bitmap height */
803 LPSIZE prevSize) /* [out] Address of structure for orig dims */
805 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
806 if (!bmp) return FALSE;
807 if (prevSize) *prevSize = bmp->size;
808 bmp->size.cx = x;
809 bmp->size.cy = y;
810 GDI_ReleaseObj( hbitmap );
811 return TRUE;