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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines