Release 951003
[wine] / objects / bitmap.c
1 /*
2  * GDI bitmap objects
3  *
4  * Copyright 1993 Alexandre Julliard
5  */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <X11/Xlib.h>
9 #include <X11/Xutil.h>
10 #include "gdi.h"
11 #include "callback.h"
12 #include "dc.h"
13 #include "bitmap.h"
14 #include "stddebug.h"
15 /* #define DEBUG_GDI    */
16 /* #define DEBUG_BITMAP */
17 #include "debug.h"
18
19   /* GCs used for B&W and color bitmap operations */
20 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
21
22 extern void CLIPPING_UpdateGCRegion( DC * dc );  /* objects/clipping.c */
23
24 /***********************************************************************
25  *           BITMAP_Init
26  */
27 BOOL BITMAP_Init(void)
28 {
29     Pixmap tmpPixmap;
30     
31       /* Create the necessary GCs */
32     
33     if ((tmpPixmap = XCreatePixmap( display, rootWindow, 1, 1, 1 )))
34     {
35         BITMAP_monoGC = XCreateGC( display, tmpPixmap, 0, NULL );
36         XSetGraphicsExposures( display, BITMAP_monoGC, False );
37         XFreePixmap( display, tmpPixmap );
38     }
39
40     if (screenDepth != 1)
41     {
42         if ((tmpPixmap = XCreatePixmap(display, rootWindow, 1,1,screenDepth)))
43         {
44             BITMAP_colorGC = XCreateGC( display, tmpPixmap, 0, NULL );
45             XSetGraphicsExposures( display, BITMAP_colorGC, False );
46             XFreePixmap( display, tmpPixmap );
47         }
48     }
49     return TRUE;
50 }
51
52
53 /***********************************************************************
54  *           BITMAP_BmpToImage
55  *
56  * Create an XImage pointing to the bitmap data.
57  */
58 static XImage *BITMAP_BmpToImage( BITMAP * bmp, void * bmpData )
59 {
60     extern void _XInitImageFuncPtrs( XImage* );
61     XImage * image;
62
63     image = XCreateImage( display, DefaultVisualOfScreen(screen),
64                           bmp->bmBitsPixel, ZPixmap, 0, bmpData,
65                           bmp->bmWidth, bmp->bmHeight, 16, bmp->bmWidthBytes );
66     if (!image) return 0;
67     image->byte_order = MSBFirst;
68     image->bitmap_bit_order = MSBFirst;
69     image->bitmap_unit = 16;
70     _XInitImageFuncPtrs(image);
71     return image;
72 }
73
74
75 /***********************************************************************
76  *           CreateBitmap    (GDI.48)
77  */
78 HBITMAP CreateBitmap( short width, short height, 
79                       BYTE planes, BYTE bpp, LPSTR bits )
80 {
81     BITMAPOBJ * bmpObjPtr;
82     HBITMAP hbitmap;
83
84     dprintf_gdi( stddeb, "CreateBitmap: %dx%d, %d colors\n", 
85                  width, height, 1 << (planes*bpp) );
86
87       /* Check parameters */
88     if (!height || !width || planes != 1) return 0;
89     if ((bpp != 1) && (bpp != screenDepth)) return 0;
90     if (height < 0) height = -height;
91     if (width < 0) width = -width;
92
93       /* Create the BITMAPOBJ */
94     hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
95     if (!hbitmap) return 0;
96     bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LIN_ADDR( hbitmap );
97
98     bmpObjPtr->size.cx = 0;
99     bmpObjPtr->size.cy = 0;
100     bmpObjPtr->bitmap.bmType = 0;
101     bmpObjPtr->bitmap.bmWidth = width;
102     bmpObjPtr->bitmap.bmHeight = height;
103     bmpObjPtr->bitmap.bmPlanes = planes;
104     bmpObjPtr->bitmap.bmBitsPixel = bpp;
105     bmpObjPtr->bitmap.bmWidthBytes = (width * bpp + 15) / 16 * 2;
106     bmpObjPtr->bitmap.bmBits = NULL;
107
108       /* Create the pixmap */
109     bmpObjPtr->pixmap = XCreatePixmap(display, rootWindow, width, height, bpp);
110     if (!bmpObjPtr->pixmap)
111     {
112         GDI_HEAP_FREE( hbitmap );
113         hbitmap = 0;
114     }
115     else if (bits)  /* Set bitmap bits */
116         SetBitmapBits( hbitmap, height * bmpObjPtr->bitmap.bmWidthBytes, bits);
117     return hbitmap;
118 }
119
120
121 /***********************************************************************
122  *           CreateCompatibleBitmap    (GDI.51)
123  */
124 HBITMAP CreateCompatibleBitmap( HDC hdc, short width, short height )
125 {
126     DC * dc;
127     dprintf_gdi(stddeb, "CreateCompatibleBitmap: "NPFMT" %dx%d\n", 
128                 hdc, width, height );
129     if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
130     return CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
131 }
132
133
134 /***********************************************************************
135  *           CreateBitmapIndirect    (GDI.49)
136  */
137 HBITMAP CreateBitmapIndirect( BITMAP * bmp )
138 {
139     return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
140                          bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
141 }
142
143
144 /***********************************************************************
145  *           GetBitmapBits    (GDI.74)
146  */
147 LONG GetBitmapBits( HBITMAP hbitmap, LONG count, LPSTR buffer )
148 {
149     BITMAPOBJ * bmp;
150     LONG height;
151     XImage * image;
152     
153     bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
154     if (!bmp) return 0;
155
156     dprintf_bitmap(stddeb, "GetBitmapBits: %dx%d %d colors %p\n",
157             bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
158             1 << bmp->bitmap.bmBitsPixel, buffer );
159       /* Only get entire lines */
160     height = count / bmp->bitmap.bmWidthBytes;
161     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
162     if (!height) return 0;
163     
164     if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
165     CallTo32_LargeStack( (int(*)())XGetSubImage, 11,
166                          display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
167                          height, AllPlanes, ZPixmap, image, 0, 0 );
168     image->data = NULL;
169     XDestroyImage( image );
170     return height * bmp->bitmap.bmWidthBytes;
171 }
172
173
174 /***********************************************************************
175  *           SetBitmapBits    (GDI.106)
176  */
177 LONG SetBitmapBits( HBITMAP hbitmap, LONG count, LPSTR buffer )
178 {
179     BITMAPOBJ * bmp;
180     LONG height;
181     XImage * image;
182     
183     bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
184     if (!bmp) return 0;
185
186     dprintf_bitmap(stddeb, "SetBitmapBits: %dx%d %d colors %p\n",
187             bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
188             1 << bmp->bitmap.bmBitsPixel, buffer );
189
190       /* Only set entire lines */
191     height = count / bmp->bitmap.bmWidthBytes;
192     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
193     if (!height) return 0;
194         
195     if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
196     CallTo32_LargeStack( XPutImage, 10,
197                          display, bmp->pixmap, BITMAP_GC(bmp), image, 0, 0,
198                          0, 0, bmp->bitmap.bmWidth, height );
199     image->data = NULL;
200     XDestroyImage( image );
201     return height * bmp->bitmap.bmWidthBytes;
202 }
203
204
205 /**********************************************************************
206  *          LoadBitmap    (USER.175)
207  */
208 HBITMAP LoadBitmap( HANDLE instance, SEGPTR name )
209 {
210     HBITMAP hbitmap = 0;
211     HDC hdc;
212     HRSRC hRsrc;
213     HGLOBAL handle;
214     BITMAPINFO *info;
215
216     if (HIWORD(name))
217     {
218         char *str = (char *)PTR_SEG_TO_LIN( name );
219         dprintf_bitmap( stddeb, "LoadBitmap("NPFMT",'%s')\n", instance, str );
220         if (str[0] == '#') name = (SEGPTR)(WORD)atoi( str + 1 );
221     }
222     else
223         dprintf_bitmap( stddeb, "LoadBitmap("NPFMT",%04x)\n",
224                         instance, LOWORD(name) );
225
226     if (!instance)  /* OEM bitmap */
227     {
228         if (HIWORD((int)name)) return 0;
229         return OBM_LoadBitmap( LOWORD((int)name) );
230     }
231
232     if (!(hRsrc = FindResource( instance, name, RT_BITMAP ))) return 0;
233     if (!(handle = LoadResource( instance, hRsrc ))) return 0;
234
235     info = (BITMAPINFO *)LockResource( handle );
236     if ((hdc = GetDC(0)) != 0)
237     {
238         char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
239         hbitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
240                                   bits, info, DIB_RGB_COLORS );
241         ReleaseDC( 0, hdc );
242     }
243     FreeResource( handle );
244     return hbitmap;
245 }
246
247
248 /***********************************************************************
249  *           BITMAP_DeleteObject
250  */
251 BOOL BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bitmap )
252 {
253     XFreePixmap( display, bitmap->pixmap );
254     return GDI_FreeObject( hbitmap );
255 }
256
257         
258 /***********************************************************************
259  *           BITMAP_GetObject
260  */
261 int BITMAP_GetObject( BITMAPOBJ * bmp, int count, LPSTR buffer )
262 {
263     if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
264     memcpy( buffer, &bmp->bitmap, count );
265     return count;
266 }
267     
268
269 /***********************************************************************
270  *           BITMAP_SelectObject
271  */
272 HBITMAP BITMAP_SelectObject( HDC hdc, DC * dc, HBITMAP hbitmap,
273                              BITMAPOBJ * bmp )
274 {
275     HRGN hrgn;
276     HBITMAP prevHandle = dc->w.hBitmap;
277     
278     if (!(dc->w.flags & DC_MEMORY)) return 0;
279     hrgn = CreateRectRgn( 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
280     if (!hrgn) return 0;
281
282     DeleteObject( dc->w.hVisRgn );
283     dc->w.hVisRgn    = hrgn;
284     dc->u.x.drawable = bmp->pixmap;
285     dc->w.hBitmap    = hbitmap;
286
287       /* Change GC depth if needed */
288
289     if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
290     {
291         XFreeGC( display, dc->u.x.gc );
292         dc->u.x.gc = XCreateGC( display, dc->u.x.drawable, 0, NULL );
293         dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
294         DC_InitDC( hdc );
295     }
296     else CLIPPING_UpdateGCRegion( dc );  /* Just update GC clip region */
297     return prevHandle;
298 }
299
300 /***********************************************************************
301  *           CreateDiscardableBitmap    (GDI.156)
302  */
303 HBITMAP CreateDiscardableBitmap(HDC hdc, short width, short height)
304 {
305     dprintf_bitmap(stddeb,"CreateDiscardableBitmap("NPFMT", %d, %d); "
306            "// call CreateCompatibleBitmap() for now!\n",
307            hdc, width, height);
308     return CreateCompatibleBitmap(hdc, width, height);
309 }
310
311 /***********************************************************************
312  *           GetBitmapDimensionEx    (GDI.468)
313  */
314 BOOL GetBitmapDimensionEx( HBITMAP hbitmap, LPSIZE size )
315 {
316     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
317     if (!bmp) return FALSE;
318     *size = bmp->size;
319     return TRUE;
320 }
321
322
323 /***********************************************************************
324  *           GetBitmapDimension    (GDI.162)
325  */
326 DWORD GetBitmapDimension( HBITMAP hbitmap )
327 {
328     SIZE size;
329     if (!GetBitmapDimensionEx( hbitmap, &size )) return 0;
330     return size.cx | (size.cy << 16);
331 }
332
333 /***********************************************************************
334  *           SetBitmapDimensionEx    (GDI.478)
335  */
336 BOOL SetBitmapDimensionEx( HBITMAP hbitmap, short x, short y, LPSIZE prevSize )
337 {
338     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
339     if (!bmp) return FALSE;
340     if (prevSize) *prevSize = bmp->size;
341     bmp->size.cx = x;
342     bmp->size.cy = y;
343     return TRUE;
344 }
345
346
347 /***********************************************************************
348  *           SetBitmapDimension    (GDI.163)
349  */
350 DWORD SetBitmapDimension( HBITMAP hbitmap, short x, short y )
351 {
352     SIZE size;
353     if (!SetBitmapDimensionEx( hbitmap, x, y, &size )) return 0;
354     return size.cx | (size.cy << 16);    
355 }