Release 940912
[wine/multimedia.git] / objects / bitmap.c
blob93b0e4ac723b54994f6999c57cd6d887c40c6f76
1 /*
2 * GDI bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
9 #include <stdlib.h>
10 #include <X11/Xlib.h>
11 #include <X11/Xutil.h>
12 #include "gdi.h"
13 #include "bitmap.h"
15 /* Include OEM bitmaps */
16 #include "bitmaps/check_boxes"
17 #include "bitmaps/check_mark"
18 #include "bitmaps/menu_arrow"
20 /* Handle of the bitmap selected by default in a memory DC */
21 HBITMAP BITMAP_hbitmapMemDC = 0;
23 /* GCs used for B&W and color bitmap operations */
24 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
27 /***********************************************************************
28 * BITMAP_Init
30 BOOL BITMAP_Init()
32 Pixmap tmpPixmap;
34 /* Create the necessary GCs */
36 if ((tmpPixmap = XCreatePixmap( display, rootWindow, 1, 1, 1 )))
38 BITMAP_monoGC = XCreateGC( display, tmpPixmap, 0, NULL );
39 XSetGraphicsExposures( display, BITMAP_monoGC, False );
40 XFreePixmap( display, tmpPixmap );
43 if (screenDepth != 1)
45 if ((tmpPixmap = XCreatePixmap(display, rootWindow, 1,1,screenDepth)))
47 BITMAP_colorGC = XCreateGC( display, tmpPixmap, 0, NULL );
48 XSetGraphicsExposures( display, BITMAP_colorGC, False );
49 XFreePixmap( display, tmpPixmap );
53 BITMAP_hbitmapMemDC = CreateBitmap( 1, 1, 1, 1, NULL );
54 return (BITMAP_hbitmapMemDC != 0);
58 /***********************************************************************
59 * BITMAP_BmpToImage
61 * Create an XImage pointing to the bitmap data.
63 static XImage *BITMAP_BmpToImage( BITMAP * bmp, void * bmpData )
65 extern void _XInitImageFuncPtrs( XImage* );
66 XImage * image;
68 image = XCreateImage( display, DefaultVisualOfScreen(screen),
69 bmp->bmBitsPixel, ZPixmap, 0, bmpData,
70 bmp->bmWidth, bmp->bmHeight, 16, bmp->bmWidthBytes );
71 if (!image) return 0;
72 image->byte_order = MSBFirst;
73 image->bitmap_bit_order = MSBFirst;
74 image->bitmap_unit = 16;
75 _XInitImageFuncPtrs(image);
76 return image;
80 /***********************************************************************
81 * BITMAP_LoadOEMBitmap
83 HBITMAP BITMAP_LoadOEMBitmap( WORD id )
85 BITMAPOBJ * bmpObjPtr;
86 HBITMAP hbitmap;
87 WORD width, height;
88 char *data;
90 switch(id)
92 case OBM_MNARROW:
93 width = menu_arrow_width;
94 height = menu_arrow_height;
95 data = menu_arrow_bits;
96 break;
98 case OBM_CHECKBOXES:
99 width = check_boxes_width;
100 height = check_boxes_height;
101 data = check_boxes_bits;
102 break;
104 case OBM_CHECK:
105 width = check_mark_width;
106 height = check_mark_height;
107 data = check_mark_bits;
108 break;
110 default:
111 return 0;
114 /* Create the BITMAPOBJ */
115 if (!(hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC )))
116 return 0;
117 bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_ADDR( hbitmap );
118 bmpObjPtr->size.cx = 0;
119 bmpObjPtr->size.cy = 0;
120 bmpObjPtr->bitmap.bmType = 0;
121 bmpObjPtr->bitmap.bmWidth = width;
122 bmpObjPtr->bitmap.bmHeight = height;
123 bmpObjPtr->bitmap.bmWidthBytes = (width + 15) / 16 * 2;
124 bmpObjPtr->bitmap.bmPlanes = 1;
125 bmpObjPtr->bitmap.bmBitsPixel = 1;
126 bmpObjPtr->bitmap.bmBits = NULL;
128 /* Create the pixmap */
129 if (!(bmpObjPtr->pixmap = XCreateBitmapFromData( display, rootWindow,
130 data, width, height )))
132 GDI_HEAP_FREE( hbitmap );
133 return 0;
135 return hbitmap;
139 /***********************************************************************
140 * CreateBitmap (GDI.48)
142 HBITMAP CreateBitmap( short width, short height,
143 BYTE planes, BYTE bpp, LPSTR bits )
145 BITMAP bitmap = { 0, width, height, 0, planes, bpp, bits };
146 #ifdef DEBUG_GDI
147 printf( "CreateBitmap: %dx%d, %d colors\n",
148 width, height, 1 << (planes*bpp) );
149 #endif
150 return CreateBitmapIndirect( &bitmap );
154 /***********************************************************************
155 * CreateCompatibleBitmap (GDI.51)
157 HBITMAP CreateCompatibleBitmap( HDC hdc, short width, short height )
159 DC * dc;
160 #ifdef DEBUG_GDI
161 printf( "CreateCompatibleBitmap: %d %dx%d\n", hdc, width, height );
162 #endif
163 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
164 return CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
168 /***********************************************************************
169 * CreateBitmapIndirect (GDI.49)
171 HBITMAP CreateBitmapIndirect( BITMAP * bmp )
173 BITMAPOBJ * bmpObjPtr;
174 HBITMAP hbitmap;
176 /* Check parameters */
177 if (!bmp->bmHeight || !bmp->bmWidth) return 0;
178 if (bmp->bmPlanes != 1) return 0;
179 if ((bmp->bmBitsPixel != 1) && (bmp->bmBitsPixel != screenDepth)) return 0;
181 if (bmp->bmHeight < 0)
182 bmp->bmHeight = -bmp->bmHeight;
184 if (bmp->bmWidth < 0)
185 bmp->bmWidth = -bmp->bmWidth;
188 /* Create the BITMAPOBJ */
189 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
190 if (!hbitmap) return 0;
191 bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_ADDR( hbitmap );
193 bmpObjPtr->size.cx = 0;
194 bmpObjPtr->size.cy = 0;
195 bmpObjPtr->bitmap = *bmp;
196 bmpObjPtr->bitmap.bmBits = NULL;
197 bmpObjPtr->bitmap.bmWidthBytes = (bmp->bmWidth*bmp->bmBitsPixel+15)/16 * 2;
199 /* Create the pixmap */
200 bmpObjPtr->pixmap = XCreatePixmap( display, rootWindow, bmp->bmWidth,
201 bmp->bmHeight, bmp->bmBitsPixel );
202 if (!bmpObjPtr->pixmap)
204 GDI_HEAP_FREE( hbitmap );
205 hbitmap = 0;
207 else if (bmp->bmBits) /* Set bitmap bits */
208 SetBitmapBits( hbitmap, bmp->bmHeight*bmp->bmWidthBytes, bmp->bmBits );
209 return hbitmap;
213 /***********************************************************************
214 * GetBitmapBits (GDI.74)
216 LONG GetBitmapBits( HBITMAP hbitmap, LONG count, LPSTR buffer )
218 BITMAPOBJ * bmp;
219 LONG height;
220 XImage * image;
222 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
223 if (!bmp) return 0;
225 #ifdef DEBUG_BITMAP
226 printf( "GetBitmapBits: %dx%d %d colors %p\n",
227 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
228 1 << bmp->bitmap.bmBitsPixel, buffer );
229 #endif
230 /* Only get entire lines */
231 height = count / bmp->bitmap.bmWidthBytes;
232 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
233 if (!height) return 0;
235 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
236 XGetSubImage( display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth, height,
237 AllPlanes, ZPixmap, image, 0, 0 );
238 image->data = NULL;
239 XDestroyImage( image );
240 return height * bmp->bitmap.bmWidthBytes;
244 /***********************************************************************
245 * SetBitmapBits (GDI.106)
247 LONG SetBitmapBits( HBITMAP hbitmap, LONG count, LPSTR buffer )
249 BITMAPOBJ * bmp;
250 LONG height;
251 XImage * image;
253 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
254 if (!bmp) return 0;
256 #ifdef DEBUG_BITMAP
257 printf( "SetBitmapBits: %dx%d %d colors %p\n",
258 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
259 1 << bmp->bitmap.bmBitsPixel, buffer );
260 #endif
261 /* Only set entire lines */
262 height = count / bmp->bitmap.bmWidthBytes;
263 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
264 if (!height) return 0;
266 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
267 XPutImage( display, bmp->pixmap, BITMAP_GC(bmp), image, 0, 0,
268 0, 0, bmp->bitmap.bmWidth, height );
269 image->data = NULL;
270 XDestroyImage( image );
271 return height * bmp->bitmap.bmWidthBytes;
275 /***********************************************************************
276 * BMP_DeleteObject
278 BOOL BMP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bitmap )
280 XFreePixmap( display, bitmap->pixmap );
281 return GDI_FreeObject( hbitmap );
285 /***********************************************************************
286 * BMP_GetObject
288 int BMP_GetObject( BITMAPOBJ * bmp, int count, LPSTR buffer )
290 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
291 memcpy( buffer, &bmp->bitmap, count );
292 return count;
296 /***********************************************************************
297 * BITMAP_SelectObject
299 HBITMAP BITMAP_SelectObject( HDC hdc, DC * dc, HBITMAP hbitmap,
300 BITMAPOBJ * bmp )
302 HBITMAP prevHandle = dc->w.hBitmap;
304 if (!(dc->w.flags & DC_MEMORY)) return 0;
305 dc->u.x.drawable = bmp->pixmap;
306 dc->w.DCSizeX = bmp->bitmap.bmWidth;
307 dc->w.DCSizeY = bmp->bitmap.bmHeight;
308 dc->w.hBitmap = hbitmap;
310 /* Change GC depth if needed */
312 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
314 XFreeGC( display, dc->u.x.gc );
315 dc->u.x.gc = XCreateGC( display, dc->u.x.drawable, 0, NULL );
316 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
317 /* Re-select objects with changed depth */
318 SelectObject( hdc, dc->w.hPen );
319 SelectObject( hdc, dc->w.hBrush );
321 return prevHandle;
324 /***********************************************************************
325 * CreateDiscardableBitmap (GDI.156)
327 HBITMAP CreateDiscardableBitmap(HDC hdc, short width, short height)
329 printf("CreateDiscardableBitmap(%04X, %d, %d); "
330 "// call CreateCompatibleBitmap() for now!\n",
331 hdc, width, height);
332 return CreateCompatibleBitmap(hdc, width, height);
335 /***********************************************************************
336 * GetBitmapDimensionEx (GDI.468)
338 BOOL GetBitmapDimensionEx( HBITMAP hbitmap, LPSIZE size )
340 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
341 if (!bmp) return FALSE;
342 *size = bmp->size;
343 return TRUE;
347 /***********************************************************************
348 * GetBitmapDimension (GDI.162)
350 DWORD GetBitmapDimension( HBITMAP hbitmap )
352 SIZE size;
353 if (!GetBitmapDimensionEx( hbitmap, &size )) return 0;
354 return size.cx | (size.cy << 16);
357 /***********************************************************************
358 * SetBitmapDimensionEx (GDI.478)
360 BOOL SetBitmapDimensionEx( HBITMAP hbitmap, short x, short y, LPSIZE prevSize )
362 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
363 if (!bmp) return FALSE;
364 if (prevSize) *prevSize = bmp->size;
365 bmp->size.cx = x;
366 bmp->size.cy = y;
367 return TRUE;
371 /***********************************************************************
372 * SetBitmapDimension (GDI.163)
374 DWORD SetBitmapDimension( HBITMAP hbitmap, short x, short y )
376 SIZE size;
377 if (!SetBitmapDimensionEx( hbitmap, x, y, &size )) return 0;
378 return size.cx | (size.cy << 16);