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.@]
88 * Creates a bitmap with the specified info.
91 * width [I] bitmap width
92 * height [I] bitmap height
93 * planes [I] Number of color planes
94 * bpp [I] Number of bits to identify a color
95 * bits [I] Pointer to array containing color data
98 * Success: Handle to bitmap
101 HBITMAP WINAPI
CreateBitmap( INT width
, INT height
, UINT planes
,
102 UINT bpp
, LPCVOID bits
)
107 planes
= (BYTE
)planes
;
111 /* Check parameters */
112 if (!height
|| !width
)
120 FIXME("planes = %d\n", planes
);
123 if (height
< 0) height
= -height
;
124 if (width
< 0) width
= -width
;
126 /* Create the BITMAPOBJ */
127 if (!(bmp
= GDI_AllocObject( sizeof(BITMAPOBJ
), BITMAP_MAGIC
,
128 (HGDIOBJ
*)&hbitmap
, &bitmap_funcs
)))
131 TRACE("%dx%d, %d colors returning %p\n", width
, height
, 1 << (planes
*bpp
), hbitmap
);
135 bmp
->bitmap
.bmType
= 0;
136 bmp
->bitmap
.bmWidth
= width
;
137 bmp
->bitmap
.bmHeight
= height
;
138 bmp
->bitmap
.bmPlanes
= planes
;
139 bmp
->bitmap
.bmBitsPixel
= bpp
;
140 bmp
->bitmap
.bmWidthBytes
= BITMAP_GetWidthBytes( width
, bpp
);
141 bmp
->bitmap
.bmBits
= NULL
;
144 bmp
->physBitmap
= NULL
;
146 bmp
->segptr_bits
= 0;
148 if (bits
) /* Set bitmap bits */
149 SetBitmapBits( hbitmap
, height
* bmp
->bitmap
.bmWidthBytes
,
151 GDI_ReleaseObj( hbitmap
);
156 /******************************************************************************
157 * CreateCompatibleBitmap [GDI32.@]
159 * Creates a bitmap compatible with the DC.
162 * hdc [I] Handle to device context
163 * width [I] Width of bitmap
164 * height [I] Height of bitmap
167 * Success: Handle to bitmap
170 HBITMAP WINAPI
CreateCompatibleBitmap( HDC hdc
, INT width
, INT height
)
175 TRACE("(%p,%d,%d) = \n", hdc
, width
, height
);
176 if (!(dc
= DC_GetDCPtr( hdc
))) return 0;
177 if ((width
>= 0x10000) || (height
>= 0x10000)) {
178 FIXME("got bad width %d or height %d, please look for reason\n",
185 if (GDIMAGIC( dc
->header
.wMagic
) != MEMORY_DC_MAGIC
)
187 planes
= GetDeviceCaps( hdc
, PLANES
);
188 bpp
= GetDeviceCaps( hdc
, BITSPIXEL
);
190 else /* memory DC, get the depth of the bitmap */
192 BITMAPOBJ
*bmp
= GDI_GetObjPtr( dc
->hBitmap
, BITMAP_MAGIC
);
193 planes
= bmp
->bitmap
.bmPlanes
;
194 bpp
= bmp
->bitmap
.bmBitsPixel
;
195 GDI_ReleaseObj( dc
->hBitmap
);
197 hbmpRet
= CreateBitmap( width
, height
, planes
, bpp
, NULL
);
199 TRACE("\t\t%p\n", hbmpRet
);
205 /******************************************************************************
206 * CreateBitmapIndirect [GDI32.@]
208 * Creates a bitmap with the specifies info.
211 * Success: Handle to bitmap
214 HBITMAP WINAPI
CreateBitmapIndirect(
215 const BITMAP
* bmp
) /* [in] Pointer to the bitmap data */
217 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
218 bmp
->bmBitsPixel
, bmp
->bmBits
);
222 /***********************************************************************
223 * GetBitmapBits [GDI32.@]
225 * Copies bitmap bits of bitmap to buffer.
228 * Success: Number of bytes copied
231 LONG WINAPI
GetBitmapBits(
232 HBITMAP hbitmap
, /* [in] Handle to bitmap */
233 LONG count
, /* [in] Number of bytes to copy */
234 LPVOID bits
) /* [out] Pointer to buffer to receive bits */
236 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
241 /* If the bits vector is null, the function should return the read size */
244 ret
= bmp
->bitmap
.bmWidthBytes
* bmp
->bitmap
.bmHeight
;
249 WARN("(%ld): Negative number of bytes passed???\n", count
);
253 /* Only get entire lines */
254 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
255 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
256 count
= height
* bmp
->bitmap
.bmWidthBytes
;
259 WARN("Less than one entire line requested\n");
265 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
266 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
267 1 << bmp
->bitmap
.bmBitsPixel
, height
);
271 TRACE("Calling device specific BitmapBits\n");
272 if(bmp
->funcs
->pGetBitmapBits
)
273 ret
= bmp
->funcs
->pGetBitmapBits(hbitmap
, bits
, count
);
276 memset( bits
, 0, count
);
281 if(!bmp
->bitmap
.bmBits
) {
282 WARN("Bitmap is empty\n");
285 memcpy(bits
, bmp
->bitmap
.bmBits
, count
);
291 GDI_ReleaseObj( hbitmap
);
296 /******************************************************************************
297 * SetBitmapBits [GDI32.@]
299 * Sets bits of color data for a bitmap.
302 * Success: Number of bytes used in setting the bitmap bits
305 LONG WINAPI
SetBitmapBits(
306 HBITMAP hbitmap
, /* [in] Handle to bitmap */
307 LONG count
, /* [in] Number of bytes in bitmap array */
308 LPCVOID bits
) /* [in] Address of array with bitmap bits */
310 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
313 if ((!bmp
) || (!bits
))
317 WARN("(%ld): Negative number of bytes passed???\n", count
);
321 /* Only get entire lines */
322 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
323 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
324 count
= height
* bmp
->bitmap
.bmWidthBytes
;
326 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
327 hbitmap
, count
, bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
328 1 << bmp
->bitmap
.bmBitsPixel
, height
);
332 TRACE("Calling device specific BitmapBits\n");
333 if(bmp
->funcs
->pSetBitmapBits
)
334 ret
= bmp
->funcs
->pSetBitmapBits(hbitmap
, bits
, count
);
339 if(!bmp
->bitmap
.bmBits
) /* Alloc enough for entire bitmap */
340 bmp
->bitmap
.bmBits
= HeapAlloc( GetProcessHeap(), 0, count
);
341 if(!bmp
->bitmap
.bmBits
) {
342 WARN("Unable to allocate bit buffer\n");
345 memcpy(bmp
->bitmap
.bmBits
, bits
, count
);
350 GDI_ReleaseObj( hbitmap
);
354 /**********************************************************************
358 HBITMAP
BITMAP_CopyBitmap(HBITMAP hbitmap
)
360 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
368 res
= CreateBitmapIndirect(&bm
);
371 char *buf
= HeapAlloc( GetProcessHeap(), 0, bm
.bmWidthBytes
*
373 GetBitmapBits (hbitmap
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
374 SetBitmapBits (res
, bm
.bmWidthBytes
* bm
.bmHeight
, buf
);
375 HeapFree( GetProcessHeap(), 0, buf
);
378 GDI_ReleaseObj( hbitmap
);
383 /***********************************************************************
386 * Set the type of DC that owns the bitmap. This is used when the
387 * bitmap is selected into a device to initialize the bitmap function
390 BOOL
BITMAP_SetOwnerDC( HBITMAP hbitmap
, DC
*dc
)
395 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
396 if (hbitmap
== GetStockObject(DEFAULT_BITMAP
)) return TRUE
;
398 if (!(bitmap
= GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
))) return FALSE
;
401 if (!bitmap
->funcs
) /* not owned by a DC yet */
403 if (dc
->funcs
->pCreateBitmap
) ret
= dc
->funcs
->pCreateBitmap( dc
->physDev
, hbitmap
);
404 if (ret
) bitmap
->funcs
= dc
->funcs
;
406 else if (bitmap
->funcs
!= dc
->funcs
)
408 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap
);
411 GDI_ReleaseObj( hbitmap
);
416 /***********************************************************************
417 * BITMAP_SelectObject
419 static HGDIOBJ
BITMAP_SelectObject( HGDIOBJ handle
, void *obj
, HDC hdc
)
422 BITMAPOBJ
*bitmap
= obj
;
423 DC
*dc
= DC_GetDCPtr( hdc
);
426 if (GetObjectType( hdc
) != OBJ_MEMDC
)
428 GDI_ReleaseObj( hdc
);
432 if (handle
== dc
->hBitmap
) goto done
; /* nothing to do */
434 if (bitmap
->header
.dwCount
&& (handle
!= GetStockObject(DEFAULT_BITMAP
)))
436 WARN( "Bitmap already selected in another DC\n" );
437 GDI_ReleaseObj( hdc
);
441 if (!bitmap
->funcs
&& !BITMAP_SetOwnerDC( handle
, dc
))
443 GDI_ReleaseObj( hdc
);
447 if (dc
->funcs
->pSelectBitmap
) handle
= dc
->funcs
->pSelectBitmap( dc
->physDev
, handle
);
451 dc
->hBitmap
= handle
;
452 dc
->totalExtent
.left
= 0;
453 dc
->totalExtent
.top
= 0;
454 dc
->totalExtent
.right
= bitmap
->bitmap
.bmWidth
;
455 dc
->totalExtent
.bottom
= bitmap
->bitmap
.bmHeight
;
456 dc
->flags
&= ~DC_DIRTY
;
457 SetRectRgn( dc
->hVisRgn
, 0, 0, bitmap
->bitmap
.bmWidth
, bitmap
->bitmap
.bmHeight
);
463 GDI_ReleaseObj( hdc
);
468 /***********************************************************************
469 * BITMAP_DeleteObject
471 static BOOL
BITMAP_DeleteObject( HGDIOBJ handle
, void *obj
)
473 BITMAPOBJ
* bmp
= obj
;
475 if (bmp
->funcs
&& bmp
->funcs
->pDeleteBitmap
)
476 bmp
->funcs
->pDeleteBitmap( handle
);
478 if( bmp
->bitmap
.bmBits
)
479 HeapFree( GetProcessHeap(), 0, bmp
->bitmap
.bmBits
);
483 DIBSECTION
*dib
= bmp
->dib
;
485 if (dib
->dsBm
.bmBits
)
489 SYSTEM_INFO SystemInfo
;
490 GetSystemInfo( &SystemInfo
);
491 UnmapViewOfFile( (char *)dib
->dsBm
.bmBits
-
492 (dib
->dsOffset
% SystemInfo
.dwAllocationGranularity
) );
494 else if (!dib
->dsOffset
)
495 VirtualFree(dib
->dsBm
.bmBits
, 0L, MEM_RELEASE
);
497 HeapFree(GetProcessHeap(), 0, dib
);
499 if (bmp
->segptr_bits
)
500 { /* free its selector array */
501 WORD sel
= SELECTOROF(bmp
->segptr_bits
);
502 WORD count
= (GetSelectorLimit16(sel
) / 0x10000) + 1;
505 for (i
= 0; i
< count
; i
++) FreeSelector16(sel
+ (i
<< __AHSHIFT
));
508 return GDI_FreeObject( handle
, obj
);
512 /***********************************************************************
515 static INT
BITMAP_GetObject16( HGDIOBJ handle
, void *obj
, INT count
, LPVOID buffer
)
517 BITMAPOBJ
*bmp
= obj
;
521 if ( count
<= sizeof(BITMAP16
) )
523 BITMAP
*bmp32
= &bmp
->dib
->dsBm
;
525 bmp16
.bmType
= bmp32
->bmType
;
526 bmp16
.bmWidth
= bmp32
->bmWidth
;
527 bmp16
.bmHeight
= bmp32
->bmHeight
;
528 bmp16
.bmWidthBytes
= bmp32
->bmWidthBytes
;
529 bmp16
.bmPlanes
= bmp32
->bmPlanes
;
530 bmp16
.bmBitsPixel
= bmp32
->bmBitsPixel
;
531 bmp16
.bmBits
= (SEGPTR
)0;
532 memcpy( buffer
, &bmp16
, count
);
537 FIXME("not implemented for DIBs: count %d\n", count
);
544 bmp16
.bmType
= bmp
->bitmap
.bmType
;
545 bmp16
.bmWidth
= bmp
->bitmap
.bmWidth
;
546 bmp16
.bmHeight
= bmp
->bitmap
.bmHeight
;
547 bmp16
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
548 bmp16
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
549 bmp16
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
550 bmp16
.bmBits
= (SEGPTR
)0;
551 if (count
> sizeof(bmp16
)) count
= sizeof(bmp16
);
552 memcpy( buffer
, &bmp16
, count
);
558 /***********************************************************************
561 static INT
BITMAP_GetObject( HGDIOBJ handle
, void *obj
, INT count
, LPVOID buffer
)
563 BITMAPOBJ
*bmp
= obj
;
568 return sizeof(DIBSECTION
);
569 if (count
< sizeof(DIBSECTION
))
571 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
575 if (count
> sizeof(DIBSECTION
)) count
= sizeof(DIBSECTION
);
578 memcpy( buffer
, bmp
->dib
, count
);
584 return sizeof(BITMAP
);
585 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
586 memcpy( buffer
, &bmp
->bitmap
, count
);
592 /******************************************************************************
593 * CreateDiscardableBitmap [GDI32.@]
595 * Creates a discardable bitmap.
598 * Success: Handle to bitmap
601 HBITMAP WINAPI
CreateDiscardableBitmap(
602 HDC hdc
, /* [in] Handle to device context */
603 INT width
, /* [in] Bitmap width */
604 INT height
) /* [in] Bitmap height */
606 return CreateCompatibleBitmap( hdc
, width
, height
);
610 /******************************************************************************
611 * GetBitmapDimensionEx [GDI32.@]
613 * Retrieves dimensions of a bitmap.
619 BOOL WINAPI
GetBitmapDimensionEx(
620 HBITMAP hbitmap
, /* [in] Handle to bitmap */
621 LPSIZE size
) /* [out] Address of struct receiving dimensions */
623 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
624 if (!bmp
) return FALSE
;
626 GDI_ReleaseObj( hbitmap
);
631 /******************************************************************************
632 * SetBitmapDimensionEx [GDI32.@]
634 * Assigns dimensions to a bitmap.
640 BOOL WINAPI
SetBitmapDimensionEx(
641 HBITMAP hbitmap
, /* [in] Handle to bitmap */
642 INT x
, /* [in] Bitmap width */
643 INT y
, /* [in] Bitmap height */
644 LPSIZE prevSize
) /* [out] Address of structure for orig dims */
646 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
647 if (!bmp
) return FALSE
;
648 if (prevSize
) *prevSize
= bmp
->size
;
651 GDI_ReleaseObj( hbitmap
);