| 1 | – | /* Copyright (c) 1992 Regents of the University of California */ | 
| 2 | – |  | 
| 1 |  | #ifndef lint | 
| 2 | < | static char SCCSid[] = "$SunId$ LBL"; | 
| 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  <unistd.h> | 
| 24 | + | #include  <sys/types.h> | 
| 25 | + | #include  <sys/wait.h> | 
| 26 |  | #include  <X11/Xlib.h> | 
| 27 |  | #include  <X11/cursorfont.h> | 
| 28 |  | #include  <X11/Xutil.h> | 
| 29 | + | #include  <X11/Xatom.h> | 
| 30 |  |  | 
| 31 |  | #include  "color.h" | 
| 32 | + | #include  "tonemap.h" | 
| 33 | + | #include  "clrtab.h" | 
| 34 |  | #include  "view.h" | 
| 30 | – | #include  "pic.h" | 
| 35 |  | #include  "x11raster.h" | 
| 36 |  | #include  "random.h" | 
| 33 | – | #include  "resolu.h" | 
| 37 |  |  | 
| 38 |  | #define  FONTNAME       "8x13"          /* text font we'll use */ | 
| 39 |  |  | 
| 43 |  |  | 
| 44 |  | #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */ | 
| 45 |  |  | 
| 46 | + | #define  FIXWEIGHT      20              /* weight to add for fixation points */ | 
| 47 | + |  | 
| 48 |  | #define  ourscreen      DefaultScreen(thedisplay) | 
| 49 |  | #define  ourroot        RootWindow(thedisplay,ourscreen) | 
| 50 |  |  | 
| 53 |  | #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras) | 
| 54 |  |  | 
| 55 |  | double  gamcor = 2.2;                   /* gamma correction */ | 
| 56 | + | char  *gamstr = NULL;                   /* gamma value override */ | 
| 57 |  |  | 
| 58 |  | int  dither = 1;                        /* dither colors? */ | 
| 59 |  | int  fast = 0;                          /* keep picture in Pixmap? */ | 
| 62 |  |  | 
| 63 |  | Window  wind = 0;                       /* our output window */ | 
| 64 |  | unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */ | 
| 59 | – | Font  fontid;                           /* our font */ | 
| 60 | – |  | 
| 65 |  | int  maxcolors = 0;                     /* maximum colors */ | 
| 66 |  | int  greyscale = 0;                     /* in grey */ | 
| 67 |  |  | 
| 70 |  | int  xoff = 0;                          /* x image offset */ | 
| 71 |  | int  yoff = 0;                          /* y image offset */ | 
| 72 |  |  | 
| 73 | + | int  parent = 0;                        /* number of children, -1 if child */ | 
| 74 | + | int  sequential = 0;                    /* display images in sequence */ | 
| 75 | + |  | 
| 76 | + | char  *tout = "od";                     /* output of 't' command */ | 
| 77 | + | int  tinterv = 0;                       /* interval between mouse reports */ | 
| 78 | + |  | 
| 79 | + | int  tmflags = TM_F_LINEAR;             /* tone mapping flags */ | 
| 80 | + |  | 
| 81 |  | VIEW  ourview = STDVIEW;                /* image view parameters */ | 
| 82 |  | int  gotview = 0;                       /* got parameters from file */ | 
| 83 |  |  | 
| 84 |  | COLR  *scanline;                        /* scan line buffer */ | 
| 85 | + | TMbright  *lscan;                       /* encoded luminance scanline */ | 
| 86 | + | uby8  *cscan;                           /* encoded chroma scanline */ | 
| 87 | + | uby8  *pscan;                           /* compute pixel scanline */ | 
| 88 |  |  | 
| 89 |  | RESOLU  inpres;                         /* input resolution and ordering */ | 
| 90 |  | int  xmax, ymax;                        /* picture dimensions */ | 
| 91 |  | int  width, height;                     /* window size */ | 
| 92 |  | char  *fname = NULL;                    /* input file name */ | 
| 93 | < | FILE  *fin = stdin;                     /* input file */ | 
| 93 | > | FILE  *fin = NULL;                      /* input file */ | 
| 94 |  | long  *scanpos = NULL;                  /* scan line positions in file */ | 
| 95 |  | int  cury = 0;                          /* current scan location */ | 
| 96 |  |  | 
| 98 |  |  | 
| 99 |  | int  wrongformat = 0;                   /* input in another format? */ | 
| 100 |  |  | 
| 101 | + | TMstruct        *tmGlobal;              /* base tone-mapping */ | 
| 102 | + | TMstruct        *tmCurrent;             /* curren tone-mapping */ | 
| 103 | + |  | 
| 104 |  | GC      ourgc;                          /* standard graphics context */ | 
| 105 |  | GC      revgc;                          /* graphics context with GXinvert */ | 
| 106 |  |  | 
| 111 |  |  | 
| 112 |  | struct { | 
| 113 |  | int  xmin, ymin, xsiz, ysiz; | 
| 114 | < | }  box = {0, 0, 0, 0};                  /* current box */ | 
| 114 | > | }  bbox = {0, 0, 0, 0};                 /* current bbox */ | 
| 115 |  |  | 
| 116 |  | char  *geometry = NULL;                 /* geometry specification */ | 
| 117 |  |  | 
| 122 |  |  | 
| 123 |  | char  errmsg[128]; | 
| 124 |  |  | 
| 125 | < | extern long  ftell(); | 
| 125 | > | uby8  clrtab[256][3];                   /* global color map */ | 
| 126 |  |  | 
| 109 | – | extern char  *malloc(), *calloc(); | 
| 127 |  |  | 
| 111 | – | extern double  pow(), log(); | 
| 112 | – |  | 
| 128 |  | Display  *thedisplay; | 
| 129 | + | Atom  closedownAtom, wmProtocolsAtom; | 
| 130 |  |  | 
| 131 | < | main(argc, argv) | 
| 132 | < | int  argc; | 
| 133 | < | char  *argv[]; | 
| 131 | > | int  sigrecv; | 
| 132 | > |  | 
| 133 | > | void  onsig(int i) { sigrecv++; } | 
| 134 | > |  | 
| 135 | > | typedef void doboxf_t(COLR *scn, int n, void *); | 
| 136 | > |  | 
| 137 | > | static gethfunc headline; | 
| 138 | > | static void init(int argc, char **argv); | 
| 139 | > | static void quiterr(char *err); | 
| 140 | > | static int viscmp(XVisualInfo *v1, XVisualInfo *v2); | 
| 141 | > | static void getbestvis(void); | 
| 142 | > | static void getras(void); | 
| 143 | > | static void getevent(void); | 
| 144 | > | static int traceray(int xpos, int ypos); | 
| 145 | > | static int docom(XKeyPressedEvent *ekey); | 
| 146 | > | static void moveimage(XButtonPressedEvent *ebut); | 
| 147 | > | static void getbox(XButtonPressedEvent *ebut); | 
| 148 | > | static void trackrays(XButtonPressedEvent *ebut); | 
| 149 | > | static void revbox(int x0, int y0, int x1, int y1); | 
| 150 | > | static doboxf_t colavg; | 
| 151 | > | static doboxf_t addfix; | 
| 152 | > | static int avgbox(COLOR cavg); | 
| 153 | > | static int dobox(doboxf_t *f, void *p); | 
| 154 | > | static void make_tonemap(void); | 
| 155 | > | static void tmap_colrs(COLR *scn, int len); | 
| 156 | > | static void getmono(void); | 
| 157 | > | static void add2icon(int y, COLR *scan); | 
| 158 | > | static void getfull(void); | 
| 159 | > | static void getgrey(void); | 
| 160 | > | static void getmapped(void); | 
| 161 | > | static void scale_rcolors(XRASTER *xr, double sf); | 
| 162 | > | static int getscan(int y); | 
| 163 | > |  | 
| 164 | > |  | 
| 165 | > | int | 
| 166 | > | main(int  argc, char  *argv[]) | 
| 167 |  | { | 
| 119 | – | extern char  *getenv(); | 
| 120 | – | char  *gv; | 
| 121 | – | int  headline(); | 
| 168 |  | int  i; | 
| 169 | + | int  pid; | 
| 170 |  |  | 
| 171 |  | progname = argv[0]; | 
| 172 | < | if ((gv = getenv("GAMMA")) != NULL) | 
| 126 | < | gamcor = atof(gv); | 
| 172 | > | fin = stdin; | 
| 173 |  |  | 
| 174 |  | for (i = 1; i < argc; i++) | 
| 175 |  | if (argv[i][0] == '-') | 
| 176 |  | switch (argv[i][1]) { | 
| 177 | < | case 'c': | 
| 177 | > | case 'c':                       /* number of colors */ | 
| 178 |  | maxcolors = atoi(argv[++i]); | 
| 179 |  | break; | 
| 180 | < | case 'b': | 
| 180 | > | case 'b':                       /* greyscale only */ | 
| 181 |  | greyscale = !greyscale; | 
| 182 |  | break; | 
| 183 | < | case 'm': | 
| 183 | > | case 'm':                       /* monochrome */ | 
| 184 | > | greyscale = 1; | 
| 185 |  | maxcolors = 2; | 
| 186 |  | break; | 
| 187 | < | case 'd': | 
| 187 | > | case 'd':                       /* display or dither */ | 
| 188 |  | if (argv[i][2] == 'i') | 
| 189 |  | dispname = argv[++i]; | 
| 190 |  | else | 
| 191 |  | dither = !dither; | 
| 192 |  | break; | 
| 193 | < | case 'f': | 
| 193 | > | case 'f':                       /* save pixmap */ | 
| 194 |  | fast = !fast; | 
| 195 |  | break; | 
| 196 | < | case 'e': | 
| 197 | < | if (argv[i+1][0] != '+' && argv[i+1][0] != '-') | 
| 196 | > | case 's':                       /* one at a time */ | 
| 197 | > | sequential = !sequential; | 
| 198 | > | break; | 
| 199 | > | case 'o':                       /* 't' output */ | 
| 200 | > | tout = argv[i]+2; | 
| 201 | > | break; | 
| 202 | > | case 't':                       /* msec interval */ | 
| 203 | > | tinterv = atoi(argv[++i]); | 
| 204 | > | break; | 
| 205 | > | case 'e':                       /* exposure comp. */ | 
| 206 | > | i++; | 
| 207 | > | if (argv[i][0] == 'a') { | 
| 208 | > | tmflags = TM_F_CAMERA; | 
| 209 | > | break; | 
| 210 | > | } | 
| 211 | > | if (argv[i][0] == 'h') { | 
| 212 | > | tmflags = TM_F_HUMAN; | 
| 213 | > | break; | 
| 214 | > | } | 
| 215 | > | if (argv[i][0] != '+' && argv[i][0] != '-') | 
| 216 |  | goto userr; | 
| 217 | < | scale = atoi(argv[++i]); | 
| 217 | > | scale = atoi(argv[i]); | 
| 218 |  | break; | 
| 219 | < | case 'g': | 
| 219 | > | case 'g':                       /* gamma comp. */ | 
| 220 |  | if (argv[i][2] == 'e') | 
| 221 |  | geometry = argv[++i]; | 
| 222 |  | else | 
| 223 | < | gamcor = atof(argv[++i]); | 
| 223 | > | gamstr = argv[++i]; | 
| 224 |  | break; | 
| 225 |  | default: | 
| 226 |  | goto userr; | 
| 230 |  | else | 
| 231 |  | break; | 
| 232 |  |  | 
| 233 | < | if (i == argc-1) { | 
| 233 | > | if (i > argc) | 
| 234 | > | goto userr; | 
| 235 | > | while (i < argc-1) { | 
| 236 | > | sigrecv = 0; | 
| 237 | > | signal(SIGCONT, onsig); | 
| 238 | > | if ((pid=fork()) == 0) {        /* a child for each picture */ | 
| 239 | > | parent = -1; | 
| 240 | > | break; | 
| 241 | > | } | 
| 242 | > | if (pid < 0) | 
| 243 | > | quiterr("fork failed"); | 
| 244 | > | parent++; | 
| 245 | > | while (!sigrecv) | 
| 246 | > | pause();        /* wait for wake-up call */ | 
| 247 | > | i++; | 
| 248 | > | } | 
| 249 | > | if (i < argc) {                 /* open picture file */ | 
| 250 |  | fname = argv[i]; | 
| 251 |  | fin = fopen(fname, "r"); | 
| 252 | < | if (fin == NULL) { | 
| 253 | < | sprintf(errmsg, "cannot open file \"%s\"", fname); | 
| 254 | < | quiterr(errmsg); | 
| 174 | < | } | 
| 175 | < | } else if (i != argc) | 
| 176 | < | goto userr; | 
| 252 | > | if (fin == NULL) | 
| 253 | > | quiterr("cannot open picture file"); | 
| 254 | > | } | 
| 255 |  | /* get header */ | 
| 256 |  | getheader(fin, headline, NULL); | 
| 257 |  | /* get picture dimensions */ | 
| 265 |  | if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) | 
| 266 |  | quiterr("out of memory"); | 
| 267 |  |  | 
| 268 | < | init();                 /* get file and open window */ | 
| 268 | > | init(argc, argv);                       /* get file and open window */ | 
| 269 |  |  | 
| 270 |  | for ( ; ; ) | 
| 271 |  | getevent();             /* main loop */ | 
| 272 |  | userr: | 
| 273 |  | fprintf(stderr, | 
| 274 | < | "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", | 
| 274 | > | "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] hdr ..\n", | 
| 275 |  | progname); | 
| 276 | < | quit(1); | 
| 276 | > | exit(1); | 
| 277 |  | } | 
| 278 |  |  | 
| 279 |  |  | 
| 280 | < | headline(s)             /* get relevant info from header */ | 
| 281 | < | char  *s; | 
| 280 | > | static int | 
| 281 | > | headline(               /* get relevant info from header */ | 
| 282 | > | char    *s, | 
| 283 | > | void    *p | 
| 284 | > | ) | 
| 285 |  | { | 
| 286 | < | char  fmt[32]; | 
| 286 | > | char  fmt[MAXFMTLEN]; | 
| 287 |  |  | 
| 288 |  | if (isexpos(s)) | 
| 289 |  | exposure *= exposval(s); | 
| 290 | < | else if (isformat(s)) { | 
| 291 | < | formatval(fmt, s); | 
| 292 | < | wrongformat = strcmp(fmt, COLRFMT); | 
| 293 | < | } else if (isview(s) && sscanview(&ourview, s) > 0) | 
| 294 | < | gotview++; | 
| 290 | > | else if (formatval(fmt, s)) | 
| 291 | > | wrongformat = strcmp(fmt, COLRFMT) && strcmp(fmt, SPECFMT); | 
| 292 | > | else if (isview(s)) | 
| 293 | > | gotview += sscanview(&ourview, s); | 
| 294 | > | else if (isncomp(s)) | 
| 295 | > | NCSAMP = ncompval(s); | 
| 296 | > | else if (iswlsplit(s)) | 
| 297 | > | wlsplitval(WLPART, s); | 
| 298 | > | return(0); | 
| 299 |  | } | 
| 300 |  |  | 
| 301 |  |  | 
| 302 | < | init()                  /* get data and open window */ | 
| 302 | > | static void | 
| 303 | > | init(                   /* get data and open window */ | 
| 304 | > | int argc, | 
| 305 | > | char **argv | 
| 306 | > | ) | 
| 307 |  | { | 
| 219 | – | XWMHints        ourxwmhints; | 
| 308 |  | XSetWindowAttributes    ourwinattr; | 
| 309 | < | XSizeHints      oursizhints; | 
| 309 | > | XClassHint      xclshints; | 
| 310 | > | XWMHints        xwmhints; | 
| 311 | > | XSizeHints      xszhints; | 
| 312 | > | XTextProperty   windowName, iconName; | 
| 313 | > | XGCValues       xgcv; | 
| 314 | > | char    *name; | 
| 315 |  | register int    i; | 
| 316 |  |  | 
| 317 |  | if (fname != NULL) { | 
| 318 |  | scanpos = (long *)malloc(ymax*sizeof(long)); | 
| 319 |  | if (scanpos == NULL) | 
| 320 | < | goto memerr; | 
| 320 | > | quiterr("out of memory"); | 
| 321 |  | for (i = 0; i < ymax; i++) | 
| 322 |  | scanpos[i] = -1; | 
| 323 | < | } | 
| 323 | > | name = fname; | 
| 324 | > | } else | 
| 325 | > | name = progname; | 
| 326 | > | /* remove directory prefix from name */ | 
| 327 | > | for (i = strlen(name); i-- > 0; ) | 
| 328 | > | if (name[i] == '/') | 
| 329 | > | break; | 
| 330 | > | name += i+1; | 
| 331 |  | if ((thedisplay = XOpenDisplay(dispname)) == NULL) | 
| 332 |  | quiterr("cannot open display"); | 
| 333 | + | /* set gamma value */ | 
| 334 | + | if (gamstr == NULL)             /* get it from the X server */ | 
| 335 | + | gamstr = XGetDefault(thedisplay, "radiance", "gamma"); | 
| 336 | + | if (gamstr == NULL)             /* get it from the environment */ | 
| 337 | + | gamstr = getenv("DISPLAY_GAMMA"); | 
| 338 | + | if (gamstr != NULL) | 
| 339 | + | gamcor = atof(gamstr); | 
| 340 |  | /* get best visual for default screen */ | 
| 341 |  | getbestvis(); | 
| 342 |  | /* store image */ | 
| 343 |  | getras(); | 
| 344 | < | /* open window */ | 
| 345 | < | ourwinattr.border_pixel = ourblack; | 
| 346 | < | ourwinattr.background_pixel = ourwhite; | 
| 240 | < | ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, | 
| 241 | < | ourvis.visual, AllocNone); | 
| 242 | < | wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, | 
| 243 | < | ourvis.depth, InputOutput, ourvis.visual, | 
| 244 | < | CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); | 
| 245 | < | if (wind == 0) | 
| 246 | < | quiterr("cannot create window"); | 
| 247 | < | XFreeColormap(thedisplay, ourwinattr.colormap); | 
| 248 | < | width = xmax; | 
| 249 | < | height = ymax; | 
| 250 | < | ourgc = XCreateGC(thedisplay, wind, 0, 0); | 
| 251 | < | XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes); | 
| 252 | < | revgc = XCreateGC(thedisplay, wind, 0, 0); | 
| 253 | < | XSetFunction(thedisplay, revgc, GXinvert); | 
| 254 | < | fontid = XLoadFont(thedisplay, FONTNAME); | 
| 255 | < | if (fontid == 0) | 
| 256 | < | quiterr("cannot get font"); | 
| 257 | < | XSetFont(thedisplay, ourgc, fontid); | 
| 258 | < | XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, | 
| 259 | < | XC_diamond_cross)); | 
| 260 | < | XStoreName(thedisplay, wind, fname == NULL ? progname : fname); | 
| 344 | > | /* get size and position */ | 
| 345 | > | xszhints.flags = 0; | 
| 346 | > | xszhints.width = xmax; xszhints.height = ymax; | 
| 347 |  | if (geometry != NULL) { | 
| 348 | < | bzero((char *)&oursizhints, sizeof(oursizhints)); | 
| 349 | < | i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, | 
| 350 | < | (unsigned *)&oursizhints.width, | 
| 265 | < | (unsigned *)&oursizhints.height); | 
| 348 | > | i = XParseGeometry(geometry, &xszhints.x, &xszhints.y, | 
| 349 | > | (unsigned *)&xszhints.width, | 
| 350 | > | (unsigned *)&xszhints.height); | 
| 351 |  | if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) | 
| 352 | < | oursizhints.flags |= USSize; | 
| 353 | < | else { | 
| 354 | < | oursizhints.width = xmax; | 
| 270 | < | oursizhints.height = ymax; | 
| 271 | < | oursizhints.flags |= PSize; | 
| 272 | < | } | 
| 352 | > | xszhints.flags |= USSize; | 
| 353 | > | else | 
| 354 | > | xszhints.flags |= PSize; | 
| 355 |  | if ((i&(XValue|YValue)) == (XValue|YValue)) { | 
| 356 | < | oursizhints.flags |= USPosition; | 
| 356 | > | xszhints.flags |= USPosition; | 
| 357 |  | if (i & XNegative) | 
| 358 | < | oursizhints.x += DisplayWidth(thedisplay, | 
| 359 | < | ourscreen)-1-oursizhints.width-2*BORWIDTH; | 
| 358 | > | xszhints.x += DisplayWidth(thedisplay, | 
| 359 | > | ourscreen)-1-xszhints.width-2*BORWIDTH; | 
| 360 |  | if (i & YNegative) | 
| 361 | < | oursizhints.y += DisplayHeight(thedisplay, | 
| 362 | < | ourscreen)-1-oursizhints.height-2*BORWIDTH; | 
| 361 | > | xszhints.y += DisplayHeight(thedisplay, | 
| 362 | > | ourscreen)-1-xszhints.height-2*BORWIDTH; | 
| 363 |  | } | 
| 282 | – | XSetNormalHints(thedisplay, wind, &oursizhints); | 
| 364 |  | } | 
| 365 | < | ourxwmhints.flags = InputHint|IconPixmapHint; | 
| 366 | < | ourxwmhints.input = True; | 
| 367 | < | ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, | 
| 365 | > | /* open window */ | 
| 366 | > | i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel; | 
| 367 | > | ourwinattr.border_pixel = ourwhite; | 
| 368 | > | ourwinattr.background_pixel = ourblack; | 
| 369 | > | if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) { | 
| 370 | > | ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual); | 
| 371 | > | i |= CWColormap; | 
| 372 | > | } | 
| 373 | > | ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask| | 
| 374 | > | ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask; | 
| 375 | > | ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross); | 
| 376 | > | wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y, | 
| 377 | > | xszhints.width, xszhints.height, BORWIDTH, | 
| 378 | > | ourvis.depth, InputOutput, ourvis.visual, | 
| 379 | > | i, &ourwinattr); | 
| 380 | > | if (wind == 0) | 
| 381 | > | quiterr("cannot create window"); | 
| 382 | > | width = xmax; | 
| 383 | > | height = ymax; | 
| 384 | > | /* prepare graphics drawing context */ | 
| 385 | > | if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) | 
| 386 | > | quiterr("cannot get font"); | 
| 387 | > | xgcv.foreground = ourblack; | 
| 388 | > | xgcv.background = ourwhite; | 
| 389 | > | ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| | 
| 390 | > | GCFont, &xgcv); | 
| 391 | > | xgcv.function = GXinvert; | 
| 392 | > | revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| | 
| 393 | > | GCFunction, &xgcv); | 
| 394 | > |  | 
| 395 | > | /* set up the window manager */ | 
| 396 | > | xwmhints.flags = InputHint|IconPixmapHint; | 
| 397 | > | xwmhints.input = True; | 
| 398 | > | xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, | 
| 399 |  | wind, icondata, iconwidth, iconheight); | 
| 400 | < | XSetWMHints(thedisplay, wind, &ourxwmhints); | 
| 401 | < | XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask | 
| 402 | < | |ButtonMotionMask|StructureNotifyMask | 
| 403 | < | |KeyPressMask|ExposureMask); | 
| 400 | > |  | 
| 401 | > | windowName.encoding = iconName.encoding = XA_STRING; | 
| 402 | > | windowName.format = iconName.format = 8; | 
| 403 | > | windowName.value = (u_char *)name; | 
| 404 | > | windowName.nitems = strlen((char *)windowName.value); | 
| 405 | > | iconName.value = (u_char *)name; | 
| 406 | > | iconName.nitems = strlen((char *)windowName.value); | 
| 407 | > |  | 
| 408 | > | xclshints.res_name = NULL; | 
| 409 | > | xclshints.res_class = "Ximage"; | 
| 410 | > | XSetWMProperties(thedisplay, wind, &windowName, &iconName, | 
| 411 | > | argv, argc, &xszhints, &xwmhints, &xclshints); | 
| 412 | > | closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False); | 
| 413 | > | wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False); | 
| 414 | > | XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); | 
| 415 | > |  | 
| 416 |  | XMapWindow(thedisplay, wind); | 
| 293 | – | return; | 
| 294 | – | memerr: | 
| 295 | – | quiterr("out of memory"); | 
| 417 |  | } /* end of init */ | 
| 418 |  |  | 
| 419 |  |  | 
| 420 | < | quiterr(err)            /* print message and exit */ | 
| 421 | < | char  *err; | 
| 420 | > | static void | 
| 421 | > | quiterr(                /* print message and exit */ | 
| 422 | > | char  *err | 
| 423 | > | ) | 
| 424 |  | { | 
| 425 | < | if (err != NULL) { | 
| 426 | < | fprintf(stderr, "%s: %s\n", progname, err); | 
| 427 | < | exit(1); | 
| 425 | > | register int  es; | 
| 426 | > | int  cs; | 
| 427 | > |  | 
| 428 | > | if ( (es = (err != NULL)) ) | 
| 429 | > | fprintf(stderr, "%s: %s: %s\n", progname, | 
| 430 | > | fname==NULL?"<stdin>":fname, err); | 
| 431 | > | if (thedisplay != NULL) | 
| 432 | > | XCloseDisplay(thedisplay); | 
| 433 | > | if ((parent < 0) & (sigrecv == 0)) | 
| 434 | > | kill(getppid(), SIGCONT); | 
| 435 | > | while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ | 
| 436 | > | if (es == 0) | 
| 437 | > | es = cs>>8 & 0xff; | 
| 438 | > | parent--; | 
| 439 |  | } | 
| 440 | < | exit(0); | 
| 440 | > | exit(es); | 
| 441 |  | } | 
| 442 |  |  | 
| 443 |  |  | 
| 310 | – | eputs(s) | 
| 311 | – | char    *s; | 
| 312 | – | { | 
| 313 | – | fputs(s, stderr); | 
| 314 | – | } | 
| 315 | – |  | 
| 316 | – |  | 
| 317 | – | quit(code) | 
| 318 | – | int  code; | 
| 319 | – | { | 
| 320 | – | exit(code); | 
| 321 | – | } | 
| 322 | – |  | 
| 323 | – |  | 
| 444 |  | static int | 
| 445 | < | viscmp(v1,v2)           /* compare visual to see which is better, descending */ | 
| 446 | < | register XVisualInfo    *v1, *v2; | 
| 445 | > | viscmp(         /* compare visual to see which is better, descending */ | 
| 446 | > | register XVisualInfo    *v1, | 
| 447 | > | register XVisualInfo    *v2 | 
| 448 | > | ) | 
| 449 |  | { | 
| 450 |  | int     bad1 = 0, bad2 = 0; | 
| 451 |  | register int  *rp; | 
| 452 |  |  | 
| 453 |  | if (v1->class == v2->class) { | 
| 454 | < | if (v1->class == TrueColor || v1->class == DirectColor) { | 
| 455 | < | /* prefer 24-bit to 32-bit */ | 
| 456 | < | if (v1->depth == 24 && v2->depth == 32) | 
| 454 | > | if ((v1->class == TrueColor) | (v1->class == DirectColor)) { | 
| 455 | > | /* prefer 24-bit */ | 
| 456 | > | if ((v1->depth == 24) & (v2->depth > 24)) | 
| 457 |  | return(-1); | 
| 458 | < | if (v1->depth == 32 && v2->depth == 24) | 
| 458 | > | if ((v1->depth > 24) & (v2->depth == 24)) | 
| 459 |  | return(1); | 
| 460 | < | return(0); | 
| 460 | > | /* go for maximum depth otherwise */ | 
| 461 | > | return(v2->depth - v1->depth); | 
| 462 |  | } | 
| 463 |  | /* don't be too greedy */ | 
| 464 | < | if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth) | 
| 464 | > | if ((maxcolors <= 1<<v1->depth) & (maxcolors <= 1<<v2->depth)) | 
| 465 |  | return(v1->depth - v2->depth); | 
| 466 |  | return(v2->depth - v1->depth); | 
| 467 |  | } | 
| 468 | < | /* prefer Pseudo when < 24-bit */ | 
| 469 | < | if ((v1->class == TrueColor || v1->class == DirectColor) && | 
| 470 | < | v1->depth < 24) | 
| 468 | > | /* prefer Pseudo when < 15-bit */ | 
| 469 | > | if ((v1->class == TrueColor) | (v1->class == DirectColor) && | 
| 470 | > | v1->depth < 15) | 
| 471 |  | bad1 = 1; | 
| 472 | < | if ((v2->class == TrueColor || v2->class == DirectColor) && | 
| 473 | < | v2->depth < 24) | 
| 472 | > | if ((v2->class == TrueColor) | (v2->class == DirectColor) && | 
| 473 | > | v2->depth < 15) | 
| 474 |  | bad2 = -1; | 
| 475 |  | if (bad1 | bad2) | 
| 476 |  | return(bad1+bad2); | 
| 485 |  | } | 
| 486 |  |  | 
| 487 |  |  | 
| 488 | < | getbestvis()                    /* get the best visual for this screen */ | 
| 488 | > | static void | 
| 489 | > | getbestvis(void)                        /* get the best visual for this screen */ | 
| 490 |  | { | 
| 491 |  | #ifdef DEBUG | 
| 492 |  | static char  vistype[][12] = { | 
| 538 |  | if (viscmp(&xvi[i],&ourvis) > 0) | 
| 539 |  | quiterr("inadequate visuals on this screen"); | 
| 540 |  | /* OK, we'll use it */ | 
| 541 | < | copystruct(&ourvis, &xvi[i]); | 
| 541 | > | ourvis = xvi[i]; | 
| 542 |  | #ifdef DEBUG | 
| 543 |  | fprintf(stderr, "Selected visual type %s, depth %d\n", | 
| 544 |  | vistype[ourvis.class], ourvis.depth); | 
| 548 |  | greyscale = 1; | 
| 549 |  | if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors) | 
| 550 |  | maxcolors = ourvis.colormap_size; | 
| 427 | – | if (maxcolors > 4) | 
| 428 | – | maxcolors -= 2; | 
| 551 |  | if (ourvis.class == StaticGray) { | 
| 552 |  | ourblack = 0; | 
| 553 |  | ourwhite = 255; | 
| 558 |  | ourblack = 0; | 
| 559 |  | ourwhite = 1; | 
| 560 |  | } | 
| 561 | + | if (maxcolors > 4) | 
| 562 | + | maxcolors -= 2; | 
| 563 |  | } else { | 
| 564 |  | ourblack = 0; | 
| 565 |  | ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask; | 
| 568 |  | } | 
| 569 |  |  | 
| 570 |  |  | 
| 571 | < | getras()                                /* get raster file */ | 
| 571 | > | static void | 
| 572 | > | getras(void)                            /* get raster file */ | 
| 573 |  | { | 
| 449 | – | colormap        ourmap; | 
| 450 | – | XVisualInfo     vinfo; | 
| 451 | – |  | 
| 574 |  | if (maxcolors <= 2) {           /* monochrome */ | 
| 575 |  | ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); | 
| 576 |  | if (ourdata == NULL) | 
| 577 |  | goto fail; | 
| 578 | < | ourras = make_raster(thedisplay, &ourvis, 1, ourdata, | 
| 578 | > | ourras = make_raster(thedisplay, &ourvis, 1, (char *)ourdata, | 
| 579 |  | xmax, ymax, 8); | 
| 580 |  | if (ourras == NULL) | 
| 581 |  | goto fail; | 
| 582 |  | getmono(); | 
| 583 | < | } else if (ourvis.class == TrueColor || ourvis.class == DirectColor) { | 
| 584 | < | ourdata = (unsigned char *)malloc(4*xmax*ymax); | 
| 583 | > | } else if ((ourvis.class == TrueColor) | (ourvis.class == DirectColor)) { | 
| 584 | > | int  datsiz = ourvis.depth>16 ? sizeof(int32) : sizeof(int16); | 
| 585 | > | ourdata = (unsigned char *)malloc(datsiz*xmax*ymax); | 
| 586 |  | if (ourdata == NULL) | 
| 587 |  | goto fail; | 
| 588 | < | ourras = make_raster(thedisplay, &ourvis, 32, | 
| 589 | < | ourdata, xmax, ymax, 32); | 
| 588 | > | ourras = make_raster(thedisplay, &ourvis, datsiz*8, | 
| 589 | > | (char *)ourdata, xmax, ymax, datsiz*8); | 
| 590 |  | if (ourras == NULL) | 
| 591 |  | goto fail; | 
| 592 |  | getfull(); | 
| 594 |  | ourdata = (unsigned char *)malloc(xmax*ymax); | 
| 595 |  | if (ourdata == NULL) | 
| 596 |  | goto fail; | 
| 597 | < | ourras = make_raster(thedisplay, &ourvis, 8, ourdata, | 
| 597 | > | ourras = make_raster(thedisplay, &ourvis, 8, (char *)ourdata, | 
| 598 |  | xmax, ymax, 8); | 
| 599 |  | if (ourras == NULL) | 
| 600 |  | goto fail; | 
| 601 | < | if (ourvis.class == StaticGray) | 
| 601 | > | if (greyscale) | 
| 602 |  | getgrey(); | 
| 603 | < | else { | 
| 604 | < | if (greyscale) | 
| 605 | < | biq(dither,maxcolors,1,ourmap); | 
| 606 | < | else | 
| 484 | < | ciq(dither,maxcolors,1,ourmap); | 
| 485 | < | if (init_rcolors(ourras, ourmap[0], | 
| 486 | < | ourmap[1], ourmap[2]) == 0) | 
| 487 | < | goto fail; | 
| 488 | < | } | 
| 603 | > | else | 
| 604 | > | getmapped(); | 
| 605 | > | if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab)) | 
| 606 | > | goto fail; | 
| 607 |  | } | 
| 608 | < | return; | 
| 608 | > | return; | 
| 609 |  | fail: | 
| 610 |  | quiterr("could not create raster image"); | 
| 611 |  | } | 
| 612 |  |  | 
| 613 |  |  | 
| 614 | < | getevent()                              /* process the next event */ | 
| 614 | > | static void | 
| 615 | > | getevent(void)                          /* process the next event */ | 
| 616 |  | { | 
| 617 | < | union { | 
| 499 | < | XEvent  u; | 
| 500 | < | XConfigureEvent  c; | 
| 501 | < | XExposeEvent  e; | 
| 502 | < | XButtonPressedEvent  b; | 
| 503 | < | XKeyPressedEvent  k; | 
| 504 | < | } e; | 
| 617 | > | XEvent xev; | 
| 618 |  |  | 
| 619 | < | XNextEvent(thedisplay, &e.u); | 
| 620 | < | switch (e.u.type) { | 
| 619 | > | XNextEvent(thedisplay, &xev); | 
| 620 | > | switch ((int)xev.type) { | 
| 621 |  | case KeyPress: | 
| 622 | < | docom(&e.k); | 
| 622 | > | docom(&xev.xkey); | 
| 623 |  | break; | 
| 624 |  | case ConfigureNotify: | 
| 625 | < | width = e.c.width; | 
| 626 | < | height = e.c.height; | 
| 625 | > | width = xev.xconfigure.width; | 
| 626 | > | height = xev.xconfigure.height; | 
| 627 |  | break; | 
| 628 |  | case MapNotify: | 
| 629 |  | map_rcolors(ourras, wind); | 
| 630 |  | if (fast) | 
| 631 |  | make_rpixmap(ourras, wind); | 
| 632 | + | if ((!sequential) & (parent < 0) & (sigrecv == 0)) { | 
| 633 | + | kill(getppid(), SIGCONT); | 
| 634 | + | sigrecv--; | 
| 635 | + | } | 
| 636 |  | break; | 
| 637 |  | case UnmapNotify: | 
| 638 |  | if (!fast) | 
| 639 |  | unmap_rcolors(ourras); | 
| 640 |  | break; | 
| 641 |  | case Expose: | 
| 642 | < | redraw(e.e.x, e.e.y, e.e.width, e.e.height); | 
| 642 | > | redraw(xev.xexpose.x, xev.xexpose.y, | 
| 643 | > | xev.xexpose.width, xev.xexpose.height); | 
| 644 |  | break; | 
| 645 |  | case ButtonPress: | 
| 646 | < | if (e.b.state & (ShiftMask|ControlMask)) | 
| 647 | < | moveimage(&e.b); | 
| 646 | > | if (xev.xbutton.state & (ShiftMask|ControlMask)) | 
| 647 | > | moveimage(&xev.xbutton); | 
| 648 |  | else | 
| 649 | < | getbox(&e.b); | 
| 649 | > | switch (xev.xbutton.button) { | 
| 650 | > | case Button1: | 
| 651 | > | getbox(&xev.xbutton); | 
| 652 | > | break; | 
| 653 | > | case Button2: | 
| 654 | > | traceray(xev.xbutton.x, xev.xbutton.y); | 
| 655 | > | break; | 
| 656 | > | case Button3: | 
| 657 | > | trackrays(&xev.xbutton); | 
| 658 | > | break; | 
| 659 | > | } | 
| 660 |  | break; | 
| 661 | + | case ClientMessage: | 
| 662 | + | if ((xev.xclient.message_type == wmProtocolsAtom) && | 
| 663 | + | (xev.xclient.data.l[0] == closedownAtom)) | 
| 664 | + | quiterr(NULL); | 
| 665 | + | break; | 
| 666 |  | } | 
| 667 |  | } | 
| 668 |  |  | 
| 669 |  |  | 
| 670 | < | docom(ekey)                                     /* execute command */ | 
| 671 | < | XKeyPressedEvent  *ekey; | 
| 670 | > | static int | 
| 671 | > | traceray(                       /* print requested pixel data */ | 
| 672 | > | int  xpos, | 
| 673 | > | int  ypos | 
| 674 | > | ) | 
| 675 |  | { | 
| 676 | + | RREAL  hv[2]; | 
| 677 | + | FVECT  rorg, rdir; | 
| 678 | + | COLOR  cval; | 
| 679 | + | register char  *cp; | 
| 680 | + |  | 
| 681 | + | bbox.xmin = xpos; bbox.xsiz = 1; | 
| 682 | + | bbox.ymin = ypos; bbox.ysiz = 1; | 
| 683 | + | avgbox(cval); | 
| 684 | + | scalecolor(cval, 1./exposure); | 
| 685 | + | pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); | 
| 686 | + | if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) | 
| 687 | + | rorg[0] = rorg[1] = rorg[2] = | 
| 688 | + | rdir[0] = rdir[1] = rdir[2] = 0.; | 
| 689 | + |  | 
| 690 | + | for (cp = tout; *cp; cp++)      /* print what they asked for */ | 
| 691 | + | switch (*cp) { | 
| 692 | + | case 'o':                       /* origin */ | 
| 693 | + | printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); | 
| 694 | + | break; | 
| 695 | + | case 'd':                       /* direction */ | 
| 696 | + | printf("%e %e %e ", rdir[0], rdir[1], rdir[2]); | 
| 697 | + | break; | 
| 698 | + | case 'v':                       /* radiance value */ | 
| 699 | + | printf("%e %e %e ", colval(cval,RED), | 
| 700 | + | colval(cval,GRN), colval(cval,BLU)); | 
| 701 | + | break; | 
| 702 | + | case 'l':                       /* luminance */ | 
| 703 | + | printf("%e ", luminance(cval)); | 
| 704 | + | break; | 
| 705 | + | case 'p':                       /* pixel position */ | 
| 706 | + | printf("%d %d ", (int)(hv[0]*inpres.xr), | 
| 707 | + | (int)(hv[1]*inpres.yr)); | 
| 708 | + | break; | 
| 709 | + | } | 
| 710 | + | putchar('\n'); | 
| 711 | + | fflush(stdout); | 
| 712 | + | return(0); | 
| 713 | + | } | 
| 714 | + |  | 
| 715 | + |  | 
| 716 | + | static int | 
| 717 | + | docom(                          /* execute command */ | 
| 718 | + | XKeyPressedEvent  *ekey | 
| 719 | + | ) | 
| 720 | + | { | 
| 721 |  | char  buf[80]; | 
| 722 |  | COLOR  cval; | 
| 723 |  | XColor  cvx; | 
| 724 |  | int  com, n; | 
| 725 |  | double  comp; | 
| 726 | < | FLOAT  hv[2]; | 
| 546 | < | FVECT  rorg, rdir; | 
| 726 | > | RREAL  hv[2]; | 
| 727 |  |  | 
| 728 |  | n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); | 
| 729 |  | if (n == 0) | 
| 731 |  | com = buf[0]; | 
| 732 |  | switch (com) {                  /* interpret command */ | 
| 733 |  | case 'q': | 
| 734 | + | case 'Q': | 
| 735 |  | case CTRL('D'):                         /* quit */ | 
| 736 | < | quit(0); | 
| 736 | > | quiterr(NULL); | 
| 737 |  | case '\n': | 
| 738 |  | case '\r': | 
| 739 |  | case 'l': | 
| 740 |  | case 'c':                               /* value */ | 
| 741 | < | if (avgbox(cval) == -1) | 
| 741 | > | if (!avgbox(cval)) | 
| 742 |  | return(-1); | 
| 743 |  | switch (com) { | 
| 744 |  | case '\n': | 
| 746 |  | sprintf(buf, "%.3f", intens(cval)/exposure); | 
| 747 |  | break; | 
| 748 |  | case 'l':                               /* luminance */ | 
| 749 | < | sprintf(buf, "%.0fL", luminance(cval)/exposure); | 
| 749 | > | sprintf(buf, "%.1fL", luminance(cval)/exposure); | 
| 750 |  | break; | 
| 751 |  | case 'c':                               /* color */ | 
| 752 |  | comp = pow(2.0, (double)scale); | 
| 757 |  | break; | 
| 758 |  | } | 
| 759 |  | XDrawImageString(thedisplay, wind, ourgc, | 
| 760 | < | box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); | 
| 760 | > | bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf)); | 
| 761 |  | return(0); | 
| 762 |  | case 'i':                               /* identify (contour) */ | 
| 763 |  | if (ourras->pixels == NULL) | 
| 779 |  | buf, strlen(buf)); | 
| 780 |  | return(0); | 
| 781 |  | case 't':                               /* trace */ | 
| 782 | < | if (!gotview) { | 
| 783 | < | XBell(thedisplay, 0); | 
| 782 | > | return(traceray(ekey->x, ekey->y)); | 
| 783 | > | case 'a':                               /* auto exposure */ | 
| 784 | > | if (fname == NULL) | 
| 785 |  | return(-1); | 
| 786 | < | } | 
| 787 | < | pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); | 
| 788 | < | if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) | 
| 786 | > | tmflags = TM_F_CAMERA; | 
| 787 | > | strcpy(buf, "auto exposure..."); | 
| 788 | > | goto remap; | 
| 789 | > | case 'h':                               /* human response */ | 
| 790 | > | if (fname == NULL) | 
| 791 |  | return(-1); | 
| 792 | < | printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); | 
| 793 | < | printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); | 
| 794 | < | fflush(stdout); | 
| 611 | < | return(0); | 
| 792 | > | tmflags = TM_F_HUMAN; | 
| 793 | > | strcpy(buf, "human exposure..."); | 
| 794 | > | goto remap; | 
| 795 |  | case '=':                               /* adjust exposure */ | 
| 796 | < | if (avgbox(cval) == -1) | 
| 796 | > | case '@':                               /* adaptation level */ | 
| 797 | > | if (!avgbox(cval)) | 
| 798 |  | return(-1); | 
| 799 | < | n = log(.5/bright(cval))/.69315 - scale;        /* truncate */ | 
| 800 | < | if (n == 0) | 
| 801 | < | return(0); | 
| 802 | < | scale_rcolors(ourras, pow(2.0, (double)n)); | 
| 799 | > | comp = bright(cval); | 
| 800 | > | if (comp < 1e-20) { | 
| 801 | > | XBell(thedisplay, 0); | 
| 802 | > | return(-1); | 
| 803 | > | } | 
| 804 | > | if (com == '@') | 
| 805 | > | comp = 106./exposure/ | 
| 806 | > | pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5); | 
| 807 | > | else | 
| 808 | > | comp = .5/comp; | 
| 809 | > | comp = log(comp)/.69315 - scale; | 
| 810 | > | n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */ | 
| 811 | > | if (tmflags != TM_F_LINEAR) | 
| 812 | > | tmflags = TM_F_LINEAR;  /* turn off tone mapping */ | 
| 813 | > | else { | 
| 814 | > | if (n == 0)             /* else check if any change */ | 
| 815 | > | return(0); | 
| 816 | > | scale_rcolors(ourras, pow(2.0, (double)n)); | 
| 817 | > | } | 
| 818 |  | scale += n; | 
| 819 |  | sprintf(buf, "%+d", scale); | 
| 820 | + | remap: | 
| 821 |  | XDrawImageString(thedisplay, wind, ourgc, | 
| 822 | < | box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); | 
| 822 | > | bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf)); | 
| 823 |  | XFlush(thedisplay); | 
| 824 | < | free(ourdata); | 
| 824 | > | /* free(ourdata);       This is done in XDestroyImage()! */ | 
| 825 |  | free_raster(ourras); | 
| 826 |  | getras(); | 
| 827 |  | /* fall through */ | 
| 834 |  | make_rpixmap(ourras, wind); | 
| 835 |  | redraw(0, 0, width, height); | 
| 836 |  | return(0); | 
| 837 | + | case 'f':                               /* turn on fast redraw */ | 
| 838 | + | fast = 1; | 
| 839 | + | make_rpixmap(ourras, wind); | 
| 840 | + | return(0); | 
| 841 | + | case 'F':                               /* turn off fast redraw */ | 
| 842 | + | fast = 0; | 
| 843 | + | free_rpixmap(ourras); | 
| 844 | + | return(0); | 
| 845 | + | case '0':                               /* recenter origin */ | 
| 846 | + | if ((xoff == 0) & (yoff == 0)) | 
| 847 | + | return(0); | 
| 848 | + | xoff = yoff = 0; | 
| 849 | + | XClearWindow(thedisplay, wind); | 
| 850 | + | redraw(0, 0, width, height); | 
| 851 | + | return(0); | 
| 852 |  | case ' ':                               /* clear */ | 
| 853 | < | redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); | 
| 853 | > | redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz); | 
| 854 |  | return(0); | 
| 855 |  | default: | 
| 856 |  | XBell(thedisplay, 0); | 
| 859 |  | } | 
| 860 |  |  | 
| 861 |  |  | 
| 862 | < | moveimage(ebut)                         /* shift the image */ | 
| 863 | < | XButtonPressedEvent  *ebut; | 
| 862 | > | static void | 
| 863 | > | moveimage(                              /* shift the image */ | 
| 864 | > | XButtonPressedEvent  *ebut | 
| 865 | > | ) | 
| 866 |  | { | 
| 867 | < | union { | 
| 651 | < | XEvent  u; | 
| 652 | < | XButtonReleasedEvent  b; | 
| 653 | < | XPointerMovedEvent  m; | 
| 654 | < | }  e; | 
| 867 | > | XEvent  e; | 
| 868 |  | int     mxo, myo; | 
| 869 |  |  | 
| 870 | < | XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); | 
| 871 | < | while (e.u.type == MotionNotify) { | 
| 872 | < | mxo = e.m.x; | 
| 873 | < | myo = e.m.y; | 
| 870 | > | XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); | 
| 871 | > | while (e.type == MotionNotify) { | 
| 872 | > | mxo = e.xmotion.x; | 
| 873 | > | myo = e.xmotion.y; | 
| 874 |  | revline(ebut->x, ebut->y, mxo, myo); | 
| 875 |  | revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, | 
| 876 |  | xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); | 
| 877 | < | XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); | 
| 877 | > | XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); | 
| 878 |  | revline(ebut->x, ebut->y, mxo, myo); | 
| 879 |  | revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, | 
| 880 |  | xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); | 
| 881 |  | } | 
| 882 | < | xoff += e.b.x - ebut->x; | 
| 883 | < | yoff += e.b.y - ebut->y; | 
| 882 | > | xoff += e.xbutton.x - ebut->x; | 
| 883 | > | yoff += e.xbutton.y - ebut->y; | 
| 884 |  | XClearWindow(thedisplay, wind); | 
| 885 |  | redraw(0, 0, width, height); | 
| 886 |  | } | 
| 887 |  |  | 
| 888 |  |  | 
| 889 | < | getbox(ebut)                            /* get new box */ | 
| 890 | < | XButtonPressedEvent  *ebut; | 
| 889 | > | static void | 
| 890 | > | getbox(                         /* get new bbox */ | 
| 891 | > | XButtonPressedEvent  *ebut | 
| 892 | > | ) | 
| 893 |  | { | 
| 894 | < | union { | 
| 680 | < | XEvent  u; | 
| 681 | < | XButtonReleasedEvent  b; | 
| 682 | < | XPointerMovedEvent  m; | 
| 683 | < | }  e; | 
| 894 | > | XEvent  e; | 
| 895 |  |  | 
| 896 | < | XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); | 
| 897 | < | while (e.u.type == MotionNotify) { | 
| 898 | < | revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y); | 
| 899 | < | XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); | 
| 900 | < | revbox(ebut->x, ebut->y, box.xmin, box.ymin); | 
| 896 | > | XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); | 
| 897 | > | while (e.type == MotionNotify) { | 
| 898 | > | revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y); | 
| 899 | > | XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); | 
| 900 | > | revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin); | 
| 901 |  | } | 
| 902 | < | box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x); | 
| 903 | < | box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y); | 
| 904 | < | if (box.xmin > ebut->x) { | 
| 905 | < | box.xsiz = box.xmin - ebut->x + 1; | 
| 906 | < | box.xmin = ebut->x; | 
| 902 | > | bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); | 
| 903 | > | bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); | 
| 904 | > | if (bbox.xmin > ebut->x) { | 
| 905 | > | bbox.xsiz = bbox.xmin - ebut->x + 1; | 
| 906 | > | bbox.xmin = ebut->x; | 
| 907 |  | } else { | 
| 908 | < | box.xsiz = ebut->x - box.xmin + 1; | 
| 908 | > | bbox.xsiz = ebut->x - bbox.xmin + 1; | 
| 909 |  | } | 
| 910 | < | if (box.ymin > ebut->y) { | 
| 911 | < | box.ysiz = box.ymin - ebut->y + 1; | 
| 912 | < | box.ymin = ebut->y; | 
| 910 | > | if (bbox.ymin > ebut->y) { | 
| 911 | > | bbox.ysiz = bbox.ymin - ebut->y + 1; | 
| 912 | > | bbox.ymin = ebut->y; | 
| 913 |  | } else { | 
| 914 | < | box.ysiz = ebut->y - box.ymin + 1; | 
| 914 | > | bbox.ysiz = ebut->y - bbox.ymin + 1; | 
| 915 |  | } | 
| 916 |  | } | 
| 917 |  |  | 
| 918 |  |  | 
| 919 | < | revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */ | 
| 920 | < | int  x0, y0, x1, y1; | 
| 919 | > | static void | 
| 920 | > | trackrays(                              /* trace rays as mouse moves */ | 
| 921 | > | XButtonPressedEvent  *ebut | 
| 922 | > | ) | 
| 923 |  | { | 
| 924 | + | XEvent  e; | 
| 925 | + | unsigned long   lastrept; | 
| 926 | + |  | 
| 927 | + | traceray(ebut->x, ebut->y); | 
| 928 | + | lastrept = ebut->time; | 
| 929 | + | XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); | 
| 930 | + | while (e.type == MotionNotify) { | 
| 931 | + | if (e.xmotion.time >= lastrept + tinterv) { | 
| 932 | + | traceray(e.xmotion.x, e.xmotion.y); | 
| 933 | + | lastrept = e.xmotion.time; | 
| 934 | + | } | 
| 935 | + | XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); | 
| 936 | + | } | 
| 937 | + | } | 
| 938 | + |  | 
| 939 | + |  | 
| 940 | + | static void | 
| 941 | + | revbox(                 /* draw bbox with reversed lines */ | 
| 942 | + | int  x0, | 
| 943 | + | int  y0, | 
| 944 | + | int  x1, | 
| 945 | + | int  y1 | 
| 946 | + | ) | 
| 947 | + | { | 
| 948 |  | revline(x0, y0, x1, y0); | 
| 949 |  | revline(x0, y1, x1, y1); | 
| 950 |  | revline(x0, y0, x0, y1); | 
| 952 |  | } | 
| 953 |  |  | 
| 954 |  |  | 
| 955 | < | avgbox(clr)                             /* average color over current box */ | 
| 956 | < | COLOR  clr; | 
| 955 | > | static void | 
| 956 | > | colavg( | 
| 957 | > | register COLR   *scn, | 
| 958 | > | register int    n, | 
| 959 | > | void *cavg | 
| 960 | > | ) | 
| 961 |  | { | 
| 962 | < | static COLOR  lc; | 
| 963 | < | static int  ll, lr, lt, lb; | 
| 962 | > | COLOR   col; | 
| 963 | > |  | 
| 964 | > | while (n--) { | 
| 965 | > | colr_color(col, *scn++); | 
| 966 | > | addcolor((COLORV*)cavg, col); | 
| 967 | > | } | 
| 968 | > | } | 
| 969 | > |  | 
| 970 | > |  | 
| 971 | > | static int | 
| 972 | > | avgbox(                         /* average color over current bbox */ | 
| 973 | > | COLOR   cavg | 
| 974 | > | ) | 
| 975 | > | { | 
| 976 | > | double  d; | 
| 977 | > | register int    rval; | 
| 978 | > |  | 
| 979 | > | setcolor(cavg, 0., 0., 0.); | 
| 980 | > | rval = dobox(colavg, (void *)cavg); | 
| 981 | > | if (rval > 0) { | 
| 982 | > | d = 1./rval; | 
| 983 | > | scalecolor(cavg, d); | 
| 984 | > | } | 
| 985 | > | return(rval); | 
| 986 | > | } | 
| 987 | > |  | 
| 988 | > |  | 
| 989 | > | static int | 
| 990 | > | dobox(                          /* run function over bbox */ | 
| 991 | > | //void  (*f)(),                 /* function to call for each subscan */ | 
| 992 | > | doboxf_t *f,                    /* function to call for each subscan */ | 
| 993 | > | void    *p                      /* pointer to private data */ | 
| 994 | > | ) | 
| 995 | > | { | 
| 996 |  | int  left, right, top, bottom; | 
| 997 |  | int  y; | 
| 725 | – | double  d; | 
| 726 | – | COLOR  ctmp; | 
| 727 | – | register int  x; | 
| 998 |  |  | 
| 999 | < | setcolor(clr, 0.0, 0.0, 0.0); | 
| 1000 | < | left = box.xmin - xoff; | 
| 731 | < | right = left + box.xsiz; | 
| 999 | > | left = bbox.xmin - xoff; | 
| 1000 | > | right = left + bbox.xsiz; | 
| 1001 |  | if (left < 0) | 
| 1002 |  | left = 0; | 
| 1003 |  | if (right > xmax) | 
| 1004 |  | right = xmax; | 
| 1005 |  | if (left >= right) | 
| 1006 | < | return(-1); | 
| 1007 | < | top = box.ymin - yoff; | 
| 1008 | < | bottom = top + box.ysiz; | 
| 1006 | > | return(0); | 
| 1007 | > | top = bbox.ymin - yoff; | 
| 1008 | > | bottom = top + bbox.ysiz; | 
| 1009 |  | if (top < 0) | 
| 1010 |  | top = 0; | 
| 1011 |  | if (bottom > ymax) | 
| 1012 |  | bottom = ymax; | 
| 1013 |  | if (top >= bottom) | 
| 1014 | < | return(-1); | 
| 746 | < | if (left == ll && right == lr && top == lt && bottom == lb) { | 
| 747 | < | copycolor(clr, lc); | 
| 748 | < | return; | 
| 749 | < | } | 
| 1014 | > | return(0); | 
| 1015 |  | for (y = top; y < bottom; y++) { | 
| 1016 |  | if (getscan(y) == -1) | 
| 1017 |  | return(-1); | 
| 1018 | < | for (x = left; x < right; x++) { | 
| 1019 | < | colr_color(ctmp, scanline[x]); | 
| 1020 | < | addcolor(clr, ctmp); | 
| 1018 | > | (*f)(scanline+left, right-left, p); | 
| 1019 | > | } | 
| 1020 | > | return((right-left)*(bottom-top)); | 
| 1021 | > | } | 
| 1022 | > |  | 
| 1023 | > |  | 
| 1024 | > | static void | 
| 1025 | > | addfix(                 /* add fixation points to histogram */ | 
| 1026 | > | COLR    *scn, | 
| 1027 | > | int     n, | 
| 1028 | > | void    *p | 
| 1029 | > | ) | 
| 1030 | > | { | 
| 1031 | > | TMstruct *      tms = (TMstruct *)p; | 
| 1032 | > |  | 
| 1033 | > | if (tmCvColrs(tms, lscan, TM_NOCHROM, scn, n)) | 
| 1034 | > | goto tmerr; | 
| 1035 | > | if (tmAddHisto(tms, lscan, n, FIXWEIGHT)) | 
| 1036 | > | goto tmerr; | 
| 1037 | > | return; | 
| 1038 | > | tmerr: | 
| 1039 | > | quiterr("tone mapping error"); | 
| 1040 | > | } | 
| 1041 | > |  | 
| 1042 | > |  | 
| 1043 | > | static void | 
| 1044 | > | make_tonemap(void)                      /* initialize tone mapping */ | 
| 1045 | > | { | 
| 1046 | > | int  flags, y; | 
| 1047 | > |  | 
| 1048 | > | if (tmflags != TM_F_LINEAR && fname == NULL) { | 
| 1049 | > | fprintf(stderr, "%s: cannot adjust tone of standard input\n", | 
| 1050 | > | progname); | 
| 1051 | > | tmflags = TM_F_LINEAR; | 
| 1052 | > | } | 
| 1053 | > | if (tmflags == TM_F_LINEAR) {   /* linear with clamping */ | 
| 1054 | > | setcolrcor(pow, 1.0/gamcor); | 
| 1055 | > | return; | 
| 1056 | > | } | 
| 1057 | > | flags = tmflags;                /* histogram adjustment */ | 
| 1058 | > | if (greyscale) flags |= TM_F_BW; | 
| 1059 | > | if (tmGlobal != NULL) {         /* reuse old histogram if one */ | 
| 1060 | > | tmDone(tmCurrent); tmCurrent = NULL; | 
| 1061 | > | tmGlobal->flags = flags; | 
| 1062 | > | } else {                        /* else initialize */ | 
| 1063 | > | if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) | 
| 1064 | > | goto memerr; | 
| 1065 | > | if (greyscale) { | 
| 1066 | > | cscan = TM_NOCHROM; | 
| 1067 | > | if ((pscan = (uby8 *)malloc(sizeof(uby8)*xmax)) == NULL) | 
| 1068 | > | goto memerr; | 
| 1069 | > | } else if ((pscan=cscan = (uby8 *)malloc(3*sizeof(uby8)*xmax)) | 
| 1070 | > | == NULL) | 
| 1071 | > | goto memerr; | 
| 1072 | > | /* initialize tm library */ | 
| 1073 | > | tmGlobal = tmInit(flags, stdprims, gamcor); | 
| 1074 | > | if (tmGlobal == NULL) | 
| 1075 | > | goto memerr; | 
| 1076 | > | if (tmSetSpace(tmGlobal, stdprims, WHTEFFICACY/exposure)) | 
| 1077 | > | goto tmerr; | 
| 1078 | > | /* compute picture histogram */ | 
| 1079 | > | for (y = 0; y < ymax; y++) { | 
| 1080 | > | getscan(y); | 
| 1081 | > | if (tmCvColrs(tmGlobal, lscan, TM_NOCHROM, | 
| 1082 | > | scanline, xmax)) | 
| 1083 | > | goto tmerr; | 
| 1084 | > | if (tmAddHisto(tmGlobal, lscan, xmax, 1)) | 
| 1085 | > | goto tmerr; | 
| 1086 |  | } | 
| 1087 |  | } | 
| 1088 | < | d = 1.0/((right-left)*(bottom-top)); | 
| 1089 | < | scalecolor(clr, d); | 
| 1090 | < | ll = left; lr = right; lt = top; lb = bottom; | 
| 1091 | < | copycolor(lc, clr); | 
| 1092 | < | return(0); | 
| 1088 | > | tmCurrent = tmDup(tmGlobal);    /* add fixations to duplicate map */ | 
| 1089 | > | dobox(addfix, (void *)tmCurrent); | 
| 1090 | > | /* (re)compute tone mapping */ | 
| 1091 | > | if (tmComputeMapping(tmCurrent, gamcor, 0., 0.)) | 
| 1092 | > | goto tmerr; | 
| 1093 | > | return; | 
| 1094 | > | memerr: | 
| 1095 | > | quiterr("out of memory in make_tonemap"); | 
| 1096 | > | tmerr: | 
| 1097 | > | quiterr("tone mapping error"); | 
| 1098 |  | } | 
| 1099 |  |  | 
| 1100 |  |  | 
| 1101 | < | getmono()                       /* get monochrome data */ | 
| 1101 | > | static void | 
| 1102 | > | tmap_colrs(             /* apply tone mapping to scanline */ | 
| 1103 | > | register COLR  *scn, | 
| 1104 | > | int  len | 
| 1105 | > | ) | 
| 1106 |  | { | 
| 1107 | + | register uby8  *ps; | 
| 1108 | + |  | 
| 1109 | + | if (tmflags == TM_F_LINEAR) { | 
| 1110 | + | if (scale) | 
| 1111 | + | shiftcolrs(scn, len, scale); | 
| 1112 | + | colrs_gambs(scn, len); | 
| 1113 | + | return; | 
| 1114 | + | } | 
| 1115 | + | if (len > xmax) | 
| 1116 | + | quiterr("code error 1 in tmap_colrs"); | 
| 1117 | + | if (tmCvColrs(tmCurrent, lscan, cscan, scn, len)) | 
| 1118 | + | goto tmerr; | 
| 1119 | + | if (tmMapPixels(tmCurrent, pscan, lscan, cscan, len)) | 
| 1120 | + | goto tmerr; | 
| 1121 | + | ps = pscan; | 
| 1122 | + | if (greyscale) | 
| 1123 | + | while (len--) { | 
| 1124 | + | scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++; | 
| 1125 | + | scn[0][EXP] = COLXS; | 
| 1126 | + | scn++; | 
| 1127 | + | } | 
| 1128 | + | else | 
| 1129 | + | while (len--) { | 
| 1130 | + | scn[0][RED] = *ps++; | 
| 1131 | + | scn[0][GRN] = *ps++; | 
| 1132 | + | scn[0][BLU] = *ps++; | 
| 1133 | + | scn[0][EXP] = COLXS; | 
| 1134 | + | scn++; | 
| 1135 | + | } | 
| 1136 | + | return; | 
| 1137 | + | tmerr: | 
| 1138 | + | quiterr("tone mapping error"); | 
| 1139 | + | } | 
| 1140 | + |  | 
| 1141 | + |  | 
| 1142 | + | static void | 
| 1143 | + | getmono(void)                   /* get monochrome data */ | 
| 1144 | + | { | 
| 1145 |  | register unsigned char  *dp; | 
| 1146 |  | register int    x, err; | 
| 1147 |  | int     y, errp; | 
| 1151 |  | quiterr("out of memory in getmono"); | 
| 1152 |  | dp = ourdata - 1; | 
| 1153 |  | for (y = 0; y < ymax; y++) { | 
| 1154 | < | if (getscan(y) < 0) | 
| 778 | < | quiterr("seek error in getmono"); | 
| 779 | < | normcolrs(scanline, xmax, scale); | 
| 1154 | > | getscan(y); | 
| 1155 |  | add2icon(y, scanline); | 
| 1156 | + | normcolrs(scanline, xmax, scale); | 
| 1157 |  | err = 0; | 
| 1158 |  | for (x = 0; x < xmax; x++) { | 
| 1159 |  | if (!(x&7)) | 
| 1168 |  | cerr[x] = err + errp; | 
| 1169 |  | } | 
| 1170 |  | } | 
| 1171 | < | free((char *)cerr); | 
| 1171 | > | free((void *)cerr); | 
| 1172 |  | } | 
| 1173 |  |  | 
| 1174 |  |  | 
| 1175 | < | add2icon(y, scan)               /* add a scanline to our icon data */ | 
| 1176 | < | int  y; | 
| 1177 | < | COLR  *scan; | 
| 1175 | > | static void | 
| 1176 | > | add2icon(               /* add a scanline to our icon data */ | 
| 1177 | > | int  y, | 
| 1178 | > | COLR  *scan | 
| 1179 | > | ) | 
| 1180 |  | { | 
| 1181 |  | static short  cerr[ICONSIZ]; | 
| 1182 |  | static int  ynext; | 
| 1183 |  | static char  *dp; | 
| 1184 | + | COLR  clr; | 
| 1185 |  | register int  err; | 
| 1186 |  | register int    x, ti; | 
| 1187 |  | int  errp; | 
| 1212 |  | *++dp = 0; | 
| 1213 |  | errp = err; | 
| 1214 |  | ti = x*xmax/iconwidth; | 
| 1215 | < | err += normbright(scan[ti]) + cerr[x]; | 
| 1215 | > | copycolr(clr, scan[ti]); | 
| 1216 | > | normcolrs(&clr, 1, scale); | 
| 1217 | > | err += normbright(clr) + cerr[x]; | 
| 1218 |  | if (err > 127) | 
| 1219 |  | err -= 255; | 
| 1220 |  | else | 
| 1226 |  | } | 
| 1227 |  |  | 
| 1228 |  |  | 
| 1229 | < | getfull()                       /* get full (24-bit) data */ | 
| 1229 | > | static void | 
| 1230 | > | getfull(void)                   /* get full (24-bit) data */ | 
| 1231 |  | { | 
| 1232 |  | int     y; | 
| 1233 | < | register unsigned long  *dp; | 
| 1233 | > | register uint32 *dp; | 
| 1234 | > | register uint16 *dph; | 
| 1235 |  | register int    x; | 
| 1236 | < | /* set gamma correction */ | 
| 1237 | < | setcolrgam(gamcor); | 
| 1236 | > | /* initialize tone mapping */ | 
| 1237 | > | make_tonemap(); | 
| 1238 |  | /* read and convert file */ | 
| 1239 | < | dp = (unsigned long *)ourdata; | 
| 1239 | > | dp = (uint32 *)ourdata; | 
| 1240 | > | dph = (uint16 *)ourdata; | 
| 1241 |  | for (y = 0; y < ymax; y++) { | 
| 1242 | < | if (getscan(y) < 0) | 
| 859 | < | quiterr("seek error in getfull"); | 
| 860 | < | if (scale) | 
| 861 | < | shiftcolrs(scanline, xmax, scale); | 
| 862 | < | colrs_gambs(scanline, xmax); | 
| 1242 | > | getscan(y); | 
| 1243 |  | add2icon(y, scanline); | 
| 1244 | < | if (ourras->image->blue_mask & 1) | 
| 1244 | > | tmap_colrs(scanline, xmax); | 
| 1245 | > | switch (ourras->image->blue_mask) { | 
| 1246 | > | case 0xff:              /* 24-bit RGB */ | 
| 1247 |  | for (x = 0; x < xmax; x++) | 
| 1248 | < | *dp++ = scanline[x][RED] << 16 | | 
| 1249 | < | scanline[x][GRN] << 8 | | 
| 1250 | < | scanline[x][BLU] ; | 
| 1251 | < | else | 
| 1248 | > | *dp++ = (uint32)scanline[x][RED] << 16 | | 
| 1249 | > | (uint32)scanline[x][GRN] << 8 | | 
| 1250 | > | (uint32)scanline[x][BLU] ; | 
| 1251 | > | break; | 
| 1252 | > | case 0xff0000:          /* 24-bit BGR */ | 
| 1253 |  | for (x = 0; x < xmax; x++) | 
| 1254 | < | *dp++ = scanline[x][RED] | | 
| 1255 | < | scanline[x][GRN] << 8 | | 
| 1256 | < | scanline[x][BLU] << 16 ; | 
| 1254 | > | *dp++ = (uint32)scanline[x][RED] | | 
| 1255 | > | (uint32)scanline[x][GRN] << 8 | | 
| 1256 | > | (uint32)scanline[x][BLU] << 16 ; | 
| 1257 | > | break; | 
| 1258 | > | #if 0 | 
| 1259 | > | case 0x1f:              /* 15-bit RGB */ | 
| 1260 | > | for (x = 0; x < xmax; x++) | 
| 1261 | > | *dph++ =        (scanline[x][RED] << 7 & 0x7c00) | | 
| 1262 | > | (scanline[x][GRN] << 2 & 0x3e0) | | 
| 1263 | > | (unsigned)scanline[x][BLU] >> 3 ; | 
| 1264 | > | break; | 
| 1265 | > | case 0x7c00:            /* 15-bit BGR */ | 
| 1266 | > | for (x = 0; x < xmax; x++) | 
| 1267 | > | *dph++ =        (unsigned)scanline[x][RED] >> 3 | | 
| 1268 | > | (scanline[x][GRN] << 2 & 0x3e0) | | 
| 1269 | > | (scanline[x][BLU] << 7 & 0x7c00) ; | 
| 1270 | > | break; | 
| 1271 | > | #endif | 
| 1272 | > | default:                /* unknown */ | 
| 1273 | > | if (ourvis.depth > 16) | 
| 1274 | > | for (x = 0; x < xmax; x++) { | 
| 1275 | > | *dp = ourras->image->red_mask & | 
| 1276 | > | ourras->image->red_mask*scanline[x][RED]/255; | 
| 1277 | > | *dp |= ourras->image->green_mask & | 
| 1278 | > | ourras->image->green_mask*scanline[x][GRN]/255; | 
| 1279 | > | *dp++ |= ourras->image->blue_mask & | 
| 1280 | > | ourras->image->blue_mask*scanline[x][BLU]/255; | 
| 1281 | > | } | 
| 1282 | > | else | 
| 1283 | > | for (x = 0; x < xmax; x++) { | 
| 1284 | > | *dph = ourras->image->red_mask & | 
| 1285 | > | ourras->image->red_mask*scanline[x][RED]/255; | 
| 1286 | > | *dph |= ourras->image->green_mask & | 
| 1287 | > | ourras->image->green_mask*scanline[x][GRN]/255; | 
| 1288 | > | *dph++ |= ourras->image->blue_mask & | 
| 1289 | > | ourras->image->blue_mask*scanline[x][BLU]/255; | 
| 1290 | > | } | 
| 1291 | > | break; | 
| 1292 | > | } | 
| 1293 |  | } | 
| 1294 |  | } | 
| 1295 |  |  | 
| 1296 |  |  | 
| 1297 | < | getgrey()                       /* get greyscale data */ | 
| 1297 | > | static void | 
| 1298 | > | getgrey(void)                   /* get greyscale data */ | 
| 1299 |  | { | 
| 1300 |  | int     y; | 
| 1301 |  | register unsigned char  *dp; | 
| 1302 |  | register int    x; | 
| 1303 | < | /* set gamma correction */ | 
| 1304 | < | setcolrgam(gamcor); | 
| 1303 | > | /* initialize tone mapping */ | 
| 1304 | > | make_tonemap(); | 
| 1305 |  | /* read and convert file */ | 
| 1306 |  | dp = ourdata; | 
| 1307 |  | for (y = 0; y < ymax; y++) { | 
| 1308 | < | if (getscan(y) < 0) | 
| 889 | < | quiterr("seek error in getfull"); | 
| 890 | < | if (scale) | 
| 891 | < | shiftcolrs(scanline, xmax, scale); | 
| 892 | < | colrs_gambs(scanline, xmax); | 
| 1308 | > | getscan(y); | 
| 1309 |  | add2icon(y, scanline); | 
| 1310 | < | if (ourvis.colormap_size < 256) | 
| 1310 | > | tmap_colrs(scanline, xmax); | 
| 1311 | > | if (maxcolors < 256) | 
| 1312 |  | for (x = 0; x < xmax; x++) | 
| 1313 | < | *dp++ = ((long)normbright(scanline[x]) * | 
| 1314 | < | ourvis.colormap_size + 128) >> 8; | 
| 1313 | > | *dp++ = ((int32)scanline[x][GRN] * | 
| 1314 | > | maxcolors + maxcolors/2) >> 8; | 
| 1315 |  | else | 
| 1316 |  | for (x = 0; x < xmax; x++) | 
| 1317 | < | *dp++ = normbright(scanline[x]); | 
| 1317 | > | *dp++ = scanline[x][GRN]; | 
| 1318 |  | } | 
| 1319 | + | for (x = 0; x < maxcolors; x++) | 
| 1320 | + | clrtab[x][RED] = clrtab[x][GRN] = | 
| 1321 | + | clrtab[x][BLU] = ((int32)x*256 + 128)/maxcolors; | 
| 1322 |  | } | 
| 1323 |  |  | 
| 1324 |  |  | 
| 1325 | < | scale_rcolors(xr, sf)                   /* scale color map */ | 
| 1326 | < | register XRASTER        *xr; | 
| 907 | < | double  sf; | 
| 1325 | > | static void | 
| 1326 | > | getmapped(void)                 /* get color-mapped data */ | 
| 1327 |  | { | 
| 1328 | + | int     y; | 
| 1329 | + | /* make sure we can do it first */ | 
| 1330 | + | if (fname == NULL) | 
| 1331 | + | quiterr("cannot map colors from standard input"); | 
| 1332 | + | /* initialize tone mapping */ | 
| 1333 | + | make_tonemap(); | 
| 1334 | + | /* make histogram */ | 
| 1335 | + | if (new_histo((int32)xmax*ymax) == -1) | 
| 1336 | + | quiterr("cannot initialize histogram"); | 
| 1337 | + | for (y = 0; y < ymax; y++) { | 
| 1338 | + | if (getscan(y) < 0) | 
| 1339 | + | break; | 
| 1340 | + | add2icon(y, scanline); | 
| 1341 | + | tmap_colrs(scanline, xmax); | 
| 1342 | + | cnt_colrs(scanline, xmax); | 
| 1343 | + | } | 
| 1344 | + | /* map pixels */ | 
| 1345 | + | if (!new_clrtab(maxcolors)) | 
| 1346 | + | quiterr("cannot create color map"); | 
| 1347 | + | for (y = 0; y < ymax; y++) { | 
| 1348 | + | getscan(y); | 
| 1349 | + | tmap_colrs(scanline, xmax); | 
| 1350 | + | if (dither) | 
| 1351 | + | dith_colrs(ourdata+y*xmax, scanline, xmax); | 
| 1352 | + | else | 
| 1353 | + | map_colrs(ourdata+y*xmax, scanline, xmax); | 
| 1354 | + | } | 
| 1355 | + | } | 
| 1356 | + |  | 
| 1357 | + |  | 
| 1358 | + | static void | 
| 1359 | + | scale_rcolors(                  /* scale color map */ | 
| 1360 | + | register XRASTER        *xr, | 
| 1361 | + | double  sf | 
| 1362 | + | ) | 
| 1363 | + | { | 
| 1364 |  | register int    i; | 
| 1365 |  | long    maxv; | 
| 1366 |  |  | 
| 1385 |  | } | 
| 1386 |  |  | 
| 1387 |  |  | 
| 1388 | < | getscan(y) | 
| 1389 | < | int  y; | 
| 1388 | > | static int | 
| 1389 | > | getscan( | 
| 1390 | > | int  y | 
| 1391 | > | ) | 
| 1392 |  | { | 
| 1393 | + | static int  trunced = -1;               /* truncated file? */ | 
| 1394 | + | skipit: | 
| 1395 | + | if (trunced >= 0 && y >= trunced) { | 
| 1396 | + | memset(scanline, '\0', xmax*sizeof(COLR)); | 
| 1397 | + | return(-1); | 
| 1398 | + | } | 
| 1399 |  | if (y != cury) { | 
| 1400 |  | if (scanpos == NULL || scanpos[y] == -1) | 
| 1401 |  | return(-1); | 
| 1405 |  | } else if (scanpos != NULL && scanpos[y] == -1) | 
| 1406 |  | scanpos[y] = ftell(fin); | 
| 1407 |  |  | 
| 1408 | < | if (freadcolrs(scanline, xmax, fin) < 0) | 
| 1409 | < | quiterr("read error"); | 
| 1410 | < |  | 
| 1408 | > | if (fread2colrs(scanline, xmax, fin, NCSAMP, WLPART) < 0) { | 
| 1409 | > | fprintf(stderr, "%s: %s: unfinished picture\n", | 
| 1410 | > | progname, fname==NULL?"<stdin>":fname); | 
| 1411 | > | trunced = y; | 
| 1412 | > | goto skipit; | 
| 1413 | > | } | 
| 1414 |  | cury++; | 
| 1415 |  | return(0); | 
| 950 | – | } | 
| 951 | – |  | 
| 952 | – |  | 
| 953 | – | picreadline3(y, l3)                     /* read in 3-byte scanline */ | 
| 954 | – | int  y; | 
| 955 | – | register rgbpixel  *l3; | 
| 956 | – | { | 
| 957 | – | register int    i; | 
| 958 | – | /* read scanline */ | 
| 959 | – | if (getscan(y) < 0) | 
| 960 | – | quiterr("cannot seek for picreadline"); | 
| 961 | – | /* convert scanline */ | 
| 962 | – | normcolrs(scanline, xmax, scale); | 
| 963 | – | add2icon(y, scanline); | 
| 964 | – | for (i = 0; i < xmax; i++) { | 
| 965 | – | l3[i].r = scanline[i][RED]; | 
| 966 | – | l3[i].g = scanline[i][GRN]; | 
| 967 | – | l3[i].b = scanline[i][BLU]; | 
| 968 | – | } | 
| 969 | – | } | 
| 970 | – |  | 
| 971 | – |  | 
| 972 | – | picwriteline(y, l)              /* add 8-bit scanline to image */ | 
| 973 | – | int  y; | 
| 974 | – | pixel  *l; | 
| 975 | – | { | 
| 976 | – | bcopy((char *)l, (char *)ourdata+y*xmax, xmax); | 
| 977 | – | } | 
| 978 | – |  | 
| 979 | – |  | 
| 980 | – | picreadcm(map)                  /* do gamma correction */ | 
| 981 | – | colormap  map; | 
| 982 | – | { | 
| 983 | – | extern double  pow(); | 
| 984 | – | register int  i, val; | 
| 985 | – |  | 
| 986 | – | for (i = 0; i < 256; i++) { | 
| 987 | – | val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; | 
| 988 | – | map[0][i] = map[1][i] = map[2][i] = val; | 
| 989 | – | } | 
| 990 | – | } | 
| 991 | – |  | 
| 992 | – |  | 
| 993 | – | picwritecm(map)                 /* handled elsewhere */ | 
| 994 | – | colormap  map; | 
| 995 | – | { | 
| 996 | – | #if 0 | 
| 997 | – | register int i; | 
| 998 | – |  | 
| 999 | – | for (i = 0; i < 256; i++) | 
| 1000 | – | printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); | 
| 1001 | – | #endif | 
| 1416 |  | } |