Release 970509
[wine/testsucceed.git] / objects / dib.c
blob0eb78f0c47d9761c3c5e02490e4df3b664cbade5
1 /*
2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
11 #include "dc.h"
12 #include "bitmap.h"
13 #include "callback.h"
14 #include "palette.h"
15 #include "stddebug.h"
16 #include "color.h"
17 #include "debug.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 )
28 int wbits;
29 XImage *testimage;
31 #define DEPTHCASE(depth) \
32 case depth: { \
33 static int w##depth = 0; \
34 if (! w##depth ) { \
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); \
39 } \
40 wbits = width*w##depth; \
41 break; \
44 switch(depth)
46 DEPTHCASE(1);
47 DEPTHCASE(4);
48 DEPTHCASE(8);
49 DEPTHCASE(15);
50 DEPTHCASE(16);
51 DEPTHCASE(24);
52 DEPTHCASE(32);
53 default:
54 fprintf(stderr, "DIB: unsupported depth %d.\n", depth );
55 /* assume 32 bits/pixel */
56 wbits = width*32;
57 break;
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 )
69 int words;
71 switch(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;
76 case 15:
77 case 16: words = (width + 1) / 2; break;
78 case 24: words = (width * 3 + 3)/4; break;
80 default:
81 fprintf(stderr, "DIB: unsupported depth %d.\n", depth );
82 /* fall through */
83 case 32:
84 words = width;
86 return 4 * words;
90 /***********************************************************************
91 * DIB_BitmapInfoSize
93 * Return the size of the bitmap info structure.
95 int DIB_BitmapInfoSize( BITMAPINFO * info, WORD coloruse )
97 int colors;
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 /***********************************************************************
118 * DIB_GetBitmapInfo
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;
131 return 1;
133 if (header->biSize == sizeof(BITMAPCOREHEADER))
135 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
136 *width = core->bcWidth;
137 *height = core->bcHeight;
138 *bpp = core->bcBitCount;
139 return 0;
141 fprintf( stderr, "DIB_GetBitmapInfo: wrong size (%ld) for header\n",
142 header->biSize );
143 return -1;
147 /***********************************************************************
148 * DIB_BuildColorMap
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,
154 BITMAPINFO *info )
156 int i, colors;
157 BOOL32 isInfo;
158 WORD *colorPtr;
159 int *colorMapping;
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)
176 if (isInfo)
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);
184 else
185 for (i = 0; i < colors; i++, rgb++)
186 colorMapping[i] = COLOR_ToPhysical( dc, RGB(rgb->rgbRed,
187 rgb->rgbGreen,
188 rgb->rgbBlue));
190 else
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);
198 else
199 for (i = 0; i < colors; i++, rgb++)
200 colorMapping[i] = COLOR_ToPhysical( dc, RGB(rgb->rgbtRed,
201 rgb->rgbtGreen,
202 rgb->rgbtBlue));
205 else /* DIB_PAL_COLORS */
207 for (i = 0; i < colors; i++, colorPtr++)
208 colorMapping[i] = COLOR_ToPhysical( dc, PALETTEINDEX(*colorPtr) );
210 return colorMapping;
214 /***********************************************************************
215 * DIB_SetImageBits_1
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 )
222 DWORD i, x;
223 BYTE pad, pix;
225 if (!(width & 31)) pad = 0;
226 else pad = ((32 - (width & 31)) + 7) / 8;
228 while (lines--)
230 for (i = width/8, x = 0; (i > 0); i--)
232 pix = *bits++;
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] );
242 pix = *bits;
243 switch(width & 7)
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] );
253 bits += pad;
258 /***********************************************************************
259 * DIB_SetImageBits_4
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 )
266 DWORD i, x;
267 BYTE pad;
269 if (!(width & 7)) pad = 0;
270 else pad = ((8 - (width & 7)) + 1) / 2;
272 while (lines--)
274 for (i = width/2, x = 0; i > 0; i--)
276 BYTE pix = *bits++;
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] );
281 bits += pad;
285 #define check_xy(x,y) \
286 if (x > width) { \
287 x = 0; \
288 if (lines) \
289 lines--; \
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;
301 BYTE *begin = bits;
303 lines--;
304 while ((int)lines >= 0)
306 length = *bits++;
307 if (length) { /* encoded */
308 c = *bits++;
309 while (length--) {
310 XPutPixel(bmpImage, x++, lines, colors[c >> 4]);
311 check_xy(x, y);
312 if (length) {
313 length--;
314 XPutPixel(bmpImage, x++, lines, colors[c & 0xf]);
315 check_xy(x, y);
318 } else {
319 length = *bits++;
320 switch (length) {
321 case 0: /* eol */
322 x = 0;
323 lines--;
324 continue;
326 case 1: /* eopicture */
327 return;
329 case 2: /* delta */
330 x += *bits++;
331 lines -= *bits++;
332 continue;
334 default: /* absolute */
335 while (length--) {
336 c = *bits++;
337 XPutPixel(bmpImage, x++, lines, colors[c >> 4]);
338 check_xy(x, y);
339 if (length) {
340 length--;
341 XPutPixel(bmpImage, x++, lines, colors[c & 0xf]);
342 check_xy(x, y);
345 if ((bits - begin) & 1)
346 bits++;
352 /***********************************************************************
353 * DIB_SetImageBits_8
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 )
360 DWORD x;
361 BYTE pad = (4 - (width & 3)) & 3;
363 while (lines--)
365 for (x = 0; x < width; x++)
366 XPutPixel( bmpImage, x, lines, colors[*bits++] );
367 bits += pad;
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
385 * about it.
387 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
388 * James A. Youngman <mbcstjy@afs.man.ac.uk>
389 * [JAY]
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. */
414 return;
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]
423 x = 0;
424 line = lines-1;
427 length = *pIn++;
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]
437 if (length != 0)
440 * [Run-Length] Encoded mode
442 color_index = (*pIn++); /* Get the colour index. */
443 color = colors[color_index];
445 while(length--)
446 XPutPixel(bmpImage, x++, line, color);
448 else
451 * Escape codes (may be an absolute sequence though)
453 escape_code = (*pIn++);
454 switch(escape_code)
456 case RleEol: /* =0, end of line */
458 x = 0;
459 line--;
460 break;
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
470 * fail. [JAY]
472 line=-1; /* Cause exit from do loop. */
473 break;
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
486 * in this case.
488 * TODO: Decide what to do here in that case. [JAY]
490 x += (*pIn++);
491 line -= (*pIn++);
492 if (line == 0)
494 dprintf_bitmap(stddeb,
495 "DIB_SetImageBits_RLE8(): "
496 "Delta to last line of bitmap "
497 "(wrongly?) causes loop exit\n");
499 break;
502 default: /* >2, switch to absolute mode */
505 * Absolute Mode
507 length = escape_code;
508 while(length--)
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]
524 if (escape_code & 1)
525 pIn++; /* Throw away the pad byte. */
526 break;
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.
537 * [JAY]
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",
544 (int)*(pIn-2),
545 (int)*(pIn-1));
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 )
558 DWORD x;
559 BYTE pad = (4 - ((width*3) & 3)) & 3;
561 /* "bits" order is reversed for some reason */
563 while (lines--)
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])) );
569 bits += pad;
574 /***********************************************************************
575 * DIB_SetImageBits
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 )
586 int *colorMapping;
587 XImage *bmpImage;
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;
596 else
597 if (!(colorMapping = DIB_BuildColorMap( dc, coloruse, depth, info )))
598 return 0;
600 if( dc->w.flags & DC_DIRTY ) CLIPPING_UpdateGCRegion(dc);
602 /* Transfer the pixels */
603 XCREATEIMAGE(bmpImage, infoWidth, lines, depth );
605 switch(infoBpp)
607 case 1:
608 DIB_SetImageBits_1( lines, bits, infoWidth,
609 colorMapping, bmpImage );
610 break;
611 case 4:
612 if (compression) DIB_SetImageBits_RLE4( lines, bits, infoWidth,
613 colorMapping, bmpImage );
614 else DIB_SetImageBits_4( lines, bits, infoWidth,
615 colorMapping, bmpImage );
616 break;
617 case 8:
618 if (compression) DIB_SetImageBits_RLE8( lines, bits, infoWidth,
619 colorMapping, bmpImage );
620 else DIB_SetImageBits_8( lines, bits, infoWidth,
621 colorMapping, bmpImage );
622 break;
623 case 24:
624 DIB_SetImageBits_24( lines, bits, infoWidth, dc, bmpImage );
625 break;
626 default:
627 fprintf( stderr, "Invalid depth %d for SetDIBits!\n", infoBpp );
628 break;
630 if (colorMapping) free(colorMapping);
631 XPutImage( display, drawable, gc, bmpImage, xSrc, ySrc,
632 xDest, yDest, width, height );
633 XDestroyImage( bmpImage );
634 return lines;
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;
661 HDC32 hdcMem;
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 );
672 return heightSrc;
676 /***********************************************************************
677 * SetDIBits16 (GDI.440)
679 INT16 SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
680 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
681 UINT16 coloruse )
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,
692 UINT32 coloruse )
694 DC * dc;
695 BITMAPOBJ * bmp;
696 DWORD width, height;
697 WORD bpp;
699 /* Check parameters */
701 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
702 if (!dc)
704 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
705 if (!dc) return 0;
707 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
708 return 0;
709 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height, &bpp ) == -1)
710 return 0;
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,
729 UINT16 coloruse )
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,
742 UINT32 coloruse )
744 DC * dc;
745 DWORD width, height;
746 WORD bpp;
748 /* Check parameters */
750 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
751 if (!dc)
753 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
754 if (!dc) return 0;
756 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height, &bpp ) == -1)
757 return 0;
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,
771 bpp, info, coloruse,
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 ),
776 cx, cy );
781 /***********************************************************************
782 * GetDIBits16 (GDI.441)
784 INT16 GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
785 UINT16 lines, LPSTR bits, BITMAPINFO * info,
786 UINT16 coloruse )
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,
797 UINT32 coloruse )
799 DC * dc;
800 BITMAPOBJ * bmp;
801 PALETTEENTRY * palEntry;
802 PALETTEOBJ * palette;
803 XImage * bmpImage;
804 int i, x, y;
806 if (!lines) return 0;
807 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
808 if (!dc)
810 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
811 if (!dc) return 0;
813 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
814 return 0;
815 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
816 return 0;
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;
833 if (bits)
835 BYTE* bbits = bits;
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 )
847 return FALSE;
848 if( yend > bmp->bitmap.bmHeight ) yend = bmp->bitmap.bmHeight;
850 /* adjust scanline width */
852 pad = info->bmiHeader.biWidth - bmp->bitmap.bmWidth;
853 if( pad < 0 )
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 )
866 case 8:
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 );
873 bbits += pad;
875 break;
876 case 1:
877 pad += ((32 - (info->bmiHeader.biWidth & 31)) / 8) & 3;
878 for( y = yend - 1; (int)y >= (int)startscan; y-- )
880 *bbits = 0;
881 for( x = 0; x < xend; x++ ) {
883 *bbits |= XGetPixel( bmpImage, x, y)<<(7-(x&7));
884 if ((x&7)==7) {
885 bbits++;
886 *bbits=0;
889 bbits += pad;
891 break;
892 case 4:
893 pad += ((8 - (info->bmiHeader.biWidth & 7)) / 2) & 3;
894 for( y = yend - 1; (int)y >= (int)startscan; y-- )
896 *bbits = 0;
897 for( x = 0; x < xend; x++ ) {
899 *bbits |= XGetPixel( bmpImage, x, y)<<(4*(1-(x&1)));
900 if ((x&1)==1) {
901 bbits++;
902 *bbits=0;
905 bbits += pad;
907 break;
908 case 15:
909 case 16:
910 pad += (4 - ((info->bmiHeader.biWidth*2) & 3)) & 3;
911 for( y = yend - 1; (int)y >= (int)startscan; y-- )
913 *bbits = 0;
914 for( x = 0; x < xend; x++ ) {
915 unsigned long pixel=XGetPixel( bmpImage, x, y);
916 *bbits++ = pixel & 0xff;
917 *bbits++ = (pixel >> 8) & 0xff;
919 bbits += pad;
921 break;
922 case 24:
923 pad += (4 - ((info->bmiHeader.biWidth*3) & 3)) & 3;
924 for( y = yend - 1; (int)y >= (int)startscan; y-- )
926 *bbits = 0;
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;
933 bbits += pad;
935 break;
936 default:
937 fprintf(stderr,"GetDIBits*: unsupported depth %d\n",
938 info->bmiHeader.biBitCount
940 break;
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;
961 return lines;
965 /***********************************************************************
966 * CreateDIBitmap16 (GDI.442)
968 HBITMAP16 CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
969 DWORD init, LPCVOID bits, const BITMAPINFO * data,
970 UINT16 coloruse )
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,
981 UINT32 coloruse )
983 HBITMAP32 handle;
984 BOOL32 fColor;
985 DWORD width, height;
986 WORD bpp;
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;
998 else
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)))
1006 rgb++;
1007 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
1008 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
1010 else fColor = TRUE;
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)))
1018 rgb++;
1019 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
1020 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
1022 else fColor = TRUE;
1024 else
1026 fprintf( stderr, "CreateDIBitmap: wrong size (%ld) for data\n",
1027 data->bmiHeader.biSize );
1028 return 0;
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 );
1040 return handle;