2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
10 #include <X11/Xutil.h>
19 extern void CLIPPING_UpdateGCRegion(DC
* );
21 static int bitmapDepthTable
[] = { 8, 1, 32, 16, 24, 15, 4, 0 };
22 static int ximageDepthTable
[] = { 0, 0, 0, 0, 0, 0, 0 };
25 /* This structure holds the arguments for DIB_SetImageBits() */
34 const BITMAPINFO
*info
;
44 } DIB_SETIMAGEBITS_DESCR
;
47 /***********************************************************************
55 for( i
= 0; bitmapDepthTable
[i
]; i
++ )
57 testimage
= XCreateImage(display
, DefaultVisualOfScreen(screen
),
58 bitmapDepthTable
[i
], ZPixmap
, 0, NULL
, 1, 1, 32, 20 );
59 if( testimage
) ximageDepthTable
[i
] = testimage
->bits_per_pixel
;
61 XDestroyImage(testimage
);
66 /***********************************************************************
67 * DIB_GetXImageWidthBytes
69 * Return the width of an X image in bytes
71 int DIB_GetXImageWidthBytes( int width
, int depth
)
75 if (!ximageDepthTable
[0]) {
78 for( i
= 0; bitmapDepthTable
[i
] ; i
++ )
79 if( bitmapDepthTable
[i
] == depth
)
80 return (4 * ((width
* ximageDepthTable
[i
] + 31)/32));
81 fprintf(stderr
, "DIB: unsupported depth %d.\n", depth
);
85 /***********************************************************************
86 * DIB_GetDIBWidthBytes
88 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
89 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
91 int DIB_GetDIBWidthBytes( int width
, int depth
)
97 case 1: words
= (width
+ 31) / 32; break;
98 case 4: words
= (width
+ 7) / 8; break;
99 case 8: words
= (width
+ 3) / 4; break;
101 case 16: words
= (width
+ 1) / 2; break;
102 case 24: words
= (width
* 3 + 3)/4; break;
105 fprintf(stderr
, "DIB: unsupported depth %d.\n", depth
);
114 /***********************************************************************
117 * Return the size of the bitmap info structure including color table.
119 int DIB_BitmapInfoSize( BITMAPINFO
* info
, WORD coloruse
)
123 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
125 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
126 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
127 return sizeof(BITMAPCOREHEADER
) + colors
*
128 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
130 else /* assume BITMAPINFOHEADER */
132 colors
= info
->bmiHeader
.biClrUsed
;
133 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
134 colors
= 1 << info
->bmiHeader
.biBitCount
;
135 return sizeof(BITMAPINFOHEADER
) + colors
*
136 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
141 /***********************************************************************
144 * Get the info from a bitmap header.
145 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
147 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
148 DWORD
*height
, WORD
*bpp
)
150 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
152 *width
= header
->biWidth
;
153 *height
= header
->biHeight
;
154 *bpp
= header
->biBitCount
;
157 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
159 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
160 *width
= core
->bcWidth
;
161 *height
= core
->bcHeight
;
162 *bpp
= core
->bcBitCount
;
165 fprintf( stderr
, "DIB_GetBitmapInfo: wrong size (%ld) for header\n",
171 /***********************************************************************
174 * Build the color map from the bitmap palette. Should not be called
175 * for a 24-bit deep bitmap.
177 static int *DIB_BuildColorMap( DC
*dc
, WORD coloruse
, WORD depth
,
178 const BITMAPINFO
*info
)
185 if ((isInfo
= (info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))))
187 colors
= info
->bmiHeader
.biClrUsed
;
188 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
189 colorPtr
= (WORD
*)info
->bmiColors
;
191 else /* assume BITMAPCOREINFO */
193 colors
= 1 << ((BITMAPCOREHEADER
*)&info
->bmiHeader
)->bcBitCount
;
194 colorPtr
= (WORD
*)((BITMAPCOREINFO
*)info
)->bmciColors
;
196 if (!(colorMapping
= (int *)malloc( colors
* sizeof(int) ))) return NULL
;
198 if (coloruse
== DIB_RGB_COLORS
)
202 RGBQUAD
* rgb
= (RGBQUAD
*)colorPtr
;
204 if (depth
== 1) /* Monochrome */
205 for (i
= 0; i
< colors
; i
++, rgb
++)
206 colorMapping
[i
] = (rgb
->rgbRed
+ rgb
->rgbGreen
+
207 rgb
->rgbBlue
> 255*3/2);
209 for (i
= 0; i
< colors
; i
++, rgb
++)
210 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbRed
,
216 RGBTRIPLE
* rgb
= (RGBTRIPLE
*)colorPtr
;
218 if (depth
== 1) /* Monochrome */
219 for (i
= 0; i
< colors
; i
++, rgb
++)
220 colorMapping
[i
] = (rgb
->rgbtRed
+ rgb
->rgbtGreen
+
221 rgb
->rgbtBlue
> 255*3/2);
223 for (i
= 0; i
< colors
; i
++, rgb
++)
224 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbtRed
,
229 else /* DIB_PAL_COLORS */
231 for (i
= 0; i
< colors
; i
++, colorPtr
++)
232 colorMapping
[i
] = COLOR_ToPhysical( dc
, PALETTEINDEX(*colorPtr
) );
238 /***********************************************************************
241 * SetDIBits for a 1-bit deep DIB.
243 static void DIB_SetImageBits_1( DWORD lines
, const BYTE
*srcbits
,
244 DWORD srcwidth
, DWORD dstwidth
,
245 int *colors
, XImage
*bmpImage
)
249 const BYTE
*bits
= srcbits
;
252 DWORD linebytes
= ((srcwidth
+ 31) & ~31) / 8;
256 for (i
= dstwidth
/8, x
= 0; (i
> 0); i
--)
259 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
260 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 6) & 1] );
261 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 5) & 1] );
262 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 4) & 1] );
263 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 3) & 1] );
264 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 2) & 1] );
265 XPutPixel( bmpImage
, x
++, lines
, colors
[(pix
>> 1) & 1] );
266 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 1] );
271 case 7: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
272 case 6: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
273 case 5: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
274 case 4: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
275 case 3: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
276 case 2: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] ); pix
<<= 1;
277 case 1: XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 7] );
279 srcbits
+= linebytes
;
285 /***********************************************************************
288 * SetDIBits for a 4-bit deep DIB.
290 static void DIB_SetImageBits_4( DWORD lines
, const BYTE
*srcbits
,
291 DWORD srcwidth
, DWORD dstwidth
,
292 int *colors
, XImage
*bmpImage
)
295 const BYTE
*bits
= srcbits
;
298 DWORD linebytes
= ((srcwidth
+7)&~7)/2;
302 for (i
= dstwidth
/2, x
= 0; i
> 0; i
--)
305 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
>> 4] );
306 XPutPixel( bmpImage
, x
++, lines
, colors
[pix
& 0x0f] );
308 if (dstwidth
& 1) XPutPixel( bmpImage
, x
, lines
, colors
[*bits
>> 4] );
309 srcbits
+= linebytes
;
314 #define check_xy(x,y) \
321 /***********************************************************************
322 * DIB_SetImageBits_RLE4
324 * SetDIBits for a 4-bit deep compressed DIB.
326 static void DIB_SetImageBits_RLE4( DWORD lines
, const BYTE
*bits
, DWORD width
,
327 DWORD dstwidth
, int *colors
, XImage
*bmpImage
)
329 int x
= 0, c
, length
;
330 const BYTE
*begin
= bits
;
333 while ((int)lines
>= 0)
336 if (length
) { /* encoded */
339 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
343 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
355 case 1: /* eopicture */
363 default: /* absolute */
366 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
370 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
374 if ((bits
- begin
) & 1)
381 /***********************************************************************
384 * SetDIBits for an 8-bit deep DIB.
386 static void DIB_SetImageBits_8( DWORD lines
, const BYTE
*srcbits
,
387 DWORD srcwidth
, DWORD dstwidth
,
388 int *colors
, XImage
*bmpImage
)
391 const BYTE
*bits
= srcbits
;
393 /* align to 32 bit */
394 DWORD linebytes
= (srcwidth
+ 3) & ~3;
398 for (x
= 0; x
< dstwidth
; x
++)
399 XPutPixel( bmpImage
, x
, lines
, colors
[*bits
++] );
400 srcbits
+= linebytes
;
405 /***********************************************************************
406 * DIB_SetImageBits_RLE8
408 * SetDIBits for an 8-bit deep compressed DIB.
410 * This function rewritten 941113 by James Youngman. WINE blew out when I
411 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
413 * This was because the algorithm assumed that all RLE8 bitmaps end with the
414 * 'End of bitmap' escape code. This code is very much laxer in what it
415 * allows to end the expansion. Possibly too lax. See the note by
416 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
417 * bitmap should end with RleEnd, but on the other hand, software exists
418 * that produces ones that don't and Windows 3.1 doesn't complain a bit
421 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
422 * James A. Youngman <mbcstjy@afs.man.ac.uk>
426 enum Rle8_EscapeCodes
429 * Apologies for polluting your file's namespace...
431 RleEol
= 0, /* End of line */
432 RleEnd
= 1, /* End of bitmap */
433 RleDelta
= 2 /* Delta */
436 static void DIB_SetImageBits_RLE8( DWORD lines
, const BYTE
*bits
, DWORD width
,
437 DWORD dstwidth
, int *colors
, XImage
*bmpImage
)
439 int x
; /* X-positon on each line. Increases. */
440 int line
; /* Line #. Starts at lines-1, decreases */
441 const BYTE
*pIn
= bits
; /* Pointer to current position in bits */
442 BYTE length
; /* The length pf a run */
443 BYTE color_index
; /* index into colors[] as read from bits */
444 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
445 WORD color
; /* value of colour[color_index] */
447 if (lines
== 0) /* Let's hope this doesn't happen. */
451 * Note that the bitmap data is stored by Windows starting at the
452 * bottom line of the bitmap and going upwards. Within each line,
453 * the data is stored left-to-right. That's the reason why line
454 * goes from lines-1 to 0. [JAY]
464 * If the length byte is not zero (which is the escape value),
465 * We have a run of length pixels all the same colour. The colour
466 * index is stored next.
468 * If the length byte is zero, we need to read the next byte to
469 * know what to do. [JAY]
474 * [Run-Length] Encoded mode
476 color_index
= (*pIn
++); /* Get the colour index. */
477 color
= colors
[color_index
];
480 XPutPixel(bmpImage
, x
++, line
, color
);
485 * Escape codes (may be an absolute sequence though)
487 escape_code
= (*pIn
++);
490 case RleEol
: /* =0, end of line */
497 case RleEnd
: /* =1, end of bitmap */
500 * Not all RLE8 bitmaps end with this
501 * code. For example, Paint Shop Pro
502 * produces some that don't. That's (I think)
503 * what caused the previous implementation to
506 line
=-1; /* Cause exit from do loop. */
510 case RleDelta
: /* =2, a delta */
513 * Note that deltaing to line 0
514 * will cause an exit from the loop,
515 * which may not be what is intended.
516 * The fact that there is a delta in the bits
517 * almost certainly implies that there is data
518 * to follow. You may feel that we should
519 * jump to the top of the loop to avoid exiting
522 * TODO: Decide what to do here in that case. [JAY]
528 dprintf_bitmap(stddeb
,
529 "DIB_SetImageBits_RLE8(): "
530 "Delta to last line of bitmap "
531 "(wrongly?) causes loop exit\n");
536 default: /* >2, switch to absolute mode */
541 length
= escape_code
;
544 color_index
= (*pIn
++);
545 XPutPixel(bmpImage
, x
++, line
,
546 colors
[color_index
]);
550 * If you think for a moment you'll realise that the
551 * only time we could ever possibly read an odd
552 * number of bytes is when there is a 0x00 (escape),
553 * a value >0x02 (absolute mode) and then an odd-
554 * length run. Therefore this is the only place we
555 * need to worry about it. Everywhere else the
556 * bytes are always read in pairs. [JAY]
559 pIn
++; /* Throw away the pad byte. */
562 } /* switch (escape_code) : Escape sequence */
563 } /* process either an encoded sequence or an escape sequence */
565 /* We expect to come here more than once per line. */
566 } while (line
>= 0); /* Do this until the bitmap is filled */
569 * Everybody comes here at the end.
570 * Check how we exited the loop and print a message if it's a bit odd.
573 if ( (*(pIn
-2) != 0/*escape*/) || (*(pIn
-1)!= RleEnd
) )
575 dprintf_bitmap(stddeb
, "DIB_SetImageBits_RLE8(): End-of-bitmap "
576 "without (strictly) proper escape code. Last two "
577 "bytes were: %02X %02X.\n",
584 /***********************************************************************
585 * DIB_SetImageBits_24
587 * SetDIBits for a 24-bit deep DIB.
589 static void DIB_SetImageBits_24( DWORD lines
, const BYTE
*srcbits
,
590 DWORD srcwidth
, DWORD dstwidth
,
591 DC
*dc
, XImage
*bmpImage
)
594 const BYTE
*bits
= srcbits
;
596 /* align to 32 bit */
597 DWORD linebytes
= (srcwidth
* 3 + 3) & ~3;
599 /* "bits" order is reversed for some reason */
603 for (x
= 0; x
< dstwidth
; x
++, bits
+= 3)
604 XPutPixel( bmpImage
, x
, lines
,
605 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])) );
607 bits
= (srcbits
+= linebytes
);
612 /***********************************************************************
615 * Transfer the bits to an X image.
616 * Helper function for SetDIBits() and SetDIBitsToDevice().
618 static int DIB_SetImageBits( const DIB_SETIMAGEBITS_DESCR
*descr
)
622 DWORD compression
= 0;
624 if (descr
->info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
625 compression
= descr
->info
->bmiHeader
.biCompression
;
627 /* Build the color mapping table */
629 if (descr
->infoBpp
> 8) colorMapping
= NULL
;
630 else if (!(colorMapping
= DIB_BuildColorMap( descr
->dc
, descr
->coloruse
,
631 descr
->depth
, descr
->info
)))
634 if( descr
->dc
->w
.flags
& DC_DIRTY
) CLIPPING_UpdateGCRegion(descr
->dc
);
636 /* Transfer the pixels */
637 XCREATEIMAGE(bmpImage
, descr
->infoWidth
, descr
->lines
, descr
->depth
);
639 switch(descr
->infoBpp
)
642 DIB_SetImageBits_1( descr
->lines
, descr
->bits
, descr
->infoWidth
,
643 descr
->width
, colorMapping
, bmpImage
);
646 if (compression
) DIB_SetImageBits_RLE4( descr
->lines
, descr
->bits
,
647 descr
->infoWidth
, descr
->width
,
648 colorMapping
, bmpImage
);
649 else DIB_SetImageBits_4( descr
->lines
, descr
->bits
, descr
->infoWidth
,
650 descr
->width
, colorMapping
, bmpImage
);
653 if (compression
) DIB_SetImageBits_RLE8( descr
->lines
, descr
->bits
,
654 descr
->infoWidth
, descr
->width
,
655 colorMapping
, bmpImage
);
656 else DIB_SetImageBits_8( descr
->lines
, descr
->bits
, descr
->infoWidth
,
657 descr
->width
, colorMapping
, bmpImage
);
660 DIB_SetImageBits_24( descr
->lines
, descr
->bits
, descr
->infoWidth
,
661 descr
->width
, descr
->dc
, bmpImage
);
664 fprintf( stderr
, "Invalid depth %d for SetDIBits!\n", descr
->infoBpp
);
667 if (colorMapping
) free(colorMapping
);
668 XPutImage( display
, descr
->drawable
, descr
->gc
, bmpImage
,
669 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
670 descr
->width
, descr
->height
);
671 XDestroyImage( bmpImage
);
676 /***********************************************************************
677 * StretchDIBits16 (GDI.439)
679 INT16 WINAPI
StretchDIBits16(HDC16 hdc
, INT16 xDst
, INT16 yDst
, INT16 widthDst
,
680 INT16 heightDst
, INT16 xSrc
, INT16 ySrc
, INT16 widthSrc
,
681 INT16 heightSrc
, const VOID
*bits
,
682 const BITMAPINFO
*info
, UINT16 wUsage
, DWORD dwRop
)
684 return (INT16
)StretchDIBits32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
685 xSrc
, ySrc
, widthSrc
, heightSrc
, bits
,
686 info
, wUsage
, dwRop
);
690 /***********************************************************************
691 * StretchDIBits32 (GDI32.351)
693 INT32 WINAPI
StretchDIBits32(HDC32 hdc
, INT32 xDst
, INT32 yDst
, INT32 widthDst
,
694 INT32 heightDst
, INT32 xSrc
, INT32 ySrc
, INT32 widthSrc
,
695 INT32 heightSrc
, const void *bits
,
696 const BITMAPINFO
*info
, UINT32 wUsage
, DWORD dwRop
)
698 HBITMAP32 hBitmap
, hOldBitmap
;
701 hBitmap
= CreateDIBitmap32( hdc
, &info
->bmiHeader
, CBM_INIT
,
702 bits
, info
, wUsage
);
703 hdcMem
= CreateCompatibleDC32( hdc
);
704 hOldBitmap
= SelectObject32( hdcMem
, hBitmap
);
705 StretchBlt32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
706 hdcMem
, xSrc
, ySrc
, widthSrc
, heightSrc
, dwRop
);
707 SelectObject32( hdcMem
, hOldBitmap
);
708 DeleteDC32( hdcMem
);
709 DeleteObject32( hBitmap
);
714 /***********************************************************************
715 * SetDIBits16 (GDI.440)
717 INT16 WINAPI
SetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
718 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
721 return SetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
725 /***********************************************************************
726 * SetDIBits32 (GDI32.312)
728 INT32 WINAPI
SetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
729 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
732 DIB_SETIMAGEBITS_DESCR descr
;
737 /* Check parameters */
739 descr
.dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
742 descr
.dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
743 if (!descr
.dc
) return 0;
745 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
747 GDI_HEAP_UNLOCK( hdc
);
750 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &descr
.infoWidth
, &height
,
751 &descr
.infoBpp
) == -1)
753 GDI_HEAP_UNLOCK( hbitmap
);
754 GDI_HEAP_UNLOCK( hdc
);
757 if (!lines
|| (startscan
>= height
))
759 GDI_HEAP_UNLOCK( hbitmap
);
760 GDI_HEAP_UNLOCK( hdc
);
763 if (startscan
+ lines
> height
) lines
= height
- startscan
;
767 descr
.depth
= bmp
->bitmap
.bmBitsPixel
;
769 descr
.coloruse
= coloruse
;
770 descr
.drawable
= bmp
->pixmap
;
771 descr
.gc
= BITMAP_GC(bmp
);
775 descr
.yDest
= height
- startscan
- lines
;
776 descr
.width
= bmp
->bitmap
.bmWidth
;
777 descr
.height
= lines
;
779 result
= CALL_LARGE_STACK( DIB_SetImageBits
, &descr
);
780 GDI_HEAP_UNLOCK( hdc
);
781 GDI_HEAP_UNLOCK( hbitmap
);
786 /***********************************************************************
787 * SetDIBitsToDevice16 (GDI.443)
789 INT16 WINAPI
SetDIBitsToDevice16(HDC16 hdc
, INT16 xDest
, INT16 yDest
, INT16 cx
,
790 INT16 cy
, INT16 xSrc
, INT16 ySrc
, UINT16 startscan
,
791 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
794 return SetDIBitsToDevice32( hdc
, xDest
, yDest
, cx
, cy
, xSrc
, ySrc
,
795 startscan
, lines
, bits
, info
, coloruse
);
799 /***********************************************************************
800 * SetDIBitsToDevice32 (GDI32.313)
802 INT32 WINAPI
SetDIBitsToDevice32(HDC32 hdc
, INT32 xDest
, INT32 yDest
, DWORD cx
,
803 DWORD cy
, INT32 xSrc
, INT32 ySrc
, UINT32 startscan
,
804 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
807 DIB_SETIMAGEBITS_DESCR descr
;
811 /* Check parameters */
813 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
816 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
819 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
,
820 &height
, &descr
.infoBpp
) == -1)
822 if (!lines
|| (startscan
>= height
)) return 0;
823 if (startscan
+ lines
> height
) lines
= height
- startscan
;
824 if (ySrc
< startscan
) ySrc
= startscan
;
825 else if (ySrc
>= startscan
+ lines
) return 0;
826 if (xSrc
>= width
) return 0;
827 if (ySrc
+ cy
>= startscan
+ lines
) cy
= startscan
+ lines
- ySrc
;
828 if (xSrc
+ cx
>= width
) cx
= width
- xSrc
;
829 if (!cx
|| !cy
) return 0;
831 DC_SetupGCForText( dc
); /* To have the correct colors */
832 XSetFunction( display
, dc
->u
.x
.gc
, DC_XROPfunction
[dc
->w
.ROPmode
-1] );
837 descr
.infoWidth
= width
;
838 descr
.depth
= dc
->w
.bitsPerPixel
;
840 descr
.coloruse
= coloruse
;
841 descr
.drawable
= dc
->u
.x
.drawable
;
842 descr
.gc
= dc
->u
.x
.gc
;
844 descr
.ySrc
= ySrc
- startscan
;
845 descr
.xDest
= dc
->w
.DCOrgX
+ XLPTODP( dc
, xDest
);
846 descr
.yDest
= dc
->w
.DCOrgY
+ YLPTODP( dc
, yDest
);
850 return CALL_LARGE_STACK( DIB_SetImageBits
, &descr
);
855 /***********************************************************************
856 * GetDIBits16 (GDI.441)
858 INT16 WINAPI
GetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
859 UINT16 lines
, LPSTR bits
, BITMAPINFO
* info
,
862 return GetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
866 /***********************************************************************
867 * GetDIBits32 (GDI32.170)
869 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
871 INT32 WINAPI
GetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
872 UINT32 lines
, LPSTR bits
, BITMAPINFO
* info
,
877 PALETTEENTRY
* palEntry
;
878 PALETTEOBJ
* palette
;
882 if (!lines
) return 0;
883 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
886 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
889 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
891 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
893 GDI_HEAP_UNLOCK( hbitmap
);
897 /* Transfer color info */
899 if (info
->bmiHeader
.biBitCount
<=8) {
900 palEntry
= palette
->logpalette
.palPalEntry
;
901 for (i
= 0; i
< info
->bmiHeader
.biClrUsed
; i
++, palEntry
++)
903 if (coloruse
== DIB_RGB_COLORS
)
905 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
906 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
907 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
908 info
->bmiColors
[i
].rgbReserved
= 0;
910 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
917 int pad
, yend
, xend
= bmp
->bitmap
.bmWidth
;
919 dprintf_bitmap(stddeb
, "GetDIBits: %u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
920 lines
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
921 (int)info
->bmiHeader
.biWidth
, (int)info
->bmiHeader
.biHeight
, startscan
);
923 /* adjust number of scanlines to copy */
925 if( lines
> info
->bmiHeader
.biHeight
) lines
= info
->bmiHeader
.biHeight
;
926 yend
= startscan
+ lines
;
927 if( startscan
>= bmp
->bitmap
.bmHeight
)
929 GDI_HEAP_UNLOCK( hbitmap
);
930 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
933 if( yend
> bmp
->bitmap
.bmHeight
) yend
= bmp
->bitmap
.bmHeight
;
935 /* adjust scanline width */
937 pad
= info
->bmiHeader
.biWidth
- bmp
->bitmap
.bmWidth
;
940 /* bitmap is wider than DIB, copy only a part */
943 xend
= info
->bmiHeader
.biWidth
;
946 bmpImage
= (XImage
*)CALL_LARGE_STACK( BITMAP_GetXImage
, bmp
);
948 switch( info
->bmiHeader
.biBitCount
)
951 /* pad up to 32 bit */
952 pad
+= (4 - (info
->bmiHeader
.biWidth
& 3)) & 3;
953 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
955 for( x
= 0; x
< xend
; x
++ )
956 *bbits
++ = XGetPixel( bmpImage
, x
, y
);
961 pad
+= ((32 - (info
->bmiHeader
.biWidth
& 31)) / 8) & 3;
962 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
965 for( x
= 0; x
< xend
; x
++ ) {
967 *bbits
|= XGetPixel( bmpImage
, x
, y
)<<(7-(x
&7));
977 pad
+= ((8 - (info
->bmiHeader
.biWidth
& 7)) / 2) & 3;
978 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
981 for( x
= 0; x
< xend
; x
++ ) {
983 *bbits
|= XGetPixel( bmpImage
, x
, y
)<<(4*(1-(x
&1)));
994 pad
+= (4 - ((info
->bmiHeader
.biWidth
*2) & 3)) & 3;
995 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
998 for( x
= 0; x
< xend
; x
++ ) {
999 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
1000 *bbits
++ = pixel
& 0xff;
1001 *bbits
++ = (pixel
>> 8) & 0xff;
1007 pad
+= (4 - ((info
->bmiHeader
.biWidth
*3) & 3)) & 3;
1008 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1011 for( x
= 0; x
< xend
; x
++ ) {
1012 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
1013 *bbits
++ = (pixel
>>16) & 0xff;
1014 *bbits
++ = (pixel
>> 8) & 0xff;
1015 *bbits
++ = pixel
& 0xff;
1021 fprintf(stderr
,"GetDIBits*: unsupported depth %d\n",
1022 info
->bmiHeader
.biBitCount
1027 XDestroyImage( bmpImage
);
1029 info
->bmiHeader
.biCompression
= 0;
1031 else if( info
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
) )
1033 /* fill in struct members */
1035 info
->bmiHeader
.biWidth
= bmp
->bitmap
.bmWidth
;
1036 info
->bmiHeader
.biHeight
= bmp
->bitmap
.bmHeight
;
1037 info
->bmiHeader
.biPlanes
= 1;
1038 info
->bmiHeader
.biBitCount
= bmp
->bitmap
.bmBitsPixel
;
1039 info
->bmiHeader
.biSizeImage
= bmp
->bitmap
.bmHeight
*
1040 DIB_GetDIBWidthBytes( bmp
->bitmap
.bmWidth
,
1041 bmp
->bitmap
.bmBitsPixel
);
1042 info
->bmiHeader
.biCompression
= 0;
1045 GDI_HEAP_UNLOCK( hbitmap
);
1046 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1051 /***********************************************************************
1052 * CreateDIBitmap16 (GDI.442)
1054 HBITMAP16 WINAPI
CreateDIBitmap16( HDC16 hdc
, const BITMAPINFOHEADER
* header
,
1055 DWORD init
, LPCVOID bits
, const BITMAPINFO
* data
,
1058 return CreateDIBitmap32( hdc
, header
, init
, bits
, data
, coloruse
);
1062 /***********************************************************************
1063 * CreateDIBitmap32 (GDI32.37)
1065 HBITMAP32 WINAPI
CreateDIBitmap32( HDC32 hdc
, const BITMAPINFOHEADER
*header
,
1066 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
1071 DWORD width
, height
;
1074 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
) == -1) return 0;
1076 /* Check if we should create a monochrome or color bitmap. */
1077 /* We create a monochrome bitmap only if it has exactly 2 */
1078 /* colors, which are either black or white, nothing else. */
1079 /* In all other cases, we create a color bitmap. */
1081 if (bpp
!= 1) fColor
= TRUE
;
1082 else if ((coloruse
!= DIB_RGB_COLORS
) ||
1083 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1086 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
1088 RGBQUAD
*rgb
= data
->bmiColors
;
1089 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1090 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
1093 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1094 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
1098 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1100 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
1101 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
1102 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
1105 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
1106 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
1112 fprintf( stderr
, "CreateDIBitmap: wrong size (%ld) for data\n",
1113 data
->bmiHeader
.biSize
);
1118 /* Now create the bitmap */
1120 handle
= fColor
? CreateCompatibleBitmap32( hdc
, width
, height
) :
1121 CreateBitmap32( width
, height
, 1, 1, NULL
);
1122 if (!handle
) return 0;
1124 if (init
== CBM_INIT
)
1125 SetDIBits32( hdc
, handle
, 0, height
, bits
, data
, coloruse
);
1129 HBITMAP32
CreateDIBSection(
1130 HDC32 hdc
,BITMAPINFO
*bmi
,UINT32 usage
, LPVOID
**bits
,HANDLE32 section
,
1133 fprintf(stderr
,"CreateDIBSection(%d,[w=%ld,h=%ld],%d,%p,0x%08x,%ld),stub\n",
1134 hdc
,bmi
->bmiHeader
.biWidth
,bmi
->bmiHeader
.biHeight
,usage
,bits
,section
,offset