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 2.10 by greg, Wed Jun 24 09:17:39 1992 UTC vs.
Revision 2.59 by greg, Fri Jun 20 00:25:50 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id";
3   #endif
6
4   /*
5   *  x11image.c - driver for X-windows
6   *
# Line 21 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "standard.h"
20  
21 + #include  <signal.h>
22   #include  <X11/Xlib.h>
23   #include  <X11/cursorfont.h>
24   #include  <X11/Xutil.h>
25 + #include  <X11/Xatom.h>
26  
27   #include  "color.h"
28 + #include  "tonemap.h"
29   #include  "view.h"
30 #include  "pic.h"
30   #include  "x11raster.h"
31   #include  "random.h"
33 #include  "resolu.h"
32  
33   #define  FONTNAME       "8x13"          /* text font we'll use */
34  
# Line 40 | Line 38 | static char SCCSid[] = "$SunId$ LBL";
38  
39   #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
40  
41 + #define  FIXWEIGHT      20              /* weight to add for fixation points */
42 +
43   #define  ourscreen      DefaultScreen(thedisplay)
44   #define  ourroot        RootWindow(thedisplay,ourscreen)
45  
# Line 48 | Line 48 | static char SCCSid[] = "$SunId$ LBL";
48   #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
49  
50   double  gamcor = 2.2;                   /* gamma correction */
51 + char  *gamstr = NULL;                   /* gamma value override */
52  
53   int  dither = 1;                        /* dither colors? */
54   int  fast = 0;                          /* keep picture in Pixmap? */
# Line 56 | Line 57 | char   *dispname = NULL;               /* our display name */
57  
58   Window  wind = 0;                       /* our output window */
59   unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
59 Font  fontid;                           /* our font */
60
60   int  maxcolors = 0;                     /* maximum colors */
61   int  greyscale = 0;                     /* in grey */
62  
# Line 66 | Line 65 | int  scale = 0;                                /* scalefactor; power of two */
65   int  xoff = 0;                          /* x image offset */
66   int  yoff = 0;                          /* y image offset */
67  
68 + int  parent = 0;                        /* number of children, -1 if child */
69 + int  sequential = 0;                    /* display images in sequence */
70 +
71 + char  *tout = "od";                     /* output of 't' command */
72 + int  tinterv = 0;                       /* interval between mouse reports */
73 +
74 + int  tmflags = TM_F_LINEAR;             /* tone mapping flags */
75 +
76   VIEW  ourview = STDVIEW;                /* image view parameters */
77   int  gotview = 0;                       /* got parameters from file */
78  
79   COLR  *scanline;                        /* scan line buffer */
80 + TMbright  *lscan;                       /* encoded luminance scanline */
81 + BYTE  *cscan;                           /* encoded chroma scanline */
82 + BYTE  *pscan;                           /* compute pixel scanline */
83  
84   RESOLU  inpres;                         /* input resolution and ordering */
85   int  xmax, ymax;                        /* picture dimensions */
86   int  width, height;                     /* window size */
87   char  *fname = NULL;                    /* input file name */
88 < FILE  *fin = stdin;                     /* input file */
88 > FILE  *fin = NULL;                      /* input file */
89   long  *scanpos = NULL;                  /* scan line positions in file */
90   int  cury = 0;                          /* current scan location */
91  
# Line 93 | Line 103 | unsigned char  *ourdata;               /* our image data */
103  
104   struct {
105          int  xmin, ymin, xsiz, ysiz;
106 < }  box = {0, 0, 0, 0};                  /* current box */
106 > }  bbox = {0, 0, 0, 0};                 /* current bbox */
107  
108   char  *geometry = NULL;                 /* geometry specification */
109  
# Line 104 | Line 114 | char  *progname;
114  
115   char  errmsg[128];
116  
117 + BYTE  clrtab[256][3];                   /* global color map */
118 +
119   extern long  ftell();
120  
121 < extern char  *malloc(), *calloc();
121 > extern char  *getenv();
122  
111 extern double  pow(), log();
112
123   Display  *thedisplay;
124 + Atom  closedownAtom, wmProtocolsAtom;
125  
126 + int  sigrecv;
127 +
128 + void  onsig(int i) { sigrecv++; }
129 +
130 +
131   main(argc, argv)
132   int  argc;
133   char  *argv[];
134   {
119        extern char  *getenv();
120        char  *gv;
135          int  headline();
136          int  i;
137 +        int  pid;
138          
139          progname = argv[0];
140 <        if ((gv = getenv("GAMMA")) != NULL)
126 <                gamcor = atof(gv);
140 >        fin = stdin;
141  
142          for (i = 1; i < argc; i++)
143                  if (argv[i][0] == '-')
144                          switch (argv[i][1]) {
145 <                        case 'c':
145 >                        case 'c':                       /* number of colors */
146                                  maxcolors = atoi(argv[++i]);
147                                  break;
148 <                        case 'b':
148 >                        case 'b':                       /* greyscale only */
149                                  greyscale = !greyscale;
150                                  break;
151 <                        case 'm':
151 >                        case 'm':                       /* monochrome */
152 >                                greyscale = 1;
153                                  maxcolors = 2;
154                                  break;
155 <                        case 'd':
155 >                        case 'd':                       /* display or dither */
156                                  if (argv[i][2] == 'i')
157                                          dispname = argv[++i];
158                                  else
159                                          dither = !dither;
160                                  break;
161 <                        case 'f':
161 >                        case 'f':                       /* save pixmap */
162                                  fast = !fast;
163                                  break;
164 <                        case 'e':
165 <                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
164 >                        case 's':                       /* one at a time */
165 >                                sequential = !sequential;
166 >                                break;
167 >                        case 'o':                       /* 't' output */
168 >                                tout = argv[i]+2;
169 >                                break;
170 >                        case 't':                       /* msec interval */
171 >                                tinterv = atoi(argv[++i]);
172 >                                break;
173 >                        case 'e':                       /* exposure comp. */
174 >                                i++;
175 >                                if (argv[i][0] == 'a') {
176 >                                        tmflags = TM_F_CAMERA;
177 >                                        break;
178 >                                }
179 >                                if (argv[i][0] == 'h') {
180 >                                        tmflags = TM_F_HUMAN;
181 >                                        break;
182 >                                }
183 >                                if (argv[i][0] != '+' && argv[i][0] != '-')
184                                          goto userr;
185 <                                scale = atoi(argv[++i]);
185 >                                scale = atoi(argv[i]);
186                                  break;
187 <                        case 'g':
187 >                        case 'g':                       /* gamma comp. */
188                                  if (argv[i][2] == 'e')
189                                          geometry = argv[++i];
190                                  else
191 <                                        gamcor = atof(argv[++i]);
191 >                                        gamstr = argv[++i];
192                                  break;
193                          default:
194                                  goto userr;
# Line 165 | Line 198 | char  *argv[];
198                  else
199                          break;
200  
201 <        if (i == argc-1) {
201 >        if (i > argc)
202 >                goto userr;
203 >        while (i < argc-1) {
204 >                sigrecv = 0;
205 >                signal(SIGCONT, onsig);
206 >                if ((pid=fork()) == 0) {        /* a child for each picture */
207 >                        parent = -1;
208 >                        break;
209 >                }
210 >                if (pid < 0)
211 >                        quiterr("fork failed");
212 >                parent++;
213 >                while (!sigrecv)
214 >                        pause();        /* wait for wake-up call */
215 >                i++;
216 >        }
217 >        if (i < argc) {                 /* open picture file */
218                  fname = argv[i];
219                  fin = fopen(fname, "r");
220 <                if (fin == NULL) {
221 <                        sprintf(errmsg, "cannot open file \"%s\"", fname);
222 <                        quiterr(errmsg);
174 <                }
175 <        } else if (i != argc)
176 <                goto userr;
220 >                if (fin == NULL)
221 >                        quiterr("cannot open picture file");
222 >        }
223                                  /* get header */
224          getheader(fin, headline, NULL);
225                                  /* get picture dimensions */
# Line 187 | Line 233 | char  *argv[];
233          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
234                  quiterr("out of memory");
235  
236 <        init();                 /* get file and open window */
236 >        init(argc, argv);                       /* get file and open window */
237  
238          for ( ; ; )
239                  getevent();             /* main loop */
240   userr:
241          fprintf(stderr,
242 <        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
242 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n",
243                          progname);
244 <        quit(1);
244 >        exit(1);
245   }
246  
247  
248 + int
249   headline(s)             /* get relevant info from header */
250   char  *s;
251   {
# Line 206 | Line 253 | char  *s;
253  
254          if (isexpos(s))
255                  exposure *= exposval(s);
256 <        else if (isformat(s)) {
210 <                formatval(fmt, s);
256 >        else if (formatval(fmt, s))
257                  wrongformat = strcmp(fmt, COLRFMT);
258 <        } else if (isview(s) && sscanview(&ourview, s) > 0)
258 >        else if (isview(s) && sscanview(&ourview, s) > 0)
259                  gotview++;
260 +        return(0);
261   }
262  
263  
264 < init()                  /* get data and open window */
264 > init(argc, argv)                        /* get data and open window */
265 > int argc;
266 > char **argv;
267   {
219        XWMHints        ourxwmhints;
268          XSetWindowAttributes    ourwinattr;
269 <        XSizeHints      oursizhints;
269 >        XClassHint      xclshints;
270 >        XWMHints        xwmhints;
271 >        XSizeHints      xszhints;
272 >        XTextProperty   windowName, iconName;
273 >        XGCValues       xgcv;
274 >        char    *name;
275          register int    i;
276          
277          if (fname != NULL) {
278                  scanpos = (long *)malloc(ymax*sizeof(long));
279                  if (scanpos == NULL)
280 <                        goto memerr;
280 >                        quiterr("out of memory");
281                  for (i = 0; i < ymax; i++)
282                          scanpos[i] = -1;
283 <        }
283 >                name = fname;
284 >        } else
285 >                name = progname;
286 >                                /* remove directory prefix from name */
287 >        for (i = strlen(name); i-- > 0; )
288 >                if (name[i] == '/')
289 >                        break;
290 >        name += i+1;
291          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
292                  quiterr("cannot open display");
293 +                                /* set gamma value */
294 +        if (gamstr == NULL)             /* get it from the X server */
295 +                gamstr = XGetDefault(thedisplay, "radiance", "gamma");
296 +        if (gamstr == NULL)             /* get it from the environment */
297 +                gamstr = getenv("DISPLAY_GAMMA");
298 +        if (gamstr != NULL)
299 +                gamcor = atof(gamstr);
300                                  /* get best visual for default screen */
301          getbestvis();
302                                  /* store image */
303          getras();
304 <                                /* open window */
305 <        ourwinattr.border_pixel = ourblack;
306 <        ourwinattr.background_pixel = ourwhite;
240 <        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
241 <                        ourvis.visual, AllocNone);
242 <        wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH,
243 <                        ourvis.depth, InputOutput, ourvis.visual,
244 <                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
245 <        if (wind == 0)
246 <                quiterr("cannot create window");
247 <        XFreeColormap(thedisplay, ourwinattr.colormap);
248 <        width = xmax;
249 <        height = ymax;
250 <        ourgc = XCreateGC(thedisplay, wind, 0, 0);
251 <        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes);
252 <        revgc = XCreateGC(thedisplay, wind, 0, 0);
253 <        XSetFunction(thedisplay, revgc, GXinvert);
254 <        fontid = XLoadFont(thedisplay, FONTNAME);
255 <        if (fontid == 0)
256 <                quiterr("cannot get font");
257 <        XSetFont(thedisplay, ourgc, fontid);
258 <        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
259 <                        XC_diamond_cross));
260 <        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
304 >                                /* get size and position */
305 >        xszhints.flags = 0;
306 >        xszhints.width = xmax; xszhints.height = ymax;
307          if (geometry != NULL) {
308 <                bzero((char *)&oursizhints, sizeof(oursizhints));
309 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
310 <                                (unsigned *)&oursizhints.width,
265 <                                (unsigned *)&oursizhints.height);
308 >                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
309 >                                (unsigned *)&xszhints.width,
310 >                                (unsigned *)&xszhints.height);
311                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
312 <                        oursizhints.flags |= USSize;
313 <                else {
314 <                        oursizhints.width = xmax;
270 <                        oursizhints.height = ymax;
271 <                        oursizhints.flags |= PSize;
272 <                }
312 >                        xszhints.flags |= USSize;
313 >                else
314 >                        xszhints.flags |= PSize;
315                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
316 <                        oursizhints.flags |= USPosition;
316 >                        xszhints.flags |= USPosition;
317                          if (i & XNegative)
318 <                                oursizhints.x += DisplayWidth(thedisplay,
319 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
318 >                                xszhints.x += DisplayWidth(thedisplay,
319 >                                ourscreen)-1-xszhints.width-2*BORWIDTH;
320                          if (i & YNegative)
321 <                                oursizhints.y += DisplayHeight(thedisplay,
322 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
321 >                                xszhints.y += DisplayHeight(thedisplay,
322 >                                ourscreen)-1-xszhints.height-2*BORWIDTH;
323                  }
282                XSetNormalHints(thedisplay, wind, &oursizhints);
324          }
325 <        ourxwmhints.flags = InputHint|IconPixmapHint;
326 <        ourxwmhints.input = True;
327 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
325 >        /* open window */
326 >        i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
327 >        ourwinattr.border_pixel = ourwhite;
328 >        ourwinattr.background_pixel = ourblack;
329 >        if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
330 >                ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
331 >                i |= CWColormap;
332 >        }
333 >        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
334 >                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
335 >        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
336 >        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
337 >                        xszhints.width, xszhints.height, BORWIDTH,
338 >                        ourvis.depth, InputOutput, ourvis.visual,
339 >                        i, &ourwinattr);
340 >        if (wind == 0)
341 >                quiterr("cannot create window");
342 >        width = xmax;
343 >        height = ymax;
344 >        /* prepare graphics drawing context */
345 >        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
346 >                quiterr("cannot get font");
347 >        xgcv.foreground = ourblack;
348 >        xgcv.background = ourwhite;
349 >        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
350 >                        GCFont, &xgcv);
351 >        xgcv.function = GXinvert;
352 >        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
353 >                        GCFunction, &xgcv);
354 >
355 >        /* set up the window manager */
356 >        xwmhints.flags = InputHint|IconPixmapHint;
357 >        xwmhints.input = True;
358 >        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
359                          wind, icondata, iconwidth, iconheight);
360 <        XSetWMHints(thedisplay, wind, &ourxwmhints);
361 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
362 <                        |ButtonMotionMask|StructureNotifyMask
363 <                        |KeyPressMask|ExposureMask);
360 >
361 >        windowName.encoding = iconName.encoding = XA_STRING;
362 >        windowName.format = iconName.format = 8;
363 >        windowName.value = (u_char *)name;
364 >        windowName.nitems = strlen(windowName.value);
365 >        iconName.value = (u_char *)name;
366 >        iconName.nitems = strlen(windowName.value);
367 >
368 >        xclshints.res_name = NULL;
369 >        xclshints.res_class = "Ximage";
370 >        XSetWMProperties(thedisplay, wind, &windowName, &iconName,
371 >                        argv, argc, &xszhints, &xwmhints, &xclshints);
372 >        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
373 >        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
374 >        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
375 >
376          XMapWindow(thedisplay, wind);
293        return;
294 memerr:
295        quiterr("out of memory");
377   } /* end of init */
378  
379  
380   quiterr(err)            /* print message and exit */
381   char  *err;
382   {
383 <        if (err != NULL) {
384 <                fprintf(stderr, "%s: %s\n", progname, err);
385 <                exit(1);
383 >        register int  es;
384 >        int  cs;
385 >
386 >        if (es = err != NULL)
387 >                fprintf(stderr, "%s: %s: %s\n", progname,
388 >                                fname==NULL?"<stdin>":fname, err);
389 >        if (thedisplay != NULL)
390 >                XCloseDisplay(thedisplay);
391 >        if (parent < 0 & sigrecv == 0)
392 >                kill(getppid(), SIGCONT);
393 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
394 >                if (es == 0)
395 >                        es = cs>>8 & 0xff;
396 >                parent--;
397          }
398 <        exit(0);
398 >        exit(es);
399   }
400  
401  
310 eputs(s)
311 char    *s;
312 {
313        fputs(s, stderr);
314 }
315
316
317 quit(code)
318 int  code;
319 {
320        exit(code);
321 }
322
323
402   static int
403   viscmp(v1,v2)           /* compare visual to see which is better, descending */
404   register XVisualInfo    *v1, *v2;
# Line 335 | Line 413 | register XVisualInfo   *v1, *v2;
413                                  return(-1);
414                          if (v1->depth == 32 && v2->depth == 24)
415                                  return(1);
416 <                        return(0);
416 >                                        /* go for maximum depth otherwise */
417 >                        return(v2->depth - v1->depth);
418                  }
419                                          /* don't be too greedy */
420                  if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
421                          return(v1->depth - v2->depth);
422                  return(v2->depth - v1->depth);
423          }
424 <                                        /* prefer Pseudo when < 24-bit */
424 >                                        /* prefer Pseudo when < 15-bit */
425          if ((v1->class == TrueColor || v1->class == DirectColor) &&
426 <                        v1->depth < 24)
426 >                        v1->depth < 15)
427                  bad1 = 1;
428          if ((v2->class == TrueColor || v2->class == DirectColor) &&
429 <                        v2->depth < 24)
429 >                        v2->depth < 15)
430                  bad2 = -1;
431          if (bad1 | bad2)
432                  return(bad1+bad2);
# Line 424 | Line 503 | static char  vistype[][12] = {
503                  greyscale = 1;
504          if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
505                  maxcolors = ourvis.colormap_size;
427        if (maxcolors > 4)
428                maxcolors -= 2;
506          if (ourvis.class == StaticGray) {
507                  ourblack = 0;
508                  ourwhite = 255;
# Line 436 | Line 513 | static char  vistype[][12] = {
513                          ourblack = 0;
514                          ourwhite = 1;
515                  }
516 +                if (maxcolors > 4)
517 +                        maxcolors -= 2;
518          } else {
519                  ourblack = 0;
520                  ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
# Line 446 | Line 525 | static char  vistype[][12] = {
525  
526   getras()                                /* get raster file */
527   {
449        colormap        ourmap;
528          XVisualInfo     vinfo;
529  
530          if (maxcolors <= 2) {           /* monochrome */
# Line 458 | Line 536 | getras()                               /* get raster file */
536                  if (ourras == NULL)
537                          goto fail;
538                  getmono();
539 <        } else if (ourvis.class == TrueColor || ourvis.class == DirectColor) {
540 <                ourdata = (unsigned char *)malloc(4*xmax*ymax);
539 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
540 >                int  datsiz = ourvis.depth>16 ? sizeof(int32) : sizeof(int16);
541 >                ourdata = (unsigned char *)malloc(datsiz*xmax*ymax);
542                  if (ourdata == NULL)
543                          goto fail;
544 <                ourras = make_raster(thedisplay, &ourvis, 32,
545 <                                ourdata, xmax, ymax, 32);
544 >                ourras = make_raster(thedisplay, &ourvis, datsiz*8,
545 >                                ourdata, xmax, ymax, datsiz*8);
546                  if (ourras == NULL)
547                          goto fail;
548                  getfull();
# Line 475 | Line 554 | getras()                               /* get raster file */
554                                  xmax, ymax, 8);
555                  if (ourras == NULL)
556                          goto fail;
557 <                if (ourvis.class == StaticGray)
557 >                if (greyscale)
558                          getgrey();
559 <                else {
560 <                        if (greyscale)
561 <                                biq(dither,maxcolors,1,ourmap);
562 <                        else
484 <                                ciq(dither,maxcolors,1,ourmap);
485 <                        if (init_rcolors(ourras, ourmap[0],
486 <                                        ourmap[1], ourmap[2]) == 0)
487 <                                goto fail;
488 <                }
559 >                else
560 >                        getmapped();
561 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
562 >                        goto fail;
563          }
564 <                return;
564 >        return;
565   fail:
566          quiterr("could not create raster image");
567   }
# Line 495 | Line 569 | fail:
569  
570   getevent()                              /* process the next event */
571   {
572 <        union {
499 <                XEvent  u;
500 <                XConfigureEvent  c;
501 <                XExposeEvent  e;
502 <                XButtonPressedEvent  b;
503 <                XKeyPressedEvent  k;
504 <        } 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, wind);
587 +                if (!sequential & parent < 0 & sigrecv == 0) {
588 +                        kill(getppid(), SIGCONT);
589 +                        sigrecv--;
590 +                }
591                  break;
592          case UnmapNotify:
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 >        FLOAT  hv[2];
629 >        FVECT  rorg, rdir;
630 >        COLOR  cval;
631 >        register char  *cp;
632 >
633 >        bbox.xmin = xpos; bbox.xsiz = 1;
634 >        bbox.ymin = ypos; bbox.ysiz = 1;
635 >        avgbox(cval);
636 >        scalecolor(cval, 1./exposure);
637 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
638 >        if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
639 >                rorg[0] = rorg[1] = rorg[2] =
640 >                rdir[0] = rdir[1] = rdir[2] = 0.;
641 >
642 >        for (cp = tout; *cp; cp++)      /* print what they asked for */
643 >                switch (*cp) {
644 >                case 'o':                       /* origin */
645 >                        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
646 >                        break;
647 >                case 'd':                       /* direction */
648 >                        printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
649 >                        break;
650 >                case 'v':                       /* radiance value */
651 >                        printf("%e %e %e ", colval(cval,RED),
652 >                                        colval(cval,GRN), colval(cval,BLU));
653 >                        break;
654 >                case 'l':                       /* luminance */
655 >                        printf("%e ", luminance(cval));
656 >                        break;
657 >                case 'p':                       /* pixel position */
658 >                        printf("%d %d ", (int)(hv[0]*inpres.xr),
659 >                                        (int)(hv[1]*inpres.yr));
660 >                        break;
661 >                }
662 >        putchar('\n');
663 >        fflush(stdout);
664 >        return(0);
665 > }
666 >
667 >
668 > docom(ekey)                             /* execute command */
669   XKeyPressedEvent  *ekey;
670   {
671          char  buf[80];
# Line 543 | Line 674 | XKeyPressedEvent  *ekey;
674          int  com, n;
675          double  comp;
676          FLOAT  hv[2];
546        FVECT  rorg, rdir;
677  
678          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
679          if (n == 0)
# Line 551 | Line 681 | XKeyPressedEvent  *ekey;
681          com = buf[0];
682          switch (com) {                  /* interpret command */
683          case 'q':
684 +        case 'Q':
685          case CTRL('D'):                         /* quit */
686 <                quit(0);
686 >                quiterr(NULL);
687          case '\n':
688          case '\r':
689          case 'l':
690          case 'c':                               /* value */
691 <                if (avgbox(cval) == -1)
691 >                if (!avgbox(cval))
692                          return(-1);
693                  switch (com) {
694                  case '\n':
# Line 565 | Line 696 | XKeyPressedEvent  *ekey;
696                          sprintf(buf, "%.3f", intens(cval)/exposure);
697                          break;
698                  case 'l':                               /* luminance */
699 <                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
699 >                        sprintf(buf, "%.1fL", luminance(cval)/exposure);
700                          break;
701                  case 'c':                               /* color */
702                          comp = pow(2.0, (double)scale);
# Line 576 | Line 707 | XKeyPressedEvent  *ekey;
707                          break;
708                  }
709                  XDrawImageString(thedisplay, wind, ourgc,
710 <                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
710 >                                bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
711                  return(0);
712          case 'i':                               /* identify (contour) */
713                  if (ourras->pixels == NULL)
# Line 598 | Line 729 | XKeyPressedEvent  *ekey;
729                                          buf, strlen(buf));
730                  return(0);
731          case 't':                               /* trace */
732 <                if (!gotview) {
733 <                        XBell(thedisplay, 0);
732 >                return(traceray(ekey->x, ekey->y));
733 >        case 'a':                               /* auto exposure */
734 >                if (fname == NULL)
735                          return(-1);
736 <                }
737 <                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
738 <                if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
736 >                tmflags = TM_F_CAMERA;
737 >                strcpy(buf, "auto exposure...");
738 >                goto remap;
739 >        case 'h':                               /* human response */
740 >                if (fname == NULL)
741                          return(-1);
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);
611 <                return(0);
742 >                tmflags = TM_F_HUMAN;
743 >                strcpy(buf, "human exposure...");
744 >                goto remap;
745          case '=':                               /* adjust exposure */
746 <                if (avgbox(cval) == -1)
746 >        case '@':                               /* adaptation level */
747 >                if (!avgbox(cval))
748                          return(-1);
749 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
750 <                if (n == 0)
751 <                        return(0);
752 <                scale_rcolors(ourras, pow(2.0, (double)n));
749 >                comp = bright(cval);
750 >                if (comp < 1e-20) {
751 >                        XBell(thedisplay, 0);
752 >                        return(-1);
753 >                }
754 >                if (com == '@')
755 >                        comp = 106./exposure/
756 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
757 >                else
758 >                        comp = .5/comp;
759 >                comp = log(comp)/.69315 - scale;
760 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
761 >                if (tmflags != TM_F_LINEAR)
762 >                        tmflags = TM_F_LINEAR;  /* turn off tone mapping */
763 >                else {
764 >                        if (n == 0)             /* else check if any change */
765 >                                return(0);
766 >                        scale_rcolors(ourras, pow(2.0, (double)n));
767 >                }
768                  scale += n;
769                  sprintf(buf, "%+d", scale);
770 +        remap:
771                  XDrawImageString(thedisplay, wind, ourgc,
772 <                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
772 >                                bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
773                  XFlush(thedisplay);
774 <                free(ourdata);
774 >                /* free(ourdata);       This is done in XDestroyImage()! */
775                  free_raster(ourras);
776                  getras();
777          /* fall through */
# Line 631 | Line 781 | XKeyPressedEvent  *ekey;
781                  XClearWindow(thedisplay, wind);
782                  map_rcolors(ourras, wind);
783                  if (fast)
784 <                        make_rpixmap(ourras);
784 >                        make_rpixmap(ourras, wind);
785                  redraw(0, 0, width, height);
786                  return(0);
787 +        case 'f':                               /* turn on fast redraw */
788 +                fast = 1;
789 +                make_rpixmap(ourras, wind);
790 +                return(0);
791 +        case 'F':                               /* turn off fast redraw */
792 +                fast = 0;
793 +                free_rpixmap(ourras);
794 +                return(0);
795 +        case '0':                               /* recenter origin */
796 +                if (xoff == 0 & yoff == 0)
797 +                        return(0);
798 +                xoff = yoff = 0;
799 +                XClearWindow(thedisplay, wind);
800 +                redraw(0, 0, width, height);
801 +                return(0);
802          case ' ':                               /* clear */
803 <                redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
803 >                redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz);
804                  return(0);
805          default:
806                  XBell(thedisplay, 0);
# Line 647 | Line 812 | XKeyPressedEvent  *ekey;
812   moveimage(ebut)                         /* shift the image */
813   XButtonPressedEvent  *ebut;
814   {
815 <        union {
651 <                XEvent  u;
652 <                XButtonReleasedEvent  b;
653 <                XPointerMovedEvent  m;
654 <        }  e;
815 >        XEvent  e;
816          int     mxo, myo;
817  
818 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
819 <        while (e.u.type == MotionNotify) {
820 <                mxo = e.m.x;
821 <                myo = e.m.y;
818 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
819 >        while (e.type == MotionNotify) {
820 >                mxo = e.xmotion.x;
821 >                myo = e.xmotion.y;
822                  revline(ebut->x, ebut->y, mxo, myo);
823                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
824                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
825 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
825 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
826                  revline(ebut->x, ebut->y, mxo, myo);
827                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
828                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
829          }
830 <        xoff += e.b.x - ebut->x;
831 <        yoff += e.b.y - ebut->y;
830 >        xoff += e.xbutton.x - ebut->x;
831 >        yoff += e.xbutton.y - ebut->y;
832          XClearWindow(thedisplay, wind);
833          redraw(0, 0, width, height);
834   }
835  
836  
837 < getbox(ebut)                            /* get new box */
837 > getbox(ebut)                            /* get new bbox */
838   XButtonPressedEvent  *ebut;
839   {
840 <        union {
680 <                XEvent  u;
681 <                XButtonReleasedEvent  b;
682 <                XPointerMovedEvent  m;
683 <        }  e;
840 >        XEvent  e;
841  
842 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
843 <        while (e.u.type == MotionNotify) {
844 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
845 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
846 <                revbox(ebut->x, ebut->y, box.xmin, box.ymin);
842 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
843 >        while (e.type == MotionNotify) {
844 >                revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y);
845 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
846 >                revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin);
847          }
848 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
849 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
850 <        if (box.xmin > ebut->x) {
851 <                box.xsiz = box.xmin - ebut->x + 1;
852 <                box.xmin = ebut->x;
848 >        bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
849 >        bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
850 >        if (bbox.xmin > ebut->x) {
851 >                bbox.xsiz = bbox.xmin - ebut->x + 1;
852 >                bbox.xmin = ebut->x;
853          } else {
854 <                box.xsiz = ebut->x - box.xmin + 1;
854 >                bbox.xsiz = ebut->x - bbox.xmin + 1;
855          }
856 <        if (box.ymin > ebut->y) {
857 <                box.ysiz = box.ymin - ebut->y + 1;
858 <                box.ymin = ebut->y;
856 >        if (bbox.ymin > ebut->y) {
857 >                bbox.ysiz = bbox.ymin - ebut->y + 1;
858 >                bbox.ymin = ebut->y;
859          } else {
860 <                box.ysiz = ebut->y - box.ymin + 1;
860 >                bbox.ysiz = ebut->y - bbox.ymin + 1;
861          }
862   }
863  
864  
865 < revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
865 > trackrays(ebut)                         /* trace rays as mouse moves */
866 > XButtonPressedEvent  *ebut;
867 > {
868 >        XEvent  e;
869 >        unsigned long   lastrept;
870 >
871 >        traceray(ebut->x, ebut->y);
872 >        lastrept = ebut->time;
873 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
874 >        while (e.type == MotionNotify) {
875 >                if (e.xmotion.time >= lastrept + tinterv) {
876 >                        traceray(e.xmotion.x, e.xmotion.y);
877 >                        lastrept = e.xmotion.time;
878 >                }
879 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
880 >        }
881 > }
882 >
883 >
884 > revbox(x0, y0, x1, y1)                  /* draw bbox with reversed lines */
885   int  x0, y0, x1, y1;
886   {
887          revline(x0, y0, x1, y0);
# Line 715 | Line 891 | int  x0, y0, x1, y1;
891   }
892  
893  
894 < avgbox(clr)                             /* average color over current box */
895 < COLOR  clr;
894 > int
895 > colavg(scn, n, cavg)
896 > register COLR   *scn;
897 > register int    n;
898 > COLOR   cavg;
899   {
900 <        static COLOR  lc;
901 <        static int  ll, lr, lt, lb;
900 >        COLOR   col;
901 >
902 >        while (n--) {
903 >                colr_color(col, *scn++);
904 >                addcolor(cavg, col);
905 >        }
906 > }
907 >
908 >
909 > int
910 > avgbox(cavg)                            /* average color over current bbox */
911 > COLOR   cavg;
912 > {
913 >        double  d;
914 >        register int    rval;
915 >
916 >        setcolor(cavg, 0., 0., 0.);
917 >        rval = dobox(colavg, (char *)cavg);
918 >        if (rval > 0) {
919 >                d = 1./rval;
920 >                scalecolor(cavg, d);
921 >        }
922 >        return(rval);
923 > }
924 >
925 >
926 > int
927 > dobox(f, p)                             /* run function over bbox */
928 > int     (*f)();                 /* function to call for each subscan */
929 > char    *p;                     /* pointer to private data */
930 > {
931          int  left, right, top, bottom;
932          int  y;
725        double  d;
726        COLOR  ctmp;
727        register int  x;
933  
934 <        setcolor(clr, 0.0, 0.0, 0.0);
935 <        left = box.xmin - xoff;
731 <        right = left + box.xsiz;
934 >        left = bbox.xmin - xoff;
935 >        right = left + bbox.xsiz;
936          if (left < 0)
937                  left = 0;
938          if (right > xmax)
939                  right = xmax;
940          if (left >= right)
941 <                return(-1);
942 <        top = box.ymin - yoff;
943 <        bottom = top + box.ysiz;
941 >                return(0);
942 >        top = bbox.ymin - yoff;
943 >        bottom = top + bbox.ysiz;
944          if (top < 0)
945                  top = 0;
946          if (bottom > ymax)
947                  bottom = ymax;
948          if (top >= bottom)
949 <                return(-1);
746 <        if (left == ll && right == lr && top == lt && bottom == lb) {
747 <                copycolor(clr, lc);
748 <                return;
749 <        }
949 >                return(0);
950          for (y = top; y < bottom; y++) {
951                  if (getscan(y) == -1)
952                          return(-1);
953 <                for (x = left; x < right; x++) {
954 <                        colr_color(ctmp, scanline[x]);
955 <                        addcolor(clr, ctmp);
953 >                (*f)(scanline+left, right-left, p);
954 >        }
955 >        return((right-left)*(bottom-top));
956 > }
957 >
958 >
959 > int
960 > addfix(scn, n)                  /* add fixation points to histogram */
961 > COLR    *scn;
962 > int     n;
963 > {
964 >        if (tmCvColrs(lscan, TM_NOCHROM, scn, n))
965 >                goto tmerr;
966 >        if (tmAddHisto(lscan, n, FIXWEIGHT))
967 >                goto tmerr;
968 >        return;
969 > tmerr:
970 >        quiterr("tone mapping error");
971 > }
972 >
973 >
974 > make_tonemap()                  /* initialize tone mapping */
975 > {
976 >        int  flags, y;
977 >
978 >        if (tmflags != TM_F_LINEAR && fname == NULL) {
979 >                fprintf(stderr, "%s: cannot adjust tone of standard input\n",
980 >                                progname);
981 >                tmflags = TM_F_LINEAR;
982 >        }
983 >        if (tmflags == TM_F_LINEAR) {   /* linear with clamping */
984 >                setcolrcor(pow, 1.0/gamcor);
985 >                return;
986 >        }
987 >        flags = tmflags;                /* histogram adjustment */
988 >        if (greyscale) flags |= TM_F_BW;
989 >        if (tmTop != NULL) {            /* reuse old histogram if one */
990 >                tmDone(tmTop);
991 >                tmTop->flags = flags;
992 >        } else {                        /* else initialize */
993 >                if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
994 >                        goto memerr;
995 >                if (greyscale) {
996 >                        cscan = TM_NOCHROM;
997 >                        if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
998 >                                goto memerr;
999 >                } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax))
1000 >                                == NULL)
1001 >                        goto memerr;
1002 >                                                /* initialize tm library */
1003 >                if (tmInit(flags, stdprims, gamcor) == NULL)
1004 >                        goto memerr;
1005 >                if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
1006 >                        goto tmerr;
1007 >                                                /* compute picture histogram */
1008 >                for (y = 0; y < ymax; y++) {
1009 >                        getscan(y);
1010 >                        if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
1011 >                                goto tmerr;
1012 >                        if (tmAddHisto(lscan, xmax, 1))
1013 >                                goto tmerr;
1014                  }
1015          }
1016 <        d = 1.0/((right-left)*(bottom-top));
1017 <        scalecolor(clr, d);
1018 <        ll = left; lr = right; lt = top; lb = bottom;
1019 <        copycolor(lc, clr);
1020 <        return(0);
1016 >        tmDup();                        /* add fixations to duplicate map */
1017 >        dobox(addfix, NULL);
1018 >                                        /* (re)compute tone mapping */
1019 >        if (tmComputeMapping(gamcor, 0., 0.))
1020 >                goto tmerr;
1021 >        return;
1022 > memerr:
1023 >        quiterr("out of memory in make_tonemap");
1024 > tmerr:
1025 >        quiterr("tone mapping error");
1026   }
1027  
1028  
1029 + tmap_colrs(scn, len)            /* apply tone mapping to scanline */
1030 + register COLR  *scn;
1031 + int  len;
1032 + {
1033 +        register BYTE  *ps;
1034 +
1035 +        if (tmflags == TM_F_LINEAR) {
1036 +                if (scale)
1037 +                        shiftcolrs(scn, len, scale);
1038 +                colrs_gambs(scn, len);
1039 +                return;
1040 +        }
1041 +        if (len > xmax)
1042 +                quiterr("code error 1 in tmap_colrs");
1043 +        if (tmCvColrs(lscan, cscan, scn, len))
1044 +                goto tmerr;
1045 +        if (tmMapPixels(pscan, lscan, cscan, len))
1046 +                goto tmerr;
1047 +        ps = pscan;
1048 +        if (greyscale)
1049 +                while (len--) {
1050 +                        scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1051 +                        scn[0][EXP] = COLXS;
1052 +                        scn++;
1053 +                }
1054 +        else
1055 +                while (len--) {
1056 +                        scn[0][RED] = *ps++;
1057 +                        scn[0][GRN] = *ps++;
1058 +                        scn[0][BLU] = *ps++;
1059 +                        scn[0][EXP] = COLXS;
1060 +                        scn++;
1061 +                }
1062 +        return;
1063 + tmerr:
1064 +        quiterr("tone mapping error");
1065 + }
1066 +
1067 +
1068   getmono()                       /* get monochrome data */
1069   {
1070          register unsigned char  *dp;
# Line 774 | Line 1076 | getmono()                      /* get monochrome data */
1076                  quiterr("out of memory in getmono");
1077          dp = ourdata - 1;
1078          for (y = 0; y < ymax; y++) {
1079 <                if (getscan(y) < 0)
778 <                        quiterr("seek error in getmono");
779 <                normcolrs(scanline, xmax, scale);
1079 >                getscan(y);
1080                  add2icon(y, scanline);
1081 +                normcolrs(scanline, xmax, scale);
1082                  err = 0;
1083                  for (x = 0; x < xmax; x++) {
1084                          if (!(x&7))
# Line 792 | Line 1093 | getmono()                      /* get monochrome data */
1093                          cerr[x] = err + errp;
1094                  }
1095          }
1096 <        free((char *)cerr);
1096 >        free((void *)cerr);
1097   }
1098  
1099  
# Line 803 | Line 1104 | COLR  *scan;
1104          static short  cerr[ICONSIZ];
1105          static int  ynext;
1106          static char  *dp;
1107 +        COLR  clr;
1108          register int  err;
1109          register int    x, ti;
1110          int  errp;
# Line 833 | Line 1135 | COLR  *scan;
1135                          *++dp = 0;
1136                  errp = err;
1137                  ti = x*xmax/iconwidth;
1138 <                err += normbright(scan[ti]) + cerr[x];
1138 >                copycolr(clr, scan[ti]);
1139 >                normcolrs(&clr, 1, scale);
1140 >                err += normbright(clr) + cerr[x];
1141                  if (err > 127)
1142                          err -= 255;
1143                  else
# Line 848 | Line 1152 | COLR  *scan;
1152   getfull()                       /* get full (24-bit) data */
1153   {
1154          int     y;
1155 <        register unsigned long  *dp;
1155 >        register uint32 *dp;
1156 >        register uint16 *dph;
1157          register int    x;
1158 <                                        /* set gamma correction */
1159 <        setcolrgam(gamcor);
1158 >                                        /* initialize tone mapping */
1159 >        make_tonemap();
1160                                          /* read and convert file */
1161 <        dp = (unsigned long *)ourdata;
1161 >        dp = (uint32 *)ourdata;
1162 >        dph = (uint16 *)ourdata;
1163          for (y = 0; y < ymax; y++) {
1164 <                if (getscan(y) < 0)
859 <                        quiterr("seek error in getfull");
860 <                if (scale)
861 <                        shiftcolrs(scanline, xmax, scale);
862 <                colrs_gambs(scanline, xmax);
1164 >                getscan(y);
1165                  add2icon(y, scanline);
1166 <                if (ourras->image->blue_mask & 1)
1166 >                tmap_colrs(scanline, xmax);
1167 >                switch (ourras->image->blue_mask) {
1168 >                case 0xff:              /* 24-bit RGB */
1169                          for (x = 0; x < xmax; x++)
1170 <                                *dp++ = scanline[x][RED] << 16 |
1171 <                                        scanline[x][GRN] << 8 |
1172 <                                        scanline[x][BLU] ;
1173 <                else
1170 >                                *dp++ = (uint32)scanline[x][RED] << 16 |
1171 >                                        (uint32)scanline[x][GRN] << 8 |
1172 >                                        (uint32)scanline[x][BLU] ;
1173 >                        break;
1174 >                case 0xff0000:          /* 24-bit BGR */
1175                          for (x = 0; x < xmax; x++)
1176 <                                *dp++ = scanline[x][RED] |
1177 <                                        scanline[x][GRN] << 8 |
1178 <                                        scanline[x][BLU] << 16 ;
1176 >                                *dp++ = (uint32)scanline[x][RED] |
1177 >                                        (uint32)scanline[x][GRN] << 8 |
1178 >                                        (uint32)scanline[x][BLU] << 16 ;
1179 >                        break;
1180 > #if 0
1181 >                case 0x1f:              /* 15-bit RGB */
1182 >                        for (x = 0; x < xmax; x++)
1183 >                                *dph++ =        (scanline[x][RED] << 7 & 0x7c00) |
1184 >                                        (scanline[x][GRN] << 2 & 0x3e0) |
1185 >                                        (unsigned)scanline[x][BLU] >> 3 ;
1186 >                        break;
1187 >                case 0x7c00:            /* 15-bit BGR */
1188 >                        for (x = 0; x < xmax; x++)
1189 >                                *dph++ =        (unsigned)scanline[x][RED] >> 3 |
1190 >                                        (scanline[x][GRN] << 2 & 0x3e0) |
1191 >                                        (scanline[x][BLU] << 7 & 0x7c00) ;
1192 >                        break;
1193 > #endif
1194 >                default:                /* unknown */
1195 >                        if (ourvis.depth > 16)
1196 >                                for (x = 0; x < xmax; x++) {
1197 >                                        *dp = ourras->image->red_mask &
1198 >                                                ourras->image->red_mask*scanline[x][RED]/255;
1199 >                                        *dp |= ourras->image->green_mask &
1200 >                                                ourras->image->green_mask*scanline[x][GRN]/255;
1201 >                                        *dp++ |= ourras->image->blue_mask &
1202 >                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1203 >                                }
1204 >                        else
1205 >                                for (x = 0; x < xmax; x++) {
1206 >                                        *dph = ourras->image->red_mask &
1207 >                                                ourras->image->red_mask*scanline[x][RED]/255;
1208 >                                        *dph |= ourras->image->green_mask &
1209 >                                                ourras->image->green_mask*scanline[x][GRN]/255;
1210 >                                        *dph++ |= ourras->image->blue_mask &
1211 >                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1212 >                                }
1213 >                        break;
1214 >                }
1215          }
1216   }
1217  
# Line 880 | Line 1221 | getgrey()                      /* get greyscale data */
1221          int     y;
1222          register unsigned char  *dp;
1223          register int    x;
1224 <                                        /* set gamma correction */
1225 <        setcolrgam(gamcor);
1224 >                                        /* initialize tone mapping */
1225 >        make_tonemap();
1226                                          /* read and convert file */
1227          dp = ourdata;
1228          for (y = 0; y < ymax; y++) {
1229 <                if (getscan(y) < 0)
889 <                        quiterr("seek error in getfull");
890 <                if (scale)
891 <                        shiftcolrs(scanline, xmax, scale);
892 <                colrs_gambs(scanline, xmax);
1229 >                getscan(y);
1230                  add2icon(y, scanline);
1231 <                if (ourvis.colormap_size < 256)
1231 >                tmap_colrs(scanline, xmax);
1232 >                if (maxcolors < 256)
1233                          for (x = 0; x < xmax; x++)
1234 <                                *dp++ = ((long)normbright(scanline[x]) *
1235 <                                        ourvis.colormap_size + 128) >> 8;
1234 >                                *dp++ = ((int32)scanline[x][GRN] *
1235 >                                        maxcolors + maxcolors/2) >> 8;
1236                  else
1237                          for (x = 0; x < xmax; x++)
1238 <                                *dp++ = normbright(scanline[x]);
1238 >                                *dp++ = scanline[x][GRN];
1239          }
1240 +        for (x = 0; x < maxcolors; x++)
1241 +                clrtab[x][RED] = clrtab[x][GRN] =
1242 +                        clrtab[x][BLU] = ((int32)x*256 + 128)/maxcolors;
1243   }
1244  
1245  
1246 + getmapped()                     /* get color-mapped data */
1247 + {
1248 +        int     y;
1249 +                                        /* make sure we can do it first */
1250 +        if (fname == NULL)
1251 +                quiterr("cannot map colors from standard input");
1252 +                                        /* initialize tone mapping */
1253 +        make_tonemap();
1254 +                                        /* make histogram */
1255 +        if (new_histo((int32)xmax*ymax) == -1)
1256 +                quiterr("cannot initialize histogram");
1257 +        for (y = 0; y < ymax; y++) {
1258 +                if (getscan(y) < 0)
1259 +                        break;
1260 +                add2icon(y, scanline);
1261 +                tmap_colrs(scanline, xmax);
1262 +                cnt_colrs(scanline, xmax);
1263 +        }
1264 +                                        /* map pixels */
1265 +        if (!new_clrtab(maxcolors))
1266 +                quiterr("cannot create color map");
1267 +        for (y = 0; y < ymax; y++) {
1268 +                getscan(y);
1269 +                tmap_colrs(scanline, xmax);
1270 +                if (dither)
1271 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
1272 +                else
1273 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
1274 +        }
1275 + }
1276 +
1277 +
1278   scale_rcolors(xr, sf)                   /* scale color map */
1279   register XRASTER        *xr;
1280   double  sf;
# Line 933 | Line 1306 | double sf;
1306   getscan(y)
1307   int  y;
1308   {
1309 +        static int  trunced = -1;               /* truncated file? */
1310 + skipit:
1311 +        if (trunced >= 0 && y >= trunced) {
1312 +                bzero(scanline, xmax*sizeof(COLR));
1313 +                return(-1);
1314 +        }
1315          if (y != cury) {
1316                  if (scanpos == NULL || scanpos[y] == -1)
1317                          return(-1);
1318                  if (fseek(fin, scanpos[y], 0) == -1)
1319                          quiterr("fseek error");
1320                  cury = y;
1321 <        } else if (scanpos != NULL)
1321 >        } else if (scanpos != NULL && scanpos[y] == -1)
1322                  scanpos[y] = ftell(fin);
1323  
1324 <        if (freadcolrs(scanline, xmax, fin) < 0)
1325 <                quiterr("read error");
1326 <
1324 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1325 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1326 >                                progname, fname==NULL?"<stdin>":fname);
1327 >                trunced = y;
1328 >                goto skipit;
1329 >        }
1330          cury++;
1331          return(0);
950 }
951
952
953 picreadline3(y, l3)                     /* read in 3-byte scanline */
954 int  y;
955 register rgbpixel  *l3;
956 {
957        register int    i;
958                                                        /* read scanline */
959        if (getscan(y) < 0)
960                quiterr("cannot seek for picreadline");
961                                                        /* convert scanline */
962        normcolrs(scanline, xmax, scale);
963        add2icon(y, scanline);
964        for (i = 0; i < xmax; i++) {
965                l3[i].r = scanline[i][RED];
966                l3[i].g = scanline[i][GRN];
967                l3[i].b = scanline[i][BLU];
968        }
969 }
970
971
972 picwriteline(y, l)              /* add 8-bit scanline to image */
973 int  y;
974 pixel  *l;
975 {
976        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
977 }
978
979
980 picreadcm(map)                  /* do gamma correction */
981 colormap  map;
982 {
983        extern double  pow();
984        register int  i, val;
985
986        for (i = 0; i < 256; i++) {
987                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
988                map[0][i] = map[1][i] = map[2][i] = val;
989        }
990 }
991
992
993 picwritecm(map)                 /* handled elsewhere */
994 colormap  map;
995 {
996 #if 0
997        register int i;
998
999        for (i = 0; i < 256; i++)
1000                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
1001 #endif
1332   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines