Release 981025.
[wine/gsoc-2012-control.git] / objects / bitmap.c
blobf3a77e49e6a79a17c3a1d5c3bebee99b66790d9b
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 "global.h"
17 #include "debug.h"
18 #include "sysmetrics.h"
19 #include "cursoricon.h"
20 #include "color.h"
22 #ifdef PRELIMINARY_WING16_SUPPORT
23 #include <sys/types.h>
24 #include <sys/ipc.h>
25 #include <sys/shm.h>
26 #endif
28 /* GCs used for B&W and color bitmap operations */
29 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
31 /***********************************************************************
32 * XPutImage_wrapper
34 * Wrapper to call XPutImage with CALL_LARGE_STACK.
37 struct XPutImage_descr
39 BITMAPOBJ *bmp;
40 XImage *image;
41 INT32 width;
42 INT32 height;
45 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
47 return XPutImage( display, descr->bmp->pixmap, BITMAP_GC(descr->bmp),
48 descr->image, 0, 0, 0, 0, descr->width, descr->height );
51 /***********************************************************************
52 * BITMAP_GetBitsPadding
54 * Return number of bytes to pad a scanline of 16-bit aligned Windows DDB data.
56 INT32 BITMAP_GetBitsPadding( int bmWidth, int bpp )
58 INT32 pad;
60 switch (bpp)
62 case 1:
63 if (!(bmWidth & 15)) pad = 0;
64 else pad = ((16 - (bmWidth & 15)) + 7) / 8;
65 break;
67 case 8:
68 pad = (2 - (bmWidth & 1)) & 1;
69 break;
71 case 24:
72 pad = (bmWidth*3) & 1;
73 break;
75 case 32:
76 case 16:
77 case 15:
78 pad = 0; /* we have 16bit alignment already */
79 break;
81 case 4:
82 if (!(bmWidth & 3)) pad = 0;
83 else pad = ((4 - (bmWidth & 3)) + 1) / 2;
84 break;
86 default:
87 WARN(bitmap,"Unknown depth %d, please report.\n", bpp );
88 return -1;
90 return pad;
93 /***********************************************************************
94 * BITMAP_GetBitsWidth
96 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB data.
98 INT32 BITMAP_GetBitsWidth( int bmWidth, int bpp )
100 switch(bpp)
102 case 1:
103 return 2 * ((bmWidth+15) >> 4);
105 case 24:
106 bmWidth *= 3; /* fall through */
107 case 8:
108 return bmWidth + (bmWidth & 1);
110 case 32:
111 return bmWidth * 4;
113 case 16:
114 case 15:
115 return bmWidth * 2;
117 case 4:
118 return 2 * ((bmWidth+3) >> 2);
120 default:
121 WARN(bitmap,"Unknown depth %d, please report.\n", bpp );
123 return -1;
126 /***********************************************************************
127 * CreateUserBitmap16 (GDI.407)
129 HBITMAP16 WINAPI CreateUserBitmap16( INT16 width, INT16 height, UINT16 planes,
130 UINT16 bpp, LPCVOID bits )
132 return CreateBitmap16( width, height, planes, bpp, bits );
135 /***********************************************************************
136 * CreateUserDiscardableBitmap16 (GDI.409)
138 HBITMAP16 WINAPI CreateUserDiscardableBitmap16( WORD dummy,
139 INT16 width, INT16 height )
141 return CreateUserBitmap16( width, height, 1, screenDepth, NULL );
146 /***********************************************************************
147 * CreateBitmap16 (GDI.48)
149 HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
150 UINT16 bpp, LPCVOID bits )
152 return CreateBitmap32( width, height, planes, bpp, bits );
156 /******************************************************************************
157 * CreateBitmap32 [GDI32.25] Creates a bitmap with the specified info
159 * PARAMS
160 * width [I] bitmap width
161 * height [I] bitmap height
162 * planes [I] Number of color planes
163 * bpp [I] Number of bits to identify a color
164 * bits [I] Pointer to array containing color data
166 * RETURNS
167 * Success: Handle to bitmap
168 * Failure: NULL
170 HBITMAP32 WINAPI CreateBitmap32( INT32 width, INT32 height, UINT32 planes,
171 UINT32 bpp, LPCVOID bits )
173 BITMAPOBJ * bmpObjPtr;
174 HBITMAP32 hbitmap;
176 planes = (BYTE)planes;
177 bpp = (BYTE)bpp;
179 TRACE(gdi, "%dx%d, %d colors\n", width, height, 1 << (planes*bpp) );
181 /* Check parameters */
182 if (!height || !width || planes != 1) return 0;
183 if ((bpp != 1) && (bpp != screenDepth)) return 0;
184 if (height < 0) height = -height;
185 if (width < 0) width = -width;
187 /* Create the BITMAPOBJ */
188 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
189 if (!hbitmap) return 0;
190 bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LOCK( hbitmap );
192 bmpObjPtr->size.cx = 0;
193 bmpObjPtr->size.cy = 0;
194 bmpObjPtr->bitmap.bmType = 0;
195 bmpObjPtr->bitmap.bmWidth = (INT16)width;
196 bmpObjPtr->bitmap.bmHeight = (INT16)height;
197 bmpObjPtr->bitmap.bmPlanes = (BYTE)planes;
198 bmpObjPtr->bitmap.bmBitsPixel = (BYTE)bpp;
199 bmpObjPtr->bitmap.bmWidthBytes = (INT16)BITMAP_WIDTH_BYTES( width, bpp );
200 bmpObjPtr->bitmap.bmBits = 0;
202 bmpObjPtr->dib = NULL;
204 /* Create the pixmap */
205 bmpObjPtr->pixmap = TSXCreatePixmap(display, rootWindow, width, height, bpp);
206 if (!bmpObjPtr->pixmap)
208 GDI_HEAP_FREE( hbitmap );
209 hbitmap = 0;
211 else if (bits) /* Set bitmap bits */
212 SetBitmapBits32( hbitmap, height * bmpObjPtr->bitmap.bmWidthBytes,
213 bits );
214 GDI_HEAP_UNLOCK( hbitmap );
215 return hbitmap;
219 /***********************************************************************
220 * CreateCompatibleBitmap16 (GDI.51)
222 HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
224 return CreateCompatibleBitmap32( hdc, width, height );
228 /******************************************************************************
229 * CreateCompatibleBitmap32 [GDI32.30] Creates a bitmap compatible with the DC
231 * PARAMS
232 * hdc [I] Handle to device context
233 * width [I] Width of bitmap
234 * height [I] Height of bitmap
236 * RETURNS
237 * Success: Handle to bitmap
238 * Failure: NULL
240 HBITMAP32 WINAPI CreateCompatibleBitmap32( HDC32 hdc, INT32 width, INT32 height)
242 HBITMAP32 hbmpRet = 0;
243 DC *dc;
245 TRACE(gdi, "(%04x,%d,%d) = \n", hdc, width, height );
246 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
247 if ((width >0x1000) || (height > 0x1000))
249 FIXME(gdi,"got bad width %d or height %d, please look for reason\n",
250 width, height );
251 return 0;
253 hbmpRet = CreateBitmap32( width, height, 1, dc->w.bitsPerPixel, NULL );
254 TRACE(gdi,"\t\t%04x\n", hbmpRet);
255 return hbmpRet;
259 /***********************************************************************
260 * CreateBitmapIndirect16 (GDI.49)
262 HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
264 return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
265 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
269 /******************************************************************************
270 * CreateBitmapIndirect32 [GDI32.26] Creates a bitmap with the specifies info
272 * RETURNS
273 * Success: Handle to bitmap
274 * Failure: NULL
276 HBITMAP32 WINAPI CreateBitmapIndirect32(
277 const BITMAP32 * bmp) /* [in] Pointer to the bitmap data */
279 return CreateBitmap32( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
280 bmp->bmBitsPixel, bmp->bmBits );
284 /***********************************************************************
285 * BITMAP_GetXImage
287 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
289 XImage *BITMAP_GetXImage( const BITMAPOBJ *bmp )
291 return XGetImage( display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
292 bmp->bitmap.bmHeight, AllPlanes, ZPixmap );
296 /***********************************************************************
297 * GetBitmapBits16 (GDI.74)
299 LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
301 return GetBitmapBits32( hbitmap, count, buffer );
305 /***********************************************************************
306 * GetBitmapBits32 [GDI32.143] Copies bitmap bits of bitmap to buffer
308 * RETURNS
309 * Success: Number of bytes copied
310 * Failure: 0
312 LONG WINAPI GetBitmapBits32(
313 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
314 LONG count, /* [in] Number of bytes to copy */
315 LPVOID buffer) /* [out] Pointer to buffer to receive bits */
317 BITMAPOBJ * bmp;
318 LONG height, old_height;
319 XImage * image;
320 LPBYTE tbuf;
321 int h,w,pad;
323 /* KLUDGE! */
324 if (count < 0) {
325 WARN(bitmap, "(%ld): Negative number of bytes passed???\n", count );
326 count = -count;
328 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
329 if (!bmp) return 0;
331 /* Only get entire lines */
332 height = count / bmp->bitmap.bmWidthBytes;
333 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
335 TRACE(bitmap, "%dx%d %d colors %p fetched height: %ld\n",
336 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
337 1 << bmp->bitmap.bmBitsPixel, buffer, height );
339 pad = BITMAP_GetBitsPadding( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
341 if (!height || (pad == -1))
343 GDI_HEAP_UNLOCK( hbitmap );
344 return 0;
347 EnterCriticalSection( &X11DRV_CritSection );
349 /* Hack: change the bitmap height temporarily to avoid */
350 /* getting unnecessary bitmap rows. */
351 old_height = bmp->bitmap.bmHeight;
352 bmp->bitmap.bmHeight = height;
353 image = (XImage *)CALL_LARGE_STACK( BITMAP_GetXImage, bmp );
354 bmp->bitmap.bmHeight = old_height;
356 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
358 tbuf = buffer;
359 switch (bmp->bitmap.bmBitsPixel)
361 case 1:
362 for (h=0;h<height;h++)
364 *tbuf = 0;
365 for (w=0;w<bmp->bitmap.bmWidth;w++)
367 if ((w%8) == 0)
368 *tbuf = 0;
369 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
370 if ((w&7) == 7) ++tbuf;
372 tbuf += pad;
374 break;
375 case 4:
376 for (h=0;h<height;h++)
378 for (w=0;w<bmp->bitmap.bmWidth;w++)
380 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
381 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
383 tbuf += pad;
385 break;
386 case 8:
387 for (h=0;h<height;h++)
389 for (w=0;w<bmp->bitmap.bmWidth;w++)
390 *tbuf++ = XGetPixel(image,w,h);
391 tbuf += pad;
393 break;
394 case 15:
395 case 16:
396 for (h=0;h<height;h++)
398 for (w=0;w<bmp->bitmap.bmWidth;w++)
400 long pixel = XGetPixel(image,w,h);
402 *tbuf++ = pixel & 0xff;
403 *tbuf++ = (pixel>>8) & 0xff;
406 break;
407 case 24:
408 for (h=0;h<height;h++)
410 for (w=0;w<bmp->bitmap.bmWidth;w++)
412 long pixel = XGetPixel(image,w,h);
414 *tbuf++ = pixel & 0xff;
415 *tbuf++ = (pixel>> 8) & 0xff;
416 *tbuf++ = (pixel>>16) & 0xff;
418 tbuf += pad;
420 break;
422 case 32:
423 for (h=0;h<height;h++)
425 for (w=0;w<bmp->bitmap.bmWidth;w++)
427 long pixel = XGetPixel(image,w,h);
429 *tbuf++ = pixel & 0xff;
430 *tbuf++ = (pixel>> 8) & 0xff;
431 *tbuf++ = (pixel>>16) & 0xff;
432 *tbuf++ = (pixel>>24) & 0xff;
434 tbuf += pad;
436 break;
437 default:
438 FIXME(bitmap, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
440 XDestroyImage( image );
441 LeaveCriticalSection( &X11DRV_CritSection );
443 GDI_HEAP_UNLOCK( hbitmap );
444 return height * bmp->bitmap.bmWidthBytes;
448 /***********************************************************************
449 * SetBitmapBits16 (GDI.106)
451 LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
453 return SetBitmapBits32( hbitmap, count, buffer );
457 /******************************************************************************
458 * SetBitmapBits32 [GDI32.303] Sets bits of color data for a bitmap
460 * RETURNS
461 * Success: Number of bytes used in setting the bitmap bits
462 * Failure: 0
464 LONG WINAPI SetBitmapBits32(
465 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
466 LONG count, /* [in] Number of bytes in bitmap array */
467 LPCVOID buffer) /* [in] Address of array with bitmap bits */
469 struct XPutImage_descr descr;
470 BITMAPOBJ * bmp;
471 LONG height;
472 XImage * image;
473 LPBYTE sbuf,tmpbuffer;
474 int w,h,pad,widthbytes;
476 /* KLUDGE! */
477 if (count < 0) {
478 WARN(bitmap, "(%ld): Negative number of bytes passed???\n", count );
479 count = -count;
481 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
482 if (!bmp) return 0;
484 TRACE(bitmap, "%dx%d %d colors %p\n",
485 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
486 1 << bmp->bitmap.bmBitsPixel, buffer );
488 /* Only set entire lines */
489 height = count / bmp->bitmap.bmWidthBytes;
490 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
492 pad = BITMAP_GetBitsPadding( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
494 if (!height || (pad == -1))
496 GDI_HEAP_UNLOCK( hbitmap );
497 return 0;
500 sbuf = (LPBYTE)buffer;
502 widthbytes = DIB_GetXImageWidthBytes(bmp->bitmap.bmWidth,bmp->bitmap.bmBitsPixel);
503 tmpbuffer = (LPBYTE)xmalloc(widthbytes*height);
505 EnterCriticalSection( &X11DRV_CritSection );
506 image = XCreateImage( display, DefaultVisualOfScreen(screen),
507 bmp->bitmap.bmBitsPixel, ZPixmap, 0, tmpbuffer,
508 bmp->bitmap.bmWidth,height,32,widthbytes );
510 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
511 sbuf = (LPBYTE)buffer;
512 switch (bmp->bitmap.bmBitsPixel)
514 case 1:
515 for (h=0;h<height;h++)
517 for (w=0;w<bmp->bitmap.bmWidth;w++)
519 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
520 if ((w&7) == 7)
521 sbuf++;
523 sbuf += pad;
525 break;
526 case 4:
527 for (h=0;h<height;h++)
529 for (w=0;w<bmp->bitmap.bmWidth;w++)
531 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
532 else XPutPixel( image, w, h, *sbuf++ & 0xf );
534 sbuf += pad;
536 break;
537 case 8:
538 for (h=0;h<height;h++)
540 for (w=0;w<bmp->bitmap.bmWidth;w++)
541 XPutPixel(image,w,h,*sbuf++);
542 sbuf += pad;
544 break;
545 case 15:
546 case 16:
547 for (h=0;h<height;h++)
549 for (w=0;w<bmp->bitmap.bmWidth;w++)
551 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
552 sbuf+=2;
555 break;
556 case 24:
557 for (h=0;h<height;h++)
559 for (w=0;w<bmp->bitmap.bmWidth;w++)
561 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
562 sbuf += 3;
564 sbuf += pad;
566 break;
567 case 32:
568 for (h=0;h<height;h++)
570 for (w=0;w<bmp->bitmap.bmWidth;w++)
572 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
573 sbuf += 4;
575 sbuf += pad;
577 break;
578 default:
579 FIXME(bitmap, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
583 descr.bmp = bmp;
584 descr.image = image;
585 descr.width = bmp->bitmap.bmWidth;
586 descr.height = height;
587 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
588 XDestroyImage( image ); /* frees tmpbuffer too */
589 LeaveCriticalSection( &X11DRV_CritSection );
591 GDI_HEAP_UNLOCK( hbitmap );
592 return height * bmp->bitmap.bmWidthBytes;
595 /***********************************************************************
596 * LoadImage16 [USER.389]
599 HANDLE16 WINAPI LoadImage16( HINSTANCE16 hinst, LPCSTR name, UINT16 type,
600 INT16 desiredx, INT16 desiredy, UINT16 loadflags)
602 if (HIWORD(name)) {
603 TRACE(resource,"(0x%04x,%s,%d,%d,%d,0x%08x)\n",
604 hinst,(char *)PTR_SEG_TO_LIN(name),type,desiredx,desiredy,loadflags);
605 } else {
606 TRACE(resource,"LoadImage16(0x%04x,%p,%d,%d,%d,0x%08x)\n",
607 hinst,name,type,desiredx,desiredy,loadflags);
609 switch (type) {
610 case IMAGE_BITMAP:
611 return LoadBitmap16(hinst,(SEGPTR)name);
612 case IMAGE_ICON:
613 return LoadIcon16(hinst,(SEGPTR)name);
614 case IMAGE_CURSOR:
615 return LoadCursor16(hinst,(SEGPTR)name);
617 return 0;
621 /**********************************************************************
622 * LoadImage32A (USER32.365)
624 * FIXME: implementation lacks some features, see LR_ defines in windows.h
627 HANDLE32 WINAPI LoadImage32A( HINSTANCE32 hinst, LPCSTR name, UINT32 type,
628 INT32 desiredx, INT32 desiredy, UINT32 loadflags)
630 HANDLE32 res;
631 LPWSTR u_name;
633 if (HIWORD(name)) u_name = HEAP_strdupAtoW(GetProcessHeap(), 0, name);
634 else u_name=(LPWSTR)name;
635 res = LoadImage32W(hinst, u_name, type, desiredx, desiredy, loadflags);
636 if (HIWORD(name)) HeapFree(GetProcessHeap(), 0, u_name);
637 return res;
641 /******************************************************************************
642 * LoadImage32W [USER32.366] Loads an icon, cursor, or bitmap
644 * PARAMS
645 * hinst [I] Handle of instance that contains image
646 * name [I] Name of image
647 * type [I] Type of image
648 * desiredx [I] Desired width
649 * desiredy [I] Desired height
650 * loadflags [I] Load flags
652 * RETURNS
653 * Success: Handle to newly loaded image
654 * Failure: NULL
656 * FIXME: Implementation lacks some features, see LR_ defines in windows.h
658 HANDLE32 WINAPI LoadImage32W( HINSTANCE32 hinst, LPCWSTR name, UINT32 type,
659 INT32 desiredx, INT32 desiredy, UINT32 loadflags )
661 if (HIWORD(name)) {
662 TRACE(resource,"(0x%04x,%p,%d,%d,%d,0x%08x)\n",
663 hinst,name,type,desiredx,desiredy,loadflags
665 } else {
666 TRACE(resource,"(0x%04x,%p,%d,%d,%d,0x%08x)\n",
667 hinst,name,type,desiredx,desiredy,loadflags
670 if (loadflags & LR_DEFAULTSIZE)
671 if (type == IMAGE_ICON) {
672 if (!desiredx) desiredx = SYSMETRICS_CXICON;
673 if (!desiredy) desiredy = SYSMETRICS_CYICON;
674 } else if (type == IMAGE_CURSOR) {
675 if (!desiredx) desiredx = SYSMETRICS_CXCURSOR;
676 if (!desiredy) desiredy = SYSMETRICS_CYCURSOR;
678 if (loadflags & LR_LOADFROMFILE) loadflags &= ~LR_SHARED;
679 switch (type) {
680 case IMAGE_BITMAP:
681 return BITMAP_LoadBitmap32W(hinst, name, loadflags);
682 case IMAGE_ICON:
683 return CURSORICON_Load32(hinst, name, desiredx, desiredy,
684 MIN(16, COLOR_GetSystemPaletteSize()), FALSE, loadflags);
685 case IMAGE_CURSOR:
686 return CURSORICON_Load32(hinst, name, desiredx, desiredy, 1, TRUE,
687 loadflags);
689 return 0;
693 /**********************************************************************
694 * CopyBitmap32 (not an API)
696 * NOTES
697 * If it is not an API, why is it declared with WINAPI?
700 HBITMAP32 WINAPI CopyBitmap32 (HBITMAP32 hnd)
702 HBITMAP32 res = 0;
703 BITMAP32 bmp;
705 if (GetObject32A (hnd, sizeof (bmp), &bmp))
707 res = CreateBitmapIndirect32 (&bmp);
708 SetBitmapBits32 (res, bmp.bmWidthBytes * bmp.bmHeight, bmp.bmBits);
710 return res;
714 /******************************************************************************
715 * CopyImage32 [USER32.61] Creates new image and copies attributes to it
717 * PARAMS
718 * hnd [I] Handle to image to copy
719 * type [I] Type of image to copy
720 * desiredx [I] Desired width of new image
721 * desiredy [I] Desired height of new image
722 * flags [I] Copy flags
724 * RETURNS
725 * Success: Handle to newly created image
726 * Failure: NULL
728 * FIXME: implementation still lacks nearly all features, see LR_*
729 * defines in windows.h
731 HICON32 WINAPI CopyImage32( HANDLE32 hnd, UINT32 type, INT32 desiredx,
732 INT32 desiredy, UINT32 flags )
734 switch (type)
736 case IMAGE_BITMAP:
737 return CopyBitmap32(hnd);
738 case IMAGE_ICON:
739 return CopyIcon32(hnd);
740 case IMAGE_CURSOR:
741 return CopyCursor32(hnd);
743 return 0;
746 /**********************************************************************
747 * LoadBitmap16 (USER.175)
749 * NOTES
750 * Can this call LoadBitmap32?
752 HBITMAP16 WINAPI LoadBitmap16( HINSTANCE16 instance, SEGPTR name )
754 HBITMAP32 hbitmap = 0;
755 HDC32 hdc;
756 HRSRC16 hRsrc;
757 HGLOBAL16 handle;
758 BITMAPINFO *info;
760 if (HIWORD(name))
762 char *str = (char *)PTR_SEG_TO_LIN( name );
763 TRACE(bitmap, "(%04x,'%s')\n", instance, str );
764 if (str[0] == '#') name = (SEGPTR)(DWORD)(WORD)atoi( str + 1 );
766 else
767 TRACE(bitmap, "(%04x,%04x)\n",
768 instance, LOWORD(name) );
770 if (!instance) /* OEM bitmap */
772 if (HIWORD((int)name)) return 0;
773 return OBM_LoadBitmap( LOWORD((int)name) );
776 if (!(hRsrc = FindResource16( instance, name, RT_BITMAP16 ))) return 0;
777 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
779 info = (BITMAPINFO *)LockResource16( handle );
780 if ((hdc = GetDC32(0)) != 0)
782 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
783 hbitmap = CreateDIBitmap32( hdc, &info->bmiHeader, CBM_INIT,
784 bits, info, DIB_RGB_COLORS );
785 ReleaseDC32( 0, hdc );
787 FreeResource16( handle );
788 return hbitmap;
792 HBITMAP32 BITMAP_LoadBitmap32W(HINSTANCE32 instance,LPCWSTR name,
793 UINT32 loadflags)
795 HBITMAP32 hbitmap = 0;
796 HDC32 hdc;
797 HRSRC32 hRsrc;
798 HGLOBAL32 handle;
799 char *ptr = NULL;
800 BITMAPINFO *info, *fix_info=NULL;
801 HGLOBAL32 hFix;
802 int size;
804 if (!(loadflags & LR_LOADFROMFILE)) {
805 if (!instance) /* OEM bitmap */
807 if (HIWORD((int)name)) return 0;
808 return OBM_LoadBitmap( LOWORD((int)name) );
811 if (!(hRsrc = FindResource32W( instance, name, RT_BITMAP32W ))) return 0;
812 if (!(handle = LoadResource32( instance, hRsrc ))) return 0;
814 if ((info = (BITMAPINFO *)LockResource32( handle )) == NULL) return 0;
816 else
818 if (!(ptr = (char *)VIRTUAL_MapFileW( name ))) return 0;
819 info = (BITMAPINFO *)(ptr + sizeof(BITMAPFILEHEADER));
821 size = DIB_BitmapInfoSize(info, DIB_RGB_COLORS);
822 if ((hFix = GlobalAlloc32(0, size))) fix_info=GlobalLock32(hFix);
823 if (fix_info) {
824 BYTE pix;
826 memcpy(fix_info, info, size);
827 pix = *((LPBYTE)info+DIB_BitmapInfoSize(info, DIB_RGB_COLORS));
828 DIB_FixColorsToLoadflags(fix_info, loadflags, pix);
829 if ((hdc = GetDC32(0)) != 0) {
830 if (loadflags & LR_CREATEDIBSECTION)
831 hbitmap = CreateDIBSection32(hdc, fix_info, DIB_RGB_COLORS, NULL, 0, 0);
832 else {
833 char *bits = (char *)info + size;;
834 hbitmap = CreateDIBitmap32( hdc, &fix_info->bmiHeader, CBM_INIT,
835 bits, fix_info, DIB_RGB_COLORS );
837 ReleaseDC32( 0, hdc );
839 GlobalUnlock32(hFix);
840 GlobalFree32(hFix);
842 if (loadflags & LR_LOADFROMFILE) UnmapViewOfFile( ptr );
843 return hbitmap;
845 /******************************************************************************
846 * LoadBitmap32W [USER32.358] Loads bitmap from the executable file
848 * RETURNS
849 * Success: Handle to specified bitmap
850 * Failure: NULL
852 HBITMAP32 WINAPI LoadBitmap32W(
853 HINSTANCE32 instance, /* [in] Handle to application instance */
854 LPCWSTR name) /* [in] Address of bitmap resource name */
856 return BITMAP_LoadBitmap32W(instance, name, 0);
860 /**********************************************************************
861 * LoadBitmap32A (USER32.357)
863 HBITMAP32 WINAPI LoadBitmap32A( HINSTANCE32 instance, LPCSTR name )
865 HBITMAP32 res;
866 if (!HIWORD(name)) res = LoadBitmap32W( instance, (LPWSTR)name );
867 else
869 LPWSTR uni = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
870 res = LoadBitmap32W( instance, uni );
871 HeapFree( GetProcessHeap(), 0, uni );
873 return res;
877 /***********************************************************************
878 * BITMAP_DeleteObject
880 BOOL32 BITMAP_DeleteObject( HBITMAP16 hbitmap, BITMAPOBJ * bmp )
882 #ifdef PRELIMINARY_WING16_SUPPORT
883 if( bmp->bitmap.bmBits )
884 TSXShmDetach( display, (XShmSegmentInfo*)bmp->bitmap.bmBits );
885 #endif
887 TSXFreePixmap( display, bmp->pixmap );
888 #ifdef PRELIMINARY_WING16_SUPPORT
889 if( bmp->bitmap.bmBits )
891 __ShmBitmapCtl* p = (__ShmBitmapCtl*)bmp->bitmap.bmBits;
892 WORD sel = HIWORD(p->bits);
893 unsigned long l, limit = GetSelectorLimit(sel);
895 for( l = 0; l < limit; l += 0x10000, sel += __AHINCR )
896 FreeSelector(sel);
897 shmctl(p->si.shmid, IPC_RMID, NULL);
898 shmdt(p->si.shmaddr); /* already marked for destruction */
900 #endif
902 DIB_DeleteDIBSection( bmp );
904 return GDI_FreeObject( hbitmap );
908 /***********************************************************************
909 * BITMAP_GetObject16
911 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
913 if (bmp->dib)
915 if ( count <= sizeof(BITMAP16) )
917 BITMAP32 *bmp32 = &bmp->dib->dibSection.dsBm;
918 BITMAP16 bmp16;
919 bmp16.bmType = bmp32->bmType;
920 bmp16.bmWidth = bmp32->bmWidth;
921 bmp16.bmHeight = bmp32->bmHeight;
922 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
923 bmp16.bmPlanes = bmp32->bmPlanes;
924 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
925 bmp16.bmBits = NULL;
926 memcpy( buffer, &bmp16, count );
927 return count;
929 else
931 FIXME(bitmap, "not implemented for DIBs: count %d\n", count);
932 return 0;
935 else
937 if (count > sizeof(bmp->bitmap)) count = sizeof(bmp->bitmap);
938 memcpy( buffer, &bmp->bitmap, count );
939 return count;
944 /***********************************************************************
945 * BITMAP_GetObject32
947 INT32 BITMAP_GetObject32( BITMAPOBJ * bmp, INT32 count, LPVOID buffer )
949 if (bmp->dib)
951 if (count < sizeof(DIBSECTION))
953 if (count > sizeof(BITMAP32)) count = sizeof(BITMAP32);
955 else
957 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
960 memcpy( buffer, &bmp->dib->dibSection, count );
961 return count;
963 else
965 BITMAP32 bmp32;
966 bmp32.bmType = bmp->bitmap.bmType;
967 bmp32.bmWidth = bmp->bitmap.bmWidth;
968 bmp32.bmHeight = bmp->bitmap.bmHeight;
969 bmp32.bmWidthBytes = bmp->bitmap.bmWidthBytes;
970 bmp32.bmPlanes = bmp->bitmap.bmPlanes;
971 bmp32.bmBitsPixel = bmp->bitmap.bmBitsPixel;
972 bmp32.bmBits = NULL;
973 if (count > sizeof(bmp32)) count = sizeof(bmp32);
974 memcpy( buffer, &bmp32, count );
975 return count;
980 /***********************************************************************
981 * CreateDiscardableBitmap16 (GDI.156)
983 HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
984 INT16 height )
986 return CreateCompatibleBitmap16( hdc, width, height );
990 /******************************************************************************
991 * CreateDiscardableBitmap32 [GDI32.38] Creates a discardable bitmap
993 * RETURNS
994 * Success: Handle to bitmap
995 * Failure: NULL
997 HBITMAP32 WINAPI CreateDiscardableBitmap32(
998 HDC32 hdc, /* [in] Handle to device context */
999 INT32 width, /* [in] Bitmap width */
1000 INT32 height) /* [in] Bitmap height */
1002 return CreateCompatibleBitmap32( hdc, width, height );
1006 /***********************************************************************
1007 * GetBitmapDimensionEx16 (GDI.468)
1009 * NOTES
1010 * Can this call GetBitmapDimensionEx32?
1012 BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
1014 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
1015 if (!bmp) return FALSE;
1016 *size = bmp->size;
1017 GDI_HEAP_UNLOCK( hbitmap );
1018 return TRUE;
1022 /******************************************************************************
1023 * GetBitmapDimensionEx32 [GDI32.144] Retrieves dimensions of a bitmap
1025 * RETURNS
1026 * Success: TRUE
1027 * Failure: FALSE
1029 BOOL32 WINAPI GetBitmapDimensionEx32(
1030 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
1031 LPSIZE32 size) /* [out] Address of struct receiving dimensions */
1033 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
1034 if (!bmp) return FALSE;
1035 size->cx = (INT32)bmp->size.cx;
1036 size->cy = (INT32)bmp->size.cy;
1037 GDI_HEAP_UNLOCK( hbitmap );
1038 return TRUE;
1042 /***********************************************************************
1043 * GetBitmapDimension (GDI.162)
1045 DWORD WINAPI GetBitmapDimension( HBITMAP16 hbitmap )
1047 SIZE16 size;
1048 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
1049 return MAKELONG( size.cx, size.cy );
1053 /***********************************************************************
1054 * SetBitmapDimensionEx16 (GDI.478)
1056 BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
1057 LPSIZE16 prevSize )
1059 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
1060 if (!bmp) return FALSE;
1061 if (prevSize) *prevSize = bmp->size;
1062 bmp->size.cx = x;
1063 bmp->size.cy = y;
1064 GDI_HEAP_UNLOCK( hbitmap );
1065 return TRUE;
1069 /******************************************************************************
1070 * SetBitmapDimensionEx32 [GDI32.304] Assignes dimensions to a bitmap
1072 * RETURNS
1073 * Success: TRUE
1074 * Failure: FALSE
1076 BOOL32 WINAPI SetBitmapDimensionEx32(
1077 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
1078 INT32 x, /* [in] Bitmap width */
1079 INT32 y, /* [in] Bitmap height */
1080 LPSIZE32 prevSize) /* [out] Address of structure for orig dims */
1082 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
1083 if (!bmp) return FALSE;
1084 if (prevSize) CONV_SIZE16TO32( &bmp->size, prevSize );
1085 bmp->size.cx = (INT16)x;
1086 bmp->size.cy = (INT16)y;
1087 GDI_HEAP_UNLOCK( hbitmap );
1088 return TRUE;
1092 /***********************************************************************
1093 * SetBitmapDimension (GDI.163)
1095 DWORD WINAPI SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
1097 SIZE16 size;
1098 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
1099 return MAKELONG( size.cx, size.cy );