Release 980503
[wine/hacks.git] / objects / bitmap.c
blob241eaf38d43235cd8c41b89f66b387be86bd45d7
1 /*
2 * GDI bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "ts_xlib.h"
11 #include "ts_xutil.h"
12 #include "gdi.h"
13 #include "callback.h"
14 #include "dc.h"
15 #include "bitmap.h"
16 #include "heap.h"
17 #include "debug.h"
19 #ifdef PRELIMINARY_WING16_SUPPORT
20 #include <sys/types.h>
21 #include <sys/ipc.h>
22 #include <sys/shm.h>
23 #endif
25 /* GCs used for B&W and color bitmap operations */
26 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
28 /***********************************************************************
29 * XPutImage_wrapper
31 * Wrapper to call XPutImage with CALL_LARGE_STACK.
34 struct XPutImage_descr
36 BITMAPOBJ *bmp;
37 XImage *image;
38 INT32 width;
39 INT32 height;
42 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
44 return XPutImage( display, descr->bmp->pixmap, BITMAP_GC(descr->bmp),
45 descr->image, 0, 0, 0, 0, descr->width, descr->height );
48 /***********************************************************************
49 * BITMAP_GetBitsPadding
51 * Return number of bytes to pad a scanline of 16-bit aligned Windows DDB data.
53 INT32 BITMAP_GetBitsPadding( int bmWidth, int bpp )
55 INT32 pad;
57 switch (bpp)
59 case 1:
60 if (!(bmWidth & 15)) pad = 0;
61 else pad = ((16 - (bmWidth & 15)) + 7) / 8;
62 break;
64 case 8:
65 pad = (2 - (bmWidth & 1)) & 1;
66 break;
68 case 24:
69 pad = (bmWidth*3) & 1;
70 break;
72 case 32:
73 case 16:
74 case 15:
75 pad = 0; /* we have 16bit alignment already */
76 break;
78 case 4:
79 if (!(bmWidth & 3)) pad = 0;
80 else pad = ((4 - (bmWidth & 3)) + 1) / 2;
81 break;
83 default:
84 WARN(bitmap,"Unknown depth %d, please report.\n", bpp );
85 return -1;
87 return pad;
90 /***********************************************************************
91 * BITMAP_GetBitsWidth
93 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB data.
95 INT32 BITMAP_GetBitsWidth( int bmWidth, int bpp )
97 switch(bpp)
99 case 1:
100 return 2 * ((bmWidth+15) >> 4);
102 case 24:
103 bmWidth *= 3; /* fall through */
104 case 8:
105 return bmWidth + (bmWidth & 1);
107 case 32:
108 return bmWidth * 4;
110 case 16:
111 case 15:
112 return bmWidth * 2;
114 case 4:
115 return 2 * ((bmWidth+3) >> 2);
117 default:
118 WARN(bitmap,"Unknown depth %d, please report.\n", bpp );
120 return -1;
123 /***********************************************************************
124 * CreateBitmap16 (GDI.48)
126 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
127 UINT16 bpp, LPCVOID bits )
129 return CreateBitmap32( width, height, planes, bpp, bits );
133 /******************************************************************************
134 * CreateBitmap32 [GDI32.25] Creates a bitmap with the specified info
136 * PARAMS
137 * width [I] bitmap width
138 * height [I] bitmap height
139 * planes [I] Number of color planes
140 * bpp [I] Number of bits to identify a color
141 * bits [I] Pointer to array containing color data
143 * RETURNS
144 * Success: Handle to bitmap
145 * Failure: NULL
147 HBITMAP32 WINAPI CreateBitmap32( INT32 width, INT32 height, UINT32 planes,
148 UINT32 bpp, LPCVOID bits )
150 BITMAPOBJ * bmpObjPtr;
151 HBITMAP32 hbitmap;
153 planes = (BYTE)planes;
154 bpp = (BYTE)bpp;
156 TRACE(gdi, "%dx%d, %d colors\n", width, height, 1 << (planes*bpp) );
158 /* Check parameters */
159 if (!height || !width || planes != 1) return 0;
160 if ((bpp != 1) && (bpp != screenDepth)) return 0;
161 if (height < 0) height = -height;
162 if (width < 0) width = -width;
164 /* Create the BITMAPOBJ */
165 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
166 if (!hbitmap) return 0;
167 bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LOCK( hbitmap );
169 bmpObjPtr->size.cx = 0;
170 bmpObjPtr->size.cy = 0;
171 bmpObjPtr->bitmap.bmType = 0;
172 bmpObjPtr->bitmap.bmWidth = (INT16)width;
173 bmpObjPtr->bitmap.bmHeight = (INT16)height;
174 bmpObjPtr->bitmap.bmPlanes = (BYTE)planes;
175 bmpObjPtr->bitmap.bmBitsPixel = (BYTE)bpp;
176 bmpObjPtr->bitmap.bmWidthBytes = (INT16)BITMAP_WIDTH_BYTES( width, bpp );
177 bmpObjPtr->bitmap.bmBits = NULL;
179 /* Create the pixmap */
180 bmpObjPtr->pixmap = TSXCreatePixmap(display, rootWindow, width, height, bpp);
181 if (!bmpObjPtr->pixmap)
183 GDI_HEAP_FREE( hbitmap );
184 hbitmap = 0;
186 else if (bits) /* Set bitmap bits */
187 SetBitmapBits32( hbitmap, height * bmpObjPtr->bitmap.bmWidthBytes,
188 bits );
189 GDI_HEAP_UNLOCK( hbitmap );
190 return hbitmap;
194 /***********************************************************************
195 * CreateCompatibleBitmap16 (GDI.51)
197 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
199 return CreateCompatibleBitmap32( hdc, width, height );
203 /******************************************************************************
204 * CreateCompatibleBitmap32 [GDI32.30] Creates a bitmap compatible with the DC
206 * PARAMS
207 * hdc [I] Handle to device context
208 * width [I] Width of bitmap
209 * height [I] Height of bitmap
211 * RETURNS
212 * Success: Handle to bitmap
213 * Failure: NULL
215 HBITMAP32 WINAPI CreateCompatibleBitmap32( HDC32 hdc, INT32 width, INT32 height)
217 HBITMAP32 hbmpRet = 0;
218 DC *dc;
220 TRACE(gdi, "(%04x,%d,%d) = \n", hdc, width, height );
221 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
222 if ((width >0x1000) || (height > 0x1000))
224 FIXME(gdi,"got bad width %d or height %d, please look for reason\n",
225 width, height );
226 return 0;
228 hbmpRet = CreateBitmap32( width, height, 1, dc->w.bitsPerPixel, NULL );
229 TRACE(gdi,"\t\t%04x\n", hbmpRet);
230 return hbmpRet;
234 /***********************************************************************
235 * CreateBitmapIndirect16 (GDI.49)
237 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
239 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
240 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
244 /******************************************************************************
245 * CreateBitmapIndirect32 [GDI32.26] Creates a bitmap with the specifies info
247 * RETURNS
248 * Success: Handle to bitmap
249 * Failure: NULL
251 HBITMAP32 WINAPI CreateBitmapIndirect32(
252 const BITMAP32 * bmp) /* [in] Pointer to the bitmap data */
254 return CreateBitmap32( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
255 bmp->bmBitsPixel, bmp->bmBits );
259 /***********************************************************************
260 * BITMAP_GetXImage
262 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
264 XImage *BITMAP_GetXImage( const BITMAPOBJ *bmp )
266 return XGetImage( display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
267 bmp->bitmap.bmHeight, AllPlanes, ZPixmap );
271 /***********************************************************************
272 * GetBitmapBits16 (GDI.74)
274 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
276 return GetBitmapBits32( hbitmap, count, buffer );
280 /***********************************************************************
281 * GetBitmapBits32 [GDI32.143] Copies bitmap bits of bitmap to buffer
283 * RETURNS
284 * Success: Number of bytes copied
285 * Failure: 0
287 LONG WINAPI GetBitmapBits32(
288 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
289 LONG count, /* [in] Number of bytes to copy */
290 LPVOID buffer) /* [out] Pointer to buffer to receive bits */
292 BITMAPOBJ * bmp;
293 LONG height, old_height;
294 XImage * image;
295 LPBYTE tbuf;
296 int h,w,pad;
298 /* KLUDGE! */
299 if (count < 0) {
300 WARN(bitmap, "(%ld): Negative number of bytes passed???\n", count );
301 count = -count;
303 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
304 if (!bmp) return 0;
306 /* Only get entire lines */
307 height = count / bmp->bitmap.bmWidthBytes;
308 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
310 TRACE(bitmap, "%dx%d %d colors %p fetched height: %ld\n",
311 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
312 1 << bmp->bitmap.bmBitsPixel, buffer, height );
314 pad = BITMAP_GetBitsPadding( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
316 if (!height || (pad == -1))
318 GDI_HEAP_UNLOCK( hbitmap );
319 return 0;
322 EnterCriticalSection( &X11DRV_CritSection );
324 /* Hack: change the bitmap height temporarily to avoid */
325 /* getting unnecessary bitmap rows. */
326 old_height = bmp->bitmap.bmHeight;
327 bmp->bitmap.bmHeight = height;
328 image = (XImage *)CALL_LARGE_STACK( BITMAP_GetXImage, bmp );
329 bmp->bitmap.bmHeight = old_height;
331 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
333 tbuf = buffer;
334 switch (bmp->bitmap.bmBitsPixel)
336 case 1:
337 for (h=0;h<height;h++)
339 *tbuf = 0;
340 for (w=0;w<bmp->bitmap.bmWidth;w++)
342 if ((w%8) == 0)
343 *tbuf = 0;
344 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
345 if ((w&7) == 7) ++tbuf;
347 tbuf += pad;
349 break;
350 case 4:
351 for (h=0;h<height;h++)
353 for (w=0;w<bmp->bitmap.bmWidth;w++)
355 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
356 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
358 tbuf += pad;
360 break;
361 case 8:
362 for (h=0;h<height;h++)
364 for (w=0;w<bmp->bitmap.bmWidth;w++)
365 *tbuf++ = XGetPixel(image,w,h);
366 tbuf += pad;
368 break;
369 case 15:
370 case 16:
371 for (h=0;h<height;h++)
373 for (w=0;w<bmp->bitmap.bmWidth;w++)
375 long pixel = XGetPixel(image,w,h);
377 *tbuf++ = pixel & 0xff;
378 *tbuf++ = (pixel>>8) & 0xff;
381 break;
382 case 24:
383 for (h=0;h<height;h++)
385 for (w=0;w<bmp->bitmap.bmWidth;w++)
387 long pixel = XGetPixel(image,w,h);
389 *tbuf++ = pixel & 0xff;
390 *tbuf++ = (pixel>> 8) & 0xff;
391 *tbuf++ = (pixel>>16) & 0xff;
393 tbuf += pad;
395 break;
396 default:
397 FIXME(bitmap, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
399 XDestroyImage( image );
400 LeaveCriticalSection( &X11DRV_CritSection );
402 GDI_HEAP_UNLOCK( hbitmap );
403 return height * bmp->bitmap.bmWidthBytes;
407 /***********************************************************************
408 * SetBitmapBits16 (GDI.106)
410 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
412 return SetBitmapBits32( hbitmap, count, buffer );
416 /******************************************************************************
417 * SetBitmapBits32 [GDI32.303] Sets bits of color data for a bitmap
419 * RETURNS
420 * Success: Number of bytes used in setting the bitmap bits
421 * Failure: 0
423 LONG WINAPI SetBitmapBits32(
424 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
425 LONG count, /* [in] Number of bytes in bitmap array */
426 LPCVOID buffer) /* [in] Address of array with bitmap bits */
428 struct XPutImage_descr descr;
429 BITMAPOBJ * bmp;
430 LONG height;
431 XImage * image;
432 LPBYTE sbuf,tmpbuffer;
433 int w,h,pad,widthbytes;
435 /* KLUDGE! */
436 if (count < 0) {
437 WARN(bitmap, "(%ld): Negative number of bytes passed???\n", count );
438 count = -count;
440 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
441 if (!bmp) return 0;
443 TRACE(bitmap, "%dx%d %d colors %p\n",
444 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
445 1 << bmp->bitmap.bmBitsPixel, buffer );
447 /* Only set entire lines */
448 height = count / bmp->bitmap.bmWidthBytes;
449 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
451 pad = BITMAP_GetBitsPadding( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
453 if (!height || (pad == -1))
455 GDI_HEAP_UNLOCK( hbitmap );
456 return 0;
459 sbuf = (LPBYTE)buffer;
461 widthbytes = DIB_GetXImageWidthBytes(bmp->bitmap.bmWidth,bmp->bitmap.bmBitsPixel);
462 tmpbuffer = (LPBYTE)xmalloc(widthbytes*height);
464 EnterCriticalSection( &X11DRV_CritSection );
465 image = XCreateImage( display, DefaultVisualOfScreen(screen),
466 bmp->bitmap.bmBitsPixel, ZPixmap, 0, tmpbuffer,
467 bmp->bitmap.bmWidth,height,32,widthbytes );
469 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
470 sbuf = (LPBYTE)buffer;
471 switch (bmp->bitmap.bmBitsPixel)
473 case 1:
474 for (h=0;h<height;h++)
476 for (w=0;w<bmp->bitmap.bmWidth;w++)
478 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
479 if ((w&7) == 7)
480 sbuf++;
482 sbuf += pad;
484 break;
485 case 4:
486 for (h=0;h<height;h++)
488 for (w=0;w<bmp->bitmap.bmWidth;w++)
490 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
491 else XPutPixel( image, w, h, *sbuf++ & 0xf );
493 sbuf += pad;
495 break;
496 case 8:
497 for (h=0;h<height;h++)
499 for (w=0;w<bmp->bitmap.bmWidth;w++)
500 XPutPixel(image,w,h,*sbuf++);
501 sbuf += pad;
503 break;
504 case 15:
505 case 16:
506 for (h=0;h<height;h++)
508 for (w=0;w<bmp->bitmap.bmWidth;w++)
510 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
511 sbuf+=2;
514 break;
515 case 24:
516 for (h=0;h<height;h++)
518 for (w=0;w<bmp->bitmap.bmWidth;w++)
520 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
521 sbuf += 3;
523 sbuf += pad;
525 break;
528 descr.bmp = bmp;
529 descr.image = image;
530 descr.width = bmp->bitmap.bmWidth;
531 descr.height = height;
532 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
533 XDestroyImage( image ); /* frees tmpbuffer too */
534 LeaveCriticalSection( &X11DRV_CritSection );
536 GDI_HEAP_UNLOCK( hbitmap );
537 return height * bmp->bitmap.bmWidthBytes;
540 /***********************************************************************
541 * LoadImage16 [USER.389]
544 HANDLE16 WINAPI LoadImage16( HINSTANCE16 hinst, LPCSTR name, UINT16 type,
545 INT16 desiredx, INT16 desiredy, UINT16 loadflags)
547 if (HIWORD(name)) {
548 TRACE(resource,"(0x%04x,%s,%d,%d,%d,0x%08x)\n",
549 hinst,(char *)PTR_SEG_TO_LIN(name),type,desiredx,desiredy,loadflags);
550 } else {
551 TRACE(resource,"LoadImage16(0x%04x,%p,%d,%d,%d,0x%08x)\n",
552 hinst,name,type,desiredx,desiredy,loadflags);
554 switch (type) {
555 case IMAGE_BITMAP:
556 return LoadBitmap16(hinst,(SEGPTR)name);
557 case IMAGE_ICON:
558 return LoadIcon16(hinst,(SEGPTR)name);
559 case IMAGE_CURSOR:
560 return LoadCursor16(hinst,(SEGPTR)name);
562 return 0;
566 /**********************************************************************
567 * LoadImage32A (USER32.365)
569 * FIXME: implementation still lacks nearly all features, see LR_*
570 * defines in windows.h
573 HANDLE32 WINAPI LoadImage32A( HINSTANCE32 hinst, LPCSTR name, UINT32 type,
574 INT32 desiredx, INT32 desiredy, UINT32 loadflags)
576 if (HIWORD(name)) {
577 TRACE(resource,"(0x%04x,%s,%d,%d,%d,0x%08x)\n",
578 hinst,name,type,desiredx,desiredy,loadflags
580 } else {
581 TRACE(resource,"(0x%04x,%p,%d,%d,%d,0x%08x)\n",
582 hinst,name,type,desiredx,desiredy,loadflags
585 switch (type) {
586 case IMAGE_BITMAP:
587 return LoadBitmap32A(hinst,name);
588 case IMAGE_ICON:
589 return LoadIcon32A(hinst,name);
590 case IMAGE_CURSOR:
591 return LoadCursor32A(hinst,name);
593 return 0;
596 /**********************************************************************
597 * LoadImage32W (USER32.366)
599 * FIXME: implementation still lacks nearly all features, see LR_*
600 * defines in windows.h
604 /******************************************************************************
605 * LoadImage32W [USER32.366] Loads an icon, cursor, or bitmap
607 * PARAMS
608 * hinst [I] Handle of instance that contains image
609 * name [I] Name of image
610 * type [I] Type of image
611 * desiredx [I] Desired width
612 * desiredy [I] Desired height
613 * loadflags [I] Load flags
615 * RETURNS
616 * Success: Handle to newly loaded image
617 * Failure: NULL
619 * BUGS
620 * Implementation still lacks nearly all features, see LR_*
621 * defines in windows.h
623 HANDLE32 WINAPI LoadImage32W( HINSTANCE32 hinst, LPCWSTR name, UINT32 type,
624 INT32 desiredx, INT32 desiredy, UINT32 loadflags )
626 if (HIWORD(name)) {
627 TRACE(resource,"(0x%04x,%p,%d,%d,%d,0x%08x)\n",
628 hinst,name,type,desiredx,desiredy,loadflags
630 } else {
631 TRACE(resource,"(0x%04x,%p,%d,%d,%d,0x%08x)\n",
632 hinst,name,type,desiredx,desiredy,loadflags
636 switch (type) {
637 case IMAGE_BITMAP:
638 return LoadBitmap32W(hinst,name);
639 case IMAGE_ICON:
640 return LoadIcon32W(hinst,name);
641 case IMAGE_CURSOR:
642 return LoadCursor32W(hinst,name);
644 return NULL;
648 /**********************************************************************
649 * CopyBitmap32 (not an API)
651 * NOTES
652 * If it is not an API, why is it declared with WINAPI?
655 HBITMAP32 WINAPI CopyBitmap32 (HBITMAP32 hnd)
657 HBITMAP32 res = 0;
658 BITMAP32 bmp;
660 if (GetObject32A (hnd, sizeof (bmp), &bmp))
662 res = CreateBitmapIndirect32 (&bmp);
663 SetBitmapBits32 (res, bmp.bmWidthBytes * bmp.bmHeight, bmp.bmBits);
665 return res;
669 /******************************************************************************
670 * CopyImage32 [USER32.61] Creates new image and copies attributes to it
672 * PARAMS
673 * hnd [I] Handle to image to copy
674 * type [I] Type of image to copy
675 * desiredx [I] Desired width of new image
676 * desiredy [I] Desired height of new image
677 * flags [I] Copy flags
679 * RETURNS
680 * Success: Handle to newly created image
681 * Failure: NULL
683 * FIXME: implementation still lacks nearly all features, see LR_*
684 * defines in windows.h
686 HANDLE32 WINAPI CopyImage32( HANDLE32 hnd, UINT32 type, INT32 desiredx,
687 INT32 desiredy, UINT32 flags )
689 switch (type)
691 case IMAGE_BITMAP:
692 return CopyBitmap32(hnd);
693 case IMAGE_ICON:
694 return CopyIcon32(hnd);
695 case IMAGE_CURSOR:
696 return CopyCursor32(hnd);
698 return 0;
702 /**********************************************************************
703 * LoadBitmap16 (USER.175)
705 * NOTES
706 * Can this call LoadBitmap32?
708 HBITMAP16 WINAPI LoadBitmap16( HINSTANCE16 instance, SEGPTR name )
710 HBITMAP32 hbitmap = 0;
711 HDC32 hdc;
712 HRSRC16 hRsrc;
713 HGLOBAL16 handle;
714 BITMAPINFO *info;
716 if (HIWORD(name))
718 char *str = (char *)PTR_SEG_TO_LIN( name );
719 TRACE(bitmap, "(%04x,'%s')\n", instance, str );
720 if (str[0] == '#') name = (SEGPTR)(DWORD)(WORD)atoi( str + 1 );
722 else
723 TRACE(bitmap, "(%04x,%04x)\n",
724 instance, LOWORD(name) );
726 if (!instance) /* OEM bitmap */
728 if (HIWORD((int)name)) return 0;
729 return OBM_LoadBitmap( LOWORD((int)name) );
732 if (!(hRsrc = FindResource16( instance, name, RT_BITMAP16 ))) return 0;
733 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
735 info = (BITMAPINFO *)LockResource16( handle );
736 if ((hdc = GetDC32(0)) != 0)
738 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
739 hbitmap = CreateDIBitmap32( hdc, &info->bmiHeader, CBM_INIT,
740 bits, info, DIB_RGB_COLORS );
741 ReleaseDC32( 0, hdc );
743 FreeResource16( handle );
744 return hbitmap;
748 /******************************************************************************
749 * LoadBitmap32W [USER32.358] Loads bitmap from the executable file
751 * RETURNS
752 * Success: Handle to specified bitmap
753 * Failure: NULL
755 HBITMAP32 WINAPI LoadBitmap32W(
756 HINSTANCE32 instance, /* [in] Handle to application instance */
757 LPCWSTR name) /* [in] Address of bitmap resource name */
759 HBITMAP32 hbitmap = 0;
760 HDC32 hdc;
761 HRSRC32 hRsrc;
762 HGLOBAL32 handle;
763 BITMAPINFO *info;
765 if (!instance) /* OEM bitmap */
767 if (HIWORD((int)name)) return 0;
768 return OBM_LoadBitmap( LOWORD((int)name) );
771 if (!(hRsrc = FindResource32W( instance, name, RT_BITMAP32W ))) return 0;
772 if (!(handle = LoadResource32( instance, hRsrc ))) return 0;
774 info = (BITMAPINFO *)LockResource32( handle );
775 if ((hdc = GetDC32(0)) != 0)
777 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
778 hbitmap = CreateDIBitmap32( hdc, &info->bmiHeader, CBM_INIT,
779 bits, info, DIB_RGB_COLORS );
780 ReleaseDC32( 0, hdc );
782 return hbitmap;
786 /**********************************************************************
787 * LoadBitmap32A (USER32.357)
789 HBITMAP32 WINAPI LoadBitmap32A( HINSTANCE32 instance, LPCSTR name )
791 HBITMAP32 res;
792 if (!HIWORD(name)) res = LoadBitmap32W( instance, (LPWSTR)name );
793 else
795 LPWSTR uni = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
796 res = LoadBitmap32W( instance, uni );
797 HeapFree( GetProcessHeap(), 0, uni );
799 return res;
803 /***********************************************************************
804 * BITMAP_DeleteObject
806 BOOL32 BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
808 #ifdef PRELIMINARY_WING16_SUPPORT
809 if( bmp->bitmap.bmBits )
810 TSXShmDetach( display, (XShmSegmentInfo*)bmp->bitmap.bmBits );
811 #endif
813 TSXFreePixmap( display, bmp->pixmap );
814 #ifdef PRELIMINARY_WING16_SUPPORT
815 if( bmp->bitmap.bmBits )
817 __ShmBitmapCtl* p = (__ShmBitmapCtl*)bmp->bitmap.bmBits;
818 WORD sel = HIWORD(p->bits);
819 unsigned long l, limit = GetSelectorLimit(sel);
821 for( l = 0; l < limit; l += 0x10000, sel += __AHINCR )
822 FreeSelector(sel);
823 shmctl(p->si.shmid, IPC_RMID, NULL);
824 shmdt(p->si.shmaddr); /* already marked for destruction */
826 #endif
827 return GDI_FreeObject( hbitmap );
831 /***********************************************************************
832 * BITMAP_GetObject16
834 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
836 if (count > sizeof(bmp->bitmap)) count = sizeof(bmp->bitmap);
837 memcpy( buffer, &bmp->bitmap, count );
838 return count;
842 /***********************************************************************
843 * BITMAP_GetObject32
845 INT32 BITMAP_GetObject32( BITMAPOBJ * bmp, INT32 count, LPVOID buffer )
847 BITMAP32 bmp32;
848 bmp32.bmType = bmp->bitmap.bmType;
849 bmp32.bmWidth = bmp->bitmap.bmWidth;
850 bmp32.bmHeight = bmp->bitmap.bmHeight;
851 bmp32.bmWidthBytes = bmp->bitmap.bmWidthBytes;
852 bmp32.bmPlanes = bmp->bitmap.bmPlanes;
853 bmp32.bmBitsPixel = bmp->bitmap.bmBitsPixel;
854 bmp32.bmBits = NULL;
855 if (count > sizeof(bmp32)) count = sizeof(bmp32);
856 memcpy( buffer, &bmp32, count );
857 return count;
861 /***********************************************************************
862 * CreateDiscardableBitmap16 (GDI.156)
864 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
865 INT16 height )
867 return CreateCompatibleBitmap16( hdc, width, height );
871 /******************************************************************************
872 * CreateDiscardableBitmap32 [GDI32.38] Creates a discardable bitmap
874 * RETURNS
875 * Success: Handle to bitmap
876 * Failure: NULL
878 HBITMAP32 WINAPI CreateDiscardableBitmap32(
879 HDC32 hdc, /* [in] Handle to device context */
880 INT32 width, /* [in] Bitmap width */
881 INT32 height) /* [in] Bitmap height */
883 return CreateCompatibleBitmap32( hdc, width, height );
887 /***********************************************************************
888 * GetBitmapDimensionEx16 (GDI.468)
890 * NOTES
891 * Can this call GetBitmapDimensionEx32?
893 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
895 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
896 if (!bmp) return FALSE;
897 *size = bmp->size;
898 GDI_HEAP_UNLOCK( hbitmap );
899 return TRUE;
903 /******************************************************************************
904 * GetBitmapDimensionEx32 [GDI32.144] Retrieves dimensions of a bitmap
906 * RETURNS
907 * Success: TRUE
908 * Failure: FALSE
910 BOOL32 WINAPI GetBitmapDimensionEx32(
911 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
912 LPSIZE32 size) /* [out] Address of struct receiving dimensions */
914 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
915 if (!bmp) return FALSE;
916 size->cx = (INT32)bmp->size.cx;
917 size->cy = (INT32)bmp->size.cy;
918 GDI_HEAP_UNLOCK( hbitmap );
919 return TRUE;
923 /***********************************************************************
924 * GetBitmapDimension (GDI.162)
926 DWORD WINAPI GetBitmapDimension( HBITMAP16 hbitmap )
928 SIZE16 size;
929 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
930 return MAKELONG( size.cx, size.cy );
934 /***********************************************************************
935 * SetBitmapDimensionEx16 (GDI.478)
937 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
938 LPSIZE16 prevSize )
940 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
941 if (!bmp) return FALSE;
942 if (prevSize) *prevSize = bmp->size;
943 bmp->size.cx = x;
944 bmp->size.cy = y;
945 GDI_HEAP_UNLOCK( hbitmap );
946 return TRUE;
950 /******************************************************************************
951 * SetBitmapDimensionEx32 [GDI32.304] Assignes dimensions to a bitmap
953 * RETURNS
954 * Success: TRUE
955 * Failure: FALSE
957 BOOL32 WINAPI SetBitmapDimensionEx32(
958 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
959 INT32 x, /* [in] Bitmap width */
960 INT32 y, /* [in] Bitmap height */
961 LPSIZE32 prevSize) /* [out] Address of structure for orig dims */
963 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
964 if (!bmp) return FALSE;
965 if (prevSize) CONV_SIZE16TO32( &bmp->size, prevSize );
966 bmp->size.cx = (INT16)x;
967 bmp->size.cy = (INT16)y;
968 GDI_HEAP_UNLOCK( hbitmap );
969 return TRUE;
973 /***********************************************************************
974 * SetBitmapDimension (GDI.163)
976 DWORD WINAPI SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
978 SIZE16 size;
979 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
980 return MAKELONG( size.cx, size.cy );