Release 960521
[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( INT width, INT height, UINT planes, UINT bpp, LPVOID bits )
79 {
80     BITMAPOBJ * bmpObjPtr;
81     HBITMAP hbitmap;
82
83     dprintf_gdi( stddeb, "CreateBitmap: %dx%d, %d colors\n", 
84                  width, height, 1 << (planes*bpp) );
85
86       /* Check parameters */
87     if (!height || !width || planes != 1) return 0;
88     if ((bpp != 1) && (bpp != screenDepth)) return 0;
89     if (height < 0) height = -height;
90     if (width < 0) width = -width;
91
92       /* Create the BITMAPOBJ */
93     hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
94     if (!hbitmap) return 0;
95     bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LIN_ADDR( hbitmap );
96
97     bmpObjPtr->size.cx = 0;
98     bmpObjPtr->size.cy = 0;
99     bmpObjPtr->bitmap.bmType = 0;
100     bmpObjPtr->bitmap.bmWidth = width;
101     bmpObjPtr->bitmap.bmHeight = height;
102     bmpObjPtr->bitmap.bmPlanes = planes;
103     bmpObjPtr->bitmap.bmBitsPixel = bpp;
104     bmpObjPtr->bitmap.bmWidthBytes = BITMAP_WIDTH_BYTES( width, bpp );
105     bmpObjPtr->bitmap.bmBits = NULL;
106
107       /* Create the pixmap */
108     bmpObjPtr->pixmap = XCreatePixmap(display, rootWindow, width, height, bpp);
109     if (!bmpObjPtr->pixmap)
110     {
111         GDI_HEAP_FREE( hbitmap );
112         hbitmap = 0;
113     }
114     else if (bits)  /* Set bitmap bits */
115         SetBitmapBits( hbitmap, height * bmpObjPtr->bitmap.bmWidthBytes, bits);
116     return hbitmap;
117 }
118
119
120 /***********************************************************************
121  *           CreateCompatibleBitmap    (GDI.51)
122  */
123 HBITMAP CreateCompatibleBitmap( HDC hdc, INT width, INT height )
124 {
125     DC * dc;
126     dprintf_gdi(stddeb, "CreateCompatibleBitmap: %04x %dx%d\n", 
127                 hdc, width, height );
128     if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
129     return CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
130 }
131
132
133 /***********************************************************************
134  *           CreateBitmapIndirect    (GDI.49)
135  */
136 HBITMAP CreateBitmapIndirect( const BITMAP * bmp )
137 {
138     return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
139                          bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
140 }
141
142
143 /***********************************************************************
144  *           GetBitmapBits    (GDI.74)
145  */
146 LONG GetBitmapBits( HBITMAP hbitmap, LONG count, LPSTR buffer )
147 {
148     BITMAPOBJ * bmp;
149     LONG height;
150     XImage * image;
151     
152     /* KLUDGE! */
153     if (count < 0) {
154         fprintf(stderr, "Negative number of bytes (%ld) passed to GetBitmapBits???\n", count );
155         count = -count;
156     }
157     bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
158     if (!bmp) return 0;
159
160       /* Only get entire lines */
161     height = count / bmp->bitmap.bmWidthBytes;
162     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
163     dprintf_bitmap(stddeb, "GetBitmapBits: %dx%d %d colors %p fetched height: %ld\n",
164             bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
165             1 << bmp->bitmap.bmBitsPixel, buffer, height );
166     if (!height) return 0;
167     
168     if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
169     CallTo32_LargeStack( (int(*)())XGetSubImage, 11,
170                          display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
171                          height, AllPlanes, ZPixmap, image, 0, 0 );
172     image->data = NULL;
173     XDestroyImage( image );
174     return height * bmp->bitmap.bmWidthBytes;
175 }
176
177
178 /***********************************************************************
179  *           SetBitmapBits    (GDI.106)
180  */
181 LONG SetBitmapBits( HBITMAP hbitmap, LONG count, LPSTR buffer )
182 {
183     BITMAPOBJ * bmp;
184     LONG height;
185     XImage * image;
186     
187     /* KLUDGE! */
188     if (count < 0) {
189         fprintf(stderr, "Negative number of bytes (%ld) passed to SetBitmapBits???\n", count );
190         count = -count;
191     }
192     bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
193     if (!bmp) return 0;
194
195     dprintf_bitmap(stddeb, "SetBitmapBits: %dx%d %d colors %p\n",
196             bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
197             1 << bmp->bitmap.bmBitsPixel, buffer );
198
199       /* Only set entire lines */
200     height = count / bmp->bitmap.bmWidthBytes;
201     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
202     if (!height) return 0;
203         
204     if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
205     CallTo32_LargeStack( XPutImage, 10,
206                          display, bmp->pixmap, BITMAP_GC(bmp), image, 0, 0,
207                          0, 0, bmp->bitmap.bmWidth, height );
208     image->data = NULL;
209     XDestroyImage( image );
210     return height * bmp->bitmap.bmWidthBytes;
211 }
212
213
214 /**********************************************************************
215  *          LoadBitmap    (USER.175)
216  */
217 HBITMAP LoadBitmap( HANDLE instance, SEGPTR name )
218 {
219     HBITMAP hbitmap = 0;
220     HDC hdc;
221     HRSRC hRsrc;
222     HGLOBAL handle;
223     BITMAPINFO *info;
224
225     if (HIWORD(name))
226     {
227         char *str = (char *)PTR_SEG_TO_LIN( name );
228         dprintf_bitmap( stddeb, "LoadBitmap(%04x,'%s')\n", instance, str );
229         if (str[0] == '#') name = (SEGPTR)(DWORD)(WORD)atoi( str + 1 );
230     }
231     else
232         dprintf_bitmap( stddeb, "LoadBitmap(%04x,%04x)\n",
233                         instance, LOWORD(name) );
234
235     if (!instance)  /* OEM bitmap */
236     {
237         if (HIWORD((int)name)) return 0;
238         return OBM_LoadBitmap( LOWORD((int)name) );
239     }
240
241     if (!(hRsrc = FindResource( instance, name, RT_BITMAP ))) return 0;
242     if (!(handle = LoadResource( instance, hRsrc ))) return 0;
243
244     info = (BITMAPINFO *)LockResource( handle );
245     if ((hdc = GetDC(0)) != 0)
246     {
247         char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
248         hbitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
249                                   bits, info, DIB_RGB_COLORS );
250         ReleaseDC( 0, hdc );
251     }
252     FreeResource( handle );
253     return hbitmap;
254 }
255
256
257 /***********************************************************************
258  *           BITMAP_DeleteObject
259  */
260 BOOL BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bitmap )
261 {
262     XFreePixmap( display, bitmap->pixmap );
263     return GDI_FreeObject( hbitmap );
264 }
265
266         
267 /***********************************************************************
268  *           BITMAP_GetObject
269  */
270 int BITMAP_GetObject( BITMAPOBJ * bmp, int count, LPSTR buffer )
271 {
272     if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
273     memcpy( buffer, &bmp->bitmap, count );
274     return count;
275 }
276     
277
278 /***********************************************************************
279  *           BITMAP_SelectObject
280  */
281 HBITMAP BITMAP_SelectObject( HDC hdc, DC * dc, HBITMAP hbitmap,
282                              BITMAPOBJ * bmp )
283 {
284     HRGN hrgn;
285     HBITMAP prevHandle = dc->w.hBitmap;
286     
287     if (!(dc->w.flags & DC_MEMORY)) return 0;
288     hrgn = CreateRectRgn( 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
289     if (!hrgn) return 0;
290
291     DeleteObject( dc->w.hVisRgn );
292     dc->w.hVisRgn    = hrgn;
293     dc->u.x.drawable = bmp->pixmap;
294     dc->w.hBitmap    = hbitmap;
295
296       /* Change GC depth if needed */
297
298     if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
299     {
300         XFreeGC( display, dc->u.x.gc );
301         dc->u.x.gc = XCreateGC( display, dc->u.x.drawable, 0, NULL );
302         dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
303         DC_InitDC( hdc );
304     }
305     else CLIPPING_UpdateGCRegion( dc );  /* Just update GC clip region */
306     return prevHandle;
307 }
308
309 /***********************************************************************
310  *           CreateDiscardableBitmap    (GDI.156)
311  */
312 HBITMAP CreateDiscardableBitmap(HDC hdc, INT width, INT height)
313 {
314     dprintf_bitmap(stddeb,"CreateDiscardableBitmap(%04x, %d, %d); "
315            "// call CreateCompatibleBitmap() for now!\n",
316            hdc, width, height);
317     return CreateCompatibleBitmap(hdc, width, height);
318 }
319
320
321 /***********************************************************************
322  *           GetBitmapDimensionEx16    (GDI.468)
323  */
324 BOOL16 GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
325 {
326     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
327     if (!bmp) return FALSE;
328     *size = bmp->size;
329     return TRUE;
330 }
331
332
333 /***********************************************************************
334  *           GetBitmapDimensionEx32    (GDI.468)
335  */
336 BOOL32 GetBitmapDimensionEx32( HBITMAP32 hbitmap, LPSIZE32 size )
337 {
338     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
339     if (!bmp) return FALSE;
340     size->cx = (INT32)bmp->size.cx;
341     size->cy = (INT32)bmp->size.cy;
342     return TRUE;
343 }
344
345
346 /***********************************************************************
347  *           GetBitmapDimension    (GDI.162)
348  */
349 DWORD GetBitmapDimension( HBITMAP16 hbitmap )
350 {
351     SIZE16 size;
352     if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
353     return MAKELONG( size.cx, size.cy );
354 }
355
356
357 /***********************************************************************
358  *           SetBitmapDimensionEx16    (GDI.478)
359  */
360 BOOL16 SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
361                                LPSIZE16 prevSize )
362 {
363     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
364     if (!bmp) return FALSE;
365     if (prevSize) *prevSize = bmp->size;
366     bmp->size.cx = x;
367     bmp->size.cy = y;
368     return TRUE;
369 }
370
371
372 /***********************************************************************
373  *           SetBitmapDimensionEx32    (GDI.478)
374  */
375 BOOL32 SetBitmapDimensionEx32( HBITMAP32 hbitmap, INT32 x, INT32 y,
376                                LPSIZE32 prevSize )
377 {
378     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
379     if (!bmp) return FALSE;
380     if (prevSize) CONV_SIZE16TO32( &bmp->size, prevSize );
381     bmp->size.cx = (INT16)x;
382     bmp->size.cy = (INT16)y;
383     return TRUE;
384 }
385
386
387 /***********************************************************************
388  *           SetBitmapDimension    (GDI.163)
389  */
390 DWORD SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
391 {
392     SIZE16 size;
393     if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
394     return MAKELONG( size.cx, size.cy );
395 }