ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
(Generate patch)

Comparing ray/src/px/x11image.c (file contents):
Revision 1.24 by greg, Thu May 23 12:00:38 1991 UTC vs.
Revision 2.50 by gregl, Thu Jul 31 21:00:57 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1990 Regents of the University of California */
1 > /* Copyright (c) 1997 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4 < static char SCCSid[] = "$SunId$ LBL";
4 > static char SCCSid[] = "$SunId$ SGI";
5   #endif
6  
7   /*
# Line 21 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
21  
22   #include  "standard.h"
23  
24 + #include  <signal.h>
25   #include  <X11/Xlib.h>
26   #include  <X11/cursorfont.h>
27   #include  <X11/Xutil.h>
28 + #include  <X11/Xatom.h>
29  
30 < #include  "color.h"
30 > #undef  NOPROTO
31 > #define NOPROTO 1
32 > #include  "tonemap.h"
33   #include  "view.h"
30 #include  "pic.h"
34   #include  "x11raster.h"
35   #include  "random.h"
36 + #include  "resolu.h"
37  
38 + #ifndef  int4
39 + #define  int4           int             /* most int's are 32-bit */
40 + #endif
41 +
42   #define  FONTNAME       "8x13"          /* text font we'll use */
43  
44 < #define  CTRL(c)        ('c'-'@')
44 > #define  CTRL(c)        ((c)-'@')
45  
46   #define  BORWIDTH       5               /* border width */
47  
48   #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
49  
50 + #define  FIXWEIGHT      20              /* weight to add for fixation points */
51 +
52   #define  ourscreen      DefaultScreen(thedisplay)
43 #define  ourblack       BlackPixel(thedisplay,ourscreen)
44 #define  ourwhite       WhitePixel(thedisplay,ourscreen)
53   #define  ourroot        RootWindow(thedisplay,ourscreen)
46 #define  ourgc          DefaultGC(thedisplay,ourscreen)
54  
55   #define  revline(x0,y0,x1,y1)   XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1)
56  
57   #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
58  
59   double  gamcor = 2.2;                   /* gamma correction */
60 + char  *gamstr = NULL;                   /* gamma value override */
61  
62   int  dither = 1;                        /* dither colors? */
63   int  fast = 0;                          /* keep picture in Pixmap? */
64  
65 < Window  wind = 0;                       /* our output window */
58 < Font  fontid;                           /* our font */
65 > char    *dispname = NULL;               /* our display name */
66  
67 + Window  wind = 0;                       /* our output window */
68 + unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
69   int  maxcolors = 0;                     /* maximum colors */
70   int  greyscale = 0;                     /* in grey */
71  
# Line 65 | Line 74 | int  scale = 0;                                /* scalefactor; power of two */
74   int  xoff = 0;                          /* x image offset */
75   int  yoff = 0;                          /* y image offset */
76  
77 + int  parent = 0;                        /* number of children, -1 if child */
78 + int  sequential = 0;                    /* display images in sequence */
79 +
80 + char  *tout = "od";                     /* output of 't' command */
81 + int  tinterv = 0;                       /* interval between mouse reports */
82 +
83 + int  tmflags = TM_F_LINEAR;             /* tone mapping flags */
84 +
85   VIEW  ourview = STDVIEW;                /* image view parameters */
86   int  gotview = 0;                       /* got parameters from file */
87  
88   COLR  *scanline;                        /* scan line buffer */
89 + TMbright  *lscan;                       /* encoded luminance scanline */
90 + BYTE  *cscan;                           /* encoded chroma scanline */
91 + BYTE  *pscan;                           /* compute pixel scanline */
92  
93 < int  xmax, ymax;                        /* picture resolution */
93 > RESOLU  inpres;                         /* input resolution and ordering */
94 > int  xmax, ymax;                        /* picture dimensions */
95   int  width, height;                     /* window size */
96   char  *fname = NULL;                    /* input file name */
97   FILE  *fin = stdin;                     /* input file */
# Line 81 | Line 102 | double  exposure = 1.0;                        /* exposure compensation use
102  
103   int  wrongformat = 0;                   /* input in another format? */
104  
105 + GC      ourgc;                          /* standard graphics context */
106   GC      revgc;                          /* graphics context with GXinvert */
107  
108 < XRASTER *ourras;                        /* our stored image */
108 > int             *ourrank;               /* our visual class ranking */
109 > XVisualInfo     ourvis;                 /* our visual */
110 > XRASTER         *ourras;                /* our stored image */
111   unsigned char   *ourdata;               /* our image data */
112  
113   struct {
# Line 99 | Line 123 | char  *progname;
123  
124   char  errmsg[128];
125  
126 + BYTE  clrtab[256][3];                   /* global color map */
127 +
128   extern long  ftell();
129  
130 < extern char  *malloc(), *calloc();
130 > extern char  *getenv();
131  
106 extern double  atof(), pow(), log();
107
132   Display  *thedisplay;
133 + Atom  closedownAtom, wmProtocolsAtom;
134  
135 + int  sigrecv;
136 +
137 + int  onsig() { sigrecv++; }
138 +
139 +
140   main(argc, argv)
141   int  argc;
142   char  *argv[];
143   {
144          int  headline();
145          int  i;
146 +        int  pid;
147          
148          progname = argv[0];
149  
150          for (i = 1; i < argc; i++)
151                  if (argv[i][0] == '-')
152                          switch (argv[i][1]) {
153 <                        case 'c':
153 >                        case 'c':                       /* number of colors */
154                                  maxcolors = atoi(argv[++i]);
155                                  break;
156 <                        case 'b':
156 >                        case 'b':                       /* greyscale only */
157                                  greyscale = !greyscale;
158                                  break;
159 <                        case 'm':
159 >                        case 'm':                       /* monochrome */
160 >                                greyscale = 1;
161                                  maxcolors = 2;
162                                  break;
163 <                        case 'd':
164 <                                dither = !dither;
163 >                        case 'd':                       /* display or dither */
164 >                                if (argv[i][2] == 'i')
165 >                                        dispname = argv[++i];
166 >                                else
167 >                                        dither = !dither;
168                                  break;
169 <                        case 'f':
169 >                        case 'f':                       /* save pixmap */
170                                  fast = !fast;
171                                  break;
172 <                        case 'e':
173 <                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
172 >                        case 's':                       /* one at a time */
173 >                                sequential = !sequential;
174 >                                break;
175 >                        case 'o':                       /* 't' output */
176 >                                tout = argv[i]+2;
177 >                                break;
178 >                        case 't':                       /* msec interval */
179 >                                tinterv = atoi(argv[++i]);
180 >                                break;
181 >                        case 'e':                       /* exposure comp. */
182 >                                i++;
183 >                                if (argv[i][0] == 'a') {
184 >                                        tmflags = TM_F_CAMERA;
185 >                                        break;
186 >                                }
187 >                                if (argv[i][0] == 'h') {
188 >                                        tmflags = TM_F_HUMAN;
189 >                                        break;
190 >                                }
191 >                                if (argv[i][0] != '+' && argv[i][0] != '-')
192                                          goto userr;
193 <                                scale = atoi(argv[++i]);
193 >                                scale = atoi(argv[i]);
194                                  break;
195 <                        case 'g':
196 <                                if (!strcmp(argv[i], "-geometry"))
195 >                        case 'g':                       /* gamma comp. */
196 >                                if (argv[i][2] == 'e')
197                                          geometry = argv[++i];
198                                  else
199 <                                        gamcor = atof(argv[++i]);
199 >                                        gamstr = argv[++i];
200                                  break;
201                          default:
202                                  goto userr;
# Line 153 | Line 206 | char  *argv[];
206                  else
207                          break;
208  
209 <        if (i == argc-1) {
209 >        if (i > argc)
210 >                goto userr;
211 >        while (i < argc-1) {
212 >                sigrecv = 0;
213 >                signal(SIGCONT, onsig);
214 >                if ((pid=fork()) == 0) {        /* a child for each picture */
215 >                        parent = -1;
216 >                        break;
217 >                }
218 >                if (pid < 0)
219 >                        quiterr("fork failed");
220 >                parent++;
221 >                while (!sigrecv)
222 >                        pause();        /* wait for wake-up call */
223 >                i++;
224 >        }
225 >        if (i < argc) {                 /* open picture file */
226                  fname = argv[i];
227                  fin = fopen(fname, "r");
228 <                if (fin == NULL) {
229 <                        sprintf(errmsg, "can't open file \"%s\"", fname);
230 <                        quiterr(errmsg);
162 <                }
163 <        } else if (i != argc)
164 <                goto userr;
228 >                if (fin == NULL)
229 >                        quiterr("cannot open picture file");
230 >        }
231                                  /* get header */
232          getheader(fin, headline, NULL);
233                                  /* get picture dimensions */
234 <        if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
234 >        if (wrongformat || !fgetsresolu(&inpres, fin))
235                  quiterr("bad picture format");
236 +        xmax = scanlen(&inpres);
237 +        ymax = numscans(&inpres);
238                                  /* set view parameters */
239          if (gotview && setview(&ourview) != NULL)
240                  gotview = 0;
241          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
242                  quiterr("out of memory");
243  
244 <        init();                 /* get file and open window */
244 >        init(argc, argv);                       /* get file and open window */
245  
246          for ( ; ; )
247                  getevent();             /* main loop */
248   userr:
249          fprintf(stderr,
250 <        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
250 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n",
251                          progname);
252 <        quit(1);
252 >        exit(1);
253   }
254  
255  
256   headline(s)             /* get relevant info from header */
257   char  *s;
258   {
191        static char  *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
192        register char  **an;
259          char  fmt[32];
260  
261          if (isexpos(s))
262                  exposure *= exposval(s);
263 <        else if (isformat(s)) {
198 <                formatval(fmt, s);
263 >        else if (formatval(fmt, s))
264                  wrongformat = strcmp(fmt, COLRFMT);
265 <        } else
266 <                for (an = altname; *an != NULL; an++)
202 <                        if (!strncmp(*an, s, strlen(*an))) {
203 <                                if (sscanview(&ourview, s+strlen(*an)) > 0)
204 <                                        gotview++;
205 <                                return;
206 <                        }
265 >        else if (isview(s) && sscanview(&ourview, s) > 0)
266 >                gotview++;
267   }
268  
269  
270 < init()                  /* get data and open window */
270 > init(argc, argv)                        /* get data and open window */
271 > int argc;
272 > char **argv;
273   {
212        XWMHints        ourxwmhints;
274          XSetWindowAttributes    ourwinattr;
275 <        XSizeHints  oursizhints;
276 <        register int  i;
275 >        XClassHint      xclshints;
276 >        XWMHints        xwmhints;
277 >        XSizeHints      xszhints;
278 >        XTextProperty   windowName, iconName;
279 >        XGCValues       xgcv;
280 >        char    *name;
281 >        register int    i;
282          
283          if (fname != NULL) {
284                  scanpos = (long *)malloc(ymax*sizeof(long));
285                  if (scanpos == NULL)
286 <                        goto memerr;
286 >                        quiterr("out of memory");
287                  for (i = 0; i < ymax; i++)
288                          scanpos[i] = -1;
289 <        }
290 <        if ((thedisplay = XOpenDisplay(NULL)) == NULL)
291 <                quiterr("can't open display; DISPLAY variable set?");
292 <        if (maxcolors == 0) {           /* get number of available colors */
293 <                i = DisplayPlanes(thedisplay,ourscreen);
294 <                maxcolors = i > 8 ? 256 : 1<<i;
295 <                if (maxcolors > 4) maxcolors -= 2;
296 <        }
289 >                name = fname;
290 >        } else
291 >                name = progname;
292 >                                /* remove directory prefix from name */
293 >        for (i = strlen(name); i-- > 0; )
294 >                if (name[i] == '/')
295 >                        break;
296 >        name += i+1;
297 >        if ((thedisplay = XOpenDisplay(dispname)) == NULL)
298 >                quiterr("cannot open display");
299 >                                /* set gamma value */
300 >        if (gamstr == NULL)             /* get it from the X server */
301 >                gamstr = XGetDefault(thedisplay, "radiance", "gamma");
302 >        if (gamstr == NULL)             /* get it from the environment */
303 >                gamstr = getenv("DISPLAY_GAMMA");
304 >        if (gamstr != NULL)
305 >                gamcor = atof(gamstr);
306 >                                /* get best visual for default screen */
307 >        getbestvis();
308                                  /* store image */
309          getras();
310 <                                /* open window */
311 <        ourwinattr.border_pixel = ourblack;
312 <        ourwinattr.background_pixel = ourwhite;
236 <        wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH,
237 <                        0, InputOutput, ourras->visual,
238 <                        CWBackPixel|CWBorderPixel, &ourwinattr);
239 <        if (wind == 0)
240 <                quiterr("can't create window");
241 <        width = xmax;
242 <        height = ymax;
243 <        fontid = XLoadFont(thedisplay, FONTNAME);
244 <        if (fontid == 0)
245 <                quiterr("can't get font");
246 <        XSetFont(thedisplay, ourgc, fontid);
247 <        revgc = XCreateGC(thedisplay, wind, 0, 0);
248 <        XSetFunction(thedisplay, revgc, GXinvert);
249 <        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
250 <                        XC_diamond_cross));
251 <        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
310 >                                /* get size and position */
311 >        xszhints.flags = 0;
312 >        xszhints.width = xmax; xszhints.height = ymax;
313          if (geometry != NULL) {
314 <                bzero((char *)&oursizhints, sizeof(oursizhints));
315 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
316 <                                (unsigned *)&oursizhints.width,
256 <                                (unsigned *)&oursizhints.height);
314 >                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
315 >                                (unsigned *)&xszhints.width,
316 >                                (unsigned *)&xszhints.height);
317                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
318 <                        oursizhints.flags |= USSize;
319 <                else {
320 <                        oursizhints.width = xmax;
261 <                        oursizhints.height = ymax;
262 <                        oursizhints.flags |= PSize;
263 <                }
318 >                        xszhints.flags |= USSize;
319 >                else
320 >                        xszhints.flags |= PSize;
321                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
322 <                        oursizhints.flags |= USPosition;
322 >                        xszhints.flags |= USPosition;
323                          if (i & XNegative)
324 <                                oursizhints.x += DisplayWidth(thedisplay,
325 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
324 >                                xszhints.x += DisplayWidth(thedisplay,
325 >                                ourscreen)-1-xszhints.width-2*BORWIDTH;
326                          if (i & YNegative)
327 <                                oursizhints.y += DisplayHeight(thedisplay,
328 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
327 >                                xszhints.y += DisplayHeight(thedisplay,
328 >                                ourscreen)-1-xszhints.height-2*BORWIDTH;
329                  }
273                XSetNormalHints(thedisplay, wind, &oursizhints);
330          }
331 <        ourxwmhints.flags = InputHint|IconPixmapHint;
332 <        ourxwmhints.input = True;
333 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
331 >        /* open window */
332 >        i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
333 >        ourwinattr.border_pixel = ourwhite;
334 >        ourwinattr.background_pixel = ourblack;
335 >        if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
336 >                ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
337 >                i |= CWColormap;
338 >        }
339 >        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
340 >                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
341 >        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
342 >        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
343 >                        xszhints.width, xszhints.height, BORWIDTH,
344 >                        ourvis.depth, InputOutput, ourvis.visual,
345 >                        i, &ourwinattr);
346 >        if (wind == 0)
347 >                quiterr("cannot create window");
348 >        width = xmax;
349 >        height = ymax;
350 >        /* prepare graphics drawing context */
351 >        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
352 >                quiterr("cannot get font");
353 >        xgcv.foreground = ourblack;
354 >        xgcv.background = ourwhite;
355 >        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
356 >                        GCFont, &xgcv);
357 >        xgcv.function = GXinvert;
358 >        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
359 >                        GCFunction, &xgcv);
360 >
361 >        /* set up the window manager */
362 >        xwmhints.flags = InputHint|IconPixmapHint;
363 >        xwmhints.input = True;
364 >        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
365                          wind, icondata, iconwidth, iconheight);
366 <        XSetWMHints(thedisplay, wind, &ourxwmhints);
367 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
368 <                        |ButtonMotionMask|StructureNotifyMask
369 <                        |KeyPressMask|ExposureMask);
366 >
367 >        windowName.encoding = iconName.encoding = XA_STRING;
368 >        windowName.format = iconName.format = 8;
369 >        windowName.value = (u_char *)name;
370 >        windowName.nitems = strlen(windowName.value);
371 >        iconName.value = (u_char *)name;
372 >        iconName.nitems = strlen(windowName.value);
373 >
374 >        xclshints.res_name = NULL;
375 >        xclshints.res_class = "Ximage";
376 >        XSetWMProperties(thedisplay, wind, &windowName, &iconName,
377 >                        argv, argc, &xszhints, &xwmhints, &xclshints);
378 >        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
379 >        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
380 >        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
381 >
382          XMapWindow(thedisplay, wind);
284        return;
285 memerr:
286        quiterr("out of memory");
383   } /* end of init */
384  
385  
386   quiterr(err)            /* print message and exit */
387   char  *err;
388   {
389 <        if (err != NULL) {
390 <                fprintf(stderr, "%s: %s\n", progname, err);
391 <                exit(1);
389 >        register int  es;
390 >        int  cs;
391 >
392 >        if (es = err != NULL)
393 >                fprintf(stderr, "%s: %s: %s\n", progname,
394 >                                fname==NULL?"<stdin>":fname, err);
395 >        if (thedisplay != NULL)
396 >                XCloseDisplay(thedisplay);
397 >        if (parent < 0 & sigrecv == 0)
398 >                kill(getppid(), SIGCONT);
399 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
400 >                if (es == 0)
401 >                        es = cs>>8 & 0xff;
402 >                parent--;
403          }
404 <        exit(0);
404 >        exit(es);
405   }
406  
407  
408 < eputs(s)
409 < char    *s;
408 > static int
409 > viscmp(v1,v2)           /* compare visual to see which is better, descending */
410 > register XVisualInfo    *v1, *v2;
411   {
412 <        fputs(s, stderr);
412 >        int     bad1 = 0, bad2 = 0;
413 >        register int  *rp;
414 >
415 >        if (v1->class == v2->class) {
416 >                if (v1->class == TrueColor || v1->class == DirectColor) {
417 >                                        /* prefer 24-bit to 32-bit */
418 >                        if (v1->depth == 24 && v2->depth == 32)
419 >                                return(-1);
420 >                        if (v1->depth == 32 && v2->depth == 24)
421 >                                return(1);
422 >                        return(0);
423 >                }
424 >                                        /* don't be too greedy */
425 >                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
426 >                        return(v1->depth - v2->depth);
427 >                return(v2->depth - v1->depth);
428 >        }
429 >                                        /* prefer Pseudo when < 24-bit */
430 >        if ((v1->class == TrueColor || v1->class == DirectColor) &&
431 >                        v1->depth < 24)
432 >                bad1 = 1;
433 >        if ((v2->class == TrueColor || v2->class == DirectColor) &&
434 >                        v2->depth < 24)
435 >                bad2 = -1;
436 >        if (bad1 | bad2)
437 >                return(bad1+bad2);
438 >                                        /* otherwise, use class ranking */
439 >        for (rp = ourrank; *rp != -1; rp++) {
440 >                if (v1->class == *rp)
441 >                        return(-1);
442 >                if (v2->class == *rp)
443 >                        return(1);
444 >        }
445 >        return(0);
446   }
447  
448  
449 < quit(code)
309 < int  code;
449 > getbestvis()                    /* get the best visual for this screen */
450   {
451 <        exit(code);
451 > #ifdef DEBUG
452 > static char  vistype[][12] = {
453 >                "StaticGray",
454 >                "GrayScale",
455 >                "StaticColor",
456 >                "PseudoColor",
457 >                "TrueColor",
458 >                "DirectColor"
459 > };
460 > #endif
461 >        static int      rankings[3][6] = {
462 >                {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
463 >                {PseudoColor,GrayScale,StaticGray,-1},
464 >                {PseudoColor,GrayScale,StaticGray,-1}
465 >        };
466 >        XVisualInfo     *xvi;
467 >        int     vismatched;
468 >        register int    i, j;
469 >
470 >        if (greyscale) {
471 >                ourrank = rankings[2];
472 >                if (maxcolors < 2) maxcolors = 256;
473 >        } else if (maxcolors >= 2 && maxcolors <= 256)
474 >                ourrank = rankings[1];
475 >        else {
476 >                ourrank = rankings[0];
477 >                maxcolors = 256;
478 >        }
479 >                                        /* find best visual */
480 >        ourvis.screen = ourscreen;
481 >        xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
482 >        if (xvi == NULL)
483 >                quiterr("no visuals for this screen!");
484 > #ifdef DEBUG
485 >        fprintf(stderr, "Supported visuals:\n");
486 >        for (i = 0; i < vismatched; i++)
487 >                fprintf(stderr, "\ttype %s, depth %d\n",
488 >                                vistype[xvi[i].class], xvi[i].depth);
489 > #endif
490 >        for (i = 0, j = 1; j < vismatched; j++)
491 >                if (viscmp(&xvi[i],&xvi[j]) > 0)
492 >                        i = j;
493 >                                        /* compare to least acceptable */
494 >        for (j = 0; ourrank[j++] != -1; )
495 >                ;
496 >        ourvis.class = ourrank[--j];
497 >        ourvis.depth = 1;
498 >        if (viscmp(&xvi[i],&ourvis) > 0)
499 >                quiterr("inadequate visuals on this screen");
500 >                                        /* OK, we'll use it */
501 >        copystruct(&ourvis, &xvi[i]);
502 > #ifdef DEBUG
503 >        fprintf(stderr, "Selected visual type %s, depth %d\n",
504 >                        vistype[ourvis.class], ourvis.depth);
505 > #endif
506 >                                        /* make appropriate adjustments */
507 >        if (ourvis.class == GrayScale || ourvis.class == StaticGray)
508 >                greyscale = 1;
509 >        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
510 >                maxcolors = ourvis.colormap_size;
511 >        if (ourvis.class == StaticGray) {
512 >                ourblack = 0;
513 >                ourwhite = 255;
514 >        } else if (ourvis.class == PseudoColor) {
515 >                ourblack = BlackPixel(thedisplay,ourscreen);
516 >                ourwhite = WhitePixel(thedisplay,ourscreen);
517 >                if ((ourblack|ourwhite) & ~255L) {
518 >                        ourblack = 0;
519 >                        ourwhite = 1;
520 >                }
521 >                if (maxcolors > 4)
522 >                        maxcolors -= 2;
523 >        } else {
524 >                ourblack = 0;
525 >                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
526 >        }
527 >        XFree((char *)xvi);
528   }
529  
530  
531   getras()                                /* get raster file */
532   {
317        colormap        ourmap;
533          XVisualInfo     vinfo;
534  
535          if (maxcolors <= 2) {           /* monochrome */
536                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
537                  if (ourdata == NULL)
538                          goto fail;
539 <                ourras = make_raster(thedisplay, ourscreen, 1, ourdata,
539 >                ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
540                                  xmax, ymax, 8);
541                  if (ourras == NULL)
542                          goto fail;
543                  getmono();
544 <        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)
545 <                                                /* kludge for DirectColor */
331 <        || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) {
332 <                ourdata = (unsigned char *)malloc(xmax*ymax*3);
544 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
545 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
546                  if (ourdata == NULL)
547                          goto fail;
548 <                ourras = make_raster(thedisplay, ourscreen, 24, ourdata,
549 <                                xmax, ymax, 8);
548 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
549 >                                ourdata, xmax, ymax, 32);
550                  if (ourras == NULL)
551                          goto fail;
552                  getfull();
# Line 341 | Line 554 | getras()                               /* get raster file */
554                  ourdata = (unsigned char *)malloc(xmax*ymax);
555                  if (ourdata == NULL)
556                          goto fail;
557 <                ourras = make_raster(thedisplay, ourscreen, 8, ourdata,
557 >                ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
558                                  xmax, ymax, 8);
559                  if (ourras == NULL)
560                          goto fail;
561                  if (greyscale)
562 <                        biq(dither,maxcolors,1,ourmap);
562 >                        getgrey();
563                  else
564 <                        ciq(dither,maxcolors,1,ourmap);
565 <                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
564 >                        getmapped();
565 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
566                          goto fail;
567          }
568          return;
# Line 360 | Line 573 | fail:
573  
574   getevent()                              /* process the next event */
575   {
576 <        union {
364 <                XEvent  u;
365 <                XConfigureEvent  c;
366 <                XExposeEvent  e;
367 <                XButtonPressedEvent  b;
368 <                XKeyPressedEvent  k;
369 <        } e;
576 >        XEvent xev;
577  
578 <        XNextEvent(thedisplay, &e.u);
579 <        switch (e.u.type) {
578 >        XNextEvent(thedisplay, &xev);
579 >        switch ((int)xev.type) {
580          case KeyPress:
581 <                docom(&e.k);
581 >                docom(&xev.xkey);
582                  break;
583          case ConfigureNotify:
584 <                width = e.c.width;
585 <                height = e.c.height;
584 >                width = xev.xconfigure.width;
585 >                height = xev.xconfigure.height;
586                  break;
587          case MapNotify:
588                  map_rcolors(ourras, wind);
589                  if (fast)
590 <                        make_rpixmap(ourras);
590 >                        make_rpixmap(ourras, wind);
591 >                if (!sequential & parent < 0 & sigrecv == 0) {
592 >                        kill(getppid(), SIGCONT);
593 >                        sigrecv--;
594 >                }
595                  break;
596          case UnmapNotify:
597 <                unmap_rcolors(ourras);
597 >                if (!fast)
598 >                        unmap_rcolors(ourras);
599                  break;
600          case Expose:
601 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
601 >                redraw(xev.xexpose.x, xev.xexpose.y,
602 >                                xev.xexpose.width, xev.xexpose.height);
603                  break;
604          case ButtonPress:
605 <                if (e.b.state & (ShiftMask|ControlMask))
606 <                        moveimage(&e.b);
605 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
606 >                        moveimage(&xev.xbutton);
607                  else
608 <                        getbox(&e.b);
608 >                        switch (xev.xbutton.button) {
609 >                        case Button1:
610 >                                getbox(&xev.xbutton);
611 >                                break;
612 >                        case Button2:
613 >                                traceray(xev.xbutton.x, xev.xbutton.y);
614 >                                break;
615 >                        case Button3:
616 >                                trackrays(&xev.xbutton);
617 >                                break;
618 >                        }
619                  break;
620 +        case ClientMessage:
621 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
622 +                                (xev.xclient.data.l[0] == closedownAtom))
623 +                        quiterr(NULL);
624 +                break;
625          }
626   }
627  
628  
629 < docom(ekey)                                     /* execute command */
629 > traceray(xpos, ypos)                    /* print requested pixel data */
630 > int  xpos, ypos;
631 > {
632 >        extern char  *index();
633 >        FLOAT  hv[2];
634 >        FVECT  rorg, rdir;
635 >        COLOR  cval;
636 >        register char  *cp;
637 >
638 >        box.xmin = xpos; box.xsiz = 1;
639 >        box.ymin = ypos; box.ysiz = 1;
640 >        avgbox(cval);
641 >        scalecolor(cval, 1./exposure);
642 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
643 >        if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
644 >                rorg[0] = rorg[1] = rorg[2] =
645 >                rdir[0] = rdir[1] = rdir[2] = 0.;
646 >
647 >        for (cp = tout; *cp; cp++)      /* print what they asked for */
648 >                switch (*cp) {
649 >                case 'o':                       /* origin */
650 >                        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
651 >                        break;
652 >                case 'd':                       /* direction */
653 >                        printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
654 >                        break;
655 >                case 'v':                       /* radiance value */
656 >                        printf("%e %e %e ", colval(cval,RED),
657 >                                        colval(cval,GRN), colval(cval,BLU));
658 >                        break;
659 >                case 'l':                       /* luminance */
660 >                        printf("%e ", luminance(cval));
661 >                        break;
662 >                case 'p':                       /* pixel position */
663 >                        printf("%d %d ", (int)(hv[0]*inpres.xr),
664 >                                        (int)(hv[1]*inpres.yr));
665 >                        break;
666 >                }
667 >        putchar('\n');
668 >        fflush(stdout);
669 >        return(0);
670 > }
671 >
672 >
673 > docom(ekey)                             /* execute command */
674   XKeyPressedEvent  *ekey;
675   {
676          char  buf[80];
# Line 406 | Line 678 | XKeyPressedEvent  *ekey;
678          XColor  cvx;
679          int  com, n;
680          double  comp;
681 <        FVECT  rorg, rdir;
681 >        FLOAT  hv[2];
682  
683          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
684          if (n == 0)
# Line 414 | Line 686 | XKeyPressedEvent  *ekey;
686          com = buf[0];
687          switch (com) {                  /* interpret command */
688          case 'q':
689 <        case CTRL(D):                           /* quit */
690 <                quit(0);
689 >        case 'Q':
690 >        case CTRL('D'):                         /* quit */
691 >                quiterr(NULL);
692          case '\n':
693          case '\r':
694          case 'l':
695          case 'c':                               /* value */
696 <                if (avgbox(cval) == -1)
696 >                if (!avgbox(cval))
697                          return(-1);
698                  switch (com) {
699                  case '\n':
# Line 454 | Line 727 | XKeyPressedEvent  *ekey;
727                  XStoreColor(thedisplay, ourras->cmap, &cvx);
728                  return(0);
729          case 'p':                               /* position */
730 <                sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
730 >                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
731 >                sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr),
732 >                                (int)(hv[1]*inpres.yr));
733                  XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
734                                          buf, strlen(buf));
735                  return(0);
736          case 't':                               /* trace */
737 <                if (!gotview) {
737 >                return(traceray(ekey->x, ekey->y));
738 >        case 'a':                               /* auto exposure */
739 >                tmflags = TM_F_CAMERA;
740 >                strcpy(buf, "auto exposure...");
741 >                goto remap;
742 >        case 'h':                               /* human response */
743 >                tmflags = TM_F_HUMAN;
744 >                strcpy(buf, "human exposure...");
745 >                goto remap;
746 >        case '=':                               /* adjust exposure */
747 >        case '@':                               /* adaptation level */
748 >                if (!avgbox(cval))
749 >                        return(-1);
750 >                comp = bright(cval);
751 >                if (comp < 1e-20) {
752                          XBell(thedisplay, 0);
753                          return(-1);
754                  }
755 <                if (viewray(rorg, rdir, &ourview,
756 <                                (ekey->x-xoff+.5)/xmax,
757 <                                (ymax-1-ekey->y+yoff+.5)/ymax) < 0)
758 <                        return(-1);
759 <                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
760 <                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
761 <                fflush(stdout);
762 <                return(0);
763 <        case '=':                               /* adjust exposure */
764 <                if (avgbox(cval) == -1)
765 <                        return(-1);
766 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
767 <                if (n == 0)
768 <                        return(0);
480 <                scale_rcolors(ourras, pow(2.0, (double)n));
755 >                if (com == '@')
756 >                        comp = 106./exposure/
757 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
758 >                else
759 >                        comp = .5/comp;
760 >                comp = log(comp)/.69315 - scale;
761 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
762 >                if (tmflags != TM_F_LINEAR)
763 >                        tmflags = TM_F_LINEAR;  /* turn off tone mapping */
764 >                else {
765 >                        if (n == 0)             /* else check if any change */
766 >                                return(0);
767 >                        scale_rcolors(ourras, pow(2.0, (double)n));
768 >                }
769                  scale += n;
770                  sprintf(buf, "%+d", scale);
771 +        remap:
772                  XDrawImageString(thedisplay, wind, ourgc,
773                                  box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
774                  XFlush(thedisplay);
# Line 487 | Line 776 | XKeyPressedEvent  *ekey;
776                  free_raster(ourras);
777                  getras();
778          /* fall through */
779 <        case CTRL(R):                           /* redraw */
780 <        case CTRL(L):
779 >        case CTRL('R'):                         /* redraw */
780 >        case CTRL('L'):
781                  unmap_rcolors(ourras);
782                  XClearWindow(thedisplay, wind);
783                  map_rcolors(ourras, wind);
784                  if (fast)
785 <                        make_rpixmap(ourras);
785 >                        make_rpixmap(ourras, wind);
786                  redraw(0, 0, width, height);
787                  return(0);
788 +        case 'f':                               /* turn on fast redraw */
789 +                fast = 1;
790 +                make_rpixmap(ourras, wind);
791 +                return(0);
792 +        case 'F':                               /* turn off fast redraw */
793 +                fast = 0;
794 +                free_rpixmap(ourras);
795 +                return(0);
796 +        case '0':                               /* recenter origin */
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);
805                  return(0);
# Line 509 | Line 813 | XKeyPressedEvent  *ekey;
813   moveimage(ebut)                         /* shift the image */
814   XButtonPressedEvent  *ebut;
815   {
816 <        union {
513 <                XEvent  u;
514 <                XButtonReleasedEvent  b;
515 <                XPointerMovedEvent  m;
516 <        }  e;
816 >        XEvent  e;
817          int     mxo, myo;
818  
819 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
820 <        while (e.u.type == MotionNotify) {
821 <                mxo = e.m.x;
822 <                myo = e.m.y;
819 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
820 >        while (e.type == MotionNotify) {
821 >                mxo = e.xmotion.x;
822 >                myo = e.xmotion.y;
823                  revline(ebut->x, ebut->y, mxo, myo);
824                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
825                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
826 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
826 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
827                  revline(ebut->x, ebut->y, mxo, myo);
828                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
829                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
830          }
831 <        xoff += e.b.x - ebut->x;
832 <        yoff += e.b.y - ebut->y;
831 >        xoff += e.xbutton.x - ebut->x;
832 >        yoff += e.xbutton.y - ebut->y;
833          XClearWindow(thedisplay, wind);
834          redraw(0, 0, width, height);
835   }
# Line 538 | Line 838 | XButtonPressedEvent  *ebut;
838   getbox(ebut)                            /* get new box */
839   XButtonPressedEvent  *ebut;
840   {
841 <        union {
542 <                XEvent  u;
543 <                XButtonReleasedEvent  b;
544 <                XPointerMovedEvent  m;
545 <        }  e;
841 >        XEvent  e;
842  
843 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
844 <        while (e.u.type == MotionNotify) {
845 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
846 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
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);
846 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
847                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
848          }
849 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
850 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
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;
# Line 567 | Line 863 | XButtonPressedEvent  *ebut;
863   }
864  
865  
866 + trackrays(ebut)                         /* trace rays as mouse moves */
867 + XButtonPressedEvent  *ebut;
868 + {
869 +        XEvent  e;
870 +        unsigned long   lastrept;
871 +
872 +        traceray(ebut->x, ebut->y);
873 +        lastrept = ebut->time;
874 +        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
875 +        while (e.type == MotionNotify) {
876 +                if (e.xmotion.time >= lastrept + tinterv) {
877 +                        traceray(e.xmotion.x, e.xmotion.y);
878 +                        lastrept = e.xmotion.time;
879 +                }
880 +                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
881 +        }
882 + }
883 +
884 +
885   revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
886   int  x0, y0, x1, y1;
887   {
# Line 577 | Line 892 | int  x0, y0, x1, y1;
892   }
893  
894  
895 < avgbox(clr)                             /* average color over current box */
896 < COLOR  clr;
895 > int
896 > colavg(scn, n, cavg)
897 > register COLR   *scn;
898 > register int    n;
899 > COLOR   cavg;
900   {
901 +        COLOR   col;
902 +
903 +        while (n--) {
904 +                colr_color(col, scn++);
905 +                addcolor(cavg, col);
906 +        }
907 + }
908 +
909 +
910 + int
911 + avgbox(cavg)                            /* average color over current box */
912 + COLOR   cavg;
913 + {
914 +        double  d;
915 +        register int    rval;
916 +
917 +        setcolor(cavg, 0., 0., 0.);
918 +        rval = dobox(colavg, (char *)cavg);
919 +        if (rval > 0) {
920 +                d = 1./rval;
921 +                scalecolor(cavg, d);
922 +        }
923 +        return(rval);
924 + }
925 +
926 +
927 + int
928 + dobox(f, p)                             /* run function over box */
929 + int     (*f)();                 /* function to call for each subscan */
930 + char    *p;                     /* pointer to private data */
931 + {
932          int  left, right, top, bottom;
933          int  y;
585        double  d;
586        COLOR  ctmp;
587        register int  x;
934  
589        setcolor(clr, 0.0, 0.0, 0.0);
935          left = box.xmin - xoff;
936          right = left + box.xsiz;
937          if (left < 0)
# Line 594 | Line 939 | COLOR  clr;
939          if (right > xmax)
940                  right = xmax;
941          if (left >= right)
942 <                return(-1);
942 >                return(0);
943          top = box.ymin - yoff;
944          bottom = top + box.ysiz;
945          if (top < 0)
# Line 602 | Line 947 | COLOR  clr;
947          if (bottom > ymax)
948                  bottom = ymax;
949          if (top >= bottom)
950 <                return(-1);
950 >                return(0);
951          for (y = top; y < bottom; y++) {
952                  if (getscan(y) == -1)
953                          return(-1);
954 <                for (x = left; x < right; x++) {
955 <                        colr_color(ctmp, scanline[x]);
956 <                        addcolor(clr, ctmp);
954 >                (*f)(scanline+left, right-left, p);
955 >        }
956 >        return((right-left)*(bottom-top));
957 > }
958 >
959 >
960 > int
961 > addfix(scn, n)                  /* add fixation points to histogram */
962 > COLR    *scn;
963 > int     n;
964 > {
965 >        if (tmCvColrs(lscan, TM_NOCHROM, scn, n))
966 >                goto tmerr;
967 >        if (tmAddHisto(lscan, n, FIXWEIGHT))
968 >                goto tmerr;
969 >        return;
970 > tmerr:
971 >        quiterr("tone mapping error");
972 > }
973 >
974 >
975 > make_tonemap()                  /* initialize tone mapping */
976 > {
977 >        int  flags, y;
978 >
979 >        if (tmflags != TM_F_LINEAR && fname == NULL) {
980 >                fprintf(stderr, "%s: cannot adjust tone of standard input\n",
981 >                                progname);
982 >                tmflags = TM_F_LINEAR;
983 >        }
984 >        if (tmflags == TM_F_LINEAR) {   /* linear with clamping */
985 >                setcolrcor(pow, 1.0/gamcor);
986 >                return;
987 >        }
988 >        flags = tmflags;                /* histogram adjustment */
989 >        if (greyscale) flags |= TM_F_BW;
990 >        if (tmTop != NULL) {            /* reuse old histogram if one */
991 >                tmDone(tmTop);
992 >                tmTop->flags = flags;
993 >        } else {                        /* else initialize */
994 >                if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
995 >                        goto memerr;
996 >                if (greyscale) {
997 >                        cscan = TM_NOCHROM;
998 >                        if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
999 >                                goto memerr;
1000 >                } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax))
1001 >                                == NULL)
1002 >                        goto memerr;
1003 >                                                /* initialize tm library */
1004 >                if (tmInit(flags, stdprims, gamcor) == NULL)
1005 >                        goto memerr;
1006 >                if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
1007 >                        goto tmerr;
1008 >                                                /* compute picture histogram */
1009 >                for (y = 0; y < ymax; y++) {
1010 >                        getscan(y);
1011 >                        if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
1012 >                                goto tmerr;
1013 >                        if (tmAddHisto(lscan, xmax, 1))
1014 >                                goto tmerr;
1015                  }
1016          }
1017 <        d = 1.0/((right-left)*(bottom-top));
1018 <        scalecolor(clr, d);
1019 <        return(0);
1017 >        tmDup();                        /* add fixations to duplicate map */
1018 >        dobox(addfix, NULL);
1019 >                                        /* (re)compute tone mapping */
1020 >        if (tmComputeMapping(gamcor, 0., 0.))
1021 >                goto tmerr;
1022 >        return;
1023 > memerr:
1024 >        quiterr("out of memory in make_tonemap");
1025 > tmerr:
1026 >        quiterr("tone mapping error");
1027   }
1028  
1029  
1030 + tmap_colrs(scn, len)            /* apply tone mapping to scanline */
1031 + register COLR  *scn;
1032 + int  len;
1033 + {
1034 +        register BYTE  *ps;
1035 +
1036 +        if (tmflags == TM_F_LINEAR) {
1037 +                if (scale)
1038 +                        shiftcolrs(scn, len, scale);
1039 +                colrs_gambs(scn, len);
1040 +                return;
1041 +        }
1042 +        if (len > xmax)
1043 +                quiterr("code error 1 in tmap_colrs");
1044 +        if (tmCvColrs(lscan, cscan, scn, len))
1045 +                goto tmerr;
1046 +        if (tmMapPixels(pscan, lscan, cscan, len))
1047 +                goto tmerr;
1048 +        ps = pscan;
1049 +        if (greyscale)
1050 +                while (len--) {
1051 +                        scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1052 +                        scn[0][EXP] = COLXS;
1053 +                        scn++;
1054 +                }
1055 +        else
1056 +                while (len--) {
1057 +                        scn[0][RED] = *ps++;
1058 +                        scn[0][GRN] = *ps++;
1059 +                        scn[0][BLU] = *ps++;
1060 +                        scn[0][EXP] = COLXS;
1061 +                        scn++;
1062 +                }
1063 +        return;
1064 + tmerr:
1065 +        quiterr("tone mapping error");
1066 + }
1067 +
1068 +
1069   getmono()                       /* get monochrome data */
1070   {
1071          register unsigned char  *dp;
# Line 628 | Line 1077 | getmono()                      /* get monochrome data */
1077                  quiterr("out of memory in getmono");
1078          dp = ourdata - 1;
1079          for (y = 0; y < ymax; y++) {
1080 <                if (getscan(y) < 0)
632 <                        quiterr("seek error in getmono");
633 <                normcolrs(scanline, xmax, scale);
1080 >                getscan(y);
1081                  add2icon(y, scanline);
1082 +                normcolrs(scanline, xmax, scale);
1083                  err = 0;
1084                  for (x = 0; x < xmax; x++) {
1085                          if (!(x&7))
# Line 657 | Line 1105 | COLR  *scan;
1105          static short  cerr[ICONSIZ];
1106          static int  ynext;
1107          static char  *dp;
1108 +        COLR  clr;
1109          register int  err;
1110          register int    x, ti;
1111          int  errp;
# Line 687 | Line 1136 | COLR  *scan;
1136                          *++dp = 0;
1137                  errp = err;
1138                  ti = x*xmax/iconwidth;
1139 <                err += normbright(scan[ti]) + cerr[x];
1139 >                copycolr(clr, scan[ti]);
1140 >                normcolrs(clr, 1, scale);
1141 >                err += normbright(clr) + cerr[x];
1142                  if (err > 127)
1143                          err -= 255;
1144                  else
# Line 702 | Line 1153 | COLR  *scan;
1153   getfull()                       /* get full (24-bit) data */
1154   {
1155          int     y;
1156 +        register unsigned int4  *dp;
1157 +        register int    x;
1158 +                                        /* initialize tone mapping */
1159 +        make_tonemap();
1160 +                                        /* read and convert file */
1161 +        dp = (unsigned int4 *)ourdata;
1162 +        for (y = 0; y < ymax; y++) {
1163 +                getscan(y);
1164 +                add2icon(y, scanline);
1165 +                tmap_colrs(scanline, xmax);
1166 +                if (ourras->image->blue_mask & 1)
1167 +                        for (x = 0; x < xmax; x++)
1168 +                                *dp++ = (unsigned int4)scanline[x][RED] << 16 |
1169 +                                        (unsigned int4)scanline[x][GRN] << 8 |
1170 +                                        (unsigned int4)scanline[x][BLU] ;
1171 +                else
1172 +                        for (x = 0; x < xmax; x++)
1173 +                                *dp++ = (unsigned int4)scanline[x][RED] |
1174 +                                        (unsigned int4)scanline[x][GRN] << 8 |
1175 +                                        (unsigned int4)scanline[x][BLU] << 16 ;
1176 +        }
1177 + }
1178 +
1179 +
1180 + getgrey()                       /* get greyscale data */
1181 + {
1182 +        int     y;
1183          register unsigned char  *dp;
1184          register int    x;
1185 <                                        /* set gamma correction */
1186 <        setcolrgam(gamcor);
1185 >                                        /* initialize tone mapping */
1186 >        make_tonemap();
1187                                          /* read and convert file */
1188          dp = ourdata;
1189          for (y = 0; y < ymax; y++) {
1190 +                getscan(y);
1191 +                add2icon(y, scanline);
1192 +                tmap_colrs(scanline, xmax);
1193 +                if (maxcolors < 256)
1194 +                        for (x = 0; x < xmax; x++)
1195 +                                *dp++ = ((int4)scanline[x][GRN] *
1196 +                                        maxcolors + maxcolors/2) >> 8;
1197 +                else
1198 +                        for (x = 0; x < xmax; x++)
1199 +                                *dp++ = scanline[x][GRN];
1200 +        }
1201 +        for (x = 0; x < maxcolors; x++)
1202 +                clrtab[x][RED] = clrtab[x][GRN] =
1203 +                        clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors;
1204 + }
1205 +
1206 +
1207 + getmapped()                     /* get color-mapped data */
1208 + {
1209 +        int     y;
1210 +                                        /* make sure we can do it first */
1211 +        if (fname == NULL)
1212 +                quiterr("cannot map colors from standard input");
1213 +                                        /* initialize tone mapping */
1214 +        make_tonemap();
1215 +                                        /* make histogram */
1216 +        if (new_histo((int4)xmax*ymax) == -1)
1217 +                quiterr("cannot initialize histogram");
1218 +        for (y = 0; y < ymax; y++) {
1219                  if (getscan(y) < 0)
1220 <                        quiterr("seek error in getfull");
714 <                if (scale)
715 <                        shiftcolrs(scanline, xmax, scale);
716 <                colrs_gambs(scanline, xmax);
1220 >                        break;
1221                  add2icon(y, scanline);
1222 <                for (x = 0; x < xmax; x++) {
1223 <                        *dp++ = scanline[x][RED];
720 <                        *dp++ = scanline[x][GRN];
721 <                        *dp++ = scanline[x][BLU];
722 <                }
1222 >                tmap_colrs(scanline, xmax);
1223 >                cnt_colrs(scanline, xmax);
1224          }
1225 +                                        /* map pixels */
1226 +        if (!new_clrtab(maxcolors))
1227 +                quiterr("cannot create color map");
1228 +        for (y = 0; y < ymax; y++) {
1229 +                getscan(y);
1230 +                tmap_colrs(scanline, xmax);
1231 +                if (dither)
1232 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
1233 +                else
1234 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
1235 +        }
1236   }
1237  
1238  
# Line 755 | Line 1267 | double sf;
1267   getscan(y)
1268   int  y;
1269   {
1270 +        static int  trunced = -1;               /* truncated file? */
1271 + skipit:
1272 +        if (trunced >= 0 && y >= trunced) {
1273 +                bzero(scanline, xmax*sizeof(COLR));
1274 +                return(-1);
1275 +        }
1276          if (y != cury) {
1277                  if (scanpos == NULL || scanpos[y] == -1)
1278                          return(-1);
1279                  if (fseek(fin, scanpos[y], 0) == -1)
1280                          quiterr("fseek error");
1281                  cury = y;
1282 <        } else if (scanpos != NULL)
1282 >        } else if (scanpos != NULL && scanpos[y] == -1)
1283                  scanpos[y] = ftell(fin);
1284  
1285 <        if (freadcolrs(scanline, xmax, fin) < 0)
1286 <                quiterr("read error");
1287 <
1285 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1286 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1287 >                                progname, fname==NULL?"<stdin>":fname);
1288 >                trunced = y;
1289 >                goto skipit;
1290 >        }
1291          cury++;
1292          return(0);
772 }
773
774
775 picreadline3(y, l3)                     /* read in 3-byte scanline */
776 int  y;
777 register rgbpixel  *l3;
778 {
779        register int    i;
780                                                        /* read scanline */
781        if (getscan(y) < 0)
782                quiterr("cannot seek for picreadline");
783                                                        /* convert scanline */
784        normcolrs(scanline, xmax, scale);
785        add2icon(y, scanline);
786        for (i = 0; i < xmax; i++) {
787                l3[i].r = scanline[i][RED];
788                l3[i].g = scanline[i][GRN];
789                l3[i].b = scanline[i][BLU];
790        }
791 }
792
793
794 picwriteline(y, l)              /* add 8-bit scanline to image */
795 int  y;
796 pixel  *l;
797 {
798        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
799 }
800
801
802 picreadcm(map)                  /* do gamma correction */
803 colormap  map;
804 {
805        extern double  pow();
806        register int  i, val;
807
808        for (i = 0; i < 256; i++) {
809                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
810                map[0][i] = map[1][i] = map[2][i] = val;
811        }
812 }
813
814
815 picwritecm(map)                 /* handled elsewhere */
816 colormap  map;
817 {
818 #ifdef DEBUG
819        register int i;
820
821        for (i = 0; i < 256; i++)
822                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
823 #endif
1293   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines