| 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 |
#include "bltInt.h" |
| 30 |
#include "bltImage.h" |
| 31 |
#include "bltHash.h" |
| 32 |
#include <X11/Xutil.h> |
| 33 |
#include <X11/Xproto.h> |
| 34 |
|
| 35 |
#define CLAMP(c) ((((c) < 0.0) ? 0.0 : ((c) > 255.0) ? 255.0 : (c))) |
| 36 |
|
| 37 |
int redAdjust, greenAdjust, blueAdjust; |
| 38 |
int redMaskShift, greenMaskShift, blueMaskShift; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
static int |
| 63 |
ShiftCount(mask) |
| 64 |
register unsigned int mask; |
| 65 |
{ |
| 66 |
register int count; |
| 67 |
|
| 68 |
for (count = 0; count < 32; count++) { |
| 69 |
if (mask & 0x01) { |
| 70 |
break; |
| 71 |
} |
| 72 |
mask >>= 1; |
| 73 |
} |
| 74 |
return count; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
static int |
| 93 |
CountBits(mask) |
| 94 |
register unsigned long mask; |
| 95 |
{ |
| 96 |
|
| 97 |
mask = (mask & 0x55555555) + ((mask >> 1) & (0x55555555)); |
| 98 |
|
| 99 |
mask = (mask & 0x33333333) + ((mask >> 2) & (0x33333333)); |
| 100 |
|
| 101 |
mask = (mask & 0x07070707) + ((mask >> 4) & (0x07070707)); |
| 102 |
|
| 103 |
mask = (mask & 0x000F000F) + ((mask >> 8) & (0x000F000F)); |
| 104 |
|
| 105 |
mask = (mask & 0x0000001F) + ((mask >> 16) & (0x0000001F)); |
| 106 |
return mask; |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
ComputeMasks(visualPtr) |
| 111 |
Visual *visualPtr; |
| 112 |
{ |
| 113 |
int count; |
| 114 |
|
| 115 |
redMaskShift = ShiftCount((unsigned int)visualPtr->red_mask); |
| 116 |
greenMaskShift = ShiftCount((unsigned int)visualPtr->green_mask); |
| 117 |
blueMaskShift = ShiftCount((unsigned int)visualPtr->blue_mask); |
| 118 |
|
| 119 |
redAdjust = greenAdjust = blueAdjust = 0; |
| 120 |
count = CountBits((unsigned long)visualPtr->red_mask); |
| 121 |
if (count < 8) { |
| 122 |
redAdjust = 8 - count; |
| 123 |
} |
| 124 |
count = CountBits((unsigned long)visualPtr->green_mask); |
| 125 |
if (count < 8) { |
| 126 |
greenAdjust = 8 - count; |
| 127 |
} |
| 128 |
count = CountBits((unsigned long)visualPtr->blue_mask); |
| 129 |
if (count < 8) { |
| 130 |
blueAdjust = 8 - count; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
static INLINE unsigned int |
| 147 |
TrueColorPixel(visualPtr, pixelPtr) |
| 148 |
Visual *visualPtr; |
| 149 |
Pix32 *pixelPtr; |
| 150 |
{ |
| 151 |
unsigned int red, green, blue; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
red = ((unsigned int)pixelPtr->Red >> redAdjust); |
| 160 |
green = ((unsigned int)pixelPtr->Green >> greenAdjust); |
| 161 |
blue = ((unsigned int)pixelPtr->Blue >> blueAdjust); |
| 162 |
|
| 163 |
|
| 164 |
red = (red << redMaskShift) & visualPtr->red_mask; |
| 165 |
green = (green << greenMaskShift) & visualPtr->green_mask; |
| 166 |
blue = (blue << blueMaskShift) & visualPtr->blue_mask; |
| 167 |
return (red | green | blue); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
static INLINE unsigned int |
| 185 |
DirectColorPixel(colorTabPtr, pixelPtr) |
| 186 |
struct ColorTableStruct *colorTabPtr; |
| 187 |
Pix32 *pixelPtr; |
| 188 |
{ |
| 189 |
unsigned int red, green, blue; |
| 190 |
|
| 191 |
red = colorTabPtr->red[pixelPtr->Red]; |
| 192 |
green = colorTabPtr->green[pixelPtr->Green]; |
| 193 |
blue = colorTabPtr->blue[pixelPtr->Blue]; |
| 194 |
return (red | green | blue); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
static INLINE unsigned int |
| 212 |
PseudoColorPixel(pixelPtr, lut) |
| 213 |
Pix32 *pixelPtr; |
| 214 |
unsigned int *lut; |
| 215 |
{ |
| 216 |
int red, green, blue; |
| 217 |
int pixel; |
| 218 |
|
| 219 |
red = (pixelPtr->Red >> 3) + 1; |
| 220 |
green = (pixelPtr->Green >> 3) + 1; |
| 221 |
blue = (pixelPtr->Blue >> 3) + 1; |
| 222 |
pixel = RGBIndex(red, green, blue); |
| 223 |
return lut[pixel]; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
Pixmap |
| 241 |
Blt_ColorImageToPixmap(interp, tkwin, image, colorTablePtr) |
| 242 |
Tcl_Interp *interp; |
| 243 |
Tk_Window tkwin; |
| 244 |
Blt_ColorImage image; |
| 245 |
ColorTable *colorTablePtr; |
| 246 |
{ |
| 247 |
Display *display; |
| 248 |
int width, height; |
| 249 |
Pixmap pixmap; |
| 250 |
GC pixmapGC; |
| 251 |
Visual *visualPtr; |
| 252 |
XImage *imagePtr; |
| 253 |
int nPixels; |
| 254 |
|
| 255 |
visualPtr = Tk_Visual(tkwin); |
| 256 |
width = Blt_ColorImageWidth(image); |
| 257 |
height = Blt_ColorImageHeight(image); |
| 258 |
display = Tk_Display(tkwin); |
| 259 |
|
| 260 |
ComputeMasks(visualPtr); |
| 261 |
|
| 262 |
*colorTablePtr = NULL; |
| 263 |
imagePtr = XCreateImage(Tk_Display(tkwin), visualPtr, Tk_Depth(tkwin), |
| 264 |
ZPixmap, 0, (char *)NULL, width, height, 32, 0); |
| 265 |
assert(imagePtr); |
| 266 |
|
| 267 |
nPixels = width * height; |
| 268 |
imagePtr->data = Blt_Malloc(sizeof(Pix32) * nPixels); |
| 269 |
assert(imagePtr->data); |
| 270 |
|
| 271 |
imagePtr->byte_order = MSBFirst; |
| 272 |
imagePtr->bitmap_bit_order = imagePtr->byte_order; |
| 273 |
imagePtr->bytes_per_line = width * sizeof(Pix32); |
| 274 |
|
| 275 |
switch (visualPtr->class) { |
| 276 |
case TrueColor: |
| 277 |
{ |
| 278 |
register int x, y; |
| 279 |
register Pix32 *srcPtr; |
| 280 |
register char *destPtr; |
| 281 |
unsigned int pixel; |
| 282 |
int rowOffset; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
srcPtr = Blt_ColorImageBits(image); |
| 288 |
rowOffset = 0; |
| 289 |
for (y = 0; y < height; y++) { |
| 290 |
destPtr = imagePtr->data + rowOffset; |
| 291 |
for (x = 0; x < width; x++, srcPtr++) { |
| 292 |
pixel = TrueColorPixel(visualPtr, srcPtr); |
| 293 |
switch (imagePtr->bits_per_pixel) { |
| 294 |
case 32: |
| 295 |
*destPtr++ = (pixel >> 24) & 0xFF; |
| 296 |
|
| 297 |
case 24: |
| 298 |
*destPtr++ = (pixel >> 16) & 0xFF; |
| 299 |
|
| 300 |
case 16: |
| 301 |
*destPtr++ = (pixel >> 8) & 0xFF; |
| 302 |
|
| 303 |
case 8: |
| 304 |
*destPtr++ = pixel & 0xFF; |
| 305 |
|
| 306 |
} |
| 307 |
} |
| 308 |
rowOffset += imagePtr->bytes_per_line; |
| 309 |
} |
| 310 |
} |
| 311 |
break; |
| 312 |
|
| 313 |
case DirectColor: |
| 314 |
{ |
| 315 |
register int x, y; |
| 316 |
register Pix32 *srcPtr; |
| 317 |
register char *destPtr; |
| 318 |
unsigned int pixel; |
| 319 |
int rowOffset; |
| 320 |
struct ColorTableStruct *colorTabPtr; |
| 321 |
|
| 322 |
|
| 323 |
colorTabPtr = Blt_DirectColorTable(interp, tkwin, image); |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
srcPtr = Blt_ColorImageBits(image); |
| 329 |
rowOffset = 0; |
| 330 |
for (y = 0; y < height; y++) { |
| 331 |
destPtr = imagePtr->data + rowOffset; |
| 332 |
for (x = 0; x < width; x++, srcPtr++) { |
| 333 |
pixel = DirectColorPixel(colorTabPtr, srcPtr); |
| 334 |
switch (imagePtr->bits_per_pixel) { |
| 335 |
case 32: |
| 336 |
*destPtr++ = (pixel >> 24) & 0xFF; |
| 337 |
|
| 338 |
case 24: |
| 339 |
*destPtr++ = (pixel >> 16) & 0xFF; |
| 340 |
|
| 341 |
case 16: |
| 342 |
*destPtr++ = (pixel >> 8) & 0xFF; |
| 343 |
|
| 344 |
case 8: |
| 345 |
*destPtr++ = pixel & 0xFF; |
| 346 |
|
| 347 |
} |
| 348 |
} |
| 349 |
rowOffset += imagePtr->bytes_per_line; |
| 350 |
} |
| 351 |
*colorTablePtr = colorTabPtr; |
| 352 |
} |
| 353 |
break; |
| 354 |
|
| 355 |
case GrayScale: |
| 356 |
case StaticGray: |
| 357 |
case PseudoColor: |
| 358 |
case StaticColor: |
| 359 |
{ |
| 360 |
register int x, y; |
| 361 |
register Pix32 *srcPtr; |
| 362 |
register char *destPtr; |
| 363 |
unsigned int pixel; |
| 364 |
int rowOffset; |
| 365 |
struct ColorTableStruct *colorTabPtr; |
| 366 |
|
| 367 |
colorTabPtr = Blt_PseudoColorTable(interp, tkwin, image); |
| 368 |
|
| 369 |
srcPtr = Blt_ColorImageBits(image); |
| 370 |
rowOffset = 0; |
| 371 |
for (y = 0; y < height; y++) { |
| 372 |
destPtr = imagePtr->data + rowOffset; |
| 373 |
for (x = 0; x < width; x++, srcPtr++) { |
| 374 |
pixel = PseudoColorPixel(srcPtr, colorTabPtr->lut); |
| 375 |
switch (imagePtr->bits_per_pixel) { |
| 376 |
case 32: |
| 377 |
*destPtr++ = (pixel >> 24) & 0xFF; |
| 378 |
|
| 379 |
case 24: |
| 380 |
*destPtr++ = (pixel >> 16) & 0xFF; |
| 381 |
|
| 382 |
case 16: |
| 383 |
*destPtr++ = (pixel >> 8) & 0xFF; |
| 384 |
|
| 385 |
case 8: |
| 386 |
*destPtr++ = pixel & 0xFF; |
| 387 |
|
| 388 |
} |
| 389 |
} |
| 390 |
rowOffset += imagePtr->bytes_per_line; |
| 391 |
} |
| 392 |
Blt_Free(colorTabPtr->lut); |
| 393 |
*colorTablePtr = colorTabPtr; |
| 394 |
} |
| 395 |
break; |
| 396 |
default: |
| 397 |
return None; |
| 398 |
} |
| 399 |
pixmapGC = Tk_GetGC(tkwin, 0L, (XGCValues *)NULL); |
| 400 |
pixmap = Tk_GetPixmap(display, Tk_WindowId(tkwin), width, height, |
| 401 |
Tk_Depth(tkwin)); |
| 402 |
XPutImage(display, pixmap, pixmapGC, imagePtr, 0, 0, 0, 0, width, height); |
| 403 |
XDestroyImage(imagePtr); |
| 404 |
Tk_FreeGC(display, pixmapGC); |
| 405 |
return pixmap; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
static int |
| 410 |
XGetImageErrorProc(clientData, errEventPtr) |
| 411 |
ClientData clientData; |
| 412 |
XErrorEvent *errEventPtr; |
| 413 |
{ |
| 414 |
int *errorPtr = clientData; |
| 415 |
|
| 416 |
*errorPtr = TCL_ERROR; |
| 417 |
return 0; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
Blt_ColorImage |
| 447 |
Blt_DrawableToColorImage(tkwin, drawable, x, y, width, height, inputGamma) |
| 448 |
Tk_Window tkwin; |
| 449 |
Drawable drawable; |
| 450 |
register int x, y; |
| 451 |
|
| 452 |
int width, height; |
| 453 |
|
| 454 |
|
| 455 |
double inputGamma; |
| 456 |
{ |
| 457 |
XImage *imagePtr; |
| 458 |
Blt_ColorImage image; |
| 459 |
register Pix32 *destPtr; |
| 460 |
unsigned long pixel; |
| 461 |
int result = TCL_OK; |
| 462 |
Tk_ErrorHandler errHandler; |
| 463 |
Visual *visualPtr; |
| 464 |
unsigned char lut[256]; |
| 465 |
|
| 466 |
errHandler = Tk_CreateErrorHandler(Tk_Display(tkwin), BadMatch, |
| 467 |
X_GetImage, -1, XGetImageErrorProc, &result); |
| 468 |
imagePtr = XGetImage(Tk_Display(tkwin), drawable, x, y, width, height, |
| 469 |
AllPlanes, ZPixmap); |
| 470 |
Tk_DeleteErrorHandler(errHandler); |
| 471 |
XSync(Tk_Display(tkwin), False); |
| 472 |
if (result != TCL_OK) { |
| 473 |
return NULL; |
| 474 |
} |
| 475 |
|
| 476 |
{ |
| 477 |
register int i; |
| 478 |
double value; |
| 479 |
|
| 480 |
for (i = 0; i < 256; i++) { |
| 481 |
value = pow(i / 255.0, inputGamma) * 255.0 + 0.5; |
| 482 |
lut[i] = (unsigned char)CLAMP(value); |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
image = Blt_CreateColorImage(width, height); |
| 489 |
visualPtr = Tk_Visual(tkwin); |
| 490 |
if (visualPtr->class == TrueColor) { |
| 491 |
unsigned int red, green, blue; |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
ComputeMasks(visualPtr); |
| 497 |
destPtr = Blt_ColorImageBits(image); |
| 498 |
for (y = 0; y < height; y++) { |
| 499 |
for (x = 0; x < width; x++) { |
| 500 |
pixel = XGetPixel(imagePtr, x, y); |
| 501 |
|
| 502 |
red = ((pixel & visualPtr->red_mask) >> redMaskShift) << redAdjust; |
| 503 |
green = ((pixel & visualPtr->green_mask) >> greenMaskShift) << greenAdjust; |
| 504 |
blue = ((pixel & visualPtr->blue_mask) >> blueMaskShift) << blueAdjust; |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
destPtr->Red = lut[red]; |
| 515 |
destPtr->Green = lut[green]; |
| 516 |
destPtr->Blue = lut[blue]; |
| 517 |
destPtr->Alpha = (unsigned char)-1; |
| 518 |
destPtr++; |
| 519 |
} |
| 520 |
} |
| 521 |
XDestroyImage(imagePtr); |
| 522 |
} else { |
| 523 |
Blt_HashEntry *hPtr; |
| 524 |
Blt_HashSearch cursor; |
| 525 |
Blt_HashTable pixelTable; |
| 526 |
XColor *colorPtr, *colorArr; |
| 527 |
Pix32 *endPtr; |
| 528 |
int nPixels; |
| 529 |
int nColors; |
| 530 |
int isNew; |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
nPixels = width * height; |
| 537 |
Blt_InitHashTableWithPool(&pixelTable, BLT_ONE_WORD_KEYS); |
| 538 |
destPtr = Blt_ColorImageBits(image); |
| 539 |
for (y = 0; y < height; y++) { |
| 540 |
for (x = 0; x < width; x++) { |
| 541 |
pixel = XGetPixel(imagePtr, x, y); |
| 542 |
hPtr = Blt_CreateHashEntry(&pixelTable, (char *)pixel, &isNew); |
| 543 |
if (isNew) { |
| 544 |
Blt_SetHashValue(hPtr, (char *)pixel); |
| 545 |
} |
| 546 |
destPtr->value = pixel; |
| 547 |
destPtr++; |
| 548 |
} |
| 549 |
} |
| 550 |
XDestroyImage(imagePtr); |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
nColors = pixelTable.numEntries; |
| 558 |
colorArr = Blt_Malloc(sizeof(XColor) * nColors); |
| 559 |
assert(colorArr); |
| 560 |
|
| 561 |
colorPtr = colorArr; |
| 562 |
for (hPtr = Blt_FirstHashEntry(&pixelTable, &cursor); hPtr != NULL; |
| 563 |
hPtr = Blt_NextHashEntry(&cursor)) { |
| 564 |
colorPtr->pixel = (unsigned long)Blt_GetHashValue(hPtr); |
| 565 |
Blt_SetHashValue(hPtr, (char *)colorPtr); |
| 566 |
colorPtr++; |
| 567 |
} |
| 568 |
XQueryColors(Tk_Display(tkwin), Tk_Colormap(tkwin), colorArr, nColors); |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
destPtr = Blt_ColorImageBits(image); |
| 575 |
endPtr = destPtr + nPixels; |
| 576 |
for (; destPtr < endPtr; destPtr++) { |
| •577 |
hPtr = Blt_FindHashEntry(&pixelTable, (char *)(intptr_t)destPtr->value); |
| 578 |
colorPtr = (XColor *)Blt_GetHashValue(hPtr); |
| 579 |
destPtr->Red = lut[colorPtr->red >> 8]; |
| 580 |
destPtr->Green = lut[colorPtr->green >> 8]; |
| 581 |
destPtr->Blue = lut[colorPtr->blue >> 8]; |
| 582 |
destPtr->Alpha = (unsigned char)-1; |
| 583 |
} |
| 584 |
Blt_Free(colorArr); |
| 585 |
Blt_DeleteHashTable(&pixelTable); |
| 586 |
} |
| 587 |
return image; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
Pixmap |
| 592 |
Blt_PhotoImageMask(tkwin, src) |
| 593 |
Tk_Window tkwin; |
| 594 |
Tk_PhotoImageBlock src; |
| 595 |
{ |
| 596 |
Pixmap bitmap; |
| 597 |
int arraySize, bytes_per_line; |
| 598 |
int offset, count; |
| 599 |
int value, bitMask; |
| 600 |
register int x, y; |
| 601 |
unsigned char *bits; |
| 602 |
unsigned char *srcPtr; |
| 603 |
unsigned char *destPtr; |
| 604 |
unsigned long pixel; |
| 605 |
|
| 606 |
bytes_per_line = (src.width + 7) / 8; |
| 607 |
arraySize = src.height * bytes_per_line; |
| 608 |
bits = Blt_Malloc(sizeof(unsigned char) * arraySize); |
| 609 |
assert(bits); |
| 610 |
destPtr = bits; |
| 611 |
offset = count = 0; |
| 612 |
for (y = 0; y < src.height; y++) { |
| 613 |
value = 0, bitMask = 1; |
| 614 |
srcPtr = src.pixelPtr + offset; |
| 615 |
for (x = 0; x < src.width; ) { |
| 616 |
pixel = (srcPtr[src.offset[3]] != 0x00); |
| 617 |
if (pixel) { |
| 618 |
value |= bitMask; |
| 619 |
} else { |
| 620 |
count++; |
| 621 |
} |
| 622 |
bitMask <<= 1; |
| 623 |
x++; |
| 624 |
if (!(x & 7)) { |
| 625 |
*destPtr++ = (unsigned char)value; |
| 626 |
value = 0, bitMask = 1; |
| 627 |
} |
| 628 |
srcPtr += src.pixelSize; |
| 629 |
} |
| 630 |
if (x & 7) { |
| 631 |
*destPtr++ = (unsigned char)value; |
| 632 |
} |
| 633 |
offset += src.pitch; |
| 634 |
} |
| 635 |
if (count > 0) { |
| 636 |
Tk_MakeWindowExist(tkwin); |
| 637 |
bitmap = XCreateBitmapFromData(Tk_Display(tkwin), Tk_WindowId(tkwin), |
| 638 |
(char *)bits, (unsigned int)src.width, (unsigned int)src.height); |
| 639 |
} else { |
| 640 |
bitmap = None; |
| 641 |
} |
| 642 |
Blt_Free(bits); |
| 643 |
return bitmap; |
| 644 |
} |
| 645 |
|
| 646 |
Pixmap |
| 647 |
Blt_ColorImageMask(tkwin, image) |
| 648 |
Tk_Window tkwin; |
| 649 |
Blt_ColorImage image; |
| 650 |
{ |
| 651 |
Pixmap bitmap; |
| 652 |
int arraySize, bytes_per_line; |
| 653 |
int count; |
| 654 |
int value, bitMask; |
| 655 |
register int x, y; |
| 656 |
unsigned char *bits; |
| 657 |
Pix32 *srcPtr; |
| 658 |
unsigned char *destPtr; |
| 659 |
unsigned long pixel; |
| 660 |
int width, height; |
| 661 |
|
| 662 |
width = Blt_ColorImageWidth(image); |
| 663 |
height = Blt_ColorImageHeight(image); |
| 664 |
bytes_per_line = (width + 7) / 8; |
| 665 |
arraySize = height * bytes_per_line; |
| 666 |
bits = Blt_Malloc(sizeof(unsigned char) * arraySize); |
| 667 |
assert(bits); |
| 668 |
destPtr = bits; |
| 669 |
count = 0; |
| 670 |
srcPtr = Blt_ColorImageBits(image); |
| 671 |
for (y = 0; y < height; y++) { |
| 672 |
value = 0, bitMask = 1; |
| 673 |
for (x = 0; x < width; ) { |
| 674 |
pixel = (srcPtr->Alpha != 0x00); |
| 675 |
if (pixel) { |
| 676 |
value |= bitMask; |
| 677 |
} else { |
| 678 |
count++; |
| 679 |
} |
| 680 |
bitMask <<= 1; |
| 681 |
x++; |
| 682 |
if (!(x & 7)) { |
| 683 |
*destPtr++ = (unsigned char)value; |
| 684 |
value = 0, bitMask = 1; |
| 685 |
} |
| 686 |
srcPtr++; |
| 687 |
} |
| 688 |
if (x & 7) { |
| 689 |
*destPtr++ = (unsigned char)value; |
| 690 |
} |
| 691 |
} |
| 692 |
if (count > 0) { |
| 693 |
Tk_MakeWindowExist(tkwin); |
| 694 |
bitmap = XCreateBitmapFromData(Tk_Display(tkwin), Tk_WindowId(tkwin), |
| 695 |
(char *)bits, (unsigned int)width, (unsigned int)height); |
| 696 |
} else { |
| 697 |
bitmap = None; |
| 698 |
} |
| 699 |
Blt_Free(bits); |
| 700 |
return bitmap; |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
Pixmap |
| 718 |
Blt_RotateBitmap(tkwin, srcBitmap, srcWidth, srcHeight, theta, |
| 719 |
destWidthPtr, destHeightPtr) |
| 720 |
Tk_Window tkwin; |
| 721 |
Pixmap srcBitmap; |
| 722 |
int srcWidth, srcHeight; |
| 723 |
double theta; |
| 724 |
int *destWidthPtr, *destHeightPtr; |
| 725 |
{ |
| 726 |
Display *display; |
| 727 |
Window root; |
| 728 |
Pixmap destBitmap; |
| 729 |
int destWidth, destHeight; |
| 730 |
XImage *src, *dest; |
| 731 |
register int x, y; |
| 732 |
register int sx, sy; |
| 733 |
unsigned long pixel; |
| 734 |
GC bitmapGC; |
| 735 |
double rotWidth, rotHeight; |
| 736 |
|
| 737 |
display = Tk_Display(tkwin); |
| 738 |
root = RootWindow(Tk_Display(tkwin), Tk_ScreenNumber(tkwin)); |
| 739 |
|
| 740 |
|
| 741 |
Blt_GetBoundingBox(srcWidth, srcHeight, theta, &rotWidth, &rotHeight, |
| 742 |
(Point2D *)NULL); |
| 743 |
destWidth = ROUND(rotWidth); |
| 744 |
destHeight = ROUND(rotHeight); |
| 745 |
destBitmap = Tk_GetPixmap(display, root, destWidth, destHeight, 1); |
| 746 |
bitmapGC = Blt_GetBitmapGC(tkwin); |
| 747 |
XSetForeground(display, bitmapGC, 0x0); |
| 748 |
XFillRectangle(display, destBitmap, bitmapGC, 0, 0, destWidth, destHeight); |
| 749 |
|
| 750 |
src = XGetImage(display, srcBitmap, 0, 0, srcWidth, srcHeight, 1, ZPixmap); |
| 751 |
dest = XGetImage(display, destBitmap, 0, 0, destWidth, destHeight, 1, |
| 752 |
ZPixmap); |
| 753 |
theta = FMOD(theta, 360.0); |
| 754 |
if (FMOD(theta, (double)90.0) == 0.0) { |
| 755 |
int quadrant; |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
quadrant = (int)(theta / 90.0); |
| 760 |
switch (quadrant) { |
| 761 |
case ROTATE_270: |
| 762 |
for (y = 0; y < destHeight; y++) { |
| 763 |
sx = y; |
| 764 |
for (x = 0; x < destWidth; x++) { |
| 765 |
sy = destWidth - x - 1; |
| 766 |
pixel = XGetPixel(src, sx, sy); |
| 767 |
if (pixel) { |
| 768 |
XPutPixel(dest, x, y, pixel); |
| 769 |
} |
| 770 |
} |
| 771 |
} |
| 772 |
break; |
| 773 |
|
| 774 |
case ROTATE_180: |
| 775 |
for (y = 0; y < destHeight; y++) { |
| 776 |
sy = destHeight - y - 1; |
| 777 |
for (x = 0; x < destWidth; x++) { |
| 778 |
sx = destWidth - x - 1, |
| 779 |
pixel = XGetPixel(src, sx, sy); |
| 780 |
if (pixel) { |
| 781 |
XPutPixel(dest, x, y, pixel); |
| 782 |
} |
| 783 |
} |
| 784 |
} |
| 785 |
break; |
| 786 |
|
| 787 |
case ROTATE_90: |
| 788 |
for (y = 0; y < destHeight; y++) { |
| 789 |
sx = destHeight - y - 1; |
| 790 |
for (x = 0; x < destWidth; x++) { |
| 791 |
sy = x; |
| 792 |
pixel = XGetPixel(src, sx, sy); |
| 793 |
if (pixel) { |
| 794 |
XPutPixel(dest, x, y, pixel); |
| 795 |
} |
| 796 |
} |
| 797 |
} |
| 798 |
break; |
| 799 |
|
| 800 |
case ROTATE_0: |
| 801 |
for (y = 0; y < destHeight; y++) { |
| 802 |
for (x = 0; x < destWidth; x++) { |
| 803 |
pixel = XGetPixel(src, x, y); |
| 804 |
if (pixel) { |
| 805 |
XPutPixel(dest, x, y, pixel); |
| 806 |
} |
| 807 |
} |
| 808 |
} |
| 809 |
break; |
| 810 |
|
| 811 |
default: |
| 812 |
|
| 813 |
break; |
| 814 |
} |
| 815 |
} else { |
| 816 |
double radians, sinTheta, cosTheta; |
| 817 |
double sox, soy; |
| 818 |
|
| 819 |
double destCX, destCY; |
| 820 |
|
| 821 |
double tx, ty; |
| 822 |
double rx, ry; |
| 823 |
|
| 824 |
radians = (theta / 180.0) * M_PI; |
| 825 |
sinTheta = sin(radians), cosTheta = cos(radians); |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
sox = srcWidth * 0.5; |
| 831 |
soy = srcHeight * 0.5; |
| 832 |
destCX = destWidth * 0.5; |
| 833 |
destCY = destHeight * 0.5; |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
for (y = 0; y < destHeight; y++) { |
| 839 |
ty = y - destCY; |
| 840 |
for (x = 0; x < destWidth; x++) { |
| 841 |
|
| 842 |
|
| 843 |
tx = x - destCX; |
| 844 |
|
| 845 |
|
| 846 |
rx = (tx * cosTheta) - (ty * sinTheta); |
| 847 |
ry = (tx * sinTheta) + (ty * cosTheta); |
| 848 |
|
| 849 |
|
| 850 |
rx += sox; |
| 851 |
ry += soy; |
| 852 |
|
| 853 |
sx = ROUND(rx); |
| 854 |
sy = ROUND(ry); |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
if ((sx >= srcWidth) || (sx < 0) || (sy >= srcHeight) || |
| 862 |
(sy < 0)) { |
| 863 |
continue; |
| 864 |
} |
| 865 |
pixel = XGetPixel(src, sx, sy); |
| 866 |
if (pixel) { |
| 867 |
XPutPixel(dest, x, y, pixel); |
| 868 |
} |
| 869 |
} |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
XPutImage(display, destBitmap, bitmapGC, dest, 0, 0, 0, 0, destWidth, |
| 874 |
destHeight); |
| 875 |
|
| 876 |
|
| 877 |
XDestroyImage(src), XDestroyImage(dest); |
| 878 |
*destWidthPtr = destWidth; |
| 879 |
*destHeightPtr = destHeight; |
| 880 |
return destBitmap; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
Pixmap |
| 904 |
Blt_ScaleBitmap(tkwin, srcBitmap, srcWidth, srcHeight, destWidth, destHeight) |
| 905 |
Tk_Window tkwin; |
| 906 |
Pixmap srcBitmap; |
| 907 |
int srcWidth, srcHeight, destWidth, destHeight; |
| 908 |
{ |
| 909 |
Display *display; |
| 910 |
GC bitmapGC; |
| 911 |
Pixmap destBitmap; |
| 912 |
Window root; |
| 913 |
XImage *src, *dest; |
| 914 |
double xScale, yScale; |
| 915 |
register int sx, sy; |
| 916 |
register int x, y; |
| 917 |
unsigned long pixel; |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
display = Tk_Display(tkwin); |
| 922 |
|
| 923 |
root = RootWindow(Tk_Display(tkwin), Tk_ScreenNumber(tkwin)); |
| 924 |
destBitmap = Tk_GetPixmap(display, root, destWidth, destHeight, 1); |
| 925 |
bitmapGC = Blt_GetBitmapGC(tkwin); |
| 926 |
XSetForeground(display, bitmapGC, 0x0); |
| 927 |
XFillRectangle(display, destBitmap, bitmapGC, 0, 0, destWidth, destHeight); |
| 928 |
|
| 929 |
src = XGetImage(display, srcBitmap, 0, 0, srcWidth, srcHeight, 1, ZPixmap); |
| 930 |
dest = XGetImage(display, destBitmap, 0, 0, destWidth, destHeight, 1, |
| 931 |
ZPixmap); |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
xScale = (double)srcWidth / (double)destWidth; |
| 939 |
yScale = (double)srcHeight / (double)destHeight; |
| 940 |
|
| 941 |
|
| 942 |
for (y = 0; y < destHeight; y++) { |
| 943 |
sy = (int)(yScale * (double)y); |
| 944 |
for (x = 0; x < destWidth; x++) { |
| 945 |
sx = (int)(xScale * (double)x); |
| 946 |
pixel = XGetPixel(src, sx, sy); |
| 947 |
if (pixel) { |
| 948 |
XPutPixel(dest, x, y, pixel); |
| 949 |
} |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
|
| 954 |
XPutImage(display, destBitmap, bitmapGC, dest, 0, 0, 0, 0, |
| 955 |
destWidth, destHeight); |
| 956 |
XDestroyImage(src), XDestroyImage(dest); |
| 957 |
return destBitmap; |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
Pixmap |
| 988 |
Blt_ScaleRotateBitmapRegion( |
| 989 |
Tk_Window tkwin, |
| 990 |
Pixmap srcBitmap, |
| 991 |
unsigned int srcWidth, |
| 992 |
unsigned int srcHeight, |
| 993 |
int regionX, |
| 994 |
int regionY, |
| 995 |
|
| 996 |
unsigned int regionWidth, |
| 997 |
unsigned int regionHeight, |
| 998 |
unsigned int destWidth, |
| 999 |
unsigned int destHeight, |
| 1000 |
double theta) |
| 1001 |
{ |
| 1002 |
Display *display; |
| 1003 |
Window root; |
| 1004 |
Pixmap destBitmap; |
| 1005 |
XImage *src, *dest; |
| 1006 |
register int x, y; |
| 1007 |
register int sx, sy; |
| 1008 |
unsigned long pixel; |
| 1009 |
double xScale, yScale; |
| 1010 |
double rotWidth, rotHeight; |
| 1011 |
GC bitmapGC; |
| 1012 |
|
| 1013 |
display = Tk_Display(tkwin); |
| 1014 |
root = RootWindow(Tk_Display(tkwin), Tk_ScreenNumber(tkwin)); |
| 1015 |
|
| 1016 |
|
| 1017 |
bitmapGC = Blt_GetBitmapGC(tkwin); |
| 1018 |
destBitmap = Tk_GetPixmap(display, root, regionWidth, regionHeight, 1); |
| 1019 |
XSetForeground(display, bitmapGC, 0x0); |
| 1020 |
XFillRectangle(display, destBitmap, bitmapGC, 0, 0, regionWidth, |
| 1021 |
regionHeight); |
| 1022 |
|
| 1023 |
src = XGetImage(display, srcBitmap, 0, 0, srcWidth, srcHeight, 1, ZPixmap); |
| 1024 |
dest = XGetImage(display, destBitmap, 0, 0, regionWidth, regionHeight, 1, |
| 1025 |
ZPixmap); |
| 1026 |
theta = FMOD(theta, 360.0); |
| 1027 |
|
| 1028 |
Blt_GetBoundingBox(srcWidth, srcHeight, theta, &rotWidth, &rotHeight, |
| 1029 |
(Point2D *)NULL); |
| 1030 |
xScale = rotWidth / (double)destWidth; |
| 1031 |
yScale = rotHeight / (double)destHeight; |
| 1032 |
|
| 1033 |
if (FMOD(theta, (double)90.0) == 0.0) { |
| 1034 |
int quadrant; |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
quadrant = (int)(theta / 90.0); |
| 1039 |
switch (quadrant) { |
| 1040 |
case ROTATE_270: |
| 1041 |
for (y = 0; y < regionHeight; y++) { |
| 1042 |
sx = (int)(yScale * (double)(y + regionY)); |
| 1043 |
for (x = 0; x < regionWidth; x++) { |
| 1044 |
sy = (int)(xScale *(double)(destWidth - (x + regionX) - 1)); |
| 1045 |
pixel = XGetPixel(src, sx, sy); |
| 1046 |
if (pixel) { |
| 1047 |
XPutPixel(dest, x, y, pixel); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |
| 1051 |
break; |
| 1052 |
|
| 1053 |
case ROTATE_180: |
| 1054 |
for (y = 0; y < regionHeight; y++) { |
| 1055 |
sy = (int)(yScale * (double)(destHeight - (y + regionY) - 1)); |
| 1056 |
for (x = 0; x < regionWidth; x++) { |
| 1057 |
sx = (int)(xScale *(double)(destWidth - (x + regionX) - 1)); |
| 1058 |
pixel = XGetPixel(src, sx, sy); |
| 1059 |
if (pixel) { |
| 1060 |
XPutPixel(dest, x, y, pixel); |
| 1061 |
} |
| 1062 |
} |
| 1063 |
} |
| 1064 |
break; |
| 1065 |
|
| 1066 |
case ROTATE_90: |
| 1067 |
for (y = 0; y < regionHeight; y++) { |
| 1068 |
sx = (int)(yScale * (double)(destHeight - (y + regionY) - 1)); |
| 1069 |
for (x = 0; x < regionWidth; x++) { |
| 1070 |
sy = (int)(xScale * (double)(x + regionX)); |
| 1071 |
pixel = XGetPixel(src, sx, sy); |
| 1072 |
if (pixel) { |
| 1073 |
XPutPixel(dest, x, y, pixel); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
} |
| 1077 |
break; |
| 1078 |
|
| 1079 |
case ROTATE_0: |
| 1080 |
for (y = 0; y < regionHeight; y++) { |
| 1081 |
sy = (int)(yScale * (double)(y + regionY)); |
| 1082 |
for (x = 0; x < regionWidth; x++) { |
| 1083 |
sx = (int)(xScale * (double)(x + regionX)); |
| 1084 |
pixel = XGetPixel(src, sx, sy); |
| 1085 |
if (pixel) { |
| 1086 |
XPutPixel(dest, x, y, pixel); |
| 1087 |
} |
| 1088 |
} |
| 1089 |
} |
| 1090 |
break; |
| 1091 |
|
| 1092 |
default: |
| 1093 |
|
| 1094 |
break; |
| 1095 |
} |
| 1096 |
} else { |
| 1097 |
double radians, sinTheta, cosTheta; |
| 1098 |
double sox, soy; |
| 1099 |
|
| 1100 |
double rox, roy; |
| 1101 |
|
| 1102 |
double tx, ty; |
| 1103 |
double rx, ry; |
| 1104 |
|
| 1105 |
radians = (theta / 180.0) * M_PI; |
| 1106 |
sinTheta = sin(radians), cosTheta = cos(radians); |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
sox = srcWidth * 0.5; |
| 1112 |
soy = srcHeight * 0.5; |
| 1113 |
rox = rotWidth * 0.5; |
| 1114 |
roy = rotHeight * 0.5; |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
for (y = 0; y < regionHeight; y++) { |
| 1120 |
ty = (yScale * (double)(y + regionY)) - roy; |
| 1121 |
for (x = 0; x < regionWidth; x++) { |
| 1122 |
|
| 1123 |
|
| 1124 |
tx = (xScale * (double)(x + regionX)) - rox; |
| 1125 |
|
| 1126 |
|
| 1127 |
rx = (tx * cosTheta) - (ty * sinTheta); |
| 1128 |
ry = (tx * sinTheta) + (ty * cosTheta); |
| 1129 |
|
| 1130 |
|
| 1131 |
rx += sox; |
| 1132 |
ry += soy; |
| 1133 |
|
| 1134 |
sx = ROUND(rx); |
| 1135 |
sy = ROUND(ry); |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
if ((sx >= srcWidth) || (sx < 0) || (sy >= srcHeight) || |
| 1143 |
(sy < 0)) { |
| 1144 |
continue; |
| 1145 |
} |
| 1146 |
pixel = XGetPixel(src, sx, sy); |
| 1147 |
if (pixel) { |
| 1148 |
XPutPixel(dest, x, y, pixel); |
| 1149 |
} |
| 1150 |
} |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
XPutImage(display, destBitmap, bitmapGC, dest, 0, 0, 0, 0, regionWidth, |
| 1155 |
regionHeight); |
| 1156 |
|
| 1157 |
|
| 1158 |
XDestroyImage(src), XDestroyImage(dest); |
| 1159 |
return destBitmap; |
| 1160 |
|
| 1161 |
} |
| 1162 |
|
| 1163 |
#if HAVE_JPEGLIB_H |
| 1164 |
|
| 1165 |
#undef HAVE_STDLIB_H |
| 1166 |
#undef EXTERN |
| 1167 |
#ifdef WIN32 |
| 1168 |
#define XMD_H 1 |
| 1169 |
#endif |
| 1170 |
#include "jpeglib.h" |
| 1171 |
#include <setjmp.h> |
| 1172 |
|
| 1173 |
typedef struct { |
| 1174 |
struct jpeg_error_mgr pub; |
| 1175 |
jmp_buf jmpBuf; |
| 1176 |
Tcl_DString dString; |
| 1177 |
} ReaderHandler; |
| 1178 |
|
| 1179 |
static void ErrorProc _ANSI_ARGS_((j_common_ptr jpegInfo)); |
| 1180 |
static void MessageProc _ANSI_ARGS_((j_common_ptr jpegInfo)); |
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
static void |
| 1187 |
ErrorProc(jpgPtr) |
| 1188 |
j_common_ptr jpgPtr; |
| 1189 |
{ |
| 1190 |
ReaderHandler *handlerPtr = (ReaderHandler *)jpgPtr->err; |
| 1191 |
|
| 1192 |
(*handlerPtr->pub.output_message) (jpgPtr); |
| 1193 |
longjmp(handlerPtr->jmpBuf, 1); |
| 1194 |
} |
| 1195 |
|
| 1196 |
static void |
| 1197 |
MessageProc(jpgPtr) |
| 1198 |
j_common_ptr jpgPtr; |
| 1199 |
{ |
| 1200 |
ReaderHandler *handlerPtr = (ReaderHandler *)jpgPtr->err; |
| 1201 |
char buffer[JMSG_LENGTH_MAX]; |
| 1202 |
|
| 1203 |
|
| 1204 |
(*handlerPtr->pub.format_message) (jpgPtr, buffer); |
| 1205 |
Tcl_DStringAppend(&(handlerPtr->dString), " ", -1); |
| 1206 |
Tcl_DStringAppend(&(handlerPtr->dString), buffer, -1); |
| 1207 |
} |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
Blt_ColorImage |
| 1223 |
Blt_JPEGToColorImage(interp, fileName) |
| 1224 |
Tcl_Interp *interp; |
| 1225 |
char *fileName; |
| 1226 |
{ |
| 1227 |
struct jpeg_decompress_struct jpg; |
| 1228 |
Blt_ColorImage image; |
| 1229 |
unsigned int imageWidth, imageHeight; |
| 1230 |
register Pix32 *destPtr; |
| 1231 |
ReaderHandler handler; |
| 1232 |
FILE *f; |
| 1233 |
JSAMPLE **readBuffer; |
| 1234 |
int row_stride; |
| 1235 |
register int i; |
| 1236 |
register JSAMPLE *bufPtr; |
| 1237 |
|
| 1238 |
f = fopen(fileName, "rb"); |
| 1239 |
if (f == NULL) { |
| 1240 |
Tcl_AppendResult(interp, "can't open \"", fileName, "\":", |
| 1241 |
Tcl_PosixError(interp), (char *)NULL); |
| 1242 |
return NULL; |
| 1243 |
} |
| 1244 |
image = NULL; |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
jpg.dct_method = JDCT_IFAST; |
| 1250 |
jpg.err = jpeg_std_error(&handler.pub); |
| 1251 |
handler.pub.error_exit = ErrorProc; |
| 1252 |
handler.pub.output_message = MessageProc; |
| 1253 |
|
| 1254 |
Tcl_DStringInit(&handler.dString); |
| 1255 |
Tcl_DStringAppend(&handler.dString, "error reading \"", -1); |
| 1256 |
Tcl_DStringAppend(&handler.dString, fileName, -1); |
| 1257 |
Tcl_DStringAppend(&handler.dString, "\": ", -1); |
| 1258 |
|
| 1259 |
if (setjmp(handler.jmpBuf)) { |
| 1260 |
jpeg_destroy_decompress(&jpg); |
| 1261 |
fclose(f); |
| 1262 |
Tcl_DStringResult(interp, &(handler.dString)); |
| 1263 |
return NULL; |
| 1264 |
} |
| 1265 |
jpeg_create_decompress(&jpg); |
| 1266 |
jpeg_stdio_src(&jpg, f); |
| 1267 |
|
| 1268 |
jpeg_read_header(&jpg, TRUE); |
| 1269 |
|
| 1270 |
jpeg_start_decompress(&jpg); |
| 1271 |
imageWidth = jpg.output_width; |
| 1272 |
imageHeight = jpg.output_height; |
| 1273 |
if ((imageWidth < 1) || (imageHeight < 1)) { |
| 1274 |
Tcl_AppendResult(interp, "bad JPEG image size", (char *)NULL); |
| 1275 |
fclose(f); |
| 1276 |
return NULL; |
| 1277 |
} |
| 1278 |
|
| 1279 |
row_stride = imageWidth * jpg.output_components; |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
readBuffer = (*jpg.mem->alloc_sarray) ((j_common_ptr)&jpg, JPOOL_IMAGE, |
| 1284 |
row_stride, 1); |
| 1285 |
image = Blt_CreateColorImage(imageWidth, imageHeight); |
| 1286 |
destPtr = Blt_ColorImageBits(image); |
| 1287 |
|
| 1288 |
if (jpg.output_components == 1) { |
| 1289 |
while (jpg.output_scanline < imageHeight) { |
| 1290 |
jpeg_read_scanlines(&jpg, readBuffer, 1); |
| 1291 |
bufPtr = readBuffer[0]; |
| 1292 |
for (i = 0; i < (int)imageWidth; i++) { |
| 1293 |
destPtr->Red = destPtr->Green = destPtr->Blue = *bufPtr++; |
| 1294 |
destPtr->Alpha = (unsigned char)-1; |
| 1295 |
destPtr++; |
| 1296 |
} |
| 1297 |
} |
| 1298 |
} else { |
| 1299 |
while (jpg.output_scanline < imageHeight) { |
| 1300 |
jpeg_read_scanlines(&jpg, readBuffer, 1); |
| 1301 |
bufPtr = readBuffer[0]; |
| 1302 |
for (i = 0; i < (int)imageWidth; i++) { |
| 1303 |
destPtr->Red = *bufPtr++; |
| 1304 |
destPtr->Green = *bufPtr++; |
| 1305 |
destPtr->Blue = *bufPtr++; |
| 1306 |
destPtr->Alpha = (unsigned char)-1; |
| 1307 |
destPtr++; |
| 1308 |
} |
| 1309 |
} |
| 1310 |
} |
| 1311 |
jpeg_finish_decompress(&jpg); |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
jpeg_destroy_decompress(&jpg); |
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
fclose(f); |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
if (handler.pub.num_warnings > 0) { |
| 1332 |
Tcl_SetErrorCode(interp, "IMAGE", "JPEG", |
| 1333 |
Tcl_DStringValue(&(handler.dString)), (char *)NULL); |
| 1334 |
} else { |
| 1335 |
Tcl_SetErrorCode(interp, "NONE", (char *)NULL); |
| 1336 |
} |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
Tcl_DStringFree(&(handler.dString)); |
| 1342 |
return image; |
| 1343 |
} |
| 1344 |
|
| 1345 |
#endif |