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.19 by greg, Fri May 3 09:54:31 1991 UTC vs.
Revision 2.41 by greg, Wed Jan 25 10:53:02 1995 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines