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 BOOL
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 * LoadBitmap16 (USER.175)
267 HBITMAP16
LoadBitmap16( HINSTANCE16 instance
, SEGPTR name
)
269 HBITMAP16 hbitmap
= 0;
277 char *str
= (char *)PTR_SEG_TO_LIN( name
);
278 dprintf_bitmap( stddeb
, "LoadBitmap16(%04x,'%s')\n", instance
, str
);
279 if (str
[0] == '#') name
= (SEGPTR
)(DWORD
)(WORD
)atoi( str
+ 1 );
282 dprintf_bitmap( stddeb
, "LoadBitmap16(%04x,%04x)\n",
283 instance
, LOWORD(name
) );
285 if (!instance
) /* OEM bitmap */
287 if (HIWORD((int)name
)) return 0;
288 return OBM_LoadBitmap( LOWORD((int)name
) );
291 if (!(hRsrc
= FindResource16( instance
, name
, RT_BITMAP
))) return 0;
292 if (!(handle
= LoadResource16( instance
, hRsrc
))) return 0;
294 info
= (BITMAPINFO
*)LockResource16( handle
);
295 if ((hdc
= GetDC(0)) != 0)
297 char *bits
= (char *)info
+ DIB_BitmapInfoSize( info
, DIB_RGB_COLORS
);
298 hbitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
299 bits
, info
, DIB_RGB_COLORS
);
302 FreeResource16( handle
);
306 /**********************************************************************
307 * LoadBitmap32W (USER32.357)
309 HBITMAP32
LoadBitmap32W( HINSTANCE32 instance
, LPCWSTR name
)
311 HBITMAP32 hbitmap
= 0;
317 if (!instance
) /* OEM bitmap */
319 if (HIWORD((int)name
)) return 0;
320 return OBM_LoadBitmap( LOWORD((int)name
) );
323 if (!(hRsrc
= FindResource32W( instance
, name
,
324 (LPWSTR
)RT_BITMAP
))) return 0;
325 if (!(handle
= LoadResource32( instance
, hRsrc
))) return 0;
327 info
= (BITMAPINFO
*)LockResource32( handle
);
328 if ((hdc
= GetDC(0)) != 0)
330 char *bits
= (char *)info
+ DIB_BitmapInfoSize( info
, DIB_RGB_COLORS
);
331 hbitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
332 bits
, info
, DIB_RGB_COLORS
);
339 /**********************************************************************
340 * LoadBitmap32A (USER32.356)
342 HBITMAP32
LoadBitmap32A( HINSTANCE32 instance
, LPCSTR name
)
345 if (!HIWORD(name
)) res
= LoadBitmap32W(instance
,(LPWSTR
)name
);
348 LPWSTR uni
= STRING32_DupAnsiToUni(name
);
349 res
= LoadBitmap32W(instance
,uni
);
356 /***********************************************************************
357 * BITMAP_DeleteObject
359 BOOL
BITMAP_DeleteObject( HBITMAP hbitmap
, BITMAPOBJ
* bitmap
)
361 XFreePixmap( display
, bitmap
->pixmap
);
362 return GDI_FreeObject( hbitmap
);
366 /***********************************************************************
369 INT16
BITMAP_GetObject16( BITMAPOBJ
* bmp
, INT16 count
, LPVOID buffer
)
371 if (count
> sizeof(bmp
->bitmap
)) count
= sizeof(bmp
->bitmap
);
372 memcpy( buffer
, &bmp
->bitmap
, count
);
377 /***********************************************************************
380 INT32
BITMAP_GetObject32( BITMAPOBJ
* bmp
, INT32 count
, LPVOID buffer
)
383 bmp32
.bmType
= bmp
->bitmap
.bmType
;
384 bmp32
.bmWidth
= bmp
->bitmap
.bmWidth
;
385 bmp32
.bmHeight
= bmp
->bitmap
.bmHeight
;
386 bmp32
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
387 bmp32
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
388 bmp32
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
390 if (count
> sizeof(bmp32
)) count
= sizeof(bmp32
);
391 memcpy( buffer
, &bmp32
, count
);
396 /***********************************************************************
397 * BITMAP_SelectObject
399 HBITMAP
BITMAP_SelectObject( DC
* dc
, HBITMAP hbitmap
,
403 HBITMAP prevHandle
= dc
->w
.hBitmap
;
405 if (!(dc
->w
.flags
& DC_MEMORY
)) return 0;
406 hrgn
= CreateRectRgn( 0, 0, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
);
409 DeleteObject( dc
->w
.hVisRgn
);
410 dc
->w
.hVisRgn
= hrgn
;
411 dc
->u
.x
.drawable
= bmp
->pixmap
;
412 dc
->w
.hBitmap
= hbitmap
;
414 /* Change GC depth if needed */
416 if (dc
->w
.bitsPerPixel
!= bmp
->bitmap
.bmBitsPixel
)
418 XFreeGC( display
, dc
->u
.x
.gc
);
419 dc
->u
.x
.gc
= XCreateGC( display
, dc
->u
.x
.drawable
, 0, NULL
);
420 dc
->w
.bitsPerPixel
= bmp
->bitmap
.bmBitsPixel
;
423 else CLIPPING_UpdateGCRegion( dc
); /* Just update GC clip region */
427 /***********************************************************************
428 * CreateDiscardableBitmap (GDI.156) (GDI32.38)
430 HBITMAP16
CreateDiscardableBitmap( HDC32 hdc
, INT32 width
, INT32 height
)
432 dprintf_bitmap(stddeb
,"CreateDiscardableBitmap(%04x, %d, %d); "
433 "// call CreateCompatibleBitmap() for now!\n",
435 return CreateCompatibleBitmap(hdc
, width
, height
);
439 /***********************************************************************
440 * GetBitmapDimensionEx16 (GDI.468)
442 BOOL16
GetBitmapDimensionEx16( HBITMAP16 hbitmap
, LPSIZE16 size
)
444 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
445 if (!bmp
) return FALSE
;
451 /***********************************************************************
452 * GetBitmapDimensionEx32 (GDI32.144)
454 BOOL32
GetBitmapDimensionEx32( HBITMAP32 hbitmap
, LPSIZE32 size
)
456 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
457 if (!bmp
) return FALSE
;
458 size
->cx
= (INT32
)bmp
->size
.cx
;
459 size
->cy
= (INT32
)bmp
->size
.cy
;
464 /***********************************************************************
465 * GetBitmapDimension (GDI.162)
467 DWORD
GetBitmapDimension( HBITMAP16 hbitmap
)
470 if (!GetBitmapDimensionEx16( hbitmap
, &size
)) return 0;
471 return MAKELONG( size
.cx
, size
.cy
);
475 /***********************************************************************
476 * SetBitmapDimensionEx16 (GDI.478)
478 BOOL16
SetBitmapDimensionEx16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
,
481 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
482 if (!bmp
) return FALSE
;
483 if (prevSize
) *prevSize
= bmp
->size
;
490 /***********************************************************************
491 * SetBitmapDimensionEx32 (GDI32.304)
493 BOOL32
SetBitmapDimensionEx32( HBITMAP32 hbitmap
, INT32 x
, INT32 y
,
496 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
497 if (!bmp
) return FALSE
;
498 if (prevSize
) CONV_SIZE16TO32( &bmp
->size
, prevSize
);
499 bmp
->size
.cx
= (INT16
)x
;
500 bmp
->size
.cy
= (INT16
)y
;
505 /***********************************************************************
506 * SetBitmapDimension (GDI.163)
508 DWORD
SetBitmapDimension( HBITMAP16 hbitmap
, INT16 x
, INT16 y
)
511 if (!SetBitmapDimensionEx16( hbitmap
, x
, y
, &size
)) return 0;
512 return MAKELONG( size
.cx
, size
.cy
);