- Add standard dll path last instead of first.
[wine.git] / objects / bitmap.c
blob8d36f4825a2a4b903ce3e1edfddd267ce9acac8f
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 "wine/debug.h"
29 #include "wine/winuser16.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.@] Creates a bitmap with the specified info
87 * PARAMS
88 * width [I] bitmap width
89 * height [I] bitmap height
90 * planes [I] Number of color planes
91 * bpp [I] Number of bits to identify a color
92 * bits [I] Pointer to array containing color data
94 * RETURNS
95 * Success: Handle to bitmap
96 * Failure: 0
98 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
99 UINT bpp, LPCVOID bits )
101 BITMAPOBJ *bmp;
102 HBITMAP hbitmap;
104 planes = (BYTE)planes;
105 bpp = (BYTE)bpp;
108 /* Check parameters */
109 if (!height || !width)
111 height = 1;
112 width = 1;
113 planes = 1;
114 bpp = 1;
116 if (planes != 1) {
117 FIXME("planes = %d\n", planes);
118 return 0;
120 if (height < 0) height = -height;
121 if (width < 0) width = -width;
123 /* Create the BITMAPOBJ */
124 if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC,
125 (HGDIOBJ *)&hbitmap, &bitmap_funcs )))
126 return 0;
128 TRACE("%dx%d, %d colors returning %p\n", width, height, 1 << (planes*bpp), hbitmap);
130 bmp->size.cx = 0;
131 bmp->size.cy = 0;
132 bmp->bitmap.bmType = 0;
133 bmp->bitmap.bmWidth = width;
134 bmp->bitmap.bmHeight = height;
135 bmp->bitmap.bmPlanes = planes;
136 bmp->bitmap.bmBitsPixel = bpp;
137 bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
138 bmp->bitmap.bmBits = NULL;
140 bmp->funcs = NULL;
141 bmp->physBitmap = NULL;
142 bmp->dib = NULL;
143 bmp->segptr_bits = 0;
145 if (bits) /* Set bitmap bits */
146 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
147 bits );
148 GDI_ReleaseObj( hbitmap );
149 return hbitmap;
153 /******************************************************************************
154 * CreateCompatibleBitmap [GDI32.@] Creates a bitmap compatible with the DC
156 * PARAMS
157 * hdc [I] Handle to device context
158 * width [I] Width of bitmap
159 * height [I] Height of bitmap
161 * RETURNS
162 * Success: Handle to bitmap
163 * Failure: 0
165 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
167 HBITMAP hbmpRet = 0;
168 DC *dc;
170 TRACE("(%p,%d,%d) = \n", hdc, width, height );
171 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
172 if ((width >= 0x10000) || (height >= 0x10000)) {
173 FIXME("got bad width %d or height %d, please look for reason\n",
174 width, height );
175 } else {
176 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
177 if (!width || !height)
178 hbmpRet = CreateBitmap( 1, 1, 1, 1, NULL );
179 else
180 hbmpRet = CreateBitmap( width, height, 1, dc->bitsPerPixel, NULL );
182 TRACE("\t\t%p\n", hbmpRet);
183 GDI_ReleaseObj(hdc);
184 return hbmpRet;
188 /******************************************************************************
189 * CreateBitmapIndirect [GDI32.@] Creates a bitmap with the specifies info
191 * RETURNS
192 * Success: Handle to bitmap
193 * Failure: NULL
195 HBITMAP WINAPI CreateBitmapIndirect(
196 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
198 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
199 bmp->bmBitsPixel, bmp->bmBits );
203 /***********************************************************************
204 * GetBitmapBits [GDI32.@] Copies bitmap bits of bitmap to buffer
206 * RETURNS
207 * Success: Number of bytes copied
208 * Failure: 0
210 LONG WINAPI GetBitmapBits(
211 HBITMAP hbitmap, /* [in] Handle to bitmap */
212 LONG count, /* [in] Number of bytes to copy */
213 LPVOID bits) /* [out] Pointer to buffer to receive bits */
215 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
216 LONG height, ret;
218 if (!bmp) return 0;
220 /* If the bits vector is null, the function should return the read size */
221 if(bits == NULL)
223 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
224 goto done;
227 if (count < 0) {
228 WARN("(%ld): Negative number of bytes passed???\n", count );
229 count = -count;
232 /* Only get entire lines */
233 height = count / bmp->bitmap.bmWidthBytes;
234 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
235 count = height * bmp->bitmap.bmWidthBytes;
236 if (count == 0)
238 WARN("Less than one entire line requested\n");
239 ret = 0;
240 goto done;
244 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
245 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
246 1 << bmp->bitmap.bmBitsPixel, height );
248 if(bmp->funcs)
250 TRACE("Calling device specific BitmapBits\n");
251 if(bmp->funcs->pGetBitmapBits)
252 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
253 else
255 memset( bits, 0, count );
256 ret = count;
258 } else {
260 if(!bmp->bitmap.bmBits) {
261 WARN("Bitmap is empty\n");
262 ret = 0;
263 } else {
264 memcpy(bits, bmp->bitmap.bmBits, count);
265 ret = count;
269 done:
270 GDI_ReleaseObj( hbitmap );
271 return ret;
275 /******************************************************************************
276 * SetBitmapBits [GDI32.@] Sets bits of color data for a bitmap
278 * RETURNS
279 * Success: Number of bytes used in setting the bitmap bits
280 * Failure: 0
282 LONG WINAPI SetBitmapBits(
283 HBITMAP hbitmap, /* [in] Handle to bitmap */
284 LONG count, /* [in] Number of bytes in bitmap array */
285 LPCVOID bits) /* [in] Address of array with bitmap bits */
287 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
288 LONG height, ret;
290 if ((!bmp) || (!bits))
291 return 0;
293 if (count < 0) {
294 WARN("(%ld): Negative number of bytes passed???\n", count );
295 count = -count;
298 /* Only get entire lines */
299 height = count / bmp->bitmap.bmWidthBytes;
300 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
301 count = height * bmp->bitmap.bmWidthBytes;
303 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
304 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
305 1 << bmp->bitmap.bmBitsPixel, height );
307 if(bmp->funcs) {
309 TRACE("Calling device specific BitmapBits\n");
310 if(bmp->funcs->pSetBitmapBits)
311 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
312 else
313 ret = 0;
314 } else {
316 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
317 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
318 if(!bmp->bitmap.bmBits) {
319 WARN("Unable to allocate bit buffer\n");
320 ret = 0;
321 } else {
322 memcpy(bmp->bitmap.bmBits, bits, count);
323 ret = count;
327 GDI_ReleaseObj( hbitmap );
328 return ret;
331 /**********************************************************************
332 * BITMAP_CopyBitmap
335 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
337 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
338 HBITMAP res = 0;
339 BITMAP bm;
341 if(!bmp) return 0;
343 bm = bmp->bitmap;
344 bm.bmBits = NULL;
345 res = CreateBitmapIndirect(&bm);
347 if(res) {
348 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
349 bm.bmHeight );
350 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
351 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
352 HeapFree( GetProcessHeap(), 0, buf );
355 GDI_ReleaseObj( hbitmap );
356 return res;
360 /***********************************************************************
361 * BITMAP_SetOwnerDC
363 * Set the type of DC that owns the bitmap. This is used when the
364 * bitmap is selected into a device to initialize the bitmap function
365 * table.
367 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
369 BITMAPOBJ *bitmap;
370 BOOL ret;
372 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
373 if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
375 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
377 ret = TRUE;
378 if (!bitmap->funcs) /* not owned by a DC yet */
380 if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap );
381 if (ret) bitmap->funcs = dc->funcs;
383 else if (bitmap->funcs != dc->funcs)
385 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
386 ret = FALSE;
388 GDI_ReleaseObj( hbitmap );
389 return ret;
393 /***********************************************************************
394 * BITMAP_SelectObject
396 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
398 HGDIOBJ ret;
399 BITMAPOBJ *bitmap = obj;
400 DC *dc = DC_GetDCPtr( hdc );
402 if (!dc) return 0;
403 if (GetObjectType( hdc ) != OBJ_MEMDC)
405 GDI_ReleaseObj( hdc );
406 return 0;
408 ret = dc->hBitmap;
409 if (handle == dc->hBitmap) goto done; /* nothing to do */
411 if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
413 WARN( "Bitmap already selected in another DC\n" );
414 GDI_ReleaseObj( hdc );
415 return 0;
418 if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
420 GDI_ReleaseObj( hdc );
421 return 0;
424 if (dc->funcs->pSelectBitmap) handle = dc->funcs->pSelectBitmap( dc->physDev, handle );
426 if (handle)
428 dc->hBitmap = handle;
429 dc->totalExtent.left = 0;
430 dc->totalExtent.top = 0;
431 dc->totalExtent.right = bitmap->bitmap.bmWidth;
432 dc->totalExtent.bottom = bitmap->bitmap.bmHeight;
433 dc->flags &= ~DC_DIRTY;
434 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
435 CLIPPING_UpdateGCRegion( dc );
437 if (dc->bitsPerPixel != bitmap->bitmap.bmBitsPixel)
439 /* depth changed, reinitialize the DC */
440 dc->bitsPerPixel = bitmap->bitmap.bmBitsPixel;
441 DC_InitDC( dc );
444 else ret = 0;
446 done:
447 GDI_ReleaseObj( hdc );
448 return ret;
452 /***********************************************************************
453 * BITMAP_DeleteObject
455 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
457 BITMAPOBJ * bmp = obj;
459 if (bmp->funcs && bmp->funcs->pDeleteBitmap)
460 bmp->funcs->pDeleteBitmap( handle );
462 if( bmp->bitmap.bmBits )
463 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
465 if (bmp->dib)
467 DIBSECTION *dib = bmp->dib;
469 if (dib->dsBm.bmBits)
471 if (dib->dshSection)
473 SYSTEM_INFO SystemInfo;
474 GetSystemInfo( &SystemInfo );
475 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
476 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
478 else if (!dib->dsOffset)
479 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
481 HeapFree(GetProcessHeap(), 0, dib);
482 bmp->dib = NULL;
483 if (bmp->segptr_bits)
484 { /* free its selector array */
485 WORD sel = SELECTOROF(bmp->segptr_bits);
486 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
487 int i;
489 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
492 return GDI_FreeObject( handle, obj );
496 /***********************************************************************
497 * BITMAP_GetObject16
499 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
501 BITMAPOBJ *bmp = obj;
503 if (bmp->dib)
505 if ( count <= sizeof(BITMAP16) )
507 BITMAP *bmp32 = &bmp->dib->dsBm;
508 BITMAP16 bmp16;
509 bmp16.bmType = bmp32->bmType;
510 bmp16.bmWidth = bmp32->bmWidth;
511 bmp16.bmHeight = bmp32->bmHeight;
512 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
513 bmp16.bmPlanes = bmp32->bmPlanes;
514 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
515 bmp16.bmBits = (SEGPTR)0;
516 memcpy( buffer, &bmp16, count );
517 return count;
519 else
521 FIXME("not implemented for DIBs: count %d\n", count);
522 return 0;
525 else
527 BITMAP16 bmp16;
528 bmp16.bmType = bmp->bitmap.bmType;
529 bmp16.bmWidth = bmp->bitmap.bmWidth;
530 bmp16.bmHeight = bmp->bitmap.bmHeight;
531 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
532 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
533 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
534 bmp16.bmBits = (SEGPTR)0;
535 if (count > sizeof(bmp16)) count = sizeof(bmp16);
536 memcpy( buffer, &bmp16, count );
537 return count;
542 /***********************************************************************
543 * BITMAP_GetObject
545 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
547 BITMAPOBJ *bmp = obj;
549 if (bmp->dib)
551 if( !buffer )
552 return sizeof(DIBSECTION);
553 if (count < sizeof(DIBSECTION))
555 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
557 else
559 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
562 memcpy( buffer, bmp->dib, count );
563 return count;
565 else
567 if( !buffer )
568 return sizeof(BITMAP);
569 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
570 memcpy( buffer, &bmp->bitmap, count );
571 return count;
576 /******************************************************************************
577 * CreateDiscardableBitmap [GDI32.@] Creates a discardable bitmap
579 * RETURNS
580 * Success: Handle to bitmap
581 * Failure: NULL
583 HBITMAP WINAPI CreateDiscardableBitmap(
584 HDC hdc, /* [in] Handle to device context */
585 INT width, /* [in] Bitmap width */
586 INT height) /* [in] Bitmap height */
588 return CreateCompatibleBitmap( hdc, width, height );
592 /******************************************************************************
593 * GetBitmapDimensionEx [GDI32.@] Retrieves dimensions of a bitmap
595 * RETURNS
596 * Success: TRUE
597 * Failure: FALSE
599 BOOL WINAPI GetBitmapDimensionEx(
600 HBITMAP hbitmap, /* [in] Handle to bitmap */
601 LPSIZE size) /* [out] Address of struct receiving dimensions */
603 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
604 if (!bmp) return FALSE;
605 *size = bmp->size;
606 GDI_ReleaseObj( hbitmap );
607 return TRUE;
611 /******************************************************************************
612 * SetBitmapDimensionEx [GDI32.@] Assignes dimensions to a bitmap
614 * RETURNS
615 * Success: TRUE
616 * Failure: FALSE
618 BOOL WINAPI SetBitmapDimensionEx(
619 HBITMAP hbitmap, /* [in] Handle to bitmap */
620 INT x, /* [in] Bitmap width */
621 INT y, /* [in] Bitmap height */
622 LPSIZE prevSize) /* [out] Address of structure for orig dims */
624 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
625 if (!bmp) return FALSE;
626 if (prevSize) *prevSize = bmp->size;
627 bmp->size.cx = x;
628 bmp->size.cy = y;
629 GDI_ReleaseObj( hbitmap );
630 return TRUE;