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.22 by greg, Tue Mar 2 10:54:08 1993 UTC vs.
Revision 2.67 by greg, Sat Apr 10 03:02:20 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  <unistd.h>
24 + #include  <sys/types.h>
25 + #include  <sys/wait.h>
26   #include  <X11/Xlib.h>
27   #include  <X11/cursorfont.h>
28   #include  <X11/Xutil.h>
29 + #include  <X11/Xatom.h>
30  
31   #include  "color.h"
32 + #include  "tonemap.h"
33 + #include  "clrtab.h"
34   #include  "view.h"
35   #include  "x11raster.h"
36   #include  "random.h"
32 #include  "resolu.h"
37  
38   #define  FONTNAME       "8x13"          /* text font we'll use */
39  
# Line 39 | Line 43 | static char SCCSid[] = "$SunId$ LBL";
43  
44   #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
45  
46 + #define  FIXWEIGHT      20              /* weight to add for fixation points */
47 +
48   #define  ourscreen      DefaultScreen(thedisplay)
49   #define  ourroot        RootWindow(thedisplay,ourscreen)
50  
# Line 47 | Line 53 | static char SCCSid[] = "$SunId$ LBL";
53   #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
54  
55   double  gamcor = 2.2;                   /* gamma correction */
56 + char  *gamstr = NULL;                   /* gamma value override */
57  
58   int  dither = 1;                        /* dither colors? */
59   int  fast = 0;                          /* keep picture in Pixmap? */
# Line 55 | Line 62 | char   *dispname = NULL;               /* our display name */
62  
63   Window  wind = 0;                       /* our output window */
64   unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
58 Font  fontid;                           /* our font */
59
65   int  maxcolors = 0;                     /* maximum colors */
66   int  greyscale = 0;                     /* in grey */
67  
# Line 65 | Line 70 | int  scale = 0;                                /* scalefactor; power of two */
70   int  xoff = 0;                          /* x image offset */
71   int  yoff = 0;                          /* y image offset */
72  
73 + int  parent = 0;                        /* number of children, -1 if child */
74 + int  sequential = 0;                    /* display images in sequence */
75 +
76 + char  *tout = "od";                     /* output of 't' command */
77 + int  tinterv = 0;                       /* interval between mouse reports */
78 +
79 + int  tmflags = TM_F_LINEAR;             /* tone mapping flags */
80 +
81   VIEW  ourview = STDVIEW;                /* image view parameters */
82   int  gotview = 0;                       /* got parameters from file */
83  
84   COLR  *scanline;                        /* scan line buffer */
85 + TMbright  *lscan;                       /* encoded luminance scanline */
86 + BYTE  *cscan;                           /* encoded chroma scanline */
87 + BYTE  *pscan;                           /* compute pixel scanline */
88  
89   RESOLU  inpres;                         /* input resolution and ordering */
90   int  xmax, ymax;                        /* picture dimensions */
91   int  width, height;                     /* window size */
92   char  *fname = NULL;                    /* input file name */
93 < FILE  *fin = stdin;                     /* input file */
93 > FILE  *fin = NULL;                      /* input file */
94   long  *scanpos = NULL;                  /* scan line positions in file */
95   int  cury = 0;                          /* current scan location */
96  
# Line 92 | Line 108 | unsigned char  *ourdata;               /* our image data */
108  
109   struct {
110          int  xmin, ymin, xsiz, ysiz;
111 < }  box = {0, 0, 0, 0};                  /* current box */
111 > }  bbox = {0, 0, 0, 0};                 /* current bbox */
112  
113   char  *geometry = NULL;                 /* geometry specification */
114  
# Line 103 | Line 119 | char  *progname;
119  
120   char  errmsg[128];
121  
122 < extern BYTE  clrtab[256][3];            /* global color map */
122 > BYTE  clrtab[256][3];                   /* global color map */
123  
108 extern long  ftell();
124  
110 extern char  *malloc(), *calloc();
111
125   Display  *thedisplay;
126 + Atom  closedownAtom, wmProtocolsAtom;
127  
128 + int  sigrecv;
129  
130 < main(argc, argv)
131 < int  argc;
132 < char  *argv[];
130 > void  onsig(int i) { sigrecv++; }
131 >
132 > typedef void doboxf_t(COLR *scn, int n, void *);
133 >
134 > static gethfunc headline;
135 > static void init(int argc, char **argv);
136 > static void quiterr(char *err);
137 > static int viscmp(XVisualInfo *v1, XVisualInfo *v2);
138 > static void getbestvis(void);
139 > static void getras(void);
140 > static void getevent(void);
141 > static int traceray(int xpos, int ypos);
142 > static int docom(XKeyPressedEvent *ekey);
143 > static void moveimage(XButtonPressedEvent *ebut);
144 > static void getbox(XButtonPressedEvent *ebut);
145 > static void trackrays(XButtonPressedEvent *ebut);
146 > static void revbox(int x0, int y0, int x1, int y1);
147 > static doboxf_t colavg;
148 > static doboxf_t addfix;
149 > static int avgbox(COLOR cavg);
150 > static int dobox(doboxf_t *f, void *p);
151 > static void make_tonemap(void);
152 > static void tmap_colrs(COLR *scn, int len);
153 > static void getmono(void);
154 > static void add2icon(int y, COLR *scan);
155 > static void getfull(void);
156 > static void getgrey(void);
157 > static void getmapped(void);
158 > static void scale_rcolors(XRASTER *xr, double sf);
159 > static int getscan(int y);
160 >
161 >
162 > int
163 > main(int  argc, char  *argv[])
164   {
119        extern char  *getenv();
120        char  *gv;
121        int  headline();
165          int  i;
166 +        int  pid;
167          
168          progname = argv[0];
169 <        if ((gv = getenv("GAMMA")) != NULL)
126 <                gamcor = atof(gv);
169 >        fin = stdin;
170  
171          for (i = 1; i < argc; i++)
172                  if (argv[i][0] == '-')
173                          switch (argv[i][1]) {
174 <                        case 'c':
174 >                        case 'c':                       /* number of colors */
175                                  maxcolors = atoi(argv[++i]);
176                                  break;
177 <                        case 'b':
177 >                        case 'b':                       /* greyscale only */
178                                  greyscale = !greyscale;
179                                  break;
180 <                        case 'm':
180 >                        case 'm':                       /* monochrome */
181 >                                greyscale = 1;
182                                  maxcolors = 2;
183                                  break;
184 <                        case 'd':
184 >                        case 'd':                       /* display or dither */
185                                  if (argv[i][2] == 'i')
186                                          dispname = argv[++i];
187                                  else
188                                          dither = !dither;
189                                  break;
190 <                        case 'f':
190 >                        case 'f':                       /* save pixmap */
191                                  fast = !fast;
192                                  break;
193 <                        case 'e':
194 <                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
193 >                        case 's':                       /* one at a time */
194 >                                sequential = !sequential;
195 >                                break;
196 >                        case 'o':                       /* 't' output */
197 >                                tout = argv[i]+2;
198 >                                break;
199 >                        case 't':                       /* msec interval */
200 >                                tinterv = atoi(argv[++i]);
201 >                                break;
202 >                        case 'e':                       /* exposure comp. */
203 >                                i++;
204 >                                if (argv[i][0] == 'a') {
205 >                                        tmflags = TM_F_CAMERA;
206 >                                        break;
207 >                                }
208 >                                if (argv[i][0] == 'h') {
209 >                                        tmflags = TM_F_HUMAN;
210 >                                        break;
211 >                                }
212 >                                if (argv[i][0] != '+' && argv[i][0] != '-')
213                                          goto userr;
214 <                                scale = atoi(argv[++i]);
214 >                                scale = atoi(argv[i]);
215                                  break;
216 <                        case 'g':
216 >                        case 'g':                       /* gamma comp. */
217                                  if (argv[i][2] == 'e')
218                                          geometry = argv[++i];
219                                  else
220 <                                        gamcor = atof(argv[++i]);
220 >                                        gamstr = argv[++i];
221                                  break;
222                          default:
223                                  goto userr;
# Line 165 | Line 227 | char  *argv[];
227                  else
228                          break;
229  
230 <        if (i == argc-1) {
230 >        if (i > argc)
231 >                goto userr;
232 >        while (i < argc-1) {
233 >                sigrecv = 0;
234 >                signal(SIGCONT, onsig);
235 >                if ((pid=fork()) == 0) {        /* a child for each picture */
236 >                        parent = -1;
237 >                        break;
238 >                }
239 >                if (pid < 0)
240 >                        quiterr("fork failed");
241 >                parent++;
242 >                while (!sigrecv)
243 >                        pause();        /* wait for wake-up call */
244 >                i++;
245 >        }
246 >        if (i < argc) {                 /* open picture file */
247                  fname = argv[i];
248                  fin = fopen(fname, "r");
249 <                if (fin == NULL) {
250 <                        sprintf(errmsg, "cannot open file \"%s\"", fname);
251 <                        quiterr(errmsg);
174 <                }
175 <        } else if (i != argc)
176 <                goto userr;
249 >                if (fin == NULL)
250 >                        quiterr("cannot open picture file");
251 >        }
252                                  /* get header */
253          getheader(fin, headline, NULL);
254                                  /* get picture dimensions */
# Line 187 | Line 262 | char  *argv[];
262          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
263                  quiterr("out of memory");
264  
265 <        init();                 /* get file and open window */
265 >        init(argc, argv);                       /* get file and open window */
266  
267          for ( ; ; )
268                  getevent();             /* main loop */
269   userr:
270          fprintf(stderr,
271 < "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n",
271 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n",
272                          progname);
273          exit(1);
274   }
275  
276  
277 < headline(s)             /* get relevant info from header */
278 < char  *s;
277 > static int
278 > headline(               /* get relevant info from header */
279 >        char    *s,
280 >        void    *p
281 > )
282   {
283          char  fmt[32];
284  
285          if (isexpos(s))
286                  exposure *= exposval(s);
287 <        else if (isformat(s)) {
210 <                formatval(fmt, s);
287 >        else if (formatval(fmt, s))
288                  wrongformat = strcmp(fmt, COLRFMT);
289 <        } else if (isview(s) && sscanview(&ourview, s) > 0)
289 >        else if (isview(s) && sscanview(&ourview, s) > 0)
290                  gotview++;
291 +        return(0);
292   }
293  
294  
295 < init()                  /* get data and open window */
295 > static void
296 > init(                   /* get data and open window */
297 >        int argc,
298 >        char **argv
299 > )
300   {
219        XWMHints        ourxwmhints;
301          XSetWindowAttributes    ourwinattr;
302 <        XSizeHints      oursizhints;
302 >        XClassHint      xclshints;
303 >        XWMHints        xwmhints;
304 >        XSizeHints      xszhints;
305 >        XTextProperty   windowName, iconName;
306 >        XGCValues       xgcv;
307 >        char    *name;
308          register int    i;
309          
310          if (fname != NULL) {
311                  scanpos = (long *)malloc(ymax*sizeof(long));
312                  if (scanpos == NULL)
313 <                        goto memerr;
313 >                        quiterr("out of memory");
314                  for (i = 0; i < ymax; i++)
315                          scanpos[i] = -1;
316 <        }
316 >                name = fname;
317 >        } else
318 >                name = progname;
319 >                                /* remove directory prefix from name */
320 >        for (i = strlen(name); i-- > 0; )
321 >                if (name[i] == '/')
322 >                        break;
323 >        name += i+1;
324          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
325                  quiterr("cannot open display");
326 +                                /* set gamma value */
327 +        if (gamstr == NULL)             /* get it from the X server */
328 +                gamstr = XGetDefault(thedisplay, "radiance", "gamma");
329 +        if (gamstr == NULL)             /* get it from the environment */
330 +                gamstr = getenv("DISPLAY_GAMMA");
331 +        if (gamstr != NULL)
332 +                gamcor = atof(gamstr);
333                                  /* get best visual for default screen */
334          getbestvis();
335                                  /* store image */
336          getras();
337                                  /* get size and position */
338 <        bzero((char *)&oursizhints, sizeof(oursizhints));
339 <        oursizhints.width = xmax; oursizhints.height = ymax;
338 >        xszhints.flags = 0;
339 >        xszhints.width = xmax; xszhints.height = ymax;
340          if (geometry != NULL) {
341 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
342 <                                (unsigned *)&oursizhints.width,
343 <                                (unsigned *)&oursizhints.height);
341 >                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
342 >                                (unsigned *)&xszhints.width,
343 >                                (unsigned *)&xszhints.height);
344                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
345 <                        oursizhints.flags |= USSize;
345 >                        xszhints.flags |= USSize;
346                  else
347 <                        oursizhints.flags |= PSize;
347 >                        xszhints.flags |= PSize;
348                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
349 <                        oursizhints.flags |= USPosition;
349 >                        xszhints.flags |= USPosition;
350                          if (i & XNegative)
351 <                                oursizhints.x += DisplayWidth(thedisplay,
352 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
351 >                                xszhints.x += DisplayWidth(thedisplay,
352 >                                ourscreen)-1-xszhints.width-2*BORWIDTH;
353                          if (i & YNegative)
354 <                                oursizhints.y += DisplayHeight(thedisplay,
355 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
354 >                                xszhints.y += DisplayHeight(thedisplay,
355 >                                ourscreen)-1-xszhints.height-2*BORWIDTH;
356                  }
357          }
358 <                                /* open window */
359 <        ourwinattr.border_pixel = ourblack;
360 <        ourwinattr.background_pixel = ourwhite;
361 <        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
362 <                        ourvis.visual, AllocNone);
363 <        wind = XCreateWindow(thedisplay, ourroot, oursizhints.x, oursizhints.y,
364 <                        oursizhints.width, oursizhints.height, BORWIDTH,
365 <                        ourvis.depth, InputOutput, ourvis.visual,
366 <                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
358 >        /* open window */
359 >        i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
360 >        ourwinattr.border_pixel = ourwhite;
361 >        ourwinattr.background_pixel = ourblack;
362 >        if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
363 >                ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
364 >                i |= CWColormap;
365 >        }
366 >        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
367 >                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
368 >        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
369 >        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
370 >                        xszhints.width, xszhints.height, BORWIDTH,
371 >                        ourvis.depth, InputOutput, ourvis.visual,
372 >                        i, &ourwinattr);
373          if (wind == 0)
374                  quiterr("cannot create window");
269        XFreeColormap(thedisplay, ourwinattr.colormap);
375          width = xmax;
376          height = ymax;
377 <        ourgc = XCreateGC(thedisplay, wind, 0, 0);
378 <        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes);
274 <        revgc = XCreateGC(thedisplay, wind, 0, 0);
275 <        XSetFunction(thedisplay, revgc, GXinvert);
276 <        fontid = XLoadFont(thedisplay, FONTNAME);
277 <        if (fontid == 0)
377 >        /* prepare graphics drawing context */
378 >        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
379                  quiterr("cannot get font");
380 <        XSetFont(thedisplay, ourgc, fontid);
381 <        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
382 <                        XC_diamond_cross));
383 <        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
384 <        if (oursizhints.flags)
385 <                XSetNormalHints(thedisplay, wind, &oursizhints);
386 <        ourxwmhints.flags = InputHint|IconPixmapHint;
387 <        ourxwmhints.input = True;
388 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
380 >        xgcv.foreground = ourblack;
381 >        xgcv.background = ourwhite;
382 >        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
383 >                        GCFont, &xgcv);
384 >        xgcv.function = GXinvert;
385 >        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
386 >                        GCFunction, &xgcv);
387 >
388 >        /* set up the window manager */
389 >        xwmhints.flags = InputHint|IconPixmapHint;
390 >        xwmhints.input = True;
391 >        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
392                          wind, icondata, iconwidth, iconheight);
393 <        XSetWMHints(thedisplay, wind, &ourxwmhints);
394 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
395 <                        |ButtonMotionMask|StructureNotifyMask
396 <                        |KeyPressMask|ExposureMask);
393 >
394 >        windowName.encoding = iconName.encoding = XA_STRING;
395 >        windowName.format = iconName.format = 8;
396 >        windowName.value = (u_char *)name;
397 >        windowName.nitems = strlen(windowName.value);
398 >        iconName.value = (u_char *)name;
399 >        iconName.nitems = strlen(windowName.value);
400 >
401 >        xclshints.res_name = NULL;
402 >        xclshints.res_class = "Ximage";
403 >        XSetWMProperties(thedisplay, wind, &windowName, &iconName,
404 >                        argv, argc, &xszhints, &xwmhints, &xclshints);
405 >        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
406 >        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
407 >        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
408 >
409          XMapWindow(thedisplay, wind);
294        return;
295 memerr:
296        quiterr("out of memory");
410   } /* end of init */
411  
412  
413 < quiterr(err)            /* print message and exit */
414 < char  *err;
413 > static void
414 > quiterr(                /* print message and exit */
415 >        char  *err
416 > )
417   {
418 <        if (err != NULL) {
419 <                fprintf(stderr, "%s: %s\n", progname, err);
420 <                exit(1);
418 >        register int  es;
419 >        int  cs;
420 >
421 >        if ( (es = err != NULL) )
422 >                fprintf(stderr, "%s: %s: %s\n", progname,
423 >                                fname==NULL?"<stdin>":fname, err);
424 >        if (thedisplay != NULL)
425 >                XCloseDisplay(thedisplay);
426 >        if ((parent < 0) & (sigrecv == 0))
427 >                kill(getppid(), SIGCONT);
428 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
429 >                if (es == 0)
430 >                        es = cs>>8 & 0xff;
431 >                parent--;
432          }
433 <        exit(0);
433 >        exit(es);
434   }
435  
436  
437   static int
438 < viscmp(v1,v2)           /* compare visual to see which is better, descending */
439 < register XVisualInfo    *v1, *v2;
438 > viscmp(         /* compare visual to see which is better, descending */
439 >        register XVisualInfo    *v1,
440 >        register XVisualInfo    *v2
441 > )
442   {
443          int     bad1 = 0, bad2 = 0;
444          register int  *rp;
# Line 322 | Line 450 | register XVisualInfo   *v1, *v2;
450                                  return(-1);
451                          if (v1->depth == 32 && v2->depth == 24)
452                                  return(1);
453 <                        return(0);
453 >                                        /* go for maximum depth otherwise */
454 >                        return(v2->depth - v1->depth);
455                  }
456                                          /* don't be too greedy */
457                  if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
458                          return(v1->depth - v2->depth);
459                  return(v2->depth - v1->depth);
460          }
461 <                                        /* prefer Pseudo when < 24-bit */
461 >                                        /* prefer Pseudo when < 15-bit */
462          if ((v1->class == TrueColor || v1->class == DirectColor) &&
463 <                        v1->depth < 24)
463 >                        v1->depth < 15)
464                  bad1 = 1;
465          if ((v2->class == TrueColor || v2->class == DirectColor) &&
466 <                        v2->depth < 24)
466 >                        v2->depth < 15)
467                  bad2 = -1;
468          if (bad1 | bad2)
469                  return(bad1+bad2);
# Line 349 | Line 478 | register XVisualInfo   *v1, *v2;
478   }
479  
480  
481 < getbestvis()                    /* get the best visual for this screen */
481 > static void
482 > getbestvis(void)                        /* get the best visual for this screen */
483   {
484   #ifdef DEBUG
485   static char  vistype[][12] = {
# Line 401 | Line 531 | static char  vistype[][12] = {
531          if (viscmp(&xvi[i],&ourvis) > 0)
532                  quiterr("inadequate visuals on this screen");
533                                          /* OK, we'll use it */
534 <        copystruct(&ourvis, &xvi[i]);
534 >        ourvis = xvi[i];
535   #ifdef DEBUG
536          fprintf(stderr, "Selected visual type %s, depth %d\n",
537                          vistype[ourvis.class], ourvis.depth);
# Line 431 | Line 561 | static char  vistype[][12] = {
561   }
562  
563  
564 < getras()                                /* get raster file */
564 > static void
565 > getras(void)                            /* get raster file */
566   {
436        XVisualInfo     vinfo;
437
567          if (maxcolors <= 2) {           /* monochrome */
568                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
569                  if (ourdata == NULL)
# Line 444 | Line 573 | getras()                               /* get raster file */
573                  if (ourras == NULL)
574                          goto fail;
575                  getmono();
576 <        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
577 <                ourdata = (unsigned char *)malloc(4*xmax*ymax);
576 >        } else if ((ourvis.class == TrueColor) | (ourvis.class == DirectColor)) {
577 >                int  datsiz = ourvis.depth>16 ? sizeof(int32) : sizeof(int16);
578 >                ourdata = (unsigned char *)malloc(datsiz*xmax*ymax);
579                  if (ourdata == NULL)
580                          goto fail;
581 <                ourras = make_raster(thedisplay, &ourvis, 32,
582 <                                ourdata, xmax, ymax, 32);
581 >                ourras = make_raster(thedisplay, &ourvis, datsiz*8,
582 >                                ourdata, xmax, ymax, datsiz*8);
583                  if (ourras == NULL)
584                          goto fail;
585                  getfull();
# Line 461 | Line 591 | getras()                               /* get raster file */
591                                  xmax, ymax, 8);
592                  if (ourras == NULL)
593                          goto fail;
594 <                if (greyscale | ourvis.class == StaticGray)
594 >                if (greyscale)
595                          getgrey();
596                  else
597                          getmapped();
# Line 474 | Line 604 | fail:
604   }
605  
606  
607 < getevent()                              /* process the next event */
607 > static void
608 > getevent(void)                          /* process the next event */
609   {
610 <        union {
480 <                XEvent  u;
481 <                XConfigureEvent  c;
482 <                XExposeEvent  e;
483 <                XButtonPressedEvent  b;
484 <                XKeyPressedEvent  k;
485 <        } e;
610 >        XEvent xev;
611  
612 <        XNextEvent(thedisplay, &e.u);
613 <        switch (e.u.type) {
612 >        XNextEvent(thedisplay, &xev);
613 >        switch ((int)xev.type) {
614          case KeyPress:
615 <                docom(&e.k);
615 >                docom(&xev.xkey);
616                  break;
617          case ConfigureNotify:
618 <                width = e.c.width;
619 <                height = e.c.height;
618 >                width = xev.xconfigure.width;
619 >                height = xev.xconfigure.height;
620                  break;
621          case MapNotify:
622                  map_rcolors(ourras, wind);
623                  if (fast)
624                          make_rpixmap(ourras, wind);
625 +                if ((!sequential) & (parent < 0) & (sigrecv == 0)) {
626 +                        kill(getppid(), SIGCONT);
627 +                        sigrecv--;
628 +                }
629                  break;
630          case UnmapNotify:
631                  if (!fast)
632                          unmap_rcolors(ourras);
633                  break;
634          case Expose:
635 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
635 >                redraw(xev.xexpose.x, xev.xexpose.y,
636 >                                xev.xexpose.width, xev.xexpose.height);
637                  break;
638          case ButtonPress:
639 <                if (e.b.state & (ShiftMask|ControlMask))
640 <                        moveimage(&e.b);
511 <                else if (e.b.button == Button2)
512 <                        traceray(e.b.x, e.b.y);
639 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
640 >                        moveimage(&xev.xbutton);
641                  else
642 <                        getbox(&e.b);
642 >                        switch (xev.xbutton.button) {
643 >                        case Button1:
644 >                                getbox(&xev.xbutton);
645 >                                break;
646 >                        case Button2:
647 >                                traceray(xev.xbutton.x, xev.xbutton.y);
648 >                                break;
649 >                        case Button3:
650 >                                trackrays(&xev.xbutton);
651 >                                break;
652 >                        }
653                  break;
654 +        case ClientMessage:
655 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
656 +                                (xev.xclient.data.l[0] == closedownAtom))
657 +                        quiterr(NULL);
658 +                break;
659          }
660   }
661  
662  
663 < traceray(xpos, ypos)                    /* print ray corresponding to pixel */
664 < int  xpos, ypos;
663 > static int
664 > traceray(                       /* print requested pixel data */
665 >        int  xpos,
666 >        int  ypos
667 > )
668   {
669 <        FLOAT  hv[2];
669 >        RREAL  hv[2];
670          FVECT  rorg, rdir;
671 +        COLOR  cval;
672 +        register char  *cp;
673  
674 <        if (!gotview) {         /* no view, no can do */
675 <                XBell(thedisplay, 0);
676 <                return(-1);
677 <        }
674 >        bbox.xmin = xpos; bbox.xsiz = 1;
675 >        bbox.ymin = ypos; bbox.ysiz = 1;
676 >        avgbox(cval);
677 >        scalecolor(cval, 1./exposure);
678          pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
679 <        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
680 <                return(-1);
681 <        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
682 <        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
679 >        if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
680 >                rorg[0] = rorg[1] = rorg[2] =
681 >                rdir[0] = rdir[1] = rdir[2] = 0.;
682 >
683 >        for (cp = tout; *cp; cp++)      /* print what they asked for */
684 >                switch (*cp) {
685 >                case 'o':                       /* origin */
686 >                        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
687 >                        break;
688 >                case 'd':                       /* direction */
689 >                        printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
690 >                        break;
691 >                case 'v':                       /* radiance value */
692 >                        printf("%e %e %e ", colval(cval,RED),
693 >                                        colval(cval,GRN), colval(cval,BLU));
694 >                        break;
695 >                case 'l':                       /* luminance */
696 >                        printf("%e ", luminance(cval));
697 >                        break;
698 >                case 'p':                       /* pixel position */
699 >                        printf("%d %d ", (int)(hv[0]*inpres.xr),
700 >                                        (int)(hv[1]*inpres.yr));
701 >                        break;
702 >                }
703 >        putchar('\n');
704          fflush(stdout);
705          return(0);
706   }
707  
708  
709 < docom(ekey)                             /* execute command */
710 < XKeyPressedEvent  *ekey;
709 > static int
710 > docom(                          /* execute command */
711 >        XKeyPressedEvent  *ekey
712 > )
713   {
714          char  buf[80];
715          COLOR  cval;
716          XColor  cvx;
717          int  com, n;
718          double  comp;
719 <        FLOAT  hv[2];
719 >        RREAL  hv[2];
720  
721          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
722          if (n == 0)
# Line 560 | Line 731 | XKeyPressedEvent  *ekey;
731          case '\r':
732          case 'l':
733          case 'c':                               /* value */
734 <                if (avgbox(cval) == -1)
734 >                if (!avgbox(cval))
735                          return(-1);
736                  switch (com) {
737                  case '\n':
# Line 568 | Line 739 | XKeyPressedEvent  *ekey;
739                          sprintf(buf, "%.3f", intens(cval)/exposure);
740                          break;
741                  case 'l':                               /* luminance */
742 <                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
742 >                        sprintf(buf, "%.1fL", luminance(cval)/exposure);
743                          break;
744                  case 'c':                               /* color */
745                          comp = pow(2.0, (double)scale);
# Line 579 | Line 750 | XKeyPressedEvent  *ekey;
750                          break;
751                  }
752                  XDrawImageString(thedisplay, wind, ourgc,
753 <                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
753 >                                bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
754                  return(0);
755          case 'i':                               /* identify (contour) */
756                  if (ourras->pixels == NULL)
# Line 602 | Line 773 | XKeyPressedEvent  *ekey;
773                  return(0);
774          case 't':                               /* trace */
775                  return(traceray(ekey->x, ekey->y));
776 +        case 'a':                               /* auto exposure */
777 +                if (fname == NULL)
778 +                        return(-1);
779 +                tmflags = TM_F_CAMERA;
780 +                strcpy(buf, "auto exposure...");
781 +                goto remap;
782 +        case 'h':                               /* human response */
783 +                if (fname == NULL)
784 +                        return(-1);
785 +                tmflags = TM_F_HUMAN;
786 +                strcpy(buf, "human exposure...");
787 +                goto remap;
788          case '=':                               /* adjust exposure */
789 <                if (avgbox(cval) == -1)
789 >        case '@':                               /* adaptation level */
790 >                if (!avgbox(cval))
791                          return(-1);
792 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
793 <                if (n == 0)
794 <                        return(0);
795 <                scale_rcolors(ourras, pow(2.0, (double)n));
792 >                comp = bright(cval);
793 >                if (comp < 1e-20) {
794 >                        XBell(thedisplay, 0);
795 >                        return(-1);
796 >                }
797 >                if (com == '@')
798 >                        comp = 106./exposure/
799 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
800 >                else
801 >                        comp = .5/comp;
802 >                comp = log(comp)/.69315 - scale;
803 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
804 >                if (tmflags != TM_F_LINEAR)
805 >                        tmflags = TM_F_LINEAR;  /* turn off tone mapping */
806 >                else {
807 >                        if (n == 0)             /* else check if any change */
808 >                                return(0);
809 >                        scale_rcolors(ourras, pow(2.0, (double)n));
810 >                }
811                  scale += n;
812                  sprintf(buf, "%+d", scale);
813 +        remap:
814                  XDrawImageString(thedisplay, wind, ourgc,
815 <                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
815 >                                bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
816                  XFlush(thedisplay);
817 <                free(ourdata);
817 >                /* free(ourdata);       This is done in XDestroyImage()! */
818                  free_raster(ourras);
819                  getras();
820          /* fall through */
# Line 627 | Line 827 | XKeyPressedEvent  *ekey;
827                          make_rpixmap(ourras, wind);
828                  redraw(0, 0, width, height);
829                  return(0);
830 +        case 'f':                               /* turn on fast redraw */
831 +                fast = 1;
832 +                make_rpixmap(ourras, wind);
833 +                return(0);
834 +        case 'F':                               /* turn off fast redraw */
835 +                fast = 0;
836 +                free_rpixmap(ourras);
837 +                return(0);
838          case '0':                               /* recenter origin */
839 <                if (xoff == 0 & yoff == 0)
839 >                if ((xoff == 0) & (yoff == 0))
840                          return(0);
841                  xoff = yoff = 0;
842                  XClearWindow(thedisplay, wind);
843                  redraw(0, 0, width, height);
844                  return(0);
845          case ' ':                               /* clear */
846 <                redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
846 >                redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz);
847                  return(0);
848          default:
849                  XBell(thedisplay, 0);
# Line 644 | Line 852 | XKeyPressedEvent  *ekey;
852   }
853  
854  
855 < moveimage(ebut)                         /* shift the image */
856 < XButtonPressedEvent  *ebut;
855 > static void
856 > moveimage(                              /* shift the image */
857 >        XButtonPressedEvent  *ebut
858 > )
859   {
860 <        union {
651 <                XEvent  u;
652 <                XButtonReleasedEvent  b;
653 <                XPointerMovedEvent  m;
654 <        }  e;
860 >        XEvent  e;
861          int     mxo, myo;
862  
863 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
864 <        while (e.u.type == MotionNotify) {
865 <                mxo = e.m.x;
866 <                myo = e.m.y;
863 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
864 >        while (e.type == MotionNotify) {
865 >                mxo = e.xmotion.x;
866 >                myo = e.xmotion.y;
867                  revline(ebut->x, ebut->y, mxo, myo);
868                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
869                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
870 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
870 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
871                  revline(ebut->x, ebut->y, mxo, myo);
872                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
873                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
874          }
875 <        xoff += e.b.x - ebut->x;
876 <        yoff += e.b.y - ebut->y;
875 >        xoff += e.xbutton.x - ebut->x;
876 >        yoff += e.xbutton.y - ebut->y;
877          XClearWindow(thedisplay, wind);
878          redraw(0, 0, width, height);
879   }
880  
881  
882 < getbox(ebut)                            /* get new box */
883 < XButtonPressedEvent  *ebut;
882 > static void
883 > getbox(                         /* get new bbox */
884 >        XButtonPressedEvent  *ebut
885 > )
886   {
887 <        union {
680 <                XEvent  u;
681 <                XButtonReleasedEvent  b;
682 <                XPointerMovedEvent  m;
683 <        }  e;
887 >        XEvent  e;
888  
889 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
890 <        while (e.u.type == MotionNotify) {
891 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
892 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
893 <                revbox(ebut->x, ebut->y, box.xmin, box.ymin);
889 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
890 >        while (e.type == MotionNotify) {
891 >                revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y);
892 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
893 >                revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin);
894          }
895 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
896 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
897 <        if (box.xmin > ebut->x) {
898 <                box.xsiz = box.xmin - ebut->x + 1;
899 <                box.xmin = ebut->x;
895 >        bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
896 >        bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
897 >        if (bbox.xmin > ebut->x) {
898 >                bbox.xsiz = bbox.xmin - ebut->x + 1;
899 >                bbox.xmin = ebut->x;
900          } else {
901 <                box.xsiz = ebut->x - box.xmin + 1;
901 >                bbox.xsiz = ebut->x - bbox.xmin + 1;
902          }
903 <        if (box.ymin > ebut->y) {
904 <                box.ysiz = box.ymin - ebut->y + 1;
905 <                box.ymin = ebut->y;
903 >        if (bbox.ymin > ebut->y) {
904 >                bbox.ysiz = bbox.ymin - ebut->y + 1;
905 >                bbox.ymin = ebut->y;
906          } else {
907 <                box.ysiz = ebut->y - box.ymin + 1;
907 >                bbox.ysiz = ebut->y - bbox.ymin + 1;
908          }
909   }
910  
911  
912 < revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
913 < int  x0, y0, x1, y1;
912 > static void
913 > trackrays(                              /* trace rays as mouse moves */
914 >        XButtonPressedEvent  *ebut
915 > )
916   {
917 +        XEvent  e;
918 +        unsigned long   lastrept;
919 +
920 +        traceray(ebut->x, ebut->y);
921 +        lastrept = ebut->time;
922 +        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
923 +        while (e.type == MotionNotify) {
924 +                if (e.xmotion.time >= lastrept + tinterv) {
925 +                        traceray(e.xmotion.x, e.xmotion.y);
926 +                        lastrept = e.xmotion.time;
927 +                }
928 +                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
929 +        }
930 + }
931 +
932 +
933 + static void
934 + revbox(                 /* draw bbox with reversed lines */
935 +        int  x0,
936 +        int  y0,
937 +        int  x1,
938 +        int  y1
939 + )
940 + {
941          revline(x0, y0, x1, y0);
942          revline(x0, y1, x1, y1);
943          revline(x0, y0, x0, y1);
# Line 715 | Line 945 | int  x0, y0, x1, y1;
945   }
946  
947  
948 < avgbox(clr)                             /* average color over current box */
949 < COLOR  clr;
948 > static void
949 > colavg(
950 >        register COLR   *scn,
951 >        register int    n,
952 >        void *cavg
953 > )
954   {
955 <        static COLOR  lc;
956 <        static int  ll, lr, lt, lb;
955 >        COLOR   col;
956 >
957 >        while (n--) {
958 >                colr_color(col, *scn++);
959 >                addcolor((COLORV*)cavg, col);
960 >        }
961 > }
962 >
963 >
964 > static int
965 > avgbox(                         /* average color over current bbox */
966 >        COLOR   cavg
967 > )
968 > {
969 >        double  d;
970 >        register int    rval;
971 >
972 >        setcolor(cavg, 0., 0., 0.);
973 >        rval = dobox(colavg, (char *)cavg);
974 >        if (rval > 0) {
975 >                d = 1./rval;
976 >                scalecolor(cavg, d);
977 >        }
978 >        return(rval);
979 > }
980 >
981 >
982 > static int
983 > dobox(                          /* run function over bbox */
984 >        //void  (*f)(),                 /* function to call for each subscan */
985 >        doboxf_t *f,                    /* function to call for each subscan */
986 >        void    *p                      /* pointer to private data */
987 > )
988 > {
989          int  left, right, top, bottom;
990          int  y;
725        double  d;
726        COLOR  ctmp;
727        register int  x;
991  
992 <        setcolor(clr, 0.0, 0.0, 0.0);
993 <        left = box.xmin - xoff;
731 <        right = left + box.xsiz;
992 >        left = bbox.xmin - xoff;
993 >        right = left + bbox.xsiz;
994          if (left < 0)
995                  left = 0;
996          if (right > xmax)
997                  right = xmax;
998          if (left >= right)
999 <                return(-1);
1000 <        top = box.ymin - yoff;
1001 <        bottom = top + box.ysiz;
999 >                return(0);
1000 >        top = bbox.ymin - yoff;
1001 >        bottom = top + bbox.ysiz;
1002          if (top < 0)
1003                  top = 0;
1004          if (bottom > ymax)
1005                  bottom = ymax;
1006          if (top >= bottom)
745                return(-1);
746        if (left == ll && right == lr && top == lt && bottom == lb) {
747                copycolor(clr, lc);
1007                  return(0);
749        }
1008          for (y = top; y < bottom; y++) {
1009                  if (getscan(y) == -1)
1010                          return(-1);
1011 <                for (x = left; x < right; x++) {
1012 <                        colr_color(ctmp, scanline[x]);
1013 <                        addcolor(clr, ctmp);
1011 >                (*f)(scanline+left, right-left, p);
1012 >        }
1013 >        return((right-left)*(bottom-top));
1014 > }
1015 >
1016 >
1017 > static void
1018 > addfix(                 /* add fixation points to histogram */
1019 >        COLR    *scn,
1020 >        int     n,
1021 >        void    *p  /*NOTUSED*/
1022 > )
1023 > {
1024 >        if (tmCvColrs(lscan, TM_NOCHROM, scn, n))
1025 >                goto tmerr;
1026 >        if (tmAddHisto(lscan, n, FIXWEIGHT))
1027 >                goto tmerr;
1028 >        return;
1029 > tmerr:
1030 >        quiterr("tone mapping error");
1031 > }
1032 >
1033 >
1034 > static void
1035 > make_tonemap(void)                      /* initialize tone mapping */
1036 > {
1037 >        int  flags, y;
1038 >
1039 >        if (tmflags != TM_F_LINEAR && fname == NULL) {
1040 >                fprintf(stderr, "%s: cannot adjust tone of standard input\n",
1041 >                                progname);
1042 >                tmflags = TM_F_LINEAR;
1043 >        }
1044 >        if (tmflags == TM_F_LINEAR) {   /* linear with clamping */
1045 >                setcolrcor(pow, 1.0/gamcor);
1046 >                return;
1047 >        }
1048 >        flags = tmflags;                /* histogram adjustment */
1049 >        if (greyscale) flags |= TM_F_BW;
1050 >        if (tmTop != NULL) {            /* reuse old histogram if one */
1051 >                tmDone(tmTop);
1052 >                tmTop->flags = flags;
1053 >        } else {                        /* else initialize */
1054 >                if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
1055 >                        goto memerr;
1056 >                if (greyscale) {
1057 >                        cscan = TM_NOCHROM;
1058 >                        if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
1059 >                                goto memerr;
1060 >                } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax))
1061 >                                == NULL)
1062 >                        goto memerr;
1063 >                                                /* initialize tm library */
1064 >                if (tmInit(flags, stdprims, gamcor) == NULL)
1065 >                        goto memerr;
1066 >                if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
1067 >                        goto tmerr;
1068 >                                                /* compute picture histogram */
1069 >                for (y = 0; y < ymax; y++) {
1070 >                        getscan(y);
1071 >                        if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
1072 >                                goto tmerr;
1073 >                        if (tmAddHisto(lscan, xmax, 1))
1074 >                                goto tmerr;
1075                  }
1076          }
1077 <        d = 1.0/((right-left)*(bottom-top));
1078 <        scalecolor(clr, d);
1079 <        ll = left; lr = right; lt = top; lb = bottom;
1080 <        copycolor(lc, clr);
1081 <        return(0);
1077 >        tmDup();                        /* add fixations to duplicate map */
1078 >        dobox(addfix, NULL);
1079 >                                        /* (re)compute tone mapping */
1080 >        if (tmComputeMapping(gamcor, 0., 0.))
1081 >                goto tmerr;
1082 >        return;
1083 > memerr:
1084 >        quiterr("out of memory in make_tonemap");
1085 > tmerr:
1086 >        quiterr("tone mapping error");
1087   }
1088  
1089  
1090 < getmono()                       /* get monochrome data */
1090 > static void
1091 > tmap_colrs(             /* apply tone mapping to scanline */
1092 >        register COLR  *scn,
1093 >        int  len
1094 > )
1095   {
1096 +        register BYTE  *ps;
1097 +
1098 +        if (tmflags == TM_F_LINEAR) {
1099 +                if (scale)
1100 +                        shiftcolrs(scn, len, scale);
1101 +                colrs_gambs(scn, len);
1102 +                return;
1103 +        }
1104 +        if (len > xmax)
1105 +                quiterr("code error 1 in tmap_colrs");
1106 +        if (tmCvColrs(lscan, cscan, scn, len))
1107 +                goto tmerr;
1108 +        if (tmMapPixels(pscan, lscan, cscan, len))
1109 +                goto tmerr;
1110 +        ps = pscan;
1111 +        if (greyscale)
1112 +                while (len--) {
1113 +                        scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1114 +                        scn[0][EXP] = COLXS;
1115 +                        scn++;
1116 +                }
1117 +        else
1118 +                while (len--) {
1119 +                        scn[0][RED] = *ps++;
1120 +                        scn[0][GRN] = *ps++;
1121 +                        scn[0][BLU] = *ps++;
1122 +                        scn[0][EXP] = COLXS;
1123 +                        scn++;
1124 +                }
1125 +        return;
1126 + tmerr:
1127 +        quiterr("tone mapping error");
1128 + }
1129 +
1130 +
1131 + static void
1132 + getmono(void)                   /* get monochrome data */
1133 + {
1134          register unsigned char  *dp;
1135          register int    x, err;
1136          int     y, errp;
# Line 791 | Line 1157 | getmono()                      /* get monochrome data */
1157                          cerr[x] = err + errp;
1158                  }
1159          }
1160 <        free((char *)cerr);
1160 >        free((void *)cerr);
1161   }
1162  
1163  
1164 < add2icon(y, scan)               /* add a scanline to our icon data */
1165 < int  y;
1166 < COLR  *scan;
1164 > static void
1165 > add2icon(               /* add a scanline to our icon data */
1166 >        int  y,
1167 >        COLR  *scan
1168 > )
1169   {
1170          static short  cerr[ICONSIZ];
1171          static int  ynext;
# Line 834 | Line 1202 | COLR  *scan;
1202                  errp = err;
1203                  ti = x*xmax/iconwidth;
1204                  copycolr(clr, scan[ti]);
1205 <                normcolrs(clr, 1, scale);
1205 >                normcolrs(&clr, 1, scale);
1206                  err += normbright(clr) + cerr[x];
1207                  if (err > 127)
1208                          err -= 255;
# Line 847 | Line 1215 | COLR  *scan;
1215   }
1216  
1217  
1218 < getfull()                       /* get full (24-bit) data */
1218 > static void
1219 > getfull(void)                   /* get full (24-bit) data */
1220   {
1221          int     y;
1222 <        register unsigned long  *dp;
1222 >        register uint32 *dp;
1223 >        register uint16 *dph;
1224          register int    x;
1225 <                                        /* set gamma correction */
1226 <        setcolrgam(gamcor);
1225 >                                        /* initialize tone mapping */
1226 >        make_tonemap();
1227                                          /* read and convert file */
1228 <        dp = (unsigned long *)ourdata;
1228 >        dp = (uint32 *)ourdata;
1229 >        dph = (uint16 *)ourdata;
1230          for (y = 0; y < ymax; y++) {
1231                  getscan(y);
1232                  add2icon(y, scanline);
1233 <                if (scale)
1234 <                        shiftcolrs(scanline, xmax, scale);
1235 <                colrs_gambs(scanline, xmax);
865 <                if (ourras->image->blue_mask & 1)
1233 >                tmap_colrs(scanline, xmax);
1234 >                switch (ourras->image->blue_mask) {
1235 >                case 0xff:              /* 24-bit RGB */
1236                          for (x = 0; x < xmax; x++)
1237 <                                *dp++ = scanline[x][RED] << 16 |
1238 <                                        scanline[x][GRN] << 8 |
1239 <                                        scanline[x][BLU] ;
1240 <                else
1237 >                                *dp++ = (uint32)scanline[x][RED] << 16 |
1238 >                                        (uint32)scanline[x][GRN] << 8 |
1239 >                                        (uint32)scanline[x][BLU] ;
1240 >                        break;
1241 >                case 0xff0000:          /* 24-bit BGR */
1242                          for (x = 0; x < xmax; x++)
1243 <                                *dp++ = scanline[x][RED] |
1244 <                                        scanline[x][GRN] << 8 |
1245 <                                        scanline[x][BLU] << 16 ;
1243 >                                *dp++ = (uint32)scanline[x][RED] |
1244 >                                        (uint32)scanline[x][GRN] << 8 |
1245 >                                        (uint32)scanline[x][BLU] << 16 ;
1246 >                        break;
1247 > #if 0
1248 >                case 0x1f:              /* 15-bit RGB */
1249 >                        for (x = 0; x < xmax; x++)
1250 >                                *dph++ =        (scanline[x][RED] << 7 & 0x7c00) |
1251 >                                        (scanline[x][GRN] << 2 & 0x3e0) |
1252 >                                        (unsigned)scanline[x][BLU] >> 3 ;
1253 >                        break;
1254 >                case 0x7c00:            /* 15-bit BGR */
1255 >                        for (x = 0; x < xmax; x++)
1256 >                                *dph++ =        (unsigned)scanline[x][RED] >> 3 |
1257 >                                        (scanline[x][GRN] << 2 & 0x3e0) |
1258 >                                        (scanline[x][BLU] << 7 & 0x7c00) ;
1259 >                        break;
1260 > #endif
1261 >                default:                /* unknown */
1262 >                        if (ourvis.depth > 16)
1263 >                                for (x = 0; x < xmax; x++) {
1264 >                                        *dp = ourras->image->red_mask &
1265 >                                                ourras->image->red_mask*scanline[x][RED]/255;
1266 >                                        *dp |= ourras->image->green_mask &
1267 >                                                ourras->image->green_mask*scanline[x][GRN]/255;
1268 >                                        *dp++ |= ourras->image->blue_mask &
1269 >                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1270 >                                }
1271 >                        else
1272 >                                for (x = 0; x < xmax; x++) {
1273 >                                        *dph = ourras->image->red_mask &
1274 >                                                ourras->image->red_mask*scanline[x][RED]/255;
1275 >                                        *dph |= ourras->image->green_mask &
1276 >                                                ourras->image->green_mask*scanline[x][GRN]/255;
1277 >                                        *dph++ |= ourras->image->blue_mask &
1278 >                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1279 >                                }
1280 >                        break;
1281 >                }
1282          }
1283   }
1284  
1285  
1286 < getgrey()                       /* get greyscale data */
1286 > static void
1287 > getgrey(void)                   /* get greyscale data */
1288   {
1289          int     y;
1290          register unsigned char  *dp;
1291          register int    x;
1292 <                                        /* set gamma correction */
1293 <        setcolrgam(gamcor);
1292 >                                        /* initialize tone mapping */
1293 >        make_tonemap();
1294                                          /* read and convert file */
1295          dp = ourdata;
1296          for (y = 0; y < ymax; y++) {
1297                  getscan(y);
1298                  add2icon(y, scanline);
1299 <                if (scale)
892 <                        shiftcolrs(scanline, xmax, scale);
893 <                for (x = 0; x < xmax; x++)
894 <                        scanline[x][GRN] = normbright(scanline[x]);
895 <                colrs_gambs(scanline, xmax);
1299 >                tmap_colrs(scanline, xmax);
1300                  if (maxcolors < 256)
1301                          for (x = 0; x < xmax; x++)
1302 <                                *dp++ = ((long)scanline[x][GRN] *
1302 >                                *dp++ = ((int32)scanline[x][GRN] *
1303                                          maxcolors + maxcolors/2) >> 8;
1304                  else
1305                          for (x = 0; x < xmax; x++)
# Line 903 | Line 1307 | getgrey()                      /* get greyscale data */
1307          }
1308          for (x = 0; x < maxcolors; x++)
1309                  clrtab[x][RED] = clrtab[x][GRN] =
1310 <                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
1310 >                        clrtab[x][BLU] = ((int32)x*256 + 128)/maxcolors;
1311   }
1312  
1313  
1314 < getmapped()                     /* get color-mapped data */
1314 > static void
1315 > getmapped(void)                 /* get color-mapped data */
1316   {
1317          int     y;
1318 <                                        /* set gamma correction */
1319 <        setcolrgam(gamcor);
1318 >                                        /* make sure we can do it first */
1319 >        if (fname == NULL)
1320 >                quiterr("cannot map colors from standard input");
1321 >                                        /* initialize tone mapping */
1322 >        make_tonemap();
1323                                          /* make histogram */
1324 <        new_histo();
1324 >        if (new_histo((int32)xmax*ymax) == -1)
1325 >                quiterr("cannot initialize histogram");
1326          for (y = 0; y < ymax; y++) {
1327                  if (getscan(y) < 0)
1328 <                        quiterr("seek error in getmapped");
1328 >                        break;
1329                  add2icon(y, scanline);
1330 <                if (scale)
922 <                        shiftcolrs(scanline, xmax, scale);
923 <                colrs_gambs(scanline, xmax);
1330 >                tmap_colrs(scanline, xmax);
1331                  cnt_colrs(scanline, xmax);
1332          }
1333                                          /* map pixels */
1334          if (!new_clrtab(maxcolors))
1335                  quiterr("cannot create color map");
1336          for (y = 0; y < ymax; y++) {
1337 <                if (getscan(y) < 0)
1338 <                        quiterr("seek error in getmapped");
932 <                if (scale)
933 <                        shiftcolrs(scanline, xmax, scale);
934 <                colrs_gambs(scanline, xmax);
1337 >                getscan(y);
1338 >                tmap_colrs(scanline, xmax);
1339                  if (dither)
1340                          dith_colrs(ourdata+y*xmax, scanline, xmax);
1341                  else
# Line 940 | Line 1344 | getmapped()                    /* get color-mapped data */
1344   }
1345  
1346  
1347 < scale_rcolors(xr, sf)                   /* scale color map */
1348 < register XRASTER        *xr;
1349 < double  sf;
1347 > static void
1348 > scale_rcolors(                  /* scale color map */
1349 >        register XRASTER        *xr,
1350 >        double  sf
1351 > )
1352   {
1353          register int    i;
1354          long    maxv;
# Line 968 | Line 1374 | double sf;
1374   }
1375  
1376  
1377 < getscan(y)
1378 < int  y;
1377 > static int
1378 > getscan(
1379 >        int  y
1380 > )
1381   {
1382 +        static int  trunced = -1;               /* truncated file? */
1383 + skipit:
1384 +        if (trunced >= 0 && y >= trunced) {
1385 +                memset(scanline, '\0', xmax*sizeof(COLR));
1386 +                return(-1);
1387 +        }
1388          if (y != cury) {
1389                  if (scanpos == NULL || scanpos[y] == -1)
1390                          return(-1);
# Line 980 | Line 1394 | int  y;
1394          } else if (scanpos != NULL && scanpos[y] == -1)
1395                  scanpos[y] = ftell(fin);
1396  
1397 <        if (freadcolrs(scanline, xmax, fin) < 0)
1398 <                quiterr("read error");
1399 <
1397 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1398 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1399 >                                progname, fname==NULL?"<stdin>":fname);
1400 >                trunced = y;
1401 >                goto skipit;
1402 >        }
1403          cury++;
1404          return(0);
1405   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines