Release 970329
[wine/multimedia.git] / objects / dib.c
blob76db125a7ee1491b040f77dd5fe7a300a14463e1
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 * 3 + 3)/4; break;
39 default:
40 fprintf(stderr, "DIB: unsupported depth %d.\n", depth );
41 /* fall through */
42 case 32:
43 words = width;
45 return 4 * words;
49 /***********************************************************************
50 * DIB_BitmapInfoSize
52 * Return the size of the bitmap info structure.
54 int DIB_BitmapInfoSize( BITMAPINFO * info, WORD coloruse )
56 int colors;
58 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
60 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
61 colors = (core->bcBitCount != 24) ? 1 << core->bcBitCount : 0;
62 return sizeof(BITMAPCOREHEADER) + colors *
63 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
65 else /* assume BITMAPINFOHEADER */
67 colors = info->bmiHeader.biClrUsed;
68 if (!colors && (info->bmiHeader.biBitCount != 24))
69 colors = 1 << info->bmiHeader.biBitCount;
70 return sizeof(BITMAPINFOHEADER) + colors *
71 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
76 /***********************************************************************
77 * DIB_GetBitmapInfo
79 * Get the info from a bitmap header.
80 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
82 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
83 DWORD *height, WORD *bpp )
85 if (header->biSize == sizeof(BITMAPINFOHEADER))
87 *width = header->biWidth;
88 *height = header->biHeight;
89 *bpp = header->biBitCount;
90 return 1;
92 if (header->biSize == sizeof(BITMAPCOREHEADER))
94 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
95 *width = core->bcWidth;
96 *height = core->bcHeight;
97 *bpp = core->bcBitCount;
98 return 0;
100 fprintf( stderr, "DIB_GetBitmapInfo: wrong size (%ld) for header\n",
101 header->biSize );
102 return -1;
106 /***********************************************************************
107 * DIB_BuildColorMap
109 * Build the color map from the bitmap palette. Should not be called
110 * for a 24-bit deep bitmap.
112 static int *DIB_BuildColorMap( DC *dc, WORD coloruse, WORD depth,
113 BITMAPINFO *info )
115 int i, colors;
116 BOOL32 isInfo;
117 WORD *colorPtr;
118 int *colorMapping;
120 if ((isInfo = (info->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))))
122 colors = info->bmiHeader.biClrUsed;
123 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
124 colorPtr = (WORD *)info->bmiColors;
126 else /* assume BITMAPCOREINFO */
128 colors = 1 << ((BITMAPCOREHEADER *)&info->bmiHeader)->bcBitCount;
129 colorPtr = (WORD *)((BITMAPCOREINFO *)info)->bmciColors;
131 if (!(colorMapping = (int *)malloc( colors * sizeof(int) ))) return NULL;
133 if (coloruse == DIB_RGB_COLORS)
135 if (isInfo)
137 RGBQUAD * rgb = (RGBQUAD *)colorPtr;
139 if (depth == 1) /* Monochrome */
140 for (i = 0; i < colors; i++, rgb++)
141 colorMapping[i] = (rgb->rgbRed + rgb->rgbGreen +
142 rgb->rgbBlue > 255*3/2);
143 else
144 for (i = 0; i < colors; i++, rgb++)
145 colorMapping[i] = COLOR_ToPhysical( dc, RGB(rgb->rgbRed,
146 rgb->rgbGreen,
147 rgb->rgbBlue));
149 else
151 RGBTRIPLE * rgb = (RGBTRIPLE *)colorPtr;
153 if (depth == 1) /* Monochrome */
154 for (i = 0; i < colors; i++, rgb++)
155 colorMapping[i] = (rgb->rgbtRed + rgb->rgbtGreen +
156 rgb->rgbtBlue > 255*3/2);
157 else
158 for (i = 0; i < colors; i++, rgb++)
159 colorMapping[i] = COLOR_ToPhysical( dc, RGB(rgb->rgbtRed,
160 rgb->rgbtGreen,
161 rgb->rgbtBlue));
164 else /* DIB_PAL_COLORS */
166 for (i = 0; i < colors; i++, colorPtr++)
167 colorMapping[i] = COLOR_ToPhysical( dc, PALETTEINDEX(*colorPtr) );
169 return colorMapping;
173 /***********************************************************************
174 * DIB_SetImageBits_1
176 * SetDIBits for a 1-bit deep DIB.
178 static void DIB_SetImageBits_1( DWORD lines, BYTE *bits, DWORD width,
179 int *colors, XImage *bmpImage )
181 DWORD i, x;
182 BYTE pad, pix;
184 if (!(width & 31)) pad = 0;
185 else pad = ((32 - (width & 31)) + 7) / 8;
187 while (lines--)
189 for (i = width/8, x = 0; (i > 0); i--)
191 pix = *bits++;
192 XPutPixel( bmpImage, x++, lines, colors[pix >> 7] );
193 XPutPixel( bmpImage, x++, lines, colors[(pix >> 6) & 1] );
194 XPutPixel( bmpImage, x++, lines, colors[(pix >> 5) & 1] );
195 XPutPixel( bmpImage, x++, lines, colors[(pix >> 4) & 1] );
196 XPutPixel( bmpImage, x++, lines, colors[(pix >> 3) & 1] );
197 XPutPixel( bmpImage, x++, lines, colors[(pix >> 2) & 1] );
198 XPutPixel( bmpImage, x++, lines, colors[(pix >> 1) & 1] );
199 XPutPixel( bmpImage, x++, lines, colors[pix & 1] );
201 pix = *bits;
202 switch(width & 7)
204 case 7: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
205 case 6: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
206 case 5: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
207 case 4: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
208 case 3: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
209 case 2: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
210 case 1: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] );
212 bits += pad;
217 /***********************************************************************
218 * DIB_SetImageBits_4
220 * SetDIBits for a 4-bit deep DIB.
222 static void DIB_SetImageBits_4( DWORD lines, BYTE *bits, DWORD width,
223 int *colors, XImage *bmpImage )
225 DWORD i, x;
226 BYTE pad;
228 if (!(width & 7)) pad = 0;
229 else pad = ((8 - (width & 7)) + 1) / 2;
231 while (lines--)
233 for (i = width/2, x = 0; i > 0; i--)
235 BYTE pix = *bits++;
236 XPutPixel( bmpImage, x++, lines, colors[pix >> 4] );
237 XPutPixel( bmpImage, x++, lines, colors[pix & 0x0f] );
239 if (width & 1) XPutPixel( bmpImage, x, lines, colors[*bits >> 4] );
240 bits += pad;
244 #define check_xy(x,y) \
245 if (x > width) { \
246 x = 0; \
247 if (lines) \
248 lines--; \
251 /***********************************************************************
252 * DIB_SetImageBits_RLE4
254 * SetDIBits for a 4-bit deep compressed DIB.
256 static void DIB_SetImageBits_RLE4( DWORD lines, BYTE *bits, DWORD width,
257 int *colors, XImage *bmpImage )
259 int x = 0, c, length;
260 BYTE *begin = bits;
262 lines--;
263 while ((int)lines >= 0)
265 length = *bits++;
266 if (length) { /* encoded */
267 c = *bits++;
268 while (length--) {
269 XPutPixel(bmpImage, x++, lines, colors[c >> 4]);
270 check_xy(x, y);
271 if (length) {
272 length--;
273 XPutPixel(bmpImage, x++, lines, colors[c & 0xf]);
274 check_xy(x, y);
277 } else {
278 length = *bits++;
279 switch (length) {
280 case 0: /* eol */
281 x = 0;
282 lines--;
283 continue;
285 case 1: /* eopicture */
286 return;
288 case 2: /* delta */
289 x += *bits++;
290 lines -= *bits++;
291 continue;
293 default: /* absolute */
294 while (length--) {
295 c = *bits++;
296 XPutPixel(bmpImage, x++, lines, colors[c >> 4]);
297 check_xy(x, y);
298 if (length) {
299 length--;
300 XPutPixel(bmpImage, x++, lines, colors[c & 0xf]);
301 check_xy(x, y);
304 if ((bits - begin) & 1)
305 bits++;
311 /***********************************************************************
312 * DIB_SetImageBits_8
314 * SetDIBits for an 8-bit deep DIB.
316 static void DIB_SetImageBits_8( DWORD lines, BYTE *bits, DWORD width,
317 int *colors, XImage *bmpImage )
319 DWORD x;
320 BYTE pad = (4 - (width & 3)) & 3;
322 while (lines--)
324 for (x = 0; x < width; x++)
325 XPutPixel( bmpImage, x, lines, colors[*bits++] );
326 bits += pad;
330 /***********************************************************************
331 * DIB_SetImageBits_RLE8
333 * SetDIBits for an 8-bit deep compressed DIB.
335 * This function rewritten 941113 by James Youngman. WINE blew out when I
336 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
338 * This was because the algorithm assumed that all RLE8 bitmaps end with the
339 * 'End of bitmap' escape code. This code is very much laxer in what it
340 * allows to end the expansion. Possibly too lax. See the note by
341 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
342 * bitmap should end with RleEnd, but on the other hand, software exists
343 * that produces ones that don't and Windows 3.1 doesn't complain a bit
344 * about it.
346 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
347 * James A. Youngman <mbcstjy@afs.man.ac.uk>
348 * [JAY]
351 enum Rle8_EscapeCodes
354 * Apologies for polluting your file's namespace...
356 RleEol = 0, /* End of line */
357 RleEnd = 1, /* End of bitmap */
358 RleDelta = 2 /* Delta */
361 static void DIB_SetImageBits_RLE8( DWORD lines, BYTE *bits, DWORD width,
362 int *colors, XImage *bmpImage )
364 int x; /* X-positon on each line. Increases. */
365 int line; /* Line #. Starts at lines-1, decreases */
366 BYTE *pIn = bits; /* Pointer to current position in bits */
367 BYTE length; /* The length pf a run */
368 BYTE color_index; /* index into colors[] as read from bits */
369 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
370 WORD color; /* value of colour[color_index] */
372 if (lines == 0) /* Let's hope this doesn't happen. */
373 return;
376 * Note that the bitmap data is stored by Windows starting at the
377 * bottom line of the bitmap and going upwards. Within each line,
378 * the data is stored left-to-right. That's the reason why line
379 * goes from lines-1 to 0. [JAY]
382 x = 0;
383 line = lines-1;
386 length = *pIn++;
389 * If the length byte is not zero (which is the escape value),
390 * We have a run of length pixels all the same colour. The colour
391 * index is stored next.
393 * If the length byte is zero, we need to read the next byte to
394 * know what to do. [JAY]
396 if (length != 0)
399 * [Run-Length] Encoded mode
401 color_index = (*pIn++); /* Get the colour index. */
402 color = colors[color_index];
404 while(length--)
405 XPutPixel(bmpImage, x++, line, color);
407 else
410 * Escape codes (may be an absolute sequence though)
412 escape_code = (*pIn++);
413 switch(escape_code)
415 case RleEol: /* =0, end of line */
417 x = 0;
418 line--;
419 break;
422 case RleEnd: /* =1, end of bitmap */
425 * Not all RLE8 bitmaps end with this
426 * code. For example, Paint Shop Pro
427 * produces some that don't. That's (I think)
428 * what caused the previous implementation to
429 * fail. [JAY]
431 line=-1; /* Cause exit from do loop. */
432 break;
435 case RleDelta: /* =2, a delta */
438 * Note that deltaing to line 0
439 * will cause an exit from the loop,
440 * which may not be what is intended.
441 * The fact that there is a delta in the bits
442 * almost certainly implies that there is data
443 * to follow. You may feel that we should
444 * jump to the top of the loop to avoid exiting
445 * in this case.
447 * TODO: Decide what to do here in that case. [JAY]
449 x += (*pIn++);
450 line -= (*pIn++);
451 if (line == 0)
453 dprintf_bitmap(stddeb,
454 "DIB_SetImageBits_RLE8(): "
455 "Delta to last line of bitmap "
456 "(wrongly?) causes loop exit\n");
458 break;
461 default: /* >2, switch to absolute mode */
464 * Absolute Mode
466 length = escape_code;
467 while(length--)
469 color_index = (*pIn++);
470 XPutPixel(bmpImage, x++, line,
471 colors[color_index]);
475 * If you think for a moment you'll realise that the
476 * only time we could ever possibly read an odd
477 * number of bytes is when there is a 0x00 (escape),
478 * a value >0x02 (absolute mode) and then an odd-
479 * length run. Therefore this is the only place we
480 * need to worry about it. Everywhere else the
481 * bytes are always read in pairs. [JAY]
483 if (escape_code & 1)
484 pIn++; /* Throw away the pad byte. */
485 break;
487 } /* switch (escape_code) : Escape sequence */
488 } /* process either an encoded sequence or an escape sequence */
490 /* We expect to come here more than once per line. */
491 } while (line >= 0); /* Do this until the bitmap is filled */
494 * Everybody comes here at the end.
495 * Check how we exited the loop and print a message if it's a bit odd.
496 * [JAY]
498 if ( (*(pIn-2) != 0/*escape*/) || (*(pIn-1)!= RleEnd) )
500 dprintf_bitmap(stddeb, "DIB_SetImageBits_RLE8(): End-of-bitmap "
501 "without (strictly) proper escape code. Last two "
502 "bytes were: %02X %02X.\n",
503 (int)*(pIn-2),
504 (int)*(pIn-1));
509 /***********************************************************************
510 * DIB_SetImageBits_24
512 * SetDIBits for a 24-bit deep DIB.
514 static void DIB_SetImageBits_24( DWORD lines, BYTE *bits, DWORD width,
515 DC *dc, XImage *bmpImage )
517 DWORD x;
518 BYTE pad = (4 - ((width*3) & 3)) & 3;
520 /* "bits" order is reversed for some reason */
522 while (lines--)
524 for (x = 0; x < width; x++, bits += 3)
525 XPutPixel( bmpImage, x, lines,
526 COLOR_ToPhysical(dc, RGB(bits[2],bits[1],bits[0])) );
528 bits += pad;
533 /***********************************************************************
534 * DIB_SetImageBits
536 * Transfer the bits to an X image.
537 * Helper function for SetDIBits() and SetDIBitsToDevice().
539 static int DIB_SetImageBits( DC *dc, DWORD lines, WORD depth, LPSTR bits,
540 DWORD infoWidth, WORD infoBpp,
541 BITMAPINFO *info, WORD coloruse,
542 Drawable drawable, GC gc, int xSrc, int ySrc,
543 int xDest, int yDest, int width, int height )
545 int *colorMapping;
546 XImage *bmpImage;
547 DWORD compression = 0;
549 if (info->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
550 compression = info->bmiHeader.biCompression;
552 /* Build the color mapping table */
554 if (infoBpp == 24) colorMapping = NULL;
555 else
556 if (!(colorMapping = DIB_BuildColorMap( dc, coloruse, depth, info )))
557 return 0;
559 if( dc->w.flags & DC_DIRTY ) CLIPPING_UpdateGCRegion(dc);
561 /* Transfer the pixels */
562 XCREATEIMAGE(bmpImage, infoWidth, lines, depth );
564 switch(infoBpp)
566 case 1:
567 DIB_SetImageBits_1( lines, bits, infoWidth,
568 colorMapping, bmpImage );
569 break;
570 case 4:
571 if (compression) DIB_SetImageBits_RLE4( lines, bits, infoWidth,
572 colorMapping, bmpImage );
573 else DIB_SetImageBits_4( lines, bits, infoWidth,
574 colorMapping, bmpImage );
575 break;
576 case 8:
577 if (compression) DIB_SetImageBits_RLE8( lines, bits, infoWidth,
578 colorMapping, bmpImage );
579 else DIB_SetImageBits_8( lines, bits, infoWidth,
580 colorMapping, bmpImage );
581 break;
582 case 24:
583 DIB_SetImageBits_24( lines, bits, infoWidth, dc, bmpImage );
584 break;
585 default:
586 fprintf( stderr, "Invalid depth %d for SetDIBits!\n", infoBpp );
587 break;
589 if (colorMapping) free(colorMapping);
590 XPutImage( display, drawable, gc, bmpImage, xSrc, ySrc,
591 xDest, yDest, width, height );
592 XDestroyImage( bmpImage );
593 return lines;
597 /***********************************************************************
598 * StretchDIBits16 (GDI.439)
600 INT16 StretchDIBits16( HDC16 hdc, INT16 xDst, INT16 yDst, INT16 widthDst,
601 INT16 heightDst, INT16 xSrc, INT16 ySrc, INT16 widthSrc,
602 INT16 heightSrc, const VOID *bits,
603 const BITMAPINFO *info, UINT16 wUsage, DWORD dwRop )
605 return (INT16)StretchDIBits32( hdc, xDst, yDst, widthDst, heightDst,
606 xSrc, ySrc, widthSrc, heightSrc, bits,
607 info, wUsage, dwRop );
611 /***********************************************************************
612 * StretchDIBits32 (GDI32.351)
614 INT32 StretchDIBits32( HDC32 hdc, INT32 xDst, INT32 yDst, INT32 widthDst,
615 INT32 heightDst, INT32 xSrc, INT32 ySrc, INT32 widthSrc,
616 INT32 heightSrc, const void *bits,
617 const BITMAPINFO *info, UINT32 wUsage, DWORD dwRop )
619 HBITMAP32 hBitmap, hOldBitmap;
620 HDC32 hdcMem;
622 hBitmap = CreateDIBitmap32( hdc, &info->bmiHeader, CBM_INIT,
623 bits, info, wUsage );
624 hdcMem = CreateCompatibleDC32( hdc );
625 hOldBitmap = SelectObject32( hdcMem, hBitmap );
626 StretchBlt32( hdc, xDst, yDst, widthDst, heightDst,
627 hdcMem, xSrc, ySrc, widthSrc, heightSrc, dwRop );
628 SelectObject32( hdcMem, hOldBitmap );
629 DeleteDC32( hdcMem );
630 DeleteObject32( hBitmap );
631 return heightSrc;
635 /***********************************************************************
636 * SetDIBits16 (GDI.440)
638 INT16 SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
639 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
640 UINT16 coloruse )
642 return SetDIBits32( hdc, hbitmap, startscan, lines, bits, info, coloruse );
646 /***********************************************************************
647 * SetDIBits32 (GDI32.312)
649 INT32 SetDIBits32( HDC32 hdc, HBITMAP32 hbitmap, UINT32 startscan,
650 UINT32 lines, LPCVOID bits, const BITMAPINFO *info,
651 UINT32 coloruse )
653 DC * dc;
654 BITMAPOBJ * bmp;
655 DWORD width, height;
656 WORD bpp;
658 /* Check parameters */
660 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
661 if (!dc)
663 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
664 if (!dc) return 0;
666 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
667 return 0;
668 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height, &bpp ) == -1)
669 return 0;
670 if (!lines || (startscan >= (WORD)height)) return 0;
671 if (startscan + lines > height) lines = height - startscan;
673 return CallTo32_LargeStack( (int(*)())DIB_SetImageBits, 16,
674 dc, lines, bmp->bitmap.bmBitsPixel,
675 bits, width, bpp, info,
676 coloruse, bmp->pixmap, BITMAP_GC(bmp), 0, 0, 0,
677 height - startscan - lines,
678 bmp->bitmap.bmWidth, lines );
682 /***********************************************************************
683 * SetDIBitsToDevice16 (GDI.443)
685 INT16 SetDIBitsToDevice16( HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
686 INT16 cy, INT16 xSrc, INT16 ySrc, UINT16 startscan,
687 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
688 UINT16 coloruse )
690 return SetDIBitsToDevice32( hdc, xDest, yDest, cx, cy, xSrc, ySrc,
691 startscan, lines, bits, info, coloruse );
695 /***********************************************************************
696 * SetDIBitsToDevice32 (GDI32.313)
698 INT32 SetDIBitsToDevice32( HDC32 hdc, INT32 xDest, INT32 yDest, DWORD cx,
699 DWORD cy, INT32 xSrc, INT32 ySrc, UINT32 startscan,
700 UINT32 lines, LPCVOID bits, const BITMAPINFO *info,
701 UINT32 coloruse )
703 DC * dc;
704 DWORD width, height;
705 WORD bpp;
707 /* Check parameters */
709 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
710 if (!dc)
712 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
713 if (!dc) return 0;
715 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height, &bpp ) == -1)
716 return 0;
717 if (!lines || (startscan >= height)) return 0;
718 if (startscan + lines > height) lines = height - startscan;
719 if (ySrc < startscan) ySrc = startscan;
720 else if (ySrc >= startscan + lines) return 0;
721 if (xSrc >= width) return 0;
722 if (ySrc + cy >= startscan + lines) cy = startscan + lines - ySrc;
723 if (xSrc + cx >= width) cx = width - xSrc;
724 if (!cx || !cy) return 0;
726 DC_SetupGCForText( dc ); /* To have the correct colors */
727 XSetFunction( display, dc->u.x.gc, DC_XROPfunction[dc->w.ROPmode-1] );
728 return CallTo32_LargeStack( (int(*)())DIB_SetImageBits, 16,
729 dc, lines, dc->w.bitsPerPixel, bits, width,
730 bpp, info, coloruse,
731 dc->u.x.drawable, dc->u.x.gc,
732 xSrc, ySrc - startscan,
733 dc->w.DCOrgX + XLPTODP( dc, xDest ),
734 dc->w.DCOrgY + YLPTODP( dc, yDest ),
735 cx, cy );
740 /***********************************************************************
741 * GetDIBits16 (GDI.441)
743 INT16 GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
744 UINT16 lines, LPSTR bits, BITMAPINFO * info,
745 UINT16 coloruse )
747 return GetDIBits32( hdc, hbitmap, startscan, lines, bits, info, coloruse );
751 /***********************************************************************
752 * GetDIBits32 (GDI32.170)
754 INT32 GetDIBits32( HDC32 hdc, HBITMAP32 hbitmap, UINT32 startscan,
755 UINT32 lines, LPSTR bits, BITMAPINFO * info,
756 UINT32 coloruse )
758 DC * dc;
759 BITMAPOBJ * bmp;
760 PALETTEENTRY * palEntry;
761 PALETTEOBJ * palette;
762 XImage * bmpImage, * dibImage;
763 int i, x, y;
765 if (!lines) return 0;
766 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
767 if (!dc)
769 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
770 if (!dc) return 0;
772 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
773 return 0;
774 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
775 return 0;
777 /* Transfer color info */
779 palEntry = palette->logpalette.palPalEntry;
780 for (i = 0; i < info->bmiHeader.biClrUsed; i++, palEntry++)
782 if (coloruse == DIB_RGB_COLORS)
784 info->bmiColors[i].rgbRed = palEntry->peRed;
785 info->bmiColors[i].rgbGreen = palEntry->peGreen;
786 info->bmiColors[i].rgbBlue = palEntry->peBlue;
787 info->bmiColors[i].rgbReserved = 0;
789 else ((WORD *)info->bmiColors)[i] = (WORD)i;
792 if (bits)
794 BYTE* bbits = bits;
795 int pad, yend, xend = bmp->bitmap.bmWidth;
797 dprintf_bitmap(stddeb, "GetDIBits: %u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
798 lines, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
799 (int)info->bmiHeader.biWidth, (int)info->bmiHeader.biHeight, startscan );
801 /* adjust number of scanlines to copy */
803 if( lines > info->bmiHeader.biHeight ) lines = info->bmiHeader.biHeight;
804 yend = startscan + lines;
805 if( startscan >= bmp->bitmap.bmHeight )
806 return FALSE;
807 if( yend > bmp->bitmap.bmHeight ) yend = bmp->bitmap.bmHeight;
809 /* adjust scanline width */
811 pad = info->bmiHeader.biWidth - bmp->bitmap.bmWidth;
812 if( pad < 0 )
814 /* bitmap is wider than DIB, copy only a part */
816 pad = 0; xend = info->bmiHeader.biWidth;
819 bmpImage = (XImage *)CallTo32_LargeStack( (int (*)())XGetImage, 8,
820 display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
821 bmp->bitmap.bmHeight, AllPlanes, ZPixmap );
823 switch( info->bmiHeader.biBitCount )
825 case 8:
827 pad += (4 - (info->bmiHeader.biWidth & 3)) & 3;
828 for( y = yend - 1; (int)y >= (int)startscan; y-- )
830 for( x = 0; x < xend; x++ )
831 *bbits++ = XGetPixel( bmpImage, x, y );
832 bbits += pad;
834 break;
836 /* add more bpp-specific shortcuts here */
838 default:
840 dibImage = XCreateImage(display, DefaultVisualOfScreen( screen ),
841 info->bmiHeader.biBitCount, ZPixmap, 0, bits,
842 info->bmiHeader.biWidth, info->bmiHeader.biHeight,
843 32, DIB_GetImageWidthBytes( info->bmiHeader.biWidth,
844 info->bmiHeader.biBitCount ) );
845 if( dibImage )
847 extern void _XInitImageFuncPtrs( XImage* );
849 dibImage->byte_order = MSBFirst;
850 dibImage->bitmap_bit_order = MSBFirst;
851 dibImage->bitmap_unit = 16;
852 _XInitImageFuncPtrs( dibImage );
854 for (y = yend - 1; (int)y >= (int)startscan; y--)
855 for (x = 0; x < xend; x++)
856 XPutPixel( dibImage, x, yend - y + 1,
857 XGetPixel( bmpImage, x, y ));
858 dibImage->data = NULL;
859 XDestroyImage( dibImage );
863 XDestroyImage( bmpImage );
865 info->bmiHeader.biCompression = 0;
867 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
869 /* fill in struct members */
871 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
872 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
873 info->bmiHeader.biPlanes = 1;
874 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
875 info->bmiHeader.biSizeImage = bmp->bitmap.bmHeight *
876 DIB_GetImageWidthBytes( bmp->bitmap.bmWidth,
877 bmp->bitmap.bmBitsPixel );
878 info->bmiHeader.biCompression = 0;
881 return lines;
885 /***********************************************************************
886 * CreateDIBitmap16 (GDI.442)
888 HBITMAP16 CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
889 DWORD init, LPCVOID bits, const BITMAPINFO * data,
890 UINT16 coloruse )
892 return CreateDIBitmap32( hdc, header, init, bits, data, coloruse );
896 /***********************************************************************
897 * CreateDIBitmap32 (GDI32.37)
899 HBITMAP32 CreateDIBitmap32( HDC32 hdc, const BITMAPINFOHEADER *header,
900 DWORD init, LPCVOID bits, const BITMAPINFO *data,
901 UINT32 coloruse )
903 HBITMAP32 handle;
904 BOOL32 fColor;
905 DWORD width, height;
906 WORD bpp;
908 if (DIB_GetBitmapInfo( header, &width, &height, &bpp ) == -1) return 0;
910 /* Check if we should create a monochrome or color bitmap. */
911 /* We create a monochrome bitmap only if it has exactly 2 */
912 /* colors, which are either black or white, nothing else. */
913 /* In all other cases, we create a color bitmap. */
915 if (bpp != 1) fColor = TRUE;
916 else if ((coloruse != DIB_RGB_COLORS) ||
917 (init != CBM_INIT) || !data) fColor = FALSE;
918 else
920 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
922 RGBQUAD *rgb = data->bmiColors;
923 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
924 if ((col == RGB(0,0,0)) || (col == RGB(0xff,0xff,0xff)))
926 rgb++;
927 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
928 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
930 else fColor = TRUE;
932 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
934 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
935 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
936 if ((col == RGB(0,0,0)) || (col == RGB(0xff,0xff,0xff)))
938 rgb++;
939 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
940 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
942 else fColor = TRUE;
944 else
946 fprintf( stderr, "CreateDIBitmap: wrong size (%ld) for data\n",
947 data->bmiHeader.biSize );
948 return 0;
952 /* Now create the bitmap */
954 handle = fColor ? CreateCompatibleBitmap32( hdc, width, height ) :
955 CreateBitmap32( width, height, 1, 1, NULL );
956 if (!handle) return 0;
958 if (init == CBM_INIT)
959 SetDIBits32( hdc, handle, 0, height, bits, data, coloruse );
960 return handle;