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.12 by greg, Fri Dec 21 17:20:11 1990 UTC vs.
Revision 2.53 by gwlarson, Tue Oct 27 09:08:28 1998 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines