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