2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
10 #include <X11/Xutil.h>
20 /***********************************************************************
21 * DIB_GetImageWidthBytes
23 * Return the width of an X image in bytes
25 int DIB_GetImageWidthBytes( int width
, int depth
)
31 case 1: words
= (width
+ 31) / 32; break;
32 case 4: words
= (width
+ 7) / 8; break;
33 case 8: words
= (width
+ 3) / 4; break;
35 case 16: words
= (width
+ 1) / 2; break;
36 case 24: words
= width
; break;
38 fprintf(stderr
, "DIB: unsupported depth %d.\n", depth
);
45 /***********************************************************************
48 * Return the size of the bitmap info structure.
50 int DIB_BitmapInfoSize( BITMAPINFO
* info
, WORD coloruse
)
52 int size
= info
->bmiHeader
.biClrUsed
;
53 if (!size
&& (info
->bmiHeader
.biBitCount
!= 24))
54 size
= 1 << info
->bmiHeader
.biBitCount
;
55 if (coloruse
== DIB_RGB_COLORS
)
56 size
= info
->bmiHeader
.biSize
+ size
* sizeof(RGBQUAD
);
58 size
= info
->bmiHeader
.biSize
+ size
* sizeof(WORD
);
63 /***********************************************************************
66 * Create an XImage pointing to the bitmap data.
68 static XImage
*DIB_DIBmpToImage( BITMAPINFOHEADER
* bmp
, void * bmpData
)
70 extern void _XInitImageFuncPtrs( XImage
* );
73 image
= XCreateImage(display
, DefaultVisualOfScreen( screen
),
74 bmp
->biBitCount
, ZPixmap
, 0, bmpData
,
75 bmp
->biWidth
, bmp
->biHeight
, 32,
76 DIB_GetImageWidthBytes(bmp
->biWidth
,bmp
->biBitCount
));
78 image
->byte_order
= MSBFirst
;
79 image
->bitmap_bit_order
= MSBFirst
;
80 image
->bitmap_unit
= 16;
81 _XInitImageFuncPtrs(image
);
86 /***********************************************************************
89 * SetDIBits for a 1-bit deep DIB.
91 static void DIB_SetImageBits_1( WORD lines
, BYTE
*bits
, WORD width
,
92 int *colors
, XImage
*bmpImage
)
97 if (!(width
& 31)) pad
= 0;
98 else pad
= ((32 - (width
& 31)) + 7) / 8;
102 for (i
= width
/8, x
= 0; (i
> 0); i
--)
105 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
106 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 6) & 1] );
107 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 5) & 1] );
108 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 4) & 1] );
109 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 3) & 1] );
110 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 2) & 1] );
111 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 1) & 1] );
112 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 1] );
117 case 7: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
118 case 6: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
119 case 5: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
120 case 4: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
121 case 3: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
122 case 2: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
123 case 1: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
130 /***********************************************************************
133 * SetDIBits for a 4-bit deep DIB.
135 static void DIB_SetImageBits_4( WORD lines
, BYTE
*bits
, WORD width
,
136 int *colors
, XImage
*bmpImage
)
141 if (!(width
& 7)) pad
= 0;
142 else pad
= ((8 - (width
& 7)) + 1) / 2;
146 for (i
= width
/2, x
= 0; i
> 0; i
--)
149 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 4] );
150 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 0x0f] );
152 if (width
& 1) XPutPixel( bmpImage
, x
, lines
, colors
[*bits
>> 4] );
157 #define check_xy(x,y) \
164 /***********************************************************************
165 * DIB_SetImageBits_RLE4
167 * SetDIBits for a 4-bit deep compressed DIB.
169 static void DIB_SetImageBits_RLE4( WORD lines
, BYTE
*bits
, WORD width
,
170 int *colors
, XImage
*bmpImage
)
172 int x
= 0, c
, length
;
178 if (length
) { /* encoded */
181 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
185 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
197 case 1: /* eopicture */
205 default: /* absolute */
208 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
212 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
216 if ((bits
- begin
) & 1)
223 /***********************************************************************
226 * SetDIBits for an 8-bit deep DIB.
228 static void DIB_SetImageBits_8( WORD lines
, BYTE
*bits
, WORD width
,
229 int *colors
, XImage
*bmpImage
)
232 BYTE pad
= (4 - (width
& 3)) & 3;
236 for (x
= 0; x
< width
; x
++)
237 XPutPixel( bmpImage
, x
, lines
, colors
[*bits
++] );
242 /***********************************************************************
243 * DIB_SetImageBits_RLE8
245 * SetDIBits for an 8-bit deep compressed DIB.
247 * This function rewritten 941113 by James Youngman. WINE blew out when I
248 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
250 * This was because the algorithm assumed that all RLE8 bitmaps end with the
251 * 'End of bitmap' escape code. This code is very much laxer in what it
252 * allows to end the expansion. Possibly too lax. See the note by
253 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
254 * bitmap should end with RleEnd, but on the other hand, software exists
255 * that produces ones that don't and Windows 3.1 doesn't complain a bit
258 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
259 * James A. Youngman <mbcstjy@afs.man.ac.uk>
263 enum Rle8_EscapeCodes
266 * Apologies for polluting your file's namespace...
268 RleEol
= 0, /* End of line */
269 RleEnd
= 1, /* End of bitmap */
270 RleDelta
= 2 /* Delta */
273 static void DIB_SetImageBits_RLE8(WORD lines
,
279 int x
; /* X-positon on each line. Increases. */
280 int line
; /* Line #. Starts at lines-1, decreases */
281 BYTE
*pIn
= bits
; /* Pointer to current position in bits */
282 BYTE length
; /* The length pf a run */
283 BYTE color_index
; /* index into colors[] as read from bits */
284 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
285 WORD color
; /* value of colour[color_index] */
287 if (lines
== 0) /* Let's hope this doesn't happen. */
291 * Note that the bitmap data is stored by Windows starting at the
292 * bottom line of the bitmap and going upwards. Within each line,
293 * the data is stored left-to-right. That's the reason why line
294 * goes from lines-1 to 0. [JAY]
304 * If the length byte is not zero (which is the escape value),
305 * We have a run of length pixels all the same colour. The colour
306 * index is stored next.
308 * If the length byte is zero, we need to read the next byte to
309 * know what to do. [JAY]
314 * [Run-Length] Encoded mode
316 color_index
= (*pIn
++); /* Get the colour index. */
317 color
= colors
[color_index
];
320 XPutPixel(bmpImage
, x
++, line
, color
);
325 * Escape codes (may be an absolute sequence though)
327 escape_code
= (*pIn
++);
330 case RleEol
: /* =0, end of line */
337 case RleEnd
: /* =1, end of bitmap */
340 * Not all RLE8 bitmaps end with this
341 * code. For example, Paint Shop Pro
342 * produces some that don't. That's (I think)
343 * what caused the previous implementation to
346 line
=0; /* Cause exit from do loop. */
349 case RleDelta
: /* =2, a delta */
352 * Note that deltaing to line 0
353 * will cause an exit from the loop,
354 * which may not be what is intended.
355 * The fact that there is a delta in the bits
356 * almost certainly implies that there is data
357 * to follow. You may feel that we should
358 * jump to the top of the loop to avoid exiting
361 * TODO: Decide what to do here in that case. [JAY]
367 dprintf_bitmap(stddeb
,
368 "DIB_SetImageBits_RLE8(): "
369 "Delta to last line of bitmap "
370 "(wrongly?) causes loop exit\n");
375 default: /* >2, switch to absolute mode */
380 length
= escape_code
;
383 color_index
= (*pIn
++);
384 XPutPixel(bmpImage
, x
++, line
,
385 colors
[color_index
]);
389 * If you think for a moment you'll realise that the
390 * only time we could ever possibly read an odd
391 * number of bytes is when there is a 0x00 (escape),
392 * a value >0x02 (absolute mode) and then an odd-
393 * length run. Therefore this is the only place we
394 * need to worry about it. Everywhere else the
395 * bytes are always read in pairs. [JAY]
398 (*pIn
++); /* Get and throw away the pad byte. */
401 } /* switch (escape_code) : Escape sequence */
402 } /* process either an encoded sequence or an escape sequence */
404 /* We expect to come here more than once per line. */
405 } while (line
> 0); /* Do this until the bitmap is filled */
408 * Everybody comes here at the end.
409 * Check how we exited the loop and print a message if it's a bit odd.
412 if ( (*(pIn
-2) != 0/*escape*/) || (*(pIn
-1)!= RleEnd
) )
414 dprintf_bitmap(stddeb
, "DIB_SetImageBits_RLE8(): End-of-bitmap "
415 "without (strictly) proper escape code. Last two "
416 "bytes were: %02X %02X.\n",
423 /***********************************************************************
424 * DIB_SetImageBits_24
426 * SetDIBits for a 24-bit deep DIB.
428 static void DIB_SetImageBits_24( WORD lines
, BYTE
*bits
, WORD width
,
429 DC
*dc
, XImage
*bmpImage
)
432 BYTE pad
= (4 - ((width
*3) & 3)) & 3;
436 for (x
= 0; x
< width
; x
++, bits
+= 3)
438 XPutPixel( bmpImage
, x
, lines
,
439 COLOR_ToPhysical( dc
, RGB(bits
[0],bits
[1],bits
[2]) ));
446 /***********************************************************************
449 * Transfer the bits to an X image.
450 * Helper function for SetDIBits() and SetDIBitsToDevice().
452 static int DIB_SetImageBits( DC
*dc
, WORD lines
, WORD depth
, LPSTR bits
,
453 BITMAPINFO
*info
, WORD coloruse
,
454 Drawable drawable
, GC gc
, int xSrc
, int ySrc
,
455 int xDest
, int yDest
, int width
, int height
)
461 /* Build the color mapping table */
463 if (info
->bmiHeader
.biBitCount
== 24) colorMapping
= NULL
;
466 colors
= info
->bmiHeader
.biClrUsed
;
467 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
468 if (!(colorMapping
= (int *)malloc( colors
* sizeof(int) )))
470 if (coloruse
== DIB_RGB_COLORS
)
472 RGBQUAD
* rgbPtr
= info
->bmiColors
;
473 for (i
= 0; i
< colors
; i
++, rgbPtr
++)
474 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgbPtr
->rgbRed
,
480 WORD
* index
= (WORD
*)info
->bmiColors
;
481 for (i
= 0; i
< colors
; i
++, index
++)
482 colorMapping
[i
] = COLOR_ToPhysical( dc
, PALETTEINDEX(*index
) );
486 /* Transfer the pixels */
487 XCREATEIMAGE(bmpImage
, info
->bmiHeader
.biWidth
, lines
, depth
);
489 switch(info
->bmiHeader
.biBitCount
)
492 DIB_SetImageBits_1( lines
, bits
, info
->bmiHeader
.biWidth
,
493 colorMapping
, bmpImage
);
496 if (info
->bmiHeader
.biCompression
)
497 DIB_SetImageBits_RLE4( lines
, bits
, info
->bmiHeader
.biWidth
,
498 colorMapping
, bmpImage
);
500 DIB_SetImageBits_4( lines
, bits
, info
->bmiHeader
.biWidth
,
501 colorMapping
, bmpImage
);
504 if (info
->bmiHeader
.biCompression
)
505 DIB_SetImageBits_RLE8( lines
, bits
, info
->bmiHeader
.biWidth
,
506 colorMapping
, bmpImage
);
508 DIB_SetImageBits_8( lines
, bits
, info
->bmiHeader
.biWidth
,
509 colorMapping
, bmpImage
);
512 DIB_SetImageBits_24( lines
, bits
, info
->bmiHeader
.biWidth
,
516 if (colorMapping
) free(colorMapping
);
518 XPutImage( display
, drawable
, gc
, bmpImage
, xSrc
, ySrc
,
519 xDest
, yDest
, width
, height
);
520 XDestroyImage( bmpImage
);
525 /***********************************************************************
526 * SetDIBits (GDI.440)
528 int SetDIBits( HDC hdc
, HBITMAP hbitmap
, WORD startscan
, WORD lines
,
529 LPSTR bits
, BITMAPINFO
* info
, WORD coloruse
)
534 /* Check parameters */
536 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
537 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
539 if (!lines
|| (startscan
>= (WORD
)info
->bmiHeader
.biHeight
)) return 0;
540 if (startscan
+lines
> info
->bmiHeader
.biHeight
)
541 lines
= info
->bmiHeader
.biHeight
- startscan
;
543 return DIB_SetImageBits( dc
, lines
, bmp
->bitmap
.bmBitsPixel
,
544 bits
, info
, coloruse
, bmp
->pixmap
, BITMAP_GC(bmp
),
545 0, 0, 0, startscan
, bmp
->bitmap
.bmWidth
, lines
);
549 /***********************************************************************
550 * SetDIBitsToDevice (GDI.443)
552 int SetDIBitsToDevice( HDC hdc
, short xDest
, short yDest
, WORD cx
, WORD cy
,
553 WORD xSrc
, WORD ySrc
, WORD startscan
, WORD lines
,
554 LPSTR bits
, BITMAPINFO
* info
, WORD coloruse
)
558 /* Check parameters */
560 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
561 if (!lines
|| (startscan
>= info
->bmiHeader
.biHeight
)) return 0;
562 if (startscan
+lines
> info
->bmiHeader
.biHeight
)
563 lines
= info
->bmiHeader
.biHeight
- startscan
;
564 if (ySrc
< startscan
) ySrc
= startscan
;
565 else if (ySrc
>= startscan
+lines
) return 0;
566 if (xSrc
>= info
->bmiHeader
.biWidth
) return 0;
567 if (ySrc
+cy
>= startscan
+lines
) cy
= startscan
+ lines
- ySrc
;
568 if (xSrc
+cx
>= info
->bmiHeader
.biWidth
) cx
= info
->bmiHeader
.biWidth
-xSrc
;
569 if (!cx
|| !cy
) return 0;
571 DC_SetupGCForText( dc
); /* To have the correct colors */
572 XSetFunction( display
, dc
->u
.x
.gc
, DC_XROPfunction
[dc
->w
.ROPmode
-1] );
573 return DIB_SetImageBits( dc
, lines
, dc
->w
.bitsPerPixel
,
574 bits
, info
, coloruse
,
575 dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
576 xSrc
, ySrc
- startscan
,
577 dc
->w
.DCOrgX
+ XLPTODP( dc
, xDest
),
578 dc
->w
.DCOrgY
+ YLPTODP( dc
, yDest
),
583 /***********************************************************************
584 * GetDIBits (GDI.441)
586 int GetDIBits( HDC hdc
, HBITMAP hbitmap
, WORD startscan
, WORD lines
,
587 LPSTR bits
, BITMAPINFO
* info
, WORD coloruse
)
591 PALETTEENTRY
* palEntry
;
592 PALETTEOBJ
* palette
;
593 XImage
* bmpImage
, * dibImage
;
596 if (!lines
) return 0;
597 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
598 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
600 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
603 /* Transfer color info */
605 palEntry
= palette
->logpalette
.palPalEntry
;
606 for (i
= 0; i
< info
->bmiHeader
.biClrUsed
; i
++, palEntry
++)
608 if (coloruse
== DIB_RGB_COLORS
)
610 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
611 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
612 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
613 info
->bmiColors
[i
].rgbReserved
= 0;
615 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
618 /* Transfer the pixels (very slow...) */
622 bmpImage
= XGetImage( display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
,
623 bmp
->bitmap
.bmHeight
, AllPlanes
, ZPixmap
);
624 dibImage
= DIB_DIBmpToImage( &info
->bmiHeader
, bits
);
626 for (y
= 0; y
< lines
; y
++)
628 for (x
= 0; x
< info
->bmiHeader
.biWidth
; x
++)
630 XPutPixel( dibImage
, x
, y
,
631 XGetPixel(bmpImage
, x
, bmp
->bitmap
.bmHeight
-startscan
-y
-1) );
636 dibImage
->data
= NULL
;
637 XDestroyImage( dibImage
);
638 XDestroyImage( bmpImage
);
644 /***********************************************************************
645 * CreateDIBitmap (GDI.442)
647 HBITMAP
CreateDIBitmap( HDC hdc
, BITMAPINFOHEADER
* header
, DWORD init
,
648 LPSTR bits
, BITMAPINFO
* data
, WORD coloruse
)
652 handle
= CreateCompatibleBitmap( hdc
, header
->biWidth
, header
->biHeight
);
653 if (!handle
) return 0;
654 if (init
== CBM_INIT
) SetDIBits( hdc
, handle
, 0, header
->biHeight
,
655 bits
, data
, coloruse
);
659 /***********************************************************************
662 BOOL
DrawIcon(HDC hDC
, short x
, short y
, HICON hIcon
)
669 dprintf_icon(stddeb
,"DrawIcon(%04X, %d, %d, %04X) \n", hDC
, x
, y
, hIcon
);
670 if (hIcon
== (HICON
)NULL
) return FALSE
;
671 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
672 GetObject(lpico
->hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
673 dprintf_icon(stddeb
,"DrawIcon / x=%d y=%d\n", x
, y
);
674 dprintf_icon(stddeb
,"DrawIcon / icon Width=%d\n",
675 (int)lpico
->descriptor
.Width
);
676 dprintf_icon(stddeb
,"DrawIcon / icon Height=%d\n",
677 (int)lpico
->descriptor
.Height
);
678 dprintf_icon(stddeb
,"DrawIcon / icon ColorCount=%d\n",
679 (int)lpico
->descriptor
.ColorCount
);
680 dprintf_icon(stddeb
,"DrawIcon / icon icoDIBSize=%lX\n",
681 (DWORD
)lpico
->descriptor
.icoDIBSize
);
682 dprintf_icon(stddeb
,"DrawIcon / icon icoDIBOffset=%lX\n",
683 (DWORD
)lpico
->descriptor
.icoDIBOffset
);
684 dprintf_icon(stddeb
,"DrawIcon / bitmap bmWidth=%d bmHeight=%d\n",
685 bm
.bmWidth
, bm
.bmHeight
);
686 hMemDC
= CreateCompatibleDC(hDC
);
688 hBitTemp
= SelectObject(hMemDC
, lpico
->hBitmap
);
689 BitBlt(hDC
, x
, y
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCCOPY
);
690 SelectObject(hMemDC
, lpico
->hBitMask
);
691 BitBlt(hDC
, x
, y
+ bm
.bmHeight
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCCOPY
);
693 hBitTemp
= SelectObject(hMemDC
, lpico
->hBitMask
);
694 BitBlt(hDC
, x
, y
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCAND
);
695 SelectObject(hMemDC
, lpico
->hBitmap
);
696 BitBlt(hDC
, x
, y
, bm
.bmWidth
, bm
.bmHeight
, hMemDC
, 0, 0, SRCPAINT
);
698 SelectObject( hMemDC
, hBitTemp
);