1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
/*
* tkWinImage.c --
*
* This file contains routines for manipulation full-color images.
*
* Copyright (c) 1995 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tkWinImage.c,v 1.3 2000/01/06 02:22:51 hobbs Exp $
*/
#include "tkWinInt.h"
static int DestroyImage _ANSI_ARGS_((XImage* data));
static unsigned long ImageGetPixel _ANSI_ARGS_((XImage *image, int x, int y));
static int PutPixel _ANSI_ARGS_((XImage *image, int x, int y,
unsigned long pixel));
/*
*----------------------------------------------------------------------
*
* DestroyImage --
*
* This is a trivial wrapper around ckfree to make it possible to
* pass ckfree as a pointer.
*
* Results:
* None.
*
* Side effects:
* Deallocates the image.
*
*----------------------------------------------------------------------
*/
static int
DestroyImage(imagePtr)
XImage *imagePtr; /* image to free */
{
if (imagePtr) {
if (imagePtr->data) {
ckfree((char*)imagePtr->data);
}
ckfree((char*)imagePtr);
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* ImageGetPixel --
*
* Get a single pixel from an image.
*
* Results:
* Returns the 32 bit pixel value.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static unsigned long
ImageGetPixel(image, x, y)
XImage *image;
int x, y;
{
unsigned long pixel = 0;
unsigned char *srcPtr = &(image->data[(y * image->bytes_per_line)
+ ((x * image->bits_per_pixel) / NBBY)]);
switch (image->bits_per_pixel) {
case 32:
case 24:
pixel = RGB(srcPtr[2], srcPtr[1], srcPtr[0]);
break;
case 16:
pixel = RGB(((((WORD*)srcPtr)[0]) >> 7) & 0xf8,
((((WORD*)srcPtr)[0]) >> 2) & 0xf8,
((((WORD*)srcPtr)[0]) << 3) & 0xf8);
break;
case 8:
pixel = srcPtr[0];
break;
case 4:
pixel = ((x%2) ? (*srcPtr) : ((*srcPtr) >> 4)) & 0x0f;
break;
case 1:
pixel = ((*srcPtr) & (0x80 >> (x%8))) ? 1 : 0;
break;
}
return pixel;
}
/*
*----------------------------------------------------------------------
*
* PutPixel --
*
* Set a single pixel in an image.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
PutPixel(image, x, y, pixel)
XImage *image;
int x, y;
unsigned long pixel;
{
unsigned char *destPtr = &(image->data[(y * image->bytes_per_line)
+ ((x * image->bits_per_pixel) / NBBY)]);
switch (image->bits_per_pixel) {
case 32:
/*
* Pixel is DWORD: 0x00BBGGRR
*/
destPtr[3] = 0;
case 24:
/*
* Pixel is triplet: 0xBBGGRR.
*/
destPtr[0] = (unsigned char) GetBValue(pixel);
destPtr[1] = (unsigned char) GetGValue(pixel);
destPtr[2] = (unsigned char) GetRValue(pixel);
break;
case 16:
/*
* Pixel is WORD: 5-5-5 (R-G-B)
*/
(*(WORD*)destPtr) =
((GetRValue(pixel) & 0xf8) << 7)
| ((GetGValue(pixel) & 0xf8) <<2)
| ((GetBValue(pixel) & 0xf8) >> 3);
break;
case 8:
/*
* Pixel is 8-bit index into color table.
*/
(*destPtr) = (unsigned char) pixel;
break;
case 4:
/*
* Pixel is 4-bit index in MSBFirst order.
*/
if (x%2) {
(*destPtr) = (unsigned char) (((*destPtr) & 0xf0)
| (pixel & 0x0f));
} else {
(*destPtr) = (unsigned char) (((*destPtr) & 0x0f)
| ((pixel << 4) & 0xf0));
}
break;
case 1: {
/*
* Pixel is bit in MSBFirst order.
*/
int mask = (0x80 >> (x%8));
if (pixel) {
(*destPtr) |= mask;
} else {
(*destPtr) &= ~mask;
}
}
break;
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* XCreateImage --
*
* Allocates storage for a new XImage.
*
* Results:
* Returns a newly allocated XImage.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
XImage *
XCreateImage(display, visual, depth, format, offset, data, width, height,
bitmap_pad, bytes_per_line)
Display* display;
Visual* visual;
unsigned int depth;
int format;
int offset;
char* data;
unsigned int width;
unsigned int height;
int bitmap_pad;
int bytes_per_line;
{
XImage* imagePtr = (XImage *) ckalloc(sizeof(XImage));
imagePtr->width = width;
imagePtr->height = height;
imagePtr->xoffset = offset;
imagePtr->format = format;
imagePtr->data = data;
imagePtr->byte_order = LSBFirst;
imagePtr->bitmap_unit = 8;
imagePtr->bitmap_bit_order = MSBFirst;
imagePtr->bitmap_pad = bitmap_pad;
imagePtr->bits_per_pixel = depth;
imagePtr->depth = depth;
/*
* Under Windows, bitmap_pad must be on an LONG data-type boundary.
*/
#define LONGBITS (sizeof(LONG) * 8)
bitmap_pad = (bitmap_pad + LONGBITS - 1) / LONGBITS * LONGBITS;
/*
* Round to the nearest bitmap_pad boundary.
*/
if (bytes_per_line) {
imagePtr->bytes_per_line = bytes_per_line;
} else {
imagePtr->bytes_per_line = (((depth * width)
+ (bitmap_pad - 1)) >> 3) & ~((bitmap_pad >> 3) - 1);
}
imagePtr->red_mask = 0;
imagePtr->green_mask = 0;
imagePtr->blue_mask = 0;
imagePtr->f.put_pixel = PutPixel;
imagePtr->f.get_pixel = ImageGetPixel;
imagePtr->f.destroy_image = DestroyImage;
imagePtr->f.create_image = NULL;
imagePtr->f.sub_image = NULL;
imagePtr->f.add_pixel = NULL;
return imagePtr;
}
/*
*----------------------------------------------------------------------
*
* XGetImage --
*
* This function copies data from a pixmap or window into an
* XImage.
*
* Results:
* Returns a newly allocated image containing the data from the
* given rectangle of the given drawable.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
XImage *
XGetImage(display, d, x, y, width, height, plane_mask, format)
Display* display;
Drawable d;
int x;
int y;
unsigned int width;
unsigned int height;
unsigned long plane_mask;
int format;
{
TkWinDrawable *twdPtr = (TkWinDrawable *)d;
XImage *imagePtr;
HDC dc;
char infoBuf[sizeof(BITMAPINFO) + sizeof(RGBQUAD)];
BITMAPINFO *infoPtr = (BITMAPINFO*)infoBuf;
if ((twdPtr->type != TWD_BITMAP) || (twdPtr->bitmap.handle == NULL)
|| (format != XYPixmap) || (plane_mask != 1)) {
panic("XGetImage: not implemented");
}
imagePtr = XCreateImage(display, NULL, 1, XYBitmap, 0, NULL,
width, height, 32, 0);
imagePtr->data = ckalloc(imagePtr->bytes_per_line * imagePtr->height);
dc = GetDC(NULL);
GetDIBits(dc, twdPtr->bitmap.handle, 0, height, NULL,
infoPtr, DIB_RGB_COLORS);
infoPtr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
infoPtr->bmiHeader.biWidth = width;
infoPtr->bmiHeader.biHeight = -(LONG)height;
infoPtr->bmiHeader.biPlanes = 1;
infoPtr->bmiHeader.biBitCount = 1;
infoPtr->bmiHeader.biCompression = BI_RGB;
infoPtr->bmiHeader.biCompression = 0;
infoPtr->bmiHeader.biXPelsPerMeter = 0;
infoPtr->bmiHeader.biYPelsPerMeter = 0;
infoPtr->bmiHeader.biClrUsed = 0;
infoPtr->bmiHeader.biClrImportant = 0;
GetDIBits(dc, twdPtr->bitmap.handle, 0, height, imagePtr->data,
infoPtr, DIB_RGB_COLORS);
ReleaseDC(NULL, dc);
return imagePtr;
}
|