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
; break;
39 fprintf(stderr
, "DIB: unsupported depth %d.\n", depth
);
46 /***********************************************************************
49 * Return the size of the bitmap info structure.
51 int DIB_BitmapInfoSize( BITMAPINFO
* info
, WORD coloruse
)
55 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
57 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
58 colors
= (core
->bcBitCount
!= 24) ? 1 << core
->bcBitCount
: 0;
59 return sizeof(BITMAPCOREHEADER
) + colors
*
60 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
62 else /* assume BITMAPINFOHEADER */
64 colors
= info
->bmiHeader
.biClrUsed
;
65 if (!colors
&& (info
->bmiHeader
.biBitCount
!= 24))
66 colors
= 1 << info
->bmiHeader
.biBitCount
;
67 return sizeof(BITMAPINFOHEADER
) + colors
*
68 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
73 /***********************************************************************
76 * Create an XImage pointing to the bitmap data.
78 static XImage
*DIB_DIBmpToImage( BITMAPINFOHEADER
* bmp
, void * bmpData
)
80 extern void _XInitImageFuncPtrs( XImage
* );
83 image
= XCreateImage(display
, DefaultVisualOfScreen( screen
),
84 bmp
->biBitCount
, ZPixmap
, 0, bmpData
,
85 bmp
->biWidth
, bmp
->biHeight
, 32,
86 DIB_GetImageWidthBytes(bmp
->biWidth
,bmp
->biBitCount
));
88 image
->byte_order
= MSBFirst
;
89 image
->bitmap_bit_order
= MSBFirst
;
90 image
->bitmap_unit
= 16;
91 _XInitImageFuncPtrs(image
);
96 /***********************************************************************
99 * Get the info from a bitmap header.
100 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
102 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
103 DWORD
*height
, WORD
*bpp
)
105 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
107 *width
= header
->biWidth
;
108 *height
= header
->biHeight
;
109 *bpp
= header
->biBitCount
;
112 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
114 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
115 *width
= core
->bcWidth
;
116 *height
= core
->bcHeight
;
117 *bpp
= core
->bcBitCount
;
120 fprintf( stderr
, "DIB_GetBitmapInfo: wrong size (%ld) for header\n",
126 /***********************************************************************
129 * Build the color map from the bitmap palette. Should not be called
130 * for a 24-bit deep bitmap.
132 static int *DIB_BuildColorMap( DC
*dc
, WORD coloruse
, WORD depth
,
140 if ((isInfo
= (info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))))
142 colors
= info
->bmiHeader
.biClrUsed
;
143 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
144 colorPtr
= (WORD
*)info
->bmiColors
;
146 else /* assume BITMAPCOREINFO */
148 colors
= 1 << ((BITMAPCOREHEADER
*)&info
->bmiHeader
)->bcBitCount
;
149 colorPtr
= (WORD
*)((BITMAPCOREINFO
*)info
)->bmciColors
;
151 if (!(colorMapping
= (int *)malloc( colors
* sizeof(int) ))) return NULL
;
153 if (coloruse
== DIB_RGB_COLORS
)
157 RGBQUAD
* rgb
= (RGBQUAD
*)colorPtr
;
159 if (depth
== 1) /* Monochrome */
160 for (i
= 0; i
< colors
; i
++, rgb
++)
161 colorMapping
[i
] = (rgb
->rgbRed
+ rgb
->rgbGreen
+
162 rgb
->rgbBlue
> 255*3/2);
164 for (i
= 0; i
< colors
; i
++, rgb
++)
165 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbRed
,
171 RGBTRIPLE
* rgb
= (RGBTRIPLE
*)colorPtr
;
173 if (depth
== 1) /* Monochrome */
174 for (i
= 0; i
< colors
; i
++, rgb
++)
175 colorMapping
[i
] = (rgb
->rgbtRed
+ rgb
->rgbtGreen
+
176 rgb
->rgbtBlue
> 255*3/2);
178 for (i
= 0; i
< colors
; i
++, rgb
++)
179 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbtRed
,
184 else /* DIB_PAL_COLORS */
186 for (i
= 0; i
< colors
; i
++, colorPtr
++)
187 colorMapping
[i
] = COLOR_ToPhysical( dc
, PALETTEINDEX(*colorPtr
) );
193 /***********************************************************************
196 * SetDIBits for a 1-bit deep DIB.
198 static void DIB_SetImageBits_1( DWORD lines
, BYTE
*bits
, DWORD width
,
199 int *colors
, XImage
*bmpImage
)
204 if (!(width
& 31)) pad
= 0;
205 else pad
= ((32 - (width
& 31)) + 7) / 8;
209 for (i
= width
/8, x
= 0; (i
> 0); i
--)
212 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
213 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 6) & 1] );
214 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 5) & 1] );
215 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 4) & 1] );
216 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 3) & 1] );
217 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 2) & 1] );
218 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 1) & 1] );
219 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 1] );
224 case 7: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
225 case 6: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
226 case 5: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
227 case 4: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
228 case 3: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
229 case 2: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
230 case 1: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
237 /***********************************************************************
240 * SetDIBits for a 4-bit deep DIB.
242 static void DIB_SetImageBits_4( DWORD lines
, BYTE
*bits
, DWORD width
,
243 int *colors
, XImage
*bmpImage
)
248 if (!(width
& 7)) pad
= 0;
249 else pad
= ((8 - (width
& 7)) + 1) / 2;
253 for (i
= width
/2, x
= 0; i
> 0; i
--)
256 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 4] );
257 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 0x0f] );
259 if (width
& 1) XPutPixel( bmpImage
, x
, lines
, colors
[*bits
>> 4] );
264 #define check_xy(x,y) \
271 /***********************************************************************
272 * DIB_SetImageBits_RLE4
274 * SetDIBits for a 4-bit deep compressed DIB.
276 static void DIB_SetImageBits_RLE4( DWORD lines
, BYTE
*bits
, DWORD width
,
277 int *colors
, XImage
*bmpImage
)
279 int x
= 0, c
, length
;
283 while ((int)lines
>= 0)
286 if (length
) { /* encoded */
289 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
293 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
305 case 1: /* eopicture */
313 default: /* absolute */
316 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
320 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
324 if ((bits
- begin
) & 1)
331 /***********************************************************************
334 * SetDIBits for an 8-bit deep DIB.
336 static void DIB_SetImageBits_8( DWORD lines
, BYTE
*bits
, DWORD width
,
337 int *colors
, XImage
*bmpImage
)
340 BYTE pad
= (4 - (width
& 3)) & 3;
344 for (x
= 0; x
< width
; x
++)
345 XPutPixel( bmpImage
, x
, lines
, colors
[*bits
++] );
350 /***********************************************************************
351 * DIB_SetImageBits_RLE8
353 * SetDIBits for an 8-bit deep compressed DIB.
355 * This function rewritten 941113 by James Youngman. WINE blew out when I
356 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
358 * This was because the algorithm assumed that all RLE8 bitmaps end with the
359 * 'End of bitmap' escape code. This code is very much laxer in what it
360 * allows to end the expansion. Possibly too lax. See the note by
361 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
362 * bitmap should end with RleEnd, but on the other hand, software exists
363 * that produces ones that don't and Windows 3.1 doesn't complain a bit
366 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
367 * James A. Youngman <mbcstjy@afs.man.ac.uk>
371 enum Rle8_EscapeCodes
374 * Apologies for polluting your file's namespace...
376 RleEol
= 0, /* End of line */
377 RleEnd
= 1, /* End of bitmap */
378 RleDelta
= 2 /* Delta */
381 static void DIB_SetImageBits_RLE8( DWORD lines
, BYTE
*bits
, DWORD width
,
382 int *colors
, XImage
*bmpImage
)
384 int x
; /* X-positon on each line. Increases. */
385 int line
; /* Line #. Starts at lines-1, decreases */
386 BYTE
*pIn
= bits
; /* Pointer to current position in bits */
387 BYTE length
; /* The length pf a run */
388 BYTE color_index
; /* index into colors[] as read from bits */
389 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
390 WORD color
; /* value of colour[color_index] */
392 if (lines
== 0) /* Let's hope this doesn't happen. */
396 * Note that the bitmap data is stored by Windows starting at the
397 * bottom line of the bitmap and going upwards. Within each line,
398 * the data is stored left-to-right. That's the reason why line
399 * goes from lines-1 to 0. [JAY]
409 * If the length byte is not zero (which is the escape value),
410 * We have a run of length pixels all the same colour. The colour
411 * index is stored next.
413 * If the length byte is zero, we need to read the next byte to
414 * know what to do. [JAY]
419 * [Run-Length] Encoded mode
421 color_index
= (*pIn
++); /* Get the colour index. */
422 color
= colors
[color_index
];
425 XPutPixel(bmpImage
, x
++, line
, color
);
430 * Escape codes (may be an absolute sequence though)
432 escape_code
= (*pIn
++);
435 case RleEol
: /* =0, end of line */
442 case RleEnd
: /* =1, end of bitmap */
445 * Not all RLE8 bitmaps end with this
446 * code. For example, Paint Shop Pro
447 * produces some that don't. That's (I think)
448 * what caused the previous implementation to
451 line
=-1; /* Cause exit from do loop. */
455 case RleDelta
: /* =2, a delta */
458 * Note that deltaing to line 0
459 * will cause an exit from the loop,
460 * which may not be what is intended.
461 * The fact that there is a delta in the bits
462 * almost certainly implies that there is data
463 * to follow. You may feel that we should
464 * jump to the top of the loop to avoid exiting
467 * TODO: Decide what to do here in that case. [JAY]
473 dprintf_bitmap(stddeb
,
474 "DIB_SetImageBits_RLE8(): "
475 "Delta to last line of bitmap "
476 "(wrongly?) causes loop exit\n");
481 default: /* >2, switch to absolute mode */
486 length
= escape_code
;
489 color_index
= (*pIn
++);
490 XPutPixel(bmpImage
, x
++, line
,
491 colors
[color_index
]);
495 * If you think for a moment you'll realise that the
496 * only time we could ever possibly read an odd
497 * number of bytes is when there is a 0x00 (escape),
498 * a value >0x02 (absolute mode) and then an odd-
499 * length run. Therefore this is the only place we
500 * need to worry about it. Everywhere else the
501 * bytes are always read in pairs. [JAY]
504 pIn
++; /* Throw away the pad byte. */
507 } /* switch (escape_code) : Escape sequence */
508 } /* process either an encoded sequence or an escape sequence */
510 /* We expect to come here more than once per line. */
511 } while (line
>= 0); /* Do this until the bitmap is filled */
514 * Everybody comes here at the end.
515 * Check how we exited the loop and print a message if it's a bit odd.
518 if ( (*(pIn
-2) != 0/*escape*/) || (*(pIn
-1)!= RleEnd
) )
520 dprintf_bitmap(stddeb
, "DIB_SetImageBits_RLE8(): End-of-bitmap "
521 "without (strictly) proper escape code. Last two "
522 "bytes were: %02X %02X.\n",
529 /***********************************************************************
530 * DIB_SetImageBits_24
532 * SetDIBits for a 24-bit deep DIB.
534 static void DIB_SetImageBits_24( DWORD lines
, BYTE
*bits
, DWORD width
,
535 DC
*dc
, XImage
*bmpImage
)
538 BYTE pad
= (4 - ((width
*3) & 3)) & 3;
540 /* "bits" order is reversed for some reason */
544 for (x
= 0; x
< width
; x
++, bits
+= 3)
545 XPutPixel( bmpImage
, x
, lines
,
546 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])) );
553 /***********************************************************************
556 * Transfer the bits to an X image.
557 * Helper function for SetDIBits() and SetDIBitsToDevice().
559 static int DIB_SetImageBits( DC
*dc
, DWORD lines
, WORD depth
, LPSTR bits
,
560 DWORD infoWidth
, WORD infoBpp
,
561 BITMAPINFO
*info
, WORD coloruse
,
562 Drawable drawable
, GC gc
, int xSrc
, int ySrc
,
563 int xDest
, int yDest
, int width
, int height
)
567 DWORD compression
= 0;
569 if (info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
570 compression
= info
->bmiHeader
.biCompression
;
572 /* Build the color mapping table */
574 if (infoBpp
== 24) colorMapping
= NULL
;
576 if (!(colorMapping
= DIB_BuildColorMap( dc
, coloruse
, depth
, info
)))
579 if( dc
->w
.flags
& DC_DIRTY
) CLIPPING_UpdateGCRegion(dc
);
581 /* Transfer the pixels */
582 XCREATEIMAGE(bmpImage
, infoWidth
, lines
, depth
);
587 DIB_SetImageBits_1( lines
, bits
, infoWidth
,
588 colorMapping
, bmpImage
);
591 if (compression
) DIB_SetImageBits_RLE4( lines
, bits
, infoWidth
,
592 colorMapping
, bmpImage
);
593 else DIB_SetImageBits_4( lines
, bits
, infoWidth
,
594 colorMapping
, bmpImage
);
597 if (compression
) DIB_SetImageBits_RLE8( lines
, bits
, infoWidth
,
598 colorMapping
, bmpImage
);
599 else DIB_SetImageBits_8( lines
, bits
, infoWidth
,
600 colorMapping
, bmpImage
);
603 DIB_SetImageBits_24( lines
, bits
, infoWidth
, dc
, bmpImage
);
606 fprintf( stderr
, "Invalid depth %d for SetDIBits!\n", infoBpp
);
609 if (colorMapping
) free(colorMapping
);
610 XPutImage( display
, drawable
, gc
, bmpImage
, xSrc
, ySrc
,
611 xDest
, yDest
, width
, height
);
612 XDestroyImage( bmpImage
);
617 /***********************************************************************
618 * StretchDIBits16 (GDI.439)
620 INT16
StretchDIBits16( HDC16 hdc
, INT16 xDst
, INT16 yDst
, INT16 widthDst
,
621 INT16 heightDst
, INT16 xSrc
, INT16 ySrc
, INT16 widthSrc
,
622 INT16 heightSrc
, const VOID
*bits
,
623 const BITMAPINFO
*info
, UINT16 wUsage
, DWORD dwRop
)
625 return (INT16
)StretchDIBits32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
626 xSrc
, ySrc
, widthSrc
, heightSrc
, bits
,
627 info
, wUsage
, dwRop
);
631 /***********************************************************************
632 * StretchDIBits32 (GDI32.351)
634 INT32
StretchDIBits32( HDC32 hdc
, INT32 xDst
, INT32 yDst
, INT32 widthDst
,
635 INT32 heightDst
, INT32 xSrc
, INT32 ySrc
, INT32 widthSrc
,
636 INT32 heightSrc
, const void *bits
,
637 const BITMAPINFO
*info
, UINT32 wUsage
, DWORD dwRop
)
639 HBITMAP32 hBitmap
, hOldBitmap
;
642 hBitmap
= CreateDIBitmap32( hdc
, &info
->bmiHeader
, CBM_INIT
,
643 bits
, info
, wUsage
);
644 hdcMem
= CreateCompatibleDC32( hdc
);
645 hOldBitmap
= SelectObject32( hdcMem
, hBitmap
);
646 StretchBlt32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
647 hdcMem
, xSrc
, ySrc
, widthSrc
, heightSrc
, dwRop
);
648 SelectObject32( hdcMem
, hOldBitmap
);
649 DeleteDC32( hdcMem
);
650 DeleteObject32( hBitmap
);
655 /***********************************************************************
656 * SetDIBits16 (GDI.440)
658 INT16
SetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
659 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
662 return SetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
666 /***********************************************************************
667 * SetDIBits32 (GDI32.312)
669 INT32
SetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
670 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
678 /* Check parameters */
680 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
683 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
686 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
688 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
, &bpp
) == -1)
690 if (!lines
|| (startscan
>= (WORD
)height
)) return 0;
691 if (startscan
+ lines
> height
) lines
= height
- startscan
;
693 return CallTo32_LargeStack( (int(*)())DIB_SetImageBits
, 16,
694 dc
, lines
, bmp
->bitmap
.bmBitsPixel
,
695 bits
, width
, bpp
, info
,
696 coloruse
, bmp
->pixmap
, BITMAP_GC(bmp
), 0, 0, 0,
697 height
- startscan
- lines
,
698 bmp
->bitmap
.bmWidth
, lines
);
702 /***********************************************************************
703 * SetDIBitsToDevice16 (GDI.443)
705 INT16
SetDIBitsToDevice16( HDC16 hdc
, INT16 xDest
, INT16 yDest
, INT16 cx
,
706 INT16 cy
, INT16 xSrc
, INT16 ySrc
, UINT16 startscan
,
707 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
710 return SetDIBitsToDevice32( hdc
, xDest
, yDest
, cx
, cy
, xSrc
, ySrc
,
711 startscan
, lines
, bits
, info
, coloruse
);
715 /***********************************************************************
716 * SetDIBitsToDevice32 (GDI32.313)
718 INT32
SetDIBitsToDevice32( HDC32 hdc
, INT32 xDest
, INT32 yDest
, DWORD cx
,
719 DWORD cy
, INT32 xSrc
, INT32 ySrc
, UINT32 startscan
,
720 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
727 /* Check parameters */
729 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
732 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
735 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
, &bpp
) == -1)
737 if (!lines
|| (startscan
>= height
)) return 0;
738 if (startscan
+ lines
> height
) lines
= height
- startscan
;
739 if (ySrc
< startscan
) ySrc
= startscan
;
740 else if (ySrc
>= startscan
+ lines
) return 0;
741 if (xSrc
>= width
) return 0;
742 if (ySrc
+ cy
>= startscan
+ lines
) cy
= startscan
+ lines
- ySrc
;
743 if (xSrc
+ cx
>= width
) cx
= width
- xSrc
;
744 if (!cx
|| !cy
) return 0;
746 DC_SetupGCForText( dc
); /* To have the correct colors */
747 XSetFunction( display
, dc
->u
.x
.gc
, DC_XROPfunction
[dc
->w
.ROPmode
-1] );
748 return CallTo32_LargeStack( (int(*)())DIB_SetImageBits
, 16,
749 dc
, lines
, dc
->w
.bitsPerPixel
, bits
, width
,
751 dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
752 xSrc
, ySrc
- startscan
,
753 dc
->w
.DCOrgX
+ XLPTODP( dc
, xDest
),
754 dc
->w
.DCOrgY
+ YLPTODP( dc
, yDest
),
760 /***********************************************************************
761 * GetDIBits16 (GDI.441)
763 INT16
GetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
764 UINT16 lines
, LPSTR bits
, BITMAPINFO
* info
,
767 return GetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
771 /***********************************************************************
772 * GetDIBits32 (GDI32.170)
774 INT32
GetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
775 UINT32 lines
, LPSTR bits
, BITMAPINFO
* info
,
780 PALETTEENTRY
* palEntry
;
781 PALETTEOBJ
* palette
;
782 XImage
* bmpImage
, * dibImage
;
785 if (!lines
) return 0;
786 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
789 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
792 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
794 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
797 /* Transfer color info */
799 palEntry
= palette
->logpalette
.palPalEntry
;
800 for (i
= 0; i
< info
->bmiHeader
.biClrUsed
; i
++, palEntry
++)
802 if (coloruse
== DIB_RGB_COLORS
)
804 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
805 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
806 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
807 info
->bmiColors
[i
].rgbReserved
= 0;
809 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
812 /* Transfer the pixels (very slow...) */
816 bmpImage
= (XImage
*)CallTo32_LargeStack( (int (*)())XGetImage
, 8,
817 display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
,
818 bmp
->bitmap
.bmHeight
, AllPlanes
, ZPixmap
);
819 dibImage
= DIB_DIBmpToImage( &info
->bmiHeader
, bits
);
821 for (y
= 0; y
< lines
; y
++)
823 for (x
= 0; x
< info
->bmiHeader
.biWidth
; x
++)
825 XPutPixel( dibImage
, x
, y
,
826 XGetPixel(bmpImage
, x
, bmp
->bitmap
.bmHeight
-startscan
-y
-1) );
831 dibImage
->data
= NULL
;
832 XDestroyImage( dibImage
);
833 XDestroyImage( bmpImage
);
835 info
->bmiHeader
.biCompression
= 0;
840 /***********************************************************************
841 * CreateDIBitmap16 (GDI.442)
843 HBITMAP16
CreateDIBitmap16( HDC16 hdc
, const BITMAPINFOHEADER
* header
,
844 DWORD init
, LPCVOID bits
, const BITMAPINFO
* data
,
847 return CreateDIBitmap32( hdc
, header
, init
, bits
, data
, coloruse
);
851 /***********************************************************************
852 * CreateDIBitmap32 (GDI32.37)
854 HBITMAP32
CreateDIBitmap32( HDC32 hdc
, const BITMAPINFOHEADER
*header
,
855 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
863 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
) == -1) return 0;
865 /* Check if we should create a monochrome or color bitmap. */
866 /* We create a monochrome bitmap only if it has exactly 2 */
867 /* colors, which are either black or white, nothing else. */
868 /* In all other cases, we create a color bitmap. */
870 if (bpp
!= 1) fColor
= TRUE
;
871 else if ((coloruse
!= DIB_RGB_COLORS
) ||
872 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
875 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
877 RGBQUAD
*rgb
= data
->bmiColors
;
878 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
879 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
882 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
883 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
887 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
889 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
890 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
891 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
894 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
895 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
901 fprintf( stderr
, "CreateDIBitmap: wrong size (%ld) for data\n",
902 data
->bmiHeader
.biSize
);
907 /* Now create the bitmap */
909 handle
= fColor
? CreateCompatibleBitmap32( hdc
, width
, height
) :
910 CreateBitmap32( width
, height
, 1, 1, NULL
);
911 if (!handle
) return 0;
913 if (init
== CBM_INIT
)
914 SetDIBits32( hdc
, handle
, 0, height
, bits
, data
, coloruse
);