2 * GDI device independent bitmaps
4 * Copyright 1993 Alexandre Julliard
7 static char Copyright
[] = "Copyright Alexandre Julliard, 1993";
12 #include <X11/Xutil.h>
17 extern WORD
COLOR_ToPhysical( DC
*dc
, COLORREF color
); /* color.c */
19 /***********************************************************************
22 * Return the size of the bitmap info structure.
24 int DIB_BitmapInfoSize( BITMAPINFO
* info
, WORD coloruse
)
26 int size
= info
->bmiHeader
.biClrUsed
;
27 if (!size
&& (info
->bmiHeader
.biBitCount
!= 24))
28 size
= 1 << info
->bmiHeader
.biBitCount
;
29 if (coloruse
== DIB_RGB_COLORS
)
30 size
= info
->bmiHeader
.biSize
+ size
* sizeof(RGBQUAD
);
32 size
= info
->bmiHeader
.biSize
+ size
* sizeof(WORD
);
37 /***********************************************************************
40 * Create an XImage pointing to the bitmap data.
42 static XImage
*DIB_DIBmpToImage( BITMAPINFOHEADER
* bmp
, void * bmpData
)
44 extern void _XInitImageFuncPtrs( XImage
* );
46 int bytesPerLine
= (bmp
->biWidth
* bmp
->biBitCount
+ 31) / 32 * 4;
48 image
= XCreateImage( display
, DefaultVisualOfScreen( screen
),
49 bmp
->biBitCount
, ZPixmap
, 0, bmpData
,
50 bmp
->biWidth
, bmp
->biHeight
, 32, bytesPerLine
);
52 image
->byte_order
= MSBFirst
;
53 image
->bitmap_bit_order
= MSBFirst
;
54 image
->bitmap_unit
= 16;
55 _XInitImageFuncPtrs(image
);
60 /***********************************************************************
63 * SetDIBits for a 1-bit deep DIB.
65 static void DIB_SetImageBits_1( WORD lines
, BYTE
*bits
, WORD width
,
66 WORD
*colors
, XImage
*bmpImage
)
71 if (!(width
& 31)) pad
= 0;
72 else pad
= ((32 - (width
& 31)) + 7) / 8;
76 for (i
= width
/8, x
= 0; (i
> 0); i
--)
79 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
80 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 6) & 1] );
81 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 5) & 1] );
82 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 4) & 1] );
83 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 3) & 1] );
84 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 2) & 1] );
85 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 1) & 1] );
86 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 1] );
91 case 7: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
92 case 6: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
93 case 5: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
94 case 4: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
95 case 3: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
96 case 2: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
97 case 1: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
104 /***********************************************************************
107 * SetDIBits for a 4-bit deep DIB.
109 static void DIB_SetImageBits_4( WORD lines
, BYTE
*bits
, WORD width
,
110 WORD
*colors
, XImage
*bmpImage
)
115 if (!(width
& 7)) pad
= 0;
116 else pad
= ((8 - (width
& 7)) + 1) / 2;
120 for (i
= width
/2, x
= 0; i
> 0; i
--)
123 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 4] );
124 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 0x0f] );
126 if (width
& 1) XPutPixel( bmpImage
, x
, lines
, colors
[*bits
>> 4] );
132 /***********************************************************************
135 * SetDIBits for an 8-bit deep DIB.
137 static void DIB_SetImageBits_8( WORD lines
, BYTE
*bits
, WORD width
,
138 WORD
*colors
, XImage
*bmpImage
)
141 BYTE pad
= (4 - (width
& 3)) & 3;
145 for (x
= 0; x
< width
; x
++)
146 XPutPixel( bmpImage
, x
, lines
, colors
[*bits
++] );
152 /***********************************************************************
153 * DIB_SetImageBits_24
155 * SetDIBits for a 24-bit deep DIB.
157 static void DIB_SetImageBits_24( WORD lines
, BYTE
*bits
, WORD width
,
158 DC
*dc
, XImage
*bmpImage
)
161 BYTE pad
= (4 - ((width
*3) & 3)) & 3;
165 for (x
= 0; x
< width
; x
++, bits
+= 3)
167 XPutPixel( bmpImage
, x
, lines
,
168 COLOR_ToPhysical( dc
, RGB(bits
[0],bits
[1],bits
[2]) ));
175 /***********************************************************************
178 * Transfer the bits to an X image.
179 * Helper function for SetDIBits() and SetDIBitsToDevice().
181 static int DIB_SetImageBits( DC
*dc
, WORD lines
, WORD depth
, LPSTR bits
,
182 BITMAPINFO
*info
, WORD coloruse
,
183 Drawable drawable
, GC gc
, int xSrc
, int ySrc
,
184 int xDest
, int yDest
, int width
, int height
)
189 int i
, colors
, widthBytes
;
191 /* Build the color mapping table */
193 if (info
->bmiHeader
.biBitCount
== 24) colorMapping
= NULL
;
196 colors
= info
->bmiHeader
.biClrUsed
;
197 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
198 if (!(colorMapping
= (WORD
*)malloc( colors
* sizeof(WORD
) )))
200 if (coloruse
== DIB_RGB_COLORS
)
202 RGBQUAD
* rgbPtr
= info
->bmiColors
;
203 for (i
= 0; i
< colors
; i
++, rgbPtr
++)
204 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgbPtr
->rgbRed
,
210 WORD
* index
= (WORD
*)info
->bmiColors
;
211 for (i
= 0; i
< colors
; i
++, index
++)
212 colorMapping
[i
] = COLOR_ToPhysical( dc
, PALETTEINDEX(*index
) );
216 /* Transfer the pixels */
218 widthBytes
= (info
->bmiHeader
.biWidth
* depth
+ 31) / 32 * 4;
219 bmpData
= malloc( lines
* widthBytes
);
220 bmpImage
= XCreateImage( display
, DefaultVisualOfScreen(screen
),
221 depth
, ZPixmap
, 0, bmpData
,
222 info
->bmiHeader
.biWidth
, lines
, 32, widthBytes
);
224 switch(info
->bmiHeader
.biBitCount
)
227 DIB_SetImageBits_1( lines
, bits
, info
->bmiHeader
.biWidth
,
228 colorMapping
, bmpImage
);
231 DIB_SetImageBits_4( lines
, bits
, info
->bmiHeader
.biWidth
,
232 colorMapping
, bmpImage
);
235 DIB_SetImageBits_8( lines
, bits
, info
->bmiHeader
.biWidth
,
236 colorMapping
, bmpImage
);
239 DIB_SetImageBits_24( lines
, bits
, info
->bmiHeader
.biWidth
,
243 if (colorMapping
) free(colorMapping
);
245 XPutImage( display
, drawable
, gc
, bmpImage
, xSrc
, ySrc
,
246 xDest
, yDest
, width
, height
);
247 XDestroyImage( bmpImage
);
252 /***********************************************************************
253 * SetDIBits (GDI.440)
255 int SetDIBits( HDC hdc
, HBITMAP hbitmap
, WORD startscan
, WORD lines
,
256 LPSTR bits
, BITMAPINFO
* info
, WORD coloruse
)
261 /* Check parameters */
263 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
264 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
266 if (!lines
|| (startscan
>= (WORD
)info
->bmiHeader
.biHeight
)) return 0;
267 if (startscan
+lines
> info
->bmiHeader
.biHeight
)
268 lines
= info
->bmiHeader
.biHeight
- startscan
;
270 return DIB_SetImageBits( dc
, lines
, bmp
->bitmap
.bmBitsPixel
,
271 bits
, info
, coloruse
, bmp
->pixmap
, BITMAP_GC(bmp
),
272 0, 0, 0, startscan
, bmp
->bitmap
.bmWidth
, lines
);
276 /***********************************************************************
277 * SetDIBitsToDevice (GDI.443)
279 int SetDIBitsToDevice( HDC hdc
, short xDest
, short yDest
, WORD cx
, WORD cy
,
280 WORD xSrc
, WORD ySrc
, WORD startscan
, WORD lines
,
281 LPSTR bits
, BITMAPINFO
* info
, WORD coloruse
)
285 /* Check parameters */
287 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
288 if (!lines
|| (startscan
>= info
->bmiHeader
.biHeight
)) return 0;
289 if (startscan
+lines
> info
->bmiHeader
.biHeight
)
290 lines
= info
->bmiHeader
.biHeight
- startscan
;
291 if (ySrc
< startscan
) ySrc
= startscan
;
292 else if (ySrc
>= startscan
+lines
) return 0;
293 if (xSrc
>= info
->bmiHeader
.biWidth
) return 0;
294 if (ySrc
+cy
>= startscan
+lines
) cy
= startscan
+ lines
- ySrc
;
295 if (xSrc
+cx
>= info
->bmiHeader
.biWidth
) cx
= info
->bmiHeader
.biWidth
-xSrc
;
296 if (!cx
|| !cy
) return 0;
298 DC_SetupGCForText( dc
); /* To have the correct ROP */
299 return DIB_SetImageBits( dc
, lines
, dc
->w
.bitsPerPixel
,
300 bits
, info
, coloruse
,
301 dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
302 xSrc
, ySrc
- startscan
,
303 dc
->w
.DCOrgX
+ XLPTODP( dc
, xDest
),
304 dc
->w
.DCOrgY
+ YLPTODP( dc
, yDest
),
309 /***********************************************************************
310 * GetDIBits (GDI.441)
312 int GetDIBits( HDC hdc
, HBITMAP hbitmap
, WORD startscan
, WORD lines
,
313 LPSTR bits
, BITMAPINFO
* info
, WORD coloruse
)
317 PALETTEENTRY
* palEntry
;
318 PALETTEOBJ
* palette
;
319 XImage
* bmpImage
, * dibImage
;
322 if (!lines
) return 0;
323 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
324 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
326 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
329 /* Transfer color info */
331 palEntry
= palette
->logpalette
.palPalEntry
;
332 for (i
= 0; i
< info
->bmiHeader
.biClrUsed
; i
++, palEntry
++)
334 if (coloruse
== DIB_RGB_COLORS
)
336 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
337 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
338 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
339 info
->bmiColors
[i
].rgbReserved
= 0;
341 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
344 /* Transfer the pixels (very slow...) */
348 bmpImage
= XGetImage( display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
,
349 bmp
->bitmap
.bmHeight
, AllPlanes
, ZPixmap
);
350 dibImage
= DIB_DIBmpToImage( &info
->bmiHeader
, bits
);
352 for (y
= 0; y
< lines
; y
++)
354 for (x
= 0; x
< info
->bmiHeader
.biWidth
; x
++)
356 XPutPixel( dibImage
, x
, y
,
357 XGetPixel(bmpImage
, x
, bmp
->bitmap
.bmHeight
-startscan
-y
-1) );
362 dibImage
->data
= NULL
;
363 XDestroyImage( dibImage
);
364 XDestroyImage( bmpImage
);
370 /***********************************************************************
371 * CreateDIBitmap (GDI.442)
373 HBITMAP
CreateDIBitmap( HDC hdc
, BITMAPINFOHEADER
* header
, DWORD init
,
374 LPSTR bits
, BITMAPINFO
* data
, WORD coloruse
)
378 handle
= CreateCompatibleBitmap( hdc
, header
->biWidth
, header
->biHeight
);
379 if (!handle
) return 0;
380 if (init
== CBM_INIT
) SetDIBits( hdc
, handle
, 0, header
->biHeight
,
381 bits
, data
, coloruse
);
385 /***********************************************************************
388 BOOL
DrawIcon(HDC hDC
, short x
, short y
, HICON hIcon
)
396 printf("DrawIcon(%04X, %d, %d, %04X) \n", hDC
, x
, y
, hIcon
);
398 if (hIcon
== (HICON
)NULL
) return FALSE
;
399 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
400 GetObject(lpico
->hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
402 printf("DrawIcon / x=%d y=%d\n", x
, y
);
403 printf("DrawIcon / icon Width=%d\n", (int)lpico
->descriptor
.Width
);
404 printf("DrawIcon / icon Height=%d\n", (int)lpico
->descriptor
.Height
);
405 printf("DrawIcon / icon ColorCount=%d\n", (int)lpico
->descriptor
.ColorCount
);
406 printf("DrawIcon / icon icoDIBSize=%lX\n", (DWORD
)lpico
->descriptor
.icoDIBSize
);
407 printf("DrawIcon / icon icoDIBOffset=%lX\n", (DWORD
)lpico
->descriptor
.icoDIBOffset
);
408 printf("DrawIcon / bitmap bmWidth=%d bmHeight=%d\n", bm
.bmWidth
, bm
.bmHeight
);
410 hMemDC
= CreateCompatibleDC(hDC
);
412 SelectObject(hMemDC
, lpico
->hBitmap
);
413 BitBlt(hDC
, x
, y
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCCOPY
);
414 SelectObject(hMemDC
, lpico
->hBitMask
);
415 BitBlt(hDC
, x
, y
+ bm
.bmHeight
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCCOPY
);
417 SelectObject(hMemDC
, lpico
->hBitMask
);
418 BitBlt(hDC
, x
, y
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCAND
);
419 SelectObject(hMemDC
, lpico
->hBitmap
);
420 BitBlt(hDC
, x
, y
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCPAINT
);