Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / gdi / bitmap.c
blob19517edfa7171ae6e999ae557cd5549c8381a0c6
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/wingdi16.h"
27 #include "gdi.h"
28 #include "gdi_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
34 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc );
35 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
36 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
37 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj );
39 static const struct gdi_obj_funcs bitmap_funcs =
41 BITMAP_SelectObject, /* pSelectObject */
42 BITMAP_GetObject16, /* pGetObject16 */
43 BITMAP_GetObject, /* pGetObjectA */
44 BITMAP_GetObject, /* pGetObjectW */
45 NULL, /* pUnrealizeObject */
46 BITMAP_DeleteObject /* pDeleteObject */
49 /***********************************************************************
50 * BITMAP_GetWidthBytes
52 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
53 * data.
55 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
57 switch(bpp)
59 case 1:
60 return 2 * ((bmWidth+15) >> 4);
62 case 24:
63 bmWidth *= 3; /* fall through */
64 case 8:
65 return bmWidth + (bmWidth & 1);
67 case 32:
68 return bmWidth * 4;
70 case 16:
71 case 15:
72 return bmWidth * 2;
74 case 4:
75 return 2 * ((bmWidth+3) >> 2);
77 default:
78 WARN("Unknown depth %d, please report.\n", bpp );
80 return -1;
84 /******************************************************************************
85 * CreateBitmap [GDI32.@]
87 * Creates a bitmap with the specified info.
89 * PARAMS
90 * width [I] bitmap width
91 * height [I] bitmap height
92 * planes [I] Number of color planes
93 * bpp [I] Number of bits to identify a color
94 * bits [I] Pointer to array containing color data
96 * RETURNS
97 * Success: Handle to bitmap
98 * Failure: 0
100 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
101 UINT bpp, LPCVOID bits )
103 BITMAP bm;
105 bm.bmType = 0;
106 bm.bmWidth = width;
107 bm.bmHeight = height;
108 bm.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
109 bm.bmPlanes = planes;
110 bm.bmBitsPixel = bpp;
111 bm.bmBits = (LPVOID)bits;
113 return CreateBitmapIndirect( &bm );
116 /******************************************************************************
117 * CreateCompatibleBitmap [GDI32.@]
119 * Creates a bitmap compatible with the DC.
121 * PARAMS
122 * hdc [I] Handle to device context
123 * width [I] Width of bitmap
124 * height [I] Height of bitmap
126 * RETURNS
127 * Success: Handle to bitmap
128 * Failure: 0
130 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
132 HBITMAP hbmpRet = 0;
133 DC *dc;
135 TRACE("(%p,%d,%d) = \n", hdc, width, height);
137 if ((width >= 0x10000) || (height >= 0x10000))
139 FIXME("got bad width %d or height %d, please look for reason\n",
140 width, height);
142 else
144 if (!(dc = DC_GetDCPtr(hdc))) return 0;
146 if (GDIMAGIC( dc->header.wMagic ) != MEMORY_DC_MAGIC)
148 hbmpRet = CreateBitmap(width, height,
149 GetDeviceCaps(hdc, PLANES),
150 GetDeviceCaps(hdc, BITSPIXEL),
151 NULL);
153 else /* Memory DC */
155 BITMAPOBJ *bmp = GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC );
157 if (!bmp->dib)
159 /* A device-dependent bitmap is selected in the DC */
160 hbmpRet = CreateBitmap(width, height,
161 bmp->bitmap.bmPlanes,
162 bmp->bitmap.bmBitsPixel,
163 NULL);
165 else
167 /* A DIB section is selected in the DC */
168 BITMAPINFO *bi;
169 void *bits;
171 /* Allocate memory for a BITMAPINFOHEADER structure and a
172 color table. The maximum number of colors in a color table
173 is 256 which corresponds to a bitmap with depth 8.
174 Bitmaps with higher depths don't have color tables. */
175 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
177 if (bi)
179 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
180 bi->bmiHeader.biWidth = width;
181 bi->bmiHeader.biHeight = height;
182 bi->bmiHeader.biPlanes = bmp->dib->dsBmih.biPlanes;
183 bi->bmiHeader.biBitCount = bmp->dib->dsBmih.biBitCount;
184 bi->bmiHeader.biCompression = bmp->dib->dsBmih.biCompression;
185 bi->bmiHeader.biSizeImage = 0;
186 bi->bmiHeader.biXPelsPerMeter = bmp->dib->dsBmih.biXPelsPerMeter;
187 bi->bmiHeader.biYPelsPerMeter = bmp->dib->dsBmih.biYPelsPerMeter;
188 bi->bmiHeader.biClrUsed = bmp->dib->dsBmih.biClrUsed;
189 bi->bmiHeader.biClrImportant = bmp->dib->dsBmih.biClrImportant;
191 if (bi->bmiHeader.biCompression == BI_BITFIELDS)
193 /* Copy the color masks */
194 CopyMemory(bi->bmiColors, bmp->dib->dsBitfields, 3 * sizeof(DWORD));
196 else if (bi->bmiHeader.biBitCount <= 8)
198 /* Copy the color table */
199 GetDIBColorTable(hdc, 0, 256, bi->bmiColors);
202 hbmpRet = CreateDIBSection(hdc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
203 HeapFree(GetProcessHeap(), 0, bi);
206 GDI_ReleaseObj(dc->hBitmap);
208 GDI_ReleaseObj(hdc);
211 TRACE("\t\t%p\n", hbmpRet);
212 return hbmpRet;
216 /******************************************************************************
217 * CreateBitmapIndirect [GDI32.@]
219 * Creates a bitmap with the specified info.
221 * PARAMS
222 * bmp [I] Pointer to the bitmap info describing the bitmap
224 * RETURNS
225 * Success: Handle to bitmap
226 * Failure: NULL. Use GetLastError() to determine the cause.
228 * NOTES
229 * If a width or height of 0 are given, a 1x1 monochrome bitmap is returned.
231 HBITMAP WINAPI CreateBitmapIndirect( const BITMAP *bmp )
233 BITMAP bm;
234 BITMAPOBJ *bmpobj;
235 HBITMAP hbitmap;
237 if (!bmp || bmp->bmType || bmp->bmPlanes != 1)
239 if (bmp && bmp->bmPlanes != 1)
240 FIXME("planes = %d\n", bmp->bmPlanes);
241 SetLastError( ERROR_INVALID_PARAMETER );
242 return NULL;
245 bm = *bmp;
247 if (!bm.bmWidth || !bm.bmHeight)
249 bm.bmWidth = bm.bmHeight = 1;
250 bm.bmPlanes = bm.bmBitsPixel = 1;
251 bm.bmWidthBytes = 2;
252 bm.bmBits = NULL;
254 else
256 if (bm.bmHeight < 0)
257 bm.bmHeight = -bm.bmHeight;
258 if (bm.bmWidth < 0)
259 bm.bmWidth = -bm.bmWidth;
262 /* Create the BITMAPOBJ */
263 bmpobj = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC,
264 (HGDIOBJ *)&hbitmap, &bitmap_funcs );
266 if (!bmpobj)
268 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
269 return NULL;
272 TRACE("%dx%d, %d colors returning %p\n", bm.bmWidth, bm.bmHeight,
273 1 << (bm.bmPlanes * bm.bmBitsPixel), hbitmap);
275 bmpobj->size.cx = 0;
276 bmpobj->size.cy = 0;
277 bmpobj->bitmap = bm;
278 bmpobj->bitmap.bmBits = NULL;
279 bmpobj->funcs = NULL;
280 bmpobj->dib = NULL;
281 bmpobj->segptr_bits = 0;
283 if (bm.bmBits)
284 SetBitmapBits( hbitmap, bm.bmHeight * bm.bmWidthBytes, bm.bmBits );
286 GDI_ReleaseObj( hbitmap );
287 return hbitmap;
291 /***********************************************************************
292 * GetBitmapBits [GDI32.@]
294 * Copies bitmap bits of bitmap to buffer.
296 * RETURNS
297 * Success: Number of bytes copied
298 * Failure: 0
300 LONG WINAPI GetBitmapBits(
301 HBITMAP hbitmap, /* [in] Handle to bitmap */
302 LONG count, /* [in] Number of bytes to copy */
303 LPVOID bits) /* [out] Pointer to buffer to receive bits */
305 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
306 LONG height, ret;
308 if (!bmp) return 0;
310 /* If the bits vector is null, the function should return the read size */
311 if(bits == NULL)
313 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
314 goto done;
317 if (count < 0) {
318 WARN("(%ld): Negative number of bytes passed???\n", count );
319 count = -count;
322 /* Only get entire lines */
323 height = count / bmp->bitmap.bmWidthBytes;
324 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
325 count = height * bmp->bitmap.bmWidthBytes;
326 if (count == 0)
328 WARN("Less than one entire line requested\n");
329 ret = 0;
330 goto done;
334 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
335 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
336 1 << bmp->bitmap.bmBitsPixel, height );
338 if(bmp->funcs && bmp->funcs->pGetBitmapBits)
340 TRACE("Calling device specific BitmapBits\n");
341 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
342 } else {
344 if(!bmp->bitmap.bmBits) {
345 WARN("Bitmap is empty\n");
346 ret = 0;
347 } else {
348 memcpy(bits, bmp->bitmap.bmBits, count);
349 ret = count;
353 done:
354 GDI_ReleaseObj( hbitmap );
355 return ret;
359 /******************************************************************************
360 * SetBitmapBits [GDI32.@]
362 * Sets bits of color data for a bitmap.
364 * RETURNS
365 * Success: Number of bytes used in setting the bitmap bits
366 * Failure: 0
368 LONG WINAPI SetBitmapBits(
369 HBITMAP hbitmap, /* [in] Handle to bitmap */
370 LONG count, /* [in] Number of bytes in bitmap array */
371 LPCVOID bits) /* [in] Address of array with bitmap bits */
373 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
374 LONG height, ret;
376 if ((!bmp) || (!bits))
377 return 0;
379 if (count < 0) {
380 WARN("(%ld): Negative number of bytes passed???\n", count );
381 count = -count;
384 /* Only get entire lines */
385 height = count / bmp->bitmap.bmWidthBytes;
386 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
387 count = height * bmp->bitmap.bmWidthBytes;
389 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
390 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
391 1 << bmp->bitmap.bmBitsPixel, height );
393 if(bmp->funcs && bmp->funcs->pSetBitmapBits) {
395 TRACE("Calling device specific BitmapBits\n");
396 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
397 } else {
399 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
400 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
401 if(!bmp->bitmap.bmBits) {
402 WARN("Unable to allocate bit buffer\n");
403 ret = 0;
404 } else {
405 memcpy(bmp->bitmap.bmBits, bits, count);
406 ret = count;
410 GDI_ReleaseObj( hbitmap );
411 return ret;
414 /**********************************************************************
415 * BITMAP_CopyBitmap
418 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
420 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
421 HBITMAP res = 0;
422 BITMAP bm;
424 if(!bmp) return 0;
426 bm = bmp->bitmap;
427 bm.bmBits = NULL;
428 res = CreateBitmapIndirect(&bm);
430 if(res) {
431 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
432 bm.bmHeight );
433 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
434 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
435 HeapFree( GetProcessHeap(), 0, buf );
438 GDI_ReleaseObj( hbitmap );
439 return res;
443 /***********************************************************************
444 * BITMAP_SetOwnerDC
446 * Set the type of DC that owns the bitmap. This is used when the
447 * bitmap is selected into a device to initialize the bitmap function
448 * table.
450 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
452 BITMAPOBJ *bitmap;
453 BOOL ret;
455 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
456 if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
458 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
460 ret = TRUE;
461 if (!bitmap->funcs) /* not owned by a DC yet */
463 if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap );
464 if (ret) bitmap->funcs = dc->funcs;
466 else if (bitmap->funcs != dc->funcs)
468 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
469 ret = FALSE;
471 GDI_ReleaseObj( hbitmap );
472 return ret;
476 /***********************************************************************
477 * BITMAP_SelectObject
479 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
481 HGDIOBJ ret;
482 BITMAPOBJ *bitmap = obj;
483 DC *dc = DC_GetDCPtr( hdc );
485 if (!dc) return 0;
486 if (GetObjectType( hdc ) != OBJ_MEMDC)
488 GDI_ReleaseObj( hdc );
489 return 0;
491 ret = dc->hBitmap;
492 if (handle == dc->hBitmap) goto done; /* nothing to do */
494 if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
496 WARN( "Bitmap already selected in another DC\n" );
497 GDI_ReleaseObj( hdc );
498 return 0;
501 if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
503 GDI_ReleaseObj( hdc );
504 return 0;
507 if (dc->funcs->pSelectBitmap) handle = dc->funcs->pSelectBitmap( dc->physDev, handle );
509 if (handle)
511 dc->hBitmap = handle;
512 dc->flags &= ~DC_DIRTY;
513 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
514 DC_InitDC( dc );
516 else ret = 0;
518 done:
519 GDI_ReleaseObj( hdc );
520 return ret;
524 /***********************************************************************
525 * BITMAP_DeleteObject
527 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
529 BITMAPOBJ * bmp = obj;
531 if (bmp->funcs && bmp->funcs->pDeleteBitmap)
532 bmp->funcs->pDeleteBitmap( handle );
534 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
536 if (bmp->dib)
538 DIBSECTION *dib = bmp->dib;
540 if (dib->dsBm.bmBits)
542 if (dib->dshSection)
544 SYSTEM_INFO SystemInfo;
545 GetSystemInfo( &SystemInfo );
546 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
547 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
549 else if (!dib->dsOffset)
550 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
552 HeapFree(GetProcessHeap(), 0, dib);
553 bmp->dib = NULL;
554 if (bmp->segptr_bits)
555 { /* free its selector array */
556 WORD sel = SELECTOROF(bmp->segptr_bits);
557 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
558 int i;
560 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
563 return GDI_FreeObject( handle, obj );
567 /***********************************************************************
568 * BITMAP_GetObject16
570 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
572 BITMAPOBJ *bmp = obj;
574 if (bmp->dib)
576 if ( count <= sizeof(BITMAP16) )
578 BITMAP *bmp32 = &bmp->dib->dsBm;
579 BITMAP16 bmp16;
580 bmp16.bmType = bmp32->bmType;
581 bmp16.bmWidth = bmp32->bmWidth;
582 bmp16.bmHeight = bmp32->bmHeight;
583 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
584 bmp16.bmPlanes = bmp32->bmPlanes;
585 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
586 bmp16.bmBits = (SEGPTR)0;
587 memcpy( buffer, &bmp16, count );
588 return count;
590 else
592 FIXME("not implemented for DIBs: count %d\n", count);
593 return 0;
596 else
598 BITMAP16 bmp16;
599 bmp16.bmType = bmp->bitmap.bmType;
600 bmp16.bmWidth = bmp->bitmap.bmWidth;
601 bmp16.bmHeight = bmp->bitmap.bmHeight;
602 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
603 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
604 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
605 bmp16.bmBits = (SEGPTR)0;
606 if (count > sizeof(bmp16)) count = sizeof(bmp16);
607 memcpy( buffer, &bmp16, count );
608 return count;
613 /***********************************************************************
614 * BITMAP_GetObject
616 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
618 BITMAPOBJ *bmp = obj;
620 if (bmp->dib)
622 if( !buffer )
623 return sizeof(DIBSECTION);
624 if (count < sizeof(DIBSECTION))
626 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
628 else
630 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
633 memcpy( buffer, bmp->dib, count );
634 return count;
636 else
638 if( !buffer )
639 return sizeof(BITMAP);
640 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
641 memcpy( buffer, &bmp->bitmap, count );
642 return count;
647 /******************************************************************************
648 * CreateDiscardableBitmap [GDI32.@]
650 * Creates a discardable bitmap.
652 * RETURNS
653 * Success: Handle to bitmap
654 * Failure: NULL
656 HBITMAP WINAPI CreateDiscardableBitmap(
657 HDC hdc, /* [in] Handle to device context */
658 INT width, /* [in] Bitmap width */
659 INT height) /* [in] Bitmap height */
661 return CreateCompatibleBitmap( hdc, width, height );
665 /******************************************************************************
666 * GetBitmapDimensionEx [GDI32.@]
668 * Retrieves dimensions of a bitmap.
670 * RETURNS
671 * Success: TRUE
672 * Failure: FALSE
674 BOOL WINAPI GetBitmapDimensionEx(
675 HBITMAP hbitmap, /* [in] Handle to bitmap */
676 LPSIZE size) /* [out] Address of struct receiving dimensions */
678 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
679 if (!bmp) return FALSE;
680 *size = bmp->size;
681 GDI_ReleaseObj( hbitmap );
682 return TRUE;
686 /******************************************************************************
687 * SetBitmapDimensionEx [GDI32.@]
689 * Assigns dimensions to a bitmap.
690 * MSDN says that this function will fail if hbitmap is a handle created by
691 * CreateDIBSection, but that's not true on Windows 2000.
693 * RETURNS
694 * Success: TRUE
695 * Failure: FALSE
697 BOOL WINAPI SetBitmapDimensionEx(
698 HBITMAP hbitmap, /* [in] Handle to bitmap */
699 INT x, /* [in] Bitmap width */
700 INT y, /* [in] Bitmap height */
701 LPSIZE prevSize) /* [out] Address of structure for orig dims */
703 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
704 if (!bmp) return FALSE;
705 if (prevSize) *prevSize = bmp->size;
706 bmp->size.cx = x;
707 bmp->size.cy = y;
708 GDI_ReleaseObj( hbitmap );
709 return TRUE;