Release 980927
[wine.git] / objects / bitmap.c
blob2da43745e1b5d2f215f6f68030477054da2b37ec
1 /*
2 * GDI bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include "ts_xlib.h"
10 #include "ts_xutil.h"
11 #include "gdi.h"
12 #include "callback.h"
13 #include "dc.h"
14 #include "bitmap.h"
15 #include "heap.h"
16 #include "debug.h"
18 #ifdef PRELIMINARY_WING16_SUPPORT
19 #include <sys/types.h>
20 #include <sys/ipc.h>
21 #include <sys/shm.h>
22 #endif
24 /* GCs used for B&W and color bitmap operations */
25 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
27 /***********************************************************************
28 * XPutImage_wrapper
30 * Wrapper to call XPutImage with CALL_LARGE_STACK.
33 struct XPutImage_descr
35 BITMAPOBJ *bmp;
36 XImage *image;
37 INT32 width;
38 INT32 height;
41 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
43 return XPutImage( display, descr->bmp->pixmap, BITMAP_GC(descr->bmp),
44 descr->image, 0, 0, 0, 0, descr->width, descr->height );
47 /***********************************************************************
48 * BITMAP_GetBitsPadding
50 * Return number of bytes to pad a scanline of 16-bit aligned Windows DDB data.
52 INT32 BITMAP_GetBitsPadding( int bmWidth, int bpp )
54 INT32 pad;
56 switch (bpp)
58 case 1:
59 if (!(bmWidth & 15)) pad = 0;
60 else pad = ((16 - (bmWidth & 15)) + 7) / 8;
61 break;
63 case 8:
64 pad = (2 - (bmWidth & 1)) & 1;
65 break;
67 case 24:
68 pad = (bmWidth*3) & 1;
69 break;
71 case 32:
72 case 16:
73 case 15:
74 pad = 0; /* we have 16bit alignment already */
75 break;
77 case 4:
78 if (!(bmWidth & 3)) pad = 0;
79 else pad = ((4 - (bmWidth & 3)) + 1) / 2;
80 break;
82 default:
83 WARN(bitmap,"Unknown depth %d, please report.\n", bpp );
84 return -1;
86 return pad;
89 /***********************************************************************
90 * BITMAP_GetBitsWidth
92 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB data.
94 INT32 BITMAP_GetBitsWidth( int bmWidth, int bpp )
96 switch(bpp)
98 case 1:
99 return 2 * ((bmWidth+15) >> 4);
101 case 24:
102 bmWidth *= 3; /* fall through */
103 case 8:
104 return bmWidth + (bmWidth & 1);
106 case 32:
107 return bmWidth * 4;
109 case 16:
110 case 15:
111 return bmWidth * 2;
113 case 4:
114 return 2 * ((bmWidth+3) >> 2);
116 default:
117 WARN(bitmap,"Unknown depth %d, please report.\n", bpp );
119 return -1;
122 /***********************************************************************
123 * CreateBitmap16 (GDI.48)
125 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
126 UINT16 bpp, LPCVOID bits )
128 return CreateBitmap32( width, height, planes, bpp, bits );
132 /******************************************************************************
133 * CreateBitmap32 [GDI32.25] Creates a bitmap with the specified info
135 * PARAMS
136 * width [I] bitmap width
137 * height [I] bitmap height
138 * planes [I] Number of color planes
139 * bpp [I] Number of bits to identify a color
140 * bits [I] Pointer to array containing color data
142 * RETURNS
143 * Success: Handle to bitmap
144 * Failure: NULL
146 HBITMAP32 WINAPI CreateBitmap32( INT32 width, INT32 height, UINT32 planes,
147 UINT32 bpp, LPCVOID bits )
149 BITMAPOBJ * bmpObjPtr;
150 HBITMAP32 hbitmap;
152 planes = (BYTE)planes;
153 bpp = (BYTE)bpp;
155 TRACE(gdi, "%dx%d, %d colors\n", width, height, 1 << (planes*bpp) );
157 /* Check parameters */
158 if (!height || !width || planes != 1) return 0;
159 if ((bpp != 1) && (bpp != screenDepth)) return 0;
160 if (height < 0) height = -height;
161 if (width < 0) width = -width;
163 /* Create the BITMAPOBJ */
164 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
165 if (!hbitmap) return 0;
166 bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LOCK( hbitmap );
168 bmpObjPtr->size.cx = 0;
169 bmpObjPtr->size.cy = 0;
170 bmpObjPtr->bitmap.bmType = 0;
171 bmpObjPtr->bitmap.bmWidth = (INT16)width;
172 bmpObjPtr->bitmap.bmHeight = (INT16)height;
173 bmpObjPtr->bitmap.bmPlanes = (BYTE)planes;
174 bmpObjPtr->bitmap.bmBitsPixel = (BYTE)bpp;
175 bmpObjPtr->bitmap.bmWidthBytes = (INT16)BITMAP_WIDTH_BYTES( width, bpp );
176 bmpObjPtr->bitmap.bmBits = NULL;
178 bmpObjPtr->dib = NULL;
180 /* Create the pixmap */
181 bmpObjPtr->pixmap = TSXCreatePixmap(display, rootWindow, width, height, bpp);
182 if (!bmpObjPtr->pixmap)
184 GDI_HEAP_FREE( hbitmap );
185 hbitmap = 0;
187 else if (bits) /* Set bitmap bits */
188 SetBitmapBits32( hbitmap, height * bmpObjPtr->bitmap.bmWidthBytes,
189 bits );
190 GDI_HEAP_UNLOCK( hbitmap );
191 return hbitmap;
195 /***********************************************************************
196 * CreateCompatibleBitmap16 (GDI.51)
198 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
200 return CreateCompatibleBitmap32( hdc, width, height );
204 /******************************************************************************
205 * CreateCompatibleBitmap32 [GDI32.30] Creates a bitmap compatible with the DC
207 * PARAMS
208 * hdc [I] Handle to device context
209 * width [I] Width of bitmap
210 * height [I] Height of bitmap
212 * RETURNS
213 * Success: Handle to bitmap
214 * Failure: NULL
216 HBITMAP32 WINAPI CreateCompatibleBitmap32( HDC32 hdc, INT32 width, INT32 height)
218 HBITMAP32 hbmpRet = 0;
219 DC *dc;
221 TRACE(gdi, "(%04x,%d,%d) = \n", hdc, width, height );
222 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
223 if ((width >0x1000) || (height > 0x1000))
225 FIXME(gdi,"got bad width %d or height %d, please look for reason\n",
226 width, height );
227 return 0;
229 hbmpRet = CreateBitmap32( width, height, 1, dc->w.bitsPerPixel, NULL );
230 TRACE(gdi,"\t\t%04x\n", hbmpRet);
231 return hbmpRet;
235 /***********************************************************************
236 * CreateBitmapIndirect16 (GDI.49)
238 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
240 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
241 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
245 /******************************************************************************
246 * CreateBitmapIndirect32 [GDI32.26] Creates a bitmap with the specifies info
248 * RETURNS
249 * Success: Handle to bitmap
250 * Failure: NULL
252 HBITMAP32 WINAPI CreateBitmapIndirect32(
253 const BITMAP32 * bmp) /* [in] Pointer to the bitmap data */
255 return CreateBitmap32( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
256 bmp->bmBitsPixel, bmp->bmBits );
260 /***********************************************************************
261 * BITMAP_GetXImage
263 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
265 XImage *BITMAP_GetXImage( const BITMAPOBJ *bmp )
267 return XGetImage( display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
268 bmp->bitmap.bmHeight, AllPlanes, ZPixmap );
272 /***********************************************************************
273 * GetBitmapBits16 (GDI.74)
275 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
277 return GetBitmapBits32( hbitmap, count, buffer );
281 /***********************************************************************
282 * GetBitmapBits32 [GDI32.143] Copies bitmap bits of bitmap to buffer
284 * RETURNS
285 * Success: Number of bytes copied
286 * Failure: 0
288 LONG WINAPI GetBitmapBits32(
289 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
290 LONG count, /* [in] Number of bytes to copy */
291 LPVOID buffer) /* [out] Pointer to buffer to receive bits */
293 BITMAPOBJ * bmp;
294 LONG height, old_height;
295 XImage * image;
296 LPBYTE tbuf;
297 int h,w,pad;
299 /* KLUDGE! */
300 if (count < 0) {
301 WARN(bitmap, "(%ld): Negative number of bytes passed???\n", count );
302 count = -count;
304 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
305 if (!bmp) return 0;
307 /* Only get entire lines */
308 height = count / bmp->bitmap.bmWidthBytes;
309 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
311 TRACE(bitmap, "%dx%d %d colors %p fetched height: %ld\n",
312 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
313 1 << bmp->bitmap.bmBitsPixel, buffer, height );
315 pad = BITMAP_GetBitsPadding( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
317 if (!height || (pad == -1))
319 GDI_HEAP_UNLOCK( hbitmap );
320 return 0;
323 EnterCriticalSection( &X11DRV_CritSection );
325 /* Hack: change the bitmap height temporarily to avoid */
326 /* getting unnecessary bitmap rows. */
327 old_height = bmp->bitmap.bmHeight;
328 bmp->bitmap.bmHeight = height;
329 image = (XImage *)CALL_LARGE_STACK( BITMAP_GetXImage, bmp );
330 bmp->bitmap.bmHeight = old_height;
332 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
334 tbuf = buffer;
335 switch (bmp->bitmap.bmBitsPixel)
337 case 1:
338 for (h=0;h<height;h++)
340 *tbuf = 0;
341 for (w=0;w<bmp->bitmap.bmWidth;w++)
343 if ((w%8) == 0)
344 *tbuf = 0;
345 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
346 if ((w&7) == 7) ++tbuf;
348 tbuf += pad;
350 break;
351 case 4:
352 for (h=0;h<height;h++)
354 for (w=0;w<bmp->bitmap.bmWidth;w++)
356 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
357 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
359 tbuf += pad;
361 break;
362 case 8:
363 for (h=0;h<height;h++)
365 for (w=0;w<bmp->bitmap.bmWidth;w++)
366 *tbuf++ = XGetPixel(image,w,h);
367 tbuf += pad;
369 break;
370 case 15:
371 case 16:
372 for (h=0;h<height;h++)
374 for (w=0;w<bmp->bitmap.bmWidth;w++)
376 long pixel = XGetPixel(image,w,h);
378 *tbuf++ = pixel & 0xff;
379 *tbuf++ = (pixel>>8) & 0xff;
382 break;
383 case 24:
384 for (h=0;h<height;h++)
386 for (w=0;w<bmp->bitmap.bmWidth;w++)
388 long pixel = XGetPixel(image,w,h);
390 *tbuf++ = pixel & 0xff;
391 *tbuf++ = (pixel>> 8) & 0xff;
392 *tbuf++ = (pixel>>16) & 0xff;
394 tbuf += pad;
396 break;
398 case 32:
399 for (h=0;h<height;h++)
401 for (w=0;w<bmp->bitmap.bmWidth;w++)
403 long pixel = XGetPixel(image,w,h);
405 *tbuf++ = pixel & 0xff;
406 *tbuf++ = (pixel>> 8) & 0xff;
407 *tbuf++ = (pixel>>16) & 0xff;
408 *tbuf++ = (pixel>>24) & 0xff;
410 tbuf += pad;
412 break;
413 default:
414 FIXME(bitmap, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
416 XDestroyImage( image );
417 LeaveCriticalSection( &X11DRV_CritSection );
419 GDI_HEAP_UNLOCK( hbitmap );
420 return height * bmp->bitmap.bmWidthBytes;
424 /***********************************************************************
425 * SetBitmapBits16 (GDI.106)
427 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
429 return SetBitmapBits32( hbitmap, count, buffer );
433 /******************************************************************************
434 * SetBitmapBits32 [GDI32.303] Sets bits of color data for a bitmap
436 * RETURNS
437 * Success: Number of bytes used in setting the bitmap bits
438 * Failure: 0
440 LONG WINAPI SetBitmapBits32(
441 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
442 LONG count, /* [in] Number of bytes in bitmap array */
443 LPCVOID buffer) /* [in] Address of array with bitmap bits */
445 struct XPutImage_descr descr;
446 BITMAPOBJ * bmp;
447 LONG height;
448 XImage * image;
449 LPBYTE sbuf,tmpbuffer;
450 int w,h,pad,widthbytes;
452 /* KLUDGE! */
453 if (count < 0) {
454 WARN(bitmap, "(%ld): Negative number of bytes passed???\n", count );
455 count = -count;
457 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
458 if (!bmp) return 0;
460 TRACE(bitmap, "%dx%d %d colors %p\n",
461 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
462 1 << bmp->bitmap.bmBitsPixel, buffer );
464 /* Only set entire lines */
465 height = count / bmp->bitmap.bmWidthBytes;
466 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
468 pad = BITMAP_GetBitsPadding( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
470 if (!height || (pad == -1))
472 GDI_HEAP_UNLOCK( hbitmap );
473 return 0;
476 sbuf = (LPBYTE)buffer;
478 widthbytes = DIB_GetXImageWidthBytes(bmp->bitmap.bmWidth,bmp->bitmap.bmBitsPixel);
479 tmpbuffer = (LPBYTE)xmalloc(widthbytes*height);
481 EnterCriticalSection( &X11DRV_CritSection );
482 image = XCreateImage( display, DefaultVisualOfScreen(screen),
483 bmp->bitmap.bmBitsPixel, ZPixmap, 0, tmpbuffer,
484 bmp->bitmap.bmWidth,height,32,widthbytes );
486 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
487 sbuf = (LPBYTE)buffer;
488 switch (bmp->bitmap.bmBitsPixel)
490 case 1:
491 for (h=0;h<height;h++)
493 for (w=0;w<bmp->bitmap.bmWidth;w++)
495 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
496 if ((w&7) == 7)
497 sbuf++;
499 sbuf += pad;
501 break;
502 case 4:
503 for (h=0;h<height;h++)
505 for (w=0;w<bmp->bitmap.bmWidth;w++)
507 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
508 else XPutPixel( image, w, h, *sbuf++ & 0xf );
510 sbuf += pad;
512 break;
513 case 8:
514 for (h=0;h<height;h++)
516 for (w=0;w<bmp->bitmap.bmWidth;w++)
517 XPutPixel(image,w,h,*sbuf++);
518 sbuf += pad;
520 break;
521 case 15:
522 case 16:
523 for (h=0;h<height;h++)
525 for (w=0;w<bmp->bitmap.bmWidth;w++)
527 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
528 sbuf+=2;
531 break;
532 case 24:
533 for (h=0;h<height;h++)
535 for (w=0;w<bmp->bitmap.bmWidth;w++)
537 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
538 sbuf += 3;
540 sbuf += pad;
542 break;
543 case 32:
544 for (h=0;h<height;h++)
546 for (w=0;w<bmp->bitmap.bmWidth;w++)
548 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
549 sbuf += 4;
551 sbuf += pad;
553 break;
554 default:
555 FIXME(bitmap, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
559 descr.bmp = bmp;
560 descr.image = image;
561 descr.width = bmp->bitmap.bmWidth;
562 descr.height = height;
563 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
564 XDestroyImage( image ); /* frees tmpbuffer too */
565 LeaveCriticalSection( &X11DRV_CritSection );
567 GDI_HEAP_UNLOCK( hbitmap );
568 return height * bmp->bitmap.bmWidthBytes;
571 /***********************************************************************
572 * LoadImage16 [USER.389]
575 HANDLE16 WINAPI LoadImage16( HINSTANCE16 hinst, LPCSTR name, UINT16 type,
576 INT16 desiredx, INT16 desiredy, UINT16 loadflags)
578 if (HIWORD(name)) {
579 TRACE(resource,"(0x%04x,%s,%d,%d,%d,0x%08x)\n",
580 hinst,(char *)PTR_SEG_TO_LIN(name),type,desiredx,desiredy,loadflags);
581 } else {
582 TRACE(resource,"LoadImage16(0x%04x,%p,%d,%d,%d,0x%08x)\n",
583 hinst,name,type,desiredx,desiredy,loadflags);
585 switch (type) {
586 case IMAGE_BITMAP:
587 return LoadBitmap16(hinst,(SEGPTR)name);
588 case IMAGE_ICON:
589 return LoadIcon16(hinst,(SEGPTR)name);
590 case IMAGE_CURSOR:
591 return LoadCursor16(hinst,(SEGPTR)name);
593 return 0;
597 /**********************************************************************
598 * LoadImage32A (USER32.365)
600 * FIXME: implementation still lacks nearly all features, see LR_*
601 * defines in windows.h
604 HANDLE32 WINAPI LoadImage32A( HINSTANCE32 hinst, LPCSTR name, UINT32 type,
605 INT32 desiredx, INT32 desiredy, UINT32 loadflags)
607 if (HIWORD(name)) {
608 TRACE(resource,"(0x%04x,%s,%d,%d,%d,0x%08x)\n",
609 hinst,name,type,desiredx,desiredy,loadflags
611 } else {
612 TRACE(resource,"(0x%04x,%p,%d,%d,%d,0x%08x)\n",
613 hinst,name,type,desiredx,desiredy,loadflags
616 switch (type) {
617 case IMAGE_BITMAP:
618 return LoadBitmap32A(hinst,name);
619 case IMAGE_ICON:
620 return LoadIcon32A(hinst,name);
621 case IMAGE_CURSOR:
622 return LoadCursor32A(hinst,name);
624 return 0;
627 /**********************************************************************
628 * LoadImage32W (USER32.366)
630 * FIXME: implementation still lacks nearly all features, see LR_*
631 * defines in windows.h
635 /******************************************************************************
636 * LoadImage32W [USER32.366] Loads an icon, cursor, or bitmap
638 * PARAMS
639 * hinst [I] Handle of instance that contains image
640 * name [I] Name of image
641 * type [I] Type of image
642 * desiredx [I] Desired width
643 * desiredy [I] Desired height
644 * loadflags [I] Load flags
646 * RETURNS
647 * Success: Handle to newly loaded image
648 * Failure: NULL
650 * BUGS
651 * Implementation still lacks nearly all features, see LR_*
652 * defines in windows.h
654 HANDLE32 WINAPI LoadImage32W( HINSTANCE32 hinst, LPCWSTR name, UINT32 type,
655 INT32 desiredx, INT32 desiredy, UINT32 loadflags )
657 if (HIWORD(name)) {
658 TRACE(resource,"(0x%04x,%p,%d,%d,%d,0x%08x)\n",
659 hinst,name,type,desiredx,desiredy,loadflags
661 } else {
662 TRACE(resource,"(0x%04x,%p,%d,%d,%d,0x%08x)\n",
663 hinst,name,type,desiredx,desiredy,loadflags
667 switch (type) {
668 case IMAGE_BITMAP:
669 return LoadBitmap32W(hinst,name);
670 case IMAGE_ICON:
671 return LoadIcon32W(hinst,name);
672 case IMAGE_CURSOR:
673 return LoadCursor32W(hinst,name);
675 return NULL;
679 /**********************************************************************
680 * CopyBitmap32 (not an API)
682 * NOTES
683 * If it is not an API, why is it declared with WINAPI?
686 HBITMAP32 WINAPI CopyBitmap32 (HBITMAP32 hnd)
688 HBITMAP32 res = 0;
689 BITMAP32 bmp;
691 if (GetObject32A (hnd, sizeof (bmp), &bmp))
693 res = CreateBitmapIndirect32 (&bmp);
694 SetBitmapBits32 (res, bmp.bmWidthBytes * bmp.bmHeight, bmp.bmBits);
696 return res;
700 /******************************************************************************
701 * CopyImage32 [USER32.61] Creates new image and copies attributes to it
703 * PARAMS
704 * hnd [I] Handle to image to copy
705 * type [I] Type of image to copy
706 * desiredx [I] Desired width of new image
707 * desiredy [I] Desired height of new image
708 * flags [I] Copy flags
710 * RETURNS
711 * Success: Handle to newly created image
712 * Failure: NULL
714 * FIXME: implementation still lacks nearly all features, see LR_*
715 * defines in windows.h
717 HICON32 WINAPI CopyImage32( HANDLE32 hnd, UINT32 type, INT32 desiredx,
718 INT32 desiredy, UINT32 flags )
720 switch (type)
722 case IMAGE_BITMAP:
723 return CopyBitmap32(hnd);
724 case IMAGE_ICON:
725 return CopyIcon32(hnd);
726 case IMAGE_CURSOR:
727 return CopyCursor32(hnd);
729 return 0;
732 /**********************************************************************
733 * LoadBitmap16 (USER.175)
735 * NOTES
736 * Can this call LoadBitmap32?
738 HBITMAP16 WINAPI LoadBitmap16( HINSTANCE16 instance, SEGPTR name )
740 HBITMAP32 hbitmap = 0;
741 HDC32 hdc;
742 HRSRC16 hRsrc;
743 HGLOBAL16 handle;
744 BITMAPINFO *info;
746 if (HIWORD(name))
748 char *str = (char *)PTR_SEG_TO_LIN( name );
749 TRACE(bitmap, "(%04x,'%s')\n", instance, str );
750 if (str[0] == '#') name = (SEGPTR)(DWORD)(WORD)atoi( str + 1 );
752 else
753 TRACE(bitmap, "(%04x,%04x)\n",
754 instance, LOWORD(name) );
756 if (!instance) /* OEM bitmap */
758 if (HIWORD((int)name)) return 0;
759 return OBM_LoadBitmap( LOWORD((int)name) );
762 if (!(hRsrc = FindResource16( instance, name, RT_BITMAP16 ))) return 0;
763 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
765 info = (BITMAPINFO *)LockResource16( handle );
766 if ((hdc = GetDC32(0)) != 0)
768 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
769 hbitmap = CreateDIBitmap32( hdc, &info->bmiHeader, CBM_INIT,
770 bits, info, DIB_RGB_COLORS );
771 ReleaseDC32( 0, hdc );
773 FreeResource16( handle );
774 return hbitmap;
778 /******************************************************************************
779 * LoadBitmap32W [USER32.358] Loads bitmap from the executable file
781 * RETURNS
782 * Success: Handle to specified bitmap
783 * Failure: NULL
785 HBITMAP32 WINAPI LoadBitmap32W(
786 HINSTANCE32 instance, /* [in] Handle to application instance */
787 LPCWSTR name) /* [in] Address of bitmap resource name */
789 HBITMAP32 hbitmap = 0;
790 HDC32 hdc;
791 HRSRC32 hRsrc;
792 HGLOBAL32 handle;
793 BITMAPINFO *info;
795 if (!instance) /* OEM bitmap */
797 if (HIWORD((int)name)) return 0;
798 return OBM_LoadBitmap( LOWORD((int)name) );
801 if (!(hRsrc = FindResource32W( instance, name, RT_BITMAP32W ))) return 0;
802 if (!(handle = LoadResource32( instance, hRsrc ))) return 0;
804 info = (BITMAPINFO *)LockResource32( handle );
805 if ((hdc = GetDC32(0)) != 0)
807 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
808 hbitmap = CreateDIBitmap32( hdc, &info->bmiHeader, CBM_INIT,
809 bits, info, DIB_RGB_COLORS );
810 ReleaseDC32( 0, hdc );
812 return hbitmap;
816 /**********************************************************************
817 * LoadBitmap32A (USER32.357)
819 HBITMAP32 WINAPI LoadBitmap32A( HINSTANCE32 instance, LPCSTR name )
821 HBITMAP32 res;
822 if (!HIWORD(name)) res = LoadBitmap32W( instance, (LPWSTR)name );
823 else
825 LPWSTR uni = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
826 res = LoadBitmap32W( instance, uni );
827 HeapFree( GetProcessHeap(), 0, uni );
829 return res;
833 /***********************************************************************
834 * BITMAP_DeleteObject
836 BOOL32 BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
838 #ifdef PRELIMINARY_WING16_SUPPORT
839 if( bmp->bitmap.bmBits )
840 TSXShmDetach( display, (XShmSegmentInfo*)bmp->bitmap.bmBits );
841 #endif
843 TSXFreePixmap( display, bmp->pixmap );
844 #ifdef PRELIMINARY_WING16_SUPPORT
845 if( bmp->bitmap.bmBits )
847 __ShmBitmapCtl* p = (__ShmBitmapCtl*)bmp->bitmap.bmBits;
848 WORD sel = HIWORD(p->bits);
849 unsigned long l, limit = GetSelectorLimit(sel);
851 for( l = 0; l < limit; l += 0x10000, sel += __AHINCR )
852 FreeSelector(sel);
853 shmctl(p->si.shmid, IPC_RMID, NULL);
854 shmdt(p->si.shmaddr); /* already marked for destruction */
856 #endif
858 DIB_DeleteDIBSection( bmp );
860 return GDI_FreeObject( hbitmap );
864 /***********************************************************************
865 * BITMAP_GetObject16
867 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
869 if (bmp->dib)
871 if ( count <= sizeof(BITMAP16) )
873 BITMAP32 *bmp32 = &bmp->dib->dibSection.dsBm;
874 BITMAP16 bmp16;
875 bmp16.bmType = bmp32->bmType;
876 bmp16.bmWidth = bmp32->bmWidth;
877 bmp16.bmHeight = bmp32->bmHeight;
878 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
879 bmp16.bmPlanes = bmp32->bmPlanes;
880 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
881 bmp16.bmBits = NULL;
882 memcpy( buffer, &bmp16, count );
883 return count;
885 else
887 FIXME(bitmap, "not implemented for DIBs: count %d\n", count);
888 return 0;
891 else
893 if (count > sizeof(bmp->bitmap)) count = sizeof(bmp->bitmap);
894 memcpy( buffer, &bmp->bitmap, count );
895 return count;
900 /***********************************************************************
901 * BITMAP_GetObject32
903 INT32 BITMAP_GetObject32( BITMAPOBJ * bmp, INT32 count, LPVOID buffer )
905 if (bmp->dib)
907 if (count < sizeof(DIBSECTION))
909 if (count > sizeof(BITMAP32)) count = sizeof(BITMAP32);
911 else
913 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
916 memcpy( buffer, &bmp->dib->dibSection, count );
917 return count;
919 else
921 BITMAP32 bmp32;
922 bmp32.bmType = bmp->bitmap.bmType;
923 bmp32.bmWidth = bmp->bitmap.bmWidth;
924 bmp32.bmHeight = bmp->bitmap.bmHeight;
925 bmp32.bmWidthBytes = bmp->bitmap.bmWidthBytes;
926 bmp32.bmPlanes = bmp->bitmap.bmPlanes;
927 bmp32.bmBitsPixel = bmp->bitmap.bmBitsPixel;
928 bmp32.bmBits = NULL;
929 if (count > sizeof(bmp32)) count = sizeof(bmp32);
930 memcpy( buffer, &bmp32, count );
931 return count;
936 /***********************************************************************
937 * CreateDiscardableBitmap16 (GDI.156)
939 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
940 INT16 height )
942 return CreateCompatibleBitmap16( hdc, width, height );
946 /******************************************************************************
947 * CreateDiscardableBitmap32 [GDI32.38] Creates a discardable bitmap
949 * RETURNS
950 * Success: Handle to bitmap
951 * Failure: NULL
953 HBITMAP32 WINAPI CreateDiscardableBitmap32(
954 HDC32 hdc, /* [in] Handle to device context */
955 INT32 width, /* [in] Bitmap width */
956 INT32 height) /* [in] Bitmap height */
958 return CreateCompatibleBitmap32( hdc, width, height );
962 /***********************************************************************
963 * GetBitmapDimensionEx16 (GDI.468)
965 * NOTES
966 * Can this call GetBitmapDimensionEx32?
968 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
970 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
971 if (!bmp) return FALSE;
972 *size = bmp->size;
973 GDI_HEAP_UNLOCK( hbitmap );
974 return TRUE;
978 /******************************************************************************
979 * GetBitmapDimensionEx32 [GDI32.144] Retrieves dimensions of a bitmap
981 * RETURNS
982 * Success: TRUE
983 * Failure: FALSE
985 BOOL32 WINAPI GetBitmapDimensionEx32(
986 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
987 LPSIZE32 size) /* [out] Address of struct receiving dimensions */
989 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
990 if (!bmp) return FALSE;
991 size->cx = (INT32)bmp->size.cx;
992 size->cy = (INT32)bmp->size.cy;
993 GDI_HEAP_UNLOCK( hbitmap );
994 return TRUE;
998 /***********************************************************************
999 * GetBitmapDimension (GDI.162)
1001 DWORD WINAPI GetBitmapDimension( HBITMAP16 hbitmap )
1003 SIZE16 size;
1004 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
1005 return MAKELONG( size.cx, size.cy );
1009 /***********************************************************************
1010 * SetBitmapDimensionEx16 (GDI.478)
1012 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
1013 LPSIZE16 prevSize )
1015 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
1016 if (!bmp) return FALSE;
1017 if (prevSize) *prevSize = bmp->size;
1018 bmp->size.cx = x;
1019 bmp->size.cy = y;
1020 GDI_HEAP_UNLOCK( hbitmap );
1021 return TRUE;
1025 /******************************************************************************
1026 * SetBitmapDimensionEx32 [GDI32.304] Assignes dimensions to a bitmap
1028 * RETURNS
1029 * Success: TRUE
1030 * Failure: FALSE
1032 BOOL32 WINAPI SetBitmapDimensionEx32(
1033 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
1034 INT32 x, /* [in] Bitmap width */
1035 INT32 y, /* [in] Bitmap height */
1036 LPSIZE32 prevSize) /* [out] Address of structure for orig dims */
1038 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
1039 if (!bmp) return FALSE;
1040 if (prevSize) CONV_SIZE16TO32( &bmp->size, prevSize );
1041 bmp->size.cx = (INT16)x;
1042 bmp->size.cy = (INT16)y;
1043 GDI_HEAP_UNLOCK( hbitmap );
1044 return TRUE;
1048 /***********************************************************************
1049 * SetBitmapDimension (GDI.163)
1051 DWORD WINAPI SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
1053 SIZE16 size;
1054 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
1055 return MAKELONG( size.cx, size.cy );