Release 980628
[wine/gsoc-2012-control.git] / objects / dib.c
blob72780f903e0102d22c44baa5d3c77569597b45b1
1 /*
2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include "ts_xlib.h"
9 #include "ts_xutil.h"
10 #include "dc.h"
11 #include "bitmap.h"
12 #include "callback.h"
13 #include "palette.h"
14 #include "color.h"
15 #include "global.h"
16 #include "debug.h"
18 static int bitmapDepthTable[] = { 8, 1, 32, 16, 24, 15, 4, 0 };
19 static int ximageDepthTable[] = { 0, 0, 0, 0, 0, 0, 0 };
22 /* This structure holds the arguments for DIB_SetImageBits() */
23 typedef struct
25 DC *dc;
26 LPCVOID bits;
27 int lines;
28 DWORD infoWidth;
29 WORD depth;
30 WORD infoBpp;
31 WORD compression;
32 RGBQUAD *colorMap;
33 int nColorMap;
34 Drawable drawable;
35 GC gc;
36 int xSrc;
37 int ySrc;
38 int xDest;
39 int yDest;
40 int width;
41 int height;
42 } DIB_SETIMAGEBITS_DESCR;
45 /***********************************************************************
46 * DIB_Init
48 BOOL32 DIB_Init(void)
50 int i;
51 XImage* testimage;
53 for( i = 0; bitmapDepthTable[i]; i++ )
55 testimage = TSXCreateImage(display, DefaultVisualOfScreen(screen),
56 bitmapDepthTable[i], ZPixmap, 0, NULL, 1, 1, 32, 20 );
57 if( testimage ) ximageDepthTable[i] = testimage->bits_per_pixel;
58 else return FALSE;
59 TSXDestroyImage(testimage);
61 return TRUE;
64 /***********************************************************************
65 * DIB_GetXImageWidthBytes
67 * Return the width of an X image in bytes
69 int DIB_GetXImageWidthBytes( int width, int depth )
71 int i;
73 if (!ximageDepthTable[0]) {
74 DIB_Init();
76 for( i = 0; bitmapDepthTable[i] ; i++ )
77 if( bitmapDepthTable[i] == depth )
78 return (4 * ((width * ximageDepthTable[i] + 31)/32));
80 WARN(bitmap, "(%d): Unsupported depth\n", depth );
81 return (4 * width);
84 /***********************************************************************
85 * DIB_GetDIBWidthBytes
87 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
88 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
90 int DIB_GetDIBWidthBytes( int width, int depth )
92 int words;
94 switch(depth)
96 case 1: words = (width + 31) / 32; break;
97 case 4: words = (width + 7) / 8; break;
98 case 8: words = (width + 3) / 4; break;
99 case 15:
100 case 16: words = (width + 1) / 2; break;
101 case 24: words = (width * 3 + 3)/4; break;
103 default:
104 WARN(bitmap, "(%d): Unsupported depth\n", depth );
105 /* fall through */
106 case 32:
107 words = width;
109 return 4 * words;
113 /***********************************************************************
114 * DIB_BitmapInfoSize
116 * Return the size of the bitmap info structure including color table.
118 int DIB_BitmapInfoSize( BITMAPINFO * info, WORD coloruse )
120 int colors;
122 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
124 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
125 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
126 return sizeof(BITMAPCOREHEADER) + colors *
127 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
129 else /* assume BITMAPINFOHEADER */
131 colors = info->bmiHeader.biClrUsed;
132 if (!colors && (info->bmiHeader.biBitCount <= 8))
133 colors = 1 << info->bmiHeader.biBitCount;
134 return sizeof(BITMAPINFOHEADER) + colors *
135 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
140 /***********************************************************************
141 * DIB_GetBitmapInfo
143 * Get the info from a bitmap header.
144 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
146 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
147 int *height, WORD *bpp, WORD *compr )
149 if (header->biSize == sizeof(BITMAPINFOHEADER))
151 *width = header->biWidth;
152 *height = header->biHeight;
153 *bpp = header->biBitCount;
154 *compr = header->biCompression;
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 *compr = 0;
164 return 0;
166 WARN(bitmap, "(%ld): wrong size for header\n", 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 >8-bit deep bitmap.
177 static RGBQUAD *DIB_BuildColorMap( const BITMAPINFO *info,
178 HPALETTE16 hPalette, int *nColors )
180 int i, colors;
181 BOOL32 isInfo;
182 WORD *colorPtr;
183 RGBQUAD *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;
197 if (colors > 256)
199 ERR(bitmap, "DIB_BuildColorMap called with >256 colors!\n");
200 return NULL;
203 if (!(colorMapping = (RGBQUAD *)HeapAlloc(GetProcessHeap(), 0,
204 colors * sizeof(RGBQUAD) )))
205 return NULL;
207 if (hPalette == 0) /* DIB_RGB_COLORS */
209 if (isInfo)
211 RGBQUAD * rgb = (RGBQUAD *)colorPtr;
213 for (i = 0; i < colors; i++, rgb++)
214 colorMapping[i] = *rgb;
216 else
218 RGBTRIPLE * rgb = (RGBTRIPLE *)colorPtr;
220 for (i = 0; i < colors; i++, rgb++)
221 colorMapping[i].rgbRed = rgb->rgbtRed,
222 colorMapping[i].rgbGreen = rgb->rgbtGreen,
223 colorMapping[i].rgbBlue = rgb->rgbtBlue;
226 else /* DIB_PAL_COLORS */
228 PALETTEOBJ *palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPalette, PALETTE_MAGIC);
229 if (!palPtr)
231 HeapFree(GetProcessHeap(), 0, colorMapping);
232 return NULL;
235 for (i = 0; i < colors; i++, colorPtr++)
236 if (*colorPtr >= palPtr->logpalette.palNumEntries)
238 colorMapping[i].rgbRed = 0;
239 colorMapping[i].rgbGreen = 0;
240 colorMapping[i].rgbBlue = 0;
242 else
244 PALETTEENTRY *pe = palPtr->logpalette.palPalEntry + *colorPtr;
245 colorMapping[i].rgbRed = pe->peRed;
246 colorMapping[i].rgbGreen = pe->peGreen;
247 colorMapping[i].rgbBlue = pe->peBlue;
250 GDI_HEAP_UNLOCK(hPalette);
253 *nColors = colors;
254 return colorMapping;
257 /***********************************************************************
258 * DIB_PhysicalColorMap
260 * Translate color map to physical colors
262 static int *DIB_PhysicalColorMap( DC *dc, int depth,
263 RGBQUAD *colorMap, int nColorMap)
265 int *colorMapping, i;
267 if (!nColorMap) return NULL;
268 if (!(colorMapping = (int *)HeapAlloc(GetProcessHeap(), 0,
269 nColorMap * sizeof(int) ))) return NULL;
271 if (depth == 1) /* Monochrome */
272 for (i = 0; i < nColorMap; i++, colorMap++)
273 colorMapping[i] = (colorMap->rgbRed + colorMap->rgbGreen +
274 colorMap->rgbBlue > 255*3/2);
275 else
276 for (i = 0; i < nColorMap; i++, colorMap++)
277 colorMapping[i] = COLOR_ToPhysical( dc, RGB(colorMap->rgbRed,
278 colorMap->rgbGreen,
279 colorMap->rgbBlue));
280 return colorMapping;
283 /***********************************************************************
284 * DIB_MapColor
286 static int DIB_MapColor( int *physMap, int nPhysMap, int phys )
288 int color;
290 for (color = 0; color < nPhysMap; color++)
291 if (physMap[color] == phys)
292 return color;
294 WARN(bitmap, "Strange color %08x\n", phys);
295 return 0;
299 /***********************************************************************
300 * DIB_SetImageBits_1_Line
302 * Handles a single line of 1 bit data.
304 static void DIB_SetImageBits_1_Line(DWORD dstwidth, int left, int *colors,
305 XImage *bmpImage, int h, const BYTE *bits)
307 BYTE pix;
308 DWORD i, x;
310 dstwidth += left; bits += left >> 3;
312 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
313 for (i = dstwidth/8, x = left&~7; (i > 0); i--)
315 pix = *bits++;
316 XPutPixel( bmpImage, x++, h, colors[pix >> 7] );
317 XPutPixel( bmpImage, x++, h, colors[(pix >> 6) & 1] );
318 XPutPixel( bmpImage, x++, h, colors[(pix >> 5) & 1] );
319 XPutPixel( bmpImage, x++, h, colors[(pix >> 4) & 1] );
320 XPutPixel( bmpImage, x++, h, colors[(pix >> 3) & 1] );
321 XPutPixel( bmpImage, x++, h, colors[(pix >> 2) & 1] );
322 XPutPixel( bmpImage, x++, h, colors[(pix >> 1) & 1] );
323 XPutPixel( bmpImage, x++, h, colors[pix & 1] );
325 pix = *bits;
326 switch(dstwidth & 7)
328 case 7: XPutPixel( bmpImage, x++, h, colors[pix >> 7] ); pix <<= 1;
329 case 6: XPutPixel( bmpImage, x++, h, colors[pix >> 7] ); pix <<= 1;
330 case 5: XPutPixel( bmpImage, x++, h, colors[pix >> 7] ); pix <<= 1;
331 case 4: XPutPixel( bmpImage, x++, h, colors[pix >> 7] ); pix <<= 1;
332 case 3: XPutPixel( bmpImage, x++, h, colors[pix >> 7] ); pix <<= 1;
333 case 2: XPutPixel( bmpImage, x++, h, colors[pix >> 7] ); pix <<= 1;
334 case 1: XPutPixel( bmpImage, x++, h, colors[pix >> 7] );
338 /***********************************************************************
339 * DIB_SetImageBits_1
341 * SetDIBits for a 1-bit deep DIB.
343 static void DIB_SetImageBits_1( int lines, const BYTE *srcbits,
344 DWORD srcwidth, DWORD dstwidth, int left,
345 int *colors, XImage *bmpImage )
347 int h;
349 /* 32 bit aligned */
350 DWORD linebytes = ((srcwidth + 31) & ~31) / 8;
352 if (lines > 0) {
353 for (h = lines-1; h >=0; h--) {
354 DIB_SetImageBits_1_Line(dstwidth, left, colors, bmpImage, h, srcbits);
355 srcbits += linebytes;
357 } else {
358 lines = -lines;
359 for (h = 0; h < lines; h++) {
360 DIB_SetImageBits_1_Line(dstwidth, left, colors, bmpImage, h, srcbits);
361 srcbits += linebytes;
367 /***********************************************************************
368 * DIB_SetImageBits_4
370 * SetDIBits for a 4-bit deep DIB.
372 static void DIB_SetImageBits_4( int lines, const BYTE *srcbits,
373 DWORD srcwidth, DWORD dstwidth, int left,
374 int *colors, XImage *bmpImage )
376 DWORD i, x;
377 int h;
378 const BYTE *bits = srcbits + (left >> 1);
380 /* 32 bit aligned */
381 DWORD linebytes = ((srcwidth+7)&~7)/2;
383 dstwidth += left;
385 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
386 if (lines > 0) {
387 for (h = lines-1; h >= 0; h--) {
388 for (i = dstwidth/2, x = left&~1; i > 0; i--) {
389 BYTE pix = *bits++;
390 XPutPixel( bmpImage, x++, h, colors[pix >> 4] );
391 XPutPixel( bmpImage, x++, h, colors[pix & 0x0f] );
393 if (dstwidth & 1) XPutPixel( bmpImage, x, h, colors[*bits >> 4] );
394 srcbits += linebytes;
395 bits = srcbits + (left >> 1);
397 } else {
398 lines = -lines;
399 for (h = 0; h < lines; h++) {
400 for (i = dstwidth/2, x = left&~1; i > 0; i--) {
401 BYTE pix = *bits++;
402 XPutPixel( bmpImage, x++, h, colors[pix >> 4] );
403 XPutPixel( bmpImage, x++, h, colors[pix & 0x0f] );
405 if (dstwidth & 1) XPutPixel( bmpImage, x, h, colors[*bits >> 4] );
406 srcbits += linebytes;
407 bits = srcbits + (left >> 1);
412 /***********************************************************************
413 * DIB_GetImageBits_4
415 * GetDIBits for a 4-bit deep DIB.
417 static void DIB_GetImageBits_4( int lines, BYTE *srcbits,
418 DWORD srcwidth, DWORD dstwidth, int left,
419 int *colors, int nColors, XImage *bmpImage )
421 DWORD i, x;
422 int h;
423 BYTE *bits = srcbits + (left >> 1);
425 /* 32 bit aligned */
426 DWORD linebytes = ((srcwidth+7)&~7)/2;
428 dstwidth += left;
430 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
431 if (lines > 0) {
432 for (h = lines-1; h >= 0; h--) {
433 for (i = dstwidth/2, x = left&~1; i > 0; i--) {
434 *bits++ = (DIB_MapColor( colors, nColors, XGetPixel( bmpImage, x++, h )) << 4)
435 | (DIB_MapColor( colors, nColors, XGetPixel( bmpImage, x++, h )) & 0x0f);
437 if (dstwidth & 1)
438 *bits = (DIB_MapColor( colors, nColors, XGetPixel( bmpImage, x++, h )) << 4);
439 srcbits += linebytes;
440 bits = srcbits + (left >> 1);
442 } else {
443 lines = -lines;
444 for (h = 0; h < lines; h++) {
445 for (i = dstwidth/2, x = left&~1; i > 0; i--) {
446 *bits++ = (DIB_MapColor( colors, nColors, XGetPixel( bmpImage, x++, h )) << 4)
447 | (DIB_MapColor( colors, nColors, XGetPixel( bmpImage, x++, h )) & 0x0f);
449 if (dstwidth & 1)
450 *bits = (DIB_MapColor( colors, nColors, XGetPixel( bmpImage, x++, h )) << 4);
451 srcbits += linebytes;
452 bits = srcbits + (left >> 1);
457 #define check_xy(x,y) \
458 if (x > width) { \
459 x = 0; \
460 if (lines) \
461 lines--; \
464 /***********************************************************************
465 * DIB_SetImageBits_RLE4
467 * SetDIBits for a 4-bit deep compressed DIB.
469 static void DIB_SetImageBits_RLE4( int lines, const BYTE *bits, DWORD width,
470 DWORD dstwidth, int left, int *colors, XImage *bmpImage )
472 int x = 0, c, length;
473 const BYTE *begin = bits;
475 dstwidth += left; /* FIXME: avoid putting x<left pixels */
477 lines--;
478 while ((int)lines >= 0)
480 length = *bits++;
481 if (length) { /* encoded */
482 c = *bits++;
483 while (length--) {
484 XPutPixel(bmpImage, x++, lines, colors[c >> 4]);
485 check_xy(x, y);
486 if (length) {
487 length--;
488 XPutPixel(bmpImage, x++, lines, colors[c & 0xf]);
489 check_xy(x, y);
492 } else {
493 length = *bits++;
494 switch (length) {
495 case 0: /* eol */
496 x = 0;
497 lines--;
498 continue;
500 case 1: /* eopicture */
501 return;
503 case 2: /* delta */
504 x += *bits++;
505 lines -= *bits++;
506 continue;
508 default: /* absolute */
509 while (length--) {
510 c = *bits++;
511 XPutPixel(bmpImage, x++, lines, colors[c >> 4]);
512 check_xy(x, y);
513 if (length) {
514 length--;
515 XPutPixel(bmpImage, x++, lines, colors[c & 0xf]);
516 check_xy(x, y);
519 if ((bits - begin) & 1)
520 bits++;
526 /***********************************************************************
527 * DIB_SetImageBits_8
529 * SetDIBits for an 8-bit deep DIB.
531 static void DIB_SetImageBits_8( int lines, const BYTE *srcbits,
532 DWORD srcwidth, DWORD dstwidth, int left,
533 int *colors, XImage *bmpImage )
535 DWORD x;
536 int h;
537 const BYTE *bits = srcbits + left;
539 /* align to 32 bit */
540 DWORD linebytes = (srcwidth + 3) & ~3;
542 dstwidth+=left;
544 if (lines > 0) {
545 for (h = lines - 1; h >= 0; h--) {
546 for (x = left; x < dstwidth; x++, bits++) {
547 XPutPixel( bmpImage, x, h, colors[*bits] );
549 bits = (srcbits += linebytes) + left;
551 } else {
552 lines = -lines;
553 for (h = 0; h < lines; h++) {
554 for (x = left; x < dstwidth; x++, bits++) {
555 XPutPixel( bmpImage, x, h, colors[*bits] );
557 bits = (srcbits += linebytes) + left;
562 /***********************************************************************
563 * DIB_GetImageBits_8
565 * GetDIBits for an 8-bit deep DIB.
567 static void DIB_GetImageBits_8( int lines, BYTE *srcbits,
568 DWORD srcwidth, DWORD dstwidth, int left,
569 int *colors, int nColors, XImage *bmpImage )
571 DWORD x;
572 int h;
573 BYTE *bits = srcbits + left;
575 /* align to 32 bit */
576 DWORD linebytes = (srcwidth + 3) & ~3;
578 dstwidth+=left;
580 if (lines > 0) {
581 for (h = lines - 1; h >= 0; h--) {
582 for (x = left; x < dstwidth; x++, bits++) {
583 if ( XGetPixel( bmpImage, x, h ) != colors[*bits] )
584 *bits = DIB_MapColor( colors, nColors,
585 XGetPixel( bmpImage, x, h ) );
587 bits = (srcbits += linebytes) + left;
589 } else {
590 lines = -lines;
591 for (h = 0; h < lines; h++) {
592 for (x = left; x < dstwidth; x++, bits++) {
593 if ( XGetPixel( bmpImage, x, h ) != colors[*bits] )
594 *bits = DIB_MapColor( colors, nColors,
595 XGetPixel( bmpImage, x, h ) );
597 bits = (srcbits += linebytes) + left;
602 /***********************************************************************
603 * DIB_SetImageBits_RLE8
605 * SetDIBits for an 8-bit deep compressed DIB.
607 * This function rewritten 941113 by James Youngman. WINE blew out when I
608 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
610 * This was because the algorithm assumed that all RLE8 bitmaps end with the
611 * 'End of bitmap' escape code. This code is very much laxer in what it
612 * allows to end the expansion. Possibly too lax. See the note by
613 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
614 * bitmap should end with RleEnd, but on the other hand, software exists
615 * that produces ones that don't and Windows 3.1 doesn't complain a bit
616 * about it.
618 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
619 * James A. Youngman <mbcstjy@afs.man.ac.uk>
620 * [JAY]
623 enum Rle8_EscapeCodes
626 * Apologies for polluting your file's namespace...
628 RleEol = 0, /* End of line */
629 RleEnd = 1, /* End of bitmap */
630 RleDelta = 2 /* Delta */
633 static void DIB_SetImageBits_RLE8( int lines, const BYTE *bits, DWORD width,
634 DWORD dstwidth, int left, int *colors, XImage *bmpImage )
636 int x; /* X-positon on each line. Increases. */
637 int line; /* Line #. Starts at lines-1, decreases */
638 const BYTE *pIn = bits; /* Pointer to current position in bits */
639 BYTE length; /* The length pf a run */
640 BYTE color_index; /* index into colors[] as read from bits */
641 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
642 WORD color; /* value of colour[color_index] */
644 if (lines == 0) /* Let's hope this doesn't happen. */
645 return;
647 dstwidth += left; /* FIXME: avoid putting x<left pixels */
650 * Note that the bitmap data is stored by Windows starting at the
651 * bottom line of the bitmap and going upwards. Within each line,
652 * the data is stored left-to-right. That's the reason why line
653 * goes from lines-1 to 0. [JAY]
656 x = 0;
657 line = lines-1;
660 length = *pIn++;
663 * If the length byte is not zero (which is the escape value),
664 * We have a run of length pixels all the same colour. The colour
665 * index is stored next.
667 * If the length byte is zero, we need to read the next byte to
668 * know what to do. [JAY]
670 if (length != 0)
673 * [Run-Length] Encoded mode
675 color_index = (*pIn++); /* Get the colour index. */
676 color = colors[color_index];
678 while(length--)
679 XPutPixel(bmpImage, x++, line, color);
681 else
684 * Escape codes (may be an absolute sequence though)
686 escape_code = (*pIn++);
687 switch(escape_code)
689 case RleEol: /* =0, end of line */
691 x = 0;
692 line--;
693 break;
696 case RleEnd: /* =1, end of bitmap */
699 * Not all RLE8 bitmaps end with this
700 * code. For example, Paint Shop Pro
701 * produces some that don't. That's (I think)
702 * what caused the previous implementation to
703 * fail. [JAY]
705 line=-1; /* Cause exit from do loop. */
706 break;
709 case RleDelta: /* =2, a delta */
712 * Note that deltaing to line 0
713 * will cause an exit from the loop,
714 * which may not be what is intended.
715 * The fact that there is a delta in the bits
716 * almost certainly implies that there is data
717 * to follow. You may feel that we should
718 * jump to the top of the loop to avoid exiting
719 * in this case.
721 * TODO: Decide what to do here in that case. [JAY]
723 x += (*pIn++);
724 line -= (*pIn++);
725 if (line == 0)
727 TRACE(bitmap, "Delta to last line of bitmap "
728 "(wrongly?) causes loop exit\n");
730 break;
733 default: /* >2, switch to absolute mode */
736 * Absolute Mode
738 length = escape_code;
739 while(length--)
741 color_index = (*pIn++);
742 XPutPixel(bmpImage, x++, line,
743 colors[color_index]);
747 * If you think for a moment you'll realise that the
748 * only time we could ever possibly read an odd
749 * number of bytes is when there is a 0x00 (escape),
750 * a value >0x02 (absolute mode) and then an odd-
751 * length run. Therefore this is the only place we
752 * need to worry about it. Everywhere else the
753 * bytes are always read in pairs. [JAY]
755 if (escape_code & 1)
756 pIn++; /* Throw away the pad byte. */
757 break;
759 } /* switch (escape_code) : Escape sequence */
760 } /* process either an encoded sequence or an escape sequence */
762 /* We expect to come here more than once per line. */
763 } while (line >= 0); /* Do this until the bitmap is filled */
766 * Everybody comes here at the end.
767 * Check how we exited the loop and print a message if it's a bit odd.
768 * [JAY]
770 if ( (*(pIn-2) != 0/*escape*/) || (*(pIn-1)!= RleEnd) )
772 TRACE(bitmap, "End-of-bitmap "
773 "without (strictly) proper escape code. Last two "
774 "bytes were: %02X %02X.\n",
775 (int)*(pIn-2),
776 (int)*(pIn-1));
781 /***********************************************************************
782 * DIB_SetImageBits_16
784 * SetDIBits for a 16-bit deep DIB.
786 static void DIB_SetImageBits_16( int lines, const BYTE *srcbits,
787 DWORD srcwidth, DWORD dstwidth, int left,
788 DC *dc, XImage *bmpImage )
790 DWORD x;
791 LPWORD ptr;
792 WORD val;
793 int h;
794 BYTE r, g, b;
796 /* align to 32 bit */
797 DWORD linebytes = (srcwidth * 2 + 3) & ~3;
799 dstwidth += left;
801 ptr = (LPWORD) srcbits + left;
802 if (lines > 0) {
803 for (h = lines - 1; h >= 0; h--) {
804 for (x = left; x < dstwidth; x++, ptr++) {
805 val = *ptr;
806 r = (BYTE) ((val & 0x7c00) >> 7);
807 g = (BYTE) ((val & 0x03e0) >> 2);
808 b = (BYTE) ((val & 0x001f) << 3);
809 XPutPixel( bmpImage, x, h,
810 COLOR_ToPhysical(dc, RGB(r,g,b)) );
812 ptr = (LPWORD) (srcbits += linebytes) + left;
814 } else {
815 lines = -lines;
816 for (h = 0; h < lines; h++) {
817 for (x = left; x < dstwidth; x++, ptr++) {
818 val = *ptr;
819 r = (BYTE) ((val & 0x7c00) >> 7);
820 g = (BYTE) ((val & 0x03e0) >> 2);
821 b = (BYTE) ((val & 0x001f) << 3);
822 XPutPixel( bmpImage, x, h,
823 COLOR_ToPhysical(dc, RGB(r,g,b)) );
825 ptr = (LPWORD) (srcbits += linebytes) + left;
831 /***********************************************************************
832 * DIB_GetImageBits_16
834 * GetDIBits for an 16-bit deep DIB.
836 static void DIB_GetImageBits_16( int lines, BYTE *srcbits,
837 DWORD srcwidth, DWORD dstwidth, int left,
838 int *colors, int nColors, XImage *bmpImage )
840 DWORD x;
841 LPWORD ptr;
842 int h;
843 BYTE r, g, b;
845 /* align to 32 bit */
846 DWORD linebytes = (srcwidth * 2 + 3) & ~3;
848 dstwidth += left;
850 ptr = (LPWORD) srcbits + left;
851 if (lines > 0) {
852 for (h = lines - 1; h >= 0; h--)
854 for (x = left; x < dstwidth; x++, ptr++)
856 COLORREF pixel = COLOR_ToLogical( XGetPixel( bmpImage, x, h ) );
857 r = (BYTE) GetRValue(pixel);
858 g = (BYTE) GetGValue(pixel);
859 b = (BYTE) GetBValue(pixel);
860 *ptr = ( ((r << 7) & 0x7c00) | ((g << 2) & 0x03e0) | ((b >> 3) & 0x001f) );
862 ptr = (LPWORD) (srcbits += linebytes) + left;
864 } else {
865 lines = -lines;
866 for (h = 0; h < lines; h++)
868 for (x = left; x < dstwidth; x++, ptr++)
870 COLORREF pixel = COLOR_ToLogical( XGetPixel( bmpImage, x, h ) );
871 r = (BYTE) GetRValue(pixel);
872 g = (BYTE) GetGValue(pixel);
873 b = (BYTE) GetBValue(pixel);
874 *ptr = ( ((r << 7) & 0x7c00) | ((g << 2) & 0x03e0) | ((b >> 3) & 0x001f) );
877 ptr = (LPWORD) (srcbits += linebytes) + left;
883 /***********************************************************************
884 * DIB_SetImageBits_24
886 * SetDIBits for a 24-bit deep DIB.
888 static void DIB_SetImageBits_24( int lines, const BYTE *srcbits,
889 DWORD srcwidth, DWORD dstwidth, int left,
890 DC *dc, XImage *bmpImage )
892 DWORD x;
893 const BYTE *bits = srcbits + left * 3;
894 int h;
896 /* align to 32 bit */
897 DWORD linebytes = (srcwidth * 3 + 3) & ~3;
899 dstwidth += left;
901 /* "bits" order is reversed for some reason */
903 if (lines > 0) {
904 for (h = lines - 1; h >= 0; h--) {
905 for (x = left; x < dstwidth; x++, bits += 3) {
906 XPutPixel( bmpImage, x, h,
907 COLOR_ToPhysical(dc, RGB(bits[2],bits[1],bits[0])));
909 bits = (srcbits += linebytes) + left * 3;
911 } else {
912 lines = -lines;
913 for (h = 0; h < lines; h++) {
914 for (x = left; x < dstwidth; x++, bits += 3) {
915 XPutPixel( bmpImage, x, h,
916 COLOR_ToPhysical(dc, RGB(bits[2],bits[1],bits[0])));
918 bits = (srcbits += linebytes) + left * 3;
924 /***********************************************************************
925 * DIB_GetImageBits_24
927 * GetDIBits for an 24-bit deep DIB.
929 static void DIB_GetImageBits_24( int lines, BYTE *srcbits,
930 DWORD srcwidth, DWORD dstwidth, int left,
931 int *colors, int nColors, XImage *bmpImage )
933 DWORD x;
934 int h;
935 BYTE *bits = srcbits + (left * 3);
937 /* align to 32 bit */
938 DWORD linebytes = (srcwidth * 3 + 3) & ~3;
940 dstwidth += left;
942 if (lines > 0) {
943 for (h = lines - 1; h >= 0; h--)
945 for (x = left; x < dstwidth; x++, bits += 3)
947 COLORREF pixel = COLOR_ToLogical( XGetPixel( bmpImage, x, h ) );
948 bits[0] = GetRValue(pixel);
949 bits[1] = GetGValue(pixel);
950 bits[2] = GetBValue(pixel);
952 bits = (srcbits += linebytes) + (left * 3);
954 } else {
955 lines = -lines;
956 for (h = 0; h < lines; h++)
958 for (x = left; x < dstwidth; x++, bits += 3)
960 COLORREF pixel = COLOR_ToLogical( XGetPixel( bmpImage, x, h ) );
961 bits[0] = GetRValue(pixel);
962 bits[1] = GetGValue(pixel);
963 bits[2] = GetBValue(pixel);
966 bits = (srcbits += linebytes) + (left * 3);
972 /***********************************************************************
973 * DIB_SetImageBits_32
975 * SetDIBits for a 32-bit deep DIB.
977 static void DIB_SetImageBits_32( int lines, const BYTE *srcbits,
978 DWORD srcwidth, DWORD dstwidth, int left,
979 DC *dc, XImage *bmpImage )
981 DWORD x;
982 const BYTE *bits = srcbits + left * 4;
983 int h;
985 DWORD linebytes = (srcwidth * 4);
987 dstwidth += left;
989 if (lines > 0) {
990 for (h = lines - 1; h >= 0; h--) {
991 for (x = left; x < dstwidth; x++, bits += 4) {
992 XPutPixel( bmpImage, x, h,
993 COLOR_ToPhysical(dc, RGB(bits[2],bits[1],bits[0])));
995 bits = (srcbits += linebytes) + left * 4;
997 } else {
998 lines = -lines;
999 for (h = 0; h < lines; h++) {
1000 for (x = left; x < dstwidth; x++, bits += 4) {
1001 XPutPixel( bmpImage, x, h,
1002 COLOR_ToPhysical(dc, RGB(bits[2],bits[1],bits[0])));
1004 bits = (srcbits += linebytes) + left * 4;
1010 /***********************************************************************
1011 * DIB_GetImageBits_32
1013 * GetDIBits for an 32-bit deep DIB.
1015 static void DIB_GetImageBits_32( int lines, BYTE *srcbits,
1016 DWORD srcwidth, DWORD dstwidth, int left,
1017 int *colors, int nColors, XImage *bmpImage )
1019 DWORD x;
1020 int h;
1021 BYTE *bits = srcbits + (left * 4);
1023 /* align to 32 bit */
1024 DWORD linebytes = (srcwidth * 4);
1026 dstwidth += left;
1028 if (lines > 0) {
1029 for (h = lines - 1; h >= 0; h--)
1031 for (x = left; x < dstwidth; x++, bits += 4)
1033 COLORREF pixel = COLOR_ToLogical( XGetPixel( bmpImage, x, h ) );
1034 bits[0] = GetRValue(pixel);
1035 bits[1] = GetGValue(pixel);
1036 bits[2] = GetBValue(pixel);
1038 bits = (srcbits += linebytes) + (left * 4);
1040 } else {
1041 lines = -lines;
1042 for (h = 0; h < lines; h++)
1044 for (x = left; x < dstwidth; x++, bits += 4)
1046 COLORREF pixel = COLOR_ToLogical( XGetPixel( bmpImage, x, h ) );
1047 bits[0] = GetRValue(pixel);
1048 bits[1] = GetGValue(pixel);
1049 bits[2] = GetBValue(pixel);
1052 bits = (srcbits += linebytes) + (left * 4);
1058 /***********************************************************************
1059 * DIB_SetImageBits
1061 * Transfer the bits to an X image.
1062 * Helper function for SetDIBits() and SetDIBitsToDevice().
1063 * The Xlib critical section must be entered before calling this function.
1065 static int DIB_SetImageBits( const DIB_SETIMAGEBITS_DESCR *descr )
1067 int *colorMapping;
1068 XImage *bmpImage;
1069 int lines;
1071 /* Translate the color mapping table */
1073 if (descr->infoBpp > 8) colorMapping = NULL;
1074 else if (!(colorMapping = DIB_PhysicalColorMap( descr->dc, descr->depth,
1075 descr->colorMap, descr->nColorMap )))
1076 return 0;
1078 if ( descr->dc && descr->dc->w.flags & DC_DIRTY )
1079 CLIPPING_UpdateGCRegion(descr->dc);
1081 /* Transfer the pixels */
1082 lines = descr->lines;
1083 if (lines < 0) lines = -lines;
1084 XCREATEIMAGE(bmpImage, descr->infoWidth, lines, descr->depth );
1086 switch(descr->infoBpp)
1088 case 1:
1089 DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
1090 descr->width, descr->xSrc, colorMapping, bmpImage );
1091 break;
1092 case 4:
1093 if (descr->compression) DIB_SetImageBits_RLE4( descr->lines, descr->bits,
1094 descr->infoWidth, descr->width, descr->xSrc,
1095 colorMapping, bmpImage );
1096 else DIB_SetImageBits_4( descr->lines, descr->bits, descr->infoWidth,
1097 descr->width, descr->xSrc, colorMapping, bmpImage );
1098 break;
1099 case 8:
1100 if (descr->compression) DIB_SetImageBits_RLE8( descr->lines, descr->bits,
1101 descr->infoWidth, descr->width, descr->xSrc,
1102 colorMapping, bmpImage );
1103 else DIB_SetImageBits_8( descr->lines, descr->bits, descr->infoWidth,
1104 descr->width, descr->xSrc, colorMapping, bmpImage );
1105 break;
1106 case 15:
1107 case 16:
1108 DIB_SetImageBits_16( descr->lines, descr->bits, descr->infoWidth,
1109 descr->width, descr->xSrc, descr->dc, bmpImage);
1110 break;
1111 case 24:
1112 DIB_SetImageBits_24( descr->lines, descr->bits, descr->infoWidth,
1113 descr->width, descr->xSrc, descr->dc, bmpImage );
1114 break;
1115 case 32:
1116 DIB_SetImageBits_32( descr->lines, descr->bits, descr->infoWidth,
1117 descr->width, descr->xSrc, descr->dc, bmpImage);
1118 break;
1119 default:
1120 WARN(bitmap, "(%d): Invalid depth\n", descr->infoBpp );
1121 break;
1123 if (colorMapping) HeapFree( GetProcessHeap(), 0, colorMapping );
1124 XPutImage( display, descr->drawable, descr->gc, bmpImage,
1125 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
1126 descr->width, descr->height );
1127 XDestroyImage( bmpImage );
1128 return lines;
1131 /***********************************************************************
1132 * DIB_GetImageBits
1134 * Transfer the bits from an X image.
1135 * The Xlib critical section must be entered before calling this function.
1137 static int DIB_GetImageBits( const DIB_SETIMAGEBITS_DESCR *descr )
1139 int *colorMapping;
1140 XImage *bmpImage;
1141 int lines;
1143 /* Translate the color mapping table */
1145 if (descr->infoBpp > 8) colorMapping = NULL;
1146 else if (!(colorMapping = DIB_PhysicalColorMap( descr->dc, descr->depth,
1147 descr->colorMap, descr->nColorMap )))
1148 return 0;
1150 /* Transfer the pixels */
1152 lines = descr->lines;
1153 if (lines < 0) lines = -lines;
1154 bmpImage = XGetImage( display, descr->drawable, descr->xDest, descr->yDest,
1155 descr->width, descr->height, AllPlanes, ZPixmap );
1157 switch(descr->infoBpp)
1159 case 1:
1160 FIXME(bitmap, "Depth 1 not yet supported!\n");
1161 break;
1163 case 4:
1164 if (descr->compression) FIXME(bitmap, "Compression not yet supported!\n");
1165 else DIB_GetImageBits_4( descr->lines, (LPVOID)descr->bits, descr->infoWidth,
1166 descr->width, descr->xSrc,
1167 colorMapping, descr->nColorMap, bmpImage );
1168 break;
1170 case 8:
1171 if (descr->compression) FIXME(bitmap, "Compression not yet supported!\n");
1172 else DIB_GetImageBits_8( descr->lines, (LPVOID)descr->bits, descr->infoWidth,
1173 descr->width, descr->xSrc,
1174 colorMapping, descr->nColorMap, bmpImage );
1175 break;
1177 case 15:
1178 case 16:
1179 DIB_GetImageBits_16( descr->lines, (LPVOID)descr->bits, descr->infoWidth,
1180 descr->width, descr->xSrc,
1181 colorMapping, descr->nColorMap, bmpImage );
1182 break;
1184 case 24:
1185 DIB_GetImageBits_24( descr->lines, (LPVOID)descr->bits, descr->infoWidth,
1186 descr->width, descr->xSrc,
1187 colorMapping, descr->nColorMap, bmpImage );
1188 break;
1190 case 32:
1191 DIB_GetImageBits_32( descr->lines, (LPVOID)descr->bits, descr->infoWidth,
1192 descr->width, descr->xSrc,
1193 colorMapping, descr->nColorMap, bmpImage );
1194 break;
1196 default:
1197 WARN(bitmap, "(%d): Invalid depth\n", descr->infoBpp );
1198 break;
1201 if (colorMapping) HeapFree( GetProcessHeap(), 0, colorMapping );
1203 XDestroyImage( bmpImage );
1204 return lines;
1208 /***********************************************************************
1209 * StretchDIBits16 (GDI.439)
1211 INT16 WINAPI StretchDIBits16(HDC16 hdc, INT16 xDst, INT16 yDst, INT16 widthDst,
1212 INT16 heightDst, INT16 xSrc, INT16 ySrc, INT16 widthSrc,
1213 INT16 heightSrc, const VOID *bits,
1214 const BITMAPINFO *info, UINT16 wUsage, DWORD dwRop )
1216 return (INT16)StretchDIBits32( hdc, xDst, yDst, widthDst, heightDst,
1217 xSrc, ySrc, widthSrc, heightSrc, bits,
1218 info, wUsage, dwRop );
1222 /***********************************************************************
1223 * StretchDIBits32 (GDI32.351)
1225 INT32 WINAPI StretchDIBits32(HDC32 hdc, INT32 xDst, INT32 yDst, INT32 widthDst,
1226 INT32 heightDst, INT32 xSrc, INT32 ySrc, INT32 widthSrc,
1227 INT32 heightSrc, const void *bits,
1228 const BITMAPINFO *info, UINT32 wUsage, DWORD dwRop )
1230 HBITMAP32 hBitmap, hOldBitmap;
1231 HDC32 hdcMem;
1233 hBitmap = CreateDIBitmap32( hdc, &info->bmiHeader, CBM_INIT,
1234 bits, info, wUsage );
1235 hdcMem = CreateCompatibleDC32( hdc );
1236 hOldBitmap = SelectObject32( hdcMem, hBitmap );
1237 StretchBlt32( hdc, xDst, yDst, widthDst, heightDst,
1238 hdcMem, xSrc, ySrc, widthSrc, heightSrc, dwRop );
1239 SelectObject32( hdcMem, hOldBitmap );
1240 DeleteDC32( hdcMem );
1241 DeleteObject32( hBitmap );
1242 return heightSrc;
1246 /***********************************************************************
1247 * SetDIBits16 (GDI.440)
1249 INT16 WINAPI SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
1250 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
1251 UINT16 coloruse )
1253 return SetDIBits32( hdc, hbitmap, startscan, lines, bits, info, coloruse );
1257 /******************************************************************************
1258 * SetDIBits32 [GDI32.312] Sets pixels in a bitmap using colors from DIB
1260 * PARAMS
1261 * hdc [I] Handle to device context
1262 * hbitmap [I] Handle to bitmap
1263 * startscan [I] Starting scan line
1264 * lines [I] Number of scan lines
1265 * bits [I] Array of bitmap bits
1266 * info [I] Address of structure with data
1267 * coloruse [I] Type of color indexes to use
1269 * RETURNS
1270 * Success: Number of scan lines copied
1271 * Failure: 0
1273 INT32 WINAPI SetDIBits32( HDC32 hdc, HBITMAP32 hbitmap, UINT32 startscan,
1274 UINT32 lines, LPCVOID bits, const BITMAPINFO *info,
1275 UINT32 coloruse )
1277 DIB_SETIMAGEBITS_DESCR descr;
1278 BITMAPOBJ * bmp;
1279 int height, tmpheight;
1280 INT32 result;
1282 /* Check parameters */
1284 descr.dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1285 if (!descr.dc)
1287 descr.dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
1288 if (!descr.dc) return 0;
1290 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
1292 GDI_HEAP_UNLOCK( hdc );
1293 return 0;
1295 if (DIB_GetBitmapInfo( &info->bmiHeader, &descr.infoWidth, &height,
1296 &descr.infoBpp, &descr.compression ) == -1)
1298 GDI_HEAP_UNLOCK( hbitmap );
1299 GDI_HEAP_UNLOCK( hdc );
1300 return 0;
1302 tmpheight = height;
1303 if (height < 0) height = -height;
1304 if (!lines || (startscan >= height))
1306 GDI_HEAP_UNLOCK( hbitmap );
1307 GDI_HEAP_UNLOCK( hdc );
1308 return 0;
1310 if (startscan + lines > height) lines = height - startscan;
1312 if (descr.infoBpp <= 8)
1314 descr.colorMap = DIB_BuildColorMap( info, coloruse == DIB_PAL_COLORS?
1315 descr.dc->w.hPalette : 0,
1316 &descr.nColorMap);
1318 if (!descr.colorMap)
1320 GDI_HEAP_UNLOCK( hbitmap );
1321 GDI_HEAP_UNLOCK( hdc );
1322 return 0;
1328 descr.bits = bits;
1329 descr.lines = tmpheight >= 0 ? lines : -lines;
1330 descr.depth = bmp->bitmap.bmBitsPixel;
1331 descr.drawable = bmp->pixmap;
1332 descr.gc = BITMAP_GC(bmp);
1333 descr.xSrc = 0;
1334 descr.ySrc = 0;
1335 descr.xDest = 0;
1336 descr.yDest = height - startscan - lines;
1337 descr.width = bmp->bitmap.bmWidth;
1338 descr.height = lines;
1340 EnterCriticalSection( &X11DRV_CritSection );
1341 result = CALL_LARGE_STACK( DIB_SetImageBits, &descr );
1342 LeaveCriticalSection( &X11DRV_CritSection );
1344 HeapFree(GetProcessHeap(), 0, descr.colorMap);
1346 GDI_HEAP_UNLOCK( hdc );
1347 GDI_HEAP_UNLOCK( hbitmap );
1348 return result;
1352 /***********************************************************************
1353 * SetDIBitsToDevice16 (GDI.443)
1355 INT16 WINAPI SetDIBitsToDevice16(HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
1356 INT16 cy, INT16 xSrc, INT16 ySrc, UINT16 startscan,
1357 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
1358 UINT16 coloruse )
1360 return SetDIBitsToDevice32( hdc, xDest, yDest, cx, cy, xSrc, ySrc,
1361 startscan, lines, bits, info, coloruse );
1365 /***********************************************************************
1366 * SetDIBitsToDevice32 (GDI32.313)
1368 INT32 WINAPI SetDIBitsToDevice32(HDC32 hdc, INT32 xDest, INT32 yDest, DWORD cx,
1369 DWORD cy, INT32 xSrc, INT32 ySrc, UINT32 startscan,
1370 UINT32 lines, LPCVOID bits, const BITMAPINFO *info,
1371 UINT32 coloruse )
1373 DIB_SETIMAGEBITS_DESCR descr;
1374 DC * dc;
1375 DWORD width, oldcy = cy;
1376 INT32 result;
1377 int height, tmpheight;
1379 /* Check parameters */
1381 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1382 if (!dc)
1384 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
1385 if (!dc) return 0;
1387 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
1388 &descr.infoBpp, &descr.compression ) == -1)
1389 return 0;
1390 tmpheight = height;
1391 if (height < 0) height = -height;
1392 if (!lines || (startscan >= height)) return 0;
1393 if (startscan + lines > height) lines = height - startscan;
1394 if (ySrc < startscan) ySrc = startscan;
1395 else if (ySrc >= startscan + lines) return 0;
1396 if (xSrc >= width) return 0;
1397 if (ySrc + cy >= startscan + lines) cy = startscan + lines - ySrc;
1398 if (xSrc + cx >= width) cx = width - xSrc;
1399 if (!cx || !cy) return 0;
1401 DC_SetupGCForText( dc ); /* To have the correct colors */
1402 TSXSetFunction( display, dc->u.x.gc, DC_XROPfunction[dc->w.ROPmode-1] );
1404 descr.dc = dc;
1405 if (descr.infoBpp <= 8)
1407 descr.colorMap = DIB_BuildColorMap( info, coloruse == DIB_PAL_COLORS?
1408 descr.dc->w.hPalette : 0,
1409 &descr.nColorMap );
1411 if (!descr.colorMap)
1412 return 0;
1414 descr.bits = bits;
1415 descr.lines = tmpheight >= 0 ? lines : -lines;
1416 descr.infoWidth = width;
1417 descr.depth = dc->w.bitsPerPixel;
1418 descr.drawable = dc->u.x.drawable;
1419 descr.gc = dc->u.x.gc;
1420 descr.xSrc = xSrc;
1421 descr.ySrc = tmpheight >= 0 ? lines-(ySrc-startscan)-cy+(oldcy-cy) : ySrc - startscan;
1422 descr.xDest = dc->w.DCOrgX + XLPTODP( dc, xDest );
1423 descr.yDest = dc->w.DCOrgY + YLPTODP( dc, yDest ) + (tmpheight >= 0 ? oldcy-cy : 0);
1424 descr.width = cx;
1425 descr.height = cy;
1427 EnterCriticalSection( &X11DRV_CritSection );
1428 result = CALL_LARGE_STACK( DIB_SetImageBits, &descr );
1429 LeaveCriticalSection( &X11DRV_CritSection );
1431 HeapFree(GetProcessHeap(), 0, descr.colorMap);
1432 return result;
1435 /***********************************************************************
1436 * SetDIBColorTable32 (GDI32.311)
1438 UINT32 WINAPI SetDIBColorTable32( HDC32 hdc, UINT32 startpos, UINT32 entries,
1439 RGBQUAD *colors )
1441 DC * dc;
1442 PALETTEENTRY * palEntry;
1443 PALETTEOBJ * palette;
1444 RGBQUAD *end;
1446 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1447 if (!dc)
1449 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
1450 if (!dc) return 0;
1453 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
1455 return 0;
1458 /* Transfer color info */
1460 if (dc->w.bitsPerPixel <= 8) {
1461 palEntry = palette->logpalette.palPalEntry + startpos;
1462 if (startpos + entries > (1 << dc->w.bitsPerPixel)) {
1463 entries = (1 << dc->w.bitsPerPixel) - startpos;
1465 for (end = colors + entries; colors < end; palEntry++, colors++)
1467 palEntry->peRed = colors->rgbRed;
1468 palEntry->peGreen = colors->rgbGreen;
1469 palEntry->peBlue = colors->rgbBlue;
1471 } else {
1472 entries = 0;
1474 GDI_HEAP_UNLOCK( dc->w.hPalette );
1475 return entries;
1478 /***********************************************************************
1479 * GetDIBColorTable32 (GDI32.169)
1481 UINT32 WINAPI GetDIBColorTable32( HDC32 hdc, UINT32 startpos, UINT32 entries,
1482 RGBQUAD *colors )
1484 DC * dc;
1485 PALETTEENTRY * palEntry;
1486 PALETTEOBJ * palette;
1487 RGBQUAD *end;
1489 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1490 if (!dc)
1492 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
1493 if (!dc) return 0;
1496 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
1498 return 0;
1501 /* Transfer color info */
1503 if (dc->w.bitsPerPixel <= 8) {
1504 palEntry = palette->logpalette.palPalEntry + startpos;
1505 if (startpos + entries > (1 << dc->w.bitsPerPixel)) {
1506 entries = (1 << dc->w.bitsPerPixel) - startpos;
1508 for (end = colors + entries; colors < end; palEntry++, colors++)
1510 colors->rgbRed = palEntry->peRed;
1511 colors->rgbGreen = palEntry->peGreen;
1512 colors->rgbBlue = palEntry->peBlue;
1513 colors->rgbReserved = 0;
1515 } else {
1516 entries = 0;
1518 GDI_HEAP_UNLOCK( dc->w.hPalette );
1519 return entries;
1522 /***********************************************************************
1523 * GetDIBits16 (GDI.441)
1525 INT16 WINAPI GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
1526 UINT16 lines, LPSTR bits, BITMAPINFO * info,
1527 UINT16 coloruse )
1529 return GetDIBits32( hdc, hbitmap, startscan, lines, bits, info, coloruse );
1533 /******************************************************************************
1534 * GetDIBits32 [GDI32.170] Retrieves bits of bitmap and copies to buffer
1536 * RETURNS
1537 * Success: Number of scan lines copied from bitmap
1538 * Failure: 0
1540 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
1542 INT32 WINAPI GetDIBits32(
1543 HDC32 hdc, /* [in] Handle to device context */
1544 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
1545 UINT32 startscan, /* [in] First scan line to set in dest bitmap */
1546 UINT32 lines, /* [in] Number of scan lines to copy */
1547 LPSTR bits, /* [out] Address of array for bitmap bits */
1548 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
1549 UINT32 coloruse) /* [in] RGB or palette index */
1551 DC * dc;
1552 BITMAPOBJ * bmp;
1553 PALETTEENTRY * palEntry;
1554 PALETTEOBJ * palette;
1555 XImage * bmpImage;
1556 int i, x, y;
1558 if (!lines) return 0;
1559 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1560 if (!dc)
1562 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
1563 if (!dc) return 0;
1565 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
1566 return 0;
1567 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
1569 GDI_HEAP_UNLOCK( hbitmap );
1570 return 0;
1573 /* Transfer color info */
1575 if (info->bmiHeader.biBitCount<=8) {
1576 palEntry = palette->logpalette.palPalEntry;
1577 for (i = 0; i < info->bmiHeader.biClrUsed; i++, palEntry++)
1579 if (coloruse == DIB_RGB_COLORS)
1581 info->bmiColors[i].rgbRed = palEntry->peRed;
1582 info->bmiColors[i].rgbGreen = palEntry->peGreen;
1583 info->bmiColors[i].rgbBlue = palEntry->peBlue;
1584 info->bmiColors[i].rgbReserved = 0;
1586 else ((WORD *)info->bmiColors)[i] = (WORD)i;
1590 if (bits)
1592 BYTE* bbits = bits;
1593 int pad, yend, xend = bmp->bitmap.bmWidth;
1595 TRACE(bitmap, "%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
1596 lines, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
1597 (int)info->bmiHeader.biWidth, (int)info->bmiHeader.biHeight, startscan );
1599 /* adjust number of scanlines to copy */
1601 if( lines > info->bmiHeader.biHeight ) lines = info->bmiHeader.biHeight;
1602 yend = startscan + lines;
1603 if( startscan >= bmp->bitmap.bmHeight )
1605 GDI_HEAP_UNLOCK( hbitmap );
1606 GDI_HEAP_UNLOCK( dc->w.hPalette );
1607 return FALSE;
1609 if( yend > bmp->bitmap.bmHeight ) yend = bmp->bitmap.bmHeight;
1611 /* adjust scanline width */
1613 pad = info->bmiHeader.biWidth - bmp->bitmap.bmWidth;
1614 if( pad < 0 )
1616 /* bitmap is wider than DIB, copy only a part */
1618 pad = 0;
1619 xend = info->bmiHeader.biWidth;
1622 EnterCriticalSection( &X11DRV_CritSection );
1623 bmpImage = (XImage *)CALL_LARGE_STACK( BITMAP_GetXImage, bmp );
1625 switch( info->bmiHeader.biBitCount )
1627 case 8:
1628 /* pad up to 32 bit */
1629 pad += (4 - (info->bmiHeader.biWidth & 3)) & 3;
1630 for( y = yend - 1; (int)y >= (int)startscan; y-- )
1632 for( x = 0; x < xend; x++ )
1633 *bbits++ = XGetPixel( bmpImage, x, y );
1634 bbits += pad;
1636 break;
1637 case 1:
1638 pad += ((32 - (info->bmiHeader.biWidth & 31)) / 8) & 3;
1639 for( y = yend - 1; (int)y >= (int)startscan; y-- )
1641 *bbits = 0;
1642 for( x = 0; x < xend; x++ ) {
1644 *bbits |= XGetPixel( bmpImage, x, y)<<(7-(x&7));
1645 if ((x&7)==7) {
1646 bbits++;
1647 *bbits=0;
1650 bbits += pad;
1652 break;
1653 case 4:
1654 pad += ((8 - (info->bmiHeader.biWidth & 7)) / 2) & 3;
1655 for( y = yend - 1; (int)y >= (int)startscan; y-- )
1657 *bbits = 0;
1658 for( x = 0; x < xend; x++ ) {
1660 *bbits |= XGetPixel( bmpImage, x, y)<<(4*(1-(x&1)));
1661 if ((x&1)==1) {
1662 bbits++;
1663 *bbits=0;
1666 bbits += pad;
1668 break;
1669 case 15:
1670 case 16:
1671 pad += (4 - ((info->bmiHeader.biWidth*2) & 3)) & 3;
1672 for( y = yend - 1; (int)y >= (int)startscan; y-- )
1674 *bbits = 0;
1675 for( x = 0; x < xend; x++ ) {
1676 unsigned long pixel=XGetPixel( bmpImage, x, y);
1677 *bbits++ = pixel & 0xff;
1678 *bbits++ = (pixel >> 8) & 0xff;
1680 bbits += pad;
1682 break;
1683 case 24:
1684 pad += (4 - ((info->bmiHeader.biWidth*3) & 3)) & 3;
1685 for( y = yend - 1; (int)y >= (int)startscan; y-- )
1687 *bbits = 0;
1688 for( x = 0; x < xend; x++ ) {
1689 unsigned long pixel=XGetPixel( bmpImage, x, y);
1690 *bbits++ = (pixel >>16) & 0xff;
1691 *bbits++ = (pixel >> 8) & 0xff;
1692 *bbits++ = pixel & 0xff;
1694 bbits += pad;
1696 break;
1697 case 32:
1698 for( y = yend - 1; (int)y >= (int)startscan; y-- )
1700 *bbits = 0;
1701 for( x = 0; x < xend; x++ ) {
1702 unsigned long pixel=XGetPixel( bmpImage, x, y);
1703 *bbits++ = (pixel >>16) & 0xff;
1704 *bbits++ = (pixel >> 8) & 0xff;
1705 *bbits++ = pixel & 0xff;
1708 break;
1709 default:
1710 WARN(bitmap,"Unsupported depth %d\n",
1711 info->bmiHeader.biBitCount);
1712 break;
1715 XDestroyImage( bmpImage );
1716 LeaveCriticalSection( &X11DRV_CritSection );
1718 info->bmiHeader.biCompression = 0;
1720 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
1722 /* fill in struct members */
1724 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
1725 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
1726 info->bmiHeader.biPlanes = 1;
1727 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
1728 info->bmiHeader.biSizeImage = bmp->bitmap.bmHeight *
1729 DIB_GetDIBWidthBytes( bmp->bitmap.bmWidth,
1730 bmp->bitmap.bmBitsPixel );
1731 info->bmiHeader.biCompression = 0;
1734 GDI_HEAP_UNLOCK( hbitmap );
1735 GDI_HEAP_UNLOCK( dc->w.hPalette );
1736 return lines;
1740 /***********************************************************************
1741 * CreateDIBitmap16 (GDI.442)
1743 HBITMAP16 WINAPI CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
1744 DWORD init, LPCVOID bits, const BITMAPINFO * data,
1745 UINT16 coloruse )
1747 return CreateDIBitmap32( hdc, header, init, bits, data, coloruse );
1751 /***********************************************************************
1752 * CreateDIBitmap32 (GDI32.37)
1754 HBITMAP32 WINAPI CreateDIBitmap32( HDC32 hdc, const BITMAPINFOHEADER *header,
1755 DWORD init, LPCVOID bits, const BITMAPINFO *data,
1756 UINT32 coloruse )
1758 HBITMAP32 handle;
1759 BOOL32 fColor;
1760 DWORD width;
1761 int height;
1762 WORD bpp;
1763 WORD compr;
1765 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
1766 if (height < 0) height = -height;
1768 /* Check if we should create a monochrome or color bitmap. */
1769 /* We create a monochrome bitmap only if it has exactly 2 */
1770 /* colors, which are either black or white, nothing else. */
1771 /* In all other cases, we create a color bitmap. */
1773 if (bpp != 1) fColor = TRUE;
1774 else if ((coloruse != DIB_RGB_COLORS) ||
1775 (init != CBM_INIT) || !data) fColor = FALSE;
1776 else
1778 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
1780 RGBQUAD *rgb = data->bmiColors;
1781 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
1782 if ((col == RGB(0,0,0)) || (col == RGB(0xff,0xff,0xff)))
1784 rgb++;
1785 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
1786 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
1788 else fColor = TRUE;
1790 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
1792 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
1793 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
1794 if ((col == RGB(0,0,0)) || (col == RGB(0xff,0xff,0xff)))
1796 rgb++;
1797 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
1798 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
1800 else fColor = TRUE;
1802 else
1804 WARN(bitmap, "(%ld): wrong size for data\n",
1805 data->bmiHeader.biSize );
1806 return 0;
1810 /* Now create the bitmap */
1812 handle = fColor ? CreateBitmap32( width, height, 1, screenDepth, NULL ) :
1813 CreateBitmap32( width, height, 1, 1, NULL );
1814 if (!handle) return 0;
1816 if (init == CBM_INIT)
1817 SetDIBits32( hdc, handle, 0, height, bits, data, coloruse );
1818 return handle;
1822 /***********************************************************************
1823 * DIB_DoProtectDIBSection
1825 static void DIB_DoProtectDIBSection( BITMAPOBJ *bmp, DWORD new_prot )
1827 DIBSECTION *dib = bmp->dibSection;
1828 INT32 effHeight = dib->dsBm.bmHeight >= 0? dib->dsBm.bmHeight
1829 : -dib->dsBm.bmHeight;
1830 INT32 totalSize = dib->dsBmih.biSizeImage? dib->dsBmih.biSizeImage
1831 : dib->dsBm.bmWidthBytes * effHeight;
1832 DWORD old_prot;
1834 VirtualProtect(dib->dsBm.bmBits, totalSize, new_prot, &old_prot);
1835 TRACE(bitmap, "Changed protection from %ld to %ld\n",
1836 old_prot, new_prot);
1839 /***********************************************************************
1840 * DIB_DoUpdateDIBSection
1842 static void DIB_DoUpdateDIBSection( BITMAPOBJ *bmp, BOOL32 toDIB )
1844 DIBSECTION *dib = bmp->dibSection;
1845 DIB_SETIMAGEBITS_DESCR descr;
1847 if (DIB_GetBitmapInfo( &dib->dsBmih, &descr.infoWidth, &descr.lines,
1848 &descr.infoBpp, &descr.compression ) == -1)
1849 return;
1851 descr.dc = NULL;
1852 descr.colorMap = bmp->colorMap;
1853 descr.nColorMap = bmp->nColorMap;
1854 descr.bits = dib->dsBm.bmBits;
1855 descr.depth = bmp->bitmap.bmBitsPixel;
1856 descr.drawable = bmp->pixmap;
1857 descr.gc = BITMAP_GC(bmp);
1858 descr.xSrc = 0;
1859 descr.ySrc = 0;
1860 descr.xDest = 0;
1861 descr.yDest = 0;
1862 descr.width = bmp->bitmap.bmWidth;
1863 descr.height = bmp->bitmap.bmHeight;
1865 if (toDIB)
1867 TRACE(bitmap, "Copying from Pixmap to DIB bits\n");
1868 EnterCriticalSection( &X11DRV_CritSection );
1869 CALL_LARGE_STACK( DIB_GetImageBits, &descr );
1870 LeaveCriticalSection( &X11DRV_CritSection );
1872 else
1874 TRACE(bitmap, "Copying from DIB bits to Pixmap\n");
1875 EnterCriticalSection( &X11DRV_CritSection );
1876 CALL_LARGE_STACK( DIB_SetImageBits, &descr );
1877 LeaveCriticalSection( &X11DRV_CritSection );
1881 /***********************************************************************
1882 * DIB_FaultHandler
1884 static BOOL32 DIB_FaultHandler( LPVOID res, LPVOID addr )
1886 BOOL32 handled = FALSE;
1887 BITMAPOBJ *bmp;
1889 bmp = (BITMAPOBJ *)GDI_GetObjPtr( (HBITMAP32)res, BITMAP_MAGIC );
1890 if (!bmp) return FALSE;
1892 if (bmp->dibSection)
1893 switch (bmp->status)
1895 case DIB_GdiMod:
1896 TRACE( bitmap, "called in status DIB_GdiMod\n" );
1897 DIB_DoProtectDIBSection( bmp, PAGE_READWRITE );
1898 DIB_DoUpdateDIBSection( bmp, TRUE );
1899 DIB_DoProtectDIBSection( bmp, PAGE_READONLY );
1900 bmp->status = DIB_InSync;
1901 handled = TRUE;
1902 break;
1904 case DIB_InSync:
1905 TRACE( bitmap, "called in status DIB_InSync\n" );
1906 DIB_DoProtectDIBSection( bmp, PAGE_READWRITE );
1907 bmp->status = DIB_AppMod;
1908 handled = TRUE;
1909 break;
1911 case DIB_AppMod:
1912 FIXME( bitmap, "called in status DIB_AppMod: "
1913 "this can't happen!\n" );
1914 break;
1916 case DIB_NoHandler:
1917 FIXME( bitmap, "called in status DIB_NoHandler: "
1918 "this can't happen!\n" );
1919 break;
1922 GDI_HEAP_UNLOCK( (HBITMAP32)res );
1923 return handled;
1926 /***********************************************************************
1927 * DIB_UpdateDIBSection
1929 void DIB_UpdateDIBSection( DC *dc, BOOL32 toDIB )
1931 BITMAPOBJ *bmp;
1933 /* Ensure this is a Compatible DC that has a DIB section selected */
1935 if (!dc) return;
1936 if (!(dc->w.flags & DC_MEMORY)) return;
1938 bmp = (BITMAPOBJ *)GDI_GetObjPtr( dc->w.hBitmap, BITMAP_MAGIC );
1939 if (!bmp) return;
1941 if (!bmp->dibSection)
1943 GDI_HEAP_UNLOCK(dc->w.hBitmap);
1944 return;
1948 if (!toDIB)
1950 /* Prepare for access to the DIB by GDI functions */
1952 switch (bmp->status)
1954 default:
1955 case DIB_NoHandler:
1956 DIB_DoUpdateDIBSection( bmp, FALSE );
1957 break;
1959 case DIB_GdiMod:
1960 TRACE( bitmap, "fromDIB called in status DIB_GdiMod\n" );
1961 /* nothing to do */
1962 break;
1964 case DIB_InSync:
1965 TRACE( bitmap, "fromDIB called in status DIB_InSync\n" );
1966 /* nothing to do */
1967 break;
1969 case DIB_AppMod:
1970 TRACE( bitmap, "fromDIB called in status DIB_AppMod\n" );
1971 DIB_DoUpdateDIBSection( bmp, FALSE );
1972 DIB_DoProtectDIBSection( bmp, PAGE_READONLY );
1973 bmp->status = DIB_InSync;
1974 break;
1977 else
1979 /* Acknowledge write access to the DIB by GDI functions */
1981 switch (bmp->status)
1983 default:
1984 case DIB_NoHandler:
1985 DIB_DoUpdateDIBSection( bmp, TRUE );
1986 break;
1988 case DIB_GdiMod:
1989 TRACE( bitmap, " toDIB called in status DIB_GdiMod\n" );
1990 /* nothing to do */
1991 break;
1993 case DIB_InSync:
1994 TRACE( bitmap, " toDIB called in status DIB_InSync\n" );
1995 DIB_DoProtectDIBSection( bmp, PAGE_NOACCESS );
1996 bmp->status = DIB_GdiMod;
1997 break;
1999 case DIB_AppMod:
2000 FIXME( bitmap, " toDIB called in status DIB_AppMod: "
2001 "this can't happen!\n" );
2002 break;
2007 GDI_HEAP_UNLOCK(dc->w.hBitmap);
2010 /***********************************************************************
2011 * CreateDIBSection16 (GDI.489)
2013 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
2014 LPVOID **bits, HANDLE16 section,
2015 DWORD offset)
2017 return CreateDIBSection32(hdc, bmi, usage, bits, section, offset);
2020 /***********************************************************************
2021 * CreateDIBSection32 (GDI32.36)
2023 HBITMAP32 WINAPI CreateDIBSection32 (HDC32 hdc, BITMAPINFO *bmi, UINT32 usage,
2024 LPVOID **bits,HANDLE32 section,
2025 DWORD offset)
2027 HBITMAP32 res = 0;
2028 BITMAPOBJ *bmp = NULL;
2029 DIBSECTION *dib = NULL;
2030 RGBQUAD *colorMap = NULL;
2031 int nColorMap;
2033 /* Fill BITMAP32 structure with DIB data */
2034 BITMAPINFOHEADER *bi = &bmi->bmiHeader;
2035 INT32 effHeight, totalSize;
2036 BITMAP32 bm;
2038 TRACE(bitmap, "format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
2039 bi->biWidth, bi->biHeight, bi->biPlanes, bi->biBitCount,
2040 bi->biSizeImage, bi->biClrUsed, usage == DIB_PAL_COLORS? "PAL" : "RGB");
2042 if (bi->biCompression)
2044 FIXME(bitmap, "Compression not implemented!\n");
2045 return 0;
2048 bm.bmType = 0;
2049 bm.bmWidth = bi->biWidth;
2050 bm.bmHeight = bi->biHeight;
2051 bm.bmWidthBytes = DIB_GetDIBWidthBytes(bm.bmWidth, bi->biBitCount);
2052 bm.bmPlanes = bi->biPlanes;
2053 bm.bmBitsPixel = bi->biBitCount;
2054 bm.bmBits = NULL;
2056 /* Get storage location for DIB bits */
2057 effHeight = bm.bmHeight >= 0 ? bm.bmHeight : -bm.bmHeight;
2058 totalSize = bi->biSizeImage? bi->biSizeImage : bm.bmWidthBytes * effHeight;
2060 if (section)
2061 bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
2062 0L, offset, totalSize);
2063 else
2064 bm.bmBits = VirtualAlloc(NULL, totalSize,
2065 MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
2067 /* Allocate Memory for DIB and fill structure */
2068 if (bm.bmBits)
2069 dib = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DIBSECTION));
2070 if (dib)
2072 dib->dsBm = bm;
2073 dib->dsBmih = *bi;
2074 /* FIXME: dib->dsBitfields ??? */
2075 dib->dshSection = section;
2076 dib->dsOffset = offset;
2079 /* Create Color Map */
2080 if (dib && bm.bmBitsPixel <= 8)
2082 HPALETTE16 hPalette = 0;
2083 if (usage == DIB_PAL_COLORS)
2085 DC *dc = (DC *)GDI_GetObjPtr(hdc, DC_MAGIC);
2086 if (!dc)
2087 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
2088 if (dc)
2090 hPalette = dc->w.hPalette;
2091 GDI_HEAP_UNLOCK(hdc);
2094 colorMap = DIB_BuildColorMap(bmi, hPalette, &nColorMap);
2097 /* Create Device Dependent Bitmap and add DIB/ColorMap info */
2098 if (dib)
2100 res = CreateDIBitmap32(hdc, bi, 0, NULL, bmi, usage);
2101 bmp = (BITMAPOBJ *) GDI_GetObjPtr(res, BITMAP_MAGIC);
2102 if (bmp)
2104 bmp->dibSection = dib;
2105 bmp->colorMap = colorMap;
2106 bmp->nColorMap = nColorMap;
2107 bmp->status = DIB_NoHandler;
2111 /* Clean up in case of errors */
2112 if (!res || !bmp || !dib || !bm.bmBits || (bm.bmBitsPixel <= 8 && !colorMap))
2114 if (bm.bmBits)
2115 if (section)
2116 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
2117 else
2118 VirtualFree(bm.bmBits, MEM_RELEASE, 0L), bm.bmBits = NULL;
2120 if (colorMap) HeapFree(GetProcessHeap(), 0, colorMap), colorMap = NULL;
2121 if (dib) HeapFree(GetProcessHeap(), 0, dib), dib = NULL;
2122 if (res) DeleteObject32(res), res = 0;
2125 /* Install fault handler, if possible */
2126 if (bm.bmBits)
2127 if (VIRTUAL_SetFaultHandler(bm.bmBits, DIB_FaultHandler, (LPVOID)res))
2129 DIB_DoProtectDIBSection( bmp, PAGE_READONLY );
2130 bmp->status = DIB_InSync;
2133 /* Return BITMAP handle and storage location */
2134 if (res) GDI_HEAP_UNLOCK(res);
2135 if (bm.bmBits && bits) *bits = bm.bmBits;
2136 return res;