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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 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 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
|
/*
* bltUnixImage.c --
*
* This module implements image processing procedures for the BLT
* toolkit.
*
* Copyright 1997-1998 Lucent Technologies, Inc.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that the copyright notice and warranty
* disclaimer appear in supporting documentation, and that the names
* of Lucent Technologies any of their entities not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission.
*
* Lucent Technologies disclaims all warranties with regard to this
* software, including all implied warranties of merchantability and
* fitness. In no event shall Lucent Technologies be liable for any
* special, indirect or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether in
* an action of contract, negligence or other tortuous action, arising
* out of or in connection with the use or performance of this
* software.
*/
#include "bltInt.h"
#include "bltImage.h"
#include "bltHash.h"
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#define CLAMP(c) ((((c) < 0.0) ? 0.0 : ((c) > 255.0) ? 255.0 : (c)))
int redAdjust, greenAdjust, blueAdjust;
int redMaskShift, greenMaskShift, blueMaskShift;
/*
*----------------------------------------------------------------------
*
* ShiftCount --
*
* Returns the position of the least significant (low) bit in
* the given mask.
*
* For TrueColor and DirectColor visuals, a pixel value is
* formed by OR-ing the red, green, and blue colormap indices
* into a single 32-bit word. The visual's color masks tell
* you where in the word the indices are supposed to be. The
* masks contain bits only where the index is found. By counting
* the leading zeros in the mask, we know how many bits to shift
* to the individual red, green, and blue values to form a pixel.
*
* Results:
* The number of the least significant bit.
*
*----------------------------------------------------------------------
*/
static int
ShiftCount(mask)
register unsigned int mask;
{
register int count;
for (count = 0; count < 32; count++) {
if (mask & 0x01) {
break;
}
mask >>= 1;
}
return count;
}
/*
*----------------------------------------------------------------------
*
* CountBits --
*
* Returns the number of bits set in the given mask.
*
* Reference: Graphics Gems Volume 2.
*
* Results:
* The number of bits to set in the mask.
*
*
*----------------------------------------------------------------------
*/
static int
CountBits(mask)
register unsigned long mask; /* 32 1-bit tallies */
{
/* 16 2-bit tallies */
mask = (mask & 0x55555555) + ((mask >> 1) & (0x55555555));
/* 8 4-bit tallies */
mask = (mask & 0x33333333) + ((mask >> 2) & (0x33333333));
/* 4 8-bit tallies */
mask = (mask & 0x07070707) + ((mask >> 4) & (0x07070707));
/* 2 16-bit tallies */
mask = (mask & 0x000F000F) + ((mask >> 8) & (0x000F000F));
/* 1 32-bit tally */
mask = (mask & 0x0000001F) + ((mask >> 16) & (0x0000001F));
return mask;
}
static void
ComputeMasks(visualPtr)
Visual *visualPtr;
{
int count;
redMaskShift = ShiftCount((unsigned int)visualPtr->red_mask);
greenMaskShift = ShiftCount((unsigned int)visualPtr->green_mask);
blueMaskShift = ShiftCount((unsigned int)visualPtr->blue_mask);
redAdjust = greenAdjust = blueAdjust = 0;
count = CountBits((unsigned long)visualPtr->red_mask);
if (count < 8) {
redAdjust = 8 - count;
}
count = CountBits((unsigned long)visualPtr->green_mask);
if (count < 8) {
greenAdjust = 8 - count;
}
count = CountBits((unsigned long)visualPtr->blue_mask);
if (count < 8) {
blueAdjust = 8 - count;
}
}
/*
*----------------------------------------------------------------------
*
* TrueColorPixel --
*
* Computes a pixel index from the 3 component RGB values.
*
* Results:
* The pixel index is returned.
*
*----------------------------------------------------------------------
*/
static INLINE unsigned int
TrueColorPixel(visualPtr, pixelPtr)
Visual *visualPtr;
Pix32 *pixelPtr;
{
unsigned int red, green, blue;
/*
* The number of bits per color may be less than eight. For example,
* 15/16 bit displays (hi-color) use only 5 bits, 8-bit displays
* use 2 or 3 bits (don't ask me why you'd have an 8-bit TrueColor
* display). So shift off the least significant bits.
*/
red = ((unsigned int)pixelPtr->Red >> redAdjust);
green = ((unsigned int)pixelPtr->Green >> greenAdjust);
blue = ((unsigned int)pixelPtr->Blue >> blueAdjust);
/* Shift each color into the proper location of the pixel index. */
red = (red << redMaskShift) & visualPtr->red_mask;
green = (green << greenMaskShift) & visualPtr->green_mask;
blue = (blue << blueMaskShift) & visualPtr->blue_mask;
return (red | green | blue);
}
/*
*----------------------------------------------------------------------
*
* DirectColorPixel --
*
* Translates the 3 component RGB values into a pixel index.
* This differs from TrueColor only in that it first translates
* the RGB values through a color table.
*
* Results:
* The pixel index is returned.
*
*----------------------------------------------------------------------
*/
static INLINE unsigned int
DirectColorPixel(colorTabPtr, pixelPtr)
struct ColorTableStruct *colorTabPtr;
Pix32 *pixelPtr;
{
unsigned int red, green, blue;
red = colorTabPtr->red[pixelPtr->Red];
green = colorTabPtr->green[pixelPtr->Green];
blue = colorTabPtr->blue[pixelPtr->Blue];
return (red | green | blue);
}
/*
*----------------------------------------------------------------------
*
* PseudoColorPixel --
*
* Translates the 3 component RGB values into a pixel index.
* This differs from TrueColor only in that it first translates
* the RGB values through a color table.
*
* Results:
* The pixel index is returned.
*
*----------------------------------------------------------------------
*/
static INLINE unsigned int
PseudoColorPixel(pixelPtr, lut)
Pix32 *pixelPtr;
unsigned int *lut;
{
int red, green, blue;
int pixel;
red = (pixelPtr->Red >> 3) + 1;
green = (pixelPtr->Green >> 3) + 1;
blue = (pixelPtr->Blue >> 3) + 1;
pixel = RGBIndex(red, green, blue);
return lut[pixel];
}
/*
*----------------------------------------------------------------------
*
* Blt_ColorImageToPixmap --
*
* Converts a color image into a pixmap.
*
* Right now this only handles TrueColor visuals.
*
* Results:
* The new pixmap is returned.
*
*----------------------------------------------------------------------
*/
Pixmap
Blt_ColorImageToPixmap(interp, tkwin, image, colorTablePtr)
Tcl_Interp *interp;
Tk_Window tkwin;
Blt_ColorImage image;
ColorTable *colorTablePtr; /* Points to array of colormap indices */
{
Display *display;
int width, height;
Pixmap pixmap;
GC pixmapGC;
Visual *visualPtr;
XImage *imagePtr;
int nPixels;
visualPtr = Tk_Visual(tkwin);
width = Blt_ColorImageWidth(image);
height = Blt_ColorImageHeight(image);
display = Tk_Display(tkwin);
ComputeMasks(visualPtr);
*colorTablePtr = NULL;
imagePtr = XCreateImage(Tk_Display(tkwin), visualPtr, Tk_Depth(tkwin),
ZPixmap, 0, (char *)NULL, width, height, 32, 0);
assert(imagePtr);
nPixels = width * height;
imagePtr->data = Blt_Malloc(sizeof(Pix32) * nPixels);
assert(imagePtr->data);
imagePtr->byte_order = MSBFirst; /* Force the byte order */
imagePtr->bitmap_bit_order = imagePtr->byte_order;
imagePtr->bytes_per_line = width * sizeof(Pix32);
switch (visualPtr->class) {
case TrueColor:
{
register int x, y;
register Pix32 *srcPtr;
register char *destPtr;
unsigned int pixel;
int rowOffset;
/*
* Compute the colormap locations directly from pixel RGB values.
*/
srcPtr = Blt_ColorImageBits(image);
rowOffset = 0;
for (y = 0; y < height; y++) {
destPtr = imagePtr->data + rowOffset;
for (x = 0; x < width; x++, srcPtr++) {
pixel = TrueColorPixel(visualPtr, srcPtr);
switch (imagePtr->bits_per_pixel) {
case 32:
*destPtr++ = (pixel >> 24) & 0xFF;
/*FALLTHRU*/
case 24:
*destPtr++ = (pixel >> 16) & 0xFF;
/*FALLTHRU*/
case 16:
*destPtr++ = (pixel >> 8) & 0xFF;
/*FALLTHRU*/
case 8:
*destPtr++ = pixel & 0xFF;
/*FALLTHRU*/
}
}
rowOffset += imagePtr->bytes_per_line;
}
}
break;
case DirectColor:
{
register int x, y;
register Pix32 *srcPtr;
register char *destPtr;
unsigned int pixel;
int rowOffset;
struct ColorTableStruct *colorTabPtr;
/* Build a color table first */
colorTabPtr = Blt_DirectColorTable(interp, tkwin, image);
/*
* Compute the colormap locations directly from pixel RGB values.
*/
srcPtr = Blt_ColorImageBits(image);
rowOffset = 0;
for (y = 0; y < height; y++) {
destPtr = imagePtr->data + rowOffset;
for (x = 0; x < width; x++, srcPtr++) {
pixel = DirectColorPixel(colorTabPtr, srcPtr);
switch (imagePtr->bits_per_pixel) {
case 32:
*destPtr++ = (pixel >> 24) & 0xFF;
/*FALLTHRU*/
case 24:
*destPtr++ = (pixel >> 16) & 0xFF;
/*FALLTHRU*/
case 16:
*destPtr++ = (pixel >> 8) & 0xFF;
/*FALLTHRU*/
case 8:
*destPtr++ = pixel & 0xFF;
/*FALLTHRU*/
}
}
rowOffset += imagePtr->bytes_per_line;
}
*colorTablePtr = colorTabPtr;
}
break;
case GrayScale:
case StaticGray:
case PseudoColor:
case StaticColor:
{
register int x, y;
register Pix32 *srcPtr;
register char *destPtr;
unsigned int pixel;
int rowOffset;
struct ColorTableStruct *colorTabPtr;
colorTabPtr = Blt_PseudoColorTable(interp, tkwin, image);
srcPtr = Blt_ColorImageBits(image);
rowOffset = 0;
for (y = 0; y < height; y++) {
destPtr = imagePtr->data + rowOffset;
for (x = 0; x < width; x++, srcPtr++) {
pixel = PseudoColorPixel(srcPtr, colorTabPtr->lut);
switch (imagePtr->bits_per_pixel) {
case 32:
*destPtr++ = (pixel >> 24) & 0xFF;
/*FALLTHRU*/
case 24:
*destPtr++ = (pixel >> 16) & 0xFF;
/*FALLTHRU*/
case 16:
*destPtr++ = (pixel >> 8) & 0xFF;
/*FALLTHRU*/
case 8:
*destPtr++ = pixel & 0xFF;
/*FALLTHRU*/
}
}
rowOffset += imagePtr->bytes_per_line;
}
Blt_Free(colorTabPtr->lut);
*colorTablePtr = colorTabPtr;
}
break;
default:
return None; /* Bad or unknown visual class. */
}
pixmapGC = Tk_GetGC(tkwin, 0L, (XGCValues *)NULL);
pixmap = Tk_GetPixmap(display, Tk_WindowId(tkwin), width, height,
Tk_Depth(tkwin));
XPutImage(display, pixmap, pixmapGC, imagePtr, 0, 0, 0, 0, width, height);
XDestroyImage(imagePtr);
Tk_FreeGC(display, pixmapGC);
return pixmap;
}
/* ARGSUSED */
static int
XGetImageErrorProc(clientData, errEventPtr)
ClientData clientData;
XErrorEvent *errEventPtr;
{
int *errorPtr = clientData;
*errorPtr = TCL_ERROR;
return 0;
}
/*
*----------------------------------------------------------------------
*
* Blt_DrawableToColorImage --
*
* Takes a snapshot of an X drawable (pixmap or window) and
* converts it to a color image.
*
* The trick here is to efficiently convert the pixel values
* (indices into the color table) into RGB color values. In the
* days of 8-bit displays, it was simpler to get RGB values for
* all 256 indices into the colormap. Instead we'll build a
* hashtable of unique pixels and from that an array of pixels to
* pass to XQueryColors. For TrueColor visuals, we'll simple
* compute the colors from the pixel.
*
* [I don't know how much faster it would be to take advantage
* of all the different visual types. This pretty much depends
* on the size of the image and the number of colors it uses.]
*
* Results:
* Returns a color image of the drawable. If an error occurred,
* NULL is returned.
*
*----------------------------------------------------------------------
*/
Blt_ColorImage
Blt_DrawableToColorImage(tkwin, drawable, x, y, width, height, inputGamma)
Tk_Window tkwin;
Drawable drawable;
register int x, y; /* Offset of image from the drawable's
* origin. */
int width, height; /* Dimension of the image. Image must
* be completely contained by the
* drawable. */
double inputGamma;
{
XImage *imagePtr;
Blt_ColorImage image;
register Pix32 *destPtr;
unsigned long pixel;
int result = TCL_OK;
Tk_ErrorHandler errHandler;
Visual *visualPtr;
unsigned char lut[256];
errHandler = Tk_CreateErrorHandler(Tk_Display(tkwin), BadMatch,
X_GetImage, -1, XGetImageErrorProc, &result);
imagePtr = XGetImage(Tk_Display(tkwin), drawable, x, y, width, height,
AllPlanes, ZPixmap);
Tk_DeleteErrorHandler(errHandler);
XSync(Tk_Display(tkwin), False);
if (result != TCL_OK) {
return NULL;
}
{
register int i;
double value;
for (i = 0; i < 256; i++) {
value = pow(i / 255.0, inputGamma) * 255.0 + 0.5;
lut[i] = (unsigned char)CLAMP(value);
}
}
/*
* First allocate a color image to hold the screen snapshot.
*/
image = Blt_CreateColorImage(width, height);
visualPtr = Tk_Visual(tkwin);
if (visualPtr->class == TrueColor) {
unsigned int red, green, blue;
/*
* Directly compute the RGB color values from the pixel index
* rather than of going through XQueryColors.
*/
ComputeMasks(visualPtr);
destPtr = Blt_ColorImageBits(image);
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
pixel = XGetPixel(imagePtr, x, y);
red = ((pixel & visualPtr->red_mask) >> redMaskShift) << redAdjust;
green = ((pixel & visualPtr->green_mask) >> greenMaskShift) << greenAdjust;
blue = ((pixel & visualPtr->blue_mask) >> blueMaskShift) << blueAdjust;
/*
* The number of bits per color in the pixel may be
* less than eight. For example, 15/16 bit displays
* (hi-color) use only 5 bits, 8-bit displays use 2 or
* 3 bits (don't ask me why you'd have an 8-bit
* TrueColor display). So shift back the least
* significant bits.
*/
destPtr->Red = lut[red];
destPtr->Green = lut[green];
destPtr->Blue = lut[blue];
destPtr->Alpha = (unsigned char)-1;
destPtr++;
}
}
XDestroyImage(imagePtr);
} else {
Blt_HashEntry *hPtr;
Blt_HashSearch cursor;
Blt_HashTable pixelTable;
XColor *colorPtr, *colorArr;
Pix32 *endPtr;
int nPixels;
int nColors;
int isNew;
/*
* Fill the array with each pixel of the image. At the same time, build
* up a hashtable of the pixels used.
*/
nPixels = width * height;
Blt_InitHashTableWithPool(&pixelTable, BLT_ONE_WORD_KEYS);
destPtr = Blt_ColorImageBits(image);
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
pixel = XGetPixel(imagePtr, x, y);
hPtr = Blt_CreateHashEntry(&pixelTable, (char *)pixel, &isNew);
if (isNew) {
Blt_SetHashValue(hPtr, (char *)pixel);
}
destPtr->value = pixel;
destPtr++;
}
}
XDestroyImage(imagePtr);
/*
* Convert the hashtable of pixels into an array of XColors so
* that we can call XQueryColors with it. XQueryColors will
* convert the pixels into their RGB values.
*/
nColors = pixelTable.numEntries;
colorArr = Blt_Malloc(sizeof(XColor) * nColors);
assert(colorArr);
colorPtr = colorArr;
for (hPtr = Blt_FirstHashEntry(&pixelTable, &cursor); hPtr != NULL;
hPtr = Blt_NextHashEntry(&cursor)) {
colorPtr->pixel = (unsigned long)Blt_GetHashValue(hPtr);
Blt_SetHashValue(hPtr, (char *)colorPtr);
colorPtr++;
}
XQueryColors(Tk_Display(tkwin), Tk_Colormap(tkwin), colorArr, nColors);
/*
* Go again through the array of pixels, replacing each pixel
* of the image with its RGB value.
*/
destPtr = Blt_ColorImageBits(image);
endPtr = destPtr + nPixels;
for (/* empty */; destPtr < endPtr; destPtr++) {
hPtr = Blt_FindHashEntry(&pixelTable, (char *)(intptr_t)destPtr->value);
colorPtr = (XColor *)Blt_GetHashValue(hPtr);
destPtr->Red = lut[colorPtr->red >> 8];
destPtr->Green = lut[colorPtr->green >> 8];
destPtr->Blue = lut[colorPtr->blue >> 8];
destPtr->Alpha = (unsigned char)-1;
}
Blt_Free(colorArr);
Blt_DeleteHashTable(&pixelTable);
}
return image;
}
Pixmap
Blt_PhotoImageMask(tkwin, src)
Tk_Window tkwin;
Tk_PhotoImageBlock src;
{
Pixmap bitmap;
int arraySize, bytes_per_line;
int offset, count;
int value, bitMask;
register int x, y;
unsigned char *bits;
unsigned char *srcPtr;
unsigned char *destPtr;
unsigned long pixel;
bytes_per_line = (src.width + 7) / 8;
arraySize = src.height * bytes_per_line;
bits = Blt_Malloc(sizeof(unsigned char) * arraySize);
assert(bits);
destPtr = bits;
offset = count = 0;
for (y = 0; y < src.height; y++) {
value = 0, bitMask = 1;
srcPtr = src.pixelPtr + offset;
for (x = 0; x < src.width; /*empty*/ ) {
pixel = (srcPtr[src.offset[3]] != 0x00);
if (pixel) {
value |= bitMask;
} else {
count++; /* Count the number of transparent pixels. */
}
bitMask <<= 1;
x++;
if (!(x & 7)) {
*destPtr++ = (unsigned char)value;
value = 0, bitMask = 1;
}
srcPtr += src.pixelSize;
}
if (x & 7) {
*destPtr++ = (unsigned char)value;
}
offset += src.pitch;
}
if (count > 0) {
Tk_MakeWindowExist(tkwin);
bitmap = XCreateBitmapFromData(Tk_Display(tkwin), Tk_WindowId(tkwin),
(char *)bits, (unsigned int)src.width, (unsigned int)src.height);
} else {
bitmap = None; /* Image is opaque. */
}
Blt_Free(bits);
return bitmap;
}
Pixmap
Blt_ColorImageMask(tkwin, image)
Tk_Window tkwin;
Blt_ColorImage image;
{
Pixmap bitmap;
int arraySize, bytes_per_line;
int count;
int value, bitMask;
register int x, y;
unsigned char *bits;
Pix32 *srcPtr;
unsigned char *destPtr;
unsigned long pixel;
int width, height;
width = Blt_ColorImageWidth(image);
height = Blt_ColorImageHeight(image);
bytes_per_line = (width + 7) / 8;
arraySize = height * bytes_per_line;
bits = Blt_Malloc(sizeof(unsigned char) * arraySize);
assert(bits);
destPtr = bits;
count = 0;
srcPtr = Blt_ColorImageBits(image);
for (y = 0; y < height; y++) {
value = 0, bitMask = 1;
for (x = 0; x < width; /*empty*/ ) {
pixel = (srcPtr->Alpha != 0x00);
if (pixel) {
value |= bitMask;
} else {
count++; /* Count the number of transparent pixels. */
}
bitMask <<= 1;
x++;
if (!(x & 7)) {
*destPtr++ = (unsigned char)value;
value = 0, bitMask = 1;
}
srcPtr++;
}
if (x & 7) {
*destPtr++ = (unsigned char)value;
}
}
if (count > 0) {
Tk_MakeWindowExist(tkwin);
bitmap = XCreateBitmapFromData(Tk_Display(tkwin), Tk_WindowId(tkwin),
(char *)bits, (unsigned int)width, (unsigned int)height);
} else {
bitmap = None; /* Image is opaque. */
}
Blt_Free(bits);
return bitmap;
}
/*
* -----------------------------------------------------------------
*
* Blt_RotateBitmap --
*
* Creates a new bitmap containing the rotated image of the given
* bitmap. We also need a special GC of depth 1, so that we do
* not need to rotate more than one plane of the bitmap.
*
* Results:
* Returns a new bitmap containing the rotated image.
*
* -----------------------------------------------------------------
*/
Pixmap
Blt_RotateBitmap(tkwin, srcBitmap, srcWidth, srcHeight, theta,
destWidthPtr, destHeightPtr)
Tk_Window tkwin;
Pixmap srcBitmap; /* Source bitmap to be rotated */
int srcWidth, srcHeight; /* Width and height of the source bitmap */
double theta; /* Right angle rotation to perform */
int *destWidthPtr, *destHeightPtr;
{
Display *display; /* X display */
Window root; /* Root window drawable */
Pixmap destBitmap;
int destWidth, destHeight;
XImage *src, *dest;
register int x, y; /* Destination bitmap coordinates */
register int sx, sy; /* Source bitmap coordinates */
unsigned long pixel;
GC bitmapGC;
double rotWidth, rotHeight;
display = Tk_Display(tkwin);
root = RootWindow(Tk_Display(tkwin), Tk_ScreenNumber(tkwin));
/* Create a bitmap and image big enough to contain the rotated text */
Blt_GetBoundingBox(srcWidth, srcHeight, theta, &rotWidth, &rotHeight,
(Point2D *)NULL);
destWidth = ROUND(rotWidth);
destHeight = ROUND(rotHeight);
destBitmap = Tk_GetPixmap(display, root, destWidth, destHeight, 1);
bitmapGC = Blt_GetBitmapGC(tkwin);
XSetForeground(display, bitmapGC, 0x0);
XFillRectangle(display, destBitmap, bitmapGC, 0, 0, destWidth, destHeight);
src = XGetImage(display, srcBitmap, 0, 0, srcWidth, srcHeight, 1, ZPixmap);
dest = XGetImage(display, destBitmap, 0, 0, destWidth, destHeight, 1,
ZPixmap);
theta = FMOD(theta, 360.0);
if (FMOD(theta, (double)90.0) == 0.0) {
int quadrant;
/* Handle right-angle rotations specifically */
quadrant = (int)(theta / 90.0);
switch (quadrant) {
case ROTATE_270: /* 270 degrees */
for (y = 0; y < destHeight; y++) {
sx = y;
for (x = 0; x < destWidth; x++) {
sy = destWidth - x - 1;
pixel = XGetPixel(src, sx, sy);
if (pixel) {
XPutPixel(dest, x, y, pixel);
}
}
}
break;
case ROTATE_180: /* 180 degrees */
for (y = 0; y < destHeight; y++) {
sy = destHeight - y - 1;
for (x = 0; x < destWidth; x++) {
sx = destWidth - x - 1,
pixel = XGetPixel(src, sx, sy);
if (pixel) {
XPutPixel(dest, x, y, pixel);
}
}
}
break;
case ROTATE_90: /* 90 degrees */
for (y = 0; y < destHeight; y++) {
sx = destHeight - y - 1;
for (x = 0; x < destWidth; x++) {
sy = x;
pixel = XGetPixel(src, sx, sy);
if (pixel) {
XPutPixel(dest, x, y, pixel);
}
}
}
break;
case ROTATE_0: /* 0 degrees */
for (y = 0; y < destHeight; y++) {
for (x = 0; x < destWidth; x++) {
pixel = XGetPixel(src, x, y);
if (pixel) {
XPutPixel(dest, x, y, pixel);
}
}
}
break;
default:
/* The calling routine should never let this happen. */
break;
}
} else {
double radians, sinTheta, cosTheta;
double sox, soy; /* Offset from the center of
* the source rectangle. */
double destCX, destCY; /* Offset to the center of the destination
* rectangle. */
double tx, ty; /* Translated coordinates from center */
double rx, ry; /* Angle of rotation for x and y coordinates */
radians = (theta / 180.0) * M_PI;
sinTheta = sin(radians), cosTheta = cos(radians);
/*
* Coordinates of the centers of the source and destination rectangles
*/
sox = srcWidth * 0.5;
soy = srcHeight * 0.5;
destCX = destWidth * 0.5;
destCY = destHeight * 0.5;
/* For each pixel of the destination image, transform back to the
* associated pixel in the source image. */
for (y = 0; y < destHeight; y++) {
ty = y - destCY;
for (x = 0; x < destWidth; x++) {
/* Translate origin to center of destination image. */
tx = x - destCX;
/* Rotate the coordinates about the origin. */
rx = (tx * cosTheta) - (ty * sinTheta);
ry = (tx * sinTheta) + (ty * cosTheta);
/* Translate back to the center of the source image. */
rx += sox;
ry += soy;
sx = ROUND(rx);
sy = ROUND(ry);
/*
* Verify the coordinates, since the destination image can be
* bigger than the source.
*/
if ((sx >= srcWidth) || (sx < 0) || (sy >= srcHeight) ||
(sy < 0)) {
continue;
}
pixel = XGetPixel(src, sx, sy);
if (pixel) {
XPutPixel(dest, x, y, pixel);
}
}
}
}
/* Write the rotated image into the destination bitmap. */
XPutImage(display, destBitmap, bitmapGC, dest, 0, 0, 0, 0, destWidth,
destHeight);
/* Clean up the temporary resources used. */
XDestroyImage(src), XDestroyImage(dest);
*destWidthPtr = destWidth;
*destHeightPtr = destHeight;
return destBitmap;
}
/*
* -----------------------------------------------------------------------
*
* Blt_ScaleBitmap --
*
* Creates a new scaled bitmap from another bitmap. The new bitmap
* is bounded by a specified region. Only this portion of the bitmap
* is scaled from the original bitmap.
*
* By bounding scaling to a region we can generate a new bitmap
* which is no bigger than the specified viewport.
*
* Results:
* The new scaled bitmap is returned.
*
* Side Effects:
* A new pixmap is allocated. The caller must release this.
*
* -----------------------------------------------------------------------
*/
Pixmap
Blt_ScaleBitmap(tkwin, srcBitmap, srcWidth, srcHeight, destWidth, destHeight)
Tk_Window tkwin;
Pixmap srcBitmap;
int srcWidth, srcHeight, destWidth, destHeight;
{
Display *display;
GC bitmapGC;
Pixmap destBitmap;
Window root;
XImage *src, *dest;
double xScale, yScale;
register int sx, sy; /* Source bitmap coordinates */
register int x, y; /* Destination bitmap coordinates */
unsigned long pixel;
/* Create a new bitmap the size of the region and clear it */
display = Tk_Display(tkwin);
root = RootWindow(Tk_Display(tkwin), Tk_ScreenNumber(tkwin));
destBitmap = Tk_GetPixmap(display, root, destWidth, destHeight, 1);
bitmapGC = Blt_GetBitmapGC(tkwin);
XSetForeground(display, bitmapGC, 0x0);
XFillRectangle(display, destBitmap, bitmapGC, 0, 0, destWidth, destHeight);
src = XGetImage(display, srcBitmap, 0, 0, srcWidth, srcHeight, 1, ZPixmap);
dest = XGetImage(display, destBitmap, 0, 0, destWidth, destHeight, 1,
ZPixmap);
/*
* Scale each pixel of destination image from results of source
* image. Verify the coordinates, since the destination image can
* be bigger than the source
*/
xScale = (double)srcWidth / (double)destWidth;
yScale = (double)srcHeight / (double)destHeight;
/* Map each pixel in the destination image back to the source. */
for (y = 0; y < destHeight; y++) {
sy = (int)(yScale * (double)y);
for (x = 0; x < destWidth; x++) {
sx = (int)(xScale * (double)x);
pixel = XGetPixel(src, sx, sy);
if (pixel) {
XPutPixel(dest, x, y, pixel);
}
}
}
/* Write the scaled image into the destination bitmap */
XPutImage(display, destBitmap, bitmapGC, dest, 0, 0, 0, 0,
destWidth, destHeight);
XDestroyImage(src), XDestroyImage(dest);
return destBitmap;
}
/*
* -----------------------------------------------------------------------
*
* Blt_RotateScaleBitmapRegion --
*
* Creates a scaled and rotated bitmap from a given bitmap. The
* caller also provides (offsets and dimensions) the region of
* interest in the destination bitmap. This saves having to
* process the entire destination bitmap is only part of it is
* showing in the viewport.
*
* This uses a simple rotation/scaling of each pixel in the
* destination image. For each pixel, the corresponding
* pixel in the source bitmap is used. This means that
* destination coordinates are first scaled to the size of
* the rotated source bitmap. These coordinates are then
* rotated back to their original orientation in the source.
*
* Results:
* The new rotated and scaled bitmap is returned.
*
* Side Effects:
* A new pixmap is allocated. The caller must release this.
*
* -----------------------------------------------------------------------
*/
Pixmap
Blt_ScaleRotateBitmapRegion(
Tk_Window tkwin,
Pixmap srcBitmap, /* Source bitmap. */
unsigned int srcWidth,
unsigned int srcHeight, /* Size of source bitmap */
int regionX,
int regionY, /* Offset of region in virtual
* destination bitmap. */
unsigned int regionWidth,
unsigned int regionHeight, /* Desire size of bitmap region. */
unsigned int destWidth,
unsigned int destHeight, /* Virtual size of destination bitmap. */
double theta) /* Angle to rotate bitmap. */
{
Display *display; /* X display */
Window root; /* Root window drawable */
Pixmap destBitmap;
XImage *src, *dest;
register int x, y; /* Destination bitmap coordinates */
register int sx, sy; /* Source bitmap coordinates */
unsigned long pixel;
double xScale, yScale;
double rotWidth, rotHeight;
GC bitmapGC;
display = Tk_Display(tkwin);
root = RootWindow(Tk_Display(tkwin), Tk_ScreenNumber(tkwin));
/* Create a bitmap and image big enough to contain the rotated text */
bitmapGC = Blt_GetBitmapGC(tkwin);
destBitmap = Tk_GetPixmap(display, root, regionWidth, regionHeight, 1);
XSetForeground(display, bitmapGC, 0x0);
XFillRectangle(display, destBitmap, bitmapGC, 0, 0, regionWidth,
regionHeight);
src = XGetImage(display, srcBitmap, 0, 0, srcWidth, srcHeight, 1, ZPixmap);
dest = XGetImage(display, destBitmap, 0, 0, regionWidth, regionHeight, 1,
ZPixmap);
theta = FMOD(theta, 360.0);
Blt_GetBoundingBox(srcWidth, srcHeight, theta, &rotWidth, &rotHeight,
(Point2D *)NULL);
xScale = rotWidth / (double)destWidth;
yScale = rotHeight / (double)destHeight;
if (FMOD(theta, (double)90.0) == 0.0) {
int quadrant;
/* Handle right-angle rotations specifically */
quadrant = (int)(theta / 90.0);
switch (quadrant) {
case ROTATE_270: /* 270 degrees */
for (y = 0; y < regionHeight; y++) {
sx = (int)(yScale * (double)(y + regionY));
for (x = 0; x < regionWidth; x++) {
sy = (int)(xScale *(double)(destWidth - (x + regionX) - 1));
pixel = XGetPixel(src, sx, sy);
if (pixel) {
XPutPixel(dest, x, y, pixel);
}
}
}
break;
case ROTATE_180: /* 180 degrees */
for (y = 0; y < regionHeight; y++) {
sy = (int)(yScale * (double)(destHeight - (y + regionY) - 1));
for (x = 0; x < regionWidth; x++) {
sx = (int)(xScale *(double)(destWidth - (x + regionX) - 1));
pixel = XGetPixel(src, sx, sy);
if (pixel) {
XPutPixel(dest, x, y, pixel);
}
}
}
break;
case ROTATE_90: /* 90 degrees */
for (y = 0; y < regionHeight; y++) {
sx = (int)(yScale * (double)(destHeight - (y + regionY) - 1));
for (x = 0; x < regionWidth; x++) {
sy = (int)(xScale * (double)(x + regionX));
pixel = XGetPixel(src, sx, sy);
if (pixel) {
XPutPixel(dest, x, y, pixel);
}
}
}
break;
case ROTATE_0: /* 0 degrees */
for (y = 0; y < regionHeight; y++) {
sy = (int)(yScale * (double)(y + regionY));
for (x = 0; x < regionWidth; x++) {
sx = (int)(xScale * (double)(x + regionX));
pixel = XGetPixel(src, sx, sy);
if (pixel) {
XPutPixel(dest, x, y, pixel);
}
}
}
break;
default:
/* The calling routine should never let this happen. */
break;
}
} else {
double radians, sinTheta, cosTheta;
double sox, soy; /* Offset from the center of the
* source rectangle. */
double rox, roy; /* Offset to the center of the
* rotated rectangle. */
double tx, ty; /* Translated coordinates from center */
double rx, ry; /* Angle of rotation for x and y coordinates */
radians = (theta / 180.0) * M_PI;
sinTheta = sin(radians), cosTheta = cos(radians);
/*
* Coordinates of the centers of the source and destination rectangles
*/
sox = srcWidth * 0.5;
soy = srcHeight * 0.5;
rox = rotWidth * 0.5;
roy = rotHeight * 0.5;
/* For each pixel of the destination image, transform back to the
* associated pixel in the source image. */
for (y = 0; y < regionHeight; y++) {
ty = (yScale * (double)(y + regionY)) - roy;
for (x = 0; x < regionWidth; x++) {
/* Translate origin to center of destination image. */
tx = (xScale * (double)(x + regionX)) - rox;
/* Rotate the coordinates about the origin. */
rx = (tx * cosTheta) - (ty * sinTheta);
ry = (tx * sinTheta) + (ty * cosTheta);
/* Translate back to the center of the source image. */
rx += sox;
ry += soy;
sx = ROUND(rx);
sy = ROUND(ry);
/*
* Verify the coordinates, since the destination image can be
* bigger than the source.
*/
if ((sx >= srcWidth) || (sx < 0) || (sy >= srcHeight) ||
(sy < 0)) {
continue;
}
pixel = XGetPixel(src, sx, sy);
if (pixel) {
XPutPixel(dest, x, y, pixel);
}
}
}
}
/* Write the rotated image into the destination bitmap. */
XPutImage(display, destBitmap, bitmapGC, dest, 0, 0, 0, 0, regionWidth,
regionHeight);
/* Clean up the temporary resources used. */
XDestroyImage(src), XDestroyImage(dest);
return destBitmap;
}
#if HAVE_JPEGLIB_H
#undef HAVE_STDLIB_H
#undef EXTERN
#ifdef WIN32
#define XMD_H 1
#endif
#include "jpeglib.h"
#include <setjmp.h>
typedef struct {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf jmpBuf;
Tcl_DString dString;
} ReaderHandler;
static void ErrorProc _ANSI_ARGS_((j_common_ptr jpegInfo));
static void MessageProc _ANSI_ARGS_((j_common_ptr jpegInfo));
/*
* Here's the routine that will replace the standard error_exit method:
*/
static void
ErrorProc(jpgPtr)
j_common_ptr jpgPtr;
{
ReaderHandler *handlerPtr = (ReaderHandler *)jpgPtr->err;
(*handlerPtr->pub.output_message) (jpgPtr);
longjmp(handlerPtr->jmpBuf, 1);
}
static void
MessageProc(jpgPtr)
j_common_ptr jpgPtr;
{
ReaderHandler *handlerPtr = (ReaderHandler *)jpgPtr->err;
char buffer[JMSG_LENGTH_MAX];
/* Create the message and append it into the dynamic string. */
(*handlerPtr->pub.format_message) (jpgPtr, buffer);
Tcl_DStringAppend(&(handlerPtr->dString), " ", -1);
Tcl_DStringAppend(&(handlerPtr->dString), buffer, -1);
}
/*
*----------------------------------------------------------------------
*
* Blt_JPEGToColorImage --
*
* Reads a JPEG file and converts it into a color image.
*
* Results:
* The color image is returned. If an error occured, such
* as the designated file could not be opened, NULL is returned.
*
*----------------------------------------------------------------------
*/
Blt_ColorImage
Blt_JPEGToColorImage(interp, fileName)
Tcl_Interp *interp;
char *fileName;
{
struct jpeg_decompress_struct jpg;
Blt_ColorImage image;
unsigned int imageWidth, imageHeight;
register Pix32 *destPtr;
ReaderHandler handler;
FILE *f;
JSAMPLE **readBuffer;
int row_stride;
register int i;
register JSAMPLE *bufPtr;
f = fopen(fileName, "rb");
if (f == NULL) {
Tcl_AppendResult(interp, "can't open \"", fileName, "\":",
Tcl_PosixError(interp), (char *)NULL);
return NULL;
}
image = NULL;
/* Step 1: allocate and initialize JPEG decompression object */
/* We set up the normal JPEG error routines, then override error_exit. */
jpg.dct_method = JDCT_IFAST;
jpg.err = jpeg_std_error(&handler.pub);
handler.pub.error_exit = ErrorProc;
handler.pub.output_message = MessageProc;
Tcl_DStringInit(&handler.dString);
Tcl_DStringAppend(&handler.dString, "error reading \"", -1);
Tcl_DStringAppend(&handler.dString, fileName, -1);
Tcl_DStringAppend(&handler.dString, "\": ", -1);
if (setjmp(handler.jmpBuf)) {
jpeg_destroy_decompress(&jpg);
fclose(f);
Tcl_DStringResult(interp, &(handler.dString));
return NULL;
}
jpeg_create_decompress(&jpg);
jpeg_stdio_src(&jpg, f);
jpeg_read_header(&jpg, TRUE); /* Step 3: read file parameters */
jpeg_start_decompress(&jpg); /* Step 5: Start decompressor */
imageWidth = jpg.output_width;
imageHeight = jpg.output_height;
if ((imageWidth < 1) || (imageHeight < 1)) {
Tcl_AppendResult(interp, "bad JPEG image size", (char *)NULL);
fclose(f);
return NULL;
}
/* JSAMPLEs per row in output buffer */
row_stride = imageWidth * jpg.output_components;
/* Make a one-row-high sample array that will go away when done
* with image */
readBuffer = (*jpg.mem->alloc_sarray) ((j_common_ptr)&jpg, JPOOL_IMAGE,
row_stride, 1);
image = Blt_CreateColorImage(imageWidth, imageHeight);
destPtr = Blt_ColorImageBits(image);
if (jpg.output_components == 1) {
while (jpg.output_scanline < imageHeight) {
jpeg_read_scanlines(&jpg, readBuffer, 1);
bufPtr = readBuffer[0];
for (i = 0; i < (int)imageWidth; i++) {
destPtr->Red = destPtr->Green = destPtr->Blue = *bufPtr++;
destPtr->Alpha = (unsigned char)-1;
destPtr++;
}
}
} else {
while (jpg.output_scanline < imageHeight) {
jpeg_read_scanlines(&jpg, readBuffer, 1);
bufPtr = readBuffer[0];
for (i = 0; i < (int)imageWidth; i++) {
destPtr->Red = *bufPtr++;
destPtr->Green = *bufPtr++;
destPtr->Blue = *bufPtr++;
destPtr->Alpha = (unsigned char)-1;
destPtr++;
}
}
}
jpeg_finish_decompress(&jpg); /* We can ignore the return value
* since suspension is not
* possible with the stdio data
* source. */
jpeg_destroy_decompress(&jpg);
/*
* After finish_decompress, we can close the input file. Here we
* postpone it until after no more JPEG errors are possible, so as
* to simplify the setjmp error logic above. (Actually, I don't
* think that jpeg_destroy can do an error exit, but why assume
* anything...)
*/
fclose(f);
/*
* At this point you may want to check to see whether any corrupt-data
* warnings occurred (test whether jerr.pub.num_warnings is nonzero).
*/
if (handler.pub.num_warnings > 0) {
Tcl_SetErrorCode(interp, "IMAGE", "JPEG",
Tcl_DStringValue(&(handler.dString)), (char *)NULL);
} else {
Tcl_SetErrorCode(interp, "NONE", (char *)NULL);
}
/*
* We're ready to call the Tk_Photo routines. They'll take the RGB
* array we've processed to build the Tk image of the JPEG.
*/
Tcl_DStringFree(&(handler.dString));
return image;
}
#endif /* HAVE_JPEGLIB_H */
|