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.19 by greg, Fri May 3 09:54:31 1991 UTC vs.
Revision 2.55 by gwlarson, Tue Jul 20 14:07:38 1999 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1990 Regents of the University of California */
1 > /* Copyright (c) 1999 Regents of the University of California */
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)
43 #define  ourblack       BlackPixel(thedisplay,ourscreen)
44 #define  ourwhite       WhitePixel(thedisplay,ourscreen)
50   #define  ourroot        RootWindow(thedisplay,ourscreen)
46 #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 */
58 < 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 65 | 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 */
94 > FILE  *fin = NULL;                      /* input file */
95   long  *scanpos = NULL;                  /* scan line positions in file */
96   int  cury = 0;                          /* current scan location */
97  
# Line 81 | Line 99 | double  exposure = 1.0;                        /* exposure compensation use
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 99 | Line 120 | 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  
106 extern double  atof(), pow(), log();
107
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 +        fin = stdin;
147  
148          for (i = 1; i < argc; i++)
149                  if (argv[i][0] == '-')
150                          switch (argv[i][1]) {
151 <                        case 'c':
151 >                        case 'c':                       /* number of colors */
152                                  maxcolors = atoi(argv[++i]);
153                                  break;
154 <                        case 'b':
154 >                        case 'b':                       /* greyscale only */
155                                  greyscale = !greyscale;
156                                  break;
157 <                        case 'm':
157 >                        case 'm':                       /* monochrome */
158 >                                greyscale = 1;
159                                  maxcolors = 2;
160                                  break;
161 <                        case 'd':
162 <                                dither = !dither;
161 >                        case 'd':                       /* display or dither */
162 >                                if (argv[i][2] == 'i')
163 >                                        dispname = argv[++i];
164 >                                else
165 >                                        dither = !dither;
166                                  break;
167 <                        case 'f':
167 >                        case 'f':                       /* save pixmap */
168                                  fast = !fast;
169                                  break;
170 <                        case 'e':
171 <                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
170 >                        case 's':                       /* one at a time */
171 >                                sequential = !sequential;
172 >                                break;
173 >                        case 'o':                       /* 't' output */
174 >                                tout = argv[i]+2;
175 >                                break;
176 >                        case 't':                       /* msec interval */
177 >                                tinterv = atoi(argv[++i]);
178 >                                break;
179 >                        case 'e':                       /* exposure comp. */
180 >                                i++;
181 >                                if (argv[i][0] == 'a') {
182 >                                        tmflags = TM_F_CAMERA;
183 >                                        break;
184 >                                }
185 >                                if (argv[i][0] == 'h') {
186 >                                        tmflags = TM_F_HUMAN;
187 >                                        break;
188 >                                }
189 >                                if (argv[i][0] != '+' && argv[i][0] != '-')
190                                          goto userr;
191 <                                scale = atoi(argv[++i]);
191 >                                scale = atoi(argv[i]);
192                                  break;
193 <                        case 'g':
194 <                                if (!strcmp(argv[i], "-geometry"))
193 >                        case 'g':                       /* gamma comp. */
194 >                                if (argv[i][2] == 'e')
195                                          geometry = argv[++i];
196                                  else
197 <                                        gamcor = atof(argv[++i]);
197 >                                        gamstr = argv[++i];
198                                  break;
199                          default:
200                                  goto userr;
# Line 153 | Line 204 | char  *argv[];
204                  else
205                          break;
206  
207 <        if (i == argc-1) {
207 >        if (i > argc)
208 >                goto userr;
209 >        while (i < argc-1) {
210 >                sigrecv = 0;
211 >                signal(SIGCONT, onsig);
212 >                if ((pid=fork()) == 0) {        /* a child for each picture */
213 >                        parent = -1;
214 >                        break;
215 >                }
216 >                if (pid < 0)
217 >                        quiterr("fork failed");
218 >                parent++;
219 >                while (!sigrecv)
220 >                        pause();        /* wait for wake-up call */
221 >                i++;
222 >        }
223 >        if (i < argc) {                 /* open picture file */
224                  fname = argv[i];
225                  fin = fopen(fname, "r");
226 <                if (fin == NULL) {
227 <                        sprintf(errmsg, "can't open file \"%s\"", fname);
228 <                        quiterr(errmsg);
162 <                }
163 <        } else if (i != argc)
164 <                goto userr;
226 >                if (fin == NULL)
227 >                        quiterr("cannot open picture file");
228 >        }
229                                  /* get header */
230          getheader(fin, headline, NULL);
231                                  /* get picture dimensions */
232 <        if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
232 >        if (wrongformat || !fgetsresolu(&inpres, fin))
233                  quiterr("bad picture format");
234 +        xmax = scanlen(&inpres);
235 +        ymax = numscans(&inpres);
236                                  /* set view parameters */
237          if (gotview && setview(&ourview) != NULL)
238                  gotview = 0;
239          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
240                  quiterr("out of memory");
241  
242 <        init();                 /* get file and open window */
242 >        init(argc, argv);                       /* get file and open window */
243  
244          for ( ; ; )
245                  getevent();             /* main loop */
246   userr:
247          fprintf(stderr,
248 <        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
248 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n",
249                          progname);
250 <        quit(1);
250 >        exit(1);
251   }
252  
253  
254 + int
255   headline(s)             /* get relevant info from header */
256   char  *s;
257   {
191        static char  *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
192        register char  **an;
258          char  fmt[32];
259  
260          if (isexpos(s))
261                  exposure *= exposval(s);
262 <        else if (isformat(s)) {
198 <                formatval(fmt, s);
262 >        else if (formatval(fmt, s))
263                  wrongformat = strcmp(fmt, COLRFMT);
264 <        } else
265 <                for (an = altname; *an != NULL; an++)
266 <                        if (!strncmp(*an, s, strlen(*an))) {
203 <                                if (sscanview(&ourview, s+strlen(*an)) > 0)
204 <                                        gotview++;
205 <                                return;
206 <                        }
264 >        else if (isview(s) && sscanview(&ourview, s) > 0)
265 >                gotview++;
266 >        return(0);
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));
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,
255 <                                (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;
260 <                        oursizhints.height = ymax;
261 <                        oursizhints.flags |= PSize;
262 <                }
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                  }
272                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);
283                                /* store name last as ready signal */
284        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
285        return;
286 memerr:
287        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 >                                        /* go for maximum depth otherwise */
423 >                        return(v2->depth - v1->depth);
424 >                }
425 >                                        /* don't be too greedy */
426 >                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
427 >                        return(v1->depth - v2->depth);
428 >                return(v2->depth - v1->depth);
429 >        }
430 >                                        /* prefer Pseudo when < 15-bit */
431 >        if ((v1->class == TrueColor || v1->class == DirectColor) &&
432 >                        v1->depth < 15)
433 >                bad1 = 1;
434 >        if ((v2->class == TrueColor || v2->class == DirectColor) &&
435 >                        v2->depth < 15)
436 >                bad2 = -1;
437 >        if (bad1 | bad2)
438 >                return(bad1+bad2);
439 >                                        /* otherwise, use class ranking */
440 >        for (rp = ourrank; *rp != -1; rp++) {
441 >                if (v1->class == *rp)
442 >                        return(-1);
443 >                if (v2->class == *rp)
444 >                        return(1);
445 >        }
446 >        return(0);
447   }
448  
449  
450 < quit(code)
310 < int  code;
450 > getbestvis()                    /* get the best visual for this screen */
451   {
452 <        exit(code);
452 > #ifdef DEBUG
453 > static char  vistype[][12] = {
454 >                "StaticGray",
455 >                "GrayScale",
456 >                "StaticColor",
457 >                "PseudoColor",
458 >                "TrueColor",
459 >                "DirectColor"
460 > };
461 > #endif
462 >        static int      rankings[3][6] = {
463 >                {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
464 >                {PseudoColor,GrayScale,StaticGray,-1},
465 >                {PseudoColor,GrayScale,StaticGray,-1}
466 >        };
467 >        XVisualInfo     *xvi;
468 >        int     vismatched;
469 >        register int    i, j;
470 >
471 >        if (greyscale) {
472 >                ourrank = rankings[2];
473 >                if (maxcolors < 2) maxcolors = 256;
474 >        } else if (maxcolors >= 2 && maxcolors <= 256)
475 >                ourrank = rankings[1];
476 >        else {
477 >                ourrank = rankings[0];
478 >                maxcolors = 256;
479 >        }
480 >                                        /* find best visual */
481 >        ourvis.screen = ourscreen;
482 >        xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
483 >        if (xvi == NULL)
484 >                quiterr("no visuals for this screen!");
485 > #ifdef DEBUG
486 >        fprintf(stderr, "Supported visuals:\n");
487 >        for (i = 0; i < vismatched; i++)
488 >                fprintf(stderr, "\ttype %s, depth %d\n",
489 >                                vistype[xvi[i].class], xvi[i].depth);
490 > #endif
491 >        for (i = 0, j = 1; j < vismatched; j++)
492 >                if (viscmp(&xvi[i],&xvi[j]) > 0)
493 >                        i = j;
494 >                                        /* compare to least acceptable */
495 >        for (j = 0; ourrank[j++] != -1; )
496 >                ;
497 >        ourvis.class = ourrank[--j];
498 >        ourvis.depth = 1;
499 >        if (viscmp(&xvi[i],&ourvis) > 0)
500 >                quiterr("inadequate visuals on this screen");
501 >                                        /* OK, we'll use it */
502 >        copystruct(&ourvis, &xvi[i]);
503 > #ifdef DEBUG
504 >        fprintf(stderr, "Selected visual type %s, depth %d\n",
505 >                        vistype[ourvis.class], ourvis.depth);
506 > #endif
507 >                                        /* make appropriate adjustments */
508 >        if (ourvis.class == GrayScale || ourvis.class == StaticGray)
509 >                greyscale = 1;
510 >        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
511 >                maxcolors = ourvis.colormap_size;
512 >        if (ourvis.class == StaticGray) {
513 >                ourblack = 0;
514 >                ourwhite = 255;
515 >        } else if (ourvis.class == PseudoColor) {
516 >                ourblack = BlackPixel(thedisplay,ourscreen);
517 >                ourwhite = WhitePixel(thedisplay,ourscreen);
518 >                if ((ourblack|ourwhite) & ~255L) {
519 >                        ourblack = 0;
520 >                        ourwhite = 1;
521 >                }
522 >                if (maxcolors > 4)
523 >                        maxcolors -= 2;
524 >        } else {
525 >                ourblack = 0;
526 >                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
527 >        }
528 >        XFree((char *)xvi);
529   }
530  
531  
532   getras()                                /* get raster file */
533   {
318        colormap        ourmap;
534          XVisualInfo     vinfo;
535  
536          if (maxcolors <= 2) {           /* monochrome */
537                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
538                  if (ourdata == NULL)
539                          goto fail;
540 <                ourras = make_raster(thedisplay, ourscreen, 1, ourdata,
540 >                ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
541                                  xmax, ymax, 8);
542                  if (ourras == NULL)
543                          goto fail;
544                  getmono();
545 <        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)
546 <                                                /* kludge for DirectColor */
547 <        || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) {
333 <                ourdata = (unsigned char *)malloc(xmax*ymax*3);
545 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
546 >                int  datsiz = ourvis.depth>16 ? sizeof(int4) : sizeof(int2);
547 >                ourdata = (unsigned char *)malloc(datsiz*xmax*ymax);
548                  if (ourdata == NULL)
549                          goto fail;
550 <                ourras = make_raster(thedisplay, ourscreen, 24, ourdata,
551 <                                xmax, ymax, 8);
550 >                ourras = make_raster(thedisplay, &ourvis, datsiz*8,
551 >                                ourdata, xmax, ymax, datsiz*8);
552                  if (ourras == NULL)
553                          goto fail;
554                  getfull();
# Line 342 | Line 556 | getras()                               /* get raster file */
556                  ourdata = (unsigned char *)malloc(xmax*ymax);
557                  if (ourdata == NULL)
558                          goto fail;
559 <                ourras = make_raster(thedisplay, ourscreen, 8, ourdata,
559 >                ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
560                                  xmax, ymax, 8);
561                  if (ourras == NULL)
562                          goto fail;
563                  if (greyscale)
564 <                        biq(dither,maxcolors,1,ourmap);
564 >                        getgrey();
565                  else
566 <                        ciq(dither,maxcolors,1,ourmap);
567 <                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
566 >                        getmapped();
567 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
568                          goto fail;
569          }
570          return;
# Line 361 | Line 575 | fail:
575  
576   getevent()                              /* process the next event */
577   {
578 <        union {
365 <                XEvent  u;
366 <                XConfigureEvent  c;
367 <                XExposeEvent  e;
368 <                XButtonPressedEvent  b;
369 <                XKeyPressedEvent  k;
370 <        } e;
578 >        XEvent xev;
579  
580 <        XNextEvent(thedisplay, &e.u);
581 <        switch (e.u.type) {
580 >        XNextEvent(thedisplay, &xev);
581 >        switch ((int)xev.type) {
582          case KeyPress:
583 <                docom(&e.k);
583 >                docom(&xev.xkey);
584                  break;
585          case ConfigureNotify:
586 <                width = e.c.width;
587 <                height = e.c.height;
586 >                width = xev.xconfigure.width;
587 >                height = xev.xconfigure.height;
588                  break;
589          case MapNotify:
590                  map_rcolors(ourras, wind);
591                  if (fast)
592 <                        make_rpixmap(ourras);
592 >                        make_rpixmap(ourras, wind);
593 >                if (!sequential & parent < 0 & sigrecv == 0) {
594 >                        kill(getppid(), SIGCONT);
595 >                        sigrecv--;
596 >                }
597                  break;
598          case UnmapNotify:
599 <                unmap_rcolors(ourras);
599 >                if (!fast)
600 >                        unmap_rcolors(ourras);
601                  break;
602          case Expose:
603 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
603 >                redraw(xev.xexpose.x, xev.xexpose.y,
604 >                                xev.xexpose.width, xev.xexpose.height);
605                  break;
606          case ButtonPress:
607 <                if (e.b.state & (ShiftMask|ControlMask))
608 <                        moveimage(&e.b);
607 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
608 >                        moveimage(&xev.xbutton);
609                  else
610 <                        getbox(&e.b);
610 >                        switch (xev.xbutton.button) {
611 >                        case Button1:
612 >                                getbox(&xev.xbutton);
613 >                                break;
614 >                        case Button2:
615 >                                traceray(xev.xbutton.x, xev.xbutton.y);
616 >                                break;
617 >                        case Button3:
618 >                                trackrays(&xev.xbutton);
619 >                                break;
620 >                        }
621                  break;
622 +        case ClientMessage:
623 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
624 +                                (xev.xclient.data.l[0] == closedownAtom))
625 +                        quiterr(NULL);
626 +                break;
627          }
628   }
629  
630  
631 < docom(ekey)                                     /* execute command */
631 > traceray(xpos, ypos)                    /* print requested pixel data */
632 > int  xpos, ypos;
633 > {
634 >        extern char  *index();
635 >        FLOAT  hv[2];
636 >        FVECT  rorg, rdir;
637 >        COLOR  cval;
638 >        register char  *cp;
639 >
640 >        box.xmin = xpos; box.xsiz = 1;
641 >        box.ymin = ypos; box.ysiz = 1;
642 >        avgbox(cval);
643 >        scalecolor(cval, 1./exposure);
644 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
645 >        if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
646 >                rorg[0] = rorg[1] = rorg[2] =
647 >                rdir[0] = rdir[1] = rdir[2] = 0.;
648 >
649 >        for (cp = tout; *cp; cp++)      /* print what they asked for */
650 >                switch (*cp) {
651 >                case 'o':                       /* origin */
652 >                        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
653 >                        break;
654 >                case 'd':                       /* direction */
655 >                        printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
656 >                        break;
657 >                case 'v':                       /* radiance value */
658 >                        printf("%e %e %e ", colval(cval,RED),
659 >                                        colval(cval,GRN), colval(cval,BLU));
660 >                        break;
661 >                case 'l':                       /* luminance */
662 >                        printf("%e ", luminance(cval));
663 >                        break;
664 >                case 'p':                       /* pixel position */
665 >                        printf("%d %d ", (int)(hv[0]*inpres.xr),
666 >                                        (int)(hv[1]*inpres.yr));
667 >                        break;
668 >                }
669 >        putchar('\n');
670 >        fflush(stdout);
671 >        return(0);
672 > }
673 >
674 >
675 > docom(ekey)                             /* execute command */
676   XKeyPressedEvent  *ekey;
677   {
678          char  buf[80];
# Line 407 | Line 680 | XKeyPressedEvent  *ekey;
680          XColor  cvx;
681          int  com, n;
682          double  comp;
683 <        FVECT  rorg, rdir;
683 >        FLOAT  hv[2];
684  
685          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
686          if (n == 0)
# Line 415 | Line 688 | XKeyPressedEvent  *ekey;
688          com = buf[0];
689          switch (com) {                  /* interpret command */
690          case 'q':
691 <        case CTRL(D):                           /* quit */
692 <                quit(0);
691 >        case 'Q':
692 >        case CTRL('D'):                         /* quit */
693 >                quiterr(NULL);
694          case '\n':
695          case '\r':
696          case 'l':
697          case 'c':                               /* value */
698 <                if (avgbox(cval) == -1)
698 >                if (!avgbox(cval))
699                          return(-1);
700                  switch (com) {
701                  case '\n':
# Line 455 | Line 729 | XKeyPressedEvent  *ekey;
729                  XStoreColor(thedisplay, ourras->cmap, &cvx);
730                  return(0);
731          case 'p':                               /* position */
732 <                sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
732 >                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
733 >                sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr),
734 >                                (int)(hv[1]*inpres.yr));
735                  XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
736                                          buf, strlen(buf));
737                  return(0);
738          case 't':                               /* trace */
739 <                if (!gotview) {
740 <                        XBell(thedisplay, 0);
739 >                return(traceray(ekey->x, ekey->y));
740 >        case 'a':                               /* auto exposure */
741 >                if (fname == NULL)
742                          return(-1);
743 <                }
744 <                if (viewray(rorg, rdir, &ourview,
745 <                                (ekey->x-xoff+.5)/xmax,
746 <                                (ymax-1-ekey->y+yoff+.5)/ymax) < 0)
743 >                tmflags = TM_F_CAMERA;
744 >                strcpy(buf, "auto exposure...");
745 >                goto remap;
746 >        case 'h':                               /* human response */
747 >                if (fname == NULL)
748                          return(-1);
749 <                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
750 <                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
751 <                fflush(stdout);
474 <                return(0);
749 >                tmflags = TM_F_HUMAN;
750 >                strcpy(buf, "human exposure...");
751 >                goto remap;
752          case '=':                               /* adjust exposure */
753 <                if (avgbox(cval) == -1)
753 >        case '@':                               /* adaptation level */
754 >                if (!avgbox(cval))
755                          return(-1);
756 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
757 <                if (n == 0)
758 <                        return(0);
759 <                scale_rcolors(ourras, pow(2.0, (double)n));
756 >                comp = bright(cval);
757 >                if (comp < 1e-20) {
758 >                        XBell(thedisplay, 0);
759 >                        return(-1);
760 >                }
761 >                if (com == '@')
762 >                        comp = 106./exposure/
763 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
764 >                else
765 >                        comp = .5/comp;
766 >                comp = log(comp)/.69315 - scale;
767 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
768 >                if (tmflags != TM_F_LINEAR)
769 >                        tmflags = TM_F_LINEAR;  /* turn off tone mapping */
770 >                else {
771 >                        if (n == 0)             /* else check if any change */
772 >                                return(0);
773 >                        scale_rcolors(ourras, pow(2.0, (double)n));
774 >                }
775                  scale += n;
776                  sprintf(buf, "%+d", scale);
777 +        remap:
778                  XDrawImageString(thedisplay, wind, ourgc,
779                                  box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
780                  XFlush(thedisplay);
# Line 488 | Line 782 | XKeyPressedEvent  *ekey;
782                  free_raster(ourras);
783                  getras();
784          /* fall through */
785 <        case CTRL(R):                           /* redraw */
786 <        case CTRL(L):
785 >        case CTRL('R'):                         /* redraw */
786 >        case CTRL('L'):
787                  unmap_rcolors(ourras);
788                  XClearWindow(thedisplay, wind);
789                  map_rcolors(ourras, wind);
790                  if (fast)
791 <                        make_rpixmap(ourras);
791 >                        make_rpixmap(ourras, wind);
792                  redraw(0, 0, width, height);
793                  return(0);
794 +        case 'f':                               /* turn on fast redraw */
795 +                fast = 1;
796 +                make_rpixmap(ourras, wind);
797 +                return(0);
798 +        case 'F':                               /* turn off fast redraw */
799 +                fast = 0;
800 +                free_rpixmap(ourras);
801 +                return(0);
802 +        case '0':                               /* recenter origin */
803 +                if (xoff == 0 & yoff == 0)
804 +                        return(0);
805 +                xoff = yoff = 0;
806 +                XClearWindow(thedisplay, wind);
807 +                redraw(0, 0, width, height);
808 +                return(0);
809          case ' ':                               /* clear */
810                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
811                  return(0);
# Line 510 | Line 819 | XKeyPressedEvent  *ekey;
819   moveimage(ebut)                         /* shift the image */
820   XButtonPressedEvent  *ebut;
821   {
822 <        union {
514 <                XEvent  u;
515 <                XButtonReleasedEvent  b;
516 <                XPointerMovedEvent  m;
517 <        }  e;
822 >        XEvent  e;
823          int     mxo, myo;
824  
825 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
826 <        while (e.u.type == MotionNotify) {
827 <                mxo = e.m.x;
828 <                myo = e.m.y;
825 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
826 >        while (e.type == MotionNotify) {
827 >                mxo = e.xmotion.x;
828 >                myo = e.xmotion.y;
829                  revline(ebut->x, ebut->y, mxo, myo);
830                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
831                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
832 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
832 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
833                  revline(ebut->x, ebut->y, mxo, myo);
834                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
835                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
836          }
837 <        xoff += e.b.x - ebut->x;
838 <        yoff += e.b.y - ebut->y;
837 >        xoff += e.xbutton.x - ebut->x;
838 >        yoff += e.xbutton.y - ebut->y;
839          XClearWindow(thedisplay, wind);
840          redraw(0, 0, width, height);
841   }
# Line 539 | Line 844 | XButtonPressedEvent  *ebut;
844   getbox(ebut)                            /* get new box */
845   XButtonPressedEvent  *ebut;
846   {
847 <        union {
543 <                XEvent  u;
544 <                XButtonReleasedEvent  b;
545 <                XPointerMovedEvent  m;
546 <        }  e;
847 >        XEvent  e;
848  
849 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
850 <        while (e.u.type == MotionNotify) {
851 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
852 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
849 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
850 >        while (e.type == MotionNotify) {
851 >                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
852 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
853                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
854          }
855 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
856 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
855 >        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
856 >        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
857          if (box.xmin > ebut->x) {
858                  box.xsiz = box.xmin - ebut->x + 1;
859                  box.xmin = ebut->x;
# Line 568 | Line 869 | XButtonPressedEvent  *ebut;
869   }
870  
871  
872 + trackrays(ebut)                         /* trace rays as mouse moves */
873 + XButtonPressedEvent  *ebut;
874 + {
875 +        XEvent  e;
876 +        unsigned long   lastrept;
877 +
878 +        traceray(ebut->x, ebut->y);
879 +        lastrept = ebut->time;
880 +        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
881 +        while (e.type == MotionNotify) {
882 +                if (e.xmotion.time >= lastrept + tinterv) {
883 +                        traceray(e.xmotion.x, e.xmotion.y);
884 +                        lastrept = e.xmotion.time;
885 +                }
886 +                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
887 +        }
888 + }
889 +
890 +
891   revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
892   int  x0, y0, x1, y1;
893   {
# Line 578 | Line 898 | int  x0, y0, x1, y1;
898   }
899  
900  
901 < avgbox(clr)                             /* average color over current box */
902 < COLOR  clr;
901 > int
902 > colavg(scn, n, cavg)
903 > register COLR   *scn;
904 > register int    n;
905 > COLOR   cavg;
906   {
907 +        COLOR   col;
908 +
909 +        while (n--) {
910 +                colr_color(col, scn++);
911 +                addcolor(cavg, col);
912 +        }
913 + }
914 +
915 +
916 + int
917 + avgbox(cavg)                            /* average color over current box */
918 + COLOR   cavg;
919 + {
920 +        double  d;
921 +        register int    rval;
922 +
923 +        setcolor(cavg, 0., 0., 0.);
924 +        rval = dobox(colavg, (char *)cavg);
925 +        if (rval > 0) {
926 +                d = 1./rval;
927 +                scalecolor(cavg, d);
928 +        }
929 +        return(rval);
930 + }
931 +
932 +
933 + int
934 + dobox(f, p)                             /* run function over box */
935 + int     (*f)();                 /* function to call for each subscan */
936 + char    *p;                     /* pointer to private data */
937 + {
938          int  left, right, top, bottom;
939          int  y;
586        double  d;
587        COLOR  ctmp;
588        register int  x;
940  
590        setcolor(clr, 0.0, 0.0, 0.0);
941          left = box.xmin - xoff;
942          right = left + box.xsiz;
943          if (left < 0)
# Line 595 | Line 945 | COLOR  clr;
945          if (right > xmax)
946                  right = xmax;
947          if (left >= right)
948 <                return(-1);
948 >                return(0);
949          top = box.ymin - yoff;
950          bottom = top + box.ysiz;
951          if (top < 0)
# Line 603 | Line 953 | COLOR  clr;
953          if (bottom > ymax)
954                  bottom = ymax;
955          if (top >= bottom)
956 <                return(-1);
956 >                return(0);
957          for (y = top; y < bottom; y++) {
958                  if (getscan(y) == -1)
959                          return(-1);
960 <                for (x = left; x < right; x++) {
961 <                        colr_color(ctmp, scanline[x]);
962 <                        addcolor(clr, ctmp);
960 >                (*f)(scanline+left, right-left, p);
961 >        }
962 >        return((right-left)*(bottom-top));
963 > }
964 >
965 >
966 > int
967 > addfix(scn, n)                  /* add fixation points to histogram */
968 > COLR    *scn;
969 > int     n;
970 > {
971 >        if (tmCvColrs(lscan, TM_NOCHROM, scn, n))
972 >                goto tmerr;
973 >        if (tmAddHisto(lscan, n, FIXWEIGHT))
974 >                goto tmerr;
975 >        return;
976 > tmerr:
977 >        quiterr("tone mapping error");
978 > }
979 >
980 >
981 > make_tonemap()                  /* initialize tone mapping */
982 > {
983 >        int  flags, y;
984 >
985 >        if (tmflags != TM_F_LINEAR && fname == NULL) {
986 >                fprintf(stderr, "%s: cannot adjust tone of standard input\n",
987 >                                progname);
988 >                tmflags = TM_F_LINEAR;
989 >        }
990 >        if (tmflags == TM_F_LINEAR) {   /* linear with clamping */
991 >                setcolrcor(pow, 1.0/gamcor);
992 >                return;
993 >        }
994 >        flags = tmflags;                /* histogram adjustment */
995 >        if (greyscale) flags |= TM_F_BW;
996 >        if (tmTop != NULL) {            /* reuse old histogram if one */
997 >                tmDone(tmTop);
998 >                tmTop->flags = flags;
999 >        } else {                        /* else initialize */
1000 >                if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
1001 >                        goto memerr;
1002 >                if (greyscale) {
1003 >                        cscan = TM_NOCHROM;
1004 >                        if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
1005 >                                goto memerr;
1006 >                } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax))
1007 >                                == NULL)
1008 >                        goto memerr;
1009 >                                                /* initialize tm library */
1010 >                if (tmInit(flags, stdprims, gamcor) == NULL)
1011 >                        goto memerr;
1012 >                if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
1013 >                        goto tmerr;
1014 >                                                /* compute picture histogram */
1015 >                for (y = 0; y < ymax; y++) {
1016 >                        getscan(y);
1017 >                        if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
1018 >                                goto tmerr;
1019 >                        if (tmAddHisto(lscan, xmax, 1))
1020 >                                goto tmerr;
1021                  }
1022          }
1023 <        d = 1.0/((right-left)*(bottom-top));
1024 <        scalecolor(clr, d);
1025 <        return(0);
1023 >        tmDup();                        /* add fixations to duplicate map */
1024 >        dobox(addfix, NULL);
1025 >                                        /* (re)compute tone mapping */
1026 >        if (tmComputeMapping(gamcor, 0., 0.))
1027 >                goto tmerr;
1028 >        return;
1029 > memerr:
1030 >        quiterr("out of memory in make_tonemap");
1031 > tmerr:
1032 >        quiterr("tone mapping error");
1033   }
1034  
1035  
1036 + tmap_colrs(scn, len)            /* apply tone mapping to scanline */
1037 + register COLR  *scn;
1038 + int  len;
1039 + {
1040 +        register BYTE  *ps;
1041 +
1042 +        if (tmflags == TM_F_LINEAR) {
1043 +                if (scale)
1044 +                        shiftcolrs(scn, len, scale);
1045 +                colrs_gambs(scn, len);
1046 +                return;
1047 +        }
1048 +        if (len > xmax)
1049 +                quiterr("code error 1 in tmap_colrs");
1050 +        if (tmCvColrs(lscan, cscan, scn, len))
1051 +                goto tmerr;
1052 +        if (tmMapPixels(pscan, lscan, cscan, len))
1053 +                goto tmerr;
1054 +        ps = pscan;
1055 +        if (greyscale)
1056 +                while (len--) {
1057 +                        scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1058 +                        scn[0][EXP] = COLXS;
1059 +                        scn++;
1060 +                }
1061 +        else
1062 +                while (len--) {
1063 +                        scn[0][RED] = *ps++;
1064 +                        scn[0][GRN] = *ps++;
1065 +                        scn[0][BLU] = *ps++;
1066 +                        scn[0][EXP] = COLXS;
1067 +                        scn++;
1068 +                }
1069 +        return;
1070 + tmerr:
1071 +        quiterr("tone mapping error");
1072 + }
1073 +
1074 +
1075   getmono()                       /* get monochrome data */
1076   {
1077          register unsigned char  *dp;
1078          register int    x, err;
1079 <        int     y;
1079 >        int     y, errp;
1080          short   *cerr;
1081  
1082          if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
1083                  quiterr("out of memory in getmono");
1084          dp = ourdata - 1;
1085          for (y = 0; y < ymax; y++) {
1086 <                if (getscan(y) < 0)
633 <                        quiterr("seek error in getmono");
634 <                normcolrs(scanline, xmax, scale);
1086 >                getscan(y);
1087                  add2icon(y, scanline);
1088 +                normcolrs(scanline, xmax, scale);
1089                  err = 0;
1090                  for (x = 0; x < xmax; x++) {
1091                          if (!(x&7))
1092                                  *++dp = 0;
1093 +                        errp = err;
1094                          err += normbright(scanline[x]) + cerr[x];
1095                          if (err > 127)
1096                                  err -= 255;
1097                          else
1098                                  *dp |= 1<<(7-(x&07));
1099 <                        cerr[x] = err >>= 1;
1099 >                        err /= 3;
1100 >                        cerr[x] = err + errp;
1101                  }
1102          }
1103          free((char *)cerr);
# Line 653 | Line 1108 | add2icon(y, scan)              /* add a scanline to our icon data
1108   int  y;
1109   COLR  *scan;
1110   {
656        static char  *dp = NULL;
1111          static short  cerr[ICONSIZ];
1112 +        static int  ynext;
1113 +        static char  *dp;
1114 +        COLR  clr;
1115          register int  err;
1116 <        register int    x, xi;
1116 >        register int    x, ti;
1117 >        int  errp;
1118  
1119          if (iconheight == 0) {          /* initialize */
1120                  if (xmax <= ICONSIZ && ymax <= ICONSIZ) {
# Line 665 | Line 1123 | COLR  *scan;
1123                  } else if (xmax > ymax) {
1124                          iconwidth = ICONSIZ;
1125                          iconheight = ICONSIZ*ymax/xmax;
1126 +                        if (iconheight < 1)
1127 +                                iconheight = 1;
1128                  } else {
1129                          iconwidth = ICONSIZ*xmax/ymax;
1130 +                        if (iconwidth < 1)
1131 +                                iconwidth = 1;
1132                          iconheight = ICONSIZ;
1133                  }
1134 +                ynext = 0;
1135                  dp = icondata - 1;
1136          }
1137 <        if (dp == NULL)                 /* done already */
1137 >        if (y < ynext*ymax/iconheight)  /* skip this one */
1138                  return;
676        if (y % (ymax/iconheight))      /* skip this one */
677                return;
1139          err = 0;
1140          for (x = 0; x < iconwidth; x++) {
1141                  if (!(x&7))
1142                          *++dp = 0;
1143 <                xi = x*xmax/iconwidth;
1144 <                err += normbright(scan[xi]) + cerr[x];
1143 >                errp = err;
1144 >                ti = x*xmax/iconwidth;
1145 >                copycolr(clr, scan[ti]);
1146 >                normcolrs(clr, 1, scale);
1147 >                err += normbright(clr) + cerr[x];
1148                  if (err > 127)
1149                          err -= 255;
1150                  else
1151                          *dp |= 1<<(x&07);
1152 <                cerr[x] = err >>= 1;
1152 >                err /= 3;
1153 >                cerr[x] = err + errp;
1154          }
1155 <        if (y >= ymax - ymax/iconheight)        /* all done */
691 <                dp = NULL;
1155 >        ynext++;
1156   }
1157  
1158  
1159   getfull()                       /* get full (24-bit) data */
1160   {
1161          int     y;
1162 +        register unsigned int4  *dp;
1163 +        register unsigned int2  *dph;
1164 +        register int    x;
1165 +                                        /* initialize tone mapping */
1166 +        make_tonemap();
1167 +                                        /* read and convert file */
1168 +        dp = (unsigned int4 *)ourdata;
1169 +        dph = (unsigned int2 *)ourdata;
1170 +        for (y = 0; y < ymax; y++) {
1171 +                getscan(y);
1172 +                add2icon(y, scanline);
1173 +                tmap_colrs(scanline, xmax);
1174 +                switch (ourras->image->blue_mask) {
1175 +                case 0xff:              /* 24-bit RGB */
1176 +                        for (x = 0; x < xmax; x++)
1177 +                                *dp++ = (unsigned int4)scanline[x][RED] << 16 |
1178 +                                        (unsigned int4)scanline[x][GRN] << 8 |
1179 +                                        (unsigned int4)scanline[x][BLU] ;
1180 +                        break;
1181 +                case 0xff0000:          /* 24-bit BGR */
1182 +                        for (x = 0; x < xmax; x++)
1183 +                                *dp++ = (unsigned int4)scanline[x][RED] |
1184 +                                        (unsigned int4)scanline[x][GRN] << 8 |
1185 +                                        (unsigned int4)scanline[x][BLU] << 16 ;
1186 +                        break;
1187 + #if 0
1188 +                case 0x1f:              /* 15-bit RGB */
1189 +                        for (x = 0; x < xmax; x++)
1190 +                                *dph++ =        (scanline[x][RED] << 7 & 0x7c00) |
1191 +                                        (scanline[x][GRN] << 2 & 0x3e0) |
1192 +                                        (unsigned)scanline[x][BLU] >> 3 ;
1193 +                        break;
1194 +                case 0x7c00:            /* 15-bit BGR */
1195 +                        for (x = 0; x < xmax; x++)
1196 +                                *dph++ =        (unsigned)scanline[x][RED] >> 3 |
1197 +                                        (scanline[x][GRN] << 2 & 0x3e0) |
1198 +                                        (scanline[x][BLU] << 7 & 0x7c00) ;
1199 +                        break;
1200 + #endif
1201 +                default:                /* unknown */
1202 +                        if (ourvis.depth > 16)
1203 +                                for (x = 0; x < xmax; x++) {
1204 +                                        *dp = ourras->image->red_mask &
1205 +                                                ourras->image->red_mask*scanline[x][RED]/255;
1206 +                                        *dp |= ourras->image->green_mask &
1207 +                                                ourras->image->green_mask*scanline[x][GRN]/255;
1208 +                                        *dp++ |= ourras->image->blue_mask &
1209 +                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1210 +                                }
1211 +                        else
1212 +                                for (x = 0; x < xmax; x++) {
1213 +                                        *dph = ourras->image->red_mask &
1214 +                                                ourras->image->red_mask*scanline[x][RED]/255;
1215 +                                        *dph |= ourras->image->green_mask &
1216 +                                                ourras->image->green_mask*scanline[x][GRN]/255;
1217 +                                        *dph++ |= ourras->image->blue_mask &
1218 +                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1219 +                                }
1220 +                        break;
1221 +                }
1222 +        }
1223 + }
1224 +
1225 +
1226 + getgrey()                       /* get greyscale data */
1227 + {
1228 +        int     y;
1229          register unsigned char  *dp;
1230          register int    x;
1231 <                                        /* set gamma correction */
1232 <        setcolrgam(gamcor);
1231 >                                        /* initialize tone mapping */
1232 >        make_tonemap();
1233                                          /* read and convert file */
1234          dp = ourdata;
1235          for (y = 0; y < ymax; y++) {
1236 +                getscan(y);
1237 +                add2icon(y, scanline);
1238 +                tmap_colrs(scanline, xmax);
1239 +                if (maxcolors < 256)
1240 +                        for (x = 0; x < xmax; x++)
1241 +                                *dp++ = ((int4)scanline[x][GRN] *
1242 +                                        maxcolors + maxcolors/2) >> 8;
1243 +                else
1244 +                        for (x = 0; x < xmax; x++)
1245 +                                *dp++ = scanline[x][GRN];
1246 +        }
1247 +        for (x = 0; x < maxcolors; x++)
1248 +                clrtab[x][RED] = clrtab[x][GRN] =
1249 +                        clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors;
1250 + }
1251 +
1252 +
1253 + getmapped()                     /* get color-mapped data */
1254 + {
1255 +        int     y;
1256 +                                        /* make sure we can do it first */
1257 +        if (fname == NULL)
1258 +                quiterr("cannot map colors from standard input");
1259 +                                        /* initialize tone mapping */
1260 +        make_tonemap();
1261 +                                        /* make histogram */
1262 +        if (new_histo((int4)xmax*ymax) == -1)
1263 +                quiterr("cannot initialize histogram");
1264 +        for (y = 0; y < ymax; y++) {
1265                  if (getscan(y) < 0)
1266 <                        quiterr("seek error in getfull");
707 <                if (scale)
708 <                        shiftcolrs(scanline, xmax, scale);
709 <                colrs_gambs(scanline, xmax);
1266 >                        break;
1267                  add2icon(y, scanline);
1268 <                for (x = 0; x < xmax; x++) {
1269 <                        *dp++ = scanline[x][RED];
713 <                        *dp++ = scanline[x][GRN];
714 <                        *dp++ = scanline[x][BLU];
715 <                }
1268 >                tmap_colrs(scanline, xmax);
1269 >                cnt_colrs(scanline, xmax);
1270          }
1271 +                                        /* map pixels */
1272 +        if (!new_clrtab(maxcolors))
1273 +                quiterr("cannot create color map");
1274 +        for (y = 0; y < ymax; y++) {
1275 +                getscan(y);
1276 +                tmap_colrs(scanline, xmax);
1277 +                if (dither)
1278 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
1279 +                else
1280 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
1281 +        }
1282   }
1283  
1284  
# Line 748 | Line 1313 | double sf;
1313   getscan(y)
1314   int  y;
1315   {
1316 +        static int  trunced = -1;               /* truncated file? */
1317 + skipit:
1318 +        if (trunced >= 0 && y >= trunced) {
1319 +                bzero(scanline, xmax*sizeof(COLR));
1320 +                return(-1);
1321 +        }
1322          if (y != cury) {
1323                  if (scanpos == NULL || scanpos[y] == -1)
1324                          return(-1);
1325                  if (fseek(fin, scanpos[y], 0) == -1)
1326                          quiterr("fseek error");
1327                  cury = y;
1328 <        } else if (scanpos != NULL)
1328 >        } else if (scanpos != NULL && scanpos[y] == -1)
1329                  scanpos[y] = ftell(fin);
1330  
1331 <        if (freadcolrs(scanline, xmax, fin) < 0)
1332 <                quiterr("read error");
1333 <
1331 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1332 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1333 >                                progname, fname==NULL?"<stdin>":fname);
1334 >                trunced = y;
1335 >                goto skipit;
1336 >        }
1337          cury++;
1338          return(0);
765 }
766
767
768 picreadline3(y, l3)                     /* read in 3-byte scanline */
769 int  y;
770 register rgbpixel  *l3;
771 {
772        register int    i;
773                                                        /* read scanline */
774        if (getscan(y) < 0)
775                quiterr("cannot seek for picreadline");
776                                                        /* convert scanline */
777        normcolrs(scanline, xmax, scale);
778        add2icon(y, scanline);
779        for (i = 0; i < xmax; i++) {
780                l3[i].r = scanline[i][RED];
781                l3[i].g = scanline[i][GRN];
782                l3[i].b = scanline[i][BLU];
783        }
784 }
785
786
787 picwriteline(y, l)              /* add 8-bit scanline to image */
788 int  y;
789 pixel  *l;
790 {
791        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
792 }
793
794
795 picreadcm(map)                  /* do gamma correction */
796 colormap  map;
797 {
798        extern double  pow();
799        register int  i, val;
800
801        for (i = 0; i < 256; i++) {
802                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
803                map[0][i] = map[1][i] = map[2][i] = val;
804        }
805 }
806
807
808 picwritecm(map)                 /* handled elsewhere */
809 colormap  map;
810 {
811 #ifdef DEBUG
812        register int i;
813
814        for (i = 0; i < 256; i++)
815                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
816 #endif
1339   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines