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