Release 940804
[wine/gsoc-2012-control.git] / objects / bitmap.c
blobb068a2a6bebe3e000cd8661a85bc18183025449d
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"
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 /***********************************************************************
24 * BITMAP_Init
26 BOOL BITMAP_Init()
28 Pixmap tmpPixmap;
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 );
39 if (screenDepth != 1)
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 /***********************************************************************
55 * BITMAP_BmpToImage
57 * Create an XImage pointing to the bitmap data.
59 static XImage *BITMAP_BmpToImage( BITMAP * bmp, void * bmpData )
61 extern void _XInitImageFuncPtrs( XImage* );
62 XImage * image;
64 image = XCreateImage( XT_display, DefaultVisualOfScreen(screen),
65 bmp->bmBitsPixel, ZPixmap, 0, bmpData,
66 bmp->bmWidth, bmp->bmHeight, 16, bmp->bmWidthBytes );
67 if (!image) return 0;
68 image->byte_order = MSBFirst;
69 image->bitmap_bit_order = MSBFirst;
70 image->bitmap_unit = 16;
71 _XInitImageFuncPtrs(image);
72 return 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 };
83 #ifdef DEBUG_GDI
84 printf( "CreateBitmap: %dx%d, %d colors\n",
85 width, height, 1 << (planes*bpp) );
86 #endif
87 return CreateBitmapIndirect( &bitmap );
91 /***********************************************************************
92 * CreateCompatibleBitmap (GDI.51)
94 HBITMAP CreateCompatibleBitmap( HDC hdc, short width, short height )
96 DC * dc;
97 #ifdef DEBUG_GDI
98 printf( "CreateCompatibleBitmap: %d %dx%d\n", hdc, width, height );
99 #endif
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;
111 HBITMAP hbitmap;
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 );
142 hbitmap = 0;
144 else if (bmp->bmBits) /* Set bitmap bits */
145 SetBitmapBits( hbitmap, bmp->bmHeight*bmp->bmWidthBytes, bmp->bmBits );
146 return hbitmap;
150 /***********************************************************************
151 * GetBitmapBits (GDI.74)
153 LONG GetBitmapBits( HBITMAP hbitmap, LONG count, LPSTR buffer )
155 BITMAPOBJ * bmp;
156 LONG height;
157 XImage * image;
159 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
160 if (!bmp) return 0;
162 #ifdef DEBUG_BITMAP
163 printf( "GetBitmapBits: %dx%d %d colors %p\n",
164 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
165 1 << bmp->bitmap.bmBitsPixel, buffer );
166 #endif
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 );
175 image->data = NULL;
176 XDestroyImage( image );
177 return height * bmp->bitmap.bmWidthBytes;
181 /***********************************************************************
182 * SetBitmapBits (GDI.106)
184 LONG SetBitmapBits( HBITMAP hbitmap, LONG count, LPSTR buffer )
186 BITMAPOBJ * bmp;
187 LONG height;
188 XImage * image;
190 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
191 if (!bmp) return 0;
193 #ifdef DEBUG_BITMAP
194 printf( "SetBitmapBits: %dx%d %d colors %p\n",
195 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
196 1 << bmp->bitmap.bmBitsPixel, buffer );
197 #endif
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 );
206 image->data = NULL;
207 XDestroyImage( image );
208 return height * bmp->bitmap.bmWidthBytes;
212 /***********************************************************************
213 * BMP_DeleteObject
215 BOOL BMP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bitmap )
217 XFreePixmap( display, bitmap->pixmap );
218 return GDI_FreeObject( hbitmap );
222 /***********************************************************************
223 * BMP_GetObject
225 int BMP_GetObject( BITMAPOBJ * bmp, int count, LPSTR buffer )
227 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
228 memcpy( buffer, &bmp->bitmap, count );
229 return count;
233 /***********************************************************************
234 * BITMAP_SelectObject
236 HBITMAP BITMAP_SelectObject( HDC hdc, DC * dc, HBITMAP hbitmap,
237 BITMAPOBJ * bmp )
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 );
258 return prevHandle;
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",
268 hdc, width, height);
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;
279 *size = bmp->size;
280 return TRUE;
284 /***********************************************************************
285 * GetBitmapDimension (GDI.162)
287 DWORD GetBitmapDimension( HBITMAP hbitmap )
289 SIZE size;
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;
302 bmp->size.cx = x;
303 bmp->size.cy = y;
304 return TRUE;
308 /***********************************************************************
309 * SetBitmapDimension (GDI.163)
311 DWORD SetBitmapDimension( HBITMAP hbitmap, short x, short y )
313 SIZE size;
314 if (!SetBitmapDimensionEx( hbitmap, x, y, &size )) return 0;
315 return size.cx | (size.cy << 16);