4 * Copyright 1993 Alexandre Julliard
11 #include "wine/winbase16.h"
17 #include "sysmetrics.h"
18 #include "cursoricon.h"
21 #include "wine/winuser16.h"
23 DECLARE_DEBUG_CHANNEL(bitmap)
24 DECLARE_DEBUG_CHANNEL(resource)
26 BITMAP_DRIVER *BITMAP_Driver = NULL;
29 /***********************************************************************
30 * BITMAP_GetWidthBytes
32 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
35 INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
40 return 2 * ((bmWidth+15) >> 4);
43 bmWidth *= 3; /* fall through */
45 return bmWidth + (bmWidth & 1);
55 return 2 * ((bmWidth+3) >> 2);
58 WARN(bitmap,"Unknown depth %d, please report.\n", bpp );
63 /***********************************************************************
64 * CreateUserBitmap16 (GDI.407)
66 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
67 UINT16 bpp, LPCVOID bits )
69 return CreateBitmap16( width, height, planes, bpp, bits );
72 /***********************************************************************
73 * CreateUserDiscardableBitmap16 (GDI.409)
75 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy,
76 INT16 width, INT16 height )
78 return CreateUserBitmap16( width, height, 1, MONITOR_GetDepth(&MONITOR_PrimaryMonitor), NULL );
82 /***********************************************************************
83 * CreateBitmap16 (GDI.48)
85 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
86 UINT16 bpp, LPCVOID bits )
88 return CreateBitmap( width, height, planes, bpp, bits );
92 /******************************************************************************
93 * CreateBitmap32 [GDI32.25] Creates a bitmap with the specified info
96 * width [I] bitmap width
97 * height [I] bitmap height
98 * planes [I] Number of color planes
99 * bpp [I] Number of bits to identify a color
100 * bits [I] Pointer to array containing color data
103 * Success: Handle to bitmap
106 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
107 UINT bpp, LPCVOID bits )
112 planes = (BYTE)planes;
116 /* Check parameters */
117 if (!height || !width) return 0;
119 FIXME(bitmap, "planes = %d\n", planes);
122 if (height < 0) height = -height;
123 if (width < 0) width = -width;
125 /* Create the BITMAPOBJ */
126 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
127 if (!hbitmap) return 0;
129 TRACE(bitmap, "%dx%d, %d colors returning %08x\n", width, height,
130 1 << (planes*bpp), hbitmap);
132 bmp = (BITMAPOBJ *) GDI_HEAP_LOCK( hbitmap );
136 bmp->bitmap.bmType = 0;
137 bmp->bitmap.bmWidth = width;
138 bmp->bitmap.bmHeight = height;
139 bmp->bitmap.bmPlanes = planes;
140 bmp->bitmap.bmBitsPixel = bpp;
141 bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
142 bmp->bitmap.bmBits = NULL;
144 bmp->DDBitmap = NULL;
147 if (bits) /* Set bitmap bits */
148 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
150 GDI_HEAP_UNLOCK( hbitmap );
155 /***********************************************************************
156 * CreateCompatibleBitmap16 (GDI.51)
158 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
160 return CreateCompatibleBitmap( hdc, width, height );
164 /******************************************************************************
165 * CreateCompatibleBitmap32 [GDI32.30] Creates a bitmap compatible with the DC
168 * hdc [I] Handle to device context
169 * width [I] Width of bitmap
170 * height [I] Height of bitmap
173 * Success: Handle to bitmap
176 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
181 TRACE(bitmap, "(%04x,%d,%d) = \n", hdc, width, height );
182 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
183 if ((width >= 0x10000) || (height >= 0x10000)) {
184 FIXME(bitmap,"got bad width %d or height %d, please look for reason\n",
187 hbmpRet = CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
188 if(dc->funcs->pCreateBitmap)
189 dc->funcs->pCreateBitmap( hbmpRet );
191 TRACE(bitmap,"\t\t%04x\n", hbmpRet);
192 GDI_HEAP_UNLOCK(hdc);
197 /***********************************************************************
198 * CreateBitmapIndirect16 (GDI.49)
200 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
202 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
203 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
207 /******************************************************************************
208 * CreateBitmapIndirect32 [GDI32.26] Creates a bitmap with the specifies info
211 * Success: Handle to bitmap
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 * GetBitmapBits16 (GDI.74)
225 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
227 return GetBitmapBits( hbitmap, count, buffer );
231 /***********************************************************************
232 * GetBitmapBits32 [GDI32.143] Copies bitmap bits of bitmap to buffer
235 * Success: Number of bytes copied
238 LONG WINAPI GetBitmapBits(
239 HBITMAP hbitmap, /* [in] Handle to bitmap */
240 LONG count, /* [in] Number of bytes to copy */
241 LPVOID bits) /* [out] Pointer to buffer to receive bits */
243 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
249 WARN(bitmap, "(%ld): Negative number of bytes passed???\n", 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;
259 WARN(bitmap, "Less then one entire line requested\n");
260 GDI_HEAP_UNLOCK( hbitmap );
265 TRACE(bitmap, "(%08x, %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 );
271 TRACE(bitmap, "Calling device specific BitmapBits\n");
272 if(bmp->DDBitmap->funcs->pBitmapBits)
273 ret = bmp->DDBitmap->funcs->pBitmapBits(hbitmap, bits, count,
276 ERR(bitmap, "BitmapBits == NULL??\n");
282 if(!bmp->bitmap.bmBits) {
283 WARN(bitmap, "Bitmap is empty\n");
286 memcpy(bits, bmp->bitmap.bmBits, count);
292 GDI_HEAP_UNLOCK( hbitmap );
297 /***********************************************************************
298 * SetBitmapBits16 (GDI.106)
300 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
302 return SetBitmapBits( hbitmap, count, buffer );
306 /******************************************************************************
307 * SetBitmapBits32 [GDI32.303] Sets bits of color data for a bitmap
310 * Success: Number of bytes used in setting the bitmap bits
313 LONG WINAPI SetBitmapBits(
314 HBITMAP hbitmap, /* [in] Handle to bitmap */
315 LONG count, /* [in] Number of bytes in bitmap array */
316 LPCVOID bits) /* [in] Address of array with bitmap bits */
318 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
324 WARN(bitmap, "(%ld): Negative number of bytes passed???\n", count );
328 /* Only get entire lines */
329 height = count / bmp->bitmap.bmWidthBytes;
330 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
331 count = height * bmp->bitmap.bmWidthBytes;
333 TRACE(bitmap, "(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
334 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
335 1 << bmp->bitmap.bmBitsPixel, height );
339 TRACE(bitmap, "Calling device specific BitmapBits\n");
340 if(bmp->DDBitmap->funcs->pBitmapBits)
341 ret = bmp->DDBitmap->funcs->pBitmapBits(hbitmap, (void *) bits,
344 ERR(bitmap, "BitmapBits == NULL??\n");
350 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
351 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
352 if(!bmp->bitmap.bmBits) {
353 WARN(bitmap, "Unable to allocate bit buffer\n");
356 memcpy(bmp->bitmap.bmBits, bits, count);
361 GDI_HEAP_UNLOCK( hbitmap );
365 /***********************************************************************
366 * LoadImage16 [USER.389]
369 HANDLE16 WINAPI LoadImage16( HINSTANCE16 hinst, LPCSTR name, UINT16 type,
370 INT16 desiredx, INT16 desiredy, UINT16 loadflags)
372 LPCSTR nameStr = HIWORD(name)? PTR_SEG_TO_LIN(name) : (LPCSTR)name;
373 return LoadImageA( hinst, nameStr, type,
374 desiredx, desiredy, loadflags );
377 /**********************************************************************
378 * LoadImageA (USER32.365)
380 * FIXME: implementation lacks some features, see LR_ defines in windows.h
383 HANDLE WINAPI LoadImageA( HINSTANCE hinst, LPCSTR name, UINT type,
384 INT desiredx, INT desiredy, UINT loadflags)
389 if (HIWORD(name)) u_name = HEAP_strdupAtoW(GetProcessHeap(), 0, name);
390 else u_name=(LPWSTR)name;
391 res = LoadImageW(hinst, u_name, type, desiredx, desiredy, loadflags);
392 if (HIWORD(name)) HeapFree(GetProcessHeap(), 0, u_name);
397 /******************************************************************************
398 * LoadImageW [USER32.366] Loads an icon, cursor, or bitmap
401 * hinst [I] Handle of instance that contains image
402 * name [I] Name of image
403 * type [I] Type of image
404 * desiredx [I] Desired width
405 * desiredy [I] Desired height
406 * loadflags [I] Load flags
409 * Success: Handle to newly loaded image
412 * FIXME: Implementation lacks some features, see LR_ defines in windows.h
414 HANDLE WINAPI LoadImageW( HINSTANCE hinst, LPCWSTR name, UINT type,
415 INT desiredx, INT desiredy, UINT loadflags )
418 TRACE(resource,"(0x%04x,%p,%d,%d,%d,0x%08x)\n",
419 hinst,name,type,desiredx,desiredy,loadflags);
421 TRACE(resource,"(0x%04x,%p,%d,%d,%d,0x%08x)\n",
422 hinst,name,type,desiredx,desiredy,loadflags);
424 if (loadflags & LR_DEFAULTSIZE) {
425 if (type == IMAGE_ICON) {
426 if (!desiredx) desiredx = SYSMETRICS_CXICON;
427 if (!desiredy) desiredy = SYSMETRICS_CYICON;
428 } else if (type == IMAGE_CURSOR) {
429 if (!desiredx) desiredx = SYSMETRICS_CXCURSOR;
430 if (!desiredy) desiredy = SYSMETRICS_CYCURSOR;
433 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
436 return BITMAP_Load( hinst, name, loadflags );
441 UINT palEnts = GetSystemPaletteEntries(hdc, 0, 0, NULL);
444 return CURSORICON_Load(hinst, name, desiredx, desiredy,
445 MIN(16, palEnts), FALSE, loadflags);
449 return CURSORICON_Load(hinst, name, desiredx, desiredy,
456 /**********************************************************************
460 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
462 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
470 res = CreateBitmapIndirect(&bm);
473 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
475 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
476 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
477 HeapFree( GetProcessHeap(), 0, buf );
480 GDI_HEAP_UNLOCK( hbitmap );
484 /******************************************************************************
485 * CopyImage16 [USER.390] Creates new image and copies attributes to it
488 HICON16 WINAPI CopyImage16( HANDLE16 hnd, UINT16 type, INT16 desiredx,
489 INT16 desiredy, UINT16 flags )
491 return (HICON16)CopyImage((HANDLE)hnd, (UINT)type, (INT)desiredx,
492 (INT)desiredy, (UINT)flags);
495 /******************************************************************************
496 * CopyImage32 [USER32.61] Creates new image and copies attributes to it
499 * hnd [I] Handle to image to copy
500 * type [I] Type of image to copy
501 * desiredx [I] Desired width of new image
502 * desiredy [I] Desired height of new image
503 * flags [I] Copy flags
506 * Success: Handle to newly created image
509 * FIXME: implementation still lacks nearly all features, see LR_*
510 * defines in windows.h
512 HICON WINAPI CopyImage( HANDLE hnd, UINT type, INT desiredx,
513 INT desiredy, UINT flags )
518 return BITMAP_CopyBitmap(hnd);
520 return CopyIcon(hnd);
522 return CopyCursor(hnd);
527 /**********************************************************************
530 HBITMAP BITMAP_Load( HINSTANCE instance,LPCWSTR name, UINT loadflags )
537 BITMAPINFO *info, *fix_info=NULL;
541 if (!(loadflags & LR_LOADFROMFILE)) {
542 if (!instance) /* OEM bitmap */
547 if (HIWORD((int)name)) return 0;
548 hdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
549 dc = DC_GetDCPtr( hdc );
550 if(dc->funcs->pLoadOEMResource)
551 hbitmap = dc->funcs->pLoadOEMResource( LOWORD((int)name),
553 GDI_HEAP_UNLOCK( hdc );
558 if (!(hRsrc = FindResourceW( instance, name, RT_BITMAPW ))) return 0;
559 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
561 if ((info = (BITMAPINFO *)LockResource( handle )) == NULL) return 0;
565 if (!(ptr = (char *)VIRTUAL_MapFileW( name ))) return 0;
566 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
568 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
569 if ((hFix = GlobalAlloc(0, size))) fix_info=GlobalLock(hFix);
573 memcpy(fix_info, info, size);
574 pix = *((LPBYTE)info+DIB_BitmapInfoSize(info, DIB_RGB_COLORS));
575 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
576 if ((hdc = GetDC(0)) != 0) {
577 char *bits = (char *)info + size;
578 if (loadflags & LR_CREATEDIBSECTION) {
580 hbitmap = CreateDIBSection(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
581 GetObjectA(hbitmap, sizeof(DIBSECTION), &dib);
582 SetDIBits(hdc, hbitmap, 0, dib.dsBm.bmHeight, bits, info,
586 hbitmap = CreateDIBitmap( hdc, &fix_info->bmiHeader, CBM_INIT,
587 bits, fix_info, DIB_RGB_COLORS );
594 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
599 /******************************************************************************
600 * LoadBitmapW [USER32.358] Loads bitmap from the executable file
603 * Success: Handle to specified bitmap
606 HBITMAP WINAPI LoadBitmapW(
607 HINSTANCE instance, /* [in] Handle to application instance */
608 LPCWSTR name) /* [in] Address of bitmap resource name */
610 return LoadImageW( instance, name, IMAGE_BITMAP, 0, 0, 0 );
613 /**********************************************************************
614 * LoadBitmapA (USER32.357)
616 HBITMAP WINAPI LoadBitmapA( HINSTANCE instance, LPCSTR name )
618 return LoadImageA( instance, name, IMAGE_BITMAP, 0, 0, 0 );
621 /**********************************************************************
622 * LoadBitmap16 (USER.175)
624 HBITMAP16 WINAPI LoadBitmap16( HINSTANCE16 instance, SEGPTR name )
626 LPCSTR nameStr = HIWORD(name)? PTR_SEG_TO_LIN(name) : (LPCSTR)name;
627 return LoadBitmapA( instance, nameStr );
632 /***********************************************************************
633 * BITMAP_DeleteObject
635 BOOL BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
637 if( bmp->DDBitmap ) {
638 if( bmp->DDBitmap->funcs->pDeleteObject )
639 bmp->DDBitmap->funcs->pDeleteObject( hbitmap );
642 if( bmp->bitmap.bmBits )
643 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
645 DIB_DeleteDIBSection( bmp );
647 return GDI_FreeObject( hbitmap );
651 /***********************************************************************
654 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
658 if ( count <= sizeof(BITMAP16) )
660 BITMAP *bmp32 = &bmp->dib->dsBm;
662 bmp16.bmType = bmp32->bmType;
663 bmp16.bmWidth = bmp32->bmWidth;
664 bmp16.bmHeight = bmp32->bmHeight;
665 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
666 bmp16.bmPlanes = bmp32->bmPlanes;
667 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
668 bmp16.bmBits = (SEGPTR)0;
669 memcpy( buffer, &bmp16, count );
674 FIXME(bitmap, "not implemented for DIBs: count %d\n", count);
681 bmp16.bmType = bmp->bitmap.bmType;
682 bmp16.bmWidth = bmp->bitmap.bmWidth;
683 bmp16.bmHeight = bmp->bitmap.bmHeight;
684 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
685 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
686 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
687 bmp16.bmBits = (SEGPTR)0;
688 if (count > sizeof(bmp16)) count = sizeof(bmp16);
689 memcpy( buffer, &bmp16, count );
695 /***********************************************************************
698 INT BITMAP_GetObject( BITMAPOBJ * bmp, INT count, LPVOID buffer )
702 if (count < sizeof(DIBSECTION))
704 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
708 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
711 memcpy( buffer, bmp->dib, count );
716 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
717 memcpy( buffer, &bmp->bitmap, count );
723 /***********************************************************************
724 * CreateDiscardableBitmap16 (GDI.156)
726 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
729 return CreateCompatibleBitmap16( hdc, width, height );
733 /******************************************************************************
734 * CreateDiscardableBitmap32 [GDI32.38] Creates a discardable bitmap
737 * Success: Handle to bitmap
740 HBITMAP WINAPI CreateDiscardableBitmap(
741 HDC hdc, /* [in] Handle to device context */
742 INT width, /* [in] Bitmap width */
743 INT height) /* [in] Bitmap height */
745 return CreateCompatibleBitmap( hdc, width, height );
749 /***********************************************************************
750 * GetBitmapDimensionEx16 (GDI.468)
753 * Can this call GetBitmapDimensionEx32?
755 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
757 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
758 if (!bmp) return FALSE;
759 CONV_SIZE32TO16( &bmp->size, size );
760 GDI_HEAP_UNLOCK( hbitmap );
765 /******************************************************************************
766 * GetBitmapDimensionEx32 [GDI32.144] Retrieves dimensions of a bitmap
772 BOOL WINAPI GetBitmapDimensionEx(
773 HBITMAP hbitmap, /* [in] Handle to bitmap */
774 LPSIZE size) /* [out] Address of struct receiving dimensions */
776 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
777 if (!bmp) return FALSE;
779 GDI_HEAP_UNLOCK( hbitmap );
784 /***********************************************************************
785 * GetBitmapDimension (GDI.162)
787 DWORD WINAPI GetBitmapDimension16( HBITMAP16 hbitmap )
790 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
791 return MAKELONG( size.cx, size.cy );
795 /***********************************************************************
796 * SetBitmapDimensionEx16 (GDI.478)
798 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
801 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
802 if (!bmp) return FALSE;
803 if (prevSize) CONV_SIZE32TO16( &bmp->size, prevSize );
806 GDI_HEAP_UNLOCK( hbitmap );
811 /******************************************************************************
812 * SetBitmapDimensionEx32 [GDI32.304] Assignes dimensions to a bitmap
818 BOOL WINAPI SetBitmapDimensionEx(
819 HBITMAP hbitmap, /* [in] Handle to bitmap */
820 INT x, /* [in] Bitmap width */
821 INT y, /* [in] Bitmap height */
822 LPSIZE prevSize) /* [out] Address of structure for orig dims */
824 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
825 if (!bmp) return FALSE;
826 if (prevSize) *prevSize = bmp->size;
829 GDI_HEAP_UNLOCK( hbitmap );
834 /***********************************************************************
835 * SetBitmapDimension (GDI.163)
837 DWORD WINAPI SetBitmapDimension16( HBITMAP16 hbitmap, INT16 x, INT16 y )
840 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
841 return MAKELONG( size.cx, size.cy );