Release 941122
[wine/multimedia.git] / objects / dib.c
blobafb79be56b75b59f41ef08f5af37c8f52a8a0d20
1 /*
2 * GDI device independent bitmaps
4 * Copyright 1993 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <X11/Xlib.h>
12 #include <X11/Xutil.h>
13 #include "gdi.h"
14 #include "bitmap.h"
15 #include "icon.h"
16 #include "stddebug.h"
17 /* #define DEBUG_ICON /* */
18 /* #undef DEBUG_ICON /* */
19 /* #define DEBUG_BITMAP /* */
20 /* #undef DEBUG_BIYMAP /* */
21 #include "debug.h"
23 extern const int DC_XROPfunction[];
25 extern WORD COLOR_ToPhysical( DC *dc, COLORREF color ); /* color.c */
27 /***********************************************************************
28 * DIB_BitmapInfoSize
30 * Return the size of the bitmap info structure.
32 int DIB_BitmapInfoSize( BITMAPINFO * info, WORD coloruse )
34 int size = info->bmiHeader.biClrUsed;
35 if (!size && (info->bmiHeader.biBitCount != 24))
36 size = 1 << info->bmiHeader.biBitCount;
37 if (coloruse == DIB_RGB_COLORS)
38 size = info->bmiHeader.biSize + size * sizeof(RGBQUAD);
39 else
40 size = info->bmiHeader.biSize + size * sizeof(WORD);
41 return size;
45 /***********************************************************************
46 * DIB_DIBmpToImage
48 * Create an XImage pointing to the bitmap data.
50 static XImage *DIB_DIBmpToImage( BITMAPINFOHEADER * bmp, void * bmpData )
52 extern void _XInitImageFuncPtrs( XImage* );
53 XImage * image;
54 int bytesPerLine = ((bmp->biBitCount == 24 ? 32 : bmp->biBitCount)
55 * bmp->biWidth + 31) / 32 * 4;
57 image = XCreateImage( display, DefaultVisualOfScreen( screen ),
58 bmp->biBitCount, ZPixmap, 0, bmpData,
59 bmp->biWidth, bmp->biHeight, 32, bytesPerLine );
60 if (!image) return 0;
61 image->byte_order = MSBFirst;
62 image->bitmap_bit_order = MSBFirst;
63 image->bitmap_unit = 16;
64 _XInitImageFuncPtrs(image);
65 return image;
69 /***********************************************************************
70 * DIB_SetImageBits_1
72 * SetDIBits for a 1-bit deep DIB.
74 static void DIB_SetImageBits_1( WORD lines, BYTE *bits, WORD width,
75 WORD *colors, XImage *bmpImage )
77 WORD i, x;
78 BYTE pad, pix;
80 if (!(width & 31)) pad = 0;
81 else pad = ((32 - (width & 31)) + 7) / 8;
83 while (lines--)
85 for (i = width/8, x = 0; (i > 0); i--)
87 pix = *bits++;
88 XPutPixel( bmpImage, x++, lines, colors[pix >> 7] );
89 XPutPixel( bmpImage, x++, lines, colors[(pix >> 6) & 1] );
90 XPutPixel( bmpImage, x++, lines, colors[(pix >> 5) & 1] );
91 XPutPixel( bmpImage, x++, lines, colors[(pix >> 4) & 1] );
92 XPutPixel( bmpImage, x++, lines, colors[(pix >> 3) & 1] );
93 XPutPixel( bmpImage, x++, lines, colors[(pix >> 2) & 1] );
94 XPutPixel( bmpImage, x++, lines, colors[(pix >> 1) & 1] );
95 XPutPixel( bmpImage, x++, lines, colors[pix & 1] );
97 pix = *bits;
98 switch(width & 7)
100 case 7: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
101 case 6: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
102 case 5: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
103 case 4: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
104 case 3: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
105 case 2: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
106 case 1: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] );
108 bits += pad;
113 /***********************************************************************
114 * DIB_SetImageBits_4
116 * SetDIBits for a 4-bit deep DIB.
118 static void DIB_SetImageBits_4( WORD lines, BYTE *bits, WORD width,
119 WORD *colors, XImage *bmpImage )
121 WORD i, x;
122 BYTE pad;
124 if (!(width & 7)) pad = 0;
125 else pad = ((8 - (width & 7)) + 1) / 2;
127 while (lines--)
129 for (i = width/2, x = 0; i > 0; i--)
131 BYTE pix = *bits++;
132 XPutPixel( bmpImage, x++, lines, colors[pix >> 4] );
133 XPutPixel( bmpImage, x++, lines, colors[pix & 0x0f] );
135 if (width & 1) XPutPixel( bmpImage, x, lines, colors[*bits >> 4] );
136 bits += pad;
140 #define check_xy(x,y) \
141 if (x > width) { \
142 x = 0; \
143 if (lines) \
144 lines--; \
147 /***********************************************************************
148 * DIB_SetImageBits_RLE4
150 * SetDIBits for a 4-bit deep compressed DIB.
152 static void DIB_SetImageBits_RLE4( WORD lines, BYTE *bits, WORD width,
153 WORD *colors, XImage *bmpImage )
155 int x = 0, c, length;
156 BYTE *begin = bits;
158 lines--;
159 while (1) {
160 length = *bits++;
161 if (length) { /* encoded */
162 c = *bits++;
163 while (length--) {
164 XPutPixel(bmpImage, x++, lines, colors[c >> 4]);
165 check_xy(x, y);
166 if (length) {
167 length--;
168 XPutPixel(bmpImage, x++, lines, colors[c & 0xf]);
169 check_xy(x, y);
172 } else {
173 length = *bits++;
174 switch (length) {
175 case 0: /* eol */
176 x = 0;
177 lines--;
178 continue;
180 case 1: /* eopicture */
181 return;
183 case 2: /* delta */
184 x += *bits++;
185 lines -= *bits++;
186 continue;
188 default: /* absolute */
189 while (length--) {
190 c = *bits++;
191 XPutPixel(bmpImage, x++, lines, colors[c >> 4]);
192 check_xy(x, y);
193 if (length) {
194 length--;
195 XPutPixel(bmpImage, x++, lines, colors[c & 0xf]);
196 check_xy(x, y);
199 if ((bits - begin) & 1)
200 bits++;
206 /***********************************************************************
207 * DIB_SetImageBits_8
209 * SetDIBits for an 8-bit deep DIB.
211 static void DIB_SetImageBits_8( WORD lines, BYTE *bits, WORD width,
212 WORD *colors, XImage *bmpImage )
214 WORD x;
215 BYTE pad = (4 - (width & 3)) & 3;
217 while (lines--)
219 for (x = 0; x < width; x++)
220 XPutPixel( bmpImage, x, lines, colors[*bits++] );
221 bits += pad;
225 /***********************************************************************
226 * DIB_SetImageBits_RLE8
228 * SetDIBits for an 8-bit deep compressed DIB.
230 * This function rewritten 941113 by James Youngman. WINE blew out when I
231 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
233 * This was because the algorithm assumed that all RLE8 bitmaps end with the
234 * 'End of bitmap' escape code. This code is very much laxer in what it
235 * allows to end the expansion. Possibly too lax. See the note by
236 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
237 * bitmap should end with RleEnd, but on the other hand, software exists
238 * that produces ones that don't and Windows 3.1 doesn't complain a bit
239 * about it.
241 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
242 * James A. Youngman <mbcstjy@afs.man.ac.uk>
243 * [JAY]
246 enum Rle8_EscapeCodes
249 * Apologies for polluting your file's namespace...
251 RleEol = 0, /* End of line */
252 RleEnd = 1, /* End of bitmap */
253 RleDelta = 2 /* Delta */
256 static void DIB_SetImageBits_RLE8(WORD lines,
257 BYTE *bits,
258 WORD width,
259 WORD *colors,
260 XImage *bmpImage)
262 int x; /* X-positon on each line. Increases. */
263 int line; /* Line #. Starts at lines-1, decreases */
264 BYTE *pIn = bits; /* Pointer to current position in bits */
265 BYTE length; /* The length pf a run */
266 BYTE color_index; /* index into colors[] as read from bits */
267 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
268 WORD color; /* value of colour[color_index] */
270 if (lines == 0) /* Let's hope this doesn't happen. */
271 return;
274 * Note that the bitmap data is stored by Windows starting at the
275 * bottom line of the bitmap and going upwards. Within each line,
276 * the data is stored left-to-right. That's the reason why line
277 * goes from lines-1 to 0. [JAY]
280 x = 0;
281 line = lines-1;
284 length = *pIn++;
287 * If the length byte is not zero (which is the escape value),
288 * We have a run of length pixels all the same colour. The colour
289 * index is stored next.
291 * If the length byte is zero, we need to read the next byte to
292 * know what to do. [JAY]
294 if (length != 0)
297 * [Run-Length] Encoded mode
299 color_index = (*pIn++); /* Get the colour index. */
300 color = colors[color_index];
302 while(length--)
303 XPutPixel(bmpImage, x++, line, color);
305 else
308 * Escape codes (may be an absolute sequence though)
310 escape_code = (*pIn++);
311 switch(escape_code)
313 case RleEol: /* =0, end of line */
315 x = 0;
316 line--;
317 break;
320 case RleEnd: /* =1, end of bitmap */
323 * Not all RLE8 bitmaps end with this
324 * code. For example, Paint Shop Pro
325 * produces some that don't. That's (I think)
326 * what caused the previous implementation to
327 * fail. [JAY]
329 line=0; /* Cause exit from do loop. */
332 case RleDelta: /* =2, a delta */
335 * Note that deltaing to line 0
336 * will cause an exit from the loop,
337 * which may not be what is intended.
338 * The fact that there is a delta in the bits
339 * almost certainly implies that there is data
340 * to follow. You may feel that we should
341 * jump to the top of the loop to avoid exiting
342 * in this case.
344 * TODO: Decide what to do here in that case. [JAY]
346 x += (*pIn++);
347 line -= (*pIn++);
348 if (line == 0)
350 dprintf_bitmap(stddeb,
351 "DIB_SetImageBits_RLE8(): "
352 "Delta to last line of bitmap "
353 "(wrongly??) causes loop exit\n");
355 break;
358 default: /* >2, switch to absolute mode */
361 * Absolute Mode
363 length = escape_code;
364 while(length--)
366 color_index = (*pIn++);
367 XPutPixel(bmpImage, x++, line,
368 colors[color_index]);
372 * If you think for a moment you'll realise that the
373 * only time we could ever possibly read an odd
374 * number of bytes is when there is a 0x00 (escape),
375 * a value >0x02 (absolute mode) and then an odd-
376 * length run. Therefore this is the only place we
377 * need to worry about it. Everywhere else the
378 * bytes are always read in pairs. [JAY]
380 if (escape_code & 1)
381 (*pIn++); /* Get and throw away the pad byte. */
382 break;
384 } /* switch (escape_code) : Escape sequence */
385 } /* process either an encoded sequence or an escape sequence */
387 /* We expect to come here more than once per line. */
388 } while (line > 0); /* Do this until the bitmap is filled */
391 * Everybody comes here at the end.
392 * Check how we exited the loop and print a message if it's a bit odd.
393 * [JAY]
395 if ( (*(pIn-2) != 0/*escape*/) || (*(pIn-1)!= RleEnd) )
397 dprintf_bitmap(stddeb, "DIB_SetImageBits_RLE8(): End-of-bitmap "
398 "without (strictly) proper escape code. Last two "
399 "bytes were: %02X %02X.\n",
400 (int)*(pIn-2),
401 (int)*(pIn-1));
406 /***********************************************************************
407 * DIB_SetImageBits_24
409 * SetDIBits for a 24-bit deep DIB.
411 static void DIB_SetImageBits_24( WORD lines, BYTE *bits, WORD width,
412 DC *dc, XImage *bmpImage )
414 WORD x;
415 BYTE pad = (4 - ((width*3) & 3)) & 3;
417 while (lines--)
419 for (x = 0; x < width; x++, bits += 3)
421 XPutPixel( bmpImage, x, lines,
422 COLOR_ToPhysical( dc, RGB(bits[0],bits[1],bits[2]) ));
424 bits += pad;
429 /***********************************************************************
430 * DIB_SetImageBits
432 * Transfer the bits to an X image.
433 * Helper function for SetDIBits() and SetDIBitsToDevice().
435 static int DIB_SetImageBits( DC *dc, WORD lines, WORD depth, LPSTR bits,
436 BITMAPINFO *info, WORD coloruse,
437 Drawable drawable, GC gc, int xSrc, int ySrc,
438 int xDest, int yDest, int width, int height )
440 WORD *colorMapping;
441 XImage *bmpImage;
442 void *bmpData;
443 int i, colors, widthBytes;
445 /* Build the color mapping table */
447 if (info->bmiHeader.biBitCount == 24) colorMapping = NULL;
448 else
450 colors = info->bmiHeader.biClrUsed;
451 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
452 if (!(colorMapping = (WORD *)malloc( colors * sizeof(WORD) )))
453 return 0;
454 if (coloruse == DIB_RGB_COLORS)
456 RGBQUAD * rgbPtr = info->bmiColors;
457 for (i = 0; i < colors; i++, rgbPtr++)
458 colorMapping[i] = COLOR_ToPhysical( dc, RGB(rgbPtr->rgbRed,
459 rgbPtr->rgbGreen,
460 rgbPtr->rgbBlue) );
462 else
464 WORD * index = (WORD *)info->bmiColors;
465 for (i = 0; i < colors; i++, index++)
466 colorMapping[i] = COLOR_ToPhysical( dc, PALETTEINDEX(*index) );
470 /* Transfer the pixels */
472 widthBytes = ((depth == 24 ? 32 : depth) * info->bmiHeader.biWidth + 31)
473 / 32 * 4;
474 bmpData = malloc( lines * widthBytes );
475 bmpImage = XCreateImage( display, DefaultVisualOfScreen(screen),
476 depth, ZPixmap, 0, bmpData,
477 info->bmiHeader.biWidth, lines, 32, widthBytes );
479 switch(info->bmiHeader.biBitCount)
481 case 1:
482 DIB_SetImageBits_1( lines, bits, info->bmiHeader.biWidth,
483 colorMapping, bmpImage );
484 break;
485 case 4:
486 if (info->bmiHeader.biCompression)
487 DIB_SetImageBits_RLE4( lines, bits, info->bmiHeader.biWidth,
488 colorMapping, bmpImage );
489 else
490 DIB_SetImageBits_4( lines, bits, info->bmiHeader.biWidth,
491 colorMapping, bmpImage );
492 break;
493 case 8:
494 if (info->bmiHeader.biCompression)
495 DIB_SetImageBits_RLE8( lines, bits, info->bmiHeader.biWidth,
496 colorMapping, bmpImage );
497 else
498 DIB_SetImageBits_8( lines, bits, info->bmiHeader.biWidth,
499 colorMapping, bmpImage );
500 break;
501 case 24:
502 DIB_SetImageBits_24( lines, bits, info->bmiHeader.biWidth,
503 dc, bmpImage );
504 break;
506 if (colorMapping) free(colorMapping);
508 XPutImage( display, drawable, gc, bmpImage, xSrc, ySrc,
509 xDest, yDest, width, height );
510 XDestroyImage( bmpImage );
511 return lines;
515 /***********************************************************************
516 * SetDIBits (GDI.440)
518 int SetDIBits( HDC hdc, HBITMAP hbitmap, WORD startscan, WORD lines,
519 LPSTR bits, BITMAPINFO * info, WORD coloruse )
521 DC * dc;
522 BITMAPOBJ * bmp;
524 /* Check parameters */
526 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
527 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
528 return 0;
529 if (!lines || (startscan >= (WORD)info->bmiHeader.biHeight)) return 0;
530 if (startscan+lines > info->bmiHeader.biHeight)
531 lines = info->bmiHeader.biHeight - startscan;
533 return DIB_SetImageBits( dc, lines, bmp->bitmap.bmBitsPixel,
534 bits, info, coloruse, bmp->pixmap, BITMAP_GC(bmp),
535 0, 0, 0, startscan, bmp->bitmap.bmWidth, lines );
539 /***********************************************************************
540 * SetDIBitsToDevice (GDI.443)
542 int SetDIBitsToDevice( HDC hdc, short xDest, short yDest, WORD cx, WORD cy,
543 WORD xSrc, WORD ySrc, WORD startscan, WORD lines,
544 LPSTR bits, BITMAPINFO * info, WORD coloruse )
546 DC * dc;
548 /* Check parameters */
550 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
551 if (!lines || (startscan >= info->bmiHeader.biHeight)) return 0;
552 if (startscan+lines > info->bmiHeader.biHeight)
553 lines = info->bmiHeader.biHeight - startscan;
554 if (ySrc < startscan) ySrc = startscan;
555 else if (ySrc >= startscan+lines) return 0;
556 if (xSrc >= info->bmiHeader.biWidth) return 0;
557 if (ySrc+cy >= startscan+lines) cy = startscan + lines - ySrc;
558 if (xSrc+cx >= info->bmiHeader.biWidth) cx = info->bmiHeader.biWidth-xSrc;
559 if (!cx || !cy) return 0;
561 DC_SetupGCForText( dc ); /* To have the correct colors */
562 XSetFunction( display, dc->u.x.gc, DC_XROPfunction[dc->w.ROPmode-1] );
563 return DIB_SetImageBits( dc, lines, dc->w.bitsPerPixel,
564 bits, info, coloruse,
565 dc->u.x.drawable, dc->u.x.gc,
566 xSrc, ySrc - startscan,
567 dc->w.DCOrgX + XLPTODP( dc, xDest ),
568 dc->w.DCOrgY + YLPTODP( dc, yDest ),
569 cx, cy );
573 /***********************************************************************
574 * GetDIBits (GDI.441)
576 int GetDIBits( HDC hdc, HBITMAP hbitmap, WORD startscan, WORD lines,
577 LPSTR bits, BITMAPINFO * info, WORD coloruse )
579 DC * dc;
580 BITMAPOBJ * bmp;
581 PALETTEENTRY * palEntry;
582 PALETTEOBJ * palette;
583 XImage * bmpImage, * dibImage;
584 int i, x, y;
586 if (!lines) return 0;
587 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
588 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
589 return 0;
590 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
591 return 0;
593 /* Transfer color info */
595 palEntry = palette->logpalette.palPalEntry;
596 for (i = 0; i < info->bmiHeader.biClrUsed; i++, palEntry++)
598 if (coloruse == DIB_RGB_COLORS)
600 info->bmiColors[i].rgbRed = palEntry->peRed;
601 info->bmiColors[i].rgbGreen = palEntry->peGreen;
602 info->bmiColors[i].rgbBlue = palEntry->peBlue;
603 info->bmiColors[i].rgbReserved = 0;
605 else ((WORD *)info->bmiColors)[i] = (WORD)i;
608 /* Transfer the pixels (very slow...) */
610 if (bits)
612 bmpImage = XGetImage( display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
613 bmp->bitmap.bmHeight, AllPlanes, ZPixmap );
614 dibImage = DIB_DIBmpToImage( &info->bmiHeader, bits );
616 for (y = 0; y < lines; y++)
618 for (x = 0; x < info->bmiHeader.biWidth; x++)
620 XPutPixel( dibImage, x, y,
621 XGetPixel(bmpImage, x, bmp->bitmap.bmHeight-startscan-y-1) );
626 dibImage->data = NULL;
627 XDestroyImage( dibImage );
628 XDestroyImage( bmpImage );
630 return lines;
634 /***********************************************************************
635 * CreateDIBitmap (GDI.442)
637 HBITMAP CreateDIBitmap( HDC hdc, BITMAPINFOHEADER * header, DWORD init,
638 LPSTR bits, BITMAPINFO * data, WORD coloruse )
640 HBITMAP handle;
642 handle = CreateCompatibleBitmap( hdc, header->biWidth, header->biHeight );
643 if (!handle) return 0;
644 if (init == CBM_INIT) SetDIBits( hdc, handle, 0, header->biHeight,
645 bits, data, coloruse );
646 return handle;
649 /***********************************************************************
650 * DrawIcon (USER.84)
652 BOOL DrawIcon(HDC hDC, short x, short y, HICON hIcon)
654 ICONALLOC *lpico;
655 BITMAP bm;
656 HBITMAP hBitTemp;
657 HDC hMemDC;
658 HDC hMemDC2;
659 dprintf_icon(stddeb,"DrawIcon(%04X, %d, %d, %04X) \n", hDC, x, y, hIcon);
660 if (hIcon == (HICON)NULL) return FALSE;
661 lpico = (ICONALLOC *)GlobalLock(hIcon);
662 GetObject(lpico->hBitmap, sizeof(BITMAP), (LPSTR)&bm);
663 dprintf_icon(stddeb,"DrawIcon / x=%d y=%d\n", x, y);
664 dprintf_icon(stddeb,"DrawIcon / icon Width=%d\n",
665 (int)lpico->descriptor.Width);
666 dprintf_icon(stddeb,"DrawIcon / icon Height=%d\n",
667 (int)lpico->descriptor.Height);
668 dprintf_icon(stddeb,"DrawIcon / icon ColorCount=%d\n",
669 (int)lpico->descriptor.ColorCount);
670 dprintf_icon(stddeb,"DrawIcon / icon icoDIBSize=%lX\n",
671 (DWORD)lpico->descriptor.icoDIBSize);
672 dprintf_icon(stddeb,"DrawIcon / icon icoDIBOffset=%lX\n",
673 (DWORD)lpico->descriptor.icoDIBOffset);
674 dprintf_icon(stddeb,"DrawIcon / bitmap bmWidth=%d bmHeight=%d\n",
675 bm.bmWidth, bm.bmHeight);
676 hMemDC = CreateCompatibleDC(hDC);
677 #ifdef DEBUG_ICON
678 hBitTemp = SelectObject(hMemDC, lpico->hBitmap);
679 BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
680 SelectObject(hMemDC, lpico->hBitMask);
681 BitBlt(hDC, x, y + bm.bmHeight, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
682 #else
683 hBitTemp = SelectObject(hMemDC, lpico->hBitMask);
684 BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCAND);
685 SelectObject(hMemDC, lpico->hBitmap);
686 BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCPAINT);
687 #endif
688 SelectObject( hMemDC, hBitTemp );
689 DeleteDC(hMemDC);
690 return TRUE;