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.5 by greg, Wed Mar 7 11:39:26 1990 UTC vs.
Revision 2.44 by greg, Tue Apr 15 16:53:58 1997 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines