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.16 by greg, Thu Oct 22 17:51:05 1992 UTC vs.
Revision 2.65 by schorsch, Fri Jan 2 12:47:01 2004 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  <string.h>
22 + #include  <signal.h>
23   #include  <X11/Xlib.h>
24   #include  <X11/cursorfont.h>
25   #include  <X11/Xutil.h>
26 + #include  <X11/Xatom.h>
27  
28   #include  "color.h"
29 + #include  "tonemap.h"
30   #include  "view.h"
31   #include  "x11raster.h"
32   #include  "random.h"
32 #include  "resolu.h"
33  
34   #define  FONTNAME       "8x13"          /* text font we'll use */
35  
# Line 39 | Line 39 | static char SCCSid[] = "$SunId$ LBL";
39  
40   #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
41  
42 + #define  FIXWEIGHT      20              /* weight to add for fixation points */
43 +
44   #define  ourscreen      DefaultScreen(thedisplay)
45   #define  ourroot        RootWindow(thedisplay,ourscreen)
46  
# Line 47 | Line 49 | static char SCCSid[] = "$SunId$ LBL";
49   #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
50  
51   double  gamcor = 2.2;                   /* gamma correction */
52 + char  *gamstr = NULL;                   /* gamma value override */
53  
54   int  dither = 1;                        /* dither colors? */
55   int  fast = 0;                          /* keep picture in Pixmap? */
# Line 55 | Line 58 | char   *dispname = NULL;               /* our display name */
58  
59   Window  wind = 0;                       /* our output window */
60   unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
58 Font  fontid;                           /* our font */
59
61   int  maxcolors = 0;                     /* maximum colors */
62   int  greyscale = 0;                     /* in grey */
63  
# Line 65 | Line 66 | int  scale = 0;                                /* scalefactor; power of two */
66   int  xoff = 0;                          /* x image offset */
67   int  yoff = 0;                          /* y image offset */
68  
69 + int  parent = 0;                        /* number of children, -1 if child */
70 + int  sequential = 0;                    /* display images in sequence */
71 +
72 + char  *tout = "od";                     /* output of 't' command */
73 + int  tinterv = 0;                       /* interval between mouse reports */
74 +
75 + int  tmflags = TM_F_LINEAR;             /* tone mapping flags */
76 +
77   VIEW  ourview = STDVIEW;                /* image view parameters */
78   int  gotview = 0;                       /* got parameters from file */
79  
80   COLR  *scanline;                        /* scan line buffer */
81 + TMbright  *lscan;                       /* encoded luminance scanline */
82 + BYTE  *cscan;                           /* encoded chroma scanline */
83 + BYTE  *pscan;                           /* compute pixel scanline */
84  
85   RESOLU  inpres;                         /* input resolution and ordering */
86   int  xmax, ymax;                        /* picture dimensions */
87   int  width, height;                     /* window size */
88   char  *fname = NULL;                    /* input file name */
89 < FILE  *fin = stdin;                     /* input file */
89 > FILE  *fin = NULL;                      /* input file */
90   long  *scanpos = NULL;                  /* scan line positions in file */
91   int  cury = 0;                          /* current scan location */
92  
# Line 92 | Line 104 | unsigned char  *ourdata;               /* our image data */
104  
105   struct {
106          int  xmin, ymin, xsiz, ysiz;
107 < }  box = {0, 0, 0, 0};                  /* current box */
107 > }  bbox = {0, 0, 0, 0};                 /* current bbox */
108  
109   char  *geometry = NULL;                 /* geometry specification */
110  
# Line 103 | Line 115 | char  *progname;
115  
116   char  errmsg[128];
117  
118 < extern BYTE  clrtab[256][3];            /* global color map */
118 > BYTE  clrtab[256][3];                   /* global color map */
119  
120   extern long  ftell();
121  
122 < extern char  *malloc(), *calloc();
122 > extern char  *getenv();
123  
124   Display  *thedisplay;
125 + Atom  closedownAtom, wmProtocolsAtom;
126  
127 + int  sigrecv;
128  
129 + void  onsig(int i) { sigrecv++; }
130 +
131 + static gethfunc headline;
132 +
133 +
134   main(argc, argv)
135   int  argc;
136   char  *argv[];
137   {
119        extern char  *getenv();
120        char  *gv;
121        int  headline();
138          int  i;
139 +        int  pid;
140          
141          progname = argv[0];
142 <        if ((gv = getenv("GAMMA")) != NULL)
126 <                gamcor = atof(gv);
142 >        fin = stdin;
143  
144          for (i = 1; i < argc; i++)
145                  if (argv[i][0] == '-')
146                          switch (argv[i][1]) {
147 <                        case 'c':
147 >                        case 'c':                       /* number of colors */
148                                  maxcolors = atoi(argv[++i]);
149                                  break;
150 <                        case 'b':
150 >                        case 'b':                       /* greyscale only */
151                                  greyscale = !greyscale;
152                                  break;
153 <                        case 'm':
153 >                        case 'm':                       /* monochrome */
154 >                                greyscale = 1;
155                                  maxcolors = 2;
156                                  break;
157 <                        case 'd':
157 >                        case 'd':                       /* display or dither */
158                                  if (argv[i][2] == 'i')
159                                          dispname = argv[++i];
160                                  else
161                                          dither = !dither;
162                                  break;
163 <                        case 'f':
163 >                        case 'f':                       /* save pixmap */
164                                  fast = !fast;
165                                  break;
166 <                        case 'e':
167 <                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
166 >                        case 's':                       /* one at a time */
167 >                                sequential = !sequential;
168 >                                break;
169 >                        case 'o':                       /* 't' output */
170 >                                tout = argv[i]+2;
171 >                                break;
172 >                        case 't':                       /* msec interval */
173 >                                tinterv = atoi(argv[++i]);
174 >                                break;
175 >                        case 'e':                       /* exposure comp. */
176 >                                i++;
177 >                                if (argv[i][0] == 'a') {
178 >                                        tmflags = TM_F_CAMERA;
179 >                                        break;
180 >                                }
181 >                                if (argv[i][0] == 'h') {
182 >                                        tmflags = TM_F_HUMAN;
183 >                                        break;
184 >                                }
185 >                                if (argv[i][0] != '+' && argv[i][0] != '-')
186                                          goto userr;
187 <                                scale = atoi(argv[++i]);
187 >                                scale = atoi(argv[i]);
188                                  break;
189 <                        case 'g':
189 >                        case 'g':                       /* gamma comp. */
190                                  if (argv[i][2] == 'e')
191                                          geometry = argv[++i];
192                                  else
193 <                                        gamcor = atof(argv[++i]);
193 >                                        gamstr = argv[++i];
194                                  break;
195                          default:
196                                  goto userr;
# Line 165 | Line 200 | char  *argv[];
200                  else
201                          break;
202  
203 <        if (i == argc-1) {
203 >        if (i > argc)
204 >                goto userr;
205 >        while (i < argc-1) {
206 >                sigrecv = 0;
207 >                signal(SIGCONT, onsig);
208 >                if ((pid=fork()) == 0) {        /* a child for each picture */
209 >                        parent = -1;
210 >                        break;
211 >                }
212 >                if (pid < 0)
213 >                        quiterr("fork failed");
214 >                parent++;
215 >                while (!sigrecv)
216 >                        pause();        /* wait for wake-up call */
217 >                i++;
218 >        }
219 >        if (i < argc) {                 /* open picture file */
220                  fname = argv[i];
221                  fin = fopen(fname, "r");
222 <                if (fin == NULL) {
223 <                        sprintf(errmsg, "cannot open file \"%s\"", fname);
224 <                        quiterr(errmsg);
174 <                }
175 <        } else if (i != argc)
176 <                goto userr;
222 >                if (fin == NULL)
223 >                        quiterr("cannot open picture file");
224 >        }
225                                  /* get header */
226          getheader(fin, headline, NULL);
227                                  /* get picture dimensions */
# Line 187 | Line 235 | char  *argv[];
235          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
236                  quiterr("out of memory");
237  
238 <        init();                 /* get file and open window */
238 >        init(argc, argv);                       /* get file and open window */
239  
240          for ( ; ; )
241                  getevent();             /* main loop */
242   userr:
243          fprintf(stderr,
244 <        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
244 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n",
245                          progname);
246 <        quit(1);
246 >        exit(1);
247   }
248  
249  
250 < headline(s)             /* get relevant info from header */
251 < char  *s;
250 > static int
251 > headline(               /* get relevant info from header */
252 >        char    *s,
253 >        void    *p
254 > )
255   {
256          char  fmt[32];
257  
258          if (isexpos(s))
259                  exposure *= exposval(s);
260 <        else if (isformat(s)) {
210 <                formatval(fmt, s);
260 >        else if (formatval(fmt, s))
261                  wrongformat = strcmp(fmt, COLRFMT);
262 <        } else if (isview(s) && sscanview(&ourview, s) > 0)
262 >        else if (isview(s) && sscanview(&ourview, s) > 0)
263                  gotview++;
264 +        return(0);
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   {
219        XWMHints        ourxwmhints;
272          XSetWindowAttributes    ourwinattr;
273 <        XSizeHints      oursizhints;
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 <        }
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;
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);
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 <                                (unsigned *)&oursizhints.width,
265 <                                (unsigned *)&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;
270 <                        oursizhints.height = ymax;
271 <                        oursizhints.flags |= PSize;
272 <                }
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                  }
282                XSetNormalHints(thedisplay, wind, &oursizhints);
328          }
329 <        ourxwmhints.flags = InputHint|IconPixmapHint;
330 <        ourxwmhints.input = True;
331 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
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 <        XSetWMHints(thedisplay, wind, &ourxwmhints);
365 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
366 <                        |ButtonMotionMask|StructureNotifyMask
367 <                        |KeyPressMask|ExposureMask);
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);
293        return;
294 memerr:
295        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  
# Line 321 | Line 417 | register XVisualInfo   *v1, *v2;
417                                  return(-1);
418                          if (v1->depth == 32 && v2->depth == 24)
419                                  return(1);
420 <                        return(0);
420 >                                        /* go for maximum depth otherwise */
421 >                        return(v2->depth - v1->depth);
422                  }
423                                          /* don't be too greedy */
424                  if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
425                          return(v1->depth - v2->depth);
426                  return(v2->depth - v1->depth);
427          }
428 <                                        /* prefer Pseudo when < 24-bit */
428 >                                        /* prefer Pseudo when < 15-bit */
429          if ((v1->class == TrueColor || v1->class == DirectColor) &&
430 <                        v1->depth < 24)
430 >                        v1->depth < 15)
431                  bad1 = 1;
432          if ((v2->class == TrueColor || v2->class == DirectColor) &&
433 <                        v2->depth < 24)
433 >                        v2->depth < 15)
434                  bad2 = -1;
435          if (bad1 | bad2)
436                  return(bad1+bad2);
# Line 400 | Line 497 | static char  vistype[][12] = {
497          if (viscmp(&xvi[i],&ourvis) > 0)
498                  quiterr("inadequate visuals on this screen");
499                                          /* OK, we'll use it */
500 <        copystruct(&ourvis, &xvi[i]);
500 >        ourvis = xvi[i];
501   #ifdef DEBUG
502          fprintf(stderr, "Selected visual type %s, depth %d\n",
503                          vistype[ourvis.class], ourvis.depth);
# Line 443 | Line 540 | getras()                               /* get raster file */
540                  if (ourras == NULL)
541                          goto fail;
542                  getmono();
543 <        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
544 <                ourdata = (unsigned char *)malloc(4*xmax*ymax);
543 >        } else if ((ourvis.class == TrueColor) | (ourvis.class == DirectColor)) {
544 >                int  datsiz = ourvis.depth>16 ? sizeof(int32) : sizeof(int16);
545 >                ourdata = (unsigned char *)malloc(datsiz*xmax*ymax);
546                  if (ourdata == NULL)
547                          goto fail;
548 <                ourras = make_raster(thedisplay, &ourvis, 32,
549 <                                ourdata, xmax, ymax, 32);
548 >                ourras = make_raster(thedisplay, &ourvis, datsiz*8,
549 >                                ourdata, xmax, ymax, datsiz*8);
550                  if (ourras == NULL)
551                          goto fail;
552                  getfull();
# Line 460 | Line 558 | getras()                               /* get raster file */
558                                  xmax, ymax, 8);
559                  if (ourras == NULL)
560                          goto fail;
561 <                if (greyscale | ourvis.class == StaticGray)
561 >                if (greyscale)
562                          getgrey();
563                  else
564                          getmapped();
# Line 475 | Line 573 | fail:
573  
574   getevent()                              /* process the next event */
575   {
576 <        union {
479 <                XEvent  u;
480 <                XConfigureEvent  c;
481 <                XExposeEvent  e;
482 <                XButtonPressedEvent  b;
483 <                XKeyPressedEvent  k;
484 <        } e;
576 >        XEvent xev;
577  
578 <        XNextEvent(thedisplay, &e.u);
579 <        switch (e.u.type) {
578 >        XNextEvent(thedisplay, &xev);
579 >        switch ((int)xev.type) {
580          case KeyPress:
581 <                docom(&e.k);
581 >                docom(&xev.xkey);
582                  break;
583          case ConfigureNotify:
584 <                width = e.c.width;
585 <                height = e.c.height;
584 >                width = xev.xconfigure.width;
585 >                height = xev.xconfigure.height;
586                  break;
587          case MapNotify:
588                  map_rcolors(ourras, wind);
589                  if (fast)
590                          make_rpixmap(ourras, wind);
591 +                if ((!sequential) & (parent < 0) & (sigrecv == 0)) {
592 +                        kill(getppid(), SIGCONT);
593 +                        sigrecv--;
594 +                }
595                  break;
596          case UnmapNotify:
597                  if (!fast)
598                          unmap_rcolors(ourras);
599                  break;
600          case Expose:
601 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
601 >                redraw(xev.xexpose.x, xev.xexpose.y,
602 >                                xev.xexpose.width, xev.xexpose.height);
603                  break;
604          case ButtonPress:
605 <                if (e.b.state & (ShiftMask|ControlMask))
606 <                        moveimage(&e.b);
605 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
606 >                        moveimage(&xev.xbutton);
607                  else
608 <                        getbox(&e.b);
608 >                        switch (xev.xbutton.button) {
609 >                        case Button1:
610 >                                getbox(&xev.xbutton);
611 >                                break;
612 >                        case Button2:
613 >                                traceray(xev.xbutton.x, xev.xbutton.y);
614 >                                break;
615 >                        case Button3:
616 >                                trackrays(&xev.xbutton);
617 >                                break;
618 >                        }
619                  break;
620 +        case ClientMessage:
621 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
622 +                                (xev.xclient.data.l[0] == closedownAtom))
623 +                        quiterr(NULL);
624 +                break;
625          }
626   }
627  
628  
629 < docom(ekey)                                     /* execute command */
629 > traceray(xpos, ypos)                    /* print requested pixel data */
630 > int  xpos, ypos;
631 > {
632 >        RREAL  hv[2];
633 >        FVECT  rorg, rdir;
634 >        COLOR  cval;
635 >        register char  *cp;
636 >
637 >        bbox.xmin = xpos; bbox.xsiz = 1;
638 >        bbox.ymin = ypos; bbox.ysiz = 1;
639 >        avgbox(cval);
640 >        scalecolor(cval, 1./exposure);
641 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
642 >        if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
643 >                rorg[0] = rorg[1] = rorg[2] =
644 >                rdir[0] = rdir[1] = rdir[2] = 0.;
645 >
646 >        for (cp = tout; *cp; cp++)      /* print what they asked for */
647 >                switch (*cp) {
648 >                case 'o':                       /* origin */
649 >                        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
650 >                        break;
651 >                case 'd':                       /* direction */
652 >                        printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
653 >                        break;
654 >                case 'v':                       /* radiance value */
655 >                        printf("%e %e %e ", colval(cval,RED),
656 >                                        colval(cval,GRN), colval(cval,BLU));
657 >                        break;
658 >                case 'l':                       /* luminance */
659 >                        printf("%e ", luminance(cval));
660 >                        break;
661 >                case 'p':                       /* pixel position */
662 >                        printf("%d %d ", (int)(hv[0]*inpres.xr),
663 >                                        (int)(hv[1]*inpres.yr));
664 >                        break;
665 >                }
666 >        putchar('\n');
667 >        fflush(stdout);
668 >        return(0);
669 > }
670 >
671 >
672 > docom(ekey)                             /* execute command */
673   XKeyPressedEvent  *ekey;
674   {
675          char  buf[80];
# Line 522 | Line 677 | XKeyPressedEvent  *ekey;
677          XColor  cvx;
678          int  com, n;
679          double  comp;
680 <        FLOAT  hv[2];
526 <        FVECT  rorg, rdir;
680 >        RREAL  hv[2];
681  
682          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
683          if (n == 0)
# Line 531 | Line 685 | XKeyPressedEvent  *ekey;
685          com = buf[0];
686          switch (com) {                  /* interpret command */
687          case 'q':
688 +        case 'Q':
689          case CTRL('D'):                         /* quit */
690 <                quit(0);
690 >                quiterr(NULL);
691          case '\n':
692          case '\r':
693          case 'l':
694          case 'c':                               /* value */
695 <                if (avgbox(cval) == -1)
695 >                if (!avgbox(cval))
696                          return(-1);
697                  switch (com) {
698                  case '\n':
# Line 545 | Line 700 | XKeyPressedEvent  *ekey;
700                          sprintf(buf, "%.3f", intens(cval)/exposure);
701                          break;
702                  case 'l':                               /* luminance */
703 <                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
703 >                        sprintf(buf, "%.1fL", luminance(cval)/exposure);
704                          break;
705                  case 'c':                               /* color */
706                          comp = pow(2.0, (double)scale);
# Line 556 | Line 711 | XKeyPressedEvent  *ekey;
711                          break;
712                  }
713                  XDrawImageString(thedisplay, wind, ourgc,
714 <                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
714 >                                bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
715                  return(0);
716          case 'i':                               /* identify (contour) */
717                  if (ourras->pixels == NULL)
# Line 578 | Line 733 | XKeyPressedEvent  *ekey;
733                                          buf, strlen(buf));
734                  return(0);
735          case 't':                               /* trace */
736 <                if (!gotview) {
737 <                        XBell(thedisplay, 0);
736 >                return(traceray(ekey->x, ekey->y));
737 >        case 'a':                               /* auto exposure */
738 >                if (fname == NULL)
739                          return(-1);
740 <                }
741 <                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
742 <                if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
740 >                tmflags = TM_F_CAMERA;
741 >                strcpy(buf, "auto exposure...");
742 >                goto remap;
743 >        case 'h':                               /* human response */
744 >                if (fname == NULL)
745                          return(-1);
746 <                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
747 <                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
748 <                fflush(stdout);
591 <                return(0);
746 >                tmflags = TM_F_HUMAN;
747 >                strcpy(buf, "human exposure...");
748 >                goto remap;
749          case '=':                               /* adjust exposure */
750 <                if (avgbox(cval) == -1)
750 >        case '@':                               /* adaptation level */
751 >                if (!avgbox(cval))
752                          return(-1);
753 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
754 <                if (n == 0)
755 <                        return(0);
756 <                scale_rcolors(ourras, pow(2.0, (double)n));
753 >                comp = bright(cval);
754 >                if (comp < 1e-20) {
755 >                        XBell(thedisplay, 0);
756 >                        return(-1);
757 >                }
758 >                if (com == '@')
759 >                        comp = 106./exposure/
760 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
761 >                else
762 >                        comp = .5/comp;
763 >                comp = log(comp)/.69315 - scale;
764 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
765 >                if (tmflags != TM_F_LINEAR)
766 >                        tmflags = TM_F_LINEAR;  /* turn off tone mapping */
767 >                else {
768 >                        if (n == 0)             /* else check if any change */
769 >                                return(0);
770 >                        scale_rcolors(ourras, pow(2.0, (double)n));
771 >                }
772                  scale += n;
773                  sprintf(buf, "%+d", scale);
774 +        remap:
775                  XDrawImageString(thedisplay, wind, ourgc,
776 <                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
776 >                                bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
777                  XFlush(thedisplay);
778 <                free(ourdata);
778 >                /* free(ourdata);       This is done in XDestroyImage()! */
779                  free_raster(ourras);
780                  getras();
781          /* fall through */
# Line 614 | Line 788 | XKeyPressedEvent  *ekey;
788                          make_rpixmap(ourras, wind);
789                  redraw(0, 0, width, height);
790                  return(0);
791 +        case 'f':                               /* turn on fast redraw */
792 +                fast = 1;
793 +                make_rpixmap(ourras, wind);
794 +                return(0);
795 +        case 'F':                               /* turn off fast redraw */
796 +                fast = 0;
797 +                free_rpixmap(ourras);
798 +                return(0);
799 +        case '0':                               /* recenter origin */
800 +                if ((xoff == 0) & (yoff == 0))
801 +                        return(0);
802 +                xoff = yoff = 0;
803 +                XClearWindow(thedisplay, wind);
804 +                redraw(0, 0, width, height);
805 +                return(0);
806          case ' ':                               /* clear */
807 <                redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
807 >                redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz);
808                  return(0);
809          default:
810                  XBell(thedisplay, 0);
# Line 627 | Line 816 | XKeyPressedEvent  *ekey;
816   moveimage(ebut)                         /* shift the image */
817   XButtonPressedEvent  *ebut;
818   {
819 <        union {
631 <                XEvent  u;
632 <                XButtonReleasedEvent  b;
633 <                XPointerMovedEvent  m;
634 <        }  e;
819 >        XEvent  e;
820          int     mxo, myo;
821  
822 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
823 <        while (e.u.type == MotionNotify) {
824 <                mxo = e.m.x;
825 <                myo = e.m.y;
822 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
823 >        while (e.type == MotionNotify) {
824 >                mxo = e.xmotion.x;
825 >                myo = e.xmotion.y;
826                  revline(ebut->x, ebut->y, mxo, myo);
827                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
828                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
829 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
829 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
830                  revline(ebut->x, ebut->y, mxo, myo);
831                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
832                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
833          }
834 <        xoff += e.b.x - ebut->x;
835 <        yoff += e.b.y - ebut->y;
834 >        xoff += e.xbutton.x - ebut->x;
835 >        yoff += e.xbutton.y - ebut->y;
836          XClearWindow(thedisplay, wind);
837          redraw(0, 0, width, height);
838   }
839  
840  
841 < getbox(ebut)                            /* get new box */
841 > getbox(ebut)                            /* get new bbox */
842   XButtonPressedEvent  *ebut;
843   {
844 <        union {
660 <                XEvent  u;
661 <                XButtonReleasedEvent  b;
662 <                XPointerMovedEvent  m;
663 <        }  e;
844 >        XEvent  e;
845  
846 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
847 <        while (e.u.type == MotionNotify) {
848 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
849 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
850 <                revbox(ebut->x, ebut->y, box.xmin, box.ymin);
846 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
847 >        while (e.type == MotionNotify) {
848 >                revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y);
849 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
850 >                revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin);
851          }
852 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
853 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
854 <        if (box.xmin > ebut->x) {
855 <                box.xsiz = box.xmin - ebut->x + 1;
856 <                box.xmin = ebut->x;
852 >        bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
853 >        bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
854 >        if (bbox.xmin > ebut->x) {
855 >                bbox.xsiz = bbox.xmin - ebut->x + 1;
856 >                bbox.xmin = ebut->x;
857          } else {
858 <                box.xsiz = ebut->x - box.xmin + 1;
858 >                bbox.xsiz = ebut->x - bbox.xmin + 1;
859          }
860 <        if (box.ymin > ebut->y) {
861 <                box.ysiz = box.ymin - ebut->y + 1;
862 <                box.ymin = ebut->y;
860 >        if (bbox.ymin > ebut->y) {
861 >                bbox.ysiz = bbox.ymin - ebut->y + 1;
862 >                bbox.ymin = ebut->y;
863          } else {
864 <                box.ysiz = ebut->y - box.ymin + 1;
864 >                bbox.ysiz = ebut->y - bbox.ymin + 1;
865          }
866   }
867  
868  
869 < revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
869 > trackrays(ebut)                         /* trace rays as mouse moves */
870 > XButtonPressedEvent  *ebut;
871 > {
872 >        XEvent  e;
873 >        unsigned long   lastrept;
874 >
875 >        traceray(ebut->x, ebut->y);
876 >        lastrept = ebut->time;
877 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
878 >        while (e.type == MotionNotify) {
879 >                if (e.xmotion.time >= lastrept + tinterv) {
880 >                        traceray(e.xmotion.x, e.xmotion.y);
881 >                        lastrept = e.xmotion.time;
882 >                }
883 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
884 >        }
885 > }
886 >
887 >
888 > revbox(x0, y0, x1, y1)                  /* draw bbox with reversed lines */
889   int  x0, y0, x1, y1;
890   {
891          revline(x0, y0, x1, y0);
# Line 695 | Line 895 | int  x0, y0, x1, y1;
895   }
896  
897  
898 < avgbox(clr)                             /* average color over current box */
899 < COLOR  clr;
898 > void
899 > colavg(scn, n, cavg)
900 > register COLR   *scn;
901 > register int    n;
902 > COLOR   cavg;
903   {
904 <        static COLOR  lc;
905 <        static int  ll, lr, lt, lb;
904 >        COLOR   col;
905 >
906 >        while (n--) {
907 >                colr_color(col, *scn++);
908 >                addcolor(cavg, col);
909 >        }
910 > }
911 >
912 >
913 > int
914 > avgbox(cavg)                            /* average color over current bbox */
915 > COLOR   cavg;
916 > {
917 >        double  d;
918 >        register int    rval;
919 >
920 >        setcolor(cavg, 0., 0., 0.);
921 >        rval = dobox(colavg, (char *)cavg);
922 >        if (rval > 0) {
923 >                d = 1./rval;
924 >                scalecolor(cavg, d);
925 >        }
926 >        return(rval);
927 > }
928 >
929 >
930 > int
931 > dobox(f, p)                             /* run function over bbox */
932 > void    (*f)();                 /* function to call for each subscan */
933 > char    *p;                     /* pointer to private data */
934 > {
935          int  left, right, top, bottom;
936          int  y;
705        double  d;
706        COLOR  ctmp;
707        register int  x;
937  
938 <        setcolor(clr, 0.0, 0.0, 0.0);
939 <        left = box.xmin - xoff;
711 <        right = left + box.xsiz;
938 >        left = bbox.xmin - xoff;
939 >        right = left + bbox.xsiz;
940          if (left < 0)
941                  left = 0;
942          if (right > xmax)
943                  right = xmax;
944          if (left >= right)
945 <                return(-1);
946 <        top = box.ymin - yoff;
947 <        bottom = top + box.ysiz;
945 >                return(0);
946 >        top = bbox.ymin - yoff;
947 >        bottom = top + bbox.ysiz;
948          if (top < 0)
949                  top = 0;
950          if (bottom > ymax)
951                  bottom = ymax;
952          if (top >= bottom)
725                return(-1);
726        if (left == ll && right == lr && top == lt && bottom == lb) {
727                copycolor(clr, lc);
953                  return(0);
729        }
954          for (y = top; y < bottom; y++) {
955                  if (getscan(y) == -1)
956                          return(-1);
957 <                for (x = left; x < right; x++) {
958 <                        colr_color(ctmp, scanline[x]);
959 <                        addcolor(clr, ctmp);
957 >                (*f)(scanline+left, right-left, p);
958 >        }
959 >        return((right-left)*(bottom-top));
960 > }
961 >
962 >
963 > void
964 > addfix(scn, n)                  /* add fixation points to histogram */
965 > COLR    *scn;
966 > int     n;
967 > {
968 >        if (tmCvColrs(lscan, TM_NOCHROM, scn, n))
969 >                goto tmerr;
970 >        if (tmAddHisto(lscan, n, FIXWEIGHT))
971 >                goto tmerr;
972 >        return;
973 > tmerr:
974 >        quiterr("tone mapping error");
975 > }
976 >
977 >
978 > make_tonemap()                  /* initialize tone mapping */
979 > {
980 >        int  flags, y;
981 >
982 >        if (tmflags != TM_F_LINEAR && fname == NULL) {
983 >                fprintf(stderr, "%s: cannot adjust tone of standard input\n",
984 >                                progname);
985 >                tmflags = TM_F_LINEAR;
986 >        }
987 >        if (tmflags == TM_F_LINEAR) {   /* linear with clamping */
988 >                setcolrcor(pow, 1.0/gamcor);
989 >                return;
990 >        }
991 >        flags = tmflags;                /* histogram adjustment */
992 >        if (greyscale) flags |= TM_F_BW;
993 >        if (tmTop != NULL) {            /* reuse old histogram if one */
994 >                tmDone(tmTop);
995 >                tmTop->flags = flags;
996 >        } else {                        /* else initialize */
997 >                if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
998 >                        goto memerr;
999 >                if (greyscale) {
1000 >                        cscan = TM_NOCHROM;
1001 >                        if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
1002 >                                goto memerr;
1003 >                } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax))
1004 >                                == NULL)
1005 >                        goto memerr;
1006 >                                                /* initialize tm library */
1007 >                if (tmInit(flags, stdprims, gamcor) == NULL)
1008 >                        goto memerr;
1009 >                if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
1010 >                        goto tmerr;
1011 >                                                /* compute picture histogram */
1012 >                for (y = 0; y < ymax; y++) {
1013 >                        getscan(y);
1014 >                        if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
1015 >                                goto tmerr;
1016 >                        if (tmAddHisto(lscan, xmax, 1))
1017 >                                goto tmerr;
1018                  }
1019          }
1020 <        d = 1.0/((right-left)*(bottom-top));
1021 <        scalecolor(clr, d);
1022 <        ll = left; lr = right; lt = top; lb = bottom;
1023 <        copycolor(lc, clr);
1024 <        return(0);
1020 >        tmDup();                        /* add fixations to duplicate map */
1021 >        dobox(addfix, NULL);
1022 >                                        /* (re)compute tone mapping */
1023 >        if (tmComputeMapping(gamcor, 0., 0.))
1024 >                goto tmerr;
1025 >        return;
1026 > memerr:
1027 >        quiterr("out of memory in make_tonemap");
1028 > tmerr:
1029 >        quiterr("tone mapping error");
1030   }
1031  
1032  
1033 + tmap_colrs(scn, len)            /* apply tone mapping to scanline */
1034 + register COLR  *scn;
1035 + int  len;
1036 + {
1037 +        register BYTE  *ps;
1038 +
1039 +        if (tmflags == TM_F_LINEAR) {
1040 +                if (scale)
1041 +                        shiftcolrs(scn, len, scale);
1042 +                colrs_gambs(scn, len);
1043 +                return;
1044 +        }
1045 +        if (len > xmax)
1046 +                quiterr("code error 1 in tmap_colrs");
1047 +        if (tmCvColrs(lscan, cscan, scn, len))
1048 +                goto tmerr;
1049 +        if (tmMapPixels(pscan, lscan, cscan, len))
1050 +                goto tmerr;
1051 +        ps = pscan;
1052 +        if (greyscale)
1053 +                while (len--) {
1054 +                        scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1055 +                        scn[0][EXP] = COLXS;
1056 +                        scn++;
1057 +                }
1058 +        else
1059 +                while (len--) {
1060 +                        scn[0][RED] = *ps++;
1061 +                        scn[0][GRN] = *ps++;
1062 +                        scn[0][BLU] = *ps++;
1063 +                        scn[0][EXP] = COLXS;
1064 +                        scn++;
1065 +                }
1066 +        return;
1067 + tmerr:
1068 +        quiterr("tone mapping error");
1069 + }
1070 +
1071 +
1072   getmono()                       /* get monochrome data */
1073   {
1074          register unsigned char  *dp;
# Line 771 | Line 1097 | getmono()                      /* get monochrome data */
1097                          cerr[x] = err + errp;
1098                  }
1099          }
1100 <        free((char *)cerr);
1100 >        free((void *)cerr);
1101   }
1102  
1103  
# Line 782 | Line 1108 | COLR  *scan;
1108          static short  cerr[ICONSIZ];
1109          static int  ynext;
1110          static char  *dp;
1111 <        double  sf;
786 <        COLOR  col;
1111 >        COLR  clr;
1112          register int  err;
1113          register int    x, ti;
1114          int  errp;
# Line 808 | Line 1133 | COLR  *scan;
1133          }
1134          if (y < ynext*ymax/iconheight)  /* skip this one */
1135                  return;
811        sf = pow(2.0, (double)(scale+8));
1136          err = 0;
1137          for (x = 0; x < iconwidth; x++) {
1138                  if (!(x&7))
1139                          *++dp = 0;
1140                  errp = err;
1141                  ti = x*xmax/iconwidth;
1142 <                colr_color(col, scan[ti]);
1143 <                ti = sf*bright(col);
1144 <                if (ti > 255) ti = 255;
821 <                err += ti + cerr[x];
1142 >                copycolr(clr, scan[ti]);
1143 >                normcolrs(&clr, 1, scale);
1144 >                err += normbright(clr) + cerr[x];
1145                  if (err > 127)
1146                          err -= 255;
1147                  else
# Line 833 | Line 1156 | COLR  *scan;
1156   getfull()                       /* get full (24-bit) data */
1157   {
1158          int     y;
1159 <        register unsigned long  *dp;
1159 >        register uint32 *dp;
1160 >        register uint16 *dph;
1161          register int    x;
1162 <                                        /* set gamma correction */
1163 <        setcolrgam(gamcor);
1162 >                                        /* initialize tone mapping */
1163 >        make_tonemap();
1164                                          /* read and convert file */
1165 <        dp = (unsigned long *)ourdata;
1165 >        dp = (uint32 *)ourdata;
1166 >        dph = (uint16 *)ourdata;
1167          for (y = 0; y < ymax; y++) {
1168                  getscan(y);
1169                  add2icon(y, scanline);
1170 <                if (scale)
1171 <                        shiftcolrs(scanline, xmax, scale);
1172 <                colrs_gambs(scanline, xmax);
848 <                if (ourras->image->blue_mask & 1)
1170 >                tmap_colrs(scanline, xmax);
1171 >                switch (ourras->image->blue_mask) {
1172 >                case 0xff:              /* 24-bit RGB */
1173                          for (x = 0; x < xmax; x++)
1174 <                                *dp++ = scanline[x][RED] << 16 |
1175 <                                        scanline[x][GRN] << 8 |
1176 <                                        scanline[x][BLU] ;
1177 <                else
1174 >                                *dp++ = (uint32)scanline[x][RED] << 16 |
1175 >                                        (uint32)scanline[x][GRN] << 8 |
1176 >                                        (uint32)scanline[x][BLU] ;
1177 >                        break;
1178 >                case 0xff0000:          /* 24-bit BGR */
1179                          for (x = 0; x < xmax; x++)
1180 <                                *dp++ = scanline[x][RED] |
1181 <                                        scanline[x][GRN] << 8 |
1182 <                                        scanline[x][BLU] << 16 ;
1180 >                                *dp++ = (uint32)scanline[x][RED] |
1181 >                                        (uint32)scanline[x][GRN] << 8 |
1182 >                                        (uint32)scanline[x][BLU] << 16 ;
1183 >                        break;
1184 > #if 0
1185 >                case 0x1f:              /* 15-bit RGB */
1186 >                        for (x = 0; x < xmax; x++)
1187 >                                *dph++ =        (scanline[x][RED] << 7 & 0x7c00) |
1188 >                                        (scanline[x][GRN] << 2 & 0x3e0) |
1189 >                                        (unsigned)scanline[x][BLU] >> 3 ;
1190 >                        break;
1191 >                case 0x7c00:            /* 15-bit BGR */
1192 >                        for (x = 0; x < xmax; x++)
1193 >                                *dph++ =        (unsigned)scanline[x][RED] >> 3 |
1194 >                                        (scanline[x][GRN] << 2 & 0x3e0) |
1195 >                                        (scanline[x][BLU] << 7 & 0x7c00) ;
1196 >                        break;
1197 > #endif
1198 >                default:                /* unknown */
1199 >                        if (ourvis.depth > 16)
1200 >                                for (x = 0; x < xmax; x++) {
1201 >                                        *dp = ourras->image->red_mask &
1202 >                                                ourras->image->red_mask*scanline[x][RED]/255;
1203 >                                        *dp |= ourras->image->green_mask &
1204 >                                                ourras->image->green_mask*scanline[x][GRN]/255;
1205 >                                        *dp++ |= ourras->image->blue_mask &
1206 >                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1207 >                                }
1208 >                        else
1209 >                                for (x = 0; x < xmax; x++) {
1210 >                                        *dph = ourras->image->red_mask &
1211 >                                                ourras->image->red_mask*scanline[x][RED]/255;
1212 >                                        *dph |= ourras->image->green_mask &
1213 >                                                ourras->image->green_mask*scanline[x][GRN]/255;
1214 >                                        *dph++ |= ourras->image->blue_mask &
1215 >                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1216 >                                }
1217 >                        break;
1218 >                }
1219          }
1220   }
1221  
# Line 864 | Line 1225 | getgrey()                      /* get greyscale data */
1225          int     y;
1226          register unsigned char  *dp;
1227          register int    x;
1228 <                                        /* set gamma correction */
1229 <        setcolrgam(gamcor);
1228 >                                        /* initialize tone mapping */
1229 >        make_tonemap();
1230                                          /* read and convert file */
1231          dp = ourdata;
1232          for (y = 0; y < ymax; y++) {
1233                  getscan(y);
1234                  add2icon(y, scanline);
1235 <                if (scale)
875 <                        shiftcolrs(scanline, xmax, scale);
876 <                colrs_gambs(scanline, xmax);
1235 >                tmap_colrs(scanline, xmax);
1236                  if (maxcolors < 256)
1237                          for (x = 0; x < xmax; x++)
1238 <                                *dp++ = ((long)normbright(scanline[x]) *
1239 <                                        maxcolors + 128) >> 8;
1238 >                                *dp++ = ((int32)scanline[x][GRN] *
1239 >                                        maxcolors + maxcolors/2) >> 8;
1240                  else
1241                          for (x = 0; x < xmax; x++)
1242 <                                *dp++ = normbright(scanline[x]);
1242 >                                *dp++ = scanline[x][GRN];
1243          }
1244          for (x = 0; x < maxcolors; x++)
1245                  clrtab[x][RED] = clrtab[x][GRN] =
1246 <                        clrtab[x][BLU] = ((long)x*256+maxcolors/2)/maxcolors;
1246 >                        clrtab[x][BLU] = ((int32)x*256 + 128)/maxcolors;
1247   }
1248  
1249  
1250   getmapped()                     /* get color-mapped data */
1251   {
1252          int     y;
1253 <                                        /* set gamma correction */
1254 <        setcolrgam(gamcor);
1253 >                                        /* make sure we can do it first */
1254 >        if (fname == NULL)
1255 >                quiterr("cannot map colors from standard input");
1256 >                                        /* initialize tone mapping */
1257 >        make_tonemap();
1258                                          /* make histogram */
1259 <        new_histo();
1259 >        if (new_histo((int32)xmax*ymax) == -1)
1260 >                quiterr("cannot initialize histogram");
1261          for (y = 0; y < ymax; y++) {
1262                  if (getscan(y) < 0)
1263 <                        quiterr("seek error in getmapped");
1263 >                        break;
1264                  add2icon(y, scanline);
1265 <                if (scale)
903 <                        shiftcolrs(scanline, xmax, scale);
904 <                colrs_gambs(scanline, xmax);
1265 >                tmap_colrs(scanline, xmax);
1266                  cnt_colrs(scanline, xmax);
1267          }
1268                                          /* map pixels */
1269          if (!new_clrtab(maxcolors))
1270                  quiterr("cannot create color map");
1271          for (y = 0; y < ymax; y++) {
1272 <                if (getscan(y) < 0)
1273 <                        quiterr("seek error in getmapped");
913 <                if (scale)
914 <                        shiftcolrs(scanline, xmax, scale);
915 <                colrs_gambs(scanline, xmax);
1272 >                getscan(y);
1273 >                tmap_colrs(scanline, xmax);
1274                  if (dither)
1275                          dith_colrs(ourdata+y*xmax, scanline, xmax);
1276                  else
# Line 952 | Line 1310 | double sf;
1310   getscan(y)
1311   int  y;
1312   {
1313 +        static int  trunced = -1;               /* truncated file? */
1314 + skipit:
1315 +        if (trunced >= 0 && y >= trunced) {
1316 +                memset(scanline, '\0', xmax*sizeof(COLR));
1317 +                return(-1);
1318 +        }
1319          if (y != cury) {
1320                  if (scanpos == NULL || scanpos[y] == -1)
1321                          return(-1);
# Line 961 | Line 1325 | int  y;
1325          } else if (scanpos != NULL && scanpos[y] == -1)
1326                  scanpos[y] = ftell(fin);
1327  
1328 <        if (freadcolrs(scanline, xmax, fin) < 0)
1329 <                quiterr("read error");
1330 <
1328 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1329 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1330 >                                progname, fname==NULL?"<stdin>":fname);
1331 >                trunced = y;
1332 >                goto skipit;
1333 >        }
1334          cury++;
1335          return(0);
1336   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines