2 * GDI device independent bitmaps
4 * Copyright 1993 Alexandre Julliard
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
16 extern XImage * BITMAP_BmpToImage( BITMAP *, void * );
19 /***********************************************************************
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);
32 size = info->bmiHeader.biSize + size * sizeof(WORD);
37 /***********************************************************************
40 * Create an XImage pointing to the bitmap data.
42 XImage * DIB_DIBmpToImage( BITMAPINFOHEADER * bmp, void * bmpData )
45 int bytesPerLine = (bmp->biWidth * bmp->biBitCount + 31) / 32 * 4;
47 image = XCreateImage( XT_display, DefaultVisualOfScreen( XT_screen ),
48 bmp->biBitCount, ZPixmap, 0, bmpData,
49 bmp->biWidth, bmp->biHeight, 32, bytesPerLine );
51 image->byte_order = MSBFirst;
52 image->bitmap_bit_order = MSBFirst;
53 image->bitmap_unit = 16;
54 _XInitImageFuncPtrs(image);
59 /***********************************************************************
62 int SetDIBits( HDC hdc, HBITMAP hbitmap, WORD startscan, WORD lines,
63 LPSTR bits, BITMAPINFO * info, WORD coloruse )
70 XImage * bmpImage, * dibImage;
71 int i, x, y, pixel, colors;
74 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
75 if (!(bmpObj = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
77 if (!(bmp = (BITMAP *) GlobalLock( bmpObj->hBitmap ))) return 0;
79 /* Build the color mapping table */
81 if (info->bmiHeader.biBitCount == 24) colorMapping = NULL;
82 else if (coloruse == DIB_RGB_COLORS)
84 colors = info->bmiHeader.biClrUsed;
85 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
86 if (!(colorMapping = (WORD *)malloc( colors * sizeof(WORD) )))
88 GlobalUnlock( bmpObj->hBitmap );
91 for (i = 0, rgbPtr = info->bmiColors; i < colors; i++, rgbPtr++)
92 colorMapping[i] = GetNearestPaletteIndex( dc->w.hPalette,
97 else colorMapping = (WORD *)info->bmiColors;
99 /* Transfer the pixels (very slow...) */
101 bmpImage = BITMAP_BmpToImage( bmp, ((char *)bmp) + sizeof(BITMAP) );
102 dibImage = DIB_DIBmpToImage( &info->bmiHeader, bits );
104 for (y = 0; y < lines; y++)
106 for (x = 0; x < info->bmiHeader.biWidth; x++)
108 pixel = XGetPixel( dibImage, x, y );
109 if (colorMapping) pixel = colorMapping[pixel];
110 else pixel = GetNearestPaletteIndex(dc->w.hPalette,(COLORREF)pixel);
111 XPutPixel( bmpImage, x, bmp->bmHeight - startscan - y - 1, pixel );
115 bmpImage->data = NULL;
116 dibImage->data = NULL;
117 XDestroyImage( bmpImage );
118 XDestroyImage( dibImage );
120 if (colorMapping && (coloruse == DIB_RGB_COLORS)) free(colorMapping);
122 GlobalUnlock( bmpObj->hBitmap );
127 /***********************************************************************
128 * GetDIBits (GDI.441)
130 int GetDIBits( HDC hdc, HBITMAP hbitmap, WORD startscan, WORD lines,
131 LPSTR bits, BITMAPINFO * info, WORD coloruse )
136 PALETTEENTRY * palEntry;
137 PALETTEOBJ * palette;
138 XImage * bmpImage, * dibImage;
141 if (!lines) return 0;
142 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
143 if (!(bmpObj = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
145 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
147 if (!(bmp = (BITMAP *) GlobalLock( bmpObj->hBitmap ))) return 0;
149 /* Transfer color info */
151 palEntry = palette->logpalette.palPalEntry;
152 for (i = 0; i < info->bmiHeader.biClrUsed; i++, palEntry++)
154 if (coloruse == DIB_RGB_COLORS)
156 info->bmiColors[i].rgbRed = palEntry->peRed;
157 info->bmiColors[i].rgbGreen = palEntry->peGreen;
158 info->bmiColors[i].rgbBlue = palEntry->peBlue;
159 info->bmiColors[i].rgbReserved = 0;
161 else ((WORD *)info->bmiColors)[i] = (WORD)i;
164 /* Transfer the pixels (very slow...) */
168 bmpImage = BITMAP_BmpToImage( bmp, ((char *)bmp) + sizeof(BITMAP) );
169 dibImage = DIB_DIBmpToImage( &info->bmiHeader, bits );
171 for (y = 0; y < lines; y++)
173 for (x = 0; x < info->bmiHeader.biWidth; x++)
175 XPutPixel( dibImage, x, y,
176 XGetPixel(bmpImage, x, bmp->bmHeight-startscan-y-1) );
181 bmpImage->data = NULL;
182 dibImage->data = NULL;
183 XDestroyImage( bmpImage );
184 XDestroyImage( dibImage );
187 GlobalUnlock( bmpObj->hBitmap );
192 /***********************************************************************
193 * CreateDIBitmap (GDI.442)
195 HBITMAP CreateDIBitmap( HDC hdc, BITMAPINFOHEADER * header, DWORD init,
196 LPSTR bits, BITMAPINFO * data, WORD coloruse )
200 handle = CreateCompatibleBitmap( hdc, header->biWidth, header->biHeight );
201 if (!handle) return 0;
202 if (init == CBM_INIT) SetDIBits( hdc, handle, 0, header->biHeight,
203 bits, data, coloruse );
207 /***********************************************************************
210 BOOL DrawIcon(HDC hDC, short x, short y, HICON hIcon)
218 printf("DrawIcon(%04X, %d, %d, %04X) \n", hDC, x, y, hIcon);
220 if (hIcon == (HICON)NULL) return FALSE;
221 lpico = (ICONALLOC *)GlobalLock(hIcon);
222 GetObject(lpico->hBitmap, sizeof(BITMAP), (LPSTR)&bm);
224 printf("DrawIcon / x=%d y=%d\n", x, y);
225 printf("DrawIcon / icon Width=%d\n", (int)lpico->descriptor.Width);
226 printf("DrawIcon / icon Height=%d\n", (int)lpico->descriptor.Height);
227 printf("DrawIcon / icon ColorCount=%d\n", (int)lpico->descriptor.ColorCount);
228 printf("DrawIcon / icon icoDIBSize=%lX\n", (DWORD)lpico->descriptor.icoDIBSize);
229 printf("DrawIcon / icon icoDIBOffset=%lX\n", (DWORD)lpico->descriptor.icoDIBOffset);
230 printf("DrawIcon / bitmap bmWidth=%d bmHeight=%d\n", bm.bmWidth, bm.bmHeight);
232 hMemDC = CreateCompatibleDC(hDC);
234 SelectObject(hMemDC, lpico->hBitmap);
235 BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
236 SelectObject(hMemDC, lpico->hBitMask);
237 BitBlt(hDC, x, y + bm.bmHeight, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
239 SelectObject(hMemDC, lpico->hBitMask);
240 BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCAND);
241 SelectObject(hMemDC, lpico->hBitmap);
242 BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCPAINT);