4 * Copyright 1993 Alexandre Julliard
10 #include <X11/Xutil.h>
20 /* GCs used for B&W and color bitmap operations */
21 GC BITMAP_monoGC
= 0, BITMAP_colorGC
= 0;
23 extern void CLIPPING_UpdateGCRegion( DC
* dc
); /* objects/clipping.c */
25 /***********************************************************************
28 BOOL32
BITMAP_Init(void)
32 /* Create the necessary GCs */
34 if ((tmpPixmap
= XCreatePixmap( display
, rootWindow
, 1, 1, 1 )))
36 BITMAP_monoGC
= XCreateGC( display
, tmpPixmap
, 0, NULL
);
37 XSetGraphicsExposures( display
, BITMAP_monoGC
, False
);
38 XFreePixmap( display
, tmpPixmap
);
43 if ((tmpPixmap
= XCreatePixmap(display
, rootWindow
, 1,1,screenDepth
)))
45 BITMAP_colorGC
= XCreateGC( display
, tmpPixmap
, 0, NULL
);
46 XSetGraphicsExposures( display
, BITMAP_colorGC
, False
);
47 XFreePixmap( display
, tmpPixmap
);
54 /***********************************************************************
57 * Create an XImage pointing to the bitmap data.
59 static XImage
*BITMAP_BmpToImage( BITMAP16
* bmp
, LPVOID bmpData
)
61 extern void _XInitImageFuncPtrs( XImage
* );
64 image
= XCreateImage( display
, DefaultVisualOfScreen(screen
),
65 bmp
->bmBitsPixel
, ZPixmap
, 0, bmpData
,
66 bmp
->bmWidth
, bmp
->bmHeight
, 16, bmp
->bmWidthBytes
);
68 image
->byte_order
= MSBFirst
;
69 image
->bitmap_bit_order
= MSBFirst
;
70 image
->bitmap_unit
= 16;
71 _XInitImageFuncPtrs(image
);
76 /***********************************************************************
77 * CreateBitmap (GDI.48) (GDI32.25)
79 HBITMAP16
CreateBitmap( INT32 width
, INT32 height
, UINT32 planes
,
80 UINT32 bpp
, LPCVOID bits
)
82 BITMAPOBJ
* bmpObjPtr
;
85 dprintf_gdi( stddeb
, "CreateBitmap: %dx%d, %d colors\n",
86 width
, height
, 1 << (planes
*bpp
) );
88 /* Check parameters */
89 if (!height
|| !width
|| planes
!= 1) return 0;
90 if ((bpp
!= 1) && (bpp
!= screenDepth
)) return 0;
91 if (height
< 0) height
= -height
;
92 if (width
< 0) width
= -width
;
94 /* Create the BITMAPOBJ */
95 hbitmap
= GDI_AllocObject( sizeof(BITMAPOBJ
), BITMAP_MAGIC
);
96 if (!hbitmap
) return 0;
97 bmpObjPtr
= (BITMAPOBJ
*) GDI_HEAP_LIN_ADDR( hbitmap
);
99 bmpObjPtr
->size
.cx
= 0;
100 bmpObjPtr
->size
.cy
= 0;
101 bmpObjPtr
->bitmap
.bmType
= 0;
102 bmpObjPtr
->bitmap
.bmWidth
= (INT16
)width
;
103 bmpObjPtr
->bitmap
.bmHeight
= (INT16
)height
;
104 bmpObjPtr
->bitmap
.bmPlanes
= (BYTE
)planes
;
105 bmpObjPtr
->bitmap
.bmBitsPixel
= (BYTE
)bpp
;
106 bmpObjPtr
->bitmap
.bmWidthBytes
= (INT16
)BITMAP_WIDTH_BYTES( width
, bpp
);
107 bmpObjPtr
->bitmap
.bmBits
= NULL
;
109 /* Create the pixmap */
110 bmpObjPtr
->pixmap
= XCreatePixmap(display
, rootWindow
, width
, height
, bpp
);
111 if (!bmpObjPtr
->pixmap
)
113 GDI_HEAP_FREE( hbitmap
);
116 else if (bits
) /* Set bitmap bits */
117 SetBitmapBits( hbitmap
, height
* bmpObjPtr
->bitmap
.bmWidthBytes
, bits
);
122 /***********************************************************************
123 * CreateCompatibleBitmap (GDI.51) (GDI32.30)
125 HBITMAP16
CreateCompatibleBitmap( HDC32 hdc
, INT32 width
, INT32 height
)
130 dprintf_gdi( stddeb
, "CreateCompatibleBitmap(%04x,%d,%d) = \n",
131 hdc
, width
, height
);
132 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
135 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
138 hbmpRet
= CreateBitmap( width
, height
, 1, dc
->w
.bitsPerPixel
, NULL
);
139 dprintf_gdi(stddeb
,"\t\t%04x\n", hbmpRet
);
144 /***********************************************************************
145 * CreateBitmapIndirect16 (GDI.49)
147 HBITMAP16
CreateBitmapIndirect16( const BITMAP16
* bmp
)
149 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
150 bmp
->bmBitsPixel
, PTR_SEG_TO_LIN( bmp
->bmBits
) );
154 /***********************************************************************
155 * CreateBitmapIndirect32 (GDI32.26)
157 HBITMAP32
CreateBitmapIndirect32( const BITMAP32
* bmp
)
159 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
160 bmp
->bmBitsPixel
, bmp
->bmBits
);
164 /***********************************************************************
165 * GetBitmapBits (GDI.74) (GDI32.143)
167 LONG
GetBitmapBits( HBITMAP32 hbitmap
, LONG count
, LPVOID buffer
)
175 fprintf(stderr
, "Negative number of bytes (%ld) passed to GetBitmapBits???\n", count
);
178 bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
181 /* Only get entire lines */
182 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
183 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
184 dprintf_bitmap(stddeb
, "GetBitmapBits: %dx%d %d colors %p fetched height: %ld\n",
185 bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
186 1 << bmp
->bitmap
.bmBitsPixel
, buffer
, height
);
187 if (!height
) return 0;
189 if (!(image
= BITMAP_BmpToImage( &bmp
->bitmap
, buffer
))) return 0;
190 CallTo32_LargeStack( (int(*)())XGetSubImage
, 11,
191 display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
,
192 height
, AllPlanes
, ZPixmap
, image
, 0, 0 );
194 XDestroyImage( image
);
195 return height
* bmp
->bitmap
.bmWidthBytes
;
199 /***********************************************************************
200 * SetBitmapBits (GDI.106) (GDI32.303)
202 LONG
SetBitmapBits( HBITMAP32 hbitmap
, LONG count
, LPCVOID buffer
)
210 fprintf(stderr
, "Negative number of bytes (%ld) passed to SetBitmapBits???\n", count
);
213 bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
216 dprintf_bitmap(stddeb
, "SetBitmapBits: %dx%d %d colors %p\n",
217 bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
218 1 << bmp
->bitmap
.bmBitsPixel
, buffer
);
220 /* Only set entire lines */
221 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
222 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
223 if (!height
) return 0;
225 if (!(image
= BITMAP_BmpToImage( &bmp
->bitmap
, (LPVOID
)buffer
))) return 0;
226 CallTo32_LargeStack( XPutImage
, 10,
227 display
, bmp
->pixmap
, BITMAP_GC(bmp
), image
, 0, 0,
228 0, 0, bmp
->bitmap
.bmWidth
, height
);
230 XDestroyImage( image
);
231 return height
* bmp
->bitmap
.bmWidthBytes
;
234 /**********************************************************************
235 * LoadImageA (USER32.364)
236 * FIXME: implementation still lacks nearly all features, see LR_*
237 * defines in windows.h
240 HANDLE32
LoadImage32A(
241 HINSTANCE32 hinst
,LPCSTR name
,UINT32 type
,INT32 desiredx
,
242 INT32 desiredy
,UINT32 loadflags
245 dprintf_resource(stddeb
,"LoadImage32A(0x%04x,%s,%d,%d,%d,0x%08x)\n",
246 hinst
,name
,type
,desiredx
,desiredy
,loadflags
249 dprintf_resource(stddeb
,"LoadImage32A(0x%04x,%p,%d,%d,%d,0x%08x)\n",
250 hinst
,name
,type
,desiredx
,desiredy
,loadflags
255 return LoadBitmap32A(hinst
,name
);
257 return LoadIcon32A(hinst
,name
);
259 return LoadCursor32A(hinst
,name
);
264 /**********************************************************************
265 * CopyImage32 (USER32.60)
267 * FIXME: implementation still lacks nearly all features, see LR_*
268 * defines in windows.h
270 HANDLE32
CopyImage32( HANDLE32 hnd
, UINT32 type
, INT32 desiredx
,
271 INT32 desiredy
, UINT32 flags
)
276 return hnd
; /* FIXME ... need to copy here */
278 return CopyIcon32(hnd
);
280 return CopyCursor32(hnd
);
286 /**********************************************************************
287 * LoadBitmap16 (USER.175)
289 HBITMAP16
LoadBitmap16( HINSTANCE16 instance
, SEGPTR name
)
291 HBITMAP16 hbitmap
= 0;
299 char *str
= (char *)PTR_SEG_TO_LIN( name
);
300 dprintf_bitmap( stddeb
, "LoadBitmap16(%04x,'%s')\n", instance
, str
);
301 if (str
[0] == '#') name
= (SEGPTR
)(DWORD
)(WORD
)atoi( str
+ 1 );
304 dprintf_bitmap( stddeb
, "LoadBitmap16(%04x,%04x)\n",
305 instance
, LOWORD(name
) );
307 if (!instance
) /* OEM bitmap */
309 if (HIWORD((int)name
)) return 0;
310 return OBM_LoadBitmap( LOWORD((int)name
) );
313 if (!(hRsrc
= FindResource16( instance
, name
, RT_BITMAP
))) return 0;
314 if (!(handle
= LoadResource16( instance
, hRsrc
))) return 0;
316 info
= (BITMAPINFO
*)LockResource16( handle
);
317 if ((hdc
= GetDC32(0)) != 0)
319 char *bits
= (char *)info
+ DIB_BitmapInfoSize( info
, DIB_RGB_COLORS
);
320 hbitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
321 bits
, info
, DIB_RGB_COLORS
);
322 ReleaseDC32( 0, hdc
);
324 FreeResource16( handle
);
328 /**********************************************************************
329 * LoadBitmap32W (USER32.357)
331 HBITMAP32
LoadBitmap32W( HINSTANCE32 instance
, LPCWSTR name
)
333 HBITMAP32 hbitmap
= 0;
339 if (!instance
) /* OEM bitmap */
341 if (HIWORD((int)name
)) return 0;
342 return OBM_LoadBitmap( LOWORD((int)name
) );
345 if (!(hRsrc
= FindResource32W( instance
, name
,
346 (LPWSTR
)RT_BITMAP
))) return 0;
347 if (!(handle
= LoadResource32( instance
, hRsrc
))) return 0;
349 info
= (BITMAPINFO
*)LockResource32( handle
);
350 if ((hdc
= GetDC32(0)) != 0)
352 char *bits
= (char *)info
+ DIB_BitmapInfoSize( info
, DIB_RGB_COLORS
);
353 hbitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
354 bits
, info
, DIB_RGB_COLORS
);
355 ReleaseDC32( 0, hdc
);
361 /**********************************************************************
362 * LoadBitmap32A (USER32.356)
364 HBITMAP32
LoadBitmap32A( HINSTANCE32 instance
, LPCSTR name
)
367 if (!HIWORD(name
)) res
= LoadBitmap32W(instance
,(LPWSTR
)name
);
370 LPWSTR uni
= STRING32_DupAnsiToUni(name
);
371 res
= LoadBitmap32W(instance
,uni
);
378 /***********************************************************************
379 * BITMAP_DeleteObject
381 BOOL32
BITMAP_DeleteObject( HBITMAP16 hbitmap
, BITMAPOBJ
* bitmap
)
383 XFreePixmap( display
, bitmap
->pixmap
);
384 return GDI_FreeObject( hbitmap
);
388 /***********************************************************************
391 INT16
BITMAP_GetObject16( BITMAPOBJ
* bmp
, INT16 count
, LPVOID buffer
)
393 if (count
> sizeof(bmp
->bitmap
)) count
= sizeof(bmp
->bitmap
);
394 memcpy( buffer
, &bmp
->bitmap
, count
);
399 /***********************************************************************
402 INT32
BITMAP_GetObject32( BITMAPOBJ
* bmp
, INT32 count
, LPVOID buffer
)
405 bmp32
.bmType
= bmp
->bitmap
.bmType
;
406 bmp32
.bmWidth
= bmp
->bitmap
.bmWidth
;
407 bmp32
.bmHeight
= bmp
->bitmap
.bmHeight
;
408 bmp32
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
409 bmp32
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
410 bmp32
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
412 if (count
> sizeof(bmp32
)) count
= sizeof(bmp32
);
413 memcpy( buffer
, &bmp32
, count
);
418 /***********************************************************************
419 * BITMAP_SelectObject
421 HBITMAP16
BITMAP_SelectObject( DC
* dc
, HBITMAP16 hbitmap
,
425 HBITMAP prevHandle
= dc
->w
.hBitmap
;
427 if (!(dc
->w
.flags
& DC_MEMORY
)) return 0;
430 SetRectRgn(dc
->w
.hVisRgn
, 0, 0, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
);
433 hrgn
= CreateRectRgn( 0, 0, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
);
435 dc
->w
.hVisRgn
= hrgn
;
438 dc
->u
.x
.drawable
= bmp
->pixmap
;
439 dc
->w
.hBitmap
= hbitmap
;
441 /* Change GC depth if needed */
443 if (dc
->w
.bitsPerPixel
!= bmp
->bitmap
.bmBitsPixel
)
445 XFreeGC( display
, dc
->u
.x
.gc
);
446 dc
->u
.x
.gc
= XCreateGC( display
, dc
->u
.x
.drawable
, 0, NULL
);
447 dc
->w
.bitsPerPixel
= bmp
->bitmap
.bmBitsPixel
;
450 else CLIPPING_UpdateGCRegion( dc
); /* Just update GC clip region */
454 /***********************************************************************
455 * CreateDiscardableBitmap (GDI.156) (GDI32.38)
457 HBITMAP16
CreateDiscardableBitmap( HDC32 hdc
, INT32 width
, INT32 height
)
459 dprintf_bitmap(stddeb
,"CreateDiscardableBitmap(%04x, %d, %d); "
460 "// call CreateCompatibleBitmap() for now!\n",
462 return CreateCompatibleBitmap(hdc
, width
, height
);
466 /***********************************************************************
467 * GetBitmapDimensionEx16 (GDI.468)
469 BOOL16
GetBitmapDimensionEx16( HBITMAP16 hbitmap
, LPSIZE16 size
)
471 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
472 if (!bmp
) return FALSE
;
478 /***********************************************************************
479 * GetBitmapDimensionEx32 (GDI32.144)
481 BOOL32
GetBitmapDimensionEx32( HBITMAP32 hbitmap
, LPSIZE32 size
)
483 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
484 if (!bmp
) return FALSE
;
485 size
->cx
= (INT32
)bmp
->size
.cx
;
486 size
->cy
= (INT32
)bmp
->size
.cy
;
491 /***********************************************************************
492 * GetBitmapDimension (GDI.162)
494 DWORD
GetBitmapDimension( HBITMAP16 hbitmap
)
497 if (!GetBitmapDimensionEx16( hbitmap
, &size
)) return 0;
498 return MAKELONG( size
.cx
, size
.cy
);
502 /***********************************************************************
503 * SetBitmapDimensionEx16 (GDI.478)
505 BOOL16
SetBitmapDimensionEx16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
,
508 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
509 if (!bmp
) return FALSE
;
510 if (prevSize
) *prevSize
= bmp
->size
;
517 /***********************************************************************
518 * SetBitmapDimensionEx32 (GDI32.304)
520 BOOL32
SetBitmapDimensionEx32( HBITMAP32 hbitmap
, INT32 x
, INT32 y
,
523 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
524 if (!bmp
) return FALSE
;
525 if (prevSize
) CONV_SIZE16TO32( &bmp
->size
, prevSize
);
526 bmp
->size
.cx
= (INT16
)x
;
527 bmp
->size
.cy
= (INT16
)y
;
532 /***********************************************************************
533 * SetBitmapDimension (GDI.163)
535 DWORD
SetBitmapDimension( HBITMAP16 hbitmap
, INT16 x
, INT16 y
)
538 if (!SetBitmapDimensionEx16( hbitmap
, x
, y
, &size
)) return 0;
539 return MAKELONG( size
.cx
, size
.cy
);