| 1 | – | /* Copyright (c) 1998 Silicon Graphics, Inc. */ | 
| 2 | – |  | 
| 1 |  | #ifndef lint | 
| 2 | < | static char SCCSid[] = "$SunId$ SGI"; | 
| 2 | > | static const char RCSid[] = "$Id"; | 
| 3 |  | #endif | 
| 6 | – |  | 
| 4 |  | /* | 
| 5 |  | *  x11image.c - driver for X-windows | 
| 6 |  | * | 
| 18 |  |  | 
| 19 |  | #include  "standard.h" | 
| 20 |  |  | 
| 21 | + | #include  <string.h> | 
| 22 |  | #include  <signal.h> | 
| 23 |  | #include  <X11/Xlib.h> | 
| 24 |  | #include  <X11/cursorfont.h> | 
| 25 |  | #include  <X11/Xutil.h> | 
| 26 |  | #include  <X11/Xatom.h> | 
| 27 |  |  | 
| 30 | – | #undef  NOPROTO | 
| 31 | – | #define NOPROTO 1 | 
| 28 |  | #include  "color.h" | 
| 29 |  | #include  "tonemap.h" | 
| 30 |  | #include  "view.h" | 
| 31 |  | #include  "x11raster.h" | 
| 32 |  | #include  "random.h" | 
| 37 | – | #include  "resolu.h" | 
| 33 |  |  | 
| 34 |  | #define  FONTNAME       "8x13"          /* text font we'll use */ | 
| 35 |  |  | 
| 104 |  |  | 
| 105 |  | struct { | 
| 106 |  | int  xmin, ymin, xsiz, ysiz; | 
| 107 | < | }  box = {0, 0, 0, 0};                  /* current box */ | 
| 107 | > | }  bbox = {0, 0, 0, 0};                 /* current bbox */ | 
| 108 |  |  | 
| 109 |  | char  *geometry = NULL;                 /* geometry specification */ | 
| 110 |  |  | 
| 126 |  |  | 
| 127 |  | int  sigrecv; | 
| 128 |  |  | 
| 129 | < | int  onsig() { sigrecv++; } | 
| 129 | > | void  onsig(int i) { sigrecv++; } | 
| 130 |  |  | 
| 131 |  |  | 
| 132 |  | main(argc, argv) | 
| 384 |  | register int  es; | 
| 385 |  | int  cs; | 
| 386 |  |  | 
| 387 | < | if (es = err != NULL) | 
| 387 | > | if ( (es = err != NULL) ) | 
| 388 |  | fprintf(stderr, "%s: %s: %s\n", progname, | 
| 389 |  | fname==NULL?"<stdin>":fname, err); | 
| 390 |  | if (thedisplay != NULL) | 
| 391 |  | XCloseDisplay(thedisplay); | 
| 392 | < | if (parent < 0 & sigrecv == 0) | 
| 392 | > | if ((parent < 0) & (sigrecv == 0)) | 
| 393 |  | kill(getppid(), SIGCONT); | 
| 394 |  | while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ | 
| 395 |  | if (es == 0) | 
| 414 |  | return(-1); | 
| 415 |  | if (v1->depth == 32 && v2->depth == 24) | 
| 416 |  | return(1); | 
| 417 | < | return(0); | 
| 417 | > | /* go for maximum depth otherwise */ | 
| 418 | > | return(v2->depth - v1->depth); | 
| 419 |  | } | 
| 420 |  | /* don't be too greedy */ | 
| 421 |  | if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth) | 
| 422 |  | return(v1->depth - v2->depth); | 
| 423 |  | return(v2->depth - v1->depth); | 
| 424 |  | } | 
| 425 | < | /* prefer Pseudo when < 24-bit */ | 
| 425 | > | /* prefer Pseudo when < 15-bit */ | 
| 426 |  | if ((v1->class == TrueColor || v1->class == DirectColor) && | 
| 427 | < | v1->depth < 24) | 
| 427 | > | v1->depth < 15) | 
| 428 |  | bad1 = 1; | 
| 429 |  | if ((v2->class == TrueColor || v2->class == DirectColor) && | 
| 430 | < | v2->depth < 24) | 
| 430 | > | v2->depth < 15) | 
| 431 |  | bad2 = -1; | 
| 432 |  | if (bad1 | bad2) | 
| 433 |  | return(bad1+bad2); | 
| 494 |  | if (viscmp(&xvi[i],&ourvis) > 0) | 
| 495 |  | quiterr("inadequate visuals on this screen"); | 
| 496 |  | /* OK, we'll use it */ | 
| 497 | < | copystruct(&ourvis, &xvi[i]); | 
| 497 | > | ourvis = xvi[i]; | 
| 498 |  | #ifdef DEBUG | 
| 499 |  | fprintf(stderr, "Selected visual type %s, depth %d\n", | 
| 500 |  | vistype[ourvis.class], ourvis.depth); | 
| 537 |  | if (ourras == NULL) | 
| 538 |  | goto fail; | 
| 539 |  | getmono(); | 
| 540 | < | } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { | 
| 541 | < | ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax); | 
| 540 | > | } else if ((ourvis.class == TrueColor) | (ourvis.class == DirectColor)) { | 
| 541 | > | int  datsiz = ourvis.depth>16 ? sizeof(int32) : sizeof(int16); | 
| 542 | > | ourdata = (unsigned char *)malloc(datsiz*xmax*ymax); | 
| 543 |  | if (ourdata == NULL) | 
| 544 |  | goto fail; | 
| 545 | < | ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8, | 
| 546 | < | ourdata, xmax, ymax, 32); | 
| 545 | > | ourras = make_raster(thedisplay, &ourvis, datsiz*8, | 
| 546 | > | ourdata, xmax, ymax, datsiz*8); | 
| 547 |  | if (ourras == NULL) | 
| 548 |  | goto fail; | 
| 549 |  | getfull(); | 
| 585 |  | map_rcolors(ourras, wind); | 
| 586 |  | if (fast) | 
| 587 |  | make_rpixmap(ourras, wind); | 
| 588 | < | if (!sequential & parent < 0 & sigrecv == 0) { | 
| 588 | > | if ((!sequential) & (parent < 0) & (sigrecv == 0)) { | 
| 589 |  | kill(getppid(), SIGCONT); | 
| 590 |  | sigrecv--; | 
| 591 |  | } | 
| 626 |  | traceray(xpos, ypos)                    /* print requested pixel data */ | 
| 627 |  | int  xpos, ypos; | 
| 628 |  | { | 
| 629 | < | extern char  *index(); | 
| 633 | < | FLOAT  hv[2]; | 
| 629 | > | RREAL  hv[2]; | 
| 630 |  | FVECT  rorg, rdir; | 
| 631 |  | COLOR  cval; | 
| 632 |  | register char  *cp; | 
| 633 |  |  | 
| 634 | < | box.xmin = xpos; box.xsiz = 1; | 
| 635 | < | box.ymin = ypos; box.ysiz = 1; | 
| 634 | > | bbox.xmin = xpos; bbox.xsiz = 1; | 
| 635 | > | bbox.ymin = ypos; bbox.ysiz = 1; | 
| 636 |  | avgbox(cval); | 
| 637 |  | scalecolor(cval, 1./exposure); | 
| 638 |  | pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); | 
| 674 |  | XColor  cvx; | 
| 675 |  | int  com, n; | 
| 676 |  | double  comp; | 
| 677 | < | FLOAT  hv[2]; | 
| 677 | > | RREAL  hv[2]; | 
| 678 |  |  | 
| 679 |  | n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); | 
| 680 |  | if (n == 0) | 
| 697 |  | sprintf(buf, "%.3f", intens(cval)/exposure); | 
| 698 |  | break; | 
| 699 |  | case 'l':                               /* luminance */ | 
| 700 | < | sprintf(buf, "%.0fL", luminance(cval)/exposure); | 
| 700 | > | sprintf(buf, "%.1fL", luminance(cval)/exposure); | 
| 701 |  | break; | 
| 702 |  | case 'c':                               /* color */ | 
| 703 |  | comp = pow(2.0, (double)scale); | 
| 708 |  | break; | 
| 709 |  | } | 
| 710 |  | XDrawImageString(thedisplay, wind, ourgc, | 
| 711 | < | box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); | 
| 711 | > | bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf)); | 
| 712 |  | return(0); | 
| 713 |  | case 'i':                               /* identify (contour) */ | 
| 714 |  | if (ourras->pixels == NULL) | 
| 770 |  | sprintf(buf, "%+d", scale); | 
| 771 |  | remap: | 
| 772 |  | XDrawImageString(thedisplay, wind, ourgc, | 
| 773 | < | box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); | 
| 773 | > | bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf)); | 
| 774 |  | XFlush(thedisplay); | 
| 775 | < | free(ourdata); | 
| 775 | > | /* free(ourdata);       This is done in XDestroyImage()! */ | 
| 776 |  | free_raster(ourras); | 
| 777 |  | getras(); | 
| 778 |  | /* fall through */ | 
| 794 |  | free_rpixmap(ourras); | 
| 795 |  | return(0); | 
| 796 |  | case '0':                               /* recenter origin */ | 
| 797 | < | if (xoff == 0 & yoff == 0) | 
| 797 | > | if ((xoff == 0) & (yoff == 0)) | 
| 798 |  | return(0); | 
| 799 |  | xoff = yoff = 0; | 
| 800 |  | XClearWindow(thedisplay, wind); | 
| 801 |  | redraw(0, 0, width, height); | 
| 802 |  | return(0); | 
| 803 |  | case ' ':                               /* clear */ | 
| 804 | < | redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); | 
| 804 | > | redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz); | 
| 805 |  | return(0); | 
| 806 |  | default: | 
| 807 |  | XBell(thedisplay, 0); | 
| 835 |  | } | 
| 836 |  |  | 
| 837 |  |  | 
| 838 | < | getbox(ebut)                            /* get new box */ | 
| 838 | > | getbox(ebut)                            /* get new bbox */ | 
| 839 |  | XButtonPressedEvent  *ebut; | 
| 840 |  | { | 
| 841 |  | XEvent  e; | 
| 842 |  |  | 
| 843 |  | XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); | 
| 844 |  | while (e.type == MotionNotify) { | 
| 845 | < | revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y); | 
| 845 | > | revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y); | 
| 846 |  | XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); | 
| 847 | < | revbox(ebut->x, ebut->y, box.xmin, box.ymin); | 
| 847 | > | revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin); | 
| 848 |  | } | 
| 849 | < | box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); | 
| 850 | < | box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); | 
| 851 | < | if (box.xmin > ebut->x) { | 
| 852 | < | box.xsiz = box.xmin - ebut->x + 1; | 
| 853 | < | box.xmin = ebut->x; | 
| 849 | > | bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); | 
| 850 | > | bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); | 
| 851 | > | if (bbox.xmin > ebut->x) { | 
| 852 | > | bbox.xsiz = bbox.xmin - ebut->x + 1; | 
| 853 | > | bbox.xmin = ebut->x; | 
| 854 |  | } else { | 
| 855 | < | box.xsiz = ebut->x - box.xmin + 1; | 
| 855 | > | bbox.xsiz = ebut->x - bbox.xmin + 1; | 
| 856 |  | } | 
| 857 | < | if (box.ymin > ebut->y) { | 
| 858 | < | box.ysiz = box.ymin - ebut->y + 1; | 
| 859 | < | box.ymin = ebut->y; | 
| 857 | > | if (bbox.ymin > ebut->y) { | 
| 858 | > | bbox.ysiz = bbox.ymin - ebut->y + 1; | 
| 859 | > | bbox.ymin = ebut->y; | 
| 860 |  | } else { | 
| 861 | < | box.ysiz = ebut->y - box.ymin + 1; | 
| 861 | > | bbox.ysiz = ebut->y - bbox.ymin + 1; | 
| 862 |  | } | 
| 863 |  | } | 
| 864 |  |  | 
| 882 |  | } | 
| 883 |  |  | 
| 884 |  |  | 
| 885 | < | revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */ | 
| 885 | > | revbox(x0, y0, x1, y1)                  /* draw bbox with reversed lines */ | 
| 886 |  | int  x0, y0, x1, y1; | 
| 887 |  | { | 
| 888 |  | revline(x0, y0, x1, y0); | 
| 892 |  | } | 
| 893 |  |  | 
| 894 |  |  | 
| 895 | < | int | 
| 895 | > | void | 
| 896 |  | colavg(scn, n, cavg) | 
| 897 |  | register COLR   *scn; | 
| 898 |  | register int    n; | 
| 901 |  | COLOR   col; | 
| 902 |  |  | 
| 903 |  | while (n--) { | 
| 904 | < | colr_color(col, scn++); | 
| 904 | > | colr_color(col, *scn++); | 
| 905 |  | addcolor(cavg, col); | 
| 906 |  | } | 
| 907 |  | } | 
| 908 |  |  | 
| 909 |  |  | 
| 910 |  | int | 
| 911 | < | avgbox(cavg)                            /* average color over current box */ | 
| 911 | > | avgbox(cavg)                            /* average color over current bbox */ | 
| 912 |  | COLOR   cavg; | 
| 913 |  | { | 
| 914 |  | double  d; | 
| 925 |  |  | 
| 926 |  |  | 
| 927 |  | int | 
| 928 | < | dobox(f, p)                             /* run function over box */ | 
| 929 | < | int     (*f)();                 /* function to call for each subscan */ | 
| 928 | > | dobox(f, p)                             /* run function over bbox */ | 
| 929 | > | void    (*f)();                 /* function to call for each subscan */ | 
| 930 |  | char    *p;                     /* pointer to private data */ | 
| 931 |  | { | 
| 932 |  | int  left, right, top, bottom; | 
| 933 |  | int  y; | 
| 934 |  |  | 
| 935 | < | left = box.xmin - xoff; | 
| 936 | < | right = left + box.xsiz; | 
| 935 | > | left = bbox.xmin - xoff; | 
| 936 | > | right = left + bbox.xsiz; | 
| 937 |  | if (left < 0) | 
| 938 |  | left = 0; | 
| 939 |  | if (right > xmax) | 
| 940 |  | right = xmax; | 
| 941 |  | if (left >= right) | 
| 942 |  | return(0); | 
| 943 | < | top = box.ymin - yoff; | 
| 944 | < | bottom = top + box.ysiz; | 
| 943 | > | top = bbox.ymin - yoff; | 
| 944 | > | bottom = top + bbox.ysiz; | 
| 945 |  | if (top < 0) | 
| 946 |  | top = 0; | 
| 947 |  | if (bottom > ymax) | 
| 957 |  | } | 
| 958 |  |  | 
| 959 |  |  | 
| 960 | < | int | 
| 960 | > | void | 
| 961 |  | addfix(scn, n)                  /* add fixation points to histogram */ | 
| 962 |  | COLR    *scn; | 
| 963 |  | int     n; | 
| 1094 |  | cerr[x] = err + errp; | 
| 1095 |  | } | 
| 1096 |  | } | 
| 1097 | < | free((char *)cerr); | 
| 1097 | > | free((void *)cerr); | 
| 1098 |  | } | 
| 1099 |  |  | 
| 1100 |  |  | 
| 1137 |  | errp = err; | 
| 1138 |  | ti = x*xmax/iconwidth; | 
| 1139 |  | copycolr(clr, scan[ti]); | 
| 1140 | < | normcolrs(clr, 1, scale); | 
| 1140 | > | normcolrs(&clr, 1, scale); | 
| 1141 |  | err += normbright(clr) + cerr[x]; | 
| 1142 |  | if (err > 127) | 
| 1143 |  | err -= 255; | 
| 1153 |  | getfull()                       /* get full (24-bit) data */ | 
| 1154 |  | { | 
| 1155 |  | int     y; | 
| 1156 | < | register unsigned int4  *dp; | 
| 1156 | > | register uint32 *dp; | 
| 1157 | > | register uint16 *dph; | 
| 1158 |  | register int    x; | 
| 1159 |  | /* initialize tone mapping */ | 
| 1160 |  | make_tonemap(); | 
| 1161 |  | /* read and convert file */ | 
| 1162 | < | dp = (unsigned int4 *)ourdata; | 
| 1162 | > | dp = (uint32 *)ourdata; | 
| 1163 | > | dph = (uint16 *)ourdata; | 
| 1164 |  | for (y = 0; y < ymax; y++) { | 
| 1165 |  | getscan(y); | 
| 1166 |  | add2icon(y, scanline); | 
| 1167 |  | tmap_colrs(scanline, xmax); | 
| 1168 | < | if (ourras->image->blue_mask & 1) | 
| 1168 | > | switch (ourras->image->blue_mask) { | 
| 1169 | > | case 0xff:              /* 24-bit RGB */ | 
| 1170 |  | for (x = 0; x < xmax; x++) | 
| 1171 | < | *dp++ = (unsigned int4)scanline[x][RED] << 16 | | 
| 1172 | < | (unsigned int4)scanline[x][GRN] << 8 | | 
| 1173 | < | (unsigned int4)scanline[x][BLU] ; | 
| 1174 | < | else | 
| 1171 | > | *dp++ = (uint32)scanline[x][RED] << 16 | | 
| 1172 | > | (uint32)scanline[x][GRN] << 8 | | 
| 1173 | > | (uint32)scanline[x][BLU] ; | 
| 1174 | > | break; | 
| 1175 | > | case 0xff0000:          /* 24-bit BGR */ | 
| 1176 |  | for (x = 0; x < xmax; x++) | 
| 1177 | < | *dp++ = (unsigned int4)scanline[x][RED] | | 
| 1178 | < | (unsigned int4)scanline[x][GRN] << 8 | | 
| 1179 | < | (unsigned int4)scanline[x][BLU] << 16 ; | 
| 1177 | > | *dp++ = (uint32)scanline[x][RED] | | 
| 1178 | > | (uint32)scanline[x][GRN] << 8 | | 
| 1179 | > | (uint32)scanline[x][BLU] << 16 ; | 
| 1180 | > | break; | 
| 1181 | > | #if 0 | 
| 1182 | > | case 0x1f:              /* 15-bit RGB */ | 
| 1183 | > | for (x = 0; x < xmax; x++) | 
| 1184 | > | *dph++ =        (scanline[x][RED] << 7 & 0x7c00) | | 
| 1185 | > | (scanline[x][GRN] << 2 & 0x3e0) | | 
| 1186 | > | (unsigned)scanline[x][BLU] >> 3 ; | 
| 1187 | > | break; | 
| 1188 | > | case 0x7c00:            /* 15-bit BGR */ | 
| 1189 | > | for (x = 0; x < xmax; x++) | 
| 1190 | > | *dph++ =        (unsigned)scanline[x][RED] >> 3 | | 
| 1191 | > | (scanline[x][GRN] << 2 & 0x3e0) | | 
| 1192 | > | (scanline[x][BLU] << 7 & 0x7c00) ; | 
| 1193 | > | break; | 
| 1194 | > | #endif | 
| 1195 | > | default:                /* unknown */ | 
| 1196 | > | if (ourvis.depth > 16) | 
| 1197 | > | for (x = 0; x < xmax; x++) { | 
| 1198 | > | *dp = ourras->image->red_mask & | 
| 1199 | > | ourras->image->red_mask*scanline[x][RED]/255; | 
| 1200 | > | *dp |= ourras->image->green_mask & | 
| 1201 | > | ourras->image->green_mask*scanline[x][GRN]/255; | 
| 1202 | > | *dp++ |= ourras->image->blue_mask & | 
| 1203 | > | ourras->image->blue_mask*scanline[x][BLU]/255; | 
| 1204 | > | } | 
| 1205 | > | else | 
| 1206 | > | for (x = 0; x < xmax; x++) { | 
| 1207 | > | *dph = ourras->image->red_mask & | 
| 1208 | > | ourras->image->red_mask*scanline[x][RED]/255; | 
| 1209 | > | *dph |= ourras->image->green_mask & | 
| 1210 | > | ourras->image->green_mask*scanline[x][GRN]/255; | 
| 1211 | > | *dph++ |= ourras->image->blue_mask & | 
| 1212 | > | ourras->image->blue_mask*scanline[x][BLU]/255; | 
| 1213 | > | } | 
| 1214 | > | break; | 
| 1215 | > | } | 
| 1216 |  | } | 
| 1217 |  | } | 
| 1218 |  |  | 
| 1232 |  | tmap_colrs(scanline, xmax); | 
| 1233 |  | if (maxcolors < 256) | 
| 1234 |  | for (x = 0; x < xmax; x++) | 
| 1235 | < | *dp++ = ((int4)scanline[x][GRN] * | 
| 1235 | > | *dp++ = ((int32)scanline[x][GRN] * | 
| 1236 |  | maxcolors + maxcolors/2) >> 8; | 
| 1237 |  | else | 
| 1238 |  | for (x = 0; x < xmax; x++) | 
| 1240 |  | } | 
| 1241 |  | for (x = 0; x < maxcolors; x++) | 
| 1242 |  | clrtab[x][RED] = clrtab[x][GRN] = | 
| 1243 | < | clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors; | 
| 1243 | > | clrtab[x][BLU] = ((int32)x*256 + 128)/maxcolors; | 
| 1244 |  | } | 
| 1245 |  |  | 
| 1246 |  |  | 
| 1253 |  | /* initialize tone mapping */ | 
| 1254 |  | make_tonemap(); | 
| 1255 |  | /* make histogram */ | 
| 1256 | < | if (new_histo((int4)xmax*ymax) == -1) | 
| 1256 | > | if (new_histo((int32)xmax*ymax) == -1) | 
| 1257 |  | quiterr("cannot initialize histogram"); | 
| 1258 |  | for (y = 0; y < ymax; y++) { | 
| 1259 |  | if (getscan(y) < 0) | 
| 1310 |  | static int  trunced = -1;               /* truncated file? */ | 
| 1311 |  | skipit: | 
| 1312 |  | if (trunced >= 0 && y >= trunced) { | 
| 1313 | < | bzero(scanline, xmax*sizeof(COLR)); | 
| 1313 | > | memset(scanline, '\0', xmax*sizeof(COLR)); | 
| 1314 |  | return(-1); | 
| 1315 |  | } | 
| 1316 |  | if (y != cury) { |