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
->flags
&= ~DC_DIRTY
;
453 SetRectRgn( dc
->hVisRgn
, 0, 0, bitmap
->bitmap
.bmWidth
, bitmap
->bitmap
.bmHeight
);
459 GDI_ReleaseObj( hdc
);
464 /***********************************************************************
465 * BITMAP_DeleteObject
467 static BOOL
BITMAP_DeleteObject( HGDIOBJ handle
, void *obj
)
469 BITMAPOBJ
* bmp
= obj
;
471 if (bmp
->funcs
&& bmp
->funcs
->pDeleteBitmap
)
472 bmp
->funcs
->pDeleteBitmap( handle
);
474 if( bmp
->bitmap
.bmBits
)
475 HeapFree( GetProcessHeap(), 0, bmp
->bitmap
.bmBits
);
479 DIBSECTION
*dib
= bmp
->dib
;
481 if (dib
->dsBm
.bmBits
)
485 SYSTEM_INFO SystemInfo
;
486 GetSystemInfo( &SystemInfo
);
487 UnmapViewOfFile( (char *)dib
->dsBm
.bmBits
-
488 (dib
->dsOffset
% SystemInfo
.dwAllocationGranularity
) );
490 else if (!dib
->dsOffset
)
491 VirtualFree(dib
->dsBm
.bmBits
, 0L, MEM_RELEASE
);
493 HeapFree(GetProcessHeap(), 0, dib
);
495 if (bmp
->segptr_bits
)
496 { /* free its selector array */
497 WORD sel
= SELECTOROF(bmp
->segptr_bits
);
498 WORD count
= (GetSelectorLimit16(sel
) / 0x10000) + 1;
501 for (i
= 0; i
< count
; i
++) FreeSelector16(sel
+ (i
<< __AHSHIFT
));
504 return GDI_FreeObject( handle
, obj
);
508 /***********************************************************************
511 static INT
BITMAP_GetObject16( HGDIOBJ handle
, void *obj
, INT count
, LPVOID buffer
)
513 BITMAPOBJ
*bmp
= obj
;
517 if ( count
<= sizeof(BITMAP16
) )
519 BITMAP
*bmp32
= &bmp
->dib
->dsBm
;
521 bmp16
.bmType
= bmp32
->bmType
;
522 bmp16
.bmWidth
= bmp32
->bmWidth
;
523 bmp16
.bmHeight
= bmp32
->bmHeight
;
524 bmp16
.bmWidthBytes
= bmp32
->bmWidthBytes
;
525 bmp16
.bmPlanes
= bmp32
->bmPlanes
;
526 bmp16
.bmBitsPixel
= bmp32
->bmBitsPixel
;
527 bmp16
.bmBits
= (SEGPTR
)0;
528 memcpy( buffer
, &bmp16
, count
);
533 FIXME("not implemented for DIBs: count %d\n", count
);
540 bmp16
.bmType
= bmp
->bitmap
.bmType
;
541 bmp16
.bmWidth
= bmp
->bitmap
.bmWidth
;
542 bmp16
.bmHeight
= bmp
->bitmap
.bmHeight
;
543 bmp16
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
544 bmp16
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
545 bmp16
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
546 bmp16
.bmBits
= (SEGPTR
)0;
547 if (count
> sizeof(bmp16
)) count
= sizeof(bmp16
);
548 memcpy( buffer
, &bmp16
, count
);
554 /***********************************************************************
557 static INT
BITMAP_GetObject( HGDIOBJ handle
, void *obj
, INT count
, LPVOID buffer
)
559 BITMAPOBJ
*bmp
= obj
;
564 return sizeof(DIBSECTION
);
565 if (count
< sizeof(DIBSECTION
))
567 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
571 if (count
> sizeof(DIBSECTION
)) count
= sizeof(DIBSECTION
);
574 memcpy( buffer
, bmp
->dib
, count
);
580 return sizeof(BITMAP
);
581 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
582 memcpy( buffer
, &bmp
->bitmap
, count
);
588 /******************************************************************************
589 * CreateDiscardableBitmap [GDI32.@]
591 * Creates a discardable bitmap.
594 * Success: Handle to bitmap
597 HBITMAP WINAPI
CreateDiscardableBitmap(
598 HDC hdc
, /* [in] Handle to device context */
599 INT width
, /* [in] Bitmap width */
600 INT height
) /* [in] Bitmap height */
602 return CreateCompatibleBitmap( hdc
, width
, height
);
606 /******************************************************************************
607 * GetBitmapDimensionEx [GDI32.@]
609 * Retrieves dimensions of a bitmap.
615 BOOL WINAPI
GetBitmapDimensionEx(
616 HBITMAP hbitmap
, /* [in] Handle to bitmap */
617 LPSIZE size
) /* [out] Address of struct receiving dimensions */
619 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
620 if (!bmp
) return FALSE
;
622 GDI_ReleaseObj( hbitmap
);
627 /******************************************************************************
628 * SetBitmapDimensionEx [GDI32.@]
630 * Assigns dimensions to a bitmap.
636 BOOL WINAPI
SetBitmapDimensionEx(
637 HBITMAP hbitmap
, /* [in] Handle to bitmap */
638 INT x
, /* [in] Bitmap width */
639 INT y
, /* [in] Bitmap height */
640 LPSIZE prevSize
) /* [out] Address of structure for orig dims */
642 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
643 if (!bmp
) return FALSE
;
644 if (prevSize
) *prevSize
= bmp
->size
;
647 GDI_ReleaseObj( hbitmap
);