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"
29 #include "gdi_private.h"
30 #include "wine/debug.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 static 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
);
85 /******************************************************************************
86 * CreateBitmap [GDI32.@] Creates a bitmap with the specified info
89 * width [I] bitmap width
90 * height [I] bitmap height
91 * planes [I] Number of color planes
92 * bpp [I] Number of bits to identify a color
93 * bits [I] Pointer to array containing color data
96 * Success: Handle to bitmap
99 HBITMAP WINAPI
CreateBitmap( INT width
, INT height
, UINT planes
,
100 UINT bpp
, LPCVOID bits
)
105 planes
= (BYTE
)planes
;
109 /* Check parameters */
110 if (!height
|| !width
)
118 FIXME("planes = %d\n", planes
);
121 if (height
< 0) height
= -height
;
122 if (width
< 0) width
= -width
;
124 /* Create the BITMAPOBJ */
125 if (!(bmp
= GDI_AllocObject( sizeof(BITMAPOBJ
), BITMAP_MAGIC
,
126 (HGDIOBJ
*)&hbitmap
, &bitmap_funcs
)))
129 TRACE("%dx%d, %d colors returning %p\n", width
, height
, 1 << (planes
*bpp
), hbitmap
);
133 bmp
->bitmap
.bmType
= 0;
134 bmp
->bitmap
.bmWidth
= width
;
135 bmp
->bitmap
.bmHeight
= height
;
136 bmp
->bitmap
.bmPlanes
= planes
;
137 bmp
->bitmap
.bmBitsPixel
= bpp
;
138 bmp
->bitmap
.bmWidthBytes
= BITMAP_GetWidthBytes( width
, bpp
);
139 bmp
->bitmap
.bmBits
= NULL
;
142 bmp
->physBitmap
= NULL
;
144 bmp
->segptr_bits
= 0;
146 if (bits
) /* Set bitmap bits */
147 SetBitmapBits( hbitmap
, height
* bmp
->bitmap
.bmWidthBytes
,
149 GDI_ReleaseObj( hbitmap
);
154 /******************************************************************************
155 * CreateCompatibleBitmap [GDI32.@] Creates a bitmap compatible with the DC
158 * hdc [I] Handle to device context
159 * width [I] Width of bitmap
160 * height [I] Height of bitmap
163 * Success: Handle to bitmap
166 HBITMAP WINAPI
CreateCompatibleBitmap( HDC hdc
, INT width
, INT height
)
171 TRACE("(%p,%d,%d) = \n", hdc
, width
, height
);
172 if (!(dc
= DC_GetDCPtr( hdc
))) return 0;
173 if ((width
>= 0x10000) || (height
>= 0x10000)) {
174 FIXME("got bad width %d or height %d, please look for reason\n",
181 if (GDIMAGIC( dc
->header
.wMagic
) != MEMORY_DC_MAGIC
)
183 planes
= GetDeviceCaps( hdc
, PLANES
);
184 bpp
= GetDeviceCaps( hdc
, BITSPIXEL
);
186 else /* memory DC, get the depth of the bitmap */
188 BITMAPOBJ
*bmp
= GDI_GetObjPtr( dc
->hBitmap
, BITMAP_MAGIC
);
189 planes
= bmp
->bitmap
.bmPlanes
;
190 bpp
= bmp
->bitmap
.bmBitsPixel
;
191 GDI_ReleaseObj( dc
->hBitmap
);
193 hbmpRet
= CreateBitmap( width
, height
, planes
, bpp
, NULL
);
195 TRACE("\t\t%p\n", hbmpRet
);
201 /******************************************************************************
202 * CreateBitmapIndirect [GDI32.@] Creates a bitmap with the specifies info
205 * Success: Handle to bitmap
208 HBITMAP WINAPI
CreateBitmapIndirect(
209 const BITMAP
* bmp
) /* [in] Pointer to the bitmap data */
211 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
212 bmp
->bmBitsPixel
, bmp
->bmBits
);
216 /***********************************************************************
217 * GetBitmapBits [GDI32.@] Copies bitmap bits of bitmap to buffer
220 * Success: Number of bytes copied
223 LONG WINAPI
GetBitmapBits(
224 HBITMAP hbitmap
, /* [in] Handle to bitmap */
225 LONG count
, /* [in] Number of bytes to copy */
226 LPVOID bits
) /* [out] Pointer to buffer to receive bits */
228 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
233 /* If the bits vector is null, the function should return the read size */
236 ret
= bmp
->bitmap
.bmWidthBytes
* bmp
->bitmap
.bmHeight
;
241 WARN("(%ld): Negative number of bytes passed???\n", count
);
245 /* Only get entire lines */
246 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
247 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
248 count
= height
* bmp
->bitmap
.bmWidthBytes
;
251 WARN("Less than one entire line requested\n");
257 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
258 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
259 1 << bmp
->bitmap
.bmBitsPixel
, height
);
263 TRACE("Calling device specific BitmapBits\n");
264 if(bmp
->funcs
->pGetBitmapBits
)
265 ret
= bmp
->funcs
->pGetBitmapBits(hbitmap
, bits
, count
);
268 memset( bits
, 0, count
);
273 if(!bmp
->bitmap
.bmBits
) {
274 WARN("Bitmap is empty\n");
277 memcpy(bits
, bmp
->bitmap
.bmBits
, count
);
283 GDI_ReleaseObj( hbitmap
);
288 /******************************************************************************
289 * SetBitmapBits [GDI32.@] Sets bits of color data for a bitmap
292 * Success: Number of bytes used in setting the bitmap bits
295 LONG WINAPI
SetBitmapBits(
296 HBITMAP hbitmap
, /* [in] Handle to bitmap */
297 LONG count
, /* [in] Number of bytes in bitmap array */
298 LPCVOID bits
) /* [in] Address of array with bitmap bits */
300 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
303 if ((!bmp
) || (!bits
))
307 WARN("(%ld): Negative number of bytes passed???\n", count
);
311 /* Only get entire lines */
312 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
313 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
314 count
= height
* bmp
->bitmap
.bmWidthBytes
;
316 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
317 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
318 1 << bmp
->bitmap
.bmBitsPixel
, height
);
322 TRACE("Calling device specific BitmapBits\n");
323 if(bmp
->funcs
->pSetBitmapBits
)
324 ret
= bmp
->funcs
->pSetBitmapBits(hbitmap
, bits
, count
);
329 if(!bmp
->bitmap
.bmBits
) /* Alloc enough for entire bitmap */
330 bmp
->bitmap
.bmBits
= HeapAlloc( GetProcessHeap(), 0, count
);
331 if(!bmp
->bitmap
.bmBits
) {
332 WARN("Unable to allocate bit buffer\n");
335 memcpy(bmp
->bitmap
.bmBits
, bits
, count
);
340 GDI_ReleaseObj( hbitmap
);
344 /**********************************************************************
348 HBITMAP
BITMAP_CopyBitmap(HBITMAP hbitmap
)
350 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
358 res
= CreateBitmapIndirect(&bm
);
361 char *buf
= HeapAlloc( GetProcessHeap(), 0, bm
.bmWidthBytes
*
363 GetBitmapBits (hbitmap
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
364 SetBitmapBits (res
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
365 HeapFree( GetProcessHeap(), 0, buf
);
368 GDI_ReleaseObj( hbitmap
);
373 /***********************************************************************
376 * Set the type of DC that owns the bitmap. This is used when the
377 * bitmap is selected into a device to initialize the bitmap function
380 BOOL
BITMAP_SetOwnerDC( HBITMAP hbitmap
, DC
*dc
)
385 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
386 if (hbitmap
== GetStockObject(DEFAULT_BITMAP
)) return TRUE
;
388 if (!(bitmap
= GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
))) return FALSE
;
391 if (!bitmap
->funcs
) /* not owned by a DC yet */
393 if (dc
->funcs
->pCreateBitmap
) ret
= dc
->funcs
->pCreateBitmap( dc
->physDev
, hbitmap
);
394 if (ret
) bitmap
->funcs
= dc
->funcs
;
396 else if (bitmap
->funcs
!= dc
->funcs
)
398 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap
);
401 GDI_ReleaseObj( hbitmap
);
406 /***********************************************************************
407 * BITMAP_SelectObject
409 static HGDIOBJ
BITMAP_SelectObject( HGDIOBJ handle
, void *obj
, HDC hdc
)
412 BITMAPOBJ
*bitmap
= obj
;
413 DC
*dc
= DC_GetDCPtr( hdc
);
416 if (GetObjectType( hdc
) != OBJ_MEMDC
)
418 GDI_ReleaseObj( hdc
);
422 if (handle
== dc
->hBitmap
) goto done
; /* nothing to do */
424 if (bitmap
->header
.dwCount
&& (handle
!= GetStockObject(DEFAULT_BITMAP
)))
426 WARN( "Bitmap already selected in another DC\n" );
427 GDI_ReleaseObj( hdc
);
431 if (!bitmap
->funcs
&& !BITMAP_SetOwnerDC( handle
, dc
))
433 GDI_ReleaseObj( hdc
);
437 if (dc
->funcs
->pSelectBitmap
) handle
= dc
->funcs
->pSelectBitmap( dc
->physDev
, handle
);
441 dc
->hBitmap
= handle
;
442 dc
->totalExtent
.left
= 0;
443 dc
->totalExtent
.top
= 0;
444 dc
->totalExtent
.right
= bitmap
->bitmap
.bmWidth
;
445 dc
->totalExtent
.bottom
= bitmap
->bitmap
.bmHeight
;
446 dc
->flags
&= ~DC_DIRTY
;
447 SetRectRgn( dc
->hVisRgn
, 0, 0, bitmap
->bitmap
.bmWidth
, bitmap
->bitmap
.bmHeight
);
453 GDI_ReleaseObj( hdc
);
458 /***********************************************************************
459 * BITMAP_DeleteObject
461 static BOOL
BITMAP_DeleteObject( HGDIOBJ handle
, void *obj
)
463 BITMAPOBJ
* bmp
= obj
;
465 if (bmp
->funcs
&& bmp
->funcs
->pDeleteBitmap
)
466 bmp
->funcs
->pDeleteBitmap( handle
);
468 if( bmp
->bitmap
.bmBits
)
469 HeapFree( GetProcessHeap(), 0, bmp
->bitmap
.bmBits
);
473 DIBSECTION
*dib
= bmp
->dib
;
475 if (dib
->dsBm
.bmBits
)
479 SYSTEM_INFO SystemInfo
;
480 GetSystemInfo( &SystemInfo
);
481 UnmapViewOfFile( (char *)dib
->dsBm
.bmBits
-
482 (dib
->dsOffset
% SystemInfo
.dwAllocationGranularity
) );
484 else if (!dib
->dsOffset
)
485 VirtualFree(dib
->dsBm
.bmBits
, 0L, MEM_RELEASE
);
487 HeapFree(GetProcessHeap(), 0, dib
);
489 if (bmp
->segptr_bits
)
490 { /* free its selector array */
491 WORD sel
= SELECTOROF(bmp
->segptr_bits
);
492 WORD count
= (GetSelectorLimit16(sel
) / 0x10000) + 1;
495 for (i
= 0; i
< count
; i
++) FreeSelector16(sel
+ (i
<< __AHSHIFT
));
498 return GDI_FreeObject( handle
, obj
);
502 /***********************************************************************
505 static INT
BITMAP_GetObject16( HGDIOBJ handle
, void *obj
, INT count
, LPVOID buffer
)
507 BITMAPOBJ
*bmp
= obj
;
511 if ( count
<= sizeof(BITMAP16
) )
513 BITMAP
*bmp32
= &bmp
->dib
->dsBm
;
515 bmp16
.bmType
= bmp32
->bmType
;
516 bmp16
.bmWidth
= bmp32
->bmWidth
;
517 bmp16
.bmHeight
= bmp32
->bmHeight
;
518 bmp16
.bmWidthBytes
= bmp32
->bmWidthBytes
;
519 bmp16
.bmPlanes
= bmp32
->bmPlanes
;
520 bmp16
.bmBitsPixel
= bmp32
->bmBitsPixel
;
521 bmp16
.bmBits
= (SEGPTR
)0;
522 memcpy( buffer
, &bmp16
, count
);
527 FIXME("not implemented for DIBs: count %d\n", count
);
534 bmp16
.bmType
= bmp
->bitmap
.bmType
;
535 bmp16
.bmWidth
= bmp
->bitmap
.bmWidth
;
536 bmp16
.bmHeight
= bmp
->bitmap
.bmHeight
;
537 bmp16
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
538 bmp16
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
539 bmp16
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
540 bmp16
.bmBits
= (SEGPTR
)0;
541 if (count
> sizeof(bmp16
)) count
= sizeof(bmp16
);
542 memcpy( buffer
, &bmp16
, count
);
548 /***********************************************************************
551 static INT
BITMAP_GetObject( HGDIOBJ handle
, void *obj
, INT count
, LPVOID buffer
)
553 BITMAPOBJ
*bmp
= obj
;
558 return sizeof(DIBSECTION
);
559 if (count
< sizeof(DIBSECTION
))
561 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
565 if (count
> sizeof(DIBSECTION
)) count
= sizeof(DIBSECTION
);
568 memcpy( buffer
, bmp
->dib
, count
);
574 return sizeof(BITMAP
);
575 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
576 memcpy( buffer
, &bmp
->bitmap
, count
);
582 /******************************************************************************
583 * CreateDiscardableBitmap [GDI32.@] Creates a discardable bitmap
586 * Success: Handle to bitmap
589 HBITMAP WINAPI
CreateDiscardableBitmap(
590 HDC hdc
, /* [in] Handle to device context */
591 INT width
, /* [in] Bitmap width */
592 INT height
) /* [in] Bitmap height */
594 return CreateCompatibleBitmap( hdc
, width
, height
);
598 /******************************************************************************
599 * GetBitmapDimensionEx [GDI32.@] Retrieves dimensions of a bitmap
605 BOOL WINAPI
GetBitmapDimensionEx(
606 HBITMAP hbitmap
, /* [in] Handle to bitmap */
607 LPSIZE size
) /* [out] Address of struct receiving dimensions */
609 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
610 if (!bmp
) return FALSE
;
612 GDI_ReleaseObj( hbitmap
);
617 /******************************************************************************
618 * SetBitmapDimensionEx [GDI32.@] Assignes dimensions to a bitmap
624 BOOL WINAPI
SetBitmapDimensionEx(
625 HBITMAP hbitmap
, /* [in] Handle to bitmap */
626 INT x
, /* [in] Bitmap width */
627 INT y
, /* [in] Bitmap height */
628 LPSIZE prevSize
) /* [out] Address of structure for orig dims */
630 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
631 if (!bmp
) return FALSE
;
632 if (prevSize
) *prevSize
= bmp
->size
;
635 GDI_ReleaseObj( hbitmap
);