Output BEGIN/END_INTERFACE for C++ base classes too.
[wine/multimedia.git] / dlls / gdi / bitmap.c
blob957c9a6a951b633eb4c1d8cf809dec9b38835858
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 "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 static 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 BITMAPOBJ *bmp;
104 HBITMAP hbitmap;
106 planes = (BYTE)planes;
107 bpp = (BYTE)bpp;
110 /* Check parameters */
111 if (!height || !width)
113 height = 1;
114 width = 1;
115 planes = 1;
116 bpp = 1;
118 if (planes != 1) {
119 FIXME("planes = %d\n", planes);
120 return 0;
122 if (height < 0) height = -height;
123 if (width < 0) width = -width;
125 /* Create the BITMAPOBJ */
126 if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC,
127 (HGDIOBJ *)&hbitmap, &bitmap_funcs )))
128 return 0;
130 TRACE("%dx%d, %d colors returning %p\n", width, height, 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.@]
158 * Creates a bitmap compatible with the DC.
160 * PARAMS
161 * hdc [I] Handle to device context
162 * width [I] Width of bitmap
163 * height [I] Height of bitmap
165 * RETURNS
166 * Success: Handle to bitmap
167 * Failure: 0
169 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
171 HBITMAP hbmpRet = 0;
172 DC *dc;
174 TRACE("(%p,%d,%d) = \n", hdc, width, height );
175 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
176 if ((width >= 0x10000) || (height >= 0x10000)) {
177 FIXME("got bad width %d or height %d, please look for reason\n",
178 width, height );
180 else
182 INT planes, bpp;
184 if (GDIMAGIC( dc->header.wMagic ) != MEMORY_DC_MAGIC)
186 planes = GetDeviceCaps( hdc, PLANES );
187 bpp = GetDeviceCaps( hdc, BITSPIXEL );
189 else /* memory DC, get the depth of the bitmap */
191 BITMAPOBJ *bmp = GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC );
192 planes = bmp->bitmap.bmPlanes;
193 bpp = bmp->bitmap.bmBitsPixel;
194 GDI_ReleaseObj( dc->hBitmap );
196 hbmpRet = CreateBitmap( width, height, planes, bpp, NULL );
198 TRACE("\t\t%p\n", hbmpRet);
199 GDI_ReleaseObj(hdc);
200 return hbmpRet;
204 /******************************************************************************
205 * CreateBitmapIndirect [GDI32.@]
207 * Creates a bitmap with the specifies info.
209 * RETURNS
210 * Success: Handle to bitmap
211 * Failure: NULL
213 HBITMAP WINAPI CreateBitmapIndirect(
214 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
216 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
217 bmp->bmBitsPixel, bmp->bmBits );
221 /***********************************************************************
222 * GetBitmapBits [GDI32.@]
224 * Copies bitmap bits of bitmap to buffer.
226 * RETURNS
227 * Success: Number of bytes copied
228 * Failure: 0
230 LONG WINAPI GetBitmapBits(
231 HBITMAP hbitmap, /* [in] Handle to bitmap */
232 LONG count, /* [in] Number of bytes to copy */
233 LPVOID bits) /* [out] Pointer to buffer to receive bits */
235 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
236 LONG height, ret;
238 if (!bmp) return 0;
240 /* If the bits vector is null, the function should return the read size */
241 if(bits == NULL)
243 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
244 goto done;
247 if (count < 0) {
248 WARN("(%ld): Negative number of bytes passed???\n", count );
249 count = -count;
252 /* Only get entire lines */
253 height = count / bmp->bitmap.bmWidthBytes;
254 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
255 count = height * bmp->bitmap.bmWidthBytes;
256 if (count == 0)
258 WARN("Less than one entire line requested\n");
259 ret = 0;
260 goto done;
264 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
265 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
266 1 << bmp->bitmap.bmBitsPixel, height );
268 if(bmp->funcs && bmp->funcs->pGetBitmapBits)
270 TRACE("Calling device specific BitmapBits\n");
271 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
272 } else {
274 if(!bmp->bitmap.bmBits) {
275 WARN("Bitmap is empty\n");
276 ret = 0;
277 } else {
278 memcpy(bits, bmp->bitmap.bmBits, count);
279 ret = count;
283 done:
284 GDI_ReleaseObj( hbitmap );
285 return ret;
289 /******************************************************************************
290 * SetBitmapBits [GDI32.@]
292 * Sets bits of color data for a bitmap.
294 * RETURNS
295 * Success: Number of bytes used in setting the bitmap bits
296 * Failure: 0
298 LONG WINAPI SetBitmapBits(
299 HBITMAP hbitmap, /* [in] Handle to bitmap */
300 LONG count, /* [in] Number of bytes in bitmap array */
301 LPCVOID bits) /* [in] Address of array with bitmap bits */
303 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
304 LONG height, ret;
306 if ((!bmp) || (!bits))
307 return 0;
309 if (count < 0) {
310 WARN("(%ld): Negative number of bytes passed???\n", count );
311 count = -count;
314 /* Only get entire lines */
315 height = count / bmp->bitmap.bmWidthBytes;
316 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
317 count = height * bmp->bitmap.bmWidthBytes;
319 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
320 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
321 1 << bmp->bitmap.bmBitsPixel, height );
323 if(bmp->funcs && bmp->funcs->pSetBitmapBits) {
325 TRACE("Calling device specific BitmapBits\n");
326 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
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->flags &= ~DC_DIRTY;
443 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
444 DC_InitDC( dc );
446 else ret = 0;
448 done:
449 GDI_ReleaseObj( hdc );
450 return ret;
454 /***********************************************************************
455 * BITMAP_DeleteObject
457 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
459 BITMAPOBJ * bmp = obj;
461 if (bmp->funcs && bmp->funcs->pDeleteBitmap)
462 bmp->funcs->pDeleteBitmap( handle );
464 if( bmp->bitmap.bmBits )
465 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
467 if (bmp->dib)
469 DIBSECTION *dib = bmp->dib;
471 if (dib->dsBm.bmBits)
473 if (dib->dshSection)
475 SYSTEM_INFO SystemInfo;
476 GetSystemInfo( &SystemInfo );
477 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
478 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
480 else if (!dib->dsOffset)
481 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
483 HeapFree(GetProcessHeap(), 0, dib);
484 bmp->dib = NULL;
485 if (bmp->segptr_bits)
486 { /* free its selector array */
487 WORD sel = SELECTOROF(bmp->segptr_bits);
488 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
489 int i;
491 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
494 return GDI_FreeObject( handle, obj );
498 /***********************************************************************
499 * BITMAP_GetObject16
501 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
503 BITMAPOBJ *bmp = obj;
505 if (bmp->dib)
507 if ( count <= sizeof(BITMAP16) )
509 BITMAP *bmp32 = &bmp->dib->dsBm;
510 BITMAP16 bmp16;
511 bmp16.bmType = bmp32->bmType;
512 bmp16.bmWidth = bmp32->bmWidth;
513 bmp16.bmHeight = bmp32->bmHeight;
514 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
515 bmp16.bmPlanes = bmp32->bmPlanes;
516 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
517 bmp16.bmBits = (SEGPTR)0;
518 memcpy( buffer, &bmp16, count );
519 return count;
521 else
523 FIXME("not implemented for DIBs: count %d\n", count);
524 return 0;
527 else
529 BITMAP16 bmp16;
530 bmp16.bmType = bmp->bitmap.bmType;
531 bmp16.bmWidth = bmp->bitmap.bmWidth;
532 bmp16.bmHeight = bmp->bitmap.bmHeight;
533 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
534 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
535 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
536 bmp16.bmBits = (SEGPTR)0;
537 if (count > sizeof(bmp16)) count = sizeof(bmp16);
538 memcpy( buffer, &bmp16, count );
539 return count;
544 /***********************************************************************
545 * BITMAP_GetObject
547 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
549 BITMAPOBJ *bmp = obj;
551 if (bmp->dib)
553 if( !buffer )
554 return sizeof(DIBSECTION);
555 if (count < sizeof(DIBSECTION))
557 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
559 else
561 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
564 memcpy( buffer, bmp->dib, count );
565 return count;
567 else
569 if( !buffer )
570 return sizeof(BITMAP);
571 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
572 memcpy( buffer, &bmp->bitmap, count );
573 return count;
578 /******************************************************************************
579 * CreateDiscardableBitmap [GDI32.@]
581 * 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.@]
599 * 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.@]
620 * Assigns dimensions to a bitmap.
622 * RETURNS
623 * Success: TRUE
624 * Failure: FALSE
626 BOOL WINAPI SetBitmapDimensionEx(
627 HBITMAP hbitmap, /* [in] Handle to bitmap */
628 INT x, /* [in] Bitmap width */
629 INT y, /* [in] Bitmap height */
630 LPSIZE prevSize) /* [out] Address of structure for orig dims */
632 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
633 if (!bmp) return FALSE;
634 if (prevSize) *prevSize = bmp->size;
635 bmp->size.cx = x;
636 bmp->size.cy = y;
637 GDI_ReleaseObj( hbitmap );
638 return TRUE;