2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
10 #include <X11/Xutil.h>
19 extern void CLIPPING_UpdateGCRegion(DC
* );
21 /***********************************************************************
22 * DIB_GetImageWidthBytes
24 * Return the width of an X image in bytes
26 int DIB_GetImageWidthBytes( int width
, int depth
)
32 case 1: words
= (width
+ 31) / 32; break;
33 case 4: words
= (width
+ 7) / 8; break;
34 case 8: words
= (width
+ 3) / 4; break;
36 case 16: words
= (width
+ 1) / 2; break;
37 case 24: words
= (width
* 3 + 3)/4; break;
40 fprintf(stderr
, "DIB: unsupported depth %d.\n", depth
);
49 /***********************************************************************
52 * Return the size of the bitmap info structure.
54 int DIB_BitmapInfoSize( BITMAPINFO
* info
, WORD coloruse
)
58 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
60 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
61 colors
= (core
->bcBitCount
!= 24) ? 1 << core
->bcBitCount
: 0;
62 return sizeof(BITMAPCOREHEADER
) + colors
*
63 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
65 else /* assume BITMAPINFOHEADER */
67 colors
= info
->bmiHeader
.biClrUsed
;
68 if (!colors
&& (info
->bmiHeader
.biBitCount
!= 24))
69 colors
= 1 << info
->bmiHeader
.biBitCount
;
70 return sizeof(BITMAPINFOHEADER
) + colors
*
71 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
76 /***********************************************************************
79 * Get the info from a bitmap header.
80 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
82 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
83 DWORD
*height
, WORD
*bpp
)
85 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
87 *width
= header
->biWidth
;
88 *height
= header
->biHeight
;
89 *bpp
= header
->biBitCount
;
92 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
94 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
95 *width
= core
->bcWidth
;
96 *height
= core
->bcHeight
;
97 *bpp
= core
->bcBitCount
;
100 fprintf( stderr
, "DIB_GetBitmapInfo: wrong size (%ld) for header\n",
106 /***********************************************************************
109 * Build the color map from the bitmap palette. Should not be called
110 * for a 24-bit deep bitmap.
112 static int *DIB_BuildColorMap( DC
*dc
, WORD coloruse
, WORD depth
,
120 if ((isInfo
= (info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))))
122 colors
= info
->bmiHeader
.biClrUsed
;
123 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
124 colorPtr
= (WORD
*)info
->bmiColors
;
126 else /* assume BITMAPCOREINFO */
128 colors
= 1 << ((BITMAPCOREHEADER
*)&info
->bmiHeader
)->bcBitCount
;
129 colorPtr
= (WORD
*)((BITMAPCOREINFO
*)info
)->bmciColors
;
131 if (!(colorMapping
= (int *)malloc( colors
* sizeof(int) ))) return NULL
;
133 if (coloruse
== DIB_RGB_COLORS
)
137 RGBQUAD
* rgb
= (RGBQUAD
*)colorPtr
;
139 if (depth
== 1) /* Monochrome */
140 for (i
= 0; i
< colors
; i
++, rgb
++)
141 colorMapping
[i
] = (rgb
->rgbRed
+ rgb
->rgbGreen
+
142 rgb
->rgbBlue
> 255*3/2);
144 for (i
= 0; i
< colors
; i
++, rgb
++)
145 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbRed
,
151 RGBTRIPLE
* rgb
= (RGBTRIPLE
*)colorPtr
;
153 if (depth
== 1) /* Monochrome */
154 for (i
= 0; i
< colors
; i
++, rgb
++)
155 colorMapping
[i
] = (rgb
->rgbtRed
+ rgb
->rgbtGreen
+
156 rgb
->rgbtBlue
> 255*3/2);
158 for (i
= 0; i
< colors
; i
++, rgb
++)
159 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbtRed
,
164 else /* DIB_PAL_COLORS */
166 for (i
= 0; i
< colors
; i
++, colorPtr
++)
167 colorMapping
[i
] = COLOR_ToPhysical( dc
, PALETTEINDEX(*colorPtr
) );
173 /***********************************************************************
176 * SetDIBits for a 1-bit deep DIB.
178 static void DIB_SetImageBits_1( DWORD lines
, BYTE
*bits
, DWORD width
,
179 int *colors
, XImage
*bmpImage
)
184 if (!(width
& 31)) pad
= 0;
185 else pad
= ((32 - (width
& 31)) + 7) / 8;
189 for (i
= width
/8, x
= 0; (i
> 0); i
--)
192 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
193 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 6) & 1] );
194 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 5) & 1] );
195 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 4) & 1] );
196 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 3) & 1] );
197 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 2) & 1] );
198 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 1) & 1] );
199 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 1] );
204 case 7: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
205 case 6: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
206 case 5: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
207 case 4: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
208 case 3: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
209 case 2: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
210 case 1: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
217 /***********************************************************************
220 * SetDIBits for a 4-bit deep DIB.
222 static void DIB_SetImageBits_4( DWORD lines
, BYTE
*bits
, DWORD width
,
223 int *colors
, XImage
*bmpImage
)
228 if (!(width
& 7)) pad
= 0;
229 else pad
= ((8 - (width
& 7)) + 1) / 2;
233 for (i
= width
/2, x
= 0; i
> 0; i
--)
236 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 4] );
237 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 0x0f] );
239 if (width
& 1) XPutPixel( bmpImage
, x
, lines
, colors
[*bits
>> 4] );
244 #define check_xy(x,y) \
251 /***********************************************************************
252 * DIB_SetImageBits_RLE4
254 * SetDIBits for a 4-bit deep compressed DIB.
256 static void DIB_SetImageBits_RLE4( DWORD lines
, BYTE
*bits
, DWORD width
,
257 int *colors
, XImage
*bmpImage
)
259 int x
= 0, c
, length
;
263 while ((int)lines
>= 0)
266 if (length
) { /* encoded */
269 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
273 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
285 case 1: /* eopicture */
293 default: /* absolute */
296 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
300 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
304 if ((bits
- begin
) & 1)
311 /***********************************************************************
314 * SetDIBits for an 8-bit deep DIB.
316 static void DIB_SetImageBits_8( DWORD lines
, BYTE
*bits
, DWORD width
,
317 int *colors
, XImage
*bmpImage
)
320 BYTE pad
= (4 - (width
& 3)) & 3;
324 for (x
= 0; x
< width
; x
++)
325 XPutPixel( bmpImage
, x
, lines
, colors
[*bits
++] );
330 /***********************************************************************
331 * DIB_SetImageBits_RLE8
333 * SetDIBits for an 8-bit deep compressed DIB.
335 * This function rewritten 941113 by James Youngman. WINE blew out when I
336 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
338 * This was because the algorithm assumed that all RLE8 bitmaps end with the
339 * 'End of bitmap' escape code. This code is very much laxer in what it
340 * allows to end the expansion. Possibly too lax. See the note by
341 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
342 * bitmap should end with RleEnd, but on the other hand, software exists
343 * that produces ones that don't and Windows 3.1 doesn't complain a bit
346 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
347 * James A. Youngman <mbcstjy@afs.man.ac.uk>
351 enum Rle8_EscapeCodes
354 * Apologies for polluting your file's namespace...
356 RleEol
= 0, /* End of line */
357 RleEnd
= 1, /* End of bitmap */
358 RleDelta
= 2 /* Delta */
361 static void DIB_SetImageBits_RLE8( DWORD lines
, BYTE
*bits
, DWORD width
,
362 int *colors
, XImage
*bmpImage
)
364 int x
; /* X-positon on each line. Increases. */
365 int line
; /* Line #. Starts at lines-1, decreases */
366 BYTE
*pIn
= bits
; /* Pointer to current position in bits */
367 BYTE length
; /* The length pf a run */
368 BYTE color_index
; /* index into colors[] as read from bits */
369 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
370 WORD color
; /* value of colour[color_index] */
372 if (lines
== 0) /* Let's hope this doesn't happen. */
376 * Note that the bitmap data is stored by Windows starting at the
377 * bottom line of the bitmap and going upwards. Within each line,
378 * the data is stored left-to-right. That's the reason why line
379 * goes from lines-1 to 0. [JAY]
389 * If the length byte is not zero (which is the escape value),
390 * We have a run of length pixels all the same colour. The colour
391 * index is stored next.
393 * If the length byte is zero, we need to read the next byte to
394 * know what to do. [JAY]
399 * [Run-Length] Encoded mode
401 color_index
= (*pIn
++); /* Get the colour index. */
402 color
= colors
[color_index
];
405 XPutPixel(bmpImage
, x
++, line
, color
);
410 * Escape codes (may be an absolute sequence though)
412 escape_code
= (*pIn
++);
415 case RleEol
: /* =0, end of line */
422 case RleEnd
: /* =1, end of bitmap */
425 * Not all RLE8 bitmaps end with this
426 * code. For example, Paint Shop Pro
427 * produces some that don't. That's (I think)
428 * what caused the previous implementation to
431 line
=-1; /* Cause exit from do loop. */
435 case RleDelta
: /* =2, a delta */
438 * Note that deltaing to line 0
439 * will cause an exit from the loop,
440 * which may not be what is intended.
441 * The fact that there is a delta in the bits
442 * almost certainly implies that there is data
443 * to follow. You may feel that we should
444 * jump to the top of the loop to avoid exiting
447 * TODO: Decide what to do here in that case. [JAY]
453 dprintf_bitmap(stddeb
,
454 "DIB_SetImageBits_RLE8(): "
455 "Delta to last line of bitmap "
456 "(wrongly?) causes loop exit\n");
461 default: /* >2, switch to absolute mode */
466 length
= escape_code
;
469 color_index
= (*pIn
++);
470 XPutPixel(bmpImage
, x
++, line
,
471 colors
[color_index
]);
475 * If you think for a moment you'll realise that the
476 * only time we could ever possibly read an odd
477 * number of bytes is when there is a 0x00 (escape),
478 * a value >0x02 (absolute mode) and then an odd-
479 * length run. Therefore this is the only place we
480 * need to worry about it. Everywhere else the
481 * bytes are always read in pairs. [JAY]
484 pIn
++; /* Throw away the pad byte. */
487 } /* switch (escape_code) : Escape sequence */
488 } /* process either an encoded sequence or an escape sequence */
490 /* We expect to come here more than once per line. */
491 } while (line
>= 0); /* Do this until the bitmap is filled */
494 * Everybody comes here at the end.
495 * Check how we exited the loop and print a message if it's a bit odd.
498 if ( (*(pIn
-2) != 0/*escape*/) || (*(pIn
-1)!= RleEnd
) )
500 dprintf_bitmap(stddeb
, "DIB_SetImageBits_RLE8(): End-of-bitmap "
501 "without (strictly) proper escape code. Last two "
502 "bytes were: %02X %02X.\n",
509 /***********************************************************************
510 * DIB_SetImageBits_24
512 * SetDIBits for a 24-bit deep DIB.
514 static void DIB_SetImageBits_24( DWORD lines
, BYTE
*bits
, DWORD width
,
515 DC
*dc
, XImage
*bmpImage
)
518 BYTE pad
= (4 - ((width
*3) & 3)) & 3;
520 /* "bits" order is reversed for some reason */
524 for (x
= 0; x
< width
; x
++, bits
+= 3)
525 XPutPixel( bmpImage
, x
, lines
,
526 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])) );
533 /***********************************************************************
536 * Transfer the bits to an X image.
537 * Helper function for SetDIBits() and SetDIBitsToDevice().
539 static int DIB_SetImageBits( DC
*dc
, DWORD lines
, WORD depth
, LPSTR bits
,
540 DWORD infoWidth
, WORD infoBpp
,
541 BITMAPINFO
*info
, WORD coloruse
,
542 Drawable drawable
, GC gc
, int xSrc
, int ySrc
,
543 int xDest
, int yDest
, int width
, int height
)
547 DWORD compression
= 0;
549 if (info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
550 compression
= info
->bmiHeader
.biCompression
;
552 /* Build the color mapping table */
554 if (infoBpp
== 24) colorMapping
= NULL
;
556 if (!(colorMapping
= DIB_BuildColorMap( dc
, coloruse
, depth
, info
)))
559 if( dc
->w
.flags
& DC_DIRTY
) CLIPPING_UpdateGCRegion(dc
);
561 /* Transfer the pixels */
562 XCREATEIMAGE(bmpImage
, infoWidth
, lines
, depth
);
567 DIB_SetImageBits_1( lines
, bits
, infoWidth
,
568 colorMapping
, bmpImage
);
571 if (compression
) DIB_SetImageBits_RLE4( lines
, bits
, infoWidth
,
572 colorMapping
, bmpImage
);
573 else DIB_SetImageBits_4( lines
, bits
, infoWidth
,
574 colorMapping
, bmpImage
);
577 if (compression
) DIB_SetImageBits_RLE8( lines
, bits
, infoWidth
,
578 colorMapping
, bmpImage
);
579 else DIB_SetImageBits_8( lines
, bits
, infoWidth
,
580 colorMapping
, bmpImage
);
583 DIB_SetImageBits_24( lines
, bits
, infoWidth
, dc
, bmpImage
);
586 fprintf( stderr
, "Invalid depth %d for SetDIBits!\n", infoBpp
);
589 if (colorMapping
) free(colorMapping
);
590 XPutImage( display
, drawable
, gc
, bmpImage
, xSrc
, ySrc
,
591 xDest
, yDest
, width
, height
);
592 XDestroyImage( bmpImage
);
597 /***********************************************************************
598 * StretchDIBits16 (GDI.439)
600 INT16
StretchDIBits16( HDC16 hdc
, INT16 xDst
, INT16 yDst
, INT16 widthDst
,
601 INT16 heightDst
, INT16 xSrc
, INT16 ySrc
, INT16 widthSrc
,
602 INT16 heightSrc
, const VOID
*bits
,
603 const BITMAPINFO
*info
, UINT16 wUsage
, DWORD dwRop
)
605 return (INT16
)StretchDIBits32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
606 xSrc
, ySrc
, widthSrc
, heightSrc
, bits
,
607 info
, wUsage
, dwRop
);
611 /***********************************************************************
612 * StretchDIBits32 (GDI32.351)
614 INT32
StretchDIBits32( HDC32 hdc
, INT32 xDst
, INT32 yDst
, INT32 widthDst
,
615 INT32 heightDst
, INT32 xSrc
, INT32 ySrc
, INT32 widthSrc
,
616 INT32 heightSrc
, const void *bits
,
617 const BITMAPINFO
*info
, UINT32 wUsage
, DWORD dwRop
)
619 HBITMAP32 hBitmap
, hOldBitmap
;
622 hBitmap
= CreateDIBitmap32( hdc
, &info
->bmiHeader
, CBM_INIT
,
623 bits
, info
, wUsage
);
624 hdcMem
= CreateCompatibleDC32( hdc
);
625 hOldBitmap
= SelectObject32( hdcMem
, hBitmap
);
626 StretchBlt32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
627 hdcMem
, xSrc
, ySrc
, widthSrc
, heightSrc
, dwRop
);
628 SelectObject32( hdcMem
, hOldBitmap
);
629 DeleteDC32( hdcMem
);
630 DeleteObject32( hBitmap
);
635 /***********************************************************************
636 * SetDIBits16 (GDI.440)
638 INT16
SetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
639 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
642 return SetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
646 /***********************************************************************
647 * SetDIBits32 (GDI32.312)
649 INT32
SetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
650 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
658 /* Check parameters */
660 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
663 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
666 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
668 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
, &bpp
) == -1)
670 if (!lines
|| (startscan
>= (WORD
)height
)) return 0;
671 if (startscan
+ lines
> height
) lines
= height
- startscan
;
673 return CallTo32_LargeStack( (int(*)())DIB_SetImageBits
, 16,
674 dc
, lines
, bmp
->bitmap
.bmBitsPixel
,
675 bits
, width
, bpp
, info
,
676 coloruse
, bmp
->pixmap
, BITMAP_GC(bmp
), 0, 0, 0,
677 height
- startscan
- lines
,
678 bmp
->bitmap
.bmWidth
, lines
);
682 /***********************************************************************
683 * SetDIBitsToDevice16 (GDI.443)
685 INT16
SetDIBitsToDevice16( HDC16 hdc
, INT16 xDest
, INT16 yDest
, INT16 cx
,
686 INT16 cy
, INT16 xSrc
, INT16 ySrc
, UINT16 startscan
,
687 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
690 return SetDIBitsToDevice32( hdc
, xDest
, yDest
, cx
, cy
, xSrc
, ySrc
,
691 startscan
, lines
, bits
, info
, coloruse
);
695 /***********************************************************************
696 * SetDIBitsToDevice32 (GDI32.313)
698 INT32
SetDIBitsToDevice32( HDC32 hdc
, INT32 xDest
, INT32 yDest
, DWORD cx
,
699 DWORD cy
, INT32 xSrc
, INT32 ySrc
, UINT32 startscan
,
700 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
707 /* Check parameters */
709 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
712 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
715 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
, &bpp
) == -1)
717 if (!lines
|| (startscan
>= height
)) return 0;
718 if (startscan
+ lines
> height
) lines
= height
- startscan
;
719 if (ySrc
< startscan
) ySrc
= startscan
;
720 else if (ySrc
>= startscan
+ lines
) return 0;
721 if (xSrc
>= width
) return 0;
722 if (ySrc
+ cy
>= startscan
+ lines
) cy
= startscan
+ lines
- ySrc
;
723 if (xSrc
+ cx
>= width
) cx
= width
- xSrc
;
724 if (!cx
|| !cy
) return 0;
726 DC_SetupGCForText( dc
); /* To have the correct colors */
727 XSetFunction( display
, dc
->u
.x
.gc
, DC_XROPfunction
[dc
->w
.ROPmode
-1] );
728 return CallTo32_LargeStack( (int(*)())DIB_SetImageBits
, 16,
729 dc
, lines
, dc
->w
.bitsPerPixel
, bits
, width
,
731 dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
732 xSrc
, ySrc
- startscan
,
733 dc
->w
.DCOrgX
+ XLPTODP( dc
, xDest
),
734 dc
->w
.DCOrgY
+ YLPTODP( dc
, yDest
),
740 /***********************************************************************
741 * GetDIBits16 (GDI.441)
743 INT16
GetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
744 UINT16 lines
, LPSTR bits
, BITMAPINFO
* info
,
747 return GetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
751 /***********************************************************************
752 * GetDIBits32 (GDI32.170)
754 INT32
GetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
755 UINT32 lines
, LPSTR bits
, BITMAPINFO
* info
,
760 PALETTEENTRY
* palEntry
;
761 PALETTEOBJ
* palette
;
762 XImage
* bmpImage
, * dibImage
;
765 if (!lines
) return 0;
766 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
769 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
772 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
774 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
777 /* Transfer color info */
779 palEntry
= palette
->logpalette
.palPalEntry
;
780 for (i
= 0; i
< info
->bmiHeader
.biClrUsed
; i
++, palEntry
++)
782 if (coloruse
== DIB_RGB_COLORS
)
784 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
785 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
786 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
787 info
->bmiColors
[i
].rgbReserved
= 0;
789 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
795 int pad
, yend
, xend
= bmp
->bitmap
.bmWidth
;
797 dprintf_bitmap(stddeb
, "GetDIBits: %u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
798 lines
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
799 (int)info
->bmiHeader
.biWidth
, (int)info
->bmiHeader
.biHeight
, startscan
);
801 /* adjust number of scanlines to copy */
803 if( lines
> info
->bmiHeader
.biHeight
) lines
= info
->bmiHeader
.biHeight
;
804 yend
= startscan
+ lines
;
805 if( startscan
>= bmp
->bitmap
.bmHeight
)
807 if( yend
> bmp
->bitmap
.bmHeight
) yend
= bmp
->bitmap
.bmHeight
;
809 /* adjust scanline width */
811 pad
= info
->bmiHeader
.biWidth
- bmp
->bitmap
.bmWidth
;
814 /* bitmap is wider than DIB, copy only a part */
816 pad
= 0; xend
= info
->bmiHeader
.biWidth
;
819 bmpImage
= (XImage
*)CallTo32_LargeStack( (int (*)())XGetImage
, 8,
820 display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
,
821 bmp
->bitmap
.bmHeight
, AllPlanes
, ZPixmap
);
823 switch( info
->bmiHeader
.biBitCount
)
827 pad
+= (4 - (info
->bmiHeader
.biWidth
& 3)) & 3;
828 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
830 for( x
= 0; x
< xend
; x
++ )
831 *bbits
++ = XGetPixel( bmpImage
, x
, y
);
836 /* add more bpp-specific shortcuts here */
840 dibImage
= XCreateImage(display
, DefaultVisualOfScreen( screen
),
841 info
->bmiHeader
.biBitCount
, ZPixmap
, 0, bits
,
842 info
->bmiHeader
.biWidth
, info
->bmiHeader
.biHeight
,
843 32, DIB_GetImageWidthBytes( info
->bmiHeader
.biWidth
,
844 info
->bmiHeader
.biBitCount
) );
847 extern void _XInitImageFuncPtrs( XImage
* );
849 dibImage
->byte_order
= MSBFirst
;
850 dibImage
->bitmap_bit_order
= MSBFirst
;
851 dibImage
->bitmap_unit
= 16;
852 _XInitImageFuncPtrs( dibImage
);
854 for (y
= yend
- 1; (int)y
>= (int)startscan
; y
--)
855 for (x
= 0; x
< xend
; x
++)
856 XPutPixel( dibImage
, x
, yend
- y
+ 1,
857 XGetPixel( bmpImage
, x
, y
));
858 dibImage
->data
= NULL
;
859 XDestroyImage( dibImage
);
863 XDestroyImage( bmpImage
);
865 info
->bmiHeader
.biCompression
= 0;
867 else if( info
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
) )
869 /* fill in struct members */
871 info
->bmiHeader
.biWidth
= bmp
->bitmap
.bmWidth
;
872 info
->bmiHeader
.biHeight
= bmp
->bitmap
.bmHeight
;
873 info
->bmiHeader
.biPlanes
= 1;
874 info
->bmiHeader
.biBitCount
= bmp
->bitmap
.bmBitsPixel
;
875 info
->bmiHeader
.biSizeImage
= bmp
->bitmap
.bmHeight
*
876 DIB_GetImageWidthBytes( bmp
->bitmap
.bmWidth
,
877 bmp
->bitmap
.bmBitsPixel
);
878 info
->bmiHeader
.biCompression
= 0;
885 /***********************************************************************
886 * CreateDIBitmap16 (GDI.442)
888 HBITMAP16
CreateDIBitmap16( HDC16 hdc
, const BITMAPINFOHEADER
* header
,
889 DWORD init
, LPCVOID bits
, const BITMAPINFO
* data
,
892 return CreateDIBitmap32( hdc
, header
, init
, bits
, data
, coloruse
);
896 /***********************************************************************
897 * CreateDIBitmap32 (GDI32.37)
899 HBITMAP32
CreateDIBitmap32( HDC32 hdc
, const BITMAPINFOHEADER
*header
,
900 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
908 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
) == -1) return 0;
910 /* Check if we should create a monochrome or color bitmap. */
911 /* We create a monochrome bitmap only if it has exactly 2 */
912 /* colors, which are either black or white, nothing else. */
913 /* In all other cases, we create a color bitmap. */
915 if (bpp
!= 1) fColor
= TRUE
;
916 else if ((coloruse
!= DIB_RGB_COLORS
) ||
917 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
920 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
922 RGBQUAD
*rgb
= data
->bmiColors
;
923 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
924 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
927 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
928 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
932 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
934 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
935 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
936 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
939 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
940 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
946 fprintf( stderr
, "CreateDIBitmap: wrong size (%ld) for data\n",
947 data
->bmiHeader
.biSize
);
952 /* Now create the bitmap */
954 handle
= fColor
? CreateCompatibleBitmap32( hdc
, width
, height
) :
955 CreateBitmap32( width
, height
, 1, 1, NULL
);
956 if (!handle
) return 0;
958 if (init
== CBM_INIT
)
959 SetDIBits32( hdc
, handle
, 0, height
, bits
, data
, coloruse
);