Removed the bitsPerPixel field in the generic DC structure, and leave
[wine/multimedia.git] / objects / bitmap.c
blob64d59e6b4973d2c284945a7fad0a948fcfff4d06
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdlib.h>
23 #include <string.h>
25 #include "wine/winbase16.h"
26 #include "wine/winuser16.h"
27 #include "gdi.h"
28 #include "bitmap.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
54 * data.
56 static INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
58 switch(bpp)
60 case 1:
61 return 2 * ((bmWidth+15) >> 4);
63 case 24:
64 bmWidth *= 3; /* fall through */
65 case 8:
66 return bmWidth + (bmWidth & 1);
68 case 32:
69 return bmWidth * 4;
71 case 16:
72 case 15:
73 return bmWidth * 2;
75 case 4:
76 return 2 * ((bmWidth+3) >> 2);
78 default:
79 WARN("Unknown depth %d, please report.\n", bpp );
81 return -1;
85 /******************************************************************************
86 * CreateBitmap [GDI32.@] Creates a bitmap with the specified info
88 * PARAMS
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
95 * RETURNS
96 * Success: Handle to bitmap
97 * Failure: 0
99 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
100 UINT bpp, LPCVOID bits )
102 BITMAPOBJ *bmp;
103 HBITMAP hbitmap;
105 planes = (BYTE)planes;
106 bpp = (BYTE)bpp;
109 /* Check parameters */
110 if (!height || !width)
112 height = 1;
113 width = 1;
114 planes = 1;
115 bpp = 1;
117 if (planes != 1) {
118 FIXME("planes = %d\n", planes);
119 return 0;
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 )))
127 return 0;
129 TRACE("%dx%d, %d colors returning %p\n", width, height, 1 << (planes*bpp), hbitmap);
131 bmp->size.cx = 0;
132 bmp->size.cy = 0;
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;
141 bmp->funcs = NULL;
142 bmp->physBitmap = NULL;
143 bmp->dib = NULL;
144 bmp->segptr_bits = 0;
146 if (bits) /* Set bitmap bits */
147 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
148 bits );
149 GDI_ReleaseObj( hbitmap );
150 return hbitmap;
154 /******************************************************************************
155 * CreateCompatibleBitmap [GDI32.@] Creates a bitmap compatible with the DC
157 * PARAMS
158 * hdc [I] Handle to device context
159 * width [I] Width of bitmap
160 * height [I] Height of bitmap
162 * RETURNS
163 * Success: Handle to bitmap
164 * Failure: 0
166 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
168 HBITMAP hbmpRet = 0;
169 DC *dc;
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",
175 width, height );
177 else
179 INT planes, bpp;
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);
196 GDI_ReleaseObj(hdc);
197 return hbmpRet;
201 /******************************************************************************
202 * CreateBitmapIndirect [GDI32.@] Creates a bitmap with the specifies info
204 * RETURNS
205 * Success: Handle to bitmap
206 * Failure: NULL
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
219 * RETURNS
220 * Success: Number of bytes copied
221 * Failure: 0
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 );
229 LONG height, ret;
231 if (!bmp) return 0;
233 /* If the bits vector is null, the function should return the read size */
234 if(bits == NULL)
236 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
237 goto done;
240 if (count < 0) {
241 WARN("(%ld): Negative number of bytes passed???\n", count );
242 count = -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;
249 if (count == 0)
251 WARN("Less than one entire line requested\n");
252 ret = 0;
253 goto done;
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 );
261 if(bmp->funcs)
263 TRACE("Calling device specific BitmapBits\n");
264 if(bmp->funcs->pGetBitmapBits)
265 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
266 else
268 memset( bits, 0, count );
269 ret = count;
271 } else {
273 if(!bmp->bitmap.bmBits) {
274 WARN("Bitmap is empty\n");
275 ret = 0;
276 } else {
277 memcpy(bits, bmp->bitmap.bmBits, count);
278 ret = count;
282 done:
283 GDI_ReleaseObj( hbitmap );
284 return ret;
288 /******************************************************************************
289 * SetBitmapBits [GDI32.@] Sets bits of color data for a bitmap
291 * RETURNS
292 * Success: Number of bytes used in setting the bitmap bits
293 * Failure: 0
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 );
301 LONG height, ret;
303 if ((!bmp) || (!bits))
304 return 0;
306 if (count < 0) {
307 WARN("(%ld): Negative number of bytes passed???\n", count );
308 count = -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 );
320 if(bmp->funcs) {
322 TRACE("Calling device specific BitmapBits\n");
323 if(bmp->funcs->pSetBitmapBits)
324 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
325 else
326 ret = 0;
327 } else {
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");
333 ret = 0;
334 } else {
335 memcpy(bmp->bitmap.bmBits, bits, count);
336 ret = count;
340 GDI_ReleaseObj( hbitmap );
341 return ret;
344 /**********************************************************************
345 * BITMAP_CopyBitmap
348 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
350 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
351 HBITMAP res = 0;
352 BITMAP bm;
354 if(!bmp) return 0;
356 bm = bmp->bitmap;
357 bm.bmBits = NULL;
358 res = CreateBitmapIndirect(&bm);
360 if(res) {
361 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
362 bm.bmHeight );
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 );
369 return res;
373 /***********************************************************************
374 * BITMAP_SetOwnerDC
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
378 * table.
380 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
382 BITMAPOBJ *bitmap;
383 BOOL ret;
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;
390 ret = TRUE;
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 );
399 ret = FALSE;
401 GDI_ReleaseObj( hbitmap );
402 return ret;
406 /***********************************************************************
407 * BITMAP_SelectObject
409 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
411 HGDIOBJ ret;
412 BITMAPOBJ *bitmap = obj;
413 DC *dc = DC_GetDCPtr( hdc );
415 if (!dc) return 0;
416 if (GetObjectType( hdc ) != OBJ_MEMDC)
418 GDI_ReleaseObj( hdc );
419 return 0;
421 ret = dc->hBitmap;
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 );
428 return 0;
431 if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
433 GDI_ReleaseObj( hdc );
434 return 0;
437 if (dc->funcs->pSelectBitmap) handle = dc->funcs->pSelectBitmap( dc->physDev, handle );
439 if (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);
448 DC_InitDC( dc );
450 else ret = 0;
452 done:
453 GDI_ReleaseObj( hdc );
454 return ret;
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 );
471 if (bmp->dib)
473 DIBSECTION *dib = bmp->dib;
475 if (dib->dsBm.bmBits)
477 if (dib->dshSection)
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);
488 bmp->dib = NULL;
489 if (bmp->segptr_bits)
490 { /* free its selector array */
491 WORD sel = SELECTOROF(bmp->segptr_bits);
492 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
493 int i;
495 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
498 return GDI_FreeObject( handle, obj );
502 /***********************************************************************
503 * BITMAP_GetObject16
505 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
507 BITMAPOBJ *bmp = obj;
509 if (bmp->dib)
511 if ( count <= sizeof(BITMAP16) )
513 BITMAP *bmp32 = &bmp->dib->dsBm;
514 BITMAP16 bmp16;
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 );
523 return count;
525 else
527 FIXME("not implemented for DIBs: count %d\n", count);
528 return 0;
531 else
533 BITMAP16 bmp16;
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 );
543 return count;
548 /***********************************************************************
549 * BITMAP_GetObject
551 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
553 BITMAPOBJ *bmp = obj;
555 if (bmp->dib)
557 if( !buffer )
558 return sizeof(DIBSECTION);
559 if (count < sizeof(DIBSECTION))
561 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
563 else
565 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
568 memcpy( buffer, bmp->dib, count );
569 return count;
571 else
573 if( !buffer )
574 return sizeof(BITMAP);
575 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
576 memcpy( buffer, &bmp->bitmap, count );
577 return count;
582 /******************************************************************************
583 * CreateDiscardableBitmap [GDI32.@] Creates a discardable bitmap
585 * RETURNS
586 * Success: Handle to bitmap
587 * Failure: NULL
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
601 * RETURNS
602 * Success: TRUE
603 * Failure: FALSE
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;
611 *size = bmp->size;
612 GDI_ReleaseObj( hbitmap );
613 return TRUE;
617 /******************************************************************************
618 * SetBitmapDimensionEx [GDI32.@] Assignes dimensions to a bitmap
620 * RETURNS
621 * Success: TRUE
622 * Failure: FALSE
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;
633 bmp->size.cx = x;
634 bmp->size.cy = y;
635 GDI_ReleaseObj( hbitmap );
636 return TRUE;