Some -DSTRICT fixes.
[wine/multimedia.git] / objects / bitmap.c
blob60f05d6bfa875aee4ac08362aac2092e33b0598b
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 "gdi.h"
27 #include "bitmap.h"
28 #include "selectors.h"
29 #include "wine/debug.h"
30 #include "wine/winuser16.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 %08x\n", width, height,
130 1 << (planes*bpp), hbitmap);
132 bmp->size.cx = 0;
133 bmp->size.cy = 0;
134 bmp->bitmap.bmType = 0;
135 bmp->bitmap.bmWidth = width;
136 bmp->bitmap.bmHeight = height;
137 bmp->bitmap.bmPlanes = planes;
138 bmp->bitmap.bmBitsPixel = bpp;
139 bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
140 bmp->bitmap.bmBits = NULL;
142 bmp->funcs = NULL;
143 bmp->physBitmap = NULL;
144 bmp->dib = NULL;
145 bmp->segptr_bits = 0;
147 if (bits) /* Set bitmap bits */
148 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
149 bits );
150 GDI_ReleaseObj( hbitmap );
151 return hbitmap;
155 /******************************************************************************
156 * CreateCompatibleBitmap [GDI32.@] Creates a bitmap compatible with the DC
158 * PARAMS
159 * hdc [I] Handle to device context
160 * width [I] Width of bitmap
161 * height [I] Height of bitmap
163 * RETURNS
164 * Success: Handle to bitmap
165 * Failure: 0
167 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
169 HBITMAP hbmpRet = 0;
170 DC *dc;
172 TRACE("(%04x,%d,%d) = \n", hdc, width, height );
173 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
174 if ((width >= 0x10000) || (height >= 0x10000)) {
175 FIXME("got bad width %d or height %d, please look for reason\n",
176 width, height );
177 } else {
178 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
179 if (!width || !height)
180 hbmpRet = CreateBitmap( 1, 1, 1, 1, NULL );
181 else
182 hbmpRet = CreateBitmap( width, height, 1, dc->bitsPerPixel, NULL );
184 if (!BITMAP_SetOwnerDC( hbmpRet, dc ))
186 DeleteObject( hbmpRet );
187 hbmpRet = 0;
190 TRACE("\t\t%04x\n", hbmpRet);
191 GDI_ReleaseObj(hdc);
192 return hbmpRet;
196 /******************************************************************************
197 * CreateBitmapIndirect [GDI32.@] Creates a bitmap with the specifies info
199 * RETURNS
200 * Success: Handle to bitmap
201 * Failure: NULL
203 HBITMAP WINAPI CreateBitmapIndirect(
204 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
206 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
207 bmp->bmBitsPixel, bmp->bmBits );
211 /***********************************************************************
212 * GetBitmapBits [GDI32.@] Copies bitmap bits of bitmap to buffer
214 * RETURNS
215 * Success: Number of bytes copied
216 * Failure: 0
218 LONG WINAPI GetBitmapBits(
219 HBITMAP hbitmap, /* [in] Handle to bitmap */
220 LONG count, /* [in] Number of bytes to copy */
221 LPVOID bits) /* [out] Pointer to buffer to receive bits */
223 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
224 LONG height, ret;
226 if (!bmp) return 0;
228 /* If the bits vector is null, the function should return the read size */
229 if(bits == NULL)
231 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
232 goto done;
235 if (count < 0) {
236 WARN("(%ld): Negative number of bytes passed???\n", count );
237 count = -count;
240 /* Only get entire lines */
241 height = count / bmp->bitmap.bmWidthBytes;
242 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
243 count = height * bmp->bitmap.bmWidthBytes;
244 if (count == 0)
246 WARN("Less than one entire line requested\n");
247 ret = 0;
248 goto done;
252 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
253 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
254 1 << bmp->bitmap.bmBitsPixel, height );
256 if(bmp->funcs)
258 TRACE("Calling device specific BitmapBits\n");
259 if(bmp->funcs->pGetBitmapBits)
260 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
261 else
263 memset( bits, 0, count );
264 ret = count;
266 } else {
268 if(!bmp->bitmap.bmBits) {
269 WARN("Bitmap is empty\n");
270 ret = 0;
271 } else {
272 memcpy(bits, bmp->bitmap.bmBits, count);
273 ret = count;
277 done:
278 GDI_ReleaseObj( hbitmap );
279 return ret;
283 /******************************************************************************
284 * SetBitmapBits [GDI32.@] Sets bits of color data for a bitmap
286 * RETURNS
287 * Success: Number of bytes used in setting the bitmap bits
288 * Failure: 0
290 LONG WINAPI SetBitmapBits(
291 HBITMAP hbitmap, /* [in] Handle to bitmap */
292 LONG count, /* [in] Number of bytes in bitmap array */
293 LPCVOID bits) /* [in] Address of array with bitmap bits */
295 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
296 LONG height, ret;
298 if ((!bmp) || (!bits))
299 return 0;
301 if (count < 0) {
302 WARN("(%ld): Negative number of bytes passed???\n", count );
303 count = -count;
306 /* Only get entire lines */
307 height = count / bmp->bitmap.bmWidthBytes;
308 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
309 count = height * bmp->bitmap.bmWidthBytes;
311 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
312 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
313 1 << bmp->bitmap.bmBitsPixel, height );
315 if(bmp->funcs) {
317 TRACE("Calling device specific BitmapBits\n");
318 if(bmp->funcs->pSetBitmapBits)
319 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
320 else
321 ret = 0;
322 } else {
324 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
325 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
326 if(!bmp->bitmap.bmBits) {
327 WARN("Unable to allocate bit buffer\n");
328 ret = 0;
329 } else {
330 memcpy(bmp->bitmap.bmBits, bits, count);
331 ret = count;
335 GDI_ReleaseObj( hbitmap );
336 return ret;
339 /**********************************************************************
340 * BITMAP_CopyBitmap
343 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
345 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
346 HBITMAP res = 0;
347 BITMAP bm;
349 if(!bmp) return 0;
351 bm = bmp->bitmap;
352 bm.bmBits = NULL;
353 res = CreateBitmapIndirect(&bm);
355 if(res) {
356 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
357 bm.bmHeight );
358 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
359 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
360 HeapFree( GetProcessHeap(), 0, buf );
363 GDI_ReleaseObj( hbitmap );
364 return res;
368 /***********************************************************************
369 * BITMAP_SetOwnerDC
371 * Set the type of DC that owns the bitmap. This is used when the
372 * bitmap is selected into a device to initialize the bitmap function
373 * table.
375 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
377 BITMAPOBJ *bitmap;
378 BOOL ret;
380 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
381 if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
383 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
385 ret = TRUE;
386 if (!bitmap->funcs) /* not owned by a DC yet */
388 if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap );
389 if (ret) bitmap->funcs = dc->funcs;
391 else if (bitmap->funcs != dc->funcs)
393 FIXME( "Trying to select bitmap %x in different DC type\n", hbitmap );
394 ret = FALSE;
396 GDI_ReleaseObj( hbitmap );
397 return ret;
401 /***********************************************************************
402 * BITMAP_SelectObject
404 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
406 HGDIOBJ ret;
407 BITMAPOBJ *bitmap = obj;
408 DC *dc = DC_GetDCPtr( hdc );
410 if (!dc) return 0;
411 if (!(dc->flags & DC_MEMORY))
413 GDI_ReleaseObj( hdc );
414 return 0;
416 ret = dc->hBitmap;
417 if (handle == dc->hBitmap) goto done; /* nothing to do */
419 if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
421 WARN( "Bitmap already selected in another DC\n" );
422 GDI_ReleaseObj( hdc );
423 return 0;
426 if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
428 GDI_ReleaseObj( hdc );
429 return 0;
432 if (dc->funcs->pSelectBitmap) handle = dc->funcs->pSelectBitmap( dc->physDev, handle );
434 if (handle)
436 dc->hBitmap = handle;
437 dc->totalExtent.left = 0;
438 dc->totalExtent.top = 0;
439 dc->totalExtent.right = bitmap->bitmap.bmWidth;
440 dc->totalExtent.bottom = bitmap->bitmap.bmHeight;
441 dc->flags &= ~DC_DIRTY;
442 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
443 CLIPPING_UpdateGCRegion( dc );
445 if (dc->bitsPerPixel != bitmap->bitmap.bmBitsPixel)
447 /* depth changed, reinitialize the DC */
448 dc->bitsPerPixel = bitmap->bitmap.bmBitsPixel;
449 DC_InitDC( dc );
452 else ret = 0;
454 done:
455 GDI_ReleaseObj( hdc );
456 return ret;
460 /***********************************************************************
461 * BITMAP_DeleteObject
463 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
465 BITMAPOBJ * bmp = obj;
467 if (bmp->funcs && bmp->funcs->pDeleteBitmap)
468 bmp->funcs->pDeleteBitmap( handle );
470 if( bmp->bitmap.bmBits )
471 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
473 if (bmp->dib)
475 DIBSECTION *dib = bmp->dib;
477 if (dib->dsBm.bmBits)
479 if (dib->dshSection)
481 SYSTEM_INFO SystemInfo;
482 GetSystemInfo( &SystemInfo );
483 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
484 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
486 else if (!dib->dsOffset)
487 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
489 HeapFree(GetProcessHeap(), 0, dib);
490 bmp->dib = NULL;
491 if (bmp->segptr_bits)
492 { /* free its selector array */
493 WORD sel = SELECTOROF(bmp->segptr_bits);
494 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
495 int i;
497 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
500 return GDI_FreeObject( handle, obj );
504 /***********************************************************************
505 * BITMAP_GetObject16
507 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
509 BITMAPOBJ *bmp = obj;
511 if (bmp->dib)
513 if ( count <= sizeof(BITMAP16) )
515 BITMAP *bmp32 = &bmp->dib->dsBm;
516 BITMAP16 bmp16;
517 bmp16.bmType = bmp32->bmType;
518 bmp16.bmWidth = bmp32->bmWidth;
519 bmp16.bmHeight = bmp32->bmHeight;
520 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
521 bmp16.bmPlanes = bmp32->bmPlanes;
522 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
523 bmp16.bmBits = (SEGPTR)0;
524 memcpy( buffer, &bmp16, count );
525 return count;
527 else
529 FIXME("not implemented for DIBs: count %d\n", count);
530 return 0;
533 else
535 BITMAP16 bmp16;
536 bmp16.bmType = bmp->bitmap.bmType;
537 bmp16.bmWidth = bmp->bitmap.bmWidth;
538 bmp16.bmHeight = bmp->bitmap.bmHeight;
539 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
540 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
541 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
542 bmp16.bmBits = (SEGPTR)0;
543 if (count > sizeof(bmp16)) count = sizeof(bmp16);
544 memcpy( buffer, &bmp16, count );
545 return count;
550 /***********************************************************************
551 * BITMAP_GetObject
553 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
555 BITMAPOBJ *bmp = obj;
557 if (bmp->dib)
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 (count > sizeof(BITMAP)) count = sizeof(BITMAP);
574 memcpy( buffer, &bmp->bitmap, count );
575 return count;
580 /******************************************************************************
581 * CreateDiscardableBitmap [GDI32.@] Creates a discardable bitmap
583 * RETURNS
584 * Success: Handle to bitmap
585 * Failure: NULL
587 HBITMAP WINAPI CreateDiscardableBitmap(
588 HDC hdc, /* [in] Handle to device context */
589 INT width, /* [in] Bitmap width */
590 INT height) /* [in] Bitmap height */
592 return CreateCompatibleBitmap( hdc, width, height );
596 /******************************************************************************
597 * GetBitmapDimensionEx [GDI32.@] Retrieves dimensions of a bitmap
599 * RETURNS
600 * Success: TRUE
601 * Failure: FALSE
603 BOOL WINAPI GetBitmapDimensionEx(
604 HBITMAP hbitmap, /* [in] Handle to bitmap */
605 LPSIZE size) /* [out] Address of struct receiving dimensions */
607 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
608 if (!bmp) return FALSE;
609 *size = bmp->size;
610 GDI_ReleaseObj( hbitmap );
611 return TRUE;
615 /******************************************************************************
616 * SetBitmapDimensionEx [GDI32.@] Assignes dimensions to a bitmap
618 * RETURNS
619 * Success: TRUE
620 * Failure: FALSE
622 BOOL WINAPI SetBitmapDimensionEx(
623 HBITMAP hbitmap, /* [in] Handle to bitmap */
624 INT x, /* [in] Bitmap width */
625 INT y, /* [in] Bitmap height */
626 LPSIZE prevSize) /* [out] Address of structure for orig dims */
628 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
629 if (!bmp) return FALSE;
630 if (prevSize) *prevSize = bmp->size;
631 bmp->size.cx = x;
632 bmp->size.cy = y;
633 GDI_ReleaseObj( hbitmap );
634 return TRUE;