4 * Copyright 1993 Alexandre Julliard
10 #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 */
31 /***********************************************************************
34 BOOL32
BITMAP_Init(void)
38 /* Create the necessary GCs */
40 if ((tmpPixmap
= XCreatePixmap( display
, rootWindow
, 1, 1, 1 )))
42 BITMAP_monoGC
= XCreateGC( display
, tmpPixmap
, 0, NULL
);
43 XSetGraphicsExposures( display
, BITMAP_monoGC
, False
);
44 XFreePixmap( display
, tmpPixmap
);
49 if ((tmpPixmap
= XCreatePixmap(display
, rootWindow
, 1,1,screenDepth
)))
51 BITMAP_colorGC
= XCreateGC( display
, tmpPixmap
, 0, NULL
);
52 XSetGraphicsExposures( display
, BITMAP_colorGC
, False
);
53 XFreePixmap( display
, tmpPixmap
);
60 /***********************************************************************
63 * Create an XImage pointing to the bitmap data.
65 static XImage
*BITMAP_BmpToImage( BITMAP16
* bmp
, LPVOID bmpData
)
67 extern void _XInitImageFuncPtrs( XImage
* );
70 image
= XCreateImage( display
, DefaultVisualOfScreen(screen
),
71 bmp
->bmBitsPixel
, ZPixmap
, 0, bmpData
,
72 bmp
->bmWidth
, bmp
->bmHeight
, 16, bmp
->bmWidthBytes
);
74 image
->byte_order
= MSBFirst
;
75 image
->bitmap_bit_order
= MSBFirst
;
76 image
->bitmap_unit
= 16;
77 _XInitImageFuncPtrs(image
);
82 /***********************************************************************
83 * CreateBitmap (GDI.48) (GDI32.25)
85 HBITMAP16
CreateBitmap( INT32 width
, INT32 height
, UINT32 planes
,
86 UINT32 bpp
, LPCVOID bits
)
88 BITMAPOBJ
* bmpObjPtr
;
91 dprintf_gdi( stddeb
, "CreateBitmap: %dx%d, %d colors\n",
92 width
, height
, 1 << (planes
*bpp
) );
94 /* Check parameters */
95 if (!height
|| !width
|| planes
!= 1) return 0;
96 if ((bpp
!= 1) && (bpp
!= screenDepth
)) return 0;
97 if (height
< 0) height
= -height
;
98 if (width
< 0) width
= -width
;
100 /* Create the BITMAPOBJ */
101 hbitmap
= GDI_AllocObject( sizeof(BITMAPOBJ
), BITMAP_MAGIC
);
102 if (!hbitmap
) return 0;
103 bmpObjPtr
= (BITMAPOBJ
*) GDI_HEAP_LIN_ADDR( hbitmap
);
105 bmpObjPtr
->size
.cx
= 0;
106 bmpObjPtr
->size
.cy
= 0;
107 bmpObjPtr
->bitmap
.bmType
= 0;
108 bmpObjPtr
->bitmap
.bmWidth
= (INT16
)width
;
109 bmpObjPtr
->bitmap
.bmHeight
= (INT16
)height
;
110 bmpObjPtr
->bitmap
.bmPlanes
= (BYTE
)planes
;
111 bmpObjPtr
->bitmap
.bmBitsPixel
= (BYTE
)bpp
;
112 bmpObjPtr
->bitmap
.bmWidthBytes
= (INT16
)BITMAP_WIDTH_BYTES( width
, bpp
);
113 bmpObjPtr
->bitmap
.bmBits
= NULL
;
115 /* Create the pixmap */
116 bmpObjPtr
->pixmap
= XCreatePixmap(display
, rootWindow
, width
, height
, bpp
);
117 if (!bmpObjPtr
->pixmap
)
119 GDI_HEAP_FREE( hbitmap
);
122 else if (bits
) /* Set bitmap bits */
123 SetBitmapBits( hbitmap
, height
* bmpObjPtr
->bitmap
.bmWidthBytes
, bits
);
128 /***********************************************************************
129 * CreateCompatibleBitmap (GDI.51) (GDI32.30)
131 HBITMAP16
CreateCompatibleBitmap( HDC32 hdc
, INT32 width
, INT32 height
)
133 HBITMAP16 hbmpRet
= 0;
136 dprintf_gdi( stddeb
, "CreateCompatibleBitmap(%04x,%d,%d) = \n",
137 hdc
, width
, height
);
138 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
141 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
144 hbmpRet
= CreateBitmap( width
, height
, 1, dc
->w
.bitsPerPixel
, NULL
);
145 dprintf_gdi(stddeb
,"\t\t%04x\n", hbmpRet
);
150 /***********************************************************************
151 * CreateBitmapIndirect16 (GDI.49)
153 HBITMAP16
CreateBitmapIndirect16( const BITMAP16
* bmp
)
155 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
156 bmp
->bmBitsPixel
, PTR_SEG_TO_LIN( bmp
->bmBits
) );
160 /***********************************************************************
161 * CreateBitmapIndirect32 (GDI32.26)
163 HBITMAP32
CreateBitmapIndirect32( const BITMAP32
* bmp
)
165 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
166 bmp
->bmBitsPixel
, bmp
->bmBits
);
170 /***********************************************************************
171 * GetBitmapBits (GDI.74) (GDI32.143)
173 LONG
GetBitmapBits( HBITMAP32 hbitmap
, LONG count
, LPVOID buffer
)
181 fprintf(stderr
, "Negative number of bytes (%ld) passed to GetBitmapBits???\n", count
);
184 bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
187 /* Only get entire lines */
188 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
189 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
190 dprintf_bitmap(stddeb
, "GetBitmapBits: %dx%d %d colors %p fetched height: %ld\n",
191 bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
192 1 << bmp
->bitmap
.bmBitsPixel
, buffer
, height
);
193 if (!height
) return 0;
195 if (!(image
= BITMAP_BmpToImage( &bmp
->bitmap
, buffer
))) return 0;
196 CallTo32_LargeStack( (int(*)())XGetSubImage
, 11,
197 display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
,
198 height
, AllPlanes
, ZPixmap
, image
, 0, 0 );
200 XDestroyImage( image
);
201 return height
* bmp
->bitmap
.bmWidthBytes
;
205 /***********************************************************************
206 * SetBitmapBits (GDI.106) (GDI32.303)
208 LONG
SetBitmapBits( HBITMAP32 hbitmap
, LONG count
, LPCVOID buffer
)
216 fprintf(stderr
, "Negative number of bytes (%ld) passed to SetBitmapBits???\n", count
);
219 bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
222 dprintf_bitmap(stddeb
, "SetBitmapBits: %dx%d %d colors %p\n",
223 bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
224 1 << bmp
->bitmap
.bmBitsPixel
, buffer
);
226 /* Only set entire lines */
227 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
228 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
229 if (!height
) return 0;
231 if (!(image
= BITMAP_BmpToImage( &bmp
->bitmap
, (LPVOID
)buffer
))) return 0;
232 CallTo32_LargeStack( XPutImage
, 10,
233 display
, bmp
->pixmap
, BITMAP_GC(bmp
), image
, 0, 0,
234 0, 0, bmp
->bitmap
.bmWidth
, height
);
236 XDestroyImage( image
);
237 return height
* bmp
->bitmap
.bmWidthBytes
;
240 /**********************************************************************
241 * LoadImageA (USER32.364)
242 * FIXME: implementation still lacks nearly all features, see LR_*
243 * defines in windows.h
246 HANDLE32
LoadImage32A(
247 HINSTANCE32 hinst
,LPCSTR name
,UINT32 type
,INT32 desiredx
,
248 INT32 desiredy
,UINT32 loadflags
251 dprintf_resource(stddeb
,"LoadImage32A(0x%04x,%s,%d,%d,%d,0x%08x)\n",
252 hinst
,name
,type
,desiredx
,desiredy
,loadflags
255 dprintf_resource(stddeb
,"LoadImage32A(0x%04x,%p,%d,%d,%d,0x%08x)\n",
256 hinst
,name
,type
,desiredx
,desiredy
,loadflags
261 return LoadBitmap32A(hinst
,name
);
263 return LoadIcon32A(hinst
,name
);
265 return LoadCursor32A(hinst
,name
);
270 /**********************************************************************
271 * CopyImage32 (USER32.60)
273 * FIXME: implementation still lacks nearly all features, see LR_*
274 * defines in windows.h
276 HANDLE32
CopyImage32( HANDLE32 hnd
, UINT32 type
, INT32 desiredx
,
277 INT32 desiredy
, UINT32 flags
)
282 return hnd
; /* FIXME ... need to copy here */
284 return CopyIcon32(hnd
);
286 return CopyCursor32(hnd
);
292 /**********************************************************************
293 * LoadBitmap16 (USER.175)
295 HBITMAP16
LoadBitmap16( HINSTANCE16 instance
, SEGPTR name
)
297 HBITMAP16 hbitmap
= 0;
305 char *str
= (char *)PTR_SEG_TO_LIN( name
);
306 dprintf_bitmap( stddeb
, "LoadBitmap16(%04x,'%s')\n", instance
, str
);
307 if (str
[0] == '#') name
= (SEGPTR
)(DWORD
)(WORD
)atoi( str
+ 1 );
310 dprintf_bitmap( stddeb
, "LoadBitmap16(%04x,%04x)\n",
311 instance
, LOWORD(name
) );
313 if (!instance
) /* OEM bitmap */
315 if (HIWORD((int)name
)) return 0;
316 return OBM_LoadBitmap( LOWORD((int)name
) );
319 if (!(hRsrc
= FindResource16( instance
, name
, RT_BITMAP
))) return 0;
320 if (!(handle
= LoadResource16( instance
, hRsrc
))) return 0;
322 info
= (BITMAPINFO
*)LockResource16( handle
);
323 if ((hdc
= GetDC32(0)) != 0)
325 char *bits
= (char *)info
+ DIB_BitmapInfoSize( info
, DIB_RGB_COLORS
);
326 hbitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
327 bits
, info
, DIB_RGB_COLORS
);
328 ReleaseDC32( 0, hdc
);
330 FreeResource16( handle
);
334 /**********************************************************************
335 * LoadBitmap32W (USER32.357)
337 HBITMAP32
LoadBitmap32W( HINSTANCE32 instance
, LPCWSTR name
)
339 HBITMAP32 hbitmap
= 0;
345 if (!instance
) /* OEM bitmap */
347 if (HIWORD((int)name
)) return 0;
348 return OBM_LoadBitmap( LOWORD((int)name
) );
351 if (!(hRsrc
= FindResource32W( instance
, name
,
352 (LPWSTR
)RT_BITMAP
))) return 0;
353 if (!(handle
= LoadResource32( instance
, hRsrc
))) return 0;
355 info
= (BITMAPINFO
*)LockResource32( handle
);
356 if ((hdc
= GetDC32(0)) != 0)
358 char *bits
= (char *)info
+ DIB_BitmapInfoSize( info
, DIB_RGB_COLORS
);
359 hbitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
360 bits
, info
, DIB_RGB_COLORS
);
361 ReleaseDC32( 0, hdc
);
367 /**********************************************************************
368 * LoadBitmap32A (USER32.356)
370 HBITMAP32
LoadBitmap32A( HINSTANCE32 instance
, LPCSTR name
)
373 if (!HIWORD(name
)) res
= LoadBitmap32W(instance
,(LPWSTR
)name
);
376 LPWSTR uni
= STRING32_DupAnsiToUni(name
);
377 res
= LoadBitmap32W(instance
,uni
);
384 /***********************************************************************
385 * BITMAP_DeleteObject
387 BOOL32
BITMAP_DeleteObject( HBITMAP16 hbitmap
, BITMAPOBJ
* bmp
)
389 #ifdef PRELIMINARY_WING16_SUPPORT
390 if( bmp
->bitmap
.bmBits
)
391 XShmDetach( display
, (XShmSegmentInfo
*)bmp
->bitmap
.bmBits
);
394 XFreePixmap( display
, bmp
->pixmap
);
395 #ifdef PRELIMINARY_WING16_SUPPORT
396 if( bmp
->bitmap
.bmBits
)
398 __ShmBitmapCtl
* p
= (__ShmBitmapCtl
*)bmp
->bitmap
.bmBits
;
399 WORD sel
= HIWORD(p
->bits
);
400 unsigned long l
, limit
= GetSelectorLimit(sel
);
402 for( l
= 0; l
< limit
; l
+= 0x10000, sel
+= __AHINCR
)
404 shmctl(p
->si
.shmid
, IPC_RMID
, NULL
);
405 shmdt(p
->si
.shmaddr
); /* already marked for destruction */
408 return GDI_FreeObject( hbitmap
);
412 /***********************************************************************
415 INT16
BITMAP_GetObject16( BITMAPOBJ
* bmp
, INT16 count
, LPVOID buffer
)
417 if (count
> sizeof(bmp
->bitmap
)) count
= sizeof(bmp
->bitmap
);
418 memcpy( buffer
, &bmp
->bitmap
, count
);
423 /***********************************************************************
426 INT32
BITMAP_GetObject32( BITMAPOBJ
* bmp
, INT32 count
, LPVOID buffer
)
429 bmp32
.bmType
= bmp
->bitmap
.bmType
;
430 bmp32
.bmWidth
= bmp
->bitmap
.bmWidth
;
431 bmp32
.bmHeight
= bmp
->bitmap
.bmHeight
;
432 bmp32
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
433 bmp32
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
434 bmp32
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
436 if (count
> sizeof(bmp32
)) count
= sizeof(bmp32
);
437 memcpy( buffer
, &bmp32
, count
);
442 /***********************************************************************
443 * BITMAP_SelectObject
445 HBITMAP16
BITMAP_SelectObject( DC
* dc
, HBITMAP16 hbitmap
,
449 HBITMAP16 prevHandle
= dc
->w
.hBitmap
;
451 if (!(dc
->w
.flags
& DC_MEMORY
)) return 0;
454 SetRectRgn(dc
->w
.hVisRgn
, 0, 0, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
);
457 hrgn
= CreateRectRgn32(0, 0, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
);
459 dc
->w
.hVisRgn
= hrgn
;
462 dc
->u
.x
.drawable
= bmp
->pixmap
;
463 dc
->w
.hBitmap
= hbitmap
;
465 /* Change GC depth if needed */
467 if (dc
->w
.bitsPerPixel
!= bmp
->bitmap
.bmBitsPixel
)
469 XFreeGC( display
, dc
->u
.x
.gc
);
470 dc
->u
.x
.gc
= XCreateGC( display
, dc
->u
.x
.drawable
, 0, NULL
);
471 dc
->w
.bitsPerPixel
= bmp
->bitmap
.bmBitsPixel
;
474 else CLIPPING_UpdateGCRegion( dc
); /* Just update GC clip region */
478 /***********************************************************************
479 * CreateDiscardableBitmap (GDI.156) (GDI32.38)
481 HBITMAP16
CreateDiscardableBitmap( HDC32 hdc
, INT32 width
, INT32 height
)
483 dprintf_bitmap(stddeb
,"CreateDiscardableBitmap(%04x, %d, %d); "
484 "// call CreateCompatibleBitmap() for now!\n",
486 return CreateCompatibleBitmap(hdc
, width
, height
);
490 /***********************************************************************
491 * GetBitmapDimensionEx16 (GDI.468)
493 BOOL16
GetBitmapDimensionEx16( HBITMAP16 hbitmap
, LPSIZE16 size
)
495 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
496 if (!bmp
) return FALSE
;
502 /***********************************************************************
503 * GetBitmapDimensionEx32 (GDI32.144)
505 BOOL32
GetBitmapDimensionEx32( HBITMAP32 hbitmap
, LPSIZE32 size
)
507 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
508 if (!bmp
) return FALSE
;
509 size
->cx
= (INT32
)bmp
->size
.cx
;
510 size
->cy
= (INT32
)bmp
->size
.cy
;
515 /***********************************************************************
516 * GetBitmapDimension (GDI.162)
518 DWORD
GetBitmapDimension( HBITMAP16 hbitmap
)
521 if (!GetBitmapDimensionEx16( hbitmap
, &size
)) return 0;
522 return MAKELONG( size
.cx
, size
.cy
);
526 /***********************************************************************
527 * SetBitmapDimensionEx16 (GDI.478)
529 BOOL16
SetBitmapDimensionEx16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
,
532 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
533 if (!bmp
) return FALSE
;
534 if (prevSize
) *prevSize
= bmp
->size
;
541 /***********************************************************************
542 * SetBitmapDimensionEx32 (GDI32.304)
544 BOOL32
SetBitmapDimensionEx32( HBITMAP32 hbitmap
, INT32 x
, INT32 y
,
547 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
548 if (!bmp
) return FALSE
;
549 if (prevSize
) CONV_SIZE16TO32( &bmp
->size
, prevSize
);
550 bmp
->size
.cx
= (INT16
)x
;
551 bmp
->size
.cy
= (INT16
)y
;
556 /***********************************************************************
557 * SetBitmapDimension (GDI.163)
559 DWORD
SetBitmapDimension( HBITMAP16 hbitmap
, INT16 x
, INT16 y
)
562 if (!SetBitmapDimensionEx16( hbitmap
, x
, y
, &size
)) return 0;
563 return MAKELONG( size
.cx
, size
.cy
);