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 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
134 hbmpRet
= CreateBitmap( width
, height
, 1, dc
->w
.bitsPerPixel
, NULL
);
135 dprintf_gdi(stddeb
,"\t\t%04x\n", hbmpRet
);
140 /***********************************************************************
141 * CreateBitmapIndirect16 (GDI.49)
143 HBITMAP16
CreateBitmapIndirect16( const BITMAP16
* bmp
)
145 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
146 bmp
->bmBitsPixel
, PTR_SEG_TO_LIN( bmp
->bmBits
) );
150 /***********************************************************************
151 * CreateBitmapIndirect32 (GDI32.26)
153 HBITMAP32
CreateBitmapIndirect32( const BITMAP32
* bmp
)
155 return CreateBitmap( bmp
->bmWidth
, bmp
->bmHeight
, bmp
->bmPlanes
,
156 bmp
->bmBitsPixel
, bmp
->bmBits
);
160 /***********************************************************************
161 * GetBitmapBits (GDI.74) (GDI32.143)
163 LONG
GetBitmapBits( HBITMAP32 hbitmap
, LONG count
, LPVOID buffer
)
171 fprintf(stderr
, "Negative number of bytes (%ld) passed to GetBitmapBits???\n", count
);
174 bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
177 /* Only get entire lines */
178 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
179 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
180 dprintf_bitmap(stddeb
, "GetBitmapBits: %dx%d %d colors %p fetched height: %ld\n",
181 bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
182 1 << bmp
->bitmap
.bmBitsPixel
, buffer
, height
);
183 if (!height
) return 0;
185 if (!(image
= BITMAP_BmpToImage( &bmp
->bitmap
, buffer
))) return 0;
186 CallTo32_LargeStack( (int(*)())XGetSubImage
, 11,
187 display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
,
188 height
, AllPlanes
, ZPixmap
, image
, 0, 0 );
190 XDestroyImage( image
);
191 return height
* bmp
->bitmap
.bmWidthBytes
;
195 /***********************************************************************
196 * SetBitmapBits (GDI.106) (GDI32.303)
198 LONG
SetBitmapBits( HBITMAP32 hbitmap
, LONG count
, LPCVOID buffer
)
206 fprintf(stderr
, "Negative number of bytes (%ld) passed to SetBitmapBits???\n", count
);
209 bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
212 dprintf_bitmap(stddeb
, "SetBitmapBits: %dx%d %d colors %p\n",
213 bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
214 1 << bmp
->bitmap
.bmBitsPixel
, buffer
);
216 /* Only set entire lines */
217 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
218 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
219 if (!height
) return 0;
221 if (!(image
= BITMAP_BmpToImage( &bmp
->bitmap
, (LPVOID
)buffer
))) return 0;
222 CallTo32_LargeStack( XPutImage
, 10,
223 display
, bmp
->pixmap
, BITMAP_GC(bmp
), image
, 0, 0,
224 0, 0, bmp
->bitmap
.bmWidth
, height
);
226 XDestroyImage( image
);
227 return height
* bmp
->bitmap
.bmWidthBytes
;
230 /**********************************************************************
231 * LoadImageA (USER32.364)
232 * FIXME: implementation still lacks nearly all features, see LR_*
233 * defines in windows.h
236 HANDLE32
LoadImage32A(
237 HINSTANCE32 hinst
,LPCSTR name
,UINT32 type
,INT32 desiredx
,
238 INT32 desiredy
,UINT32 loadflags
241 dprintf_resource(stddeb
,"LoadImage32A(0x%04x,%s,%d,%d,%d,0x%08x)\n",
242 hinst
,name
,type
,desiredx
,desiredy
,loadflags
245 dprintf_resource(stddeb
,"LoadImage32A(0x%04x,%p,%d,%d,%d,0x%08x)\n",
246 hinst
,name
,type
,desiredx
,desiredy
,loadflags
251 return LoadBitmap32A(hinst
,name
);
253 return LoadIcon32A(hinst
,name
);
255 return LoadCursor32A(hinst
,name
);
260 /**********************************************************************
261 * LoadBitmap16 (USER.175)
263 HBITMAP16
LoadBitmap16( HINSTANCE16 instance
, SEGPTR name
)
265 HBITMAP16 hbitmap
= 0;
273 char *str
= (char *)PTR_SEG_TO_LIN( name
);
274 dprintf_bitmap( stddeb
, "LoadBitmap16(%04x,'%s')\n", instance
, str
);
275 if (str
[0] == '#') name
= (SEGPTR
)(DWORD
)(WORD
)atoi( str
+ 1 );
278 dprintf_bitmap( stddeb
, "LoadBitmap16(%04x,%04x)\n",
279 instance
, LOWORD(name
) );
281 if (!instance
) /* OEM bitmap */
283 if (HIWORD((int)name
)) return 0;
284 return OBM_LoadBitmap( LOWORD((int)name
) );
287 if (!(hRsrc
= FindResource16( instance
, name
, RT_BITMAP
))) return 0;
288 if (!(handle
= LoadResource16( instance
, hRsrc
))) return 0;
290 info
= (BITMAPINFO
*)LockResource16( handle
);
291 if ((hdc
= GetDC(0)) != 0)
293 char *bits
= (char *)info
+ DIB_BitmapInfoSize( info
, DIB_RGB_COLORS
);
294 hbitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
295 bits
, info
, DIB_RGB_COLORS
);
298 FreeResource16( handle
);
302 /**********************************************************************
303 * LoadBitmap32W (USER32.357)
305 HBITMAP32
LoadBitmap32W( HINSTANCE32 instance
, LPCWSTR name
)
307 HBITMAP32 hbitmap
= 0;
313 if (!instance
) /* OEM bitmap */
315 if (HIWORD((int)name
)) return 0;
316 return OBM_LoadBitmap( LOWORD((int)name
) );
319 if (!(hRsrc
= FindResource32W( instance
, name
,
320 (LPWSTR
)RT_BITMAP
))) return 0;
321 if (!(handle
= LoadResource32( instance
, hRsrc
))) return 0;
323 info
= (BITMAPINFO
*)LockResource32( handle
);
324 if ((hdc
= GetDC(0)) != 0)
326 char *bits
= (char *)info
+ DIB_BitmapInfoSize( info
, DIB_RGB_COLORS
);
327 hbitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
328 bits
, info
, DIB_RGB_COLORS
);
335 /**********************************************************************
336 * LoadBitmap32A (USER32.356)
338 HBITMAP32
LoadBitmap32A( HINSTANCE32 instance
, LPCSTR name
)
341 if (!HIWORD(name
)) res
= LoadBitmap32W(instance
,(LPWSTR
)name
);
344 LPWSTR uni
= STRING32_DupAnsiToUni(name
);
345 res
= LoadBitmap32W(instance
,uni
);
352 /***********************************************************************
353 * BITMAP_DeleteObject
355 BOOL
BITMAP_DeleteObject( HBITMAP hbitmap
, BITMAPOBJ
* bitmap
)
357 XFreePixmap( display
, bitmap
->pixmap
);
358 return GDI_FreeObject( hbitmap
);
362 /***********************************************************************
365 INT16
BITMAP_GetObject16( BITMAPOBJ
* bmp
, INT16 count
, LPVOID buffer
)
367 if (count
> sizeof(bmp
->bitmap
)) count
= sizeof(bmp
->bitmap
);
368 memcpy( buffer
, &bmp
->bitmap
, count
);
373 /***********************************************************************
376 INT32
BITMAP_GetObject32( BITMAPOBJ
* bmp
, INT32 count
, LPVOID buffer
)
379 bmp32
.bmType
= bmp
->bitmap
.bmType
;
380 bmp32
.bmWidth
= bmp
->bitmap
.bmWidth
;
381 bmp32
.bmHeight
= bmp
->bitmap
.bmHeight
;
382 bmp32
.bmWidthBytes
= bmp
->bitmap
.bmWidthBytes
;
383 bmp32
.bmPlanes
= bmp
->bitmap
.bmPlanes
;
384 bmp32
.bmBitsPixel
= bmp
->bitmap
.bmBitsPixel
;
386 if (count
> sizeof(bmp32
)) count
= sizeof(bmp32
);
387 memcpy( buffer
, &bmp32
, count
);
392 /***********************************************************************
393 * BITMAP_SelectObject
395 HBITMAP
BITMAP_SelectObject( DC
* dc
, HBITMAP hbitmap
,
399 HBITMAP prevHandle
= dc
->w
.hBitmap
;
401 if (!(dc
->w
.flags
& DC_MEMORY
)) return 0;
402 hrgn
= CreateRectRgn( 0, 0, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
);
405 DeleteObject( dc
->w
.hVisRgn
);
406 dc
->w
.hVisRgn
= hrgn
;
407 dc
->u
.x
.drawable
= bmp
->pixmap
;
408 dc
->w
.hBitmap
= hbitmap
;
410 /* Change GC depth if needed */
412 if (dc
->w
.bitsPerPixel
!= bmp
->bitmap
.bmBitsPixel
)
414 XFreeGC( display
, dc
->u
.x
.gc
);
415 dc
->u
.x
.gc
= XCreateGC( display
, dc
->u
.x
.drawable
, 0, NULL
);
416 dc
->w
.bitsPerPixel
= bmp
->bitmap
.bmBitsPixel
;
419 else CLIPPING_UpdateGCRegion( dc
); /* Just update GC clip region */
423 /***********************************************************************
424 * CreateDiscardableBitmap (GDI.156) (GDI32.38)
426 HBITMAP16
CreateDiscardableBitmap( HDC32 hdc
, INT32 width
, INT32 height
)
428 dprintf_bitmap(stddeb
,"CreateDiscardableBitmap(%04x, %d, %d); "
429 "// call CreateCompatibleBitmap() for now!\n",
431 return CreateCompatibleBitmap(hdc
, width
, height
);
435 /***********************************************************************
436 * GetBitmapDimensionEx16 (GDI.468)
438 BOOL16
GetBitmapDimensionEx16( HBITMAP16 hbitmap
, LPSIZE16 size
)
440 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
441 if (!bmp
) return FALSE
;
447 /***********************************************************************
448 * GetBitmapDimensionEx32 (GDI32.144)
450 BOOL32
GetBitmapDimensionEx32( HBITMAP32 hbitmap
, LPSIZE32 size
)
452 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
453 if (!bmp
) return FALSE
;
454 size
->cx
= (INT32
)bmp
->size
.cx
;
455 size
->cy
= (INT32
)bmp
->size
.cy
;
460 /***********************************************************************
461 * GetBitmapDimension (GDI.162)
463 DWORD
GetBitmapDimension( HBITMAP16 hbitmap
)
466 if (!GetBitmapDimensionEx16( hbitmap
, &size
)) return 0;
467 return MAKELONG( size
.cx
, size
.cy
);
471 /***********************************************************************
472 * SetBitmapDimensionEx16 (GDI.478)
474 BOOL16
SetBitmapDimensionEx16( HBITMAP16 hbitmap
, INT16 x
, INT16 y
,
477 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
478 if (!bmp
) return FALSE
;
479 if (prevSize
) *prevSize
= bmp
->size
;
486 /***********************************************************************
487 * SetBitmapDimensionEx32 (GDI32.304)
489 BOOL32
SetBitmapDimensionEx32( HBITMAP32 hbitmap
, INT32 x
, INT32 y
,
492 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
493 if (!bmp
) return FALSE
;
494 if (prevSize
) CONV_SIZE16TO32( &bmp
->size
, prevSize
);
495 bmp
->size
.cx
= (INT16
)x
;
496 bmp
->size
.cy
= (INT16
)y
;
501 /***********************************************************************
502 * SetBitmapDimension (GDI.163)
504 DWORD
SetBitmapDimension( HBITMAP16 hbitmap
, INT16 x
, INT16 y
)
507 if (!SetBitmapDimensionEx16( hbitmap
, x
, y
, &size
)) return 0;
508 return MAKELONG( size
.cx
, size
.cy
);