4 * Copyright 1993 Alexandre Julliard
7 static char Copyright
[] = "Copyright Alexandre Julliard, 1993";
11 #include <X11/Xutil.h>
16 /* Handle of the bitmap selected by default in a memory DC */
17 HBITMAP BITMAP_hbitmapMemDC
= 0;
19 /* GCs used for B&W and color bitmap operations */
20 GC BITMAP_monoGC
= 0, BITMAP_colorGC
= 0;
23 /***********************************************************************
30 /* Create the necessary GCs */
32 if ((tmpPixmap
= XCreatePixmap( display
, rootWindow
, 1, 1, 1 )))
34 BITMAP_monoGC
= XCreateGC( display
, tmpPixmap
, 0, NULL
);
35 XSetGraphicsExposures( display
, BITMAP_monoGC
, False
);
36 XFreePixmap( display
, tmpPixmap
);
41 if ((tmpPixmap
= XCreatePixmap(display
, rootWindow
, 1,1,screenDepth
)))
43 BITMAP_colorGC
= XCreateGC( display
, tmpPixmap
, 0, NULL
);
44 XSetGraphicsExposures( display
, BITMAP_colorGC
, False
);
45 XFreePixmap( display
, tmpPixmap
);
49 BITMAP_hbitmapMemDC
= CreateBitmap( 1, 1, 1, 1, NULL
);
50 return (BITMAP_hbitmapMemDC
!= 0);
54 /***********************************************************************
57 * Create an XImage pointing to the bitmap data.
59 static XImage
*BITMAP_BmpToImage( BITMAP
* bmp
, void * bmpData
)
61 extern void _XInitImageFuncPtrs( XImage
* );
64 image
= XCreateImage( XT_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)
79 HBITMAP
CreateBitmap( short width
, short height
,
80 BYTE planes
, BYTE bpp
, LPSTR bits
)
82 BITMAP bitmap
= { 0, width
, height
, 0, planes
, bpp
, bits
};
84 printf( "CreateBitmap: %dx%d, %d colors\n",
85 width
, height
, 1 << (planes
*bpp
) );
87 return CreateBitmapIndirect( &bitmap
);
91 /***********************************************************************
92 * CreateCompatibleBitmap (GDI.51)
94 HBITMAP
CreateCompatibleBitmap( HDC hdc
, short width
, short height
)
98 printf( "CreateCompatibleBitmap: %d %dx%d\n", hdc
, width
, height
);
100 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
101 return CreateBitmap( width
, height
, 1, dc
->w
.bitsPerPixel
, NULL
);
105 /***********************************************************************
106 * CreateBitmapIndirect (GDI.49)
108 HBITMAP
CreateBitmapIndirect( BITMAP
* bmp
)
110 BITMAPOBJ
* bmpObjPtr
;
113 /* Check parameters */
114 if (!bmp
->bmHeight
|| !bmp
->bmWidth
) return 0;
115 if (bmp
->bmPlanes
!= 1) return 0;
116 if ((bmp
->bmBitsPixel
!= 1) && (bmp
->bmBitsPixel
!= screenDepth
)) return 0;
118 if (bmp
->bmHeight
< 0)
119 bmp
->bmHeight
= -bmp
->bmHeight
;
121 if (bmp
->bmWidth
< 0)
122 bmp
->bmWidth
= -bmp
->bmWidth
;
125 /* Create the BITMAPOBJ */
126 hbitmap
= GDI_AllocObject( sizeof(BITMAPOBJ
), BITMAP_MAGIC
);
127 if (!hbitmap
) return 0;
128 bmpObjPtr
= (BITMAPOBJ
*) GDI_HEAP_ADDR( hbitmap
);
130 bmpObjPtr
->size
.cx
= 0;
131 bmpObjPtr
->size
.cy
= 0;
132 bmpObjPtr
->bitmap
= *bmp
;
133 bmpObjPtr
->bitmap
.bmBits
= NULL
;
134 bmpObjPtr
->bitmap
.bmWidthBytes
= (bmp
->bmWidth
*bmp
->bmBitsPixel
+15)/16 * 2;
136 /* Create the pixmap */
137 bmpObjPtr
->pixmap
= XCreatePixmap( display
, rootWindow
, bmp
->bmWidth
,
138 bmp
->bmHeight
, bmp
->bmBitsPixel
);
139 if (!bmpObjPtr
->pixmap
)
141 GDI_HEAP_FREE( hbitmap
);
144 else if (bmp
->bmBits
) /* Set bitmap bits */
145 SetBitmapBits( hbitmap
, bmp
->bmHeight
*bmp
->bmWidthBytes
, bmp
->bmBits
);
150 /***********************************************************************
151 * GetBitmapBits (GDI.74)
153 LONG
GetBitmapBits( HBITMAP hbitmap
, LONG count
, LPSTR buffer
)
159 bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
163 printf( "GetBitmapBits: %dx%d %d colors %p\n",
164 bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
165 1 << bmp
->bitmap
.bmBitsPixel
, buffer
);
167 /* Only get entire lines */
168 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
169 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
170 if (!height
) return 0;
172 if (!(image
= BITMAP_BmpToImage( &bmp
->bitmap
, buffer
))) return 0;
173 XGetSubImage( display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
, height
,
174 AllPlanes
, ZPixmap
, image
, 0, 0 );
176 XDestroyImage( image
);
177 return height
* bmp
->bitmap
.bmWidthBytes
;
181 /***********************************************************************
182 * SetBitmapBits (GDI.106)
184 LONG
SetBitmapBits( HBITMAP hbitmap
, LONG count
, LPSTR buffer
)
190 bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
194 printf( "SetBitmapBits: %dx%d %d colors %p\n",
195 bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
196 1 << bmp
->bitmap
.bmBitsPixel
, buffer
);
198 /* Only set entire lines */
199 height
= count
/ bmp
->bitmap
.bmWidthBytes
;
200 if (height
> bmp
->bitmap
.bmHeight
) height
= bmp
->bitmap
.bmHeight
;
201 if (!height
) return 0;
203 if (!(image
= BITMAP_BmpToImage( &bmp
->bitmap
, buffer
))) return 0;
204 XPutImage( display
, bmp
->pixmap
, BITMAP_GC(bmp
), image
, 0, 0,
205 0, 0, bmp
->bitmap
.bmWidth
, height
);
207 XDestroyImage( image
);
208 return height
* bmp
->bitmap
.bmWidthBytes
;
212 /***********************************************************************
215 BOOL
BMP_DeleteObject( HBITMAP hbitmap
, BITMAPOBJ
* bitmap
)
217 XFreePixmap( display
, bitmap
->pixmap
);
218 return GDI_FreeObject( hbitmap
);
222 /***********************************************************************
225 int BMP_GetObject( BITMAPOBJ
* bmp
, int count
, LPSTR buffer
)
227 if (count
> sizeof(BITMAP
)) count
= sizeof(BITMAP
);
228 memcpy( buffer
, &bmp
->bitmap
, count
);
233 /***********************************************************************
234 * BITMAP_SelectObject
236 HBITMAP
BITMAP_SelectObject( HDC hdc
, DC
* dc
, HBITMAP hbitmap
,
239 HBITMAP prevHandle
= dc
->w
.hBitmap
;
241 if (!(dc
->w
.flags
& DC_MEMORY
)) return 0;
242 dc
->u
.x
.drawable
= bmp
->pixmap
;
243 dc
->w
.DCSizeX
= bmp
->bitmap
.bmWidth
;
244 dc
->w
.DCSizeY
= bmp
->bitmap
.bmHeight
;
245 dc
->w
.hBitmap
= hbitmap
;
247 /* Change GC depth if needed */
249 if (dc
->w
.bitsPerPixel
!= bmp
->bitmap
.bmBitsPixel
)
251 XFreeGC( display
, dc
->u
.x
.gc
);
252 dc
->u
.x
.gc
= XCreateGC( display
, dc
->u
.x
.drawable
, 0, NULL
);
253 dc
->w
.bitsPerPixel
= bmp
->bitmap
.bmBitsPixel
;
254 /* Re-select objects with changed depth */
255 SelectObject( hdc
, dc
->w
.hPen
);
256 SelectObject( hdc
, dc
->w
.hBrush
);
261 /***********************************************************************
262 * CreateDiscardableBitmap (GDI.156)
264 HBITMAP
CreateDiscardableBitmap(HDC hdc
, short width
, short height
)
266 printf("CreateDiscardableBitmap(%04X, %d, %d); "
267 "// call CreateCompatibleBitmap() for now!\n",
269 return CreateCompatibleBitmap(hdc
, width
, height
);
272 /***********************************************************************
273 * GetBitmapDimensionEx (GDI.468)
275 BOOL
GetBitmapDimensionEx( HBITMAP hbitmap
, LPSIZE size
)
277 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
278 if (!bmp
) return FALSE
;
284 /***********************************************************************
285 * GetBitmapDimension (GDI.162)
287 DWORD
GetBitmapDimension( HBITMAP hbitmap
)
290 if (!GetBitmapDimensionEx( hbitmap
, &size
)) return 0;
291 return size
.cx
| (size
.cy
<< 16);
294 /***********************************************************************
295 * SetBitmapDimensionEx (GDI.478)
297 BOOL
SetBitmapDimensionEx( HBITMAP hbitmap
, short x
, short y
, LPSIZE prevSize
)
299 BITMAPOBJ
* bmp
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
);
300 if (!bmp
) return FALSE
;
301 if (prevSize
) *prevSize
= bmp
->size
;
308 /***********************************************************************
309 * SetBitmapDimension (GDI.163)
311 DWORD
SetBitmapDimension( HBITMAP hbitmap
, short x
, short y
)
314 if (!SetBitmapDimensionEx( hbitmap
, x
, y
, &size
)) return 0;
315 return size
.cx
| (size
.cy
<< 16);