4 * Copyright 1993 Alexandre Julliard
11 #include <X11/Xutil.h>
20 #ifdef PRELIMINARY_WING16_SUPPORT
21 #include <sys/types.h>
26 /* GCs used for B&W and color bitmap operations */
27 GC BITMAP_monoGC
= 0, BITMAP_colorGC
= 0;
29 extern void CLIPPING_UpdateGCRegion( DC
* dc
); /* objects/clipping.c */
32 /***********************************************************************
33 * CreateBitmap16 (GDI.48)
35 HBITMAP16
CreateBitmap16( INT16 width
, INT16 height
, UINT16 planes
,
36 UINT16 bpp
, LPCVOID bits
)
38 return CreateBitmap32( width
, height
, planes
, bpp
, bits
);
42 /***********************************************************************
43 * CreateBitmap32 (GDI32.25)
45 HBITMAP32
CreateBitmap32( INT32 width
, INT32 height
, UINT32 planes
,
46 UINT32 bpp
, LPCVOID bits
)
48 BITMAPOBJ
* bmpObjPtr
;
51 planes
= (BYTE
)planes
;
54 dprintf_gdi( stddeb
, "CreateBitmap: %dx%d, %d colors\n",
55 width
, height
, 1 << (planes
*bpp
) );
57 /* Check parameters */
58 if (!height
|| !width
|| planes
!= 1) return 0;
59 if ((bpp
!= 1) && (bpp
!= screenDepth
)) return 0;
60 if (height
< 0) height
= -height
;
61 if (width
< 0) width
= -width
;
63 /* Create the BITMAPOBJ */
64 hbitmap
= GDI_AllocObject( sizeof(BITMAPOBJ
), BITMAP_MAGIC
);
65 if (!hbitmap
) return 0;
66 bmpObjPtr
= (BITMAPOBJ
*) GDI_HEAP_LIN_ADDR( hbitmap
);
68 bmpObjPtr
->size
.cx
= 0;
69 bmpObjPtr
->size
.cy
= 0;
70 bmpObjPtr
->bitmap
.bmType
= 0;
71 bmpObjPtr
->bitmap
.bmWidth
= (INT16
)width
;
72 bmpObjPtr
->bitmap
.bmHeight
= (INT16
)height
;
73 bmpObjPtr
->bitmap
.bmPlanes
= (BYTE
)planes
;
74 bmpObjPtr
->bitmap
.bmBitsPixel
= (BYTE
)bpp
;
75 bmpObjPtr
->bitmap
.bmWidthBytes
= (INT16
)BITMAP_WIDTH_BYTES( width
, bpp
);
76 bmpObjPtr
->bitmap
.bmBits
= NULL
;
78 /* Create the pixmap */
79 bmpObjPtr
->pixmap
= XCreatePixmap(display
, rootWindow
, width
, height
, bpp
);
80 if (!bmpObjPtr
->pixmap
)
82 GDI_HEAP_FREE( hbitmap
);
85 else if (bits
) /* Set bitmap bits */
86 SetBitmapBits32( hbitmap
, height
* bmpObjPtr
->bitmap
.bmWidthBytes
,
92 /***********************************************************************
93 * CreateCompatibleBitmap16 (GDI.51)
95 HBITMAP16
CreateCompatibleBitmap16( HDC16 hdc
, INT16 width
, INT16 height
)
97 return CreateCompatibleBitmap32( hdc
, width
, height
);
101 /***********************************************************************
102 * CreateCompatibleBitmap32 (GDI32.30)
104 HBITMAP32
CreateCompatibleBitmap32( HDC32 hdc
, INT32 width
, INT32 height
)
106 HBITMAP32 hbmpRet
= 0;
109 dprintf_gdi( stddeb
, "CreateCompatibleBitmap(%04x,%d,%d) = \n",
110 hdc
, width
, height
);
111 if (!(dc
= DC_GetDCPtr( hdc
))) return 0;
112 hbmpRet
= CreateBitmap32( width
, height
, 1, dc
->w
.bitsPerPixel
, NULL
);
113 dprintf_gdi(stddeb
,"\t\t%04x\n", hbmpRet
);
118 /***********************************************************************
119 * CreateBitmapIndirect16 (GDI.49)
121 HBITMAP16
CreateBitmapIndirect16( const BITMAP16
* bmp
)
123 return CreateBitmap16( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
124 bmp
->bmBitsPixel
, PTR_SEG_TO_LIN( bmp
->bmBits
) );
128 /***********************************************************************
129 * CreateBitmapIndirect32 (GDI32.26)
131 HBITMAP32
CreateBitmapIndirect32( const BITMAP32
* bmp
)
133 return CreateBitmap32( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
134 bmp
->bmBitsPixel
, bmp
->bmBits
);
138 /***********************************************************************
139 * GetBitmapBits16 (GDI.74)
141 LONG
GetBitmapBits16( HBITMAP16 hbitmap
, LONG count
, LPVOID buffer
)
143 return GetBitmapBits32( hbitmap
, count
, buffer
);
147 /***********************************************************************
148 * GetBitmapBits32 (GDI32.143)
150 LONG
GetBitmapBits32( HBITMAP32 hbitmap
, LONG count
, LPVOID buffer
)
153 LONG height
,widthbytes
;
155 LPBYTE tmpbuffer
,tbuf
;
160 fprintf(stderr
, "Negative number of bytes (%ld) passed to GetBitmapBits???\n", count
);
163 bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
166 /* Only get entire lines */
167 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
168 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
169 dprintf_bitmap(stddeb
, "GetBitmapBits: %dx%d %d colors %p fetched height: %ld\n",
170 bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
171 1 << bmp
->bitmap
.bmBitsPixel
, buffer
, height
);
172 if (!height
) return 0;
174 switch (bmp
->bitmap
.bmBitsPixel
) {
176 if (!(bmp
->bitmap
.bmWidth
& 15))
179 pad
= ((16 - (bmp
->bitmap
.bmWidth
& 15)) + 7) / 8;
182 if (!(bmp
->bitmap
.bmWidth
& 3))
185 pad
= ((4 - (bmp
->bitmap
.bmWidth
& 3)) + 1) / 2;
188 pad
= (2 - (bmp
->bitmap
.bmWidth
& 1)) & 1;
192 pad
= 0; /* we have 16bit alignment already */
195 pad
= (bmp
->bitmap
.bmWidth
*3) & 1;
198 fprintf(stderr
,"GetBitMapBits32: unknown depth %d, please report.\n",
199 bmp
->bitmap
.bmBitsPixel
204 widthbytes
= DIB_GetXImageWidthBytes(bmp
->bitmap
.bmWidth
,bmp
->bitmap
.bmBitsPixel
);
205 tmpbuffer
= (LPBYTE
)xmalloc(widthbytes
*height
);
206 image
= XCreateImage( display
, DefaultVisualOfScreen(screen
),
207 bmp
->bitmap
.bmBitsPixel
, ZPixmap
, 0, tmpbuffer
,
208 bmp
->bitmap
.bmWidth
,height
,32,widthbytes
211 CallTo32_LargeStack( (int(*)())XGetSubImage
, 11,
212 display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
,
213 height
, AllPlanes
, ZPixmap
, image
, 0, 0 );
215 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
218 switch (bmp
->bitmap
.bmBitsPixel
)
221 for (h
=0;h
<height
;h
++)
224 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
228 *tbuf
|= XGetPixel(image
,w
,h
)<<(7-(w
&7));
229 if ((w
&7) == 7) ++tbuf
;
235 for (h
=0;h
<height
;h
++)
237 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
239 if (!(w
& 1)) *tbuf
= XGetPixel( image
, w
, h
) << 4;
240 else *tbuf
++ |= XGetPixel( image
, w
, h
) & 0x0f;
246 for (h
=0;h
<height
;h
++)
248 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
249 *tbuf
++ = XGetPixel(image
,w
,h
);
255 for (h
=0;h
<height
;h
++)
257 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
259 long pixel
= XGetPixel(image
,w
,h
);
261 *tbuf
++ = pixel
& 0xff;
262 *tbuf
++ = (pixel
>>8) & 0xff;
267 for (h
=0;h
<height
;h
++)
269 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
271 long pixel
= XGetPixel(image
,w
,h
);
273 *tbuf
++ = pixel
& 0xff;
274 *tbuf
++ = (pixel
>> 8) & 0xff;
275 *tbuf
++ = (pixel
>>16) & 0xff;
280 XDestroyImage( image
); /* frees tbuffer too */
281 return height
* bmp
->bitmap
.bmWidthBytes
;
285 /***********************************************************************
286 * SetBitmapBits16 (GDI.106)
288 LONG
SetBitmapBits16( HBITMAP16 hbitmap
, LONG count
, LPCVOID buffer
)
290 return SetBitmapBits32( hbitmap
, count
, buffer
);
294 /***********************************************************************
295 * SetBitmapBits32 (GDI32.303)
297 LONG
SetBitmapBits32( HBITMAP32 hbitmap
, LONG count
, LPCVOID buffer
)
302 LPBYTE sbuf
,tmpbuffer
;
303 int w
,h
,pad
,widthbytes
;
307 fprintf(stderr
, "Negative number of bytes (%ld) passed to SetBitmapBits???\n", count
);
310 bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
313 dprintf_bitmap(stddeb
, "SetBitmapBits: %dx%d %d colors %p\n",
314 bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
315 1 << bmp
->bitmap
.bmBitsPixel
, buffer
);
317 /* Only set entire lines */
318 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
319 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
320 if (!height
) return 0;
322 switch (bmp
->bitmap
.bmBitsPixel
) {
324 if (!(bmp
->bitmap
.bmWidth
& 15))
327 pad
= ((16 - (bmp
->bitmap
.bmWidth
& 15)) + 7) / 8;
330 if (!(bmp
->bitmap
.bmWidth
& 3))
333 pad
= ((4 - (bmp
->bitmap
.bmWidth
& 3)) + 1) / 2;
336 pad
= (2 - (bmp
->bitmap
.bmWidth
& 1)) & 1;
340 pad
= 0; /* we have 16bit alignment already */
343 pad
= (bmp
->bitmap
.bmWidth
*3) & 1;
346 fprintf(stderr
,"SetBitMapBits32: unknown depth %d, please report.\n",
347 bmp
->bitmap
.bmBitsPixel
351 sbuf
= (LPBYTE
)buffer
;
353 widthbytes
= DIB_GetXImageWidthBytes(bmp
->bitmap
.bmWidth
,bmp
->bitmap
.bmBitsPixel
);
354 tmpbuffer
= (LPBYTE
)xmalloc(widthbytes
*height
);
355 image
= XCreateImage( display
, DefaultVisualOfScreen(screen
),
356 bmp
->bitmap
.bmBitsPixel
, ZPixmap
, 0, tmpbuffer
,
357 bmp
->bitmap
.bmWidth
,height
,32,widthbytes
360 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
361 sbuf
= (LPBYTE
)buffer
;
362 switch (bmp
->bitmap
.bmBitsPixel
)
365 for (h
=0;h
<height
;h
++)
367 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
369 XPutPixel(image
,w
,h
,(sbuf
[0]>>(7-(w
&7))) & 1);
377 for (h
=0;h
<height
;h
++)
379 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
381 if (!(w
& 1)) XPutPixel( image
, w
, h
, *sbuf
>> 4 );
382 else XPutPixel( image
, w
, h
, *sbuf
++ & 0xf );
388 for (h
=0;h
<height
;h
++)
390 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
391 XPutPixel(image
,w
,h
,*sbuf
++);
397 for (h
=0;h
<height
;h
++)
399 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
401 XPutPixel(image
,w
,h
,sbuf
[1]*256+sbuf
[0]);
407 for (h
=0;h
<height
;h
++)
409 for (w
=0;w
<bmp
->bitmap
.bmWidth
;w
++)
411 XPutPixel(image
,w
,h
,(sbuf
[2]<<16)+(sbuf
[1]<<8)+sbuf
[0]);
419 CallTo32_LargeStack( XPutImage
, 10,
420 display
, bmp
->pixmap
, BITMAP_GC(bmp
), image
, 0, 0,
421 0, 0, bmp
->bitmap
.bmWidth
, height
);
422 XDestroyImage( image
); /* frees tmpbuffer too */
423 return height
* bmp
->bitmap
.bmWidthBytes
;
426 /**********************************************************************
427 * LoadImageA (USER32.364)
428 * FIXME: implementation still lacks nearly all features, see LR_*
429 * defines in windows.h
432 HANDLE32
LoadImage32A(
433 HINSTANCE32 hinst
,LPCSTR name
,UINT32 type
,INT32 desiredx
,
434 INT32 desiredy
,UINT32 loadflags
437 dprintf_resource(stddeb
,"LoadImage32A(0x%04x,%s,%d,%d,%d,0x%08x)\n",
438 hinst
,name
,type
,desiredx
,desiredy
,loadflags
441 dprintf_resource(stddeb
,"LoadImage32A(0x%04x,%p,%d,%d,%d,0x%08x)\n",
442 hinst
,name
,type
,desiredx
,desiredy
,loadflags
447 return LoadBitmap32A(hinst
,name
);
449 return LoadIcon32A(hinst
,name
);
451 return LoadCursor32A(hinst
,name
);
456 /**********************************************************************
457 * CopyImage32 (USER32.60)
459 * FIXME: implementation still lacks nearly all features, see LR_*
460 * defines in windows.h
462 HANDLE32
CopyImage32( HANDLE32 hnd
, UINT32 type
, INT32 desiredx
,
463 INT32 desiredy
, UINT32 flags
)
468 return hnd
; /* FIXME ... need to copy here */
470 return CopyIcon32(hnd
);
472 return CopyCursor32(hnd
);
478 /**********************************************************************
479 * LoadBitmap16 (USER.175)
481 HBITMAP16
LoadBitmap16( HINSTANCE16 instance
, SEGPTR name
)
483 HBITMAP32 hbitmap
= 0;
491 char *str
= (char *)PTR_SEG_TO_LIN( name
);
492 dprintf_bitmap( stddeb
, "LoadBitmap16(%04x,'%s')\n", instance
, str
);
493 if (str
[0] == '#') name
= (SEGPTR
)(DWORD
)(WORD
)atoi( str
+ 1 );
496 dprintf_bitmap( stddeb
, "LoadBitmap16(%04x,%04x)\n",
497 instance
, LOWORD(name
) );
499 if (!instance
) /* OEM bitmap */
501 if (HIWORD((int)name
)) return 0;
502 return OBM_LoadBitmap( LOWORD((int)name
) );
505 if (!(hRsrc
= FindResource16( instance
, name
, RT_BITMAP
))) return 0;
506 if (!(handle
= LoadResource16( instance
, hRsrc
))) return 0;
508 info
= (BITMAPINFO
*)LockResource16( handle
);
509 if ((hdc
= GetDC32(0)) != 0)
511 char *bits
= (char *)info
+ DIB_BitmapInfoSize( info
, DIB_RGB_COLORS
);
512 hbitmap
= CreateDIBitmap32( hdc
, &info
->bmiHeader
, CBM_INIT
,
513 bits
, info
, DIB_RGB_COLORS
);
514 ReleaseDC32( 0, hdc
);
516 FreeResource16( handle
);
520 /**********************************************************************
521 * LoadBitmap32W (USER32.357)
523 HBITMAP32
LoadBitmap32W( HINSTANCE32 instance
, LPCWSTR name
)
525 HBITMAP32 hbitmap
= 0;
531 if (!instance
) /* OEM bitmap */
533 if (HIWORD((int)name
)) return 0;
534 return OBM_LoadBitmap( LOWORD((int)name
) );
537 if (!(hRsrc
= FindResource32W( instance
, name
,
538 (LPWSTR
)RT_BITMAP
))) return 0;
539 if (!(handle
= LoadResource32( instance
, hRsrc
))) return 0;
541 info
= (BITMAPINFO
*)LockResource32( handle
);
542 if ((hdc
= GetDC32(0)) != 0)
544 char *bits
= (char *)info
+ DIB_BitmapInfoSize( info
, DIB_RGB_COLORS
);
545 hbitmap
= CreateDIBitmap32( hdc
, &info
->bmiHeader
, CBM_INIT
,
546 bits
, info
, DIB_RGB_COLORS
);
547 ReleaseDC32( 0, hdc
);
553 /**********************************************************************
554 * LoadBitmap32A (USER32.356)
556 HBITMAP32
LoadBitmap32A( HINSTANCE32 instance
, LPCSTR name
)
559 if (!HIWORD(name
)) res
= LoadBitmap32W( instance
, (LPWSTR
)name
);
562 LPWSTR uni
= HEAP_strdupAtoW( GetProcessHeap(), 0, name
);
563 res
= LoadBitmap32W( instance
, uni
);
564 HeapFree( GetProcessHeap(), 0, uni
);
570 /***********************************************************************
571 * BITMAP_DeleteObject
573 BOOL32
BITMAP_DeleteObject( HBITMAP16 hbitmap
, BITMAPOBJ
* bmp
)
575 #ifdef PRELIMINARY_WING16_SUPPORT
576 if( bmp
->bitmap
.bmBits
)
577 XShmDetach( display
, (XShmSegmentInfo
*)bmp
->bitmap
.bmBits
);
580 XFreePixmap( display
, bmp
->pixmap
);
581 #ifdef PRELIMINARY_WING16_SUPPORT
582 if( bmp
->bitmap
.bmBits
)
584 __ShmBitmapCtl
* p
= (__ShmBitmapCtl
*)bmp
->bitmap
.bmBits
;
585 WORD sel
= HIWORD(p
->bits
);
586 unsigned long l
, limit
= GetSelectorLimit(sel
);
588 for( l
= 0; l
< limit
; l
+= 0x10000, sel
+= __AHINCR
)
590 shmctl(p
->si
.shmid
, IPC_RMID
, NULL
);
591 shmdt(p
->si
.shmaddr
); /* already marked for destruction */
594 return GDI_FreeObject( hbitmap
);
598 /***********************************************************************
601 INT16
BITMAP_GetObject16( BITMAPOBJ
* bmp
, INT16 count
, LPVOID buffer
)
603 if (count
> sizeof(bmp
->bitmap
)) count
= sizeof(bmp
->bitmap
);
604 memcpy( buffer
, &bmp
->bitmap
, count
);
609 /***********************************************************************
612 INT32
BITMAP_GetObject32( BITMAPOBJ
* bmp
, INT32 count
, LPVOID buffer
)
615 bmp32
.bmType
= bmp
->bitmap
.bmType
;
616 bmp32
.bmWidth
= bmp
->bitmap
.bmWidth
;
617 bmp32
.bmHeight
= bmp
->bitmap
.bmHeight
;
618 bmp32
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
619 bmp32
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
620 bmp32
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
622 if (count
> sizeof(bmp32
)) count
= sizeof(bmp32
);
623 memcpy( buffer
, &bmp32
, count
);
629 /***********************************************************************
630 * CreateDiscardableBitmap16 (GDI.156)
632 HBITMAP16
CreateDiscardableBitmap16( HDC16 hdc
, INT16 width
, INT16 height
)
634 return CreateCompatibleBitmap16( hdc
, width
, height
);
638 /***********************************************************************
639 * CreateDiscardableBitmap32 (GDI32.38)
641 HBITMAP32
CreateDiscardableBitmap32( HDC32 hdc
, INT32 width
, INT32 height
)
643 return CreateCompatibleBitmap32( hdc
, width
, height
);
647 /***********************************************************************
648 * GetBitmapDimensionEx16 (GDI.468)
650 BOOL16
GetBitmapDimensionEx16( HBITMAP16 hbitmap
, LPSIZE16 size
)
652 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
653 if (!bmp
) return FALSE
;
659 /***********************************************************************
660 * GetBitmapDimensionEx32 (GDI32.144)
662 BOOL32
GetBitmapDimensionEx32( HBITMAP32 hbitmap
, LPSIZE32 size
)
664 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
665 if (!bmp
) return FALSE
;
666 size
->cx
= (INT32
)bmp
->size
.cx
;
667 size
->cy
= (INT32
)bmp
->size
.cy
;
672 /***********************************************************************
673 * GetBitmapDimension (GDI.162)
675 DWORD
GetBitmapDimension( HBITMAP16 hbitmap
)
678 if (!GetBitmapDimensionEx16( hbitmap
, &size
)) return 0;
679 return MAKELONG( size
.cx
, size
.cy
);
683 /***********************************************************************
684 * SetBitmapDimensionEx16 (GDI.478)
686 BOOL16
SetBitmapDimensionEx16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
,
689 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
690 if (!bmp
) return FALSE
;
691 if (prevSize
) *prevSize
= bmp
->size
;
698 /***********************************************************************
699 * SetBitmapDimensionEx32 (GDI32.304)
701 BOOL32
SetBitmapDimensionEx32( HBITMAP32 hbitmap
, INT32 x
, INT32 y
,
704 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
705 if (!bmp
) return FALSE
;
706 if (prevSize
) CONV_SIZE16TO32( &bmp
->size
, prevSize
);
707 bmp
->size
.cx
= (INT16
)x
;
708 bmp
->size
.cy
= (INT16
)y
;
713 /***********************************************************************
714 * SetBitmapDimension (GDI.163)
716 DWORD
SetBitmapDimension( HBITMAP16 hbitmap
, INT16 x
, INT16 y
)
719 if (!SetBitmapDimensionEx16( hbitmap
, x
, y
, &size
)) return 0;
720 return MAKELONG( size
.cx
, size
.cy
);