Release 960717
[wine/multimedia.git] / objects / bitmap.c
blob56805817b265901c812604a15345772751f63f3a
1 /*
2 * GDI bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
11 #include "gdi.h"
12 #include "callback.h"
13 #include "dc.h"
14 #include "bitmap.h"
15 #include "heap.h"
16 #include "string32.h"
17 #include "stddebug.h"
18 #include "debug.h"
20 /* GCs used for B&W and color bitmap operations */
21 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
23 extern void CLIPPING_UpdateGCRegion( DC * dc ); /* objects/clipping.c */
25 /***********************************************************************
26 * BITMAP_Init
28 BOOL BITMAP_Init(void)
30 Pixmap tmpPixmap;
32 /* Create the necessary GCs */
34 if ((tmpPixmap = XCreatePixmap( display, rootWindow, 1, 1, 1 )))
36 BITMAP_monoGC = XCreateGC( display, tmpPixmap, 0, NULL );
37 XSetGraphicsExposures( display, BITMAP_monoGC, False );
38 XFreePixmap( display, tmpPixmap );
41 if (screenDepth != 1)
43 if ((tmpPixmap = XCreatePixmap(display, rootWindow, 1,1,screenDepth)))
45 BITMAP_colorGC = XCreateGC( display, tmpPixmap, 0, NULL );
46 XSetGraphicsExposures( display, BITMAP_colorGC, False );
47 XFreePixmap( display, tmpPixmap );
50 return TRUE;
54 /***********************************************************************
55 * BITMAP_BmpToImage
57 * Create an XImage pointing to the bitmap data.
59 static XImage *BITMAP_BmpToImage( BITMAP16 * bmp, LPVOID bmpData )
61 extern void _XInitImageFuncPtrs( XImage* );
62 XImage * image;
64 image = XCreateImage( 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) (GDI32.25)
79 HBITMAP16 CreateBitmap( INT32 width, INT32 height, UINT32 planes,
80 UINT32 bpp, LPCVOID bits )
82 BITMAPOBJ * bmpObjPtr;
83 HBITMAP16 hbitmap;
85 dprintf_gdi( stddeb, "CreateBitmap: %dx%d, %d colors\n",
86 width, height, 1 << (planes*bpp) );
88 /* Check parameters */
89 if (!height || !width || planes != 1) return 0;
90 if ((bpp != 1) && (bpp != screenDepth)) return 0;
91 if (height < 0) height = -height;
92 if (width < 0) width = -width;
94 /* Create the BITMAPOBJ */
95 hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
96 if (!hbitmap) return 0;
97 bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LIN_ADDR( hbitmap );
99 bmpObjPtr->size.cx = 0;
100 bmpObjPtr->size.cy = 0;
101 bmpObjPtr->bitmap.bmType = 0;
102 bmpObjPtr->bitmap.bmWidth = (INT16)width;
103 bmpObjPtr->bitmap.bmHeight = (INT16)height;
104 bmpObjPtr->bitmap.bmPlanes = (BYTE)planes;
105 bmpObjPtr->bitmap.bmBitsPixel = (BYTE)bpp;
106 bmpObjPtr->bitmap.bmWidthBytes = (INT16)BITMAP_WIDTH_BYTES( width, bpp );
107 bmpObjPtr->bitmap.bmBits = NULL;
109 /* Create the pixmap */
110 bmpObjPtr->pixmap = XCreatePixmap(display, rootWindow, width, height, bpp);
111 if (!bmpObjPtr->pixmap)
113 GDI_HEAP_FREE( hbitmap );
114 hbitmap = 0;
116 else if (bits) /* Set bitmap bits */
117 SetBitmapBits( hbitmap, height * bmpObjPtr->bitmap.bmWidthBytes, bits);
118 return hbitmap;
122 /***********************************************************************
123 * CreateCompatibleBitmap (GDI.51) (GDI32.30)
125 HBITMAP16 CreateCompatibleBitmap( HDC32 hdc, INT32 width, INT32 height )
127 HBITMAP hbmpRet = 0;
128 DC *dc;
130 dprintf_gdi( stddeb, "CreateCompatibleBitmap(%04x,%d,%d) = \n",
131 hdc, width, height );
132 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
134 hbmpRet = CreateBitmap( width, height, 1, dc->w.bitsPerPixel, NULL );
135 dprintf_gdi(stddeb,"\t\t%04x\n", hbmpRet);
136 return hbmpRet;
140 /***********************************************************************
141 * CreateBitmapIndirect16 (GDI.49)
143 HBITMAP16 CreateBitmapIndirect16( const BITMAP16 * bmp )
145 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
146 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
150 /***********************************************************************
151 * CreateBitmapIndirect32 (GDI32.26)
153 HBITMAP32 CreateBitmapIndirect32( const BITMAP32 * bmp )
155 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
156 bmp->bmBitsPixel, bmp->bmBits );
160 /***********************************************************************
161 * GetBitmapBits (GDI.74) (GDI32.143)
163 LONG GetBitmapBits( HBITMAP32 hbitmap, LONG count, LPVOID buffer )
165 BITMAPOBJ * bmp;
166 LONG height;
167 XImage * image;
169 /* KLUDGE! */
170 if (count < 0) {
171 fprintf(stderr, "Negative number of bytes (%ld) passed to GetBitmapBits???\n", count );
172 count = -count;
174 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
175 if (!bmp) return 0;
177 /* Only get entire lines */
178 height = count / bmp->bitmap.bmWidthBytes;
179 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
180 dprintf_bitmap(stddeb, "GetBitmapBits: %dx%d %d colors %p fetched height: %ld\n",
181 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
182 1 << bmp->bitmap.bmBitsPixel, buffer, height );
183 if (!height) return 0;
185 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, buffer ))) return 0;
186 CallTo32_LargeStack( (int(*)())XGetSubImage, 11,
187 display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
188 height, AllPlanes, ZPixmap, image, 0, 0 );
189 image->data = NULL;
190 XDestroyImage( image );
191 return height * bmp->bitmap.bmWidthBytes;
195 /***********************************************************************
196 * SetBitmapBits (GDI.106) (GDI32.303)
198 LONG SetBitmapBits( HBITMAP32 hbitmap, LONG count, LPCVOID buffer )
200 BITMAPOBJ * bmp;
201 LONG height;
202 XImage * image;
204 /* KLUDGE! */
205 if (count < 0) {
206 fprintf(stderr, "Negative number of bytes (%ld) passed to SetBitmapBits???\n", count );
207 count = -count;
209 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
210 if (!bmp) return 0;
212 dprintf_bitmap(stddeb, "SetBitmapBits: %dx%d %d colors %p\n",
213 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
214 1 << bmp->bitmap.bmBitsPixel, buffer );
216 /* Only set entire lines */
217 height = count / bmp->bitmap.bmWidthBytes;
218 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
219 if (!height) return 0;
221 if (!(image = BITMAP_BmpToImage( &bmp->bitmap, (LPVOID)buffer ))) return 0;
222 CallTo32_LargeStack( XPutImage, 10,
223 display, bmp->pixmap, BITMAP_GC(bmp), image, 0, 0,
224 0, 0, bmp->bitmap.bmWidth, height );
225 image->data = NULL;
226 XDestroyImage( image );
227 return height * bmp->bitmap.bmWidthBytes;
231 /**********************************************************************
232 * LoadBitmap16 (USER.175)
234 HBITMAP16 LoadBitmap16( HINSTANCE16 instance, SEGPTR name )
236 HBITMAP16 hbitmap = 0;
237 HDC hdc;
238 HRSRC16 hRsrc;
239 HGLOBAL16 handle;
240 BITMAPINFO *info;
242 if (HIWORD(name))
244 char *str = (char *)PTR_SEG_TO_LIN( name );
245 dprintf_bitmap( stddeb, "LoadBitmap16(%04x,'%s')\n", instance, str );
246 if (str[0] == '#') name = (SEGPTR)(DWORD)(WORD)atoi( str + 1 );
248 else
249 dprintf_bitmap( stddeb, "LoadBitmap16(%04x,%04x)\n",
250 instance, LOWORD(name) );
252 if (!instance) /* OEM bitmap */
254 if (HIWORD((int)name)) return 0;
255 return OBM_LoadBitmap( LOWORD((int)name) );
258 if (!(hRsrc = FindResource16( instance, name, RT_BITMAP ))) return 0;
259 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
261 info = (BITMAPINFO *)LockResource16( handle );
262 if ((hdc = GetDC(0)) != 0)
264 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
265 hbitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
266 bits, info, DIB_RGB_COLORS );
267 ReleaseDC( 0, hdc );
269 FreeResource16( handle );
270 return hbitmap;
273 /**********************************************************************
274 * LoadBitmap32W (USER32.357)
276 HBITMAP32 LoadBitmap32W( HINSTANCE32 instance, LPCWSTR name )
278 HBITMAP32 hbitmap = 0;
279 HDC hdc;
280 HRSRC32 hRsrc;
281 HGLOBAL32 handle;
282 BITMAPINFO *info;
284 if (!instance) /* OEM bitmap */
286 if (HIWORD((int)name)) return 0;
287 return OBM_LoadBitmap( LOWORD((int)name) );
290 if (!(hRsrc = FindResource32W( instance, name,
291 (LPWSTR)RT_BITMAP ))) return 0;
292 if (!(handle = LoadResource32( instance, hRsrc ))) return 0;
294 info = (BITMAPINFO *)LockResource32( handle );
295 if ((hdc = GetDC(0)) != 0)
297 char *bits = (char *)info + DIB_BitmapInfoSize( info, DIB_RGB_COLORS );
298 hbitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
299 bits, info, DIB_RGB_COLORS );
300 ReleaseDC( 0, hdc );
302 return hbitmap;
306 /**********************************************************************
307 * LoadBitmap32A (USER32.356)
309 HBITMAP32 LoadBitmap32A( HINSTANCE32 instance, LPCSTR name )
311 HBITMAP32 res;
312 if (!HIWORD(name)) res = LoadBitmap32W(instance,(LPWSTR)name);
313 else
315 LPWSTR uni = STRING32_DupAnsiToUni(name);
316 res = LoadBitmap32W(instance,uni);
317 free(uni);
319 return res;
323 /***********************************************************************
324 * BITMAP_DeleteObject
326 BOOL BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bitmap )
328 XFreePixmap( display, bitmap->pixmap );
329 return GDI_FreeObject( hbitmap );
333 /***********************************************************************
334 * BITMAP_GetObject16
336 INT16 BITMAP_GetObject16( BITMAPOBJ * bmp, INT16 count, LPVOID buffer )
338 if (count > sizeof(bmp->bitmap)) count = sizeof(bmp->bitmap);
339 memcpy( buffer, &bmp->bitmap, count );
340 return count;
344 /***********************************************************************
345 * BITMAP_GetObject32
347 INT32 BITMAP_GetObject32( BITMAPOBJ * bmp, INT32 count, LPVOID buffer )
349 BITMAP32 bmp32;
350 bmp32.bmType = bmp->bitmap.bmType;
351 bmp32.bmWidth = bmp->bitmap.bmWidth;
352 bmp32.bmHeight = bmp->bitmap.bmHeight;
353 bmp32.bmWidthBytes = bmp->bitmap.bmWidthBytes;
354 bmp32.bmPlanes = bmp->bitmap.bmPlanes;
355 bmp32.bmBitsPixel = bmp->bitmap.bmBitsPixel;
356 bmp32.bmBits = NULL;
357 if (count > sizeof(bmp32)) count = sizeof(bmp32);
358 memcpy( buffer, &bmp32, count );
359 return count;
363 /***********************************************************************
364 * BITMAP_SelectObject
366 HBITMAP BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
367 BITMAPOBJ * bmp )
369 HRGN hrgn;
370 HBITMAP prevHandle = dc->w.hBitmap;
372 if (!(dc->w.flags & DC_MEMORY)) return 0;
373 hrgn = CreateRectRgn( 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
374 if (!hrgn) return 0;
376 DeleteObject( dc->w.hVisRgn );
377 dc->w.hVisRgn = hrgn;
378 dc->u.x.drawable = bmp->pixmap;
379 dc->w.hBitmap = hbitmap;
381 /* Change GC depth if needed */
383 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
385 XFreeGC( display, dc->u.x.gc );
386 dc->u.x.gc = XCreateGC( display, dc->u.x.drawable, 0, NULL );
387 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
388 DC_InitDC( dc );
390 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
391 return prevHandle;
394 /***********************************************************************
395 * CreateDiscardableBitmap (GDI.156) (GDI32.38)
397 HBITMAP16 CreateDiscardableBitmap( HDC32 hdc, INT32 width, INT32 height )
399 dprintf_bitmap(stddeb,"CreateDiscardableBitmap(%04x, %d, %d); "
400 "// call CreateCompatibleBitmap() for now!\n",
401 hdc, width, height);
402 return CreateCompatibleBitmap(hdc, width, height);
406 /***********************************************************************
407 * GetBitmapDimensionEx16 (GDI.468)
409 BOOL16 GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
411 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
412 if (!bmp) return FALSE;
413 *size = bmp->size;
414 return TRUE;
418 /***********************************************************************
419 * GetBitmapDimensionEx32 (GDI32.144)
421 BOOL32 GetBitmapDimensionEx32( HBITMAP32 hbitmap, LPSIZE32 size )
423 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
424 if (!bmp) return FALSE;
425 size->cx = (INT32)bmp->size.cx;
426 size->cy = (INT32)bmp->size.cy;
427 return TRUE;
431 /***********************************************************************
432 * GetBitmapDimension (GDI.162)
434 DWORD GetBitmapDimension( HBITMAP16 hbitmap )
436 SIZE16 size;
437 if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
438 return MAKELONG( size.cx, size.cy );
442 /***********************************************************************
443 * SetBitmapDimensionEx16 (GDI.478)
445 BOOL16 SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
446 LPSIZE16 prevSize )
448 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
449 if (!bmp) return FALSE;
450 if (prevSize) *prevSize = bmp->size;
451 bmp->size.cx = x;
452 bmp->size.cy = y;
453 return TRUE;
457 /***********************************************************************
458 * SetBitmapDimensionEx32 (GDI32.304)
460 BOOL32 SetBitmapDimensionEx32( HBITMAP32 hbitmap, INT32 x, INT32 y,
461 LPSIZE32 prevSize )
463 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
464 if (!bmp) return FALSE;
465 if (prevSize) CONV_SIZE16TO32( &bmp->size, prevSize );
466 bmp->size.cx = (INT16)x;
467 bmp->size.cy = (INT16)y;
468 return TRUE;
472 /***********************************************************************
473 * SetBitmapDimension (GDI.163)
475 DWORD SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
477 SIZE16 size;
478 if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
479 return MAKELONG( size.cx, size.cy );