2 * GDI device independent bitmaps
4 * Copyright 1993 Alexandre Julliard
7 static char Copyright
[] = "Copyright Alexandre Julliard, 1993";
12 #include <X11/Xutil.h>
17 /* #define DEBUG_ICON /* */
18 /* #undef DEBUG_ICON /* */
19 /* #define DEBUG_BITMAP /* */
20 /* #undef DEBUG_BIYMAP /* */
23 extern const int DC_XROPfunction
[];
25 extern WORD
COLOR_ToPhysical( DC
*dc
, COLORREF color
); /* color.c */
27 /***********************************************************************
30 * Return the size of the bitmap info structure.
32 int DIB_BitmapInfoSize( BITMAPINFO
* info
, WORD coloruse
)
34 int size
= info
->bmiHeader
.biClrUsed
;
35 if (!size
&& (info
->bmiHeader
.biBitCount
!= 24))
36 size
= 1 << info
->bmiHeader
.biBitCount
;
37 if (coloruse
== DIB_RGB_COLORS
)
38 size
= info
->bmiHeader
.biSize
+ size
* sizeof(RGBQUAD
);
40 size
= info
->bmiHeader
.biSize
+ size
* sizeof(WORD
);
45 /***********************************************************************
48 * Create an XImage pointing to the bitmap data.
50 static XImage
*DIB_DIBmpToImage( BITMAPINFOHEADER
* bmp
, void * bmpData
)
52 extern void _XInitImageFuncPtrs( XImage
* );
54 int bytesPerLine
= ((bmp
->biBitCount
== 24 ? 32 : bmp
->biBitCount
)
55 * bmp
->biWidth
+ 31) / 32 * 4;
57 image
= XCreateImage( display
, DefaultVisualOfScreen( screen
),
58 bmp
->biBitCount
, ZPixmap
, 0, bmpData
,
59 bmp
->biWidth
, bmp
->biHeight
, 32, bytesPerLine
);
61 image
->byte_order
= MSBFirst
;
62 image
->bitmap_bit_order
= MSBFirst
;
63 image
->bitmap_unit
= 16;
64 _XInitImageFuncPtrs(image
);
69 /***********************************************************************
72 * SetDIBits for a 1-bit deep DIB.
74 static void DIB_SetImageBits_1( WORD lines
, BYTE
*bits
, WORD width
,
75 WORD
*colors
, XImage
*bmpImage
)
80 if (!(width
& 31)) pad
= 0;
81 else pad
= ((32 - (width
& 31)) + 7) / 8;
85 for (i
= width
/8, x
= 0; (i
> 0); i
--)
88 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
89 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 6) & 1] );
90 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 5) & 1] );
91 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 4) & 1] );
92 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 3) & 1] );
93 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 2) & 1] );
94 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 1) & 1] );
95 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 1] );
100 case 7: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
101 case 6: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
102 case 5: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
103 case 4: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
104 case 3: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
105 case 2: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
106 case 1: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
113 /***********************************************************************
116 * SetDIBits for a 4-bit deep DIB.
118 static void DIB_SetImageBits_4( WORD lines
, BYTE
*bits
, WORD width
,
119 WORD
*colors
, XImage
*bmpImage
)
124 if (!(width
& 7)) pad
= 0;
125 else pad
= ((8 - (width
& 7)) + 1) / 2;
129 for (i
= width
/2, x
= 0; i
> 0; i
--)
132 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 4] );
133 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 0x0f] );
135 if (width
& 1) XPutPixel( bmpImage
, x
, lines
, colors
[*bits
>> 4] );
140 #define check_xy(x,y) \
147 /***********************************************************************
148 * DIB_SetImageBits_RLE4
150 * SetDIBits for a 4-bit deep compressed DIB.
152 static void DIB_SetImageBits_RLE4( WORD lines
, BYTE
*bits
, WORD width
,
153 WORD
*colors
, XImage
*bmpImage
)
155 int x
= 0, c
, length
;
161 if (length
) { /* encoded */
164 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
168 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
180 case 1: /* eopicture */
188 default: /* absolute */
191 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
195 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
199 if ((bits
- begin
) & 1)
206 /***********************************************************************
209 * SetDIBits for an 8-bit deep DIB.
211 static void DIB_SetImageBits_8( WORD lines
, BYTE
*bits
, WORD width
,
212 WORD
*colors
, XImage
*bmpImage
)
215 BYTE pad
= (4 - (width
& 3)) & 3;
219 for (x
= 0; x
< width
; x
++)
220 XPutPixel( bmpImage
, x
, lines
, colors
[*bits
++] );
225 /***********************************************************************
226 * DIB_SetImageBits_RLE8
228 * SetDIBits for an 8-bit deep compressed DIB.
230 * This function rewritten 941113 by James Youngman. WINE blew out when I
231 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
233 * This was because the algorithm assumed that all RLE8 bitmaps end with the
234 * 'End of bitmap' escape code. This code is very much laxer in what it
235 * allows to end the expansion. Possibly too lax. See the note by
236 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
237 * bitmap should end with RleEnd, but on the other hand, software exists
238 * that produces ones that don't and Windows 3.1 doesn't complain a bit
241 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
242 * James A. Youngman <mbcstjy@afs.man.ac.uk>
246 enum Rle8_EscapeCodes
249 * Apologies for polluting your file's namespace...
251 RleEol
= 0, /* End of line */
252 RleEnd
= 1, /* End of bitmap */
253 RleDelta
= 2 /* Delta */
256 static void DIB_SetImageBits_RLE8(WORD lines
,
262 int x
; /* X-positon on each line. Increases. */
263 int line
; /* Line #. Starts at lines-1, decreases */
264 BYTE
*pIn
= bits
; /* Pointer to current position in bits */
265 BYTE length
; /* The length pf a run */
266 BYTE color_index
; /* index into colors[] as read from bits */
267 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
268 WORD color
; /* value of colour[color_index] */
270 if (lines
== 0) /* Let's hope this doesn't happen. */
274 * Note that the bitmap data is stored by Windows starting at the
275 * bottom line of the bitmap and going upwards. Within each line,
276 * the data is stored left-to-right. That's the reason why line
277 * goes from lines-1 to 0. [JAY]
287 * If the length byte is not zero (which is the escape value),
288 * We have a run of length pixels all the same colour. The colour
289 * index is stored next.
291 * If the length byte is zero, we need to read the next byte to
292 * know what to do. [JAY]
297 * [Run-Length] Encoded mode
299 color_index
= (*pIn
++); /* Get the colour index. */
300 color
= colors
[color_index
];
303 XPutPixel(bmpImage
, x
++, line
, color
);
308 * Escape codes (may be an absolute sequence though)
310 escape_code
= (*pIn
++);
313 case RleEol
: /* =0, end of line */
320 case RleEnd
: /* =1, end of bitmap */
323 * Not all RLE8 bitmaps end with this
324 * code. For example, Paint Shop Pro
325 * produces some that don't. That's (I think)
326 * what caused the previous implementation to
329 line
=0; /* Cause exit from do loop. */
332 case RleDelta
: /* =2, a delta */
335 * Note that deltaing to line 0
336 * will cause an exit from the loop,
337 * which may not be what is intended.
338 * The fact that there is a delta in the bits
339 * almost certainly implies that there is data
340 * to follow. You may feel that we should
341 * jump to the top of the loop to avoid exiting
344 * TODO: Decide what to do here in that case. [JAY]
350 dprintf_bitmap(stddeb
,
351 "DIB_SetImageBits_RLE8(): "
352 "Delta to last line of bitmap "
353 "(wrongly??) causes loop exit\n");
358 default: /* >2, switch to absolute mode */
363 length
= escape_code
;
366 color_index
= (*pIn
++);
367 XPutPixel(bmpImage
, x
++, line
,
368 colors
[color_index
]);
372 * If you think for a moment you'll realise that the
373 * only time we could ever possibly read an odd
374 * number of bytes is when there is a 0x00 (escape),
375 * a value >0x02 (absolute mode) and then an odd-
376 * length run. Therefore this is the only place we
377 * need to worry about it. Everywhere else the
378 * bytes are always read in pairs. [JAY]
381 (*pIn
++); /* Get and throw away the pad byte. */
384 } /* switch (escape_code) : Escape sequence */
385 } /* process either an encoded sequence or an escape sequence */
387 /* We expect to come here more than once per line. */
388 } while (line
> 0); /* Do this until the bitmap is filled */
391 * Everybody comes here at the end.
392 * Check how we exited the loop and print a message if it's a bit odd.
395 if ( (*(pIn
-2) != 0/*escape*/) || (*(pIn
-1)!= RleEnd
) )
397 dprintf_bitmap(stddeb
, "DIB_SetImageBits_RLE8(): End-of-bitmap "
398 "without (strictly) proper escape code. Last two "
399 "bytes were: %02X %02X.\n",
406 /***********************************************************************
407 * DIB_SetImageBits_24
409 * SetDIBits for a 24-bit deep DIB.
411 static void DIB_SetImageBits_24( WORD lines
, BYTE
*bits
, WORD width
,
412 DC
*dc
, XImage
*bmpImage
)
415 BYTE pad
= (4 - ((width
*3) & 3)) & 3;
419 for (x
= 0; x
< width
; x
++, bits
+= 3)
421 XPutPixel( bmpImage
, x
, lines
,
422 COLOR_ToPhysical( dc
, RGB(bits
[0],bits
[1],bits
[2]) ));
429 /***********************************************************************
432 * Transfer the bits to an X image.
433 * Helper function for SetDIBits() and SetDIBitsToDevice().
435 static int DIB_SetImageBits( DC
*dc
, WORD lines
, WORD depth
, LPSTR bits
,
436 BITMAPINFO
*info
, WORD coloruse
,
437 Drawable drawable
, GC gc
, int xSrc
, int ySrc
,
438 int xDest
, int yDest
, int width
, int height
)
443 int i
, colors
, widthBytes
;
445 /* Build the color mapping table */
447 if (info
->bmiHeader
.biBitCount
== 24) colorMapping
= NULL
;
450 colors
= info
->bmiHeader
.biClrUsed
;
451 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
452 if (!(colorMapping
= (WORD
*)malloc( colors
* sizeof(WORD
) )))
454 if (coloruse
== DIB_RGB_COLORS
)
456 RGBQUAD
* rgbPtr
= info
->bmiColors
;
457 for (i
= 0; i
< colors
; i
++, rgbPtr
++)
458 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgbPtr
->rgbRed
,
464 WORD
* index
= (WORD
*)info
->bmiColors
;
465 for (i
= 0; i
< colors
; i
++, index
++)
466 colorMapping
[i
] = COLOR_ToPhysical( dc
, PALETTEINDEX(*index
) );
470 /* Transfer the pixels */
472 widthBytes
= ((depth
== 24 ? 32 : depth
) * info
->bmiHeader
.biWidth
+ 31)
474 bmpData
= malloc( lines
* widthBytes
);
475 bmpImage
= XCreateImage( display
, DefaultVisualOfScreen(screen
),
476 depth
, ZPixmap
, 0, bmpData
,
477 info
->bmiHeader
.biWidth
, lines
, 32, widthBytes
);
479 switch(info
->bmiHeader
.biBitCount
)
482 DIB_SetImageBits_1( lines
, bits
, info
->bmiHeader
.biWidth
,
483 colorMapping
, bmpImage
);
486 if (info
->bmiHeader
.biCompression
)
487 DIB_SetImageBits_RLE4( lines
, bits
, info
->bmiHeader
.biWidth
,
488 colorMapping
, bmpImage
);
490 DIB_SetImageBits_4( lines
, bits
, info
->bmiHeader
.biWidth
,
491 colorMapping
, bmpImage
);
494 if (info
->bmiHeader
.biCompression
)
495 DIB_SetImageBits_RLE8( lines
, bits
, info
->bmiHeader
.biWidth
,
496 colorMapping
, bmpImage
);
498 DIB_SetImageBits_8( lines
, bits
, info
->bmiHeader
.biWidth
,
499 colorMapping
, bmpImage
);
502 DIB_SetImageBits_24( lines
, bits
, info
->bmiHeader
.biWidth
,
506 if (colorMapping
) free(colorMapping
);
508 XPutImage( display
, drawable
, gc
, bmpImage
, xSrc
, ySrc
,
509 xDest
, yDest
, width
, height
);
510 XDestroyImage( bmpImage
);
515 /***********************************************************************
516 * SetDIBits (GDI.440)
518 int SetDIBits( HDC hdc
, HBITMAP hbitmap
, WORD startscan
, WORD lines
,
519 LPSTR bits
, BITMAPINFO
* info
, WORD coloruse
)
524 /* Check parameters */
526 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
527 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
529 if (!lines
|| (startscan
>= (WORD
)info
->bmiHeader
.biHeight
)) return 0;
530 if (startscan
+lines
> info
->bmiHeader
.biHeight
)
531 lines
= info
->bmiHeader
.biHeight
- startscan
;
533 return DIB_SetImageBits( dc
, lines
, bmp
->bitmap
.bmBitsPixel
,
534 bits
, info
, coloruse
, bmp
->pixmap
, BITMAP_GC(bmp
),
535 0, 0, 0, startscan
, bmp
->bitmap
.bmWidth
, lines
);
539 /***********************************************************************
540 * SetDIBitsToDevice (GDI.443)
542 int SetDIBitsToDevice( HDC hdc
, short xDest
, short yDest
, WORD cx
, WORD cy
,
543 WORD xSrc
, WORD ySrc
, WORD startscan
, WORD lines
,
544 LPSTR bits
, BITMAPINFO
* info
, WORD coloruse
)
548 /* Check parameters */
550 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
551 if (!lines
|| (startscan
>= info
->bmiHeader
.biHeight
)) return 0;
552 if (startscan
+lines
> info
->bmiHeader
.biHeight
)
553 lines
= info
->bmiHeader
.biHeight
- startscan
;
554 if (ySrc
< startscan
) ySrc
= startscan
;
555 else if (ySrc
>= startscan
+lines
) return 0;
556 if (xSrc
>= info
->bmiHeader
.biWidth
) return 0;
557 if (ySrc
+cy
>= startscan
+lines
) cy
= startscan
+ lines
- ySrc
;
558 if (xSrc
+cx
>= info
->bmiHeader
.biWidth
) cx
= info
->bmiHeader
.biWidth
-xSrc
;
559 if (!cx
|| !cy
) return 0;
561 DC_SetupGCForText( dc
); /* To have the correct colors */
562 XSetFunction( display
, dc
->u
.x
.gc
, DC_XROPfunction
[dc
->w
.ROPmode
-1] );
563 return DIB_SetImageBits( dc
, lines
, dc
->w
.bitsPerPixel
,
564 bits
, info
, coloruse
,
565 dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
566 xSrc
, ySrc
- startscan
,
567 dc
->w
.DCOrgX
+ XLPTODP( dc
, xDest
),
568 dc
->w
.DCOrgY
+ YLPTODP( dc
, yDest
),
573 /***********************************************************************
574 * GetDIBits (GDI.441)
576 int GetDIBits( HDC hdc
, HBITMAP hbitmap
, WORD startscan
, WORD lines
,
577 LPSTR bits
, BITMAPINFO
* info
, WORD coloruse
)
581 PALETTEENTRY
* palEntry
;
582 PALETTEOBJ
* palette
;
583 XImage
* bmpImage
, * dibImage
;
586 if (!lines
) return 0;
587 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
588 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
590 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
593 /* Transfer color info */
595 palEntry
= palette
->logpalette
.palPalEntry
;
596 for (i
= 0; i
< info
->bmiHeader
.biClrUsed
; i
++, palEntry
++)
598 if (coloruse
== DIB_RGB_COLORS
)
600 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
601 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
602 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
603 info
->bmiColors
[i
].rgbReserved
= 0;
605 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
608 /* Transfer the pixels (very slow...) */
612 bmpImage
= XGetImage( display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
,
613 bmp
->bitmap
.bmHeight
, AllPlanes
, ZPixmap
);
614 dibImage
= DIB_DIBmpToImage( &info
->bmiHeader
, bits
);
616 for (y
= 0; y
< lines
; y
++)
618 for (x
= 0; x
< info
->bmiHeader
.biWidth
; x
++)
620 XPutPixel( dibImage
, x
, y
,
621 XGetPixel(bmpImage
, x
, bmp
->bitmap
.bmHeight
-startscan
-y
-1) );
626 dibImage
->data
= NULL
;
627 XDestroyImage( dibImage
);
628 XDestroyImage( bmpImage
);
634 /***********************************************************************
635 * CreateDIBitmap (GDI.442)
637 HBITMAP
CreateDIBitmap( HDC hdc
, BITMAPINFOHEADER
* header
, DWORD init
,
638 LPSTR bits
, BITMAPINFO
* data
, WORD coloruse
)
642 handle
= CreateCompatibleBitmap( hdc
, header
->biWidth
, header
->biHeight
);
643 if (!handle
) return 0;
644 if (init
== CBM_INIT
) SetDIBits( hdc
, handle
, 0, header
->biHeight
,
645 bits
, data
, coloruse
);
649 /***********************************************************************
652 BOOL
DrawIcon(HDC hDC
, short x
, short y
, HICON hIcon
)
659 dprintf_icon(stddeb
,"DrawIcon(%04X, %d, %d, %04X) \n", hDC
, x
, y
, hIcon
);
660 if (hIcon
== (HICON
)NULL
) return FALSE
;
661 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
662 GetObject(lpico
->hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
663 dprintf_icon(stddeb
,"DrawIcon / x=%d y=%d\n", x
, y
);
664 dprintf_icon(stddeb
,"DrawIcon / icon Width=%d\n",
665 (int)lpico
->descriptor
.Width
);
666 dprintf_icon(stddeb
,"DrawIcon / icon Height=%d\n",
667 (int)lpico
->descriptor
.Height
);
668 dprintf_icon(stddeb
,"DrawIcon / icon ColorCount=%d\n",
669 (int)lpico
->descriptor
.ColorCount
);
670 dprintf_icon(stddeb
,"DrawIcon / icon icoDIBSize=%lX\n",
671 (DWORD
)lpico
->descriptor
.icoDIBSize
);
672 dprintf_icon(stddeb
,"DrawIcon / icon icoDIBOffset=%lX\n",
673 (DWORD
)lpico
->descriptor
.icoDIBOffset
);
674 dprintf_icon(stddeb
,"DrawIcon / bitmap bmWidth=%d bmHeight=%d\n",
675 bm
.bmWidth
, bm
.bmHeight
);
676 hMemDC
= CreateCompatibleDC(hDC
);
678 hBitTemp
= SelectObject(hMemDC
, lpico
->hBitmap
);
679 BitBlt(hDC
, x
, y
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCCOPY
);
680 SelectObject(hMemDC
, lpico
->hBitMask
);
681 BitBlt(hDC
, x
, y
+ bm
.bmHeight
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCCOPY
);
683 hBitTemp
= SelectObject(hMemDC
, lpico
->hBitMask
);
684 BitBlt(hDC
, x
, y
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCAND
);
685 SelectObject(hMemDC
, lpico
->hBitmap
);
686 BitBlt(hDC
, x
, y
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCPAINT
);
688 SelectObject( hMemDC
, hBitTemp
);