4 * This file contains routines for manipulation full-color images.
6 * Copyright (c) 1995 Sun Microsystems, Inc.
8 * See the file "license.terms" for information on usage and redistribution
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 static int DestroyImage _ANSI_ARGS_((XImage* data));
17 static unsigned long ImageGetPixel _ANSI_ARGS_((XImage *image, int x, int y));
18 static int PutPixel _ANSI_ARGS_((XImage *image, int x, int y,
19 unsigned long pixel));
22 *----------------------------------------------------------------------
26 * This is a trivial wrapper around ckfree to make it possible to
27 * pass ckfree as a pointer.
33 * Deallocates the image.
35 *----------------------------------------------------------------------
39 DestroyImage(imagePtr)
40 XImage *imagePtr; /* image to free */
44 ckfree((char*)imagePtr->data);
46 ckfree((char*)imagePtr);
52 *----------------------------------------------------------------------
56 * Get a single pixel from an image.
59 * Returns the 32 bit pixel value.
64 *----------------------------------------------------------------------
68 ImageGetPixel(image, x, y)
72 unsigned long pixel = 0;
73 unsigned char *srcPtr = &(image->data[(y * image->bytes_per_line)
74 + ((x * image->bits_per_pixel) / NBBY)]);
76 switch (image->bits_per_pixel) {
79 pixel = RGB(srcPtr[2], srcPtr[1], srcPtr[0]);
82 pixel = RGB(((((WORD*)srcPtr)[0]) >> 7) & 0xf8,
83 ((((WORD*)srcPtr)[0]) >> 2) & 0xf8,
84 ((((WORD*)srcPtr)[0]) << 3) & 0xf8);
90 pixel = ((x%2) ? (*srcPtr) : ((*srcPtr) >> 4)) & 0x0f;
93 pixel = ((*srcPtr) & (0x80 >> (x%8))) ? 1 : 0;
100 *----------------------------------------------------------------------
104 * Set a single pixel in an image.
112 *----------------------------------------------------------------------
116 PutPixel(image, x, y, pixel)
121 unsigned char *destPtr = &(image->data[(y * image->bytes_per_line)
122 + ((x * image->bits_per_pixel) / NBBY)]);
124 switch (image->bits_per_pixel) {
127 * Pixel is DWORD: 0x00BBGGRR
133 * Pixel is triplet: 0xBBGGRR.
136 destPtr[0] = (unsigned char) GetBValue(pixel);
137 destPtr[1] = (unsigned char) GetGValue(pixel);
138 destPtr[2] = (unsigned char) GetRValue(pixel);
142 * Pixel is WORD: 5-5-5 (R-G-B)
146 ((GetRValue(pixel) & 0xf8) << 7)
147 | ((GetGValue(pixel) & 0xf8) <<2)
148 | ((GetBValue(pixel) & 0xf8) >> 3);
152 * Pixel is 8-bit index into color table.
155 (*destPtr) = (unsigned char) pixel;
159 * Pixel is 4-bit index in MSBFirst order.
162 (*destPtr) = (unsigned char) (((*destPtr) & 0xf0)
165 (*destPtr) = (unsigned char) (((*destPtr) & 0x0f)
166 | ((pixel << 4) & 0xf0));
171 * Pixel is bit in MSBFirst order.
174 int mask = (0x80 >> (x%8));
187 *----------------------------------------------------------------------
191 * Allocates storage for a new XImage.
194 * Returns a newly allocated XImage.
199 *----------------------------------------------------------------------
203 XCreateImage(display, visual, depth, format, offset, data, width, height,
204 bitmap_pad, bytes_per_line)
216 XImage* imagePtr = (XImage *) ckalloc(sizeof(XImage));
217 imagePtr->width = width;
218 imagePtr->height = height;
219 imagePtr->xoffset = offset;
220 imagePtr->format = format;
221 imagePtr->data = data;
222 imagePtr->byte_order = LSBFirst;
223 imagePtr->bitmap_unit = 8;
224 imagePtr->bitmap_bit_order = MSBFirst;
225 imagePtr->bitmap_pad = bitmap_pad;
226 imagePtr->bits_per_pixel = depth;
227 imagePtr->depth = depth;
230 * Under Windows, bitmap_pad must be on an LONG data-type boundary.
233 #define LONGBITS (sizeof(LONG) * 8)
235 bitmap_pad = (bitmap_pad + LONGBITS - 1) / LONGBITS * LONGBITS;
238 * Round to the nearest bitmap_pad boundary.
241 if (bytes_per_line) {
242 imagePtr->bytes_per_line = bytes_per_line;
244 imagePtr->bytes_per_line = (((depth * width)
245 + (bitmap_pad - 1)) >> 3) & ~((bitmap_pad >> 3) - 1);
248 imagePtr->red_mask = 0;
249 imagePtr->green_mask = 0;
250 imagePtr->blue_mask = 0;
252 imagePtr->f.put_pixel = PutPixel;
253 imagePtr->f.get_pixel = ImageGetPixel;
254 imagePtr->f.destroy_image = DestroyImage;
255 imagePtr->f.create_image = NULL;
256 imagePtr->f.sub_image = NULL;
257 imagePtr->f.add_pixel = NULL;
263 *----------------------------------------------------------------------
267 * This function copies data from a pixmap or window into an
271 * Returns a newly allocated image containing the data from the
272 * given rectangle of the given drawable.
277 *----------------------------------------------------------------------
281 XGetImage(display, d, x, y, width, height, plane_mask, format)
288 unsigned long plane_mask;
291 TkWinDrawable *twdPtr = (TkWinDrawable *)d;
294 char infoBuf[sizeof(BITMAPINFO) + sizeof(RGBQUAD)];
295 BITMAPINFO *infoPtr = (BITMAPINFO*)infoBuf;
297 if ((twdPtr->type != TWD_BITMAP) || (twdPtr->bitmap.handle == NULL)
298 || (format != XYPixmap) || (plane_mask != 1)) {
299 panic("XGetImage: not implemented");
303 imagePtr = XCreateImage(display, NULL, 1, XYBitmap, 0, NULL,
304 width, height, 32, 0);
305 imagePtr->data = ckalloc(imagePtr->bytes_per_line * imagePtr->height);
309 GetDIBits(dc, twdPtr->bitmap.handle, 0, height, NULL,
310 infoPtr, DIB_RGB_COLORS);
312 infoPtr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
313 infoPtr->bmiHeader.biWidth = width;
314 infoPtr->bmiHeader.biHeight = -(LONG)height;
315 infoPtr->bmiHeader.biPlanes = 1;
316 infoPtr->bmiHeader.biBitCount = 1;
317 infoPtr->bmiHeader.biCompression = BI_RGB;
318 infoPtr->bmiHeader.biCompression = 0;
319 infoPtr->bmiHeader.biXPelsPerMeter = 0;
320 infoPtr->bmiHeader.biYPelsPerMeter = 0;
321 infoPtr->bmiHeader.biClrUsed = 0;
322 infoPtr->bmiHeader.biClrImportant = 0;
324 GetDIBits(dc, twdPtr->bitmap.handle, 0, height, imagePtr->data,
325 infoPtr, DIB_RGB_COLORS);