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 /* Create the BITMAPOBJ */
119 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
120 if (!hbitmap) return 0;
121 bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_ADDR( hbitmap );
123 bmpObjPtr->size.cx = 0;
124 bmpObjPtr->size.cy = 0;
125 bmpObjPtr->bitmap = *bmp;
126 bmpObjPtr->bitmap.bmBits = NULL;
127 bmpObjPtr->bitmap.bmWidthBytes = (bmp->bmWidth*bmp->bmBitsPixel+15)/16 * 2;
129 /* Create the pixmap */
130 bmpObjPtr->pixmap = XCreatePixmap( display, rootWindow, bmp->bmWidth,
131 bmp->bmHeight, bmp->bmBitsPixel );
132 if (!bmpObjPtr->pixmap)
134 GDI_HEAP_FREE( hbitmap );
137 else if (bmp->bmBits) /* Set bitmap bits */
138 SetBitmapBits( hbitmap, bmp->bmHeight*bmp->bmWidthBytes, bmp->bmBits );
143 /***********************************************************************
144 * GetBitmapBits (GDI.74)
146 LONG GetBitmapBits( HBITMAP hbitmap, LONG count, LPSTR buffer )
152 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
156 printf( "GetBitmapBits: %dx%d %d colors %p\n",
157 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
158 1 << bmp->bitmap.bmBitsPixel, buffer );
160 /* Only get entire lines */
161 height = count / bmp->bitmap.bmWidthBytes;
162 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
163 if (!height) return 0;
165 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
166 XGetSubImage( display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth, height,
167 AllPlanes, ZPixmap, image, 0, 0 );
169 XDestroyImage( image );
170 return height * bmp->bitmap.bmWidthBytes;
174 /***********************************************************************
175 * SetBitmapBits (GDI.106)
177 LONG SetBitmapBits( HBITMAP hbitmap, LONG count, LPSTR buffer )
183 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
187 printf( "SetBitmapBits: %dx%d %d colors %p\n",
188 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
189 1 << bmp->bitmap.bmBitsPixel, buffer );
191 /* Only set entire lines */
192 height = count / bmp->bitmap.bmWidthBytes;
193 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
194 if (!height) return 0;
196 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
197 XPutImage( display, bmp->pixmap, BITMAP_GC(bmp), image, 0, 0,
198 0, 0, bmp->bitmap.bmWidth, height );
200 XDestroyImage( image );
201 return height * bmp->bitmap.bmWidthBytes;
205 /***********************************************************************
208 BOOL BMP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bitmap )
210 XFreePixmap( display, bitmap->pixmap );
211 return GDI_FreeObject( hbitmap );
215 /***********************************************************************
218 int BMP_GetObject( BITMAPOBJ * bmp, int count, LPSTR buffer )
220 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
221 memcpy( buffer, &bmp->bitmap, count );
226 /***********************************************************************
227 * BITMAP_SelectObject
229 HBITMAP BITMAP_SelectObject( HDC hdc, DC * dc, HBITMAP hbitmap,
232 HBITMAP prevHandle = dc->w.hBitmap;
234 if (!(dc->w.flags & DC_MEMORY)) return 0;
235 dc->u.x.drawable = bmp->pixmap;
236 dc->w.DCSizeX = bmp->bitmap.bmWidth;
237 dc->w.DCSizeY = bmp->bitmap.bmHeight;
238 dc->w.hBitmap = hbitmap;
240 /* Change GC depth if needed */
242 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
244 XFreeGC( display, dc->u.x.gc );
245 dc->u.x.gc = XCreateGC( display, dc->u.x.drawable, 0, NULL );
246 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
247 /* Re-select objects with changed depth */
248 SelectObject( hdc, dc->w.hPen );
249 SelectObject( hdc, dc->w.hBrush );
254 /***********************************************************************
255 * CreateDiscardableBitmap (GDI.156)
257 HBITMAP CreateDiscardableBitmap(HDC hdc, short width, short height)
259 printf("CreateDiscardableBitmap(%04X, %d, %d); "
260 "// call CreateCompatibleBitmap() for now!\n",
262 return CreateCompatibleBitmap(hdc, width, height);
265 /***********************************************************************
266 * GetBitmapDimensionEx (GDI.468)
268 BOOL GetBitmapDimensionEx( HBITMAP hbitmap, LPSIZE size )
270 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
271 if (!bmp) return FALSE;
277 /***********************************************************************
278 * GetBitmapDimension (GDI.162)
280 DWORD GetBitmapDimension( HBITMAP hbitmap )
283 if (!GetBitmapDimensionEx( hbitmap, &size )) return 0;
284 return size.cx | (size.cy << 16);
287 /***********************************************************************
288 * SetBitmapDimensionEx (GDI.478)
290 BOOL SetBitmapDimensionEx( HBITMAP hbitmap, short x, short y, LPSIZE prevSize )
292 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
293 if (!bmp) return FALSE;
294 if (prevSize) *prevSize = bmp->size;
301 /***********************************************************************
302 * SetBitmapDimension (GDI.163)
304 DWORD SetBitmapDimension( HBITMAP hbitmap, short x, short y )
307 if (!SetBitmapDimensionEx( hbitmap, x, y, &size )) return 0;
308 return size.cx | (size.cy << 16);