4 * Copyright 1993 Alexandre Julliard
15 /* #define DEBUG_GDI */
16 /* #define DEBUG_BITMAP */
19 /* GCs used for B&W and color bitmap operations */
20 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
22 extern void CLIPPING_UpdateGCRegion( DC * dc ); /* objects/clipping.c */
24 /***********************************************************************
27 BOOL BITMAP_Init(void)
31 /* Create the necessary GCs */
33 if ((tmpPixmap = XCreatePixmap( display, rootWindow, 1, 1, 1 )))
35 BITMAP_monoGC = XCreateGC( display, tmpPixmap, 0, NULL );
36 XSetGraphicsExposures( display, BITMAP_monoGC, False );
37 XFreePixmap( display, tmpPixmap );
42 if ((tmpPixmap = XCreatePixmap(display, rootWindow, 1,1,screenDepth)))
44 BITMAP_colorGC = XCreateGC( display, tmpPixmap, 0, NULL );
45 XSetGraphicsExposures( display, BITMAP_colorGC, False );
46 XFreePixmap( display, tmpPixmap );
53 /***********************************************************************
56 * Create an XImage pointing to the bitmap data.
58 static XImage *BITMAP_BmpToImage( BITMAP * bmp, void * bmpData )
60 extern void _XInitImageFuncPtrs( XImage* );
63 image = XCreateImage( display, DefaultVisualOfScreen(screen),
64 bmp->bmBitsPixel, ZPixmap, 0, bmpData,
65 bmp->bmWidth, bmp->bmHeight, 16, bmp->bmWidthBytes );
67 image->byte_order = MSBFirst;
68 image->bitmap_bit_order = MSBFirst;
69 image->bitmap_unit = 16;
70 _XInitImageFuncPtrs(image);
75 /***********************************************************************
76 * CreateBitmap (GDI.48)
78 HBITMAP CreateBitmap( short width, short height,
79 BYTE planes, BYTE bpp, LPSTR bits )
81 BITMAPOBJ * bmpObjPtr;
84 dprintf_gdi( stddeb, "CreateBitmap: %dx%d, %d colors\n",
85 width, height, 1 << (planes*bpp) );
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;
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 );
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;
108 /* Create the pixmap */
109 bmpObjPtr->pixmap = XCreatePixmap(display, rootWindow, width, height, bpp);
110 if (!bmpObjPtr->pixmap)
112 GDI_HEAP_FREE( hbitmap );
115 else if (bits) /* Set bitmap bits */
116 SetBitmapBits( hbitmap, height * bmpObjPtr->bitmap.bmWidthBytes, bits);
121 /***********************************************************************
122 * CreateCompatibleBitmap (GDI.51)
124 HBITMAP CreateCompatibleBitmap( HDC hdc, short width, short height )
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 );
134 /***********************************************************************
135 * CreateBitmapIndirect (GDI.49)
137 HBITMAP CreateBitmapIndirect( BITMAP * bmp )
139 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
140 bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
144 /***********************************************************************
145 * GetBitmapBits (GDI.74)
147 LONG GetBitmapBits( HBITMAP hbitmap, LONG count, LPSTR buffer )
153 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
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;
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 );
169 XDestroyImage( image );
170 return height * bmp->bitmap.bmWidthBytes;
174 /***********************************************************************
175 * SetBitmapBits (GDI.106)
177 LONG SetBitmapBits( HBITMAP hbitmap, LONG count, LPSTR buffer )
183 bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
186 dprintf_bitmap(stddeb, "SetBitmapBits: %dx%d %d colors %p\n",
187 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
188 1 << bmp->bitmap.bmBitsPixel, buffer );
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;
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 );
200 XDestroyImage( image );
201 return height * bmp->bitmap.bmWidthBytes;
205 /**********************************************************************
206 * LoadBitmap (USER.175)
208 HBITMAP LoadBitmap( HANDLE instance, SEGPTR name )
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 );
223 dprintf_bitmap( stddeb, "LoadBitmap("NPFMT",%04x)\n",
224 instance, LOWORD(name) );
226 if (!instance) /* OEM bitmap */
228 if (HIWORD((int)name)) return 0;
229 return OBM_LoadBitmap( LOWORD((int)name) );
232 if (!(hRsrc = FindResource( instance, name, RT_BITMAP ))) return 0;
233 if (!(handle = LoadResource( instance, hRsrc ))) return 0;
235 info = (BITMAPINFO *)LockResource( handle );
236 if ((hdc = GetDC(0)) != 0)
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 );
243 FreeResource( handle );
248 /***********************************************************************
249 * BITMAP_DeleteObject
251 BOOL BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bitmap )
253 XFreePixmap( display, bitmap->pixmap );
254 return GDI_FreeObject( hbitmap );
258 /***********************************************************************
261 int BITMAP_GetObject( BITMAPOBJ * bmp, int count, LPSTR buffer )
263 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
264 memcpy( buffer, &bmp->bitmap, count );
269 /***********************************************************************
270 * BITMAP_SelectObject
272 HBITMAP BITMAP_SelectObject( HDC hdc, DC * dc, HBITMAP hbitmap,
276 HBITMAP prevHandle = dc->w.hBitmap;
278 if (!(dc->w.flags & DC_MEMORY)) return 0;
279 hrgn = CreateRectRgn( 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
282 DeleteObject( dc->w.hVisRgn );
283 dc->w.hVisRgn = hrgn;
284 dc->u.x.drawable = bmp->pixmap;
285 dc->w.hBitmap = hbitmap;
287 /* Change GC depth if needed */
289 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
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;
296 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
300 /***********************************************************************
301 * CreateDiscardableBitmap (GDI.156)
303 HBITMAP CreateDiscardableBitmap(HDC hdc, short width, short height)
305 dprintf_bitmap(stddeb,"CreateDiscardableBitmap("NPFMT", %d, %d); "
306 "// call CreateCompatibleBitmap() for now!\n",
308 return CreateCompatibleBitmap(hdc, width, height);
311 /***********************************************************************
312 * GetBitmapDimensionEx (GDI.468)
314 BOOL GetBitmapDimensionEx( HBITMAP hbitmap, LPSIZE size )
316 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
317 if (!bmp) return FALSE;
323 /***********************************************************************
324 * GetBitmapDimension (GDI.162)
326 DWORD GetBitmapDimension( HBITMAP hbitmap )
329 if (!GetBitmapDimensionEx( hbitmap, &size )) return 0;
330 return size.cx | (size.cy << 16);
333 /***********************************************************************
334 * SetBitmapDimensionEx (GDI.478)
336 BOOL SetBitmapDimensionEx( HBITMAP hbitmap, short x, short y, LPSIZE prevSize )
338 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
339 if (!bmp) return FALSE;
340 if (prevSize) *prevSize = bmp->size;
347 /***********************************************************************
348 * SetBitmapDimension (GDI.163)
350 DWORD SetBitmapDimension( HBITMAP hbitmap, short x, short y )
353 if (!SetBitmapDimensionEx( hbitmap, x, y, &size )) return 0;
354 return size.cx | (size.cy << 16);