- fixed ELF modules' size information
[wine.git] / objects / bitmap.c
blob8858c16a529eab4ee72d58701a87420df854917c
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.@]
88 * Creates a bitmap with the specified info.
90 * PARAMS
91 * width [I] bitmap width
92 * height [I] bitmap height
93 * planes [I] Number of color planes
94 * bpp [I] Number of bits to identify a color
95 * bits [I] Pointer to array containing color data
97 * RETURNS
98 * Success: Handle to bitmap
99 * Failure: 0
101 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
102 UINT bpp, LPCVOID bits )
104 BITMAPOBJ *bmp;
105 HBITMAP hbitmap;
107 planes = (BYTE)planes;
108 bpp = (BYTE)bpp;
111 /* Check parameters */
112 if (!height || !width)
114 height = 1;
115 width = 1;
116 planes = 1;
117 bpp = 1;
119 if (planes != 1) {
120 FIXME("planes = %d\n", planes);
121 return 0;
123 if (height < 0) height = -height;
124 if (width < 0) width = -width;
126 /* Create the BITMAPOBJ */
127 if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC,
128 (HGDIOBJ *)&hbitmap, &bitmap_funcs )))
129 return 0;
131 TRACE("%dx%d, %d colors returning %p\n", width, height, 1 << (planes*bpp), hbitmap);
133 bmp->size.cx = 0;
134 bmp->size.cy = 0;
135 bmp->bitmap.bmType = 0;
136 bmp->bitmap.bmWidth = width;
137 bmp->bitmap.bmHeight = height;
138 bmp->bitmap.bmPlanes = planes;
139 bmp->bitmap.bmBitsPixel = bpp;
140 bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
141 bmp->bitmap.bmBits = NULL;
143 bmp->funcs = NULL;
144 bmp->physBitmap = NULL;
145 bmp->dib = NULL;
146 bmp->segptr_bits = 0;
148 if (bits) /* Set bitmap bits */
149 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
150 bits );
151 GDI_ReleaseObj( hbitmap );
152 return hbitmap;
156 /******************************************************************************
157 * CreateCompatibleBitmap [GDI32.@]
159 * Creates a bitmap compatible with the DC.
161 * PARAMS
162 * hdc [I] Handle to device context
163 * width [I] Width of bitmap
164 * height [I] Height of bitmap
166 * RETURNS
167 * Success: Handle to bitmap
168 * Failure: 0
170 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
172 HBITMAP hbmpRet = 0;
173 DC *dc;
175 TRACE("(%p,%d,%d) = \n", hdc, width, height );
176 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
177 if ((width >= 0x10000) || (height >= 0x10000)) {
178 FIXME("got bad width %d or height %d, please look for reason\n",
179 width, height );
181 else
183 INT planes, bpp;
185 if (GDIMAGIC( dc->header.wMagic ) != MEMORY_DC_MAGIC)
187 planes = GetDeviceCaps( hdc, PLANES );
188 bpp = GetDeviceCaps( hdc, BITSPIXEL );
190 else /* memory DC, get the depth of the bitmap */
192 BITMAPOBJ *bmp = GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC );
193 planes = bmp->bitmap.bmPlanes;
194 bpp = bmp->bitmap.bmBitsPixel;
195 GDI_ReleaseObj( dc->hBitmap );
197 hbmpRet = CreateBitmap( width, height, planes, bpp, NULL );
199 TRACE("\t\t%p\n", hbmpRet);
200 GDI_ReleaseObj(hdc);
201 return hbmpRet;
205 /******************************************************************************
206 * CreateBitmapIndirect [GDI32.@]
208 * Creates a bitmap with the specifies info.
210 * RETURNS
211 * Success: Handle to bitmap
212 * Failure: NULL
214 HBITMAP WINAPI CreateBitmapIndirect(
215 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
217 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
218 bmp->bmBitsPixel, bmp->bmBits );
222 /***********************************************************************
223 * GetBitmapBits [GDI32.@]
225 * Copies bitmap bits of bitmap to buffer.
227 * RETURNS
228 * Success: Number of bytes copied
229 * Failure: 0
231 LONG WINAPI GetBitmapBits(
232 HBITMAP hbitmap, /* [in] Handle to bitmap */
233 LONG count, /* [in] Number of bytes to copy */
234 LPVOID bits) /* [out] Pointer to buffer to receive bits */
236 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
237 LONG height, ret;
239 if (!bmp) return 0;
241 /* If the bits vector is null, the function should return the read size */
242 if(bits == NULL)
244 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
245 goto done;
248 if (count < 0) {
249 WARN("(%ld): Negative number of bytes passed???\n", count );
250 count = -count;
253 /* Only get entire lines */
254 height = count / bmp->bitmap.bmWidthBytes;
255 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
256 count = height * bmp->bitmap.bmWidthBytes;
257 if (count == 0)
259 WARN("Less than one entire line requested\n");
260 ret = 0;
261 goto done;
265 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
266 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
267 1 << bmp->bitmap.bmBitsPixel, height );
269 if(bmp->funcs)
271 TRACE("Calling device specific BitmapBits\n");
272 if(bmp->funcs->pGetBitmapBits)
273 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
274 else
276 memset( bits, 0, count );
277 ret = count;
279 } else {
281 if(!bmp->bitmap.bmBits) {
282 WARN("Bitmap is empty\n");
283 ret = 0;
284 } else {
285 memcpy(bits, bmp->bitmap.bmBits, count);
286 ret = count;
290 done:
291 GDI_ReleaseObj( hbitmap );
292 return ret;
296 /******************************************************************************
297 * SetBitmapBits [GDI32.@]
299 * Sets bits of color data for a bitmap.
301 * RETURNS
302 * Success: Number of bytes used in setting the bitmap bits
303 * Failure: 0
305 LONG WINAPI SetBitmapBits(
306 HBITMAP hbitmap, /* [in] Handle to bitmap */
307 LONG count, /* [in] Number of bytes in bitmap array */
308 LPCVOID bits) /* [in] Address of array with bitmap bits */
310 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
311 LONG height, ret;
313 if ((!bmp) || (!bits))
314 return 0;
316 if (count < 0) {
317 WARN("(%ld): Negative number of bytes passed???\n", count );
318 count = -count;
321 /* Only get entire lines */
322 height = count / bmp->bitmap.bmWidthBytes;
323 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
324 count = height * bmp->bitmap.bmWidthBytes;
326 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
327 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
328 1 << bmp->bitmap.bmBitsPixel, height );
330 if(bmp->funcs) {
332 TRACE("Calling device specific BitmapBits\n");
333 if(bmp->funcs->pSetBitmapBits)
334 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
335 else
336 ret = 0;
337 } else {
339 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
340 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
341 if(!bmp->bitmap.bmBits) {
342 WARN("Unable to allocate bit buffer\n");
343 ret = 0;
344 } else {
345 memcpy(bmp->bitmap.bmBits, bits, count);
346 ret = count;
350 GDI_ReleaseObj( hbitmap );
351 return ret;
354 /**********************************************************************
355 * BITMAP_CopyBitmap
358 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
360 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
361 HBITMAP res = 0;
362 BITMAP bm;
364 if(!bmp) return 0;
366 bm = bmp->bitmap;
367 bm.bmBits = NULL;
368 res = CreateBitmapIndirect(&bm);
370 if(res) {
371 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
372 bm.bmHeight );
373 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
374 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
375 HeapFree( GetProcessHeap(), 0, buf );
378 GDI_ReleaseObj( hbitmap );
379 return res;
383 /***********************************************************************
384 * BITMAP_SetOwnerDC
386 * Set the type of DC that owns the bitmap. This is used when the
387 * bitmap is selected into a device to initialize the bitmap function
388 * table.
390 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
392 BITMAPOBJ *bitmap;
393 BOOL ret;
395 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
396 if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
398 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
400 ret = TRUE;
401 if (!bitmap->funcs) /* not owned by a DC yet */
403 if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap );
404 if (ret) bitmap->funcs = dc->funcs;
406 else if (bitmap->funcs != dc->funcs)
408 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
409 ret = FALSE;
411 GDI_ReleaseObj( hbitmap );
412 return ret;
416 /***********************************************************************
417 * BITMAP_SelectObject
419 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
421 HGDIOBJ ret;
422 BITMAPOBJ *bitmap = obj;
423 DC *dc = DC_GetDCPtr( hdc );
425 if (!dc) return 0;
426 if (GetObjectType( hdc ) != OBJ_MEMDC)
428 GDI_ReleaseObj( hdc );
429 return 0;
431 ret = dc->hBitmap;
432 if (handle == dc->hBitmap) goto done; /* nothing to do */
434 if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
436 WARN( "Bitmap already selected in another DC\n" );
437 GDI_ReleaseObj( hdc );
438 return 0;
441 if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
443 GDI_ReleaseObj( hdc );
444 return 0;
447 if (dc->funcs->pSelectBitmap) handle = dc->funcs->pSelectBitmap( dc->physDev, handle );
449 if (handle)
451 dc->hBitmap = handle;
452 dc->flags &= ~DC_DIRTY;
453 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
454 DC_InitDC( dc );
456 else ret = 0;
458 done:
459 GDI_ReleaseObj( hdc );
460 return ret;
464 /***********************************************************************
465 * BITMAP_DeleteObject
467 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
469 BITMAPOBJ * bmp = obj;
471 if (bmp->funcs && bmp->funcs->pDeleteBitmap)
472 bmp->funcs->pDeleteBitmap( handle );
474 if( bmp->bitmap.bmBits )
475 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
477 if (bmp->dib)
479 DIBSECTION *dib = bmp->dib;
481 if (dib->dsBm.bmBits)
483 if (dib->dshSection)
485 SYSTEM_INFO SystemInfo;
486 GetSystemInfo( &SystemInfo );
487 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
488 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
490 else if (!dib->dsOffset)
491 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
493 HeapFree(GetProcessHeap(), 0, dib);
494 bmp->dib = NULL;
495 if (bmp->segptr_bits)
496 { /* free its selector array */
497 WORD sel = SELECTOROF(bmp->segptr_bits);
498 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
499 int i;
501 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
504 return GDI_FreeObject( handle, obj );
508 /***********************************************************************
509 * BITMAP_GetObject16
511 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
513 BITMAPOBJ *bmp = obj;
515 if (bmp->dib)
517 if ( count <= sizeof(BITMAP16) )
519 BITMAP *bmp32 = &bmp->dib->dsBm;
520 BITMAP16 bmp16;
521 bmp16.bmType = bmp32->bmType;
522 bmp16.bmWidth = bmp32->bmWidth;
523 bmp16.bmHeight = bmp32->bmHeight;
524 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
525 bmp16.bmPlanes = bmp32->bmPlanes;
526 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
527 bmp16.bmBits = (SEGPTR)0;
528 memcpy( buffer, &bmp16, count );
529 return count;
531 else
533 FIXME("not implemented for DIBs: count %d\n", count);
534 return 0;
537 else
539 BITMAP16 bmp16;
540 bmp16.bmType = bmp->bitmap.bmType;
541 bmp16.bmWidth = bmp->bitmap.bmWidth;
542 bmp16.bmHeight = bmp->bitmap.bmHeight;
543 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
544 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
545 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
546 bmp16.bmBits = (SEGPTR)0;
547 if (count > sizeof(bmp16)) count = sizeof(bmp16);
548 memcpy( buffer, &bmp16, count );
549 return count;
554 /***********************************************************************
555 * BITMAP_GetObject
557 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
559 BITMAPOBJ *bmp = obj;
561 if (bmp->dib)
563 if( !buffer )
564 return sizeof(DIBSECTION);
565 if (count < sizeof(DIBSECTION))
567 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
569 else
571 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
574 memcpy( buffer, bmp->dib, count );
575 return count;
577 else
579 if( !buffer )
580 return sizeof(BITMAP);
581 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
582 memcpy( buffer, &bmp->bitmap, count );
583 return count;
588 /******************************************************************************
589 * CreateDiscardableBitmap [GDI32.@]
591 * Creates a discardable bitmap.
593 * RETURNS
594 * Success: Handle to bitmap
595 * Failure: NULL
597 HBITMAP WINAPI CreateDiscardableBitmap(
598 HDC hdc, /* [in] Handle to device context */
599 INT width, /* [in] Bitmap width */
600 INT height) /* [in] Bitmap height */
602 return CreateCompatibleBitmap( hdc, width, height );
606 /******************************************************************************
607 * GetBitmapDimensionEx [GDI32.@]
609 * Retrieves dimensions of a bitmap.
611 * RETURNS
612 * Success: TRUE
613 * Failure: FALSE
615 BOOL WINAPI GetBitmapDimensionEx(
616 HBITMAP hbitmap, /* [in] Handle to bitmap */
617 LPSIZE size) /* [out] Address of struct receiving dimensions */
619 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
620 if (!bmp) return FALSE;
621 *size = bmp->size;
622 GDI_ReleaseObj( hbitmap );
623 return TRUE;
627 /******************************************************************************
628 * SetBitmapDimensionEx [GDI32.@]
630 * Assigns dimensions to a bitmap.
632 * RETURNS
633 * Success: TRUE
634 * Failure: FALSE
636 BOOL WINAPI SetBitmapDimensionEx(
637 HBITMAP hbitmap, /* [in] Handle to bitmap */
638 INT x, /* [in] Bitmap width */
639 INT y, /* [in] Bitmap height */
640 LPSIZE prevSize) /* [out] Address of structure for orig dims */
642 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
643 if (!bmp) return FALSE;
644 if (prevSize) *prevSize = bmp->size;
645 bmp->size.cx = x;
646 bmp->size.cy = y;
647 GDI_ReleaseObj( hbitmap );
648 return TRUE;