Release 940912
[wine/gsoc-2012-control.git] / objects / dib.c
blob6042cb278c8416b11fc1880d4cf4f1642af46539
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"
17 extern WORD COLOR_ToPhysical( DC *dc, COLORREF color ); /* color.c */
19 /***********************************************************************
20 * DIB_BitmapInfoSize
22 * Return the size of the bitmap info structure.
24 int DIB_BitmapInfoSize( BITMAPINFO * info, WORD coloruse )
26 int size = info->bmiHeader.biClrUsed;
27 if (!size && (info->bmiHeader.biBitCount != 24))
28 size = 1 << info->bmiHeader.biBitCount;
29 if (coloruse == DIB_RGB_COLORS)
30 size = info->bmiHeader.biSize + size * sizeof(RGBQUAD);
31 else
32 size = info->bmiHeader.biSize + size * sizeof(WORD);
33 return size;
37 /***********************************************************************
38 * DIB_DIBmpToImage
40 * Create an XImage pointing to the bitmap data.
42 static XImage *DIB_DIBmpToImage( BITMAPINFOHEADER * bmp, void * bmpData )
44 extern void _XInitImageFuncPtrs( XImage* );
45 XImage * image;
46 int bytesPerLine = (bmp->biWidth * bmp->biBitCount + 31) / 32 * 4;
48 image = XCreateImage( display, DefaultVisualOfScreen( screen ),
49 bmp->biBitCount, ZPixmap, 0, bmpData,
50 bmp->biWidth, bmp->biHeight, 32, bytesPerLine );
51 if (!image) return 0;
52 image->byte_order = MSBFirst;
53 image->bitmap_bit_order = MSBFirst;
54 image->bitmap_unit = 16;
55 _XInitImageFuncPtrs(image);
56 return image;
60 /***********************************************************************
61 * DIB_SetImageBits_1
63 * SetDIBits for a 1-bit deep DIB.
65 static void DIB_SetImageBits_1( WORD lines, BYTE *bits, WORD width,
66 WORD *colors, XImage *bmpImage )
68 WORD i, x;
69 BYTE pad, pix;
71 if (!(width & 31)) pad = 0;
72 else pad = ((32 - (width & 31)) + 7) / 8;
74 while (lines--)
76 for (i = width/8, x = 0; (i > 0); i--)
78 pix = *bits++;
79 XPutPixel( bmpImage, x++, lines, colors[pix >> 7] );
80 XPutPixel( bmpImage, x++, lines, colors[(pix >> 6) & 1] );
81 XPutPixel( bmpImage, x++, lines, colors[(pix >> 5) & 1] );
82 XPutPixel( bmpImage, x++, lines, colors[(pix >> 4) & 1] );
83 XPutPixel( bmpImage, x++, lines, colors[(pix >> 3) & 1] );
84 XPutPixel( bmpImage, x++, lines, colors[(pix >> 2) & 1] );
85 XPutPixel( bmpImage, x++, lines, colors[(pix >> 1) & 1] );
86 XPutPixel( bmpImage, x++, lines, colors[pix & 1] );
88 pix = *bits;
89 switch(width & 7)
91 case 7: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
92 case 6: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
93 case 5: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
94 case 4: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
95 case 3: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
96 case 2: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
97 case 1: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] );
99 bits += pad;
104 /***********************************************************************
105 * DIB_SetImageBits_4
107 * SetDIBits for a 4-bit deep DIB.
109 static void DIB_SetImageBits_4( WORD lines, BYTE *bits, WORD width,
110 WORD *colors, XImage *bmpImage )
112 WORD i, x;
113 BYTE pad;
115 if (!(width & 7)) pad = 0;
116 else pad = ((8 - (width & 7)) + 1) / 2;
118 while (lines--)
120 for (i = width/2, x = 0; i > 0; i--)
122 BYTE pix = *bits++;
123 XPutPixel( bmpImage, x++, lines, colors[pix >> 4] );
124 XPutPixel( bmpImage, x++, lines, colors[pix & 0x0f] );
126 if (width & 1) XPutPixel( bmpImage, x, lines, colors[*bits >> 4] );
127 bits += pad;
131 #define check_xy(x,y) \
132 if (x > width) { \
133 x = 0; \
134 if (lines) \
135 lines--; \
138 /***********************************************************************
139 * DIB_SetImageBits_RLE4
141 * SetDIBits for a 4-bit deep compressed DIB.
143 static void DIB_SetImageBits_RLE4( WORD lines, BYTE *bits, WORD width,
144 WORD *colors, XImage *bmpImage )
146 int x = 0, c, length;
147 BYTE *begin = bits;
149 lines--;
150 while (1) {
151 length = *bits++;
152 if (length) { /* encoded */
153 c = *bits++;
154 while (length--) {
155 XPutPixel(bmpImage, x++, lines, colors[c >> 4]);
156 check_xy(x, y);
157 if (length) {
158 length--;
159 XPutPixel(bmpImage, x++, lines, colors[c & 0xf]);
160 check_xy(x, y);
163 } else {
164 length = *bits++;
165 switch (length) {
166 case 0: /* eol */
167 x = 0;
168 lines--;
169 continue;
171 case 1: /* eopicture */
172 return;
174 case 2: /* delta */
175 x += *bits++;
176 lines -= *bits++;
177 continue;
179 default: /* absolute */
180 while (length--) {
181 c = *bits++;
182 XPutPixel(bmpImage, x++, lines, colors[c >> 4]);
183 check_xy(x, y);
184 if (length) {
185 length--;
186 XPutPixel(bmpImage, x++, lines, colors[c & 0xf]);
187 check_xy(x, y);
190 if ((bits - begin) & 1)
191 bits++;
197 /***********************************************************************
198 * DIB_SetImageBits_8
200 * SetDIBits for an 8-bit deep DIB.
202 static void DIB_SetImageBits_8( WORD lines, BYTE *bits, WORD width,
203 WORD *colors, XImage *bmpImage )
205 WORD x;
206 BYTE pad = (4 - (width & 3)) & 3;
208 while (lines--)
210 for (x = 0; x < width; x++)
211 XPutPixel( bmpImage, x, lines, colors[*bits++] );
212 bits += pad;
216 /***********************************************************************
217 * DIB_SetImageBits_RLE8
219 * SetDIBits for an 8-bit deep compressed DIB.
221 static void DIB_SetImageBits_RLE8( WORD lines, BYTE *bits, WORD width,
222 WORD *colors, XImage *bmpImage )
224 int x = 0, i, length;
225 BYTE *begin = bits;
227 lines--;
228 while (1) {
229 length = *bits++;
230 if (length) { /* encoded */
231 while (length--) {
232 XPutPixel(bmpImage, x++, lines, colors[*bits]);
233 if (x > width) {
234 x = 0;
235 if (lines)
236 lines--;
239 bits++;
240 } else {
241 length = *bits++;
242 switch (length) {
243 case 0: /* eol */
244 x = 0;
245 lines--;
246 continue;
248 case 1: /* eopicture */
249 return;
251 case 2: /* delta */
252 x += *bits++;
253 lines -= *bits++;
254 continue;
256 default: /* absolute */
257 for (i = length; i ; i--) {
258 XPutPixel(bmpImage, x++, lines,
259 colors[*bits++]);
260 if (x > width) {
261 x = 0;
262 if (lines)
263 lines--;
266 if ((bits - begin) & 1)
267 bits++;
273 /***********************************************************************
274 * DIB_SetImageBits_24
276 * SetDIBits for a 24-bit deep DIB.
278 static void DIB_SetImageBits_24( WORD lines, BYTE *bits, WORD width,
279 DC *dc, XImage *bmpImage )
281 WORD x;
282 BYTE pad = (4 - ((width*3) & 3)) & 3;
284 while (lines--)
286 for (x = 0; x < width; x++, bits += 3)
288 XPutPixel( bmpImage, x, lines,
289 COLOR_ToPhysical( dc, RGB(bits[0],bits[1],bits[2]) ));
291 bits += pad;
296 /***********************************************************************
297 * DIB_SetImageBits
299 * Transfer the bits to an X image.
300 * Helper function for SetDIBits() and SetDIBitsToDevice().
302 static int DIB_SetImageBits( DC *dc, WORD lines, WORD depth, LPSTR bits,
303 BITMAPINFO *info, WORD coloruse,
304 Drawable drawable, GC gc, int xSrc, int ySrc,
305 int xDest, int yDest, int width, int height )
307 WORD *colorMapping;
308 XImage *bmpImage;
309 void *bmpData;
310 int i, colors, widthBytes;
312 /* Build the color mapping table */
314 if (info->bmiHeader.biBitCount == 24) colorMapping = NULL;
315 else
317 colors = info->bmiHeader.biClrUsed;
318 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
319 if (!(colorMapping = (WORD *)malloc( colors * sizeof(WORD) )))
320 return 0;
321 if (coloruse == DIB_RGB_COLORS)
323 RGBQUAD * rgbPtr = info->bmiColors;
324 for (i = 0; i < colors; i++, rgbPtr++)
325 colorMapping[i] = COLOR_ToPhysical( dc, RGB(rgbPtr->rgbRed,
326 rgbPtr->rgbGreen,
327 rgbPtr->rgbBlue) );
329 else
331 WORD * index = (WORD *)info->bmiColors;
332 for (i = 0; i < colors; i++, index++)
333 colorMapping[i] = COLOR_ToPhysical( dc, PALETTEINDEX(*index) );
337 /* Transfer the pixels */
339 widthBytes = (info->bmiHeader.biWidth * depth + 31) / 32 * 4;
340 bmpData = malloc( lines * widthBytes );
341 bmpImage = XCreateImage( display, DefaultVisualOfScreen(screen),
342 depth, ZPixmap, 0, bmpData,
343 info->bmiHeader.biWidth, lines, 32, widthBytes );
345 switch(info->bmiHeader.biBitCount)
347 case 1:
348 DIB_SetImageBits_1( lines, bits, info->bmiHeader.biWidth,
349 colorMapping, bmpImage );
350 break;
351 case 4:
352 if (info->bmiHeader.biCompression)
353 DIB_SetImageBits_RLE4( lines, bits, info->bmiHeader.biWidth,
354 colorMapping, bmpImage );
355 else
356 DIB_SetImageBits_4( lines, bits, info->bmiHeader.biWidth,
357 colorMapping, bmpImage );
358 break;
359 case 8:
360 if (info->bmiHeader.biCompression)
361 DIB_SetImageBits_RLE8( lines, bits, info->bmiHeader.biWidth,
362 colorMapping, bmpImage );
363 else
364 DIB_SetImageBits_8( lines, bits, info->bmiHeader.biWidth,
365 colorMapping, bmpImage );
366 break;
367 case 24:
368 DIB_SetImageBits_24( lines, bits, info->bmiHeader.biWidth,
369 dc, bmpImage );
370 break;
372 if (colorMapping) free(colorMapping);
374 XPutImage( display, drawable, gc, bmpImage, xSrc, ySrc,
375 xDest, yDest, width, height );
376 XDestroyImage( bmpImage );
377 return lines;
381 /***********************************************************************
382 * SetDIBits (GDI.440)
384 int SetDIBits( HDC hdc, HBITMAP hbitmap, WORD startscan, WORD lines,
385 LPSTR bits, BITMAPINFO * info, WORD coloruse )
387 DC * dc;
388 BITMAPOBJ * bmp;
390 /* Check parameters */
392 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
393 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
394 return 0;
395 if (!lines || (startscan >= (WORD)info->bmiHeader.biHeight)) return 0;
396 if (startscan+lines > info->bmiHeader.biHeight)
397 lines = info->bmiHeader.biHeight - startscan;
399 return DIB_SetImageBits( dc, lines, bmp->bitmap.bmBitsPixel,
400 bits, info, coloruse, bmp->pixmap, BITMAP_GC(bmp),
401 0, 0, 0, startscan, bmp->bitmap.bmWidth, lines );
405 /***********************************************************************
406 * SetDIBitsToDevice (GDI.443)
408 int SetDIBitsToDevice( HDC hdc, short xDest, short yDest, WORD cx, WORD cy,
409 WORD xSrc, WORD ySrc, WORD startscan, WORD lines,
410 LPSTR bits, BITMAPINFO * info, WORD coloruse )
412 DC * dc;
414 /* Check parameters */
416 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
417 if (!lines || (startscan >= info->bmiHeader.biHeight)) return 0;
418 if (startscan+lines > info->bmiHeader.biHeight)
419 lines = info->bmiHeader.biHeight - startscan;
420 if (ySrc < startscan) ySrc = startscan;
421 else if (ySrc >= startscan+lines) return 0;
422 if (xSrc >= info->bmiHeader.biWidth) return 0;
423 if (ySrc+cy >= startscan+lines) cy = startscan + lines - ySrc;
424 if (xSrc+cx >= info->bmiHeader.biWidth) cx = info->bmiHeader.biWidth-xSrc;
425 if (!cx || !cy) return 0;
427 DC_SetupGCForText( dc ); /* To have the correct ROP */
428 return DIB_SetImageBits( dc, lines, dc->w.bitsPerPixel,
429 bits, info, coloruse,
430 dc->u.x.drawable, dc->u.x.gc,
431 xSrc, ySrc - startscan,
432 dc->w.DCOrgX + XLPTODP( dc, xDest ),
433 dc->w.DCOrgY + YLPTODP( dc, yDest ),
434 cx, cy );
438 /***********************************************************************
439 * GetDIBits (GDI.441)
441 int GetDIBits( HDC hdc, HBITMAP hbitmap, WORD startscan, WORD lines,
442 LPSTR bits, BITMAPINFO * info, WORD coloruse )
444 DC * dc;
445 BITMAPOBJ * bmp;
446 PALETTEENTRY * palEntry;
447 PALETTEOBJ * palette;
448 XImage * bmpImage, * dibImage;
449 int i, x, y;
451 if (!lines) return 0;
452 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
453 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
454 return 0;
455 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
456 return 0;
458 /* Transfer color info */
460 palEntry = palette->logpalette.palPalEntry;
461 for (i = 0; i < info->bmiHeader.biClrUsed; i++, palEntry++)
463 if (coloruse == DIB_RGB_COLORS)
465 info->bmiColors[i].rgbRed = palEntry->peRed;
466 info->bmiColors[i].rgbGreen = palEntry->peGreen;
467 info->bmiColors[i].rgbBlue = palEntry->peBlue;
468 info->bmiColors[i].rgbReserved = 0;
470 else ((WORD *)info->bmiColors)[i] = (WORD)i;
473 /* Transfer the pixels (very slow...) */
475 if (bits)
477 bmpImage = XGetImage( display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
478 bmp->bitmap.bmHeight, AllPlanes, ZPixmap );
479 dibImage = DIB_DIBmpToImage( &info->bmiHeader, bits );
481 for (y = 0; y < lines; y++)
483 for (x = 0; x < info->bmiHeader.biWidth; x++)
485 XPutPixel( dibImage, x, y,
486 XGetPixel(bmpImage, x, bmp->bitmap.bmHeight-startscan-y-1) );
491 dibImage->data = NULL;
492 XDestroyImage( dibImage );
493 XDestroyImage( bmpImage );
495 return lines;
499 /***********************************************************************
500 * CreateDIBitmap (GDI.442)
502 HBITMAP CreateDIBitmap( HDC hdc, BITMAPINFOHEADER * header, DWORD init,
503 LPSTR bits, BITMAPINFO * data, WORD coloruse )
505 HBITMAP handle;
507 handle = CreateCompatibleBitmap( hdc, header->biWidth, header->biHeight );
508 if (!handle) return 0;
509 if (init == CBM_INIT) SetDIBits( hdc, handle, 0, header->biHeight,
510 bits, data, coloruse );
511 return handle;
514 /***********************************************************************
515 * DrawIcon (USER.84)
517 BOOL DrawIcon(HDC hDC, short x, short y, HICON hIcon)
519 ICONALLOC *lpico;
520 BITMAP bm;
521 HBITMAP hBitTemp;
522 HDC hMemDC;
523 HDC hMemDC2;
524 #ifdef DEBUG_ICON
525 printf("DrawIcon(%04X, %d, %d, %04X) \n", hDC, x, y, hIcon);
526 #endif
527 if (hIcon == (HICON)NULL) return FALSE;
528 lpico = (ICONALLOC *)GlobalLock(hIcon);
529 GetObject(lpico->hBitmap, sizeof(BITMAP), (LPSTR)&bm);
530 #ifdef DEBUG_ICON
531 printf("DrawIcon / x=%d y=%d\n", x, y);
532 printf("DrawIcon / icon Width=%d\n", (int)lpico->descriptor.Width);
533 printf("DrawIcon / icon Height=%d\n", (int)lpico->descriptor.Height);
534 printf("DrawIcon / icon ColorCount=%d\n", (int)lpico->descriptor.ColorCount);
535 printf("DrawIcon / icon icoDIBSize=%lX\n", (DWORD)lpico->descriptor.icoDIBSize);
536 printf("DrawIcon / icon icoDIBOffset=%lX\n", (DWORD)lpico->descriptor.icoDIBOffset);
537 printf("DrawIcon / bitmap bmWidth=%d bmHeight=%d\n", bm.bmWidth, bm.bmHeight);
538 #endif
539 hMemDC = CreateCompatibleDC(hDC);
540 #ifdef DEBUG_ICON
541 SelectObject(hMemDC, lpico->hBitmap);
542 BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
543 SelectObject(hMemDC, lpico->hBitMask);
544 BitBlt(hDC, x, y + bm.bmHeight, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
545 #else
546 SelectObject(hMemDC, lpico->hBitMask);
547 BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCAND);
548 SelectObject(hMemDC, lpico->hBitmap);
549 BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCPAINT);
550 #endif
551 DeleteDC(hMemDC);
552 return TRUE;