ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
(Generate patch)

Comparing ray/src/px/x11image.c (file contents):
Revision 1.5 by greg, Wed Mar 7 11:39:26 1990 UTC vs.
Revision 2.34 by greg, Mon Nov 15 10:55:33 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1990 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 21 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
21  
22   #include  "standard.h"
23  
24 + #include  <signal.h>
25   #include  <X11/Xlib.h>
26   #include  <X11/cursorfont.h>
27   #include  <X11/Xutil.h>
28 + #include  <X11/Xatom.h>
29  
30   #include  "color.h"
31   #include  "view.h"
30 #include  "pic.h"
32   #include  "x11raster.h"
33   #include  "random.h"
34 + #include  "resolu.h"
35  
36 + #ifdef  __alpha
37 + #define  int4           int
38 + #endif
39 + #ifndef  int4
40 + #define  int4           long
41 + #endif
42 +
43   #define  FONTNAME       "8x13"          /* text font we'll use */
44  
45 < #define  CTRL(c)        ('c'-'@')
45 > #define  CTRL(c)        ((c)-'@')
46  
47   #define  BORWIDTH       5               /* border width */
48  
49 + #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
50 +
51   #define  ourscreen      DefaultScreen(thedisplay)
41 #define  ourblack       BlackPixel(thedisplay,ourscreen)
42 #define  ourwhite       WhitePixel(thedisplay,ourscreen)
52   #define  ourroot        RootWindow(thedisplay,ourscreen)
44 #define  ourgc          DefaultGC(thedisplay,ourscreen)
53  
54   #define  revline(x0,y0,x1,y1)   XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1)
55  
# Line 52 | Line 60 | double  gamcor = 2.2;                  /* gamma correction */
60   int  dither = 1;                        /* dither colors? */
61   int  fast = 0;                          /* keep picture in Pixmap? */
62  
63 < Window  wind = 0;                       /* our output window */
56 < Font  fontid;                           /* our font */
63 > char    *dispname = NULL;               /* our display name */
64  
65 + Window  wind = 0;                       /* our output window */
66 + unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
67   int  maxcolors = 0;                     /* maximum colors */
68   int  greyscale = 0;                     /* in grey */
69  
# Line 63 | Line 72 | int  scale = 0;                                /* scalefactor; power of two */
72   int  xoff = 0;                          /* x image offset */
73   int  yoff = 0;                          /* y image offset */
74  
75 + int  parent = 0;                        /* number of children, -1 if child */
76 +
77   VIEW  ourview = STDVIEW;                /* image view parameters */
78   int  gotview = 0;                       /* got parameters from file */
79  
80   COLR  *scanline;                        /* scan line buffer */
81  
82 < int  xmax, ymax;                        /* picture resolution */
82 > RESOLU  inpres;                         /* input resolution and ordering */
83 > int  xmax, ymax;                        /* picture dimensions */
84   int  width, height;                     /* window size */
85   char  *fname = NULL;                    /* input file name */
86   FILE  *fin = stdin;                     /* input file */
# Line 77 | Line 89 | int  cury = 0;                         /* current scan location */
89  
90   double  exposure = 1.0;                 /* exposure compensation used */
91  
92 + int  wrongformat = 0;                   /* input in another format? */
93 +
94 + GC      ourgc;                          /* standard graphics context */
95   GC      revgc;                          /* graphics context with GXinvert */
96  
97 < XRASTER *ourras;                        /* our stored image */
97 > int             *ourrank;               /* our visual class ranking */
98 > XVisualInfo     ourvis;                 /* our visual */
99 > XRASTER         *ourras;                /* our stored image */
100   unsigned char   *ourdata;               /* our image data */
101  
102   struct {
# Line 88 | Line 105 | struct {
105  
106   char  *geometry = NULL;                 /* geometry specification */
107  
108 + char  icondata[ICONSIZ*ICONSIZ/8];      /* icon bitmap data */
109 + int  iconwidth = 0, iconheight = 0;
110 +
111   char  *progname;
112  
113   char  errmsg[128];
114  
115 + extern BYTE  clrtab[256][3];            /* global color map */
116 +
117   extern long  ftell();
118  
119 < extern char  *malloc(), *calloc();
119 > Display  *thedisplay;
120 > Atom  closedownAtom, wmProtocolsAtom;
121  
122 < extern double  atof(), pow(), log();
122 > int  sigrecv;
123  
124 < Display  *thedisplay;
124 > int  onsig() { sigrecv++; }
125  
126 +
127   main(argc, argv)
128   int  argc;
129   char  *argv[];
130   {
131 +        extern char  *getenv();
132 +        char  *gv;
133          int  headline();
134          int  i;
135 +        int  pid;
136          
137          progname = argv[0];
138 +        if ((gv = getenv("GAMMA")) != NULL)
139 +                gamcor = atof(gv);
140  
141          for (i = 1; i < argc; i++)
142                  if (argv[i][0] == '-')
# Line 122 | Line 151 | char  *argv[];
151                                  maxcolors = 2;
152                                  break;
153                          case 'd':
154 <                                dither = !dither;
154 >                                if (argv[i][2] == 'i')
155 >                                        dispname = argv[++i];
156 >                                else
157 >                                        dither = !dither;
158                                  break;
159                          case 'f':
160                                  fast = !fast;
# Line 133 | Line 165 | char  *argv[];
165                                  scale = atoi(argv[++i]);
166                                  break;
167                          case 'g':
168 <                                if (!strcmp(argv[i], "-geometry"))
168 >                                if (argv[i][2] == 'e')
169                                          geometry = argv[++i];
170                                  else
171                                          gamcor = atof(argv[++i]);
# Line 146 | Line 178 | char  *argv[];
178                  else
179                          break;
180  
181 <        if (i == argc-1) {
181 >        if (i > argc)
182 >                goto userr;
183 >        while (i < argc-1) {
184 >                sigrecv = 0;
185 >                signal(SIGCONT, onsig);
186 >                if ((pid=fork()) == 0) {        /* a child for each picture */
187 >                        parent = -1;
188 >                        break;
189 >                }
190 >                if (pid < 0)
191 >                        quiterr("fork failed");
192 >                parent++;
193 >                while (!sigrecv)
194 >                        pause();        /* wait for wake-up call */
195 >                i++;
196 >        }
197 >        if (i < argc) {                 /* open picture file */
198                  fname = argv[i];
199                  fin = fopen(fname, "r");
200 <                if (fin == NULL) {
201 <                        sprintf(errmsg, "can't open file \"%s\"", fname);
202 <                        quiterr(errmsg);
155 <                }
156 <        } else if (i != argc)
157 <                goto userr;
200 >                if (fin == NULL)
201 >                        quiterr("cannot open picture file");
202 >        }
203                                  /* get header */
204 <        getheader(fin, headline);
204 >        getheader(fin, headline, NULL);
205                                  /* get picture dimensions */
206 <        if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
207 <                quiterr("bad picture size");
206 >        if (wrongformat || !fgetsresolu(&inpres, fin))
207 >                quiterr("bad picture format");
208 >        xmax = scanlen(&inpres);
209 >        ymax = numscans(&inpres);
210                                  /* set view parameters */
211          if (gotview && setview(&ourview) != NULL)
212                  gotview = 0;
213          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
214                  quiterr("out of memory");
215  
216 <        init();                 /* get file and open window */
216 >        init(argc, argv);                       /* get file and open window */
217  
218 +        if (parent < 0) {
219 +                kill(getppid(), SIGCONT);       /* signal parent if child */
220 +                sigrecv--;
221 +        }
222          for ( ; ; )
223                  getevent();             /* main loop */
224   userr:
225          fprintf(stderr,
226 <        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
226 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic ..\n",
227                          progname);
228 <        quit(1);
228 >        exit(1);
229   }
230  
231  
232   headline(s)             /* get relevant info from header */
233   char  *s;
234   {
235 <        static char  *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
185 <        register char  **an;
235 >        char  fmt[32];
236  
237          if (isexpos(s))
238                  exposure *= exposval(s);
239 <        else
240 <                for (an = altname; *an != NULL; an++)
241 <                        if (!strncmp(*an, s, strlen(*an))) {
242 <                                if (sscanview(&ourview, s+strlen(*an)) > 0)
243 <                                        gotview++;
194 <                                return;
195 <                        }
239 >        else if (isformat(s)) {
240 >                formatval(fmt, s);
241 >                wrongformat = strcmp(fmt, COLRFMT);
242 >        } else if (isview(s) && sscanview(&ourview, s) > 0)
243 >                gotview++;
244   }
245  
246  
247 < init()                  /* get data and open window */
247 > init(argc, argv)                        /* get data and open window */
248 > int argc;
249 > char **argv;
250   {
251          XSetWindowAttributes    ourwinattr;
252 <        XSizeHints  oursizhints;
253 <        register int  i;
252 >        XClassHint      xclshints;
253 >        XWMHints        xwmhints;
254 >        XSizeHints      xszhints;
255 >        XTextProperty   windowName, iconName;
256 >        XGCValues       xgcv;
257 >        char    *name;
258 >        register int    i;
259          
260          if (fname != NULL) {
261                  scanpos = (long *)malloc(ymax*sizeof(long));
262                  if (scanpos == NULL)
263 <                        goto memerr;
263 >                        quiterr("out of memory");
264                  for (i = 0; i < ymax; i++)
265                          scanpos[i] = -1;
266 <        }
267 <        if ((thedisplay = XOpenDisplay(NULL)) == NULL)
268 <                quiterr("can't open display; DISPLAY variable set?");
269 <        if (maxcolors == 0) {           /* get number of available colors */
270 <                i = DisplayPlanes(thedisplay,ourscreen);
271 <                maxcolors = i > 8 ? 256 : 1<<i;
272 <                if (maxcolors > 4) maxcolors -= 2;
273 <        }
266 >                name = fname;
267 >        } else
268 >                name = progname;
269 >                                /* remove directory prefix from name */
270 >        for (i = strlen(name); i-- > 0; )
271 >                if (name[i] == '/')
272 >                        break;
273 >        name += i+1;
274 >        if ((thedisplay = XOpenDisplay(dispname)) == NULL)
275 >                quiterr("cannot open display");
276 >                                /* get best visual for default screen */
277 >        getbestvis();
278                                  /* store image */
279          getras();
280 <                                /* open window */
281 <        ourwinattr.border_pixel = ourblack;
282 <        ourwinattr.background_pixel = ourwhite;
224 <        wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH,
225 <                        0, InputOutput, ourras->visual,
226 <                        CWBackPixel|CWBorderPixel, &ourwinattr);
227 <        if (wind == 0)
228 <                quiterr("can't create window");
229 <        width = xmax;
230 <        height = ymax;
231 <        fontid = XLoadFont(thedisplay, FONTNAME);
232 <        if (fontid == 0)
233 <                quiterr("can't get font");
234 <        XSetFont(thedisplay, ourgc, fontid);
235 <        revgc = XCreateGC(thedisplay, wind, 0, 0);
236 <        XSetFunction(thedisplay, revgc, GXinvert);
237 <        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
238 <        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
239 <                        XC_diamond_cross));
280 >                                /* get size and position */
281 >        xszhints.flags = 0;
282 >        xszhints.width = xmax; xszhints.height = ymax;
283          if (geometry != NULL) {
284 <                bzero((char *)&oursizhints, sizeof(oursizhints));
285 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
286 <                                &oursizhints.width, &oursizhints.height);
284 >                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
285 >                                (unsigned *)&xszhints.width,
286 >                                (unsigned *)&xszhints.height);
287                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
288 <                        oursizhints.flags |= USSize;
289 <                else {
290 <                        oursizhints.width = xmax;
248 <                        oursizhints.height = ymax;
249 <                        oursizhints.flags |= PSize;
250 <                }
288 >                        xszhints.flags |= USSize;
289 >                else
290 >                        xszhints.flags |= PSize;
291                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
292 <                        oursizhints.flags |= USPosition;
292 >                        xszhints.flags |= USPosition;
293                          if (i & XNegative)
294 <                                oursizhints.x += DisplayWidth(thedisplay,
295 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
294 >                                xszhints.x += DisplayWidth(thedisplay,
295 >                                ourscreen)-1-xszhints.width-2*BORWIDTH;
296                          if (i & YNegative)
297 <                                oursizhints.y += DisplayHeight(thedisplay,
298 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
297 >                                xszhints.y += DisplayHeight(thedisplay,
298 >                                ourscreen)-1-xszhints.height-2*BORWIDTH;
299                  }
260                XSetNormalHints(thedisplay, wind, &oursizhints);
300          }
301 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
302 <                        |ButtonMotionMask|StructureNotifyMask
303 <                        |KeyPressMask|ExposureMask);
301 >        /* open window */
302 >        ourwinattr.border_pixel = ourwhite;
303 >        ourwinattr.background_pixel = ourblack;
304 >        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
305 >                        ourvis.visual, AllocNone);
306 >        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
307 >                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
308 >        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
309 >        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
310 >                        xszhints.width, xszhints.height, BORWIDTH,
311 >                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
312 >                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
313 >        if (wind == 0)
314 >                quiterr("cannot create window");
315 >        width = xmax;
316 >        height = ymax;
317 >        /* prepare graphics drawing context */
318 >        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
319 >                quiterr("cannot get font");
320 >        xgcv.foreground = ourblack;
321 >        xgcv.background = ourwhite;
322 >        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
323 >                        GCFont, &xgcv);
324 >        xgcv.function = GXinvert;
325 >        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
326 >                        GCFunction, &xgcv);
327 >
328 >        /* set up the window manager */
329 >        xwmhints.flags = InputHint|IconPixmapHint;
330 >        xwmhints.input = True;
331 >        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
332 >                        wind, icondata, iconwidth, iconheight);
333 >
334 >        windowName.encoding = iconName.encoding = XA_STRING;
335 >        windowName.format = iconName.format = 8;
336 >        windowName.value = (u_char *)name;
337 >        windowName.nitems = strlen(windowName.value);
338 >        iconName.value = (u_char *)name;
339 >        iconName.nitems = strlen(windowName.value);
340 >
341 >        xclshints.res_name = NULL;
342 >        xclshints.res_class = "Ximage";
343 >        XSetWMProperties(thedisplay, wind, &windowName, &iconName,
344 >                        argv, argc, &xszhints, &xwmhints, &xclshints);
345 >        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
346 >        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
347 >        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
348 >
349          XMapWindow(thedisplay, wind);
350          return;
267 memerr:
268        quiterr("out of memory");
351   } /* end of init */
352  
353  
354   quiterr(err)            /* print message and exit */
355   char  *err;
356   {
357 <        if (err != NULL) {
358 <                fprintf(stderr, "%s: %s\n", progname, err);
359 <                exit(1);
357 >        register int  es;
358 >        int  cs;
359 >
360 >        if (es = err != NULL)
361 >                fprintf(stderr, "%s: %s: %s\n", progname,
362 >                                fname==NULL?"<stdin>":fname, err);
363 >        if (parent > 0 & wind != 0) {
364 >                XDestroyWindow(thedisplay, wind);
365 >                XFlush(thedisplay);
366 >        } else if (parent < 0 & sigrecv == 0)
367 >                kill(getppid(), SIGCONT);
368 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
369 >                if (es == 0)
370 >                        es = cs>>8 & 0xff;
371 >                parent--;
372          }
373 <        exit(0);
373 >        exit(es);
374   }
375  
376  
377 < eputs(s)
378 < char    *s;
377 > static int
378 > viscmp(v1,v2)           /* compare visual to see which is better, descending */
379 > register XVisualInfo    *v1, *v2;
380   {
381 <        fputs(s, stderr);
381 >        int     bad1 = 0, bad2 = 0;
382 >        register int  *rp;
383 >
384 >        if (v1->class == v2->class) {
385 >                if (v1->class == TrueColor || v1->class == DirectColor) {
386 >                                        /* prefer 24-bit to 32-bit */
387 >                        if (v1->depth == 24 && v2->depth == 32)
388 >                                return(-1);
389 >                        if (v1->depth == 32 && v2->depth == 24)
390 >                                return(1);
391 >                        return(0);
392 >                }
393 >                                        /* don't be too greedy */
394 >                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
395 >                        return(v1->depth - v2->depth);
396 >                return(v2->depth - v1->depth);
397 >        }
398 >                                        /* prefer Pseudo when < 24-bit */
399 >        if ((v1->class == TrueColor || v1->class == DirectColor) &&
400 >                        v1->depth < 24)
401 >                bad1 = 1;
402 >        if ((v2->class == TrueColor || v2->class == DirectColor) &&
403 >                        v2->depth < 24)
404 >                bad2 = -1;
405 >        if (bad1 | bad2)
406 >                return(bad1+bad2);
407 >                                        /* otherwise, use class ranking */
408 >        for (rp = ourrank; *rp != -1; rp++) {
409 >                if (v1->class == *rp)
410 >                        return(-1);
411 >                if (v2->class == *rp)
412 >                        return(1);
413 >        }
414 >        return(0);
415   }
416  
417  
418 < quit(code)
291 < int  code;
418 > getbestvis()                    /* get the best visual for this screen */
419   {
420 <        exit(code);
420 > #ifdef DEBUG
421 > static char  vistype[][12] = {
422 >                "StaticGray",
423 >                "GrayScale",
424 >                "StaticColor",
425 >                "PseudoColor",
426 >                "TrueColor",
427 >                "DirectColor"
428 > };
429 > #endif
430 >        static int      rankings[3][6] = {
431 >                {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
432 >                {PseudoColor,GrayScale,StaticGray,-1},
433 >                {PseudoColor,GrayScale,StaticGray,-1}
434 >        };
435 >        XVisualInfo     *xvi;
436 >        int     vismatched;
437 >        register int    i, j;
438 >
439 >        if (greyscale) {
440 >                ourrank = rankings[2];
441 >                if (maxcolors < 2) maxcolors = 256;
442 >        } else if (maxcolors >= 2 && maxcolors <= 256)
443 >                ourrank = rankings[1];
444 >        else {
445 >                ourrank = rankings[0];
446 >                maxcolors = 256;
447 >        }
448 >                                        /* find best visual */
449 >        ourvis.screen = ourscreen;
450 >        xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
451 >        if (xvi == NULL)
452 >                quiterr("no visuals for this screen!");
453 > #ifdef DEBUG
454 >        fprintf(stderr, "Supported visuals:\n");
455 >        for (i = 0; i < vismatched; i++)
456 >                fprintf(stderr, "\ttype %s, depth %d\n",
457 >                                vistype[xvi[i].class], xvi[i].depth);
458 > #endif
459 >        for (i = 0, j = 1; j < vismatched; j++)
460 >                if (viscmp(&xvi[i],&xvi[j]) > 0)
461 >                        i = j;
462 >                                        /* compare to least acceptable */
463 >        for (j = 0; ourrank[j++] != -1; )
464 >                ;
465 >        ourvis.class = ourrank[--j];
466 >        ourvis.depth = 1;
467 >        if (viscmp(&xvi[i],&ourvis) > 0)
468 >                quiterr("inadequate visuals on this screen");
469 >                                        /* OK, we'll use it */
470 >        copystruct(&ourvis, &xvi[i]);
471 > #ifdef DEBUG
472 >        fprintf(stderr, "Selected visual type %s, depth %d\n",
473 >                        vistype[ourvis.class], ourvis.depth);
474 > #endif
475 >                                        /* make appropriate adjustments */
476 >        if (ourvis.class == GrayScale || ourvis.class == StaticGray)
477 >                greyscale = 1;
478 >        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
479 >                maxcolors = ourvis.colormap_size;
480 >        if (ourvis.class == StaticGray) {
481 >                ourblack = 0;
482 >                ourwhite = 255;
483 >        } else if (ourvis.class == PseudoColor) {
484 >                ourblack = BlackPixel(thedisplay,ourscreen);
485 >                ourwhite = WhitePixel(thedisplay,ourscreen);
486 >                if ((ourblack|ourwhite) & ~255L) {
487 >                        ourblack = 0;
488 >                        ourwhite = 1;
489 >                }
490 >                if (maxcolors > 4)
491 >                        maxcolors -= 2;
492 >        } else {
493 >                ourblack = 0;
494 >                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
495 >        }
496 >        XFree((char *)xvi);
497   }
498  
499  
500   getras()                                /* get raster file */
501   {
299        colormap        ourmap;
502          XVisualInfo     vinfo;
503  
504          if (maxcolors <= 2) {           /* monochrome */
505                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
506                  if (ourdata == NULL)
507                          goto fail;
508 <                ourras = make_raster(thedisplay, ourscreen, 1, ourdata,
508 >                ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
509                                  xmax, ymax, 8);
510                  if (ourras == NULL)
511                          goto fail;
512                  getmono();
513 <        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)) {
514 <                ourdata = (unsigned char *)malloc(xmax*ymax*3);
513 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
514 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
515                  if (ourdata == NULL)
516                          goto fail;
517 <                ourras = make_raster(thedisplay, ourscreen, 24, ourdata,
518 <                                xmax, ymax, 8);
517 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
518 >                                ourdata, xmax, ymax, 32);
519                  if (ourras == NULL)
520                          goto fail;
521                  getfull();
# Line 321 | Line 523 | getras()                               /* get raster file */
523                  ourdata = (unsigned char *)malloc(xmax*ymax);
524                  if (ourdata == NULL)
525                          goto fail;
526 <                ourras = make_raster(thedisplay, ourscreen, 8, ourdata,
526 >                ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
527                                  xmax, ymax, 8);
528                  if (ourras == NULL)
529                          goto fail;
530 <                if (greyscale)
531 <                        biq(dither,maxcolors,1,ourmap);
530 >                if (greyscale | ourvis.class == StaticGray)
531 >                        getgrey();
532                  else
533 <                        ciq(dither,maxcolors,1,ourmap);
534 <                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
533 >                        getmapped();
534 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
535                          goto fail;
536          }
537          return;
538   fail:
539 <        quit("could not create raster image");
539 >        quiterr("could not create raster image");
540   }
541  
542  
543   getevent()                              /* process the next event */
544   {
545 <        union {
344 <                XEvent  u;
345 <                XConfigureEvent  c;
346 <                XExposeEvent  e;
347 <                XButtonPressedEvent  b;
348 <                XKeyPressedEvent  k;
349 <        } e;
545 >        XEvent xev;
546  
547 <        XNextEvent(thedisplay, &e.u);
548 <        switch (e.u.type) {
547 >        XNextEvent(thedisplay, &xev);
548 >        switch ((int)xev.type) {
549          case KeyPress:
550 <                docom(&e.k);
550 >                docom(&xev.xkey);
551                  break;
552          case ConfigureNotify:
553 <                width = e.c.width;
554 <                height = e.c.height;
553 >                width = xev.xconfigure.width;
554 >                height = xev.xconfigure.height;
555                  break;
556          case MapNotify:
557                  map_rcolors(ourras, wind);
558                  if (fast)
559 <                        make_rpixmap(ourras);
559 >                        make_rpixmap(ourras, wind);
560                  break;
561          case UnmapNotify:
562 <                unmap_rcolors(ourras);
562 >                if (!fast)
563 >                        unmap_rcolors(ourras);
564                  break;
565          case Expose:
566 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
566 >                redraw(xev.xexpose.x, xev.xexpose.y,
567 >                                xev.xexpose.width, xev.xexpose.height);
568                  break;
569          case ButtonPress:
570 <                if (e.b.state & (ShiftMask|ControlMask))
571 <                        moveimage(&e.b);
570 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
571 >                        moveimage(&xev.xbutton);
572 >                else if (xev.xbutton.button == Button2)
573 >                        traceray(xev.xbutton.x, xev.xbutton.y);
574                  else
575 <                        getbox(&e.b);
575 >                        getbox(&xev.xbutton);
576                  break;
577 +        case ClientMessage:
578 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
579 +                                (xev.xclient.data.l[0] == closedownAtom))
580 +                        quiterr(NULL);
581 +                break;
582          }
583   }
584  
585  
586 < docom(ekey)                                     /* execute command */
586 > traceray(xpos, ypos)                    /* print ray corresponding to pixel */
587 > int  xpos, ypos;
588 > {
589 >        FLOAT  hv[2];
590 >        FVECT  rorg, rdir;
591 >
592 >        if (!gotview) {         /* no view, no can do */
593 >                XBell(thedisplay, 0);
594 >                return(-1);
595 >        }
596 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
597 >        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
598 >                return(-1);
599 >        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
600 >        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
601 >        fflush(stdout);
602 >        return(0);
603 > }
604 >
605 >
606 > docom(ekey)                             /* execute command */
607   XKeyPressedEvent  *ekey;
608   {
609          char  buf[80];
# Line 386 | Line 611 | XKeyPressedEvent  *ekey;
611          XColor  cvx;
612          int  com, n;
613          double  comp;
614 <        FVECT  rorg, rdir;
614 >        FLOAT  hv[2];
615  
616          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
617          if (n == 0)
# Line 394 | Line 619 | XKeyPressedEvent  *ekey;
619          com = buf[0];
620          switch (com) {                  /* interpret command */
621          case 'q':
622 <        case CTRL(D):                           /* quit */
623 <                quit(0);
622 >        case 'Q':
623 >        case CTRL('D'):                         /* quit */
624 >                quiterr(NULL);
625          case '\n':
626          case '\r':
627          case 'l':
# Line 408 | Line 634 | XKeyPressedEvent  *ekey;
634                          sprintf(buf, "%.3f", intens(cval)/exposure);
635                          break;
636                  case 'l':                               /* luminance */
637 <                        sprintf(buf, "%.0fn", bright(cval)*683.0/exposure);
637 >                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
638                          break;
639                  case 'c':                               /* color */
640                          comp = pow(2.0, (double)scale);
# Line 434 | Line 660 | XKeyPressedEvent  *ekey;
660                  XStoreColor(thedisplay, ourras->cmap, &cvx);
661                  return(0);
662          case 'p':                               /* position */
663 <                sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
663 >                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
664 >                sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr),
665 >                                (int)(hv[1]*inpres.yr));
666                  XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
667                                          buf, strlen(buf));
668                  return(0);
669          case 't':                               /* trace */
670 <                if (!gotview) {
443 <                        XBell(thedisplay, 0);
444 <                        return(-1);
445 <                }
446 <                viewray(rorg, rdir, &ourview,
447 <                                (ekey->x-xoff+.5)/xmax,
448 <                                (ymax-1-ekey->y+yoff+.5)/ymax);
449 <                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
450 <                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
451 <                fflush(stdout);
452 <                return(0);
670 >                return(traceray(ekey->x, ekey->y));
671          case '=':                               /* adjust exposure */
672 +        case '@':                               /* adaptation level */
673                  if (avgbox(cval) == -1)
674                          return(-1);
675 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
675 >                comp = bright(cval);
676 >                if (comp < 1e-20) {
677 >                        XBell(thedisplay, 0);
678 >                        return(-1);
679 >                }
680 >                if (com == '@')
681 >                        comp = 106./exposure/
682 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
683 >                else
684 >                        comp = .5/comp;
685 >                comp = log(comp)/.69315 - scale;
686 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
687                  if (n == 0)
688                          return(0);
689                  scale_rcolors(ourras, pow(2.0, (double)n));
# Line 466 | Line 696 | XKeyPressedEvent  *ekey;
696                  free_raster(ourras);
697                  getras();
698          /* fall through */
699 <        case CTRL(R):                           /* redraw */
700 <        case CTRL(L):
699 >        case CTRL('R'):                         /* redraw */
700 >        case CTRL('L'):
701                  unmap_rcolors(ourras);
702                  XClearWindow(thedisplay, wind);
703                  map_rcolors(ourras, wind);
704                  if (fast)
705 <                        make_rpixmap(ourras);
705 >                        make_rpixmap(ourras, wind);
706                  redraw(0, 0, width, height);
707                  return(0);
708 +        case 'f':                               /* turn on fast redraw */
709 +                fast = 1;
710 +                make_rpixmap(ourras, wind);
711 +                return(0);
712 +        case 'F':                               /* turn off fast redraw */
713 +                fast = 0;
714 +                free_rpixmap(ourras);
715 +                return(0);
716 +        case '0':                               /* recenter origin */
717 +                if (xoff == 0 & yoff == 0)
718 +                        return(0);
719 +                xoff = yoff = 0;
720 +                XClearWindow(thedisplay, wind);
721 +                redraw(0, 0, width, height);
722 +                return(0);
723          case ' ':                               /* clear */
724                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
725                  return(0);
# Line 488 | Line 733 | XKeyPressedEvent  *ekey;
733   moveimage(ebut)                         /* shift the image */
734   XButtonPressedEvent  *ebut;
735   {
736 <        union {
492 <                XEvent  u;
493 <                XButtonReleasedEvent  b;
494 <                XPointerMovedEvent  m;
495 <        }  e;
736 >        XEvent  e;
737          int     mxo, myo;
738  
739 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
740 <        while (e.u.type == MotionNotify) {
741 <                mxo = e.m.x;
742 <                myo = e.m.y;
739 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
740 >        while (e.type == MotionNotify) {
741 >                mxo = e.xmotion.x;
742 >                myo = e.xmotion.y;
743                  revline(ebut->x, ebut->y, mxo, myo);
744                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
745                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
746 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
746 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
747                  revline(ebut->x, ebut->y, mxo, myo);
748                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
749                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
750          }
751 <        xoff += e.b.x - ebut->x;
752 <        yoff += e.b.y - ebut->y;
751 >        xoff += e.xbutton.x - ebut->x;
752 >        yoff += e.xbutton.y - ebut->y;
753          XClearWindow(thedisplay, wind);
754          redraw(0, 0, width, height);
755   }
# Line 517 | Line 758 | XButtonPressedEvent  *ebut;
758   getbox(ebut)                            /* get new box */
759   XButtonPressedEvent  *ebut;
760   {
761 <        union {
521 <                XEvent  u;
522 <                XButtonReleasedEvent  b;
523 <                XPointerMovedEvent  m;
524 <        }  e;
761 >        XEvent  e;
762  
763 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
764 <        while (e.u.type == MotionNotify) {
765 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
766 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
763 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
764 >        while (e.type == MotionNotify) {
765 >                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
766 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
767                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
768          }
769 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
770 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
769 >        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
770 >        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
771          if (box.xmin > ebut->x) {
772                  box.xsiz = box.xmin - ebut->x + 1;
773                  box.xmin = ebut->x;
# Line 559 | Line 796 | int  x0, y0, x1, y1;
796   avgbox(clr)                             /* average color over current box */
797   COLOR  clr;
798   {
799 +        static COLOR  lc;
800 +        static int  ll, lr, lt, lb;
801          int  left, right, top, bottom;
802          int  y;
803          double  d;
# Line 582 | Line 821 | COLOR  clr;
821                  bottom = ymax;
822          if (top >= bottom)
823                  return(-1);
824 +        if (left == ll && right == lr && top == lt && bottom == lb) {
825 +                copycolor(clr, lc);
826 +                return(0);
827 +        }
828          for (y = top; y < bottom; y++) {
829                  if (getscan(y) == -1)
830                          return(-1);
# Line 592 | Line 835 | COLOR  clr;
835          }
836          d = 1.0/((right-left)*(bottom-top));
837          scalecolor(clr, d);
838 +        ll = left; lr = right; lt = top; lb = bottom;
839 +        copycolor(lc, clr);
840          return(0);
841   }
842  
# Line 600 | Line 845 | getmono()                      /* get monochrome data */
845   {
846          register unsigned char  *dp;
847          register int    x, err;
848 <        int     y;
604 <        rgbpixel        *inline;
848 >        int     y, errp;
849          short   *cerr;
850  
851 <        if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
852 <                        || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
609 <                quit("out of memory in getmono");
851 >        if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
852 >                quiterr("out of memory in getmono");
853          dp = ourdata - 1;
854          for (y = 0; y < ymax; y++) {
855 <                picreadline3(y, inline);
855 >                getscan(y);
856 >                add2icon(y, scanline);
857 >                normcolrs(scanline, xmax, scale);
858                  err = 0;
859                  for (x = 0; x < xmax; x++) {
860                          if (!(x&7))
861                                  *++dp = 0;
862 <                        err += rgb_bright(&inline[x]) + cerr[x];
862 >                        errp = err;
863 >                        err += normbright(scanline[x]) + cerr[x];
864                          if (err > 127)
865                                  err -= 255;
866                          else
867                                  *dp |= 1<<(7-(x&07));
868 <                        cerr[x] = err >>= 1;
868 >                        err /= 3;
869 >                        cerr[x] = err + errp;
870                  }
871          }
625        free((char *)inline);
872          free((char *)cerr);
873   }
874  
875  
876 + add2icon(y, scan)               /* add a scanline to our icon data */
877 + int  y;
878 + COLR  *scan;
879 + {
880 +        static short  cerr[ICONSIZ];
881 +        static int  ynext;
882 +        static char  *dp;
883 +        COLR  clr;
884 +        register int  err;
885 +        register int    x, ti;
886 +        int  errp;
887 +
888 +        if (iconheight == 0) {          /* initialize */
889 +                if (xmax <= ICONSIZ && ymax <= ICONSIZ) {
890 +                        iconwidth = xmax;
891 +                        iconheight = ymax;
892 +                } else if (xmax > ymax) {
893 +                        iconwidth = ICONSIZ;
894 +                        iconheight = ICONSIZ*ymax/xmax;
895 +                        if (iconheight < 1)
896 +                                iconheight = 1;
897 +                } else {
898 +                        iconwidth = ICONSIZ*xmax/ymax;
899 +                        if (iconwidth < 1)
900 +                                iconwidth = 1;
901 +                        iconheight = ICONSIZ;
902 +                }
903 +                ynext = 0;
904 +                dp = icondata - 1;
905 +        }
906 +        if (y < ynext*ymax/iconheight)  /* skip this one */
907 +                return;
908 +        err = 0;
909 +        for (x = 0; x < iconwidth; x++) {
910 +                if (!(x&7))
911 +                        *++dp = 0;
912 +                errp = err;
913 +                ti = x*xmax/iconwidth;
914 +                copycolr(clr, scan[ti]);
915 +                normcolrs(clr, 1, scale);
916 +                err += normbright(clr) + cerr[x];
917 +                if (err > 127)
918 +                        err -= 255;
919 +                else
920 +                        *dp |= 1<<(x&07);
921 +                err /= 3;
922 +                cerr[x] = err + errp;
923 +        }
924 +        ynext++;
925 + }
926 +
927 +
928   getfull()                       /* get full (24-bit) data */
929   {
930          int     y;
931 +        register unsigned int4  *dp;
932 +        register int    x;
933 +                                        /* set gamma correction */
934 +        setcolrgam(gamcor);
935 +                                        /* read and convert file */
936 +        dp = (unsigned int4 *)ourdata;
937 +        for (y = 0; y < ymax; y++) {
938 +                getscan(y);
939 +                add2icon(y, scanline);
940 +                if (scale)
941 +                        shiftcolrs(scanline, xmax, scale);
942 +                colrs_gambs(scanline, xmax);
943 +                if (ourras->image->blue_mask & 1)
944 +                        for (x = 0; x < xmax; x++)
945 +                                *dp++ = scanline[x][RED] << 16 |
946 +                                        scanline[x][GRN] << 8 |
947 +                                        scanline[x][BLU] ;
948 +                else
949 +                        for (x = 0; x < xmax; x++)
950 +                                *dp++ = scanline[x][RED] |
951 +                                        scanline[x][GRN] << 8 |
952 +                                        scanline[x][BLU] << 16 ;
953 +        }
954 + }
955  
956 <        for (y = 0; y < ymax; y++)
957 <                picreadline3(y, (rgbpixel *)(ourdata+y*xmax*3));
956 >
957 > getgrey()                       /* get greyscale data */
958 > {
959 >        int     y;
960 >        register unsigned char  *dp;
961 >        register int    x;
962 >                                        /* set gamma correction */
963 >        setcolrgam(gamcor);
964 >                                        /* read and convert file */
965 >        dp = ourdata;
966 >        for (y = 0; y < ymax; y++) {
967 >                getscan(y);
968 >                add2icon(y, scanline);
969 >                if (scale)
970 >                        shiftcolrs(scanline, xmax, scale);
971 >                for (x = 0; x < xmax; x++)
972 >                        scanline[x][GRN] = normbright(scanline[x]);
973 >                colrs_gambs(scanline, xmax);
974 >                if (maxcolors < 256)
975 >                        for (x = 0; x < xmax; x++)
976 >                                *dp++ = ((long)scanline[x][GRN] *
977 >                                        maxcolors + maxcolors/2) >> 8;
978 >                else
979 >                        for (x = 0; x < xmax; x++)
980 >                                *dp++ = scanline[x][GRN];
981 >        }
982 >        for (x = 0; x < maxcolors; x++)
983 >                clrtab[x][RED] = clrtab[x][GRN] =
984 >                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
985   }
986  
987  
988 + getmapped()                     /* get color-mapped data */
989 + {
990 +        int     y;
991 +                                        /* make sure we can do it first */
992 +        if (fname == NULL)
993 +                quiterr("cannot map colors from standard input");
994 +                                        /* set gamma correction */
995 +        setcolrgam(gamcor);
996 +                                        /* make histogram */
997 +        new_histo();
998 +        for (y = 0; y < ymax; y++) {
999 +                if (getscan(y) < 0)
1000 +                        break;
1001 +                add2icon(y, scanline);
1002 +                if (scale)
1003 +                        shiftcolrs(scanline, xmax, scale);
1004 +                colrs_gambs(scanline, xmax);
1005 +                cnt_colrs(scanline, xmax);
1006 +        }
1007 +                                        /* map pixels */
1008 +        if (!new_clrtab(maxcolors))
1009 +                quiterr("cannot create color map");
1010 +        for (y = 0; y < ymax; y++) {
1011 +                getscan(y);
1012 +                if (scale)
1013 +                        shiftcolrs(scanline, xmax, scale);
1014 +                colrs_gambs(scanline, xmax);
1015 +                if (dither)
1016 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
1017 +                else
1018 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
1019 +        }
1020 + }
1021 +
1022 +
1023   scale_rcolors(xr, sf)                   /* scale color map */
1024   register XRASTER        *xr;
1025   double  sf;
# Line 667 | Line 1051 | double sf;
1051   getscan(y)
1052   int  y;
1053   {
1054 +        static int  trunced = -1;               /* truncated file? */
1055 + skipit:
1056 +        if (trunced >= 0 && y >= trunced) {
1057 +                bzero(scanline, xmax*sizeof(COLR));
1058 +                return(-1);
1059 +        }
1060          if (y != cury) {
1061                  if (scanpos == NULL || scanpos[y] == -1)
1062                          return(-1);
1063                  if (fseek(fin, scanpos[y], 0) == -1)
1064 <                        quit("fseek error");
1064 >                        quiterr("fseek error");
1065                  cury = y;
1066 <        } else if (scanpos != NULL)
1066 >        } else if (scanpos != NULL && scanpos[y] == -1)
1067                  scanpos[y] = ftell(fin);
1068  
1069 <        if (freadcolrs(scanline, xmax, fin) < 0)
1070 <                quiterr("read error");
1071 <
1069 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1070 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1071 >                                progname, fname==NULL?"<stdin>":fname);
1072 >                trunced = y;
1073 >                goto skipit;
1074 >        }
1075          cury++;
1076          return(0);
684 }
685
686
687 picreadline3(y, l3)                     /* read in 3-byte scanline */
688 int  y;
689 register rgbpixel  *l3;
690 {
691        register int    i;
692                                                        /* read scanline */
693        if (getscan(y) < 0)
694                quiterr("cannot seek for picreadline");
695                                                        /* convert scanline */
696        normcolrs(scanline, xmax, scale);
697        for (i = 0; i < xmax; i++) {
698                l3[i].r = scanline[i][RED];
699                l3[i].g = scanline[i][GRN];
700                l3[i].b = scanline[i][BLU];
701        }
702 }
703
704
705 picwriteline(y, l)              /* add 8-bit scanline to image */
706 int  y;
707 pixel  *l;
708 {
709        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
710 }
711
712
713 picreadcm(map)                  /* do gamma correction */
714 colormap  map;
715 {
716        extern double  pow();
717        register int  i, val;
718
719        for (i = 0; i < 256; i++) {
720                val = pow(i/256.0, 1.0/gamcor) * 256.0;
721                map[0][i] = map[1][i] = map[2][i] = val;
722        }
723 }
724
725
726 picwritecm(map)                 /* handled elsewhere */
727 colormap  map;
728 {
729 #ifdef DEBUG
730        register int i;
731
732        for (i = 0; i < 256; i++)
733                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
734 #endif
1077   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines