Moved the selector access functions out of winnt.h into libwine.
[wine/multimedia.git] / objects / bitmap.c
blob57ee54cb5c9a41989e5cbf9daed9d5997b790587
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 /***********************************************************************
36 * BITMAP_GetWidthBytes
38 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
39 * data.
41 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
43 switch(bpp)
45 case 1:
46 return 2 * ((bmWidth+15) >> 4);
48 case 24:
49 bmWidth *= 3; /* fall through */
50 case 8:
51 return bmWidth + (bmWidth & 1);
53 case 32:
54 return bmWidth * 4;
56 case 16:
57 case 15:
58 return bmWidth * 2;
60 case 4:
61 return 2 * ((bmWidth+3) >> 2);
63 default:
64 WARN("Unknown depth %d, please report.\n", bpp );
66 return -1;
69 /***********************************************************************
70 * CreateUserBitmap (GDI.407)
72 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
73 UINT16 bpp, LPCVOID bits )
75 return CreateBitmap16( width, height, planes, bpp, bits );
78 /***********************************************************************
79 * CreateUserDiscardableBitmap (GDI.409)
81 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy,
82 INT16 width, INT16 height )
84 HDC hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
85 HBITMAP16 ret = CreateCompatibleBitmap16( hdc, width, height );
86 DeleteDC( hdc );
87 return ret;
91 /***********************************************************************
92 * CreateBitmap (GDI.48)
94 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
95 UINT16 bpp, LPCVOID bits )
97 return CreateBitmap( width, height, planes, bpp, bits );
101 /******************************************************************************
102 * CreateBitmap [GDI32.@] Creates a bitmap with the specified info
104 * PARAMS
105 * width [I] bitmap width
106 * height [I] bitmap height
107 * planes [I] Number of color planes
108 * bpp [I] Number of bits to identify a color
109 * bits [I] Pointer to array containing color data
111 * RETURNS
112 * Success: Handle to bitmap
113 * Failure: 0
115 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
116 UINT bpp, LPCVOID bits )
118 BITMAPOBJ *bmp;
119 HBITMAP hbitmap;
121 planes = (BYTE)planes;
122 bpp = (BYTE)bpp;
125 /* Check parameters */
126 if (!height || !width)
128 height = 1;
129 width = 1;
130 planes = 1;
131 bpp = 1;
133 if (planes != 1) {
134 FIXME("planes = %d\n", planes);
135 return 0;
137 if (height < 0) height = -height;
138 if (width < 0) width = -width;
140 /* Create the BITMAPOBJ */
141 if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC, &hbitmap ))) return 0;
143 TRACE("%dx%d, %d colors returning %08x\n", width, height,
144 1 << (planes*bpp), hbitmap);
146 bmp->size.cx = 0;
147 bmp->size.cy = 0;
148 bmp->bitmap.bmType = 0;
149 bmp->bitmap.bmWidth = width;
150 bmp->bitmap.bmHeight = height;
151 bmp->bitmap.bmPlanes = planes;
152 bmp->bitmap.bmBitsPixel = bpp;
153 bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
154 bmp->bitmap.bmBits = NULL;
156 bmp->funcs = NULL;
157 bmp->physBitmap = NULL;
158 bmp->dib = NULL;
159 bmp->segptr_bits = 0;
161 if (bits) /* Set bitmap bits */
162 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
163 bits );
164 GDI_ReleaseObj( hbitmap );
165 return hbitmap;
169 /***********************************************************************
170 * CreateCompatibleBitmap (GDI.51)
172 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
174 return CreateCompatibleBitmap( hdc, width, height );
178 /******************************************************************************
179 * CreateCompatibleBitmap [GDI32.@] Creates a bitmap compatible with the DC
181 * PARAMS
182 * hdc [I] Handle to device context
183 * width [I] Width of bitmap
184 * height [I] Height of bitmap
186 * RETURNS
187 * Success: Handle to bitmap
188 * Failure: 0
190 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
192 HBITMAP hbmpRet = 0;
193 DC *dc;
195 TRACE("(%04x,%d,%d) = \n", hdc, width, height );
196 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
197 if ((width >= 0x10000) || (height >= 0x10000)) {
198 FIXME("got bad width %d or height %d, please look for reason\n",
199 width, height );
200 } else {
201 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
202 if (!width || !height)
203 hbmpRet = CreateBitmap( 1, 1, 1, 1, NULL );
204 else
205 hbmpRet = CreateBitmap( width, height, 1, dc->bitsPerPixel, NULL );
206 if(dc->funcs->pCreateBitmap)
207 dc->funcs->pCreateBitmap( hbmpRet );
209 TRACE("\t\t%04x\n", hbmpRet);
210 GDI_ReleaseObj(hdc);
211 return hbmpRet;
215 /***********************************************************************
216 * CreateBitmapIndirect (GDI.49)
218 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
220 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
221 bmp->bmBitsPixel, MapSL( bmp->bmBits ) );
225 /******************************************************************************
226 * CreateBitmapIndirect [GDI32.@] Creates a bitmap with the specifies info
228 * RETURNS
229 * Success: Handle to bitmap
230 * Failure: NULL
232 HBITMAP WINAPI CreateBitmapIndirect(
233 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
235 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
236 bmp->bmBitsPixel, bmp->bmBits );
240 /***********************************************************************
241 * GetBitmapBits (GDI.74)
243 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
245 return GetBitmapBits( hbitmap, count, buffer );
249 /***********************************************************************
250 * GetBitmapBits [GDI32.@] Copies bitmap bits of bitmap to buffer
252 * RETURNS
253 * Success: Number of bytes copied
254 * Failure: 0
256 LONG WINAPI GetBitmapBits(
257 HBITMAP hbitmap, /* [in] Handle to bitmap */
258 LONG count, /* [in] Number of bytes to copy */
259 LPVOID bits) /* [out] Pointer to buffer to receive bits */
261 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
262 LONG height, ret;
264 if (!bmp) return 0;
266 /* If the bits vector is null, the function should return the read size */
267 if(bits == NULL)
269 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
270 goto done;
273 if (count < 0) {
274 WARN("(%ld): Negative number of bytes passed???\n", count );
275 count = -count;
278 /* Only get entire lines */
279 height = count / bmp->bitmap.bmWidthBytes;
280 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
281 count = height * bmp->bitmap.bmWidthBytes;
282 if (count == 0)
284 WARN("Less than one entire line requested\n");
285 ret = 0;
286 goto done;
290 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
291 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
292 1 << bmp->bitmap.bmBitsPixel, height );
294 if(bmp->funcs) {
296 TRACE("Calling device specific BitmapBits\n");
297 if(bmp->funcs->pBitmapBits)
298 ret = bmp->funcs->pBitmapBits(hbitmap, bits, count, DDB_GET);
299 else {
300 ERR("BitmapBits == NULL??\n");
301 ret = 0;
304 } else {
306 if(!bmp->bitmap.bmBits) {
307 WARN("Bitmap is empty\n");
308 ret = 0;
309 } else {
310 memcpy(bits, bmp->bitmap.bmBits, count);
311 ret = count;
315 done:
316 GDI_ReleaseObj( hbitmap );
317 return ret;
321 /***********************************************************************
322 * SetBitmapBits (GDI.106)
324 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
326 return SetBitmapBits( hbitmap, count, buffer );
330 /******************************************************************************
331 * SetBitmapBits [GDI32.@] Sets bits of color data for a bitmap
333 * RETURNS
334 * Success: Number of bytes used in setting the bitmap bits
335 * Failure: 0
337 LONG WINAPI SetBitmapBits(
338 HBITMAP hbitmap, /* [in] Handle to bitmap */
339 LONG count, /* [in] Number of bytes in bitmap array */
340 LPCVOID bits) /* [in] Address of array with bitmap bits */
342 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
343 LONG height, ret;
345 if ((!bmp) || (!bits))
346 return 0;
348 if (count < 0) {
349 WARN("(%ld): Negative number of bytes passed???\n", count );
350 count = -count;
353 /* Only get entire lines */
354 height = count / bmp->bitmap.bmWidthBytes;
355 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
356 count = height * bmp->bitmap.bmWidthBytes;
358 TRACE("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
359 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
360 1 << bmp->bitmap.bmBitsPixel, height );
362 if(bmp->funcs) {
364 TRACE("Calling device specific BitmapBits\n");
365 if(bmp->funcs->pBitmapBits)
366 ret = bmp->funcs->pBitmapBits(hbitmap, (void *) bits, count, DDB_SET);
367 else {
368 ERR("BitmapBits == NULL??\n");
369 ret = 0;
372 } else {
374 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
375 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
376 if(!bmp->bitmap.bmBits) {
377 WARN("Unable to allocate bit buffer\n");
378 ret = 0;
379 } else {
380 memcpy(bmp->bitmap.bmBits, bits, count);
381 ret = count;
385 GDI_ReleaseObj( hbitmap );
386 return ret;
389 /**********************************************************************
390 * BITMAP_CopyBitmap
393 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
395 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
396 HBITMAP res = 0;
397 BITMAP bm;
399 if(!bmp) return 0;
401 bm = bmp->bitmap;
402 bm.bmBits = NULL;
403 res = CreateBitmapIndirect(&bm);
405 if(res) {
406 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
407 bm.bmHeight );
408 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
409 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
410 HeapFree( GetProcessHeap(), 0, buf );
413 GDI_ReleaseObj( hbitmap );
414 return res;
417 /***********************************************************************
418 * BITMAP_DeleteObject
420 BOOL BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
422 if (bmp->funcs && bmp->funcs->pDeleteObject)
423 bmp->funcs->pDeleteObject( hbitmap );
425 if( bmp->bitmap.bmBits )
426 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
428 if (bmp->dib)
430 DIBSECTION *dib = bmp->dib;
432 if (dib->dsBm.bmBits)
434 if (dib->dshSection)
436 SYSTEM_INFO SystemInfo;
437 GetSystemInfo( &SystemInfo );
438 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
439 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
441 else if (!dib->dsOffset)
442 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
444 HeapFree(GetProcessHeap(), 0, dib);
445 bmp->dib = NULL;
446 if (bmp->segptr_bits)
447 { /* free its selector array */
448 WORD sel = SELECTOROF(bmp->segptr_bits);
449 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
450 int i;
452 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
455 return GDI_FreeObject( hbitmap, bmp );
459 /***********************************************************************
460 * BITMAP_GetObject16
462 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
464 if (bmp->dib)
466 if ( count <= sizeof(BITMAP16) )
468 BITMAP *bmp32 = &bmp->dib->dsBm;
469 BITMAP16 bmp16;
470 bmp16.bmType = bmp32->bmType;
471 bmp16.bmWidth = bmp32->bmWidth;
472 bmp16.bmHeight = bmp32->bmHeight;
473 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
474 bmp16.bmPlanes = bmp32->bmPlanes;
475 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
476 bmp16.bmBits = (SEGPTR)0;
477 memcpy( buffer, &bmp16, count );
478 return count;
480 else
482 FIXME("not implemented for DIBs: count %d\n", count);
483 return 0;
486 else
488 BITMAP16 bmp16;
489 bmp16.bmType = bmp->bitmap.bmType;
490 bmp16.bmWidth = bmp->bitmap.bmWidth;
491 bmp16.bmHeight = bmp->bitmap.bmHeight;
492 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
493 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
494 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
495 bmp16.bmBits = (SEGPTR)0;
496 if (count > sizeof(bmp16)) count = sizeof(bmp16);
497 memcpy( buffer, &bmp16, count );
498 return count;
503 /***********************************************************************
504 * BITMAP_GetObject
506 INT BITMAP_GetObject( BITMAPOBJ * bmp, INT count, LPVOID buffer )
508 if (bmp->dib)
510 if (count < sizeof(DIBSECTION))
512 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
514 else
516 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
519 memcpy( buffer, bmp->dib, count );
520 return count;
522 else
524 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
525 memcpy( buffer, &bmp->bitmap, count );
526 return count;
531 /***********************************************************************
532 * CreateDiscardableBitmap (GDI.156)
534 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
535 INT16 height )
537 return CreateCompatibleBitmap16( hdc, width, height );
541 /******************************************************************************
542 * CreateDiscardableBitmap [GDI32.@] Creates a discardable bitmap
544 * RETURNS
545 * Success: Handle to bitmap
546 * Failure: NULL
548 HBITMAP WINAPI CreateDiscardableBitmap(
549 HDC hdc, /* [in] Handle to device context */
550 INT width, /* [in] Bitmap width */
551 INT height) /* [in] Bitmap height */
553 return CreateCompatibleBitmap( hdc, width, height );
557 /***********************************************************************
558 * GetBitmapDimensionEx (GDI.468)
560 * NOTES
561 * Can this call GetBitmapDimensionEx?
563 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
565 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
566 if (!bmp) return FALSE;
567 size->cx = bmp->size.cx;
568 size->cy = bmp->size.cy;
569 GDI_ReleaseObj( hbitmap );
570 return TRUE;
574 /******************************************************************************
575 * GetBitmapDimensionEx [GDI32.@] Retrieves dimensions of a bitmap
577 * RETURNS
578 * Success: TRUE
579 * Failure: FALSE
581 BOOL WINAPI GetBitmapDimensionEx(
582 HBITMAP hbitmap, /* [in] Handle to bitmap */
583 LPSIZE size) /* [out] Address of struct receiving dimensions */
585 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
586 if (!bmp) return FALSE;
587 *size = bmp->size;
588 GDI_ReleaseObj( hbitmap );
589 return TRUE;
593 /***********************************************************************
594 * GetBitmapDimension (GDI.162)
596 DWORD WINAPI GetBitmapDimension16( HBITMAP16 hbitmap )
598 SIZE16 size;
599 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
600 return MAKELONG( size.cx, size.cy );
604 /***********************************************************************
605 * SetBitmapDimensionEx (GDI.478)
607 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
608 LPSIZE16 prevSize )
610 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
611 if (!bmp) return FALSE;
612 if (prevSize)
614 prevSize->cx = bmp->size.cx;
615 prevSize->cy = bmp->size.cy;
617 bmp->size.cx = x;
618 bmp->size.cy = y;
619 GDI_ReleaseObj( hbitmap );
620 return TRUE;
624 /******************************************************************************
625 * SetBitmapDimensionEx [GDI32.@] Assignes dimensions to a bitmap
627 * RETURNS
628 * Success: TRUE
629 * Failure: FALSE
631 BOOL WINAPI SetBitmapDimensionEx(
632 HBITMAP hbitmap, /* [in] Handle to bitmap */
633 INT x, /* [in] Bitmap width */
634 INT y, /* [in] Bitmap height */
635 LPSIZE prevSize) /* [out] Address of structure for orig dims */
637 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
638 if (!bmp) return FALSE;
639 if (prevSize) *prevSize = bmp->size;
640 bmp->size.cx = x;
641 bmp->size.cy = y;
642 GDI_ReleaseObj( hbitmap );
643 return TRUE;
647 /***********************************************************************
648 * SetBitmapDimension (GDI.163)
650 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
652 SIZE16 size;
653 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
654 return MAKELONG( size.cx, size.cy );