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] );
131 #define check_xy(x,y) \
138 /***********************************************************************
139 * DIB_SetImageBits_RLE4
141 * SetDIBits for a 4-bit deep compressed DIB.
143 static void DIB_SetImageBits_RLE4( WORD lines
, BYTE
*bits
, WORD width
,
144 WORD
*colors
, XImage
*bmpImage
)
146 int x
= 0, c
, length
;
152 if (length
) { /* encoded */
155 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
159 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
171 case 1: /* eopicture */
179 default: /* absolute */
182 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
186 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
190 if ((bits
- begin
) & 1)
197 /***********************************************************************
200 * SetDIBits for an 8-bit deep DIB.
202 static void DIB_SetImageBits_8( WORD lines
, BYTE
*bits
, WORD width
,
203 WORD
*colors
, XImage
*bmpImage
)
206 BYTE pad
= (4 - (width
& 3)) & 3;
210 for (x
= 0; x
< width
; x
++)
211 XPutPixel( bmpImage
, x
, lines
, colors
[*bits
++] );
216 /***********************************************************************
217 * DIB_SetImageBits_RLE8
219 * SetDIBits for an 8-bit deep compressed DIB.
221 static void DIB_SetImageBits_RLE8( WORD lines
, BYTE
*bits
, WORD width
,
222 WORD
*colors
, XImage
*bmpImage
)
224 int x
= 0, i
, length
;
230 if (length
) { /* encoded */
232 XPutPixel(bmpImage
, x
++, lines
, colors
[*bits
]);
248 case 1: /* eopicture */
256 default: /* absolute */
257 for (i
= length
; i
; i
--) {
258 XPutPixel(bmpImage
, x
++, lines
,
266 if ((bits
- begin
) & 1)
273 /***********************************************************************
274 * DIB_SetImageBits_24
276 * SetDIBits for a 24-bit deep DIB.
278 static void DIB_SetImageBits_24( WORD lines
, BYTE
*bits
, WORD width
,
279 DC
*dc
, XImage
*bmpImage
)
282 BYTE pad
= (4 - ((width
*3) & 3)) & 3;
286 for (x
= 0; x
< width
; x
++, bits
+= 3)
288 XPutPixel( bmpImage
, x
, lines
,
289 COLOR_ToPhysical( dc
, RGB(bits
[0],bits
[1],bits
[2]) ));
296 /***********************************************************************
299 * Transfer the bits to an X image.
300 * Helper function for SetDIBits() and SetDIBitsToDevice().
302 static int DIB_SetImageBits( DC
*dc
, WORD lines
, WORD depth
, LPSTR bits
,
303 BITMAPINFO
*info
, WORD coloruse
,
304 Drawable drawable
, GC gc
, int xSrc
, int ySrc
,
305 int xDest
, int yDest
, int width
, int height
)
310 int i
, colors
, widthBytes
;
312 /* Build the color mapping table */
314 if (info
->bmiHeader
.biBitCount
== 24) colorMapping
= NULL
;
317 colors
= info
->bmiHeader
.biClrUsed
;
318 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
319 if (!(colorMapping
= (WORD
*)malloc( colors
* sizeof(WORD
) )))
321 if (coloruse
== DIB_RGB_COLORS
)
323 RGBQUAD
* rgbPtr
= info
->bmiColors
;
324 for (i
= 0; i
< colors
; i
++, rgbPtr
++)
325 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgbPtr
->rgbRed
,
331 WORD
* index
= (WORD
*)info
->bmiColors
;
332 for (i
= 0; i
< colors
; i
++, index
++)
333 colorMapping
[i
] = COLOR_ToPhysical( dc
, PALETTEINDEX(*index
) );
337 /* Transfer the pixels */
339 widthBytes
= (info
->bmiHeader
.biWidth
* depth
+ 31) / 32 * 4;
340 bmpData
= malloc( lines
* widthBytes
);
341 bmpImage
= XCreateImage( display
, DefaultVisualOfScreen(screen
),
342 depth
, ZPixmap
, 0, bmpData
,
343 info
->bmiHeader
.biWidth
, lines
, 32, widthBytes
);
345 switch(info
->bmiHeader
.biBitCount
)
348 DIB_SetImageBits_1( lines
, bits
, info
->bmiHeader
.biWidth
,
349 colorMapping
, bmpImage
);
352 if (info
->bmiHeader
.biCompression
)
353 DIB_SetImageBits_RLE4( lines
, bits
, info
->bmiHeader
.biWidth
,
354 colorMapping
, bmpImage
);
356 DIB_SetImageBits_4( lines
, bits
, info
->bmiHeader
.biWidth
,
357 colorMapping
, bmpImage
);
360 if (info
->bmiHeader
.biCompression
)
361 DIB_SetImageBits_RLE8( lines
, bits
, info
->bmiHeader
.biWidth
,
362 colorMapping
, bmpImage
);
364 DIB_SetImageBits_8( lines
, bits
, info
->bmiHeader
.biWidth
,
365 colorMapping
, bmpImage
);
368 DIB_SetImageBits_24( lines
, bits
, info
->bmiHeader
.biWidth
,
372 if (colorMapping
) free(colorMapping
);
374 XPutImage( display
, drawable
, gc
, bmpImage
, xSrc
, ySrc
,
375 xDest
, yDest
, width
, height
);
376 XDestroyImage( bmpImage
);
381 /***********************************************************************
382 * SetDIBits (GDI.440)
384 int SetDIBits( HDC hdc
, HBITMAP hbitmap
, WORD startscan
, WORD lines
,
385 LPSTR bits
, BITMAPINFO
* info
, WORD coloruse
)
390 /* Check parameters */
392 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
393 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
395 if (!lines
|| (startscan
>= (WORD
)info
->bmiHeader
.biHeight
)) return 0;
396 if (startscan
+lines
> info
->bmiHeader
.biHeight
)
397 lines
= info
->bmiHeader
.biHeight
- startscan
;
399 return DIB_SetImageBits( dc
, lines
, bmp
->bitmap
.bmBitsPixel
,
400 bits
, info
, coloruse
, bmp
->pixmap
, BITMAP_GC(bmp
),
401 0, 0, 0, startscan
, bmp
->bitmap
.bmWidth
, lines
);
405 /***********************************************************************
406 * SetDIBitsToDevice (GDI.443)
408 int SetDIBitsToDevice( HDC hdc
, short xDest
, short yDest
, WORD cx
, WORD cy
,
409 WORD xSrc
, WORD ySrc
, WORD startscan
, WORD lines
,
410 LPSTR bits
, BITMAPINFO
* info
, WORD coloruse
)
414 /* Check parameters */
416 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
417 if (!lines
|| (startscan
>= info
->bmiHeader
.biHeight
)) return 0;
418 if (startscan
+lines
> info
->bmiHeader
.biHeight
)
419 lines
= info
->bmiHeader
.biHeight
- startscan
;
420 if (ySrc
< startscan
) ySrc
= startscan
;
421 else if (ySrc
>= startscan
+lines
) return 0;
422 if (xSrc
>= info
->bmiHeader
.biWidth
) return 0;
423 if (ySrc
+cy
>= startscan
+lines
) cy
= startscan
+ lines
- ySrc
;
424 if (xSrc
+cx
>= info
->bmiHeader
.biWidth
) cx
= info
->bmiHeader
.biWidth
-xSrc
;
425 if (!cx
|| !cy
) return 0;
427 DC_SetupGCForText( dc
); /* To have the correct ROP */
428 return DIB_SetImageBits( dc
, lines
, dc
->w
.bitsPerPixel
,
429 bits
, info
, coloruse
,
430 dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
431 xSrc
, ySrc
- startscan
,
432 dc
->w
.DCOrgX
+ XLPTODP( dc
, xDest
),
433 dc
->w
.DCOrgY
+ YLPTODP( dc
, yDest
),
438 /***********************************************************************
439 * GetDIBits (GDI.441)
441 int GetDIBits( HDC hdc
, HBITMAP hbitmap
, WORD startscan
, WORD lines
,
442 LPSTR bits
, BITMAPINFO
* info
, WORD coloruse
)
446 PALETTEENTRY
* palEntry
;
447 PALETTEOBJ
* palette
;
448 XImage
* bmpImage
, * dibImage
;
451 if (!lines
) return 0;
452 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
453 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
455 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
458 /* Transfer color info */
460 palEntry
= palette
->logpalette
.palPalEntry
;
461 for (i
= 0; i
< info
->bmiHeader
.biClrUsed
; i
++, palEntry
++)
463 if (coloruse
== DIB_RGB_COLORS
)
465 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
466 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
467 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
468 info
->bmiColors
[i
].rgbReserved
= 0;
470 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
473 /* Transfer the pixels (very slow...) */
477 bmpImage
= XGetImage( display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
,
478 bmp
->bitmap
.bmHeight
, AllPlanes
, ZPixmap
);
479 dibImage
= DIB_DIBmpToImage( &info
->bmiHeader
, bits
);
481 for (y
= 0; y
< lines
; y
++)
483 for (x
= 0; x
< info
->bmiHeader
.biWidth
; x
++)
485 XPutPixel( dibImage
, x
, y
,
486 XGetPixel(bmpImage
, x
, bmp
->bitmap
.bmHeight
-startscan
-y
-1) );
491 dibImage
->data
= NULL
;
492 XDestroyImage( dibImage
);
493 XDestroyImage( bmpImage
);
499 /***********************************************************************
500 * CreateDIBitmap (GDI.442)
502 HBITMAP
CreateDIBitmap( HDC hdc
, BITMAPINFOHEADER
* header
, DWORD init
,
503 LPSTR bits
, BITMAPINFO
* data
, WORD coloruse
)
507 handle
= CreateCompatibleBitmap( hdc
, header
->biWidth
, header
->biHeight
);
508 if (!handle
) return 0;
509 if (init
== CBM_INIT
) SetDIBits( hdc
, handle
, 0, header
->biHeight
,
510 bits
, data
, coloruse
);
514 /***********************************************************************
517 BOOL
DrawIcon(HDC hDC
, short x
, short y
, HICON hIcon
)
525 printf("DrawIcon(%04X, %d, %d, %04X) \n", hDC
, x
, y
, hIcon
);
527 if (hIcon
== (HICON
)NULL
) return FALSE
;
528 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
529 GetObject(lpico
->hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
531 printf("DrawIcon / x=%d y=%d\n", x
, y
);
532 printf("DrawIcon / icon Width=%d\n", (int)lpico
->descriptor
.Width
);
533 printf("DrawIcon / icon Height=%d\n", (int)lpico
->descriptor
.Height
);
534 printf("DrawIcon / icon ColorCount=%d\n", (int)lpico
->descriptor
.ColorCount
);
535 printf("DrawIcon / icon icoDIBSize=%lX\n", (DWORD
)lpico
->descriptor
.icoDIBSize
);
536 printf("DrawIcon / icon icoDIBOffset=%lX\n", (DWORD
)lpico
->descriptor
.icoDIBOffset
);
537 printf("DrawIcon / bitmap bmWidth=%d bmHeight=%d\n", bm
.bmWidth
, bm
.bmHeight
);
539 hMemDC
= CreateCompatibleDC(hDC
);
541 SelectObject(hMemDC
, lpico
->hBitmap
);
542 BitBlt(hDC
, x
, y
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCCOPY
);
543 SelectObject(hMemDC
, lpico
->hBitMask
);
544 BitBlt(hDC
, x
, y
+ bm
.bmHeight
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCCOPY
);
546 SelectObject(hMemDC
, lpico
->hBitMask
);
547 BitBlt(hDC
, x
, y
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCAND
);
548 SelectObject(hMemDC
, lpico
->hBitmap
);
549 BitBlt(hDC
, x
, y
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCPAINT
);