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