Release 940405
[wine.git] / objects / dib.c
blobc6d9d4474f39ebcb3df23dc5160bf819f2a07e5f
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;
132 /***********************************************************************
133 * DIB_SetImageBits_8
135 * SetDIBits for an 8-bit deep DIB.
137 static void DIB_SetImageBits_8( WORD lines, BYTE *bits, WORD width,
138 WORD *colors, XImage *bmpImage )
140 WORD x;
141 BYTE pad = (4 - (width & 3)) & 3;
143 while (lines--)
145 for (x = 0; x < width; x++)
146 XPutPixel( bmpImage, x, lines, colors[*bits++] );
147 bits += pad;
152 /***********************************************************************
153 * DIB_SetImageBits_24
155 * SetDIBits for a 24-bit deep DIB.
157 static void DIB_SetImageBits_24( WORD lines, BYTE *bits, WORD width,
158 DC *dc, XImage *bmpImage )
160 WORD x;
161 BYTE pad = (4 - ((width*3) & 3)) & 3;
163 while (lines--)
165 for (x = 0; x < width; x++, bits += 3)
167 XPutPixel( bmpImage, x, lines,
168 COLOR_ToPhysical( dc, RGB(bits[0],bits[1],bits[2]) ));
170 bits += pad;
175 /***********************************************************************
176 * DIB_SetImageBits
178 * Transfer the bits to an X image.
179 * Helper function for SetDIBits() and SetDIBitsToDevice().
181 static int DIB_SetImageBits( DC *dc, WORD lines, WORD depth, LPSTR bits,
182 BITMAPINFO *info, WORD coloruse,
183 Drawable drawable, GC gc, int xSrc, int ySrc,
184 int xDest, int yDest, int width, int height )
186 WORD *colorMapping;
187 XImage *bmpImage;
188 void *bmpData;
189 int i, colors, widthBytes;
191 /* Build the color mapping table */
193 if (info->bmiHeader.biBitCount == 24) colorMapping = NULL;
194 else
196 colors = info->bmiHeader.biClrUsed;
197 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
198 if (!(colorMapping = (WORD *)malloc( colors * sizeof(WORD) )))
199 return 0;
200 if (coloruse == DIB_RGB_COLORS)
202 RGBQUAD * rgbPtr = info->bmiColors;
203 for (i = 0; i < colors; i++, rgbPtr++)
204 colorMapping[i] = COLOR_ToPhysical( dc, RGB(rgbPtr->rgbRed,
205 rgbPtr->rgbGreen,
206 rgbPtr->rgbBlue) );
208 else
210 WORD * index = (WORD *)info->bmiColors;
211 for (i = 0; i < colors; i++, index++)
212 colorMapping[i] = COLOR_ToPhysical( dc, PALETTEINDEX(*index) );
216 /* Transfer the pixels */
218 widthBytes = (info->bmiHeader.biWidth * depth + 31) / 32 * 4;
219 bmpData = malloc( lines * widthBytes );
220 bmpImage = XCreateImage( display, DefaultVisualOfScreen(screen),
221 depth, ZPixmap, 0, bmpData,
222 info->bmiHeader.biWidth, lines, 32, widthBytes );
224 switch(info->bmiHeader.biBitCount)
226 case 1:
227 DIB_SetImageBits_1( lines, bits, info->bmiHeader.biWidth,
228 colorMapping, bmpImage );
229 break;
230 case 4:
231 DIB_SetImageBits_4( lines, bits, info->bmiHeader.biWidth,
232 colorMapping, bmpImage );
233 break;
234 case 8:
235 DIB_SetImageBits_8( lines, bits, info->bmiHeader.biWidth,
236 colorMapping, bmpImage );
237 break;
238 case 24:
239 DIB_SetImageBits_24( lines, bits, info->bmiHeader.biWidth,
240 dc, bmpImage );
241 break;
243 if (colorMapping) free(colorMapping);
245 XPutImage( display, drawable, gc, bmpImage, xSrc, ySrc,
246 xDest, yDest, width, height );
247 XDestroyImage( bmpImage );
248 return lines;
252 /***********************************************************************
253 * SetDIBits (GDI.440)
255 int SetDIBits( HDC hdc, HBITMAP hbitmap, WORD startscan, WORD lines,
256 LPSTR bits, BITMAPINFO * info, WORD coloruse )
258 DC * dc;
259 BITMAPOBJ * bmp;
261 /* Check parameters */
263 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
264 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
265 return 0;
266 if (!lines || (startscan >= (WORD)info->bmiHeader.biHeight)) return 0;
267 if (startscan+lines > info->bmiHeader.biHeight)
268 lines = info->bmiHeader.biHeight - startscan;
270 return DIB_SetImageBits( dc, lines, bmp->bitmap.bmBitsPixel,
271 bits, info, coloruse, bmp->pixmap, BITMAP_GC(bmp),
272 0, 0, 0, startscan, bmp->bitmap.bmWidth, lines );
276 /***********************************************************************
277 * SetDIBitsToDevice (GDI.443)
279 int SetDIBitsToDevice( HDC hdc, short xDest, short yDest, WORD cx, WORD cy,
280 WORD xSrc, WORD ySrc, WORD startscan, WORD lines,
281 LPSTR bits, BITMAPINFO * info, WORD coloruse )
283 DC * dc;
285 /* Check parameters */
287 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
288 if (!lines || (startscan >= info->bmiHeader.biHeight)) return 0;
289 if (startscan+lines > info->bmiHeader.biHeight)
290 lines = info->bmiHeader.biHeight - startscan;
291 if (ySrc < startscan) ySrc = startscan;
292 else if (ySrc >= startscan+lines) return 0;
293 if (xSrc >= info->bmiHeader.biWidth) return 0;
294 if (ySrc+cy >= startscan+lines) cy = startscan + lines - ySrc;
295 if (xSrc+cx >= info->bmiHeader.biWidth) cx = info->bmiHeader.biWidth-xSrc;
296 if (!cx || !cy) return 0;
298 DC_SetupGCForText( dc ); /* To have the correct ROP */
299 return DIB_SetImageBits( dc, lines, dc->w.bitsPerPixel,
300 bits, info, coloruse,
301 dc->u.x.drawable, dc->u.x.gc,
302 xSrc, ySrc - startscan,
303 dc->w.DCOrgX + XLPTODP( dc, xDest ),
304 dc->w.DCOrgY + YLPTODP( dc, yDest ),
305 cx, cy );
309 /***********************************************************************
310 * GetDIBits (GDI.441)
312 int GetDIBits( HDC hdc, HBITMAP hbitmap, WORD startscan, WORD lines,
313 LPSTR bits, BITMAPINFO * info, WORD coloruse )
315 DC * dc;
316 BITMAPOBJ * bmp;
317 PALETTEENTRY * palEntry;
318 PALETTEOBJ * palette;
319 XImage * bmpImage, * dibImage;
320 int i, x, y;
322 if (!lines) return 0;
323 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
324 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
325 return 0;
326 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
327 return 0;
329 /* Transfer color info */
331 palEntry = palette->logpalette.palPalEntry;
332 for (i = 0; i < info->bmiHeader.biClrUsed; i++, palEntry++)
334 if (coloruse == DIB_RGB_COLORS)
336 info->bmiColors[i].rgbRed = palEntry->peRed;
337 info->bmiColors[i].rgbGreen = palEntry->peGreen;
338 info->bmiColors[i].rgbBlue = palEntry->peBlue;
339 info->bmiColors[i].rgbReserved = 0;
341 else ((WORD *)info->bmiColors)[i] = (WORD)i;
344 /* Transfer the pixels (very slow...) */
346 if (bits)
348 bmpImage = XGetImage( display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
349 bmp->bitmap.bmHeight, AllPlanes, ZPixmap );
350 dibImage = DIB_DIBmpToImage( &info->bmiHeader, bits );
352 for (y = 0; y < lines; y++)
354 for (x = 0; x < info->bmiHeader.biWidth; x++)
356 XPutPixel( dibImage, x, y,
357 XGetPixel(bmpImage, x, bmp->bitmap.bmHeight-startscan-y-1) );
362 dibImage->data = NULL;
363 XDestroyImage( dibImage );
364 XDestroyImage( bmpImage );
366 return lines;
370 /***********************************************************************
371 * CreateDIBitmap (GDI.442)
373 HBITMAP CreateDIBitmap( HDC hdc, BITMAPINFOHEADER * header, DWORD init,
374 LPSTR bits, BITMAPINFO * data, WORD coloruse )
376 HBITMAP handle;
378 handle = CreateCompatibleBitmap( hdc, header->biWidth, header->biHeight );
379 if (!handle) return 0;
380 if (init == CBM_INIT) SetDIBits( hdc, handle, 0, header->biHeight,
381 bits, data, coloruse );
382 return handle;
385 /***********************************************************************
386 * DrawIcon (USER.84)
388 BOOL DrawIcon(HDC hDC, short x, short y, HICON hIcon)
390 ICONALLOC *lpico;
391 BITMAP bm;
392 HBITMAP hBitTemp;
393 HDC hMemDC;
394 HDC hMemDC2;
395 #ifdef DEBUG_ICON
396 printf("DrawIcon(%04X, %d, %d, %04X) \n", hDC, x, y, hIcon);
397 #endif
398 if (hIcon == (HICON)NULL) return FALSE;
399 lpico = (ICONALLOC *)GlobalLock(hIcon);
400 GetObject(lpico->hBitmap, sizeof(BITMAP), (LPSTR)&bm);
401 #ifdef DEBUG_ICON
402 printf("DrawIcon / x=%d y=%d\n", x, y);
403 printf("DrawIcon / icon Width=%d\n", (int)lpico->descriptor.Width);
404 printf("DrawIcon / icon Height=%d\n", (int)lpico->descriptor.Height);
405 printf("DrawIcon / icon ColorCount=%d\n", (int)lpico->descriptor.ColorCount);
406 printf("DrawIcon / icon icoDIBSize=%lX\n", (DWORD)lpico->descriptor.icoDIBSize);
407 printf("DrawIcon / icon icoDIBOffset=%lX\n", (DWORD)lpico->descriptor.icoDIBOffset);
408 printf("DrawIcon / bitmap bmWidth=%d bmHeight=%d\n", bm.bmWidth, bm.bmHeight);
409 #endif
410 hMemDC = CreateCompatibleDC(hDC);
411 #ifdef DEBUG_ICON
412 SelectObject(hMemDC, lpico->hBitmap);
413 BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
414 SelectObject(hMemDC, lpico->hBitMask);
415 BitBlt(hDC, x, y + bm.bmHeight, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
416 #else
417 SelectObject(hMemDC, lpico->hBitMask);
418 BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCAND);
419 SelectObject(hMemDC, lpico->hBitmap);
420 BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCPAINT);
421 #endif
422 DeleteDC(hMemDC);
423 return TRUE;