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_GetImageWidthBytesX11
24 * Return the width of an X image in bytes
26 int DIB_GetImageWidthBytesX11( int width
, int depth
)
31 #define DEPTHCASE(depth) \
33 static int w##depth = 0; \
35 testimage=XCreateImage(display,DefaultVisualOfScreen(screen),\
36 depth,ZPixmap,0,NULL,1,1,32,20); \
37 w##depth = testimage->bits_per_pixel; \
38 XDestroyImage(testimage); \
40 wbits = width*w##depth; \
54 fprintf(stderr
, "DIB: unsupported depth %d.\n", depth
);
55 /* assume 32 bits/pixel */
59 return 4 * ((wbits
+31)/32);
62 /***********************************************************************
63 * DIB_GetImageWidthBytes
65 * Return the width of an X image in bytes
67 int DIB_GetImageWidthBytes( int width
, int depth
)
73 case 1: words
= (width
+ 31) / 32; break;
74 case 4: words
= (width
+ 7) / 8; break;
75 case 8: words
= (width
+ 3) / 4; break;
77 case 16: words
= (width
+ 1) / 2; break;
78 case 24: words
= (width
* 3 + 3)/4; break;
81 fprintf(stderr
, "DIB: unsupported depth %d.\n", depth
);
90 /***********************************************************************
93 * Return the size of the bitmap info structure.
95 int DIB_BitmapInfoSize( BITMAPINFO
* info
, WORD coloruse
)
99 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
101 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
102 colors
= (core
->bcBitCount
!= 24) ? 1 << core
->bcBitCount
: 0;
103 return sizeof(BITMAPCOREHEADER
) + colors
*
104 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
106 else /* assume BITMAPINFOHEADER */
108 colors
= info
->bmiHeader
.biClrUsed
;
109 if (!colors
&& (info
->bmiHeader
.biBitCount
!= 24))
110 colors
= 1 << info
->bmiHeader
.biBitCount
;
111 return sizeof(BITMAPINFOHEADER
) + colors
*
112 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
117 /***********************************************************************
120 * Get the info from a bitmap header.
121 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
123 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
124 DWORD
*height
, WORD
*bpp
)
126 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
128 *width
= header
->biWidth
;
129 *height
= header
->biHeight
;
130 *bpp
= header
->biBitCount
;
133 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
135 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
136 *width
= core
->bcWidth
;
137 *height
= core
->bcHeight
;
138 *bpp
= core
->bcBitCount
;
141 fprintf( stderr
, "DIB_GetBitmapInfo: wrong size (%ld) for header\n",
147 /***********************************************************************
150 * Build the color map from the bitmap palette. Should not be called
151 * for a 24-bit deep bitmap.
153 static int *DIB_BuildColorMap( DC
*dc
, WORD coloruse
, WORD depth
,
161 if ((isInfo
= (info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))))
163 colors
= info
->bmiHeader
.biClrUsed
;
164 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
165 colorPtr
= (WORD
*)info
->bmiColors
;
167 else /* assume BITMAPCOREINFO */
169 colors
= 1 << ((BITMAPCOREHEADER
*)&info
->bmiHeader
)->bcBitCount
;
170 colorPtr
= (WORD
*)((BITMAPCOREINFO
*)info
)->bmciColors
;
172 if (!(colorMapping
= (int *)malloc( colors
* sizeof(int) ))) return NULL
;
174 if (coloruse
== DIB_RGB_COLORS
)
178 RGBQUAD
* rgb
= (RGBQUAD
*)colorPtr
;
180 if (depth
== 1) /* Monochrome */
181 for (i
= 0; i
< colors
; i
++, rgb
++)
182 colorMapping
[i
] = (rgb
->rgbRed
+ rgb
->rgbGreen
+
183 rgb
->rgbBlue
> 255*3/2);
185 for (i
= 0; i
< colors
; i
++, rgb
++)
186 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbRed
,
192 RGBTRIPLE
* rgb
= (RGBTRIPLE
*)colorPtr
;
194 if (depth
== 1) /* Monochrome */
195 for (i
= 0; i
< colors
; i
++, rgb
++)
196 colorMapping
[i
] = (rgb
->rgbtRed
+ rgb
->rgbtGreen
+
197 rgb
->rgbtBlue
> 255*3/2);
199 for (i
= 0; i
< colors
; i
++, rgb
++)
200 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbtRed
,
205 else /* DIB_PAL_COLORS */
207 for (i
= 0; i
< colors
; i
++, colorPtr
++)
208 colorMapping
[i
] = COLOR_ToPhysical( dc
, PALETTEINDEX(*colorPtr
) );
214 /***********************************************************************
217 * SetDIBits for a 1-bit deep DIB.
219 static void DIB_SetImageBits_1( DWORD lines
, BYTE
*bits
, DWORD width
,
220 int *colors
, XImage
*bmpImage
)
225 if (!(width
& 31)) pad
= 0;
226 else pad
= ((32 - (width
& 31)) + 7) / 8;
230 for (i
= width
/8, x
= 0; (i
> 0); i
--)
233 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
234 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 6) & 1] );
235 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 5) & 1] );
236 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 4) & 1] );
237 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 3) & 1] );
238 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 2) & 1] );
239 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 1) & 1] );
240 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 1] );
245 case 7: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
246 case 6: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
247 case 5: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
248 case 4: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
249 case 3: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
250 case 2: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
251 case 1: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
258 /***********************************************************************
261 * SetDIBits for a 4-bit deep DIB.
263 static void DIB_SetImageBits_4( DWORD lines
, BYTE
*bits
, DWORD width
,
264 int *colors
, XImage
*bmpImage
)
269 if (!(width
& 7)) pad
= 0;
270 else pad
= ((8 - (width
& 7)) + 1) / 2;
274 for (i
= width
/2, x
= 0; i
> 0; i
--)
277 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 4] );
278 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 0x0f] );
280 if (width
& 1) XPutPixel( bmpImage
, x
, lines
, colors
[*bits
>> 4] );
285 #define check_xy(x,y) \
292 /***********************************************************************
293 * DIB_SetImageBits_RLE4
295 * SetDIBits for a 4-bit deep compressed DIB.
297 static void DIB_SetImageBits_RLE4( DWORD lines
, BYTE
*bits
, DWORD width
,
298 int *colors
, XImage
*bmpImage
)
300 int x
= 0, c
, length
;
304 while ((int)lines
>= 0)
307 if (length
) { /* encoded */
310 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
314 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
326 case 1: /* eopicture */
334 default: /* absolute */
337 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
341 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
345 if ((bits
- begin
) & 1)
352 /***********************************************************************
355 * SetDIBits for an 8-bit deep DIB.
357 static void DIB_SetImageBits_8( DWORD lines
, BYTE
*bits
, DWORD width
,
358 int *colors
, XImage
*bmpImage
)
361 BYTE pad
= (4 - (width
& 3)) & 3;
365 for (x
= 0; x
< width
; x
++)
366 XPutPixel( bmpImage
, x
, lines
, colors
[*bits
++] );
371 /***********************************************************************
372 * DIB_SetImageBits_RLE8
374 * SetDIBits for an 8-bit deep compressed DIB.
376 * This function rewritten 941113 by James Youngman. WINE blew out when I
377 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
379 * This was because the algorithm assumed that all RLE8 bitmaps end with the
380 * 'End of bitmap' escape code. This code is very much laxer in what it
381 * allows to end the expansion. Possibly too lax. See the note by
382 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
383 * bitmap should end with RleEnd, but on the other hand, software exists
384 * that produces ones that don't and Windows 3.1 doesn't complain a bit
387 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
388 * James A. Youngman <mbcstjy@afs.man.ac.uk>
392 enum Rle8_EscapeCodes
395 * Apologies for polluting your file's namespace...
397 RleEol
= 0, /* End of line */
398 RleEnd
= 1, /* End of bitmap */
399 RleDelta
= 2 /* Delta */
402 static void DIB_SetImageBits_RLE8( DWORD lines
, BYTE
*bits
, DWORD width
,
403 int *colors
, XImage
*bmpImage
)
405 int x
; /* X-positon on each line. Increases. */
406 int line
; /* Line #. Starts at lines-1, decreases */
407 BYTE
*pIn
= bits
; /* Pointer to current position in bits */
408 BYTE length
; /* The length pf a run */
409 BYTE color_index
; /* index into colors[] as read from bits */
410 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
411 WORD color
; /* value of colour[color_index] */
413 if (lines
== 0) /* Let's hope this doesn't happen. */
417 * Note that the bitmap data is stored by Windows starting at the
418 * bottom line of the bitmap and going upwards. Within each line,
419 * the data is stored left-to-right. That's the reason why line
420 * goes from lines-1 to 0. [JAY]
430 * If the length byte is not zero (which is the escape value),
431 * We have a run of length pixels all the same colour. The colour
432 * index is stored next.
434 * If the length byte is zero, we need to read the next byte to
435 * know what to do. [JAY]
440 * [Run-Length] Encoded mode
442 color_index
= (*pIn
++); /* Get the colour index. */
443 color
= colors
[color_index
];
446 XPutPixel(bmpImage
, x
++, line
, color
);
451 * Escape codes (may be an absolute sequence though)
453 escape_code
= (*pIn
++);
456 case RleEol
: /* =0, end of line */
463 case RleEnd
: /* =1, end of bitmap */
466 * Not all RLE8 bitmaps end with this
467 * code. For example, Paint Shop Pro
468 * produces some that don't. That's (I think)
469 * what caused the previous implementation to
472 line
=-1; /* Cause exit from do loop. */
476 case RleDelta
: /* =2, a delta */
479 * Note that deltaing to line 0
480 * will cause an exit from the loop,
481 * which may not be what is intended.
482 * The fact that there is a delta in the bits
483 * almost certainly implies that there is data
484 * to follow. You may feel that we should
485 * jump to the top of the loop to avoid exiting
488 * TODO: Decide what to do here in that case. [JAY]
494 dprintf_bitmap(stddeb
,
495 "DIB_SetImageBits_RLE8(): "
496 "Delta to last line of bitmap "
497 "(wrongly?) causes loop exit\n");
502 default: /* >2, switch to absolute mode */
507 length
= escape_code
;
510 color_index
= (*pIn
++);
511 XPutPixel(bmpImage
, x
++, line
,
512 colors
[color_index
]);
516 * If you think for a moment you'll realise that the
517 * only time we could ever possibly read an odd
518 * number of bytes is when there is a 0x00 (escape),
519 * a value >0x02 (absolute mode) and then an odd-
520 * length run. Therefore this is the only place we
521 * need to worry about it. Everywhere else the
522 * bytes are always read in pairs. [JAY]
525 pIn
++; /* Throw away the pad byte. */
528 } /* switch (escape_code) : Escape sequence */
529 } /* process either an encoded sequence or an escape sequence */
531 /* We expect to come here more than once per line. */
532 } while (line
>= 0); /* Do this until the bitmap is filled */
535 * Everybody comes here at the end.
536 * Check how we exited the loop and print a message if it's a bit odd.
539 if ( (*(pIn
-2) != 0/*escape*/) || (*(pIn
-1)!= RleEnd
) )
541 dprintf_bitmap(stddeb
, "DIB_SetImageBits_RLE8(): End-of-bitmap "
542 "without (strictly) proper escape code. Last two "
543 "bytes were: %02X %02X.\n",
550 /***********************************************************************
551 * DIB_SetImageBits_24
553 * SetDIBits for a 24-bit deep DIB.
555 static void DIB_SetImageBits_24( DWORD lines
, BYTE
*bits
, DWORD width
,
556 DC
*dc
, XImage
*bmpImage
)
559 BYTE pad
= (4 - ((width
*3) & 3)) & 3;
561 /* "bits" order is reversed for some reason */
565 for (x
= 0; x
< width
; x
++, bits
+= 3)
566 XPutPixel( bmpImage
, x
, lines
,
567 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])) );
574 /***********************************************************************
577 * Transfer the bits to an X image.
578 * Helper function for SetDIBits() and SetDIBitsToDevice().
580 static int DIB_SetImageBits( DC
*dc
, DWORD lines
, WORD depth
, LPSTR bits
,
581 DWORD infoWidth
, WORD infoBpp
,
582 BITMAPINFO
*info
, WORD coloruse
,
583 Drawable drawable
, GC gc
, int xSrc
, int ySrc
,
584 int xDest
, int yDest
, int width
, int height
)
588 DWORD compression
= 0;
590 if (info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
591 compression
= info
->bmiHeader
.biCompression
;
593 /* Build the color mapping table */
595 if (infoBpp
== 24) colorMapping
= NULL
;
597 if (!(colorMapping
= DIB_BuildColorMap( dc
, coloruse
, depth
, info
)))
600 if( dc
->w
.flags
& DC_DIRTY
) CLIPPING_UpdateGCRegion(dc
);
602 /* Transfer the pixels */
603 XCREATEIMAGE(bmpImage
, infoWidth
, lines
, depth
);
608 DIB_SetImageBits_1( lines
, bits
, infoWidth
,
609 colorMapping
, bmpImage
);
612 if (compression
) DIB_SetImageBits_RLE4( lines
, bits
, infoWidth
,
613 colorMapping
, bmpImage
);
614 else DIB_SetImageBits_4( lines
, bits
, infoWidth
,
615 colorMapping
, bmpImage
);
618 if (compression
) DIB_SetImageBits_RLE8( lines
, bits
, infoWidth
,
619 colorMapping
, bmpImage
);
620 else DIB_SetImageBits_8( lines
, bits
, infoWidth
,
621 colorMapping
, bmpImage
);
624 DIB_SetImageBits_24( lines
, bits
, infoWidth
, dc
, bmpImage
);
627 fprintf( stderr
, "Invalid depth %d for SetDIBits!\n", infoBpp
);
630 if (colorMapping
) free(colorMapping
);
631 XPutImage( display
, drawable
, gc
, bmpImage
, xSrc
, ySrc
,
632 xDest
, yDest
, width
, height
);
633 XDestroyImage( bmpImage
);
638 /***********************************************************************
639 * StretchDIBits16 (GDI.439)
641 INT16
StretchDIBits16( HDC16 hdc
, INT16 xDst
, INT16 yDst
, INT16 widthDst
,
642 INT16 heightDst
, INT16 xSrc
, INT16 ySrc
, INT16 widthSrc
,
643 INT16 heightSrc
, const VOID
*bits
,
644 const BITMAPINFO
*info
, UINT16 wUsage
, DWORD dwRop
)
646 return (INT16
)StretchDIBits32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
647 xSrc
, ySrc
, widthSrc
, heightSrc
, bits
,
648 info
, wUsage
, dwRop
);
652 /***********************************************************************
653 * StretchDIBits32 (GDI32.351)
655 INT32
StretchDIBits32( HDC32 hdc
, INT32 xDst
, INT32 yDst
, INT32 widthDst
,
656 INT32 heightDst
, INT32 xSrc
, INT32 ySrc
, INT32 widthSrc
,
657 INT32 heightSrc
, const void *bits
,
658 const BITMAPINFO
*info
, UINT32 wUsage
, DWORD dwRop
)
660 HBITMAP32 hBitmap
, hOldBitmap
;
663 hBitmap
= CreateDIBitmap32( hdc
, &info
->bmiHeader
, CBM_INIT
,
664 bits
, info
, wUsage
);
665 hdcMem
= CreateCompatibleDC32( hdc
);
666 hOldBitmap
= SelectObject32( hdcMem
, hBitmap
);
667 StretchBlt32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
668 hdcMem
, xSrc
, ySrc
, widthSrc
, heightSrc
, dwRop
);
669 SelectObject32( hdcMem
, hOldBitmap
);
670 DeleteDC32( hdcMem
);
671 DeleteObject32( hBitmap
);
676 /***********************************************************************
677 * SetDIBits16 (GDI.440)
679 INT16
SetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
680 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
683 return SetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
687 /***********************************************************************
688 * SetDIBits32 (GDI32.312)
690 INT32
SetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
691 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
699 /* Check parameters */
701 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
704 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
707 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
709 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
, &bpp
) == -1)
711 if (!lines
|| (startscan
>= (WORD
)height
)) return 0;
712 if (startscan
+ lines
> height
) lines
= height
- startscan
;
714 return CallTo32_LargeStack( (int(*)())DIB_SetImageBits
, 16,
715 dc
, lines
, bmp
->bitmap
.bmBitsPixel
,
716 bits
, width
, bpp
, info
,
717 coloruse
, bmp
->pixmap
, BITMAP_GC(bmp
), 0, 0, 0,
718 height
- startscan
- lines
,
719 bmp
->bitmap
.bmWidth
, lines
);
723 /***********************************************************************
724 * SetDIBitsToDevice16 (GDI.443)
726 INT16
SetDIBitsToDevice16( HDC16 hdc
, INT16 xDest
, INT16 yDest
, INT16 cx
,
727 INT16 cy
, INT16 xSrc
, INT16 ySrc
, UINT16 startscan
,
728 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
731 return SetDIBitsToDevice32( hdc
, xDest
, yDest
, cx
, cy
, xSrc
, ySrc
,
732 startscan
, lines
, bits
, info
, coloruse
);
736 /***********************************************************************
737 * SetDIBitsToDevice32 (GDI32.313)
739 INT32
SetDIBitsToDevice32( HDC32 hdc
, INT32 xDest
, INT32 yDest
, DWORD cx
,
740 DWORD cy
, INT32 xSrc
, INT32 ySrc
, UINT32 startscan
,
741 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
748 /* Check parameters */
750 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
753 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
756 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
, &bpp
) == -1)
758 if (!lines
|| (startscan
>= height
)) return 0;
759 if (startscan
+ lines
> height
) lines
= height
- startscan
;
760 if (ySrc
< startscan
) ySrc
= startscan
;
761 else if (ySrc
>= startscan
+ lines
) return 0;
762 if (xSrc
>= width
) return 0;
763 if (ySrc
+ cy
>= startscan
+ lines
) cy
= startscan
+ lines
- ySrc
;
764 if (xSrc
+ cx
>= width
) cx
= width
- xSrc
;
765 if (!cx
|| !cy
) return 0;
767 DC_SetupGCForText( dc
); /* To have the correct colors */
768 XSetFunction( display
, dc
->u
.x
.gc
, DC_XROPfunction
[dc
->w
.ROPmode
-1] );
769 return CallTo32_LargeStack( (int(*)())DIB_SetImageBits
, 16,
770 dc
, lines
, dc
->w
.bitsPerPixel
, bits
, width
,
772 dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
773 xSrc
, ySrc
- startscan
,
774 dc
->w
.DCOrgX
+ XLPTODP( dc
, xDest
),
775 dc
->w
.DCOrgY
+ YLPTODP( dc
, yDest
),
781 /***********************************************************************
782 * GetDIBits16 (GDI.441)
784 INT16
GetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
785 UINT16 lines
, LPSTR bits
, BITMAPINFO
* info
,
788 return GetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
792 /***********************************************************************
793 * GetDIBits32 (GDI32.170)
795 INT32
GetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
796 UINT32 lines
, LPSTR bits
, BITMAPINFO
* info
,
801 PALETTEENTRY
* palEntry
;
802 PALETTEOBJ
* palette
;
806 if (!lines
) return 0;
807 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
810 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
813 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
815 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
818 /* Transfer color info */
820 palEntry
= palette
->logpalette
.palPalEntry
;
821 for (i
= 0; i
< info
->bmiHeader
.biClrUsed
; i
++, palEntry
++)
823 if (coloruse
== DIB_RGB_COLORS
)
825 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
826 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
827 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
828 info
->bmiColors
[i
].rgbReserved
= 0;
830 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
836 int pad
, yend
, xend
= bmp
->bitmap
.bmWidth
;
838 dprintf_bitmap(stddeb
, "GetDIBits: %u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
839 lines
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
840 (int)info
->bmiHeader
.biWidth
, (int)info
->bmiHeader
.biHeight
, startscan
);
842 /* adjust number of scanlines to copy */
844 if( lines
> info
->bmiHeader
.biHeight
) lines
= info
->bmiHeader
.biHeight
;
845 yend
= startscan
+ lines
;
846 if( startscan
>= bmp
->bitmap
.bmHeight
)
848 if( yend
> bmp
->bitmap
.bmHeight
) yend
= bmp
->bitmap
.bmHeight
;
850 /* adjust scanline width */
852 pad
= info
->bmiHeader
.biWidth
- bmp
->bitmap
.bmWidth
;
855 /* bitmap is wider than DIB, copy only a part */
857 pad
= 0; xend
= info
->bmiHeader
.biWidth
;
860 bmpImage
= (XImage
*)CallTo32_LargeStack( (int (*)())XGetImage
, 8,
861 display
, bmp
->pixmap
, 0, 0, bmp
->bitmap
.bmWidth
,
862 bmp
->bitmap
.bmHeight
, AllPlanes
, ZPixmap
);
864 switch( info
->bmiHeader
.biBitCount
)
867 /* pad up to 32 bit (FIXME: not 16? ) */
868 pad
+= (4 - (info
->bmiHeader
.biWidth
& 3)) & 3;
869 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
871 for( x
= 0; x
< xend
; x
++ )
872 *bbits
++ = XGetPixel( bmpImage
, x
, y
);
877 pad
+= ((32 - (info
->bmiHeader
.biWidth
& 31)) / 8) & 3;
878 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
881 for( x
= 0; x
< xend
; x
++ ) {
883 *bbits
|= XGetPixel( bmpImage
, x
, y
)<<(7-(x
&7));
893 pad
+= ((8 - (info
->bmiHeader
.biWidth
& 7)) / 2) & 3;
894 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
897 for( x
= 0; x
< xend
; x
++ ) {
899 *bbits
|= XGetPixel( bmpImage
, x
, y
)<<(4*(1-(x
&1)));
910 pad
+= (4 - ((info
->bmiHeader
.biWidth
*2) & 3)) & 3;
911 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
914 for( x
= 0; x
< xend
; x
++ ) {
915 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
916 *bbits
++ = pixel
& 0xff;
917 *bbits
++ = (pixel
>> 8) & 0xff;
923 pad
+= (4 - ((info
->bmiHeader
.biWidth
*3) & 3)) & 3;
924 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
927 for( x
= 0; x
< xend
; x
++ ) {
928 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
929 *bbits
++ = (pixel
>>16) & 0xff;
930 *bbits
++ = (pixel
>> 8) & 0xff;
931 *bbits
++ = pixel
& 0xff;
937 fprintf(stderr
,"GetDIBits*: unsupported depth %d\n",
938 info
->bmiHeader
.biBitCount
943 XDestroyImage( bmpImage
);
945 info
->bmiHeader
.biCompression
= 0;
947 else if( info
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
) )
949 /* fill in struct members */
951 info
->bmiHeader
.biWidth
= bmp
->bitmap
.bmWidth
;
952 info
->bmiHeader
.biHeight
= bmp
->bitmap
.bmHeight
;
953 info
->bmiHeader
.biPlanes
= 1;
954 info
->bmiHeader
.biBitCount
= bmp
->bitmap
.bmBitsPixel
;
955 info
->bmiHeader
.biSizeImage
= bmp
->bitmap
.bmHeight
*
956 DIB_GetImageWidthBytes( bmp
->bitmap
.bmWidth
,
957 bmp
->bitmap
.bmBitsPixel
);
958 info
->bmiHeader
.biCompression
= 0;
965 /***********************************************************************
966 * CreateDIBitmap16 (GDI.442)
968 HBITMAP16
CreateDIBitmap16( HDC16 hdc
, const BITMAPINFOHEADER
* header
,
969 DWORD init
, LPCVOID bits
, const BITMAPINFO
* data
,
972 return CreateDIBitmap32( hdc
, header
, init
, bits
, data
, coloruse
);
976 /***********************************************************************
977 * CreateDIBitmap32 (GDI32.37)
979 HBITMAP32
CreateDIBitmap32( HDC32 hdc
, const BITMAPINFOHEADER
*header
,
980 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
988 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
) == -1) return 0;
990 /* Check if we should create a monochrome or color bitmap. */
991 /* We create a monochrome bitmap only if it has exactly 2 */
992 /* colors, which are either black or white, nothing else. */
993 /* In all other cases, we create a color bitmap. */
995 if (bpp
!= 1) fColor
= TRUE
;
996 else if ((coloruse
!= DIB_RGB_COLORS
) ||
997 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1000 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
1002 RGBQUAD
*rgb
= data
->bmiColors
;
1003 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1004 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
1007 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1008 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
1012 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1014 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
1015 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
1016 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
1019 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
1020 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
1026 fprintf( stderr
, "CreateDIBitmap: wrong size (%ld) for data\n",
1027 data
->bmiHeader
.biSize
);
1032 /* Now create the bitmap */
1034 handle
= fColor
? CreateCompatibleBitmap32( hdc
, width
, height
) :
1035 CreateBitmap32( width
, height
, 1, 1, NULL
);
1036 if (!handle
) return 0;
1038 if (init
== CBM_INIT
)
1039 SetDIBits32( hdc
, handle
, 0, height
, bits
, data
, coloruse
);