2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
7 #define NO_TRANSITION_TYPES /* This file is Win32-clean */
11 #include <X11/Xutil.h>
20 extern void CLIPPING_UpdateGCRegion(DC
* );
22 /***********************************************************************
23 * DIB_GetImageWidthBytes
25 * Return the width of an X image in bytes
27 int DIB_GetImageWidthBytes( int width
, int depth
)
33 case 1: words
= (width
+ 31) / 32; break;
34 case 4: words
= (width
+ 7) / 8; break;
35 case 8: words
= (width
+ 3) / 4; break;
37 case 16: words
= (width
+ 1) / 2; break;
38 case 24: words
= width
; break;
40 fprintf(stderr
, "DIB: unsupported depth %d.\n", depth
);
47 /***********************************************************************
50 * Return the size of the bitmap info structure.
52 int DIB_BitmapInfoSize( BITMAPINFO
* info
, WORD coloruse
)
56 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
58 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
59 colors
= (core
->bcBitCount
!= 24) ? 1 << core
->bcBitCount
: 0;
60 return sizeof(BITMAPCOREHEADER
) + colors
*
61 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
63 else /* assume BITMAPINFOHEADER */
65 colors
= info
->bmiHeader
.biClrUsed
;
66 if (!colors
&& (info
->bmiHeader
.biBitCount
!= 24))
67 colors
= 1 << info
->bmiHeader
.biBitCount
;
68 return sizeof(BITMAPINFOHEADER
) + colors
*
69 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
74 /***********************************************************************
77 * Create an XImage pointing to the bitmap data.
79 static XImage
*DIB_DIBmpToImage( BITMAPINFOHEADER
* bmp
, void * bmpData
)
81 extern void _XInitImageFuncPtrs( XImage
* );
84 image
= XCreateImage(display
, DefaultVisualOfScreen( screen
),
85 bmp
->biBitCount
, ZPixmap
, 0, bmpData
,
86 bmp
->biWidth
, bmp
->biHeight
, 32,
87 DIB_GetImageWidthBytes(bmp
->biWidth
,bmp
->biBitCount
));
89 image
->byte_order
= MSBFirst
;
90 image
->bitmap_bit_order
= MSBFirst
;
91 image
->bitmap_unit
= 16;
92 _XInitImageFuncPtrs(image
);
97 /***********************************************************************
100 * Get the info from a bitmap header.
101 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
103 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
104 DWORD
*height
, WORD
*bpp
)
106 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
108 *width
= header
->biWidth
;
109 *height
= header
->biHeight
;
110 *bpp
= header
->biBitCount
;
113 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
115 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
116 *width
= core
->bcWidth
;
117 *height
= core
->bcHeight
;
118 *bpp
= core
->bcBitCount
;
121 fprintf( stderr
, "DIB_GetBitmapInfo: wrong size (%ld) for header\n",
127 /***********************************************************************
130 * Build the color map from the bitmap palette. Should not be called
131 * for a 24-bit deep bitmap.
133 static int *DIB_BuildColorMap( DC
*dc
, WORD coloruse
, WORD depth
,
141 if ((isInfo
= (info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))))
143 colors
= info
->bmiHeader
.biClrUsed
;
144 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
145 colorPtr
= (WORD
*)info
->bmiColors
;
147 else /* assume BITMAPCOREINFO */
149 colors
= 1 << ((BITMAPCOREHEADER
*)&info
->bmiHeader
)->bcBitCount
;
150 colorPtr
= (WORD
*)((BITMAPCOREINFO
*)info
)->bmciColors
;
152 if (!(colorMapping
= (int *)malloc( colors
* sizeof(int) ))) return NULL
;
154 if (coloruse
== DIB_RGB_COLORS
)
158 RGBQUAD
* rgb
= (RGBQUAD
*)colorPtr
;
160 if (depth
== 1) /* Monochrome */
161 for (i
= 0; i
< colors
; i
++, rgb
++)
162 colorMapping
[i
] = (rgb
->rgbRed
+ rgb
->rgbGreen
+
163 rgb
->rgbBlue
> 255*3/2);
165 for (i
= 0; i
< colors
; i
++, rgb
++)
166 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbRed
,
172 RGBTRIPLE
* rgb
= (RGBTRIPLE
*)colorPtr
;
174 if (depth
== 1) /* Monochrome */
175 for (i
= 0; i
< colors
; i
++, rgb
++)
176 colorMapping
[i
] = (rgb
->rgbtRed
+ rgb
->rgbtGreen
+
177 rgb
->rgbtBlue
> 255*3/2);
179 for (i
= 0; i
< colors
; i
++, rgb
++)
180 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbtRed
,
185 else /* DIB_PAL_COLORS */
187 for (i
= 0; i
< colors
; i
++, colorPtr
++)
188 colorMapping
[i
] = COLOR_ToPhysical( dc
, PALETTEINDEX(*colorPtr
) );
194 /***********************************************************************
197 * SetDIBits for a 1-bit deep DIB.
199 static void DIB_SetImageBits_1( DWORD lines
, BYTE
*bits
, DWORD width
,
200 int *colors
, XImage
*bmpImage
)
205 if (!(width
& 31)) pad
= 0;
206 else pad
= ((32 - (width
& 31)) + 7) / 8;
210 for (i
= width
/8, x
= 0; (i
> 0); i
--)
213 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
214 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 6) & 1] );
215 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 5) & 1] );
216 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 4) & 1] );
217 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 3) & 1] );
218 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 2) & 1] );
219 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 1) & 1] );
220 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 1] );
225 case 7: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
226 case 6: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
227 case 5: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
228 case 4: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
229 case 3: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
230 case 2: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
231 case 1: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
238 /***********************************************************************
241 * SetDIBits for a 4-bit deep DIB.
243 static void DIB_SetImageBits_4( DWORD lines
, BYTE
*bits
, DWORD width
,
244 int *colors
, XImage
*bmpImage
)
249 if (!(width
& 7)) pad
= 0;
250 else pad
= ((8 - (width
& 7)) + 1) / 2;
254 for (i
= width
/2, x
= 0; i
> 0; i
--)
257 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 4] );
258 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 0x0f] );
260 if (width
& 1) XPutPixel( bmpImage
, x
, lines
, colors
[*bits
>> 4] );
265 #define check_xy(x,y) \
272 /***********************************************************************
273 * DIB_SetImageBits_RLE4
275 * SetDIBits for a 4-bit deep compressed DIB.
277 static void DIB_SetImageBits_RLE4( DWORD lines
, BYTE
*bits
, DWORD width
,
278 int *colors
, XImage
*bmpImage
)
280 int x
= 0, c
, length
;
284 while ((int)lines
>= 0)
287 if (length
) { /* encoded */
290 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
294 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
306 case 1: /* eopicture */
314 default: /* absolute */
317 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
321 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
325 if ((bits
- begin
) & 1)
332 /***********************************************************************
335 * SetDIBits for an 8-bit deep DIB.
337 static void DIB_SetImageBits_8( DWORD lines
, BYTE
*bits
, DWORD width
,
338 int *colors
, XImage
*bmpImage
)
341 BYTE pad
= (4 - (width
& 3)) & 3;
345 for (x
= 0; x
< width
; x
++)
346 XPutPixel( bmpImage
, x
, lines
, colors
[*bits
++] );
351 /***********************************************************************
352 * DIB_SetImageBits_RLE8
354 * SetDIBits for an 8-bit deep compressed DIB.
356 * This function rewritten 941113 by James Youngman. WINE blew out when I
357 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
359 * This was because the algorithm assumed that all RLE8 bitmaps end with the
360 * 'End of bitmap' escape code. This code is very much laxer in what it
361 * allows to end the expansion. Possibly too lax. See the note by
362 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
363 * bitmap should end with RleEnd, but on the other hand, software exists
364 * that produces ones that don't and Windows 3.1 doesn't complain a bit
367 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
368 * James A. Youngman <mbcstjy@afs.man.ac.uk>
372 enum Rle8_EscapeCodes
375 * Apologies for polluting your file's namespace...
377 RleEol
= 0, /* End of line */
378 RleEnd
= 1, /* End of bitmap */
379 RleDelta
= 2 /* Delta */
382 static void DIB_SetImageBits_RLE8( DWORD lines
, BYTE
*bits
, DWORD width
,
383 int *colors
, XImage
*bmpImage
)
385 int x
; /* X-positon on each line. Increases. */
386 int line
; /* Line #. Starts at lines-1, decreases */
387 BYTE
*pIn
= bits
; /* Pointer to current position in bits */
388 BYTE length
; /* The length pf a run */
389 BYTE color_index
; /* index into colors[] as read from bits */
390 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
391 WORD color
; /* value of colour[color_index] */
393 if (lines
== 0) /* Let's hope this doesn't happen. */
397 * Note that the bitmap data is stored by Windows starting at the
398 * bottom line of the bitmap and going upwards. Within each line,
399 * the data is stored left-to-right. That's the reason why line
400 * goes from lines-1 to 0. [JAY]
410 * If the length byte is not zero (which is the escape value),
411 * We have a run of length pixels all the same colour. The colour
412 * index is stored next.
414 * If the length byte is zero, we need to read the next byte to
415 * know what to do. [JAY]
420 * [Run-Length] Encoded mode
422 color_index
= (*pIn
++); /* Get the colour index. */
423 color
= colors
[color_index
];
426 XPutPixel(bmpImage
, x
++, line
, color
);
431 * Escape codes (may be an absolute sequence though)
433 escape_code
= (*pIn
++);
436 case RleEol
: /* =0, end of line */
443 case RleEnd
: /* =1, end of bitmap */
446 * Not all RLE8 bitmaps end with this
447 * code. For example, Paint Shop Pro
448 * produces some that don't. That's (I think)
449 * what caused the previous implementation to
452 line
=-1; /* Cause exit from do loop. */
456 case RleDelta
: /* =2, a delta */
459 * Note that deltaing to line 0
460 * will cause an exit from the loop,
461 * which may not be what is intended.
462 * The fact that there is a delta in the bits
463 * almost certainly implies that there is data
464 * to follow. You may feel that we should
465 * jump to the top of the loop to avoid exiting
468 * TODO: Decide what to do here in that case. [JAY]
474 dprintf_bitmap(stddeb
,
475 "DIB_SetImageBits_RLE8(): "
476 "Delta to last line of bitmap "
477 "(wrongly?) causes loop exit\n");
482 default: /* >2, switch to absolute mode */
487 length
= escape_code
;
490 color_index
= (*pIn
++);
491 XPutPixel(bmpImage
, x
++, line
,
492 colors
[color_index
]);
496 * If you think for a moment you'll realise that the
497 * only time we could ever possibly read an odd
498 * number of bytes is when there is a 0x00 (escape),
499 * a value >0x02 (absolute mode) and then an odd-
500 * length run. Therefore this is the only place we
501 * need to worry about it. Everywhere else the
502 * bytes are always read in pairs. [JAY]
505 pIn
++; /* Throw away the pad byte. */
508 } /* switch (escape_code) : Escape sequence */
509 } /* process either an encoded sequence or an escape sequence */
511 /* We expect to come here more than once per line. */
512 } while (line
>= 0); /* Do this until the bitmap is filled */
515 * Everybody comes here at the end.
516 * Check how we exited the loop and print a message if it's a bit odd.
519 if ( (*(pIn
-2) != 0/*escape*/) || (*(pIn
-1)!= RleEnd
) )
521 dprintf_bitmap(stddeb
, "DIB_SetImageBits_RLE8(): End-of-bitmap "
522 "without (strictly) proper escape code. Last two "
523 "bytes were: %02X %02X.\n",
530 /***********************************************************************
531 * DIB_SetImageBits_24
533 * SetDIBits for a 24-bit deep DIB.
535 static void DIB_SetImageBits_24( DWORD lines
, BYTE
*bits
, DWORD width
,
536 DC
*dc
, XImage
*bmpImage
)
539 BYTE pad
= (4 - ((width
*3) & 3)) & 3;
541 /* "bits" order is reversed for some reason */
545 for (x
= 0; x
< width
; x
++, bits
+= 3)
546 XPutPixel( bmpImage
, x
, lines
,
547 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])) );
554 /***********************************************************************
557 * Transfer the bits to an X image.
558 * Helper function for SetDIBits() and SetDIBitsToDevice().
560 static int DIB_SetImageBits( DC
*dc
, DWORD lines
, WORD depth
, LPSTR bits
,
561 DWORD infoWidth
, WORD infoBpp
,
562 BITMAPINFO
*info
, WORD coloruse
,
563 Drawable drawable
, GC gc
, int xSrc
, int ySrc
,
564 int xDest
, int yDest
, int width
, int height
)
568 DWORD compression
= 0;
570 if (info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
571 compression
= info
->bmiHeader
.biCompression
;
573 /* Build the color mapping table */
575 if (infoBpp
== 24) colorMapping
= NULL
;
577 if (!(colorMapping
= DIB_BuildColorMap( dc
, coloruse
, depth
, info
)))
580 if( dc
->w
.flags
& DC_DIRTY
) CLIPPING_UpdateGCRegion(dc
);
582 /* Transfer the pixels */
583 XCREATEIMAGE(bmpImage
, infoWidth
, lines
, depth
);
588 DIB_SetImageBits_1( lines
, bits
, infoWidth
,
589 colorMapping
, bmpImage
);
592 if (compression
) DIB_SetImageBits_RLE4( lines
, bits
, infoWidth
,
593 colorMapping
, bmpImage
);
594 else DIB_SetImageBits_4( lines
, bits
, infoWidth
,
595 colorMapping
, bmpImage
);
598 if (compression
) DIB_SetImageBits_RLE8( lines
, bits
, infoWidth
,
599 colorMapping
, bmpImage
);
600 else DIB_SetImageBits_8( lines
, bits
, infoWidth
,
601 colorMapping
, bmpImage
);
604 DIB_SetImageBits_24( lines
, bits
, infoWidth
, dc
, bmpImage
);
607 fprintf( stderr
, "Invalid depth %d for SetDIBits!\n", infoBpp
);
610 if (colorMapping
) free(colorMapping
);
611 XPutImage( display
, drawable
, gc
, bmpImage
, xSrc
, ySrc
,
612 xDest
, yDest
, width
, height
);
613 XDestroyImage( bmpImage
);
618 /***********************************************************************
619 * StretchDIBits16 (GDI.439)
621 INT16
StretchDIBits16( HDC16 hdc
, INT16 xDst
, INT16 yDst
, INT16 widthDst
,
622 INT16 heightDst
, INT16 xSrc
, INT16 ySrc
, INT16 widthSrc
,
623 INT16 heightSrc
, const VOID
*bits
,
624 const BITMAPINFO
*info
, UINT16 wUsage
, DWORD dwRop
)
626 return (INT16
)StretchDIBits32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
627 xSrc
, ySrc
, widthSrc
, heightSrc
, bits
,
628 info
, wUsage
, dwRop
);
632 /***********************************************************************
633 * StretchDIBits32 (GDI32.351)
635 INT32
StretchDIBits32( HDC32 hdc
, INT32 xDst
, INT32 yDst
, INT32 widthDst
,
636 INT32 heightDst
, INT32 xSrc
, INT32 ySrc
, INT32 widthSrc
,
637 INT32 heightSrc
, const void *bits
,
638 const BITMAPINFO
*info
, UINT32 wUsage
, DWORD dwRop
)
640 HBITMAP32 hBitmap
, hOldBitmap
;
643 hBitmap
= CreateDIBitmap32( hdc
, &info
->bmiHeader
, CBM_INIT
,
644 bits
, info
, wUsage
);
645 hdcMem
= CreateCompatibleDC32( hdc
);
646 hOldBitmap
= SelectObject32( hdcMem
, hBitmap
);
647 StretchBlt32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
648 hdcMem
, xSrc
, ySrc
, widthSrc
, heightSrc
, dwRop
);
649 SelectObject32( hdcMem
, hOldBitmap
);
650 DeleteDC32( hdcMem
);
651 DeleteObject32( hBitmap
);
656 /***********************************************************************
657 * SetDIBits16 (GDI.440)
659 INT16
SetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
660 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
663 return SetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
667 /***********************************************************************
668 * SetDIBits32 (GDI32.312)
670 INT32
SetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
671 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
679 /* Check parameters */
681 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
684 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
687 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
689 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
, &bpp
) == -1)
691 if (!lines
|| (startscan
>= (WORD
)height
)) return 0;
692 if (startscan
+ lines
> height
) lines
= height
- startscan
;
694 return CallTo32_LargeStack( (int(*)())DIB_SetImageBits
, 16,
695 dc
, lines
, bmp
->bitmap
.bmBitsPixel
,
696 bits
, width
, bpp
, info
,
697 coloruse
, bmp
->pixmap
, BITMAP_GC(bmp
), 0, 0, 0,
698 height
- startscan
- lines
,
699 bmp
->bitmap
.bmWidth
, lines
);
703 /***********************************************************************
704 * SetDIBitsToDevice16 (GDI.443)
706 INT16
SetDIBitsToDevice16( HDC16 hdc
, INT16 xDest
, INT16 yDest
, INT16 cx
,
707 INT16 cy
, INT16 xSrc
, INT16 ySrc
, UINT16 startscan
,
708 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
711 return SetDIBitsToDevice32( hdc
, xDest
, yDest
, cx
, cy
, xSrc
, ySrc
,
712 startscan
, lines
, bits
, info
, coloruse
);
716 /***********************************************************************
717 * SetDIBitsToDevice32 (GDI32.313)
719 INT32
SetDIBitsToDevice32( HDC32 hdc
, INT32 xDest
, INT32 yDest
, DWORD cx
,
720 DWORD cy
, INT32 xSrc
, INT32 ySrc
, UINT32 startscan
,
721 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
728 /* Check parameters */
730 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
733 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
736 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
, &bpp
) == -1)
738 if (!lines
|| (startscan
>= height
)) return 0;
739 if (startscan
+ lines
> height
) lines
= height
- startscan
;
740 if (ySrc
< startscan
) ySrc
= startscan
;
741 else if (ySrc
>= startscan
+ lines
) return 0;
742 if (xSrc
>= width
) return 0;
743 if (ySrc
+ cy
>= startscan
+ lines
) cy
= startscan
+ lines
- ySrc
;
744 if (xSrc
+ cx
>= width
) cx
= width
- xSrc
;
745 if (!cx
|| !cy
) return 0;
747 DC_SetupGCForText( dc
); /* To have the correct colors */
748 XSetFunction( display
, dc
->u
.x
.gc
, DC_XROPfunction
[dc
->w
.ROPmode
-1] );
749 return CallTo32_LargeStack( (int(*)())DIB_SetImageBits
, 16,
750 dc
, lines
, dc
->w
.bitsPerPixel
, bits
, width
,
752 dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
753 xSrc
, ySrc
- startscan
,
754 dc
->w
.DCOrgX
+ XLPTODP( dc
, xDest
),
755 dc
->w
.DCOrgY
+ YLPTODP( dc
, yDest
),
761 /***********************************************************************
762 * GetDIBits16 (GDI.441)
764 INT16
GetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
765 UINT16 lines
, LPSTR bits
, BITMAPINFO
* info
,
768 return GetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
772 /***********************************************************************
773 * GetDIBits32 (GDI32.170)
775 INT32
GetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
776 UINT32 lines
, LPSTR bits
, BITMAPINFO
* info
,
781 PALETTEENTRY
* palEntry
;
782 PALETTEOBJ
* palette
;
783 XImage
* bmpImage
, * dibImage
;
786 if (!lines
) return 0;
787 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
790 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
793 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
795 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
798 /* Transfer color info */
800 palEntry
= palette
->logpalette
.palPalEntry
;
801 for (i
= 0; i
< info
->bmiHeader
.biClrUsed
; i
++, palEntry
++)
803 if (coloruse
== DIB_RGB_COLORS
)
805 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
806 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
807 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
808 info
->bmiColors
[i
].rgbReserved
= 0;
810 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
813 /* Transfer the pixels (very slow...) */
817 bmpImage
= (XImage
*)CallTo32_LargeStack( (int (*)())XGetImage
, 8,
818 display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
,
819 bmp
->bitmap
.bmHeight
, AllPlanes
, ZPixmap
);
820 dibImage
= DIB_DIBmpToImage( &info
->bmiHeader
, bits
);
822 for (y
= 0; y
< lines
; y
++)
824 for (x
= 0; x
< info
->bmiHeader
.biWidth
; x
++)
826 XPutPixel( dibImage
, x
, y
,
827 XGetPixel(bmpImage
, x
, bmp
->bitmap
.bmHeight
-startscan
-y
-1) );
832 dibImage
->data
= NULL
;
833 XDestroyImage( dibImage
);
834 XDestroyImage( bmpImage
);
836 info
->bmiHeader
.biCompression
= 0;
841 /***********************************************************************
842 * CreateDIBitmap16 (GDI.442)
844 HBITMAP16
CreateDIBitmap16( HDC16 hdc
, const BITMAPINFOHEADER
* header
,
845 DWORD init
, LPCVOID bits
, const BITMAPINFO
* data
,
848 return CreateDIBitmap32( hdc
, header
, init
, bits
, data
, coloruse
);
852 /***********************************************************************
853 * CreateDIBitmap32 (GDI32.37)
855 HBITMAP32
CreateDIBitmap32( HDC32 hdc
, const BITMAPINFOHEADER
*header
,
856 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
864 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
) == -1) return 0;
866 /* Check if we should create a monochrome or color bitmap. */
867 /* We create a monochrome bitmap only if it has exactly 2 */
868 /* colors, which are either black or white, nothing else. */
869 /* In all other cases, we create a color bitmap. */
871 if (bpp
!= 1) fColor
= TRUE
;
872 else if ((coloruse
!= DIB_RGB_COLORS
) ||
873 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
876 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
878 RGBQUAD
*rgb
= data
->bmiColors
;
879 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
880 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
883 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
884 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
888 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
890 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
891 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
892 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
895 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
896 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
902 fprintf( stderr
, "CreateDIBitmap: wrong size (%ld) for data\n",
903 data
->bmiHeader
.biSize
);
908 /* Now create the bitmap */
910 handle
= fColor
? CreateCompatibleBitmap( hdc
, width
, height
) :
911 CreateBitmap( width
, height
, 1, 1, NULL
);
912 if (!handle
) return 0;
914 if (init
== CBM_INIT
)
915 SetDIBits32( hdc
, handle
, 0, height
, bits
, data
, coloruse
);