Release 980503
[wine/hacks.git] / objects / dib.c
bloba5ae6e370df99eb9ea4c7af445b1749889558676
1 /*
2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "ts_xlib.h"
10 #include "ts_xutil.h"
11 #include "dc.h"
12 #include "bitmap.h"
13 #include "callback.h"
14 #include "palette.h"
15 #include "color.h"
16 #include "debug.h"
18 extern void CLIPPING_UpdateGCRegion(DC* );
20 static int bitmapDepthTable[] = { 8, 1, 32, 16, 24, 15, 4, 0 };
21 static int ximageDepthTable[] = { 0, 0, 0, 0, 0, 0, 0 };
24 /* This structure holds the arguments for DIB_SetImageBits() */
25 typedef struct
27 DC *dc;
28 LPCVOID bits;
29 int lines;
30 DWORD infoWidth;
31 WORD depth;
32 WORD infoBpp;
33 const BITMAPINFO *info;
34 WORD coloruse;
35 Drawable drawable;
36 GC gc;
37 int xSrc;
38 int ySrc;
39 int xDest;
40 int yDest;
41 int width;
42 int height;
43 } DIB_SETIMAGEBITS_DESCR;
46 /***********************************************************************
47 * DIB_Init
49 BOOL32 DIB_Init(void)
51 int i;
52 XImage* testimage;
54 for( i = 0; bitmapDepthTable[i]; i++ )
56 testimage = TSXCreateImage(display, DefaultVisualOfScreen(screen),
57 bitmapDepthTable[i], ZPixmap, 0, NULL, 1, 1, 32, 20 );
58 if( testimage ) ximageDepthTable[i] = testimage->bits_per_pixel;
59 else return FALSE;
60 TSXDestroyImage(testimage);
62 return TRUE;
65 /***********************************************************************
66 * DIB_GetXImageWidthBytes
68 * Return the width of an X image in bytes
70 int DIB_GetXImageWidthBytes( int width, int depth )
72 int i;
74 if (!ximageDepthTable[0]) {
75 DIB_Init();
77 for( i = 0; bitmapDepthTable[i] ; i++ )
78 if( bitmapDepthTable[i] == depth )
79 return (4 * ((width * ximageDepthTable[i] + 31)/32));
81 WARN(bitmap, "(%d): Unsupported depth\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 WARN(bitmap, "(%d): Unsupported depth\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 int *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 WARN(bitmap, "(%ld): wrong size for header\n", header->biSize );
166 return -1;
170 /***********************************************************************
171 * DIB_BuildColorMap
173 * Build the color map from the bitmap palette. Should not be called
174 * for a 24-bit deep bitmap.
176 static int *DIB_BuildColorMap( DC *dc, WORD coloruse, WORD depth,
177 const BITMAPINFO *info )
179 int i, colors;
180 BOOL32 isInfo;
181 WORD *colorPtr;
182 int *colorMapping;
184 if ((isInfo = (info->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))))
186 colors = info->bmiHeader.biClrUsed;
187 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
188 colorPtr = (WORD *)info->bmiColors;
190 else /* assume BITMAPCOREINFO */
192 colors = 1 << ((BITMAPCOREHEADER *)&info->bmiHeader)->bcBitCount;
193 colorPtr = (WORD *)((BITMAPCOREINFO *)info)->bmciColors;
195 if (!(colorMapping = (int *)HeapAlloc(GetProcessHeap(), 0,
196 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;
237 /***********************************************************************
238 * DIB_SetImageBits_1_Line
240 * Handles a single line of 1 bit data.
242 static void DIB_SetImageBits_1_Line(DWORD dstwidth, int left, int *colors,
243 XImage *bmpImage, int h, const BYTE *bits)
245 BYTE pix;
246 DWORD i, x;
248 dstwidth += left; bits += left >> 3;
250 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
251 for (i = dstwidth/8, x = left&~7; (i > 0); i--)
253 pix = *bits++;
254 XPutPixel( bmpImage, x++, h, colors[pix >> 7] );
255 XPutPixel( bmpImage, x++, h, colors[(pix >> 6) & 1] );
256 XPutPixel( bmpImage, x++, h, colors[(pix >> 5) & 1] );
257 XPutPixel( bmpImage, x++, h, colors[(pix >> 4) & 1] );
258 XPutPixel( bmpImage, x++, h, colors[(pix >> 3) & 1] );
259 XPutPixel( bmpImage, x++, h, colors[(pix >> 2) & 1] );
260 XPutPixel( bmpImage, x++, h, colors[(pix >> 1) & 1] );
261 XPutPixel( bmpImage, x++, h, colors[pix & 1] );
263 pix = *bits;
264 switch(dstwidth & 7)
266 case 7: XPutPixel( bmpImage, x++, h, colors[pix >> 7] ); pix <<= 1;
267 case 6: XPutPixel( bmpImage, x++, h, colors[pix >> 7] ); pix <<= 1;
268 case 5: XPutPixel( bmpImage, x++, h, colors[pix >> 7] ); pix <<= 1;
269 case 4: XPutPixel( bmpImage, x++, h, colors[pix >> 7] ); pix <<= 1;
270 case 3: XPutPixel( bmpImage, x++, h, colors[pix >> 7] ); pix <<= 1;
271 case 2: XPutPixel( bmpImage, x++, h, colors[pix >> 7] ); pix <<= 1;
272 case 1: XPutPixel( bmpImage, x++, h, colors[pix >> 7] );
276 /***********************************************************************
277 * DIB_SetImageBits_1
279 * SetDIBits for a 1-bit deep DIB.
281 static void DIB_SetImageBits_1( int lines, const BYTE *srcbits,
282 DWORD srcwidth, DWORD dstwidth, int left,
283 int *colors, XImage *bmpImage )
285 int h;
287 /* 32 bit aligned */
288 DWORD linebytes = ((srcwidth + 31) & ~31) / 8;
290 if (lines > 0) {
291 for (h = lines-1; h >=0; h--) {
292 DIB_SetImageBits_1_Line(dstwidth, left, colors, bmpImage, h, srcbits);
293 srcbits += linebytes;
295 } else {
296 lines = -lines;
297 for (h = 0; h < lines; h++) {
298 DIB_SetImageBits_1_Line(dstwidth, left, colors, bmpImage, h, srcbits);
299 srcbits += linebytes;
305 /***********************************************************************
306 * DIB_SetImageBits_4
308 * SetDIBits for a 4-bit deep DIB.
310 static void DIB_SetImageBits_4( int lines, const BYTE *srcbits,
311 DWORD srcwidth, DWORD dstwidth, int left,
312 int *colors, XImage *bmpImage )
314 DWORD i, x;
315 int h;
316 const BYTE *bits = srcbits + (left >> 1);
318 /* 32 bit aligned */
319 DWORD linebytes = ((srcwidth+7)&~7)/2;
321 dstwidth += left;
323 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
324 if (lines > 0) {
325 for (h = lines-1; h >= 0; h--) {
326 for (i = dstwidth/2, x = left&~1; i > 0; i--) {
327 BYTE pix = *bits++;
328 XPutPixel( bmpImage, x++, h, colors[pix >> 4] );
329 XPutPixel( bmpImage, x++, h, colors[pix & 0x0f] );
331 if (dstwidth & 1) XPutPixel( bmpImage, x, h, colors[*bits >> 4] );
332 srcbits += linebytes;
333 bits = srcbits + (left >> 1);
335 } else {
336 lines = -lines;
337 for (h = 0; h < lines; h++) {
338 for (i = dstwidth/2, x = left&~1; i > 0; i--) {
339 BYTE pix = *bits++;
340 XPutPixel( bmpImage, x++, h, colors[pix >> 4] );
341 XPutPixel( bmpImage, x++, h, colors[pix & 0x0f] );
343 if (dstwidth & 1) XPutPixel( bmpImage, x, h, colors[*bits >> 4] );
344 srcbits += linebytes;
345 bits = srcbits + (left >> 1);
350 #define check_xy(x,y) \
351 if (x > width) { \
352 x = 0; \
353 if (lines) \
354 lines--; \
357 /***********************************************************************
358 * DIB_SetImageBits_RLE4
360 * SetDIBits for a 4-bit deep compressed DIB.
362 static void DIB_SetImageBits_RLE4( int lines, const BYTE *bits, DWORD width,
363 DWORD dstwidth, int left, int *colors, XImage *bmpImage )
365 int x = 0, c, length;
366 const BYTE *begin = bits;
368 dstwidth += left; /* FIXME: avoid putting x<left pixels */
370 lines--;
371 while ((int)lines >= 0)
373 length = *bits++;
374 if (length) { /* encoded */
375 c = *bits++;
376 while (length--) {
377 XPutPixel(bmpImage, x++, lines, colors[c >> 4]);
378 check_xy(x, y);
379 if (length) {
380 length--;
381 XPutPixel(bmpImage, x++, lines, colors[c & 0xf]);
382 check_xy(x, y);
385 } else {
386 length = *bits++;
387 switch (length) {
388 case 0: /* eol */
389 x = 0;
390 lines--;
391 continue;
393 case 1: /* eopicture */
394 return;
396 case 2: /* delta */
397 x += *bits++;
398 lines -= *bits++;
399 continue;
401 default: /* absolute */
402 while (length--) {
403 c = *bits++;
404 XPutPixel(bmpImage, x++, lines, colors[c >> 4]);
405 check_xy(x, y);
406 if (length) {
407 length--;
408 XPutPixel(bmpImage, x++, lines, colors[c & 0xf]);
409 check_xy(x, y);
412 if ((bits - begin) & 1)
413 bits++;
419 /***********************************************************************
420 * DIB_SetImageBits_8
422 * SetDIBits for an 8-bit deep DIB.
424 static void DIB_SetImageBits_8( int lines, const BYTE *srcbits,
425 DWORD srcwidth, DWORD dstwidth, int left,
426 int *colors, XImage *bmpImage )
428 DWORD x;
429 int h;
430 const BYTE *bits = srcbits + left;
432 /* align to 32 bit */
433 DWORD linebytes = (srcwidth + 3) & ~3;
435 dstwidth+=left;
437 if (lines > 0) {
438 for (h = lines - 1; h >= 0; h--) {
439 for (x = left; x < dstwidth; x++, bits++) {
440 XPutPixel( bmpImage, x, h, colors[*bits] );
442 bits = (srcbits += linebytes) + left;
444 } else {
445 lines = -lines;
446 for (h = 0; h < lines; h++) {
447 for (x = left; x < dstwidth; x++, bits++) {
448 XPutPixel( bmpImage, x, h, colors[*bits] );
450 bits = (srcbits += linebytes) + left;
455 /***********************************************************************
456 * DIB_SetImageBits_RLE8
458 * SetDIBits for an 8-bit deep compressed DIB.
460 * This function rewritten 941113 by James Youngman. WINE blew out when I
461 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
463 * This was because the algorithm assumed that all RLE8 bitmaps end with the
464 * 'End of bitmap' escape code. This code is very much laxer in what it
465 * allows to end the expansion. Possibly too lax. See the note by
466 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
467 * bitmap should end with RleEnd, but on the other hand, software exists
468 * that produces ones that don't and Windows 3.1 doesn't complain a bit
469 * about it.
471 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
472 * James A. Youngman <mbcstjy@afs.man.ac.uk>
473 * [JAY]
476 enum Rle8_EscapeCodes
479 * Apologies for polluting your file's namespace...
481 RleEol = 0, /* End of line */
482 RleEnd = 1, /* End of bitmap */
483 RleDelta = 2 /* Delta */
486 static void DIB_SetImageBits_RLE8( int lines, const BYTE *bits, DWORD width,
487 DWORD dstwidth, int left, int *colors, XImage *bmpImage )
489 int x; /* X-positon on each line. Increases. */
490 int line; /* Line #. Starts at lines-1, decreases */
491 const BYTE *pIn = bits; /* Pointer to current position in bits */
492 BYTE length; /* The length pf a run */
493 BYTE color_index; /* index into colors[] as read from bits */
494 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
495 WORD color; /* value of colour[color_index] */
497 if (lines == 0) /* Let's hope this doesn't happen. */
498 return;
500 dstwidth += left; /* FIXME: avoid putting x<left pixels */
503 * Note that the bitmap data is stored by Windows starting at the
504 * bottom line of the bitmap and going upwards. Within each line,
505 * the data is stored left-to-right. That's the reason why line
506 * goes from lines-1 to 0. [JAY]
509 x = 0;
510 line = lines-1;
513 length = *pIn++;
516 * If the length byte is not zero (which is the escape value),
517 * We have a run of length pixels all the same colour. The colour
518 * index is stored next.
520 * If the length byte is zero, we need to read the next byte to
521 * know what to do. [JAY]
523 if (length != 0)
526 * [Run-Length] Encoded mode
528 color_index = (*pIn++); /* Get the colour index. */
529 color = colors[color_index];
531 while(length--)
532 XPutPixel(bmpImage, x++, line, color);
534 else
537 * Escape codes (may be an absolute sequence though)
539 escape_code = (*pIn++);
540 switch(escape_code)
542 case RleEol: /* =0, end of line */
544 x = 0;
545 line--;
546 break;
549 case RleEnd: /* =1, end of bitmap */
552 * Not all RLE8 bitmaps end with this
553 * code. For example, Paint Shop Pro
554 * produces some that don't. That's (I think)
555 * what caused the previous implementation to
556 * fail. [JAY]
558 line=-1; /* Cause exit from do loop. */
559 break;
562 case RleDelta: /* =2, a delta */
565 * Note that deltaing to line 0
566 * will cause an exit from the loop,
567 * which may not be what is intended.
568 * The fact that there is a delta in the bits
569 * almost certainly implies that there is data
570 * to follow. You may feel that we should
571 * jump to the top of the loop to avoid exiting
572 * in this case.
574 * TODO: Decide what to do here in that case. [JAY]
576 x += (*pIn++);
577 line -= (*pIn++);
578 if (line == 0)
580 TRACE(bitmap, "Delta to last line of bitmap "
581 "(wrongly?) causes loop exit\n");
583 break;
586 default: /* >2, switch to absolute mode */
589 * Absolute Mode
591 length = escape_code;
592 while(length--)
594 color_index = (*pIn++);
595 XPutPixel(bmpImage, x++, line,
596 colors[color_index]);
600 * If you think for a moment you'll realise that the
601 * only time we could ever possibly read an odd
602 * number of bytes is when there is a 0x00 (escape),
603 * a value >0x02 (absolute mode) and then an odd-
604 * length run. Therefore this is the only place we
605 * need to worry about it. Everywhere else the
606 * bytes are always read in pairs. [JAY]
608 if (escape_code & 1)
609 pIn++; /* Throw away the pad byte. */
610 break;
612 } /* switch (escape_code) : Escape sequence */
613 } /* process either an encoded sequence or an escape sequence */
615 /* We expect to come here more than once per line. */
616 } while (line >= 0); /* Do this until the bitmap is filled */
619 * Everybody comes here at the end.
620 * Check how we exited the loop and print a message if it's a bit odd.
621 * [JAY]
623 if ( (*(pIn-2) != 0/*escape*/) || (*(pIn-1)!= RleEnd) )
625 TRACE(bitmap, "End-of-bitmap "
626 "without (strictly) proper escape code. Last two "
627 "bytes were: %02X %02X.\n",
628 (int)*(pIn-2),
629 (int)*(pIn-1));
634 /***********************************************************************
635 * DIB_SetImageBits_16
637 * SetDIBits for a 16-bit deep DIB.
639 static void DIB_SetImageBits_16( int lines, const BYTE *srcbits,
640 DWORD srcwidth, DWORD dstwidth, int left,
641 DC *dc, XImage *bmpImage )
643 DWORD x;
644 LPWORD ptr;
645 WORD val;
646 int h;
647 BYTE r, g, b;
649 /* align to 32 bit */
650 DWORD linebytes = (srcwidth * 2 + 3) & ~3;
652 dstwidth += left;
654 ptr = (LPWORD) srcbits + left;
655 if (lines > 0) {
656 for (h = lines - 1; h >= 0; h--) {
657 for (x = left; x < dstwidth; x++, ptr++) {
658 val = *ptr;
659 r = (BYTE) ((val & 0x7c00) >> 7);
660 g = (BYTE) ((val & 0x03e0) >> 2);
661 b = (BYTE) ((val & 0x001f) << 3);
662 XPutPixel( bmpImage, x, h,
663 COLOR_ToPhysical(dc, RGB(r,g,b)) );
665 ptr = (LPWORD) (srcbits += linebytes) + left;
667 } else {
668 lines = -lines;
669 for (h = 0; h < lines; h++) {
670 for (x = left; x < dstwidth; x++, ptr++) {
671 val = *ptr;
672 r = (BYTE) ((val & 0x7c00) >> 7);
673 g = (BYTE) ((val & 0x03e0) >> 2);
674 b = (BYTE) ((val & 0x001f) << 3);
675 XPutPixel( bmpImage, x, h,
676 COLOR_ToPhysical(dc, RGB(r,g,b)) );
678 ptr = (LPWORD) (srcbits += linebytes) + left;
684 /***********************************************************************
685 * DIB_SetImageBits_24
687 * SetDIBits for a 24-bit deep DIB.
689 static void DIB_SetImageBits_24( int lines, const BYTE *srcbits,
690 DWORD srcwidth, DWORD dstwidth, int left,
691 DC *dc, XImage *bmpImage )
693 DWORD x;
694 const BYTE *bits = srcbits + left * 3;
695 int h;
697 /* align to 32 bit */
698 DWORD linebytes = (srcwidth * 3 + 3) & ~3;
700 dstwidth += left;
702 /* "bits" order is reversed for some reason */
704 if (lines > 0) {
705 for (h = lines - 1; h >= 0; h--) {
706 for (x = left; x < dstwidth; x++, bits += 3) {
707 XPutPixel( bmpImage, x, h,
708 COLOR_ToPhysical(dc, RGB(bits[2],bits[1],bits[0])));
710 bits = (srcbits += linebytes) + left * 3;
712 } else {
713 lines = -lines;
714 for (h = 0; h < lines; h++) {
715 for (x = left; x < dstwidth; x++, bits += 3) {
716 XPutPixel( bmpImage, x, h,
717 COLOR_ToPhysical(dc, RGB(bits[2],bits[1],bits[0])));
719 bits = (srcbits += linebytes) + left * 3;
725 /***********************************************************************
726 * DIB_SetImageBits_32
728 * SetDIBits for a 32-bit deep DIB.
730 static void DIB_SetImageBits_32( int lines, const BYTE *srcbits,
731 DWORD srcwidth, DWORD dstwidth, int left,
732 DC *dc, XImage *bmpImage )
734 DWORD x;
735 const BYTE *bits = srcbits + left * 4;
736 int h;
738 DWORD linebytes = (srcwidth * 4);
740 dstwidth += left;
742 if (lines > 0) {
743 for (h = lines - 1; h >= 0; h--) {
744 for (x = left; x < dstwidth; x++, bits += 4) {
745 XPutPixel( bmpImage, x, h,
746 COLOR_ToPhysical(dc, RGB(bits[2],bits[1],bits[0])));
748 bits = (srcbits += linebytes) + left * 4;
750 } else {
751 lines = -lines;
752 for (h = 0; h < lines; h++) {
753 for (x = left; x < dstwidth; x++, bits += 4) {
754 XPutPixel( bmpImage, x, h,
755 COLOR_ToPhysical(dc, RGB(bits[2],bits[1],bits[0])));
757 bits = (srcbits += linebytes) + left * 4;
763 /***********************************************************************
764 * DIB_SetImageBits
766 * Transfer the bits to an X image.
767 * Helper function for SetDIBits() and SetDIBitsToDevice().
768 * The Xlib critical section must be entered before calling this function.
770 static int DIB_SetImageBits( const DIB_SETIMAGEBITS_DESCR *descr )
772 int *colorMapping;
773 XImage *bmpImage;
774 DWORD compression = 0;
775 int lines;
777 if (descr->info->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
778 compression = descr->info->bmiHeader.biCompression;
780 /* Build the color mapping table */
782 if (descr->infoBpp > 8) colorMapping = NULL;
783 else if (!(colorMapping = DIB_BuildColorMap( descr->dc, descr->coloruse,
784 descr->depth, descr->info )))
785 return 0;
787 if( descr->dc->w.flags & DC_DIRTY ) CLIPPING_UpdateGCRegion(descr->dc);
789 /* Transfer the pixels */
790 lines = descr->lines;
791 if (lines < 0) lines = -lines;
792 XCREATEIMAGE(bmpImage, descr->infoWidth, lines, descr->depth );
794 switch(descr->infoBpp)
796 case 1:
797 DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
798 descr->width, descr->xSrc, colorMapping, bmpImage );
799 break;
800 case 4:
801 if (compression) DIB_SetImageBits_RLE4( descr->lines, descr->bits,
802 descr->infoWidth, descr->width, descr->xSrc,
803 colorMapping, bmpImage );
804 else DIB_SetImageBits_4( descr->lines, descr->bits, descr->infoWidth,
805 descr->width, descr->xSrc, colorMapping, bmpImage );
806 break;
807 case 8:
808 if (compression) DIB_SetImageBits_RLE8( descr->lines, descr->bits,
809 descr->infoWidth, descr->width, descr->xSrc,
810 colorMapping, bmpImage );
811 else DIB_SetImageBits_8( descr->lines, descr->bits, descr->infoWidth,
812 descr->width, descr->xSrc, colorMapping, bmpImage );
813 break;
814 case 15:
815 case 16:
816 DIB_SetImageBits_16( descr->lines, descr->bits, descr->infoWidth,
817 descr->width, descr->xSrc, descr->dc, bmpImage);
818 break;
819 case 24:
820 DIB_SetImageBits_24( descr->lines, descr->bits, descr->infoWidth,
821 descr->width, descr->xSrc, descr->dc, bmpImage );
822 break;
823 case 32:
824 DIB_SetImageBits_32( descr->lines, descr->bits, descr->infoWidth,
825 descr->width, descr->xSrc, descr->dc, bmpImage);
826 break;
827 default:
828 WARN(bitmap, "(%d): Invalid depth\n", descr->infoBpp );
829 break;
831 if (colorMapping) HeapFree( GetProcessHeap(), 0, colorMapping );
832 XPutImage( display, descr->drawable, descr->gc, bmpImage,
833 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
834 descr->width, descr->height );
835 XDestroyImage( bmpImage );
836 return lines;
840 /***********************************************************************
841 * StretchDIBits16 (GDI.439)
843 INT16 WINAPI StretchDIBits16(HDC16 hdc, INT16 xDst, INT16 yDst, INT16 widthDst,
844 INT16 heightDst, INT16 xSrc, INT16 ySrc, INT16 widthSrc,
845 INT16 heightSrc, const VOID *bits,
846 const BITMAPINFO *info, UINT16 wUsage, DWORD dwRop )
848 return (INT16)StretchDIBits32( hdc, xDst, yDst, widthDst, heightDst,
849 xSrc, ySrc, widthSrc, heightSrc, bits,
850 info, wUsage, dwRop );
854 /***********************************************************************
855 * StretchDIBits32 (GDI32.351)
857 INT32 WINAPI StretchDIBits32(HDC32 hdc, INT32 xDst, INT32 yDst, INT32 widthDst,
858 INT32 heightDst, INT32 xSrc, INT32 ySrc, INT32 widthSrc,
859 INT32 heightSrc, const void *bits,
860 const BITMAPINFO *info, UINT32 wUsage, DWORD dwRop )
862 HBITMAP32 hBitmap, hOldBitmap;
863 HDC32 hdcMem;
865 hBitmap = CreateDIBitmap32( hdc, &info->bmiHeader, CBM_INIT,
866 bits, info, wUsage );
867 hdcMem = CreateCompatibleDC32( hdc );
868 hOldBitmap = SelectObject32( hdcMem, hBitmap );
869 StretchBlt32( hdc, xDst, yDst, widthDst, heightDst,
870 hdcMem, xSrc, ySrc, widthSrc, heightSrc, dwRop );
871 SelectObject32( hdcMem, hOldBitmap );
872 DeleteDC32( hdcMem );
873 DeleteObject32( hBitmap );
874 return heightSrc;
878 /***********************************************************************
879 * SetDIBits16 (GDI.440)
881 INT16 WINAPI SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
882 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
883 UINT16 coloruse )
885 return SetDIBits32( hdc, hbitmap, startscan, lines, bits, info, coloruse );
889 /******************************************************************************
890 * SetDIBits32 [GDI32.312] Sets pixels in a bitmap using colors from DIB
892 * PARAMS
893 * hdc [I] Handle to device context
894 * hbitmap [I] Handle to bitmap
895 * startscan [I] Starting scan line
896 * lines [I] Number of scan lines
897 * bits [I] Array of bitmap bits
898 * info [I] Address of structure with data
899 * coloruse [I] Type of color indexes to use
901 * RETURNS
902 * Success: Number of scan lines copied
903 * Failure: 0
905 INT32 WINAPI SetDIBits32( HDC32 hdc, HBITMAP32 hbitmap, UINT32 startscan,
906 UINT32 lines, LPCVOID bits, const BITMAPINFO *info,
907 UINT32 coloruse )
909 DIB_SETIMAGEBITS_DESCR descr;
910 BITMAPOBJ * bmp;
911 int height, tmpheight;
912 INT32 result;
914 /* Check parameters */
916 descr.dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
917 if (!descr.dc)
919 descr.dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
920 if (!descr.dc) return 0;
922 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
924 GDI_HEAP_UNLOCK( hdc );
925 return 0;
927 if (DIB_GetBitmapInfo( &info->bmiHeader, &descr.infoWidth, &height,
928 &descr.infoBpp ) == -1)
930 GDI_HEAP_UNLOCK( hbitmap );
931 GDI_HEAP_UNLOCK( hdc );
932 return 0;
934 tmpheight = height;
935 if (height < 0) height = -height;
936 if (!lines || (startscan >= height))
938 GDI_HEAP_UNLOCK( hbitmap );
939 GDI_HEAP_UNLOCK( hdc );
940 return 0;
942 if (startscan + lines > height) lines = height - startscan;
944 descr.bits = bits;
945 descr.lines = tmpheight >= 0 ? lines : -lines;
946 descr.depth = bmp->bitmap.bmBitsPixel;
947 descr.info = info;
948 descr.coloruse = coloruse;
949 descr.drawable = bmp->pixmap;
950 descr.gc = BITMAP_GC(bmp);
951 descr.xSrc = 0;
952 descr.ySrc = 0;
953 descr.xDest = 0;
954 descr.yDest = height - startscan - lines;
955 descr.width = bmp->bitmap.bmWidth;
956 descr.height = lines;
958 EnterCriticalSection( &X11DRV_CritSection );
959 result = CALL_LARGE_STACK( DIB_SetImageBits, &descr );
960 LeaveCriticalSection( &X11DRV_CritSection );
962 GDI_HEAP_UNLOCK( hdc );
963 GDI_HEAP_UNLOCK( hbitmap );
964 return result;
968 /***********************************************************************
969 * SetDIBitsToDevice16 (GDI.443)
971 INT16 WINAPI SetDIBitsToDevice16(HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
972 INT16 cy, INT16 xSrc, INT16 ySrc, UINT16 startscan,
973 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
974 UINT16 coloruse )
976 return SetDIBitsToDevice32( hdc, xDest, yDest, cx, cy, xSrc, ySrc,
977 startscan, lines, bits, info, coloruse );
981 /***********************************************************************
982 * SetDIBitsToDevice32 (GDI32.313)
984 INT32 WINAPI SetDIBitsToDevice32(HDC32 hdc, INT32 xDest, INT32 yDest, DWORD cx,
985 DWORD cy, INT32 xSrc, INT32 ySrc, UINT32 startscan,
986 UINT32 lines, LPCVOID bits, const BITMAPINFO *info,
987 UINT32 coloruse )
989 DIB_SETIMAGEBITS_DESCR descr;
990 DC * dc;
991 DWORD width, oldcy = cy;
992 INT32 result;
993 int height, tmpheight;
995 /* Check parameters */
997 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
998 if (!dc)
1000 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
1001 if (!dc) return 0;
1003 if (DIB_GetBitmapInfo( &info->bmiHeader, &width,
1004 &height, &descr.infoBpp ) == -1)
1005 return 0;
1006 tmpheight = height;
1007 if (height < 0) height = -height;
1008 if (!lines || (startscan >= height)) return 0;
1009 if (startscan + lines > height) lines = height - startscan;
1010 if (ySrc < startscan) ySrc = startscan;
1011 else if (ySrc >= startscan + lines) return 0;
1012 if (xSrc >= width) return 0;
1013 if (ySrc + cy >= startscan + lines) cy = startscan + lines - ySrc;
1014 if (xSrc + cx >= width) cx = width - xSrc;
1015 if (!cx || !cy) return 0;
1017 DC_SetupGCForText( dc ); /* To have the correct colors */
1018 TSXSetFunction( display, dc->u.x.gc, DC_XROPfunction[dc->w.ROPmode-1] );
1020 descr.dc = dc;
1021 descr.bits = bits;
1022 descr.lines = tmpheight >= 0 ? lines : -lines;
1023 descr.infoWidth = width;
1024 descr.depth = dc->w.bitsPerPixel;
1025 descr.info = info;
1026 descr.coloruse = coloruse;
1027 descr.drawable = dc->u.x.drawable;
1028 descr.gc = dc->u.x.gc;
1029 descr.xSrc = xSrc;
1030 descr.ySrc = tmpheight >= 0 ? lines-(ySrc-startscan)-cy+(oldcy-cy) : ySrc - startscan;
1031 descr.xDest = dc->w.DCOrgX + XLPTODP( dc, xDest );
1032 descr.yDest = dc->w.DCOrgY + YLPTODP( dc, yDest ) + (tmpheight >= 0 ? oldcy-cy : 0);
1033 descr.width = cx;
1034 descr.height = cy;
1036 EnterCriticalSection( &X11DRV_CritSection );
1037 result = CALL_LARGE_STACK( DIB_SetImageBits, &descr );
1038 LeaveCriticalSection( &X11DRV_CritSection );
1039 return result;
1042 /***********************************************************************
1043 * SetDIBColorTable32 (GDI32.311)
1045 UINT32 WINAPI SetDIBColorTable32( HDC32 hdc, UINT32 startpos, UINT32 entries,
1046 RGBQUAD *colors )
1048 DC * dc;
1049 PALETTEENTRY * palEntry;
1050 PALETTEOBJ * palette;
1051 RGBQUAD *end;
1053 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1054 if (!dc)
1056 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
1057 if (!dc) return 0;
1060 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
1062 return 0;
1065 /* Transfer color info */
1067 if (dc->w.bitsPerPixel <= 8) {
1068 palEntry = palette->logpalette.palPalEntry + startpos;
1069 if (startpos + entries > (1 << dc->w.bitsPerPixel)) {
1070 entries = (1 << dc->w.bitsPerPixel) - startpos;
1072 for (end = colors + entries; colors < end; palEntry++, colors++)
1074 palEntry->peRed = colors->rgbRed;
1075 palEntry->peGreen = colors->rgbGreen;
1076 palEntry->peBlue = colors->rgbBlue;
1078 } else {
1079 entries = 0;
1081 GDI_HEAP_UNLOCK( dc->w.hPalette );
1082 return entries;
1085 /***********************************************************************
1086 * GetDIBColorTable32 (GDI32.169)
1088 UINT32 WINAPI GetDIBColorTable32( HDC32 hdc, UINT32 startpos, UINT32 entries,
1089 RGBQUAD *colors )
1091 DC * dc;
1092 PALETTEENTRY * palEntry;
1093 PALETTEOBJ * palette;
1094 RGBQUAD *end;
1096 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1097 if (!dc)
1099 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
1100 if (!dc) return 0;
1103 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
1105 return 0;
1108 /* Transfer color info */
1110 if (dc->w.bitsPerPixel <= 8) {
1111 palEntry = palette->logpalette.palPalEntry + startpos;
1112 if (startpos + entries > (1 << dc->w.bitsPerPixel)) {
1113 entries = (1 << dc->w.bitsPerPixel) - startpos;
1115 for (end = colors + entries; colors < end; palEntry++, colors++)
1117 colors->rgbRed = palEntry->peRed;
1118 colors->rgbGreen = palEntry->peGreen;
1119 colors->rgbBlue = palEntry->peBlue;
1120 colors->rgbReserved = 0;
1122 } else {
1123 entries = 0;
1125 GDI_HEAP_UNLOCK( dc->w.hPalette );
1126 return entries;
1129 /***********************************************************************
1130 * GetDIBits16 (GDI.441)
1132 INT16 WINAPI GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
1133 UINT16 lines, LPSTR bits, BITMAPINFO * info,
1134 UINT16 coloruse )
1136 return GetDIBits32( hdc, hbitmap, startscan, lines, bits, info, coloruse );
1140 /******************************************************************************
1141 * GetDIBits32 [GDI32.170] Retrieves bits of bitmap and copies to buffer
1143 * RETURNS
1144 * Success: Number of scan lines copied from bitmap
1145 * Failure: 0
1147 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
1149 INT32 WINAPI GetDIBits32(
1150 HDC32 hdc, /* [in] Handle to device context */
1151 HBITMAP32 hbitmap, /* [in] Handle to bitmap */
1152 UINT32 startscan, /* [in] First scan line to set in dest bitmap */
1153 UINT32 lines, /* [in] Number of scan lines to copy */
1154 LPSTR bits, /* [out] Address of array for bitmap bits */
1155 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
1156 UINT32 coloruse) /* [in] RGB or palette index */
1158 DC * dc;
1159 BITMAPOBJ * bmp;
1160 PALETTEENTRY * palEntry;
1161 PALETTEOBJ * palette;
1162 XImage * bmpImage;
1163 int i, x, y;
1165 if (!lines) return 0;
1166 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
1167 if (!dc)
1169 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
1170 if (!dc) return 0;
1172 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
1173 return 0;
1174 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
1176 GDI_HEAP_UNLOCK( hbitmap );
1177 return 0;
1180 /* Transfer color info */
1182 if (info->bmiHeader.biBitCount<=8) {
1183 palEntry = palette->logpalette.palPalEntry;
1184 for (i = 0; i < info->bmiHeader.biClrUsed; i++, palEntry++)
1186 if (coloruse == DIB_RGB_COLORS)
1188 info->bmiColors[i].rgbRed = palEntry->peRed;
1189 info->bmiColors[i].rgbGreen = palEntry->peGreen;
1190 info->bmiColors[i].rgbBlue = palEntry->peBlue;
1191 info->bmiColors[i].rgbReserved = 0;
1193 else ((WORD *)info->bmiColors)[i] = (WORD)i;
1197 if (bits)
1199 BYTE* bbits = bits;
1200 int pad, yend, xend = bmp->bitmap.bmWidth;
1202 TRACE(bitmap, "%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
1203 lines, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
1204 (int)info->bmiHeader.biWidth, (int)info->bmiHeader.biHeight, startscan );
1206 /* adjust number of scanlines to copy */
1208 if( lines > info->bmiHeader.biHeight ) lines = info->bmiHeader.biHeight;
1209 yend = startscan + lines;
1210 if( startscan >= bmp->bitmap.bmHeight )
1212 GDI_HEAP_UNLOCK( hbitmap );
1213 GDI_HEAP_UNLOCK( dc->w.hPalette );
1214 return FALSE;
1216 if( yend > bmp->bitmap.bmHeight ) yend = bmp->bitmap.bmHeight;
1218 /* adjust scanline width */
1220 pad = info->bmiHeader.biWidth - bmp->bitmap.bmWidth;
1221 if( pad < 0 )
1223 /* bitmap is wider than DIB, copy only a part */
1225 pad = 0;
1226 xend = info->bmiHeader.biWidth;
1229 EnterCriticalSection( &X11DRV_CritSection );
1230 bmpImage = (XImage *)CALL_LARGE_STACK( BITMAP_GetXImage, bmp );
1232 switch( info->bmiHeader.biBitCount )
1234 case 8:
1235 /* pad up to 32 bit */
1236 pad += (4 - (info->bmiHeader.biWidth & 3)) & 3;
1237 for( y = yend - 1; (int)y >= (int)startscan; y-- )
1239 for( x = 0; x < xend; x++ )
1240 *bbits++ = XGetPixel( bmpImage, x, y );
1241 bbits += pad;
1243 break;
1244 case 1:
1245 pad += ((32 - (info->bmiHeader.biWidth & 31)) / 8) & 3;
1246 for( y = yend - 1; (int)y >= (int)startscan; y-- )
1248 *bbits = 0;
1249 for( x = 0; x < xend; x++ ) {
1251 *bbits |= XGetPixel( bmpImage, x, y)<<(7-(x&7));
1252 if ((x&7)==7) {
1253 bbits++;
1254 *bbits=0;
1257 bbits += pad;
1259 break;
1260 case 4:
1261 pad += ((8 - (info->bmiHeader.biWidth & 7)) / 2) & 3;
1262 for( y = yend - 1; (int)y >= (int)startscan; y-- )
1264 *bbits = 0;
1265 for( x = 0; x < xend; x++ ) {
1267 *bbits |= XGetPixel( bmpImage, x, y)<<(4*(1-(x&1)));
1268 if ((x&1)==1) {
1269 bbits++;
1270 *bbits=0;
1273 bbits += pad;
1275 break;
1276 case 15:
1277 case 16:
1278 pad += (4 - ((info->bmiHeader.biWidth*2) & 3)) & 3;
1279 for( y = yend - 1; (int)y >= (int)startscan; y-- )
1281 *bbits = 0;
1282 for( x = 0; x < xend; x++ ) {
1283 unsigned long pixel=XGetPixel( bmpImage, x, y);
1284 *bbits++ = pixel & 0xff;
1285 *bbits++ = (pixel >> 8) & 0xff;
1287 bbits += pad;
1289 break;
1290 case 24:
1291 pad += (4 - ((info->bmiHeader.biWidth*3) & 3)) & 3;
1292 for( y = yend - 1; (int)y >= (int)startscan; y-- )
1294 *bbits = 0;
1295 for( x = 0; x < xend; x++ ) {
1296 unsigned long pixel=XGetPixel( bmpImage, x, y);
1297 *bbits++ = (pixel >>16) & 0xff;
1298 *bbits++ = (pixel >> 8) & 0xff;
1299 *bbits++ = pixel & 0xff;
1301 bbits += pad;
1303 break;
1304 case 32:
1305 for( y = yend - 1; (int)y >= (int)startscan; y-- )
1307 *bbits = 0;
1308 for( x = 0; x < xend; x++ ) {
1309 unsigned long pixel=XGetPixel( bmpImage, x, y);
1310 *bbits++ = (pixel >>16) & 0xff;
1311 *bbits++ = (pixel >> 8) & 0xff;
1312 *bbits++ = pixel & 0xff;
1315 break;
1316 default:
1317 WARN(bitmap,"Unsupported depth %d\n",
1318 info->bmiHeader.biBitCount);
1319 break;
1322 XDestroyImage( bmpImage );
1323 LeaveCriticalSection( &X11DRV_CritSection );
1325 info->bmiHeader.biCompression = 0;
1327 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
1329 /* fill in struct members */
1331 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
1332 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
1333 info->bmiHeader.biPlanes = 1;
1334 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
1335 info->bmiHeader.biSizeImage = bmp->bitmap.bmHeight *
1336 DIB_GetDIBWidthBytes( bmp->bitmap.bmWidth,
1337 bmp->bitmap.bmBitsPixel );
1338 info->bmiHeader.biCompression = 0;
1341 GDI_HEAP_UNLOCK( hbitmap );
1342 GDI_HEAP_UNLOCK( dc->w.hPalette );
1343 return lines;
1347 /***********************************************************************
1348 * CreateDIBitmap16 (GDI.442)
1350 HBITMAP16 WINAPI CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
1351 DWORD init, LPCVOID bits, const BITMAPINFO * data,
1352 UINT16 coloruse )
1354 return CreateDIBitmap32( hdc, header, init, bits, data, coloruse );
1358 /***********************************************************************
1359 * CreateDIBitmap32 (GDI32.37)
1361 HBITMAP32 WINAPI CreateDIBitmap32( HDC32 hdc, const BITMAPINFOHEADER *header,
1362 DWORD init, LPCVOID bits, const BITMAPINFO *data,
1363 UINT32 coloruse )
1365 HBITMAP32 handle;
1366 BOOL32 fColor;
1367 DWORD width;
1368 int height;
1369 WORD bpp;
1371 if (DIB_GetBitmapInfo( header, &width, &height, &bpp ) == -1) return 0;
1372 if (height < 0) height = -height;
1374 /* Check if we should create a monochrome or color bitmap. */
1375 /* We create a monochrome bitmap only if it has exactly 2 */
1376 /* colors, which are either black or white, nothing else. */
1377 /* In all other cases, we create a color bitmap. */
1379 if (bpp != 1) fColor = TRUE;
1380 else if ((coloruse != DIB_RGB_COLORS) ||
1381 (init != CBM_INIT) || !data) fColor = FALSE;
1382 else
1384 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
1386 RGBQUAD *rgb = data->bmiColors;
1387 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
1388 if ((col == RGB(0,0,0)) || (col == RGB(0xff,0xff,0xff)))
1390 rgb++;
1391 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
1392 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
1394 else fColor = TRUE;
1396 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
1398 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
1399 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
1400 if ((col == RGB(0,0,0)) || (col == RGB(0xff,0xff,0xff)))
1402 rgb++;
1403 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
1404 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
1406 else fColor = TRUE;
1408 else
1410 WARN(bitmap, "(%ld): wrong size for data\n",
1411 data->bmiHeader.biSize );
1412 return 0;
1416 /* Now create the bitmap */
1418 handle = fColor ? CreateCompatibleBitmap32( hdc, width, height ) :
1419 CreateBitmap32( width, height, 1, 1, NULL );
1420 if (!handle) return 0;
1422 if (init == CBM_INIT)
1423 SetDIBits32( hdc, handle, 0, height, bits, data, coloruse );
1424 return handle;
1427 /***********************************************************************
1428 * CreateDIBSection16 (GDI.489)
1430 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
1431 LPVOID **bits, HANDLE16 section,
1432 DWORD offset)
1434 return CreateDIBSection32 (hdc, bmi, usage, bits, section, offset);
1437 /***********************************************************************
1438 * CreateDIBSection32 (GDI32.36)
1440 HBITMAP32 WINAPI CreateDIBSection32 (HDC32 hdc, BITMAPINFO *bmi, UINT32 usage,
1441 LPVOID **bits,HANDLE32 section,
1442 DWORD offset)
1444 HBITMAP32 res = 0;
1446 FIXME(bitmap,
1447 "(%d,[w=%ld,h=%ld],%d,%p,0x%08x,%ld),semistub\n",
1448 hdc,bmi->bmiHeader.biWidth,bmi->bmiHeader.biHeight,
1449 usage,bits,section,offset);
1451 if (bmi->bmiHeader.biHeight < 0 ) bmi->bmiHeader.biHeight = -bmi->bmiHeader.biHeight;
1452 if (bmi->bmiHeader.biWidth < 0 ) bmi->bmiHeader.biWidth = -bmi->bmiHeader.biWidth;
1453 /* FIXME. The following line isn't quite right. */
1454 res = CreateDIBitmap32 (hdc, &bmi->bmiHeader, 0, NULL, bmi, 0);
1455 if (res)
1457 BITMAP32 bmp;
1458 if (GetObject32A (res, sizeof (bmp), &bmp))
1460 /* FIXME: this is wrong! (bmBits is always NULL) */
1461 if (bits) *bits = bmp.bmBits;
1462 /* hmpf */
1463 TRACE(bitmap,"allocating %ld bytes of memory\n",
1464 bmi->bmiHeader.biWidth*bmi->bmiHeader.biHeight*4);
1465 if (bits) *bits = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
1466 bmi->bmiHeader.biWidth*bmi->bmiHeader.biHeight*4);
1467 return res;
1471 /* Error. */
1472 if (res) DeleteObject32 (res);
1473 if (bits) *bits = NULL;
1474 return 0;