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.24 by greg, Thu May 23 12:00:38 1991 UTC vs.
Revision 2.32 by greg, Thu Oct 28 11:39:59 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)
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  
# Line 54 | 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 */
58 < 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 65 | 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 81 | Line 91 | double  exposure = 1.0;                        /* exposure compensation use
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 99 | Line 112 | 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 129 | 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 140 | 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 153 | 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);
201 >                        sprintf(errmsg, "cannot open file \"%s\"", fname);
202                          quiterr(errmsg);
203                  }
204 <        } else if (i != argc)
164 <                goto userr;
204 >        }
205                                  /* get header */
206          getheader(fin, headline, NULL);
207                                  /* get picture dimensions */
208 <        if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
208 >        if (wrongformat || !fgetsresolu(&inpres, fin))
209                  quiterr("bad picture format");
210 +        xmax = scanlen(&inpres);
211 +        ymax = numscans(&inpres);
212                                  /* set view parameters */
213          if (gotview && setview(&ourview) != NULL)
214                  gotview = 0;
215          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
216                  quiterr("out of memory");
217  
218 <        init();                 /* get file and open window */
218 >        init(argc, argv);                       /* get file and open window */
219  
220 +        if (parent < 0) {
221 +                kill(getppid(), SIGCONT);       /* signal parent if child */
222 +                sigrecv--;
223 +        }
224          for ( ; ; )
225                  getevent();             /* main loop */
226   userr:
227          fprintf(stderr,
228 <        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
228 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic ..\n",
229                          progname);
230 <        quit(1);
230 >        exit(1);
231   }
232  
233  
234   headline(s)             /* get relevant info from header */
235   char  *s;
236   {
191        static char  *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
192        register char  **an;
237          char  fmt[32];
238  
239          if (isexpos(s))
# Line 197 | Line 241 | char  *s;
241          else if (isformat(s)) {
242                  formatval(fmt, s);
243                  wrongformat = strcmp(fmt, COLRFMT);
244 <        } else
245 <                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 <                        }
244 >        } else if (isview(s) && sscanview(&ourview, s) > 0)
245 >                gotview++;
246   }
247  
248  
249 < init()                  /* get data and open window */
249 > init(argc, argv)                        /* get data and open window */
250 > int argc;
251 > char **argv;
252   {
212        XWMHints        ourxwmhints;
253          XSetWindowAttributes    ourwinattr;
254 <        XSizeHints  oursizhints;
255 <        register int  i;
254 >        XClassHint      xclshints;
255 >        XWMHints        xwmhints;
256 >        XSizeHints      xszhints;
257 >        XTextProperty   windowName, iconName;
258 >        XGCValues       xgcv;
259 >        char    *name;
260 >        register int    i;
261          
262          if (fname != NULL) {
263                  scanpos = (long *)malloc(ymax*sizeof(long));
264                  if (scanpos == NULL)
265 <                        goto memerr;
265 >                        quiterr("out of memory");
266                  for (i = 0; i < ymax; i++)
267                          scanpos[i] = -1;
268 <        }
269 <        if ((thedisplay = XOpenDisplay(NULL)) == NULL)
270 <                quiterr("can't open display; DISPLAY variable set?");
271 <        if (maxcolors == 0) {           /* get number of available colors */
272 <                i = DisplayPlanes(thedisplay,ourscreen);
273 <                maxcolors = i > 8 ? 256 : 1<<i;
274 <                if (maxcolors > 4) maxcolors -= 2;
275 <        }
268 >                name = fname;
269 >        } else
270 >                name = progname;
271 >                                /* remove directory prefix from name */
272 >        for (i = strlen(name); i-- > 0; )
273 >                if (name[i] == '/')
274 >                        break;
275 >        name += i+1;
276 >        if ((thedisplay = XOpenDisplay(dispname)) == NULL)
277 >                quiterr("cannot open display");
278 >                                /* get best visual for default screen */
279 >        getbestvis();
280                                  /* store image */
281          getras();
282 <                                /* open window */
283 <        ourwinattr.border_pixel = ourblack;
284 <        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));
251 <        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
282 >                                /* get size and position */
283 >        xszhints.flags = 0;
284 >        xszhints.width = xmax; xszhints.height = ymax;
285          if (geometry != NULL) {
286 <                bzero((char *)&oursizhints, sizeof(oursizhints));
287 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
288 <                                (unsigned *)&oursizhints.width,
256 <                                (unsigned *)&oursizhints.height);
286 >                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
287 >                                (unsigned *)&xszhints.width,
288 >                                (unsigned *)&xszhints.height);
289                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
290 <                        oursizhints.flags |= USSize;
291 <                else {
292 <                        oursizhints.width = xmax;
261 <                        oursizhints.height = ymax;
262 <                        oursizhints.flags |= PSize;
263 <                }
290 >                        xszhints.flags |= USSize;
291 >                else
292 >                        xszhints.flags |= PSize;
293                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
294 <                        oursizhints.flags |= USPosition;
294 >                        xszhints.flags |= USPosition;
295                          if (i & XNegative)
296 <                                oursizhints.x += DisplayWidth(thedisplay,
297 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
296 >                                xszhints.x += DisplayWidth(thedisplay,
297 >                                ourscreen)-1-xszhints.width-2*BORWIDTH;
298                          if (i & YNegative)
299 <                                oursizhints.y += DisplayHeight(thedisplay,
300 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
299 >                                xszhints.y += DisplayHeight(thedisplay,
300 >                                ourscreen)-1-xszhints.height-2*BORWIDTH;
301                  }
273                XSetNormalHints(thedisplay, wind, &oursizhints);
302          }
303 <        ourxwmhints.flags = InputHint|IconPixmapHint;
304 <        ourxwmhints.input = True;
305 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
303 >        /* open window */
304 >        ourwinattr.border_pixel = ourwhite;
305 >        ourwinattr.background_pixel = ourblack;
306 >        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
307 >                        ourvis.visual, AllocNone);
308 >        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
309 >                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
310 >        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
311 >        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
312 >                        xszhints.width, xszhints.height, BORWIDTH,
313 >                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
314 >                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
315 >        if (wind == 0)
316 >                quiterr("cannot create window");
317 >        width = xmax;
318 >        height = ymax;
319 >        /* prepare graphics drawing context */
320 >        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
321 >                quiterr("cannot get font");
322 >        xgcv.foreground = ourblack;
323 >        xgcv.background = ourwhite;
324 >        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
325 >                        GCFont, &xgcv);
326 >        xgcv.function = GXinvert;
327 >        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
328 >                        GCFunction, &xgcv);
329 >
330 >        /* set up the window manager */
331 >        xwmhints.flags = InputHint|IconPixmapHint;
332 >        xwmhints.input = True;
333 >        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
334                          wind, icondata, iconwidth, iconheight);
335 <        XSetWMHints(thedisplay, wind, &ourxwmhints);
336 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
337 <                        |ButtonMotionMask|StructureNotifyMask
338 <                        |KeyPressMask|ExposureMask);
335 >
336 >        windowName.encoding = iconName.encoding = XA_STRING;
337 >        windowName.format = iconName.format = 8;
338 >        windowName.value = (u_char *)name;
339 >        windowName.nitems = strlen(windowName.value);
340 >        iconName.value = (u_char *)name;
341 >        iconName.nitems = strlen(windowName.value);
342 >
343 >        xclshints.res_name = NULL;
344 >        xclshints.res_class = "Ximage";
345 >        XSetWMProperties(thedisplay, wind, &windowName, &iconName,
346 >                        argv, argc, &xszhints, &xwmhints, &xclshints);
347 >        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
348 >        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
349 >        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
350 >
351          XMapWindow(thedisplay, wind);
352          return;
285 memerr:
286        quiterr("out of memory");
353   } /* end of init */
354  
355  
356   quiterr(err)            /* print message and exit */
357   char  *err;
358   {
359 <        if (err != NULL) {
360 <                fprintf(stderr, "%s: %s\n", progname, err);
361 <                exit(1);
359 >        register int  es;
360 >        int  cs;
361 >
362 >        if (es = err != NULL)
363 >                fprintf(stderr, "%s: %s: %s\n", progname,
364 >                                fname==NULL?"<stdin>":fname, err);
365 >        if (parent > 0 & wind != 0) {
366 >                XDestroyWindow(thedisplay, wind);
367 >                XFlush(thedisplay);
368 >        } else if (parent < 0 & sigrecv == 0)
369 >                kill(getppid(), SIGCONT);
370 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
371 >                if (es == 0)
372 >                        es = cs>>8 & 0xff;
373 >                parent--;
374          }
375 <        exit(0);
375 >        exit(es);
376   }
377  
378  
379 < eputs(s)
380 < char    *s;
379 > static int
380 > viscmp(v1,v2)           /* compare visual to see which is better, descending */
381 > register XVisualInfo    *v1, *v2;
382   {
383 <        fputs(s, stderr);
383 >        int     bad1 = 0, bad2 = 0;
384 >        register int  *rp;
385 >
386 >        if (v1->class == v2->class) {
387 >                if (v1->class == TrueColor || v1->class == DirectColor) {
388 >                                        /* prefer 24-bit to 32-bit */
389 >                        if (v1->depth == 24 && v2->depth == 32)
390 >                                return(-1);
391 >                        if (v1->depth == 32 && v2->depth == 24)
392 >                                return(1);
393 >                        return(0);
394 >                }
395 >                                        /* don't be too greedy */
396 >                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
397 >                        return(v1->depth - v2->depth);
398 >                return(v2->depth - v1->depth);
399 >        }
400 >                                        /* prefer Pseudo when < 24-bit */
401 >        if ((v1->class == TrueColor || v1->class == DirectColor) &&
402 >                        v1->depth < 24)
403 >                bad1 = 1;
404 >        if ((v2->class == TrueColor || v2->class == DirectColor) &&
405 >                        v2->depth < 24)
406 >                bad2 = -1;
407 >        if (bad1 | bad2)
408 >                return(bad1+bad2);
409 >                                        /* otherwise, use class ranking */
410 >        for (rp = ourrank; *rp != -1; rp++) {
411 >                if (v1->class == *rp)
412 >                        return(-1);
413 >                if (v2->class == *rp)
414 >                        return(1);
415 >        }
416 >        return(0);
417   }
418  
419  
420 < quit(code)
309 < int  code;
420 > getbestvis()                    /* get the best visual for this screen */
421   {
422 <        exit(code);
422 > #ifdef DEBUG
423 > static char  vistype[][12] = {
424 >                "StaticGray",
425 >                "GrayScale",
426 >                "StaticColor",
427 >                "PseudoColor",
428 >                "TrueColor",
429 >                "DirectColor"
430 > };
431 > #endif
432 >        static int      rankings[3][6] = {
433 >                {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
434 >                {PseudoColor,GrayScale,StaticGray,-1},
435 >                {PseudoColor,GrayScale,StaticGray,-1}
436 >        };
437 >        XVisualInfo     *xvi;
438 >        int     vismatched;
439 >        register int    i, j;
440 >
441 >        if (greyscale) {
442 >                ourrank = rankings[2];
443 >                if (maxcolors < 2) maxcolors = 256;
444 >        } else if (maxcolors >= 2 && maxcolors <= 256)
445 >                ourrank = rankings[1];
446 >        else {
447 >                ourrank = rankings[0];
448 >                maxcolors = 256;
449 >        }
450 >                                        /* find best visual */
451 >        ourvis.screen = ourscreen;
452 >        xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
453 >        if (xvi == NULL)
454 >                quiterr("no visuals for this screen!");
455 > #ifdef DEBUG
456 >        fprintf(stderr, "Supported visuals:\n");
457 >        for (i = 0; i < vismatched; i++)
458 >                fprintf(stderr, "\ttype %s, depth %d\n",
459 >                                vistype[xvi[i].class], xvi[i].depth);
460 > #endif
461 >        for (i = 0, j = 1; j < vismatched; j++)
462 >                if (viscmp(&xvi[i],&xvi[j]) > 0)
463 >                        i = j;
464 >                                        /* compare to least acceptable */
465 >        for (j = 0; ourrank[j++] != -1; )
466 >                ;
467 >        ourvis.class = ourrank[--j];
468 >        ourvis.depth = 1;
469 >        if (viscmp(&xvi[i],&ourvis) > 0)
470 >                quiterr("inadequate visuals on this screen");
471 >                                        /* OK, we'll use it */
472 >        copystruct(&ourvis, &xvi[i]);
473 > #ifdef DEBUG
474 >        fprintf(stderr, "Selected visual type %s, depth %d\n",
475 >                        vistype[ourvis.class], ourvis.depth);
476 > #endif
477 >                                        /* make appropriate adjustments */
478 >        if (ourvis.class == GrayScale || ourvis.class == StaticGray)
479 >                greyscale = 1;
480 >        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
481 >                maxcolors = ourvis.colormap_size;
482 >        if (ourvis.class == StaticGray) {
483 >                ourblack = 0;
484 >                ourwhite = 255;
485 >        } else if (ourvis.class == PseudoColor) {
486 >                ourblack = BlackPixel(thedisplay,ourscreen);
487 >                ourwhite = WhitePixel(thedisplay,ourscreen);
488 >                if ((ourblack|ourwhite) & ~255L) {
489 >                        ourblack = 0;
490 >                        ourwhite = 1;
491 >                }
492 >                if (maxcolors > 4)
493 >                        maxcolors -= 2;
494 >        } else {
495 >                ourblack = 0;
496 >                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
497 >        }
498 >        XFree((char *)xvi);
499   }
500  
501  
502   getras()                                /* get raster file */
503   {
317        colormap        ourmap;
504          XVisualInfo     vinfo;
505  
506          if (maxcolors <= 2) {           /* monochrome */
507                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
508                  if (ourdata == NULL)
509                          goto fail;
510 <                ourras = make_raster(thedisplay, ourscreen, 1, ourdata,
510 >                ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
511                                  xmax, ymax, 8);
512                  if (ourras == NULL)
513                          goto fail;
514                  getmono();
515 <        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)
516 <                                                /* kludge for DirectColor */
331 <        || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) {
332 <                ourdata = (unsigned char *)malloc(xmax*ymax*3);
515 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
516 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
517                  if (ourdata == NULL)
518                          goto fail;
519 <                ourras = make_raster(thedisplay, ourscreen, 24, ourdata,
520 <                                xmax, ymax, 8);
519 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
520 >                                ourdata, xmax, ymax, 32);
521                  if (ourras == NULL)
522                          goto fail;
523                  getfull();
# Line 341 | Line 525 | getras()                               /* get raster file */
525                  ourdata = (unsigned char *)malloc(xmax*ymax);
526                  if (ourdata == NULL)
527                          goto fail;
528 <                ourras = make_raster(thedisplay, ourscreen, 8, ourdata,
528 >                ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
529                                  xmax, ymax, 8);
530                  if (ourras == NULL)
531                          goto fail;
532 <                if (greyscale)
533 <                        biq(dither,maxcolors,1,ourmap);
532 >                if (greyscale | ourvis.class == StaticGray)
533 >                        getgrey();
534                  else
535 <                        ciq(dither,maxcolors,1,ourmap);
536 <                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
535 >                        getmapped();
536 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
537                          goto fail;
538          }
539          return;
# Line 360 | Line 544 | fail:
544  
545   getevent()                              /* process the next event */
546   {
547 <        union {
364 <                XEvent  u;
365 <                XConfigureEvent  c;
366 <                XExposeEvent  e;
367 <                XButtonPressedEvent  b;
368 <                XKeyPressedEvent  k;
369 <        } e;
547 >        XEvent xev;
548  
549 <        XNextEvent(thedisplay, &e.u);
550 <        switch (e.u.type) {
549 >        XNextEvent(thedisplay, &xev);
550 >        switch ((int)xev.type) {
551          case KeyPress:
552 <                docom(&e.k);
552 >                docom(&xev.xkey);
553                  break;
554          case ConfigureNotify:
555 <                width = e.c.width;
556 <                height = e.c.height;
555 >                width = xev.xconfigure.width;
556 >                height = xev.xconfigure.height;
557                  break;
558          case MapNotify:
559                  map_rcolors(ourras, wind);
560                  if (fast)
561 <                        make_rpixmap(ourras);
561 >                        make_rpixmap(ourras, wind);
562                  break;
563          case UnmapNotify:
564 <                unmap_rcolors(ourras);
564 >                if (!fast)
565 >                        unmap_rcolors(ourras);
566                  break;
567          case Expose:
568 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
568 >                redraw(xev.xexpose.x, xev.xexpose.y,
569 >                                xev.xexpose.width, xev.xexpose.height);
570                  break;
571          case ButtonPress:
572 <                if (e.b.state & (ShiftMask|ControlMask))
573 <                        moveimage(&e.b);
572 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
573 >                        moveimage(&xev.xbutton);
574 >                else if (xev.xbutton.button == Button2)
575 >                        traceray(xev.xbutton.x, xev.xbutton.y);
576                  else
577 <                        getbox(&e.b);
577 >                        getbox(&xev.xbutton);
578                  break;
579 +        case ClientMessage:
580 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
581 +                                (xev.xclient.data.l[0] == closedownAtom))
582 +                        quiterr(NULL);
583 +                break;
584          }
585   }
586  
587  
588 < docom(ekey)                                     /* execute command */
588 > traceray(xpos, ypos)                    /* print ray corresponding to pixel */
589 > int  xpos, ypos;
590 > {
591 >        FLOAT  hv[2];
592 >        FVECT  rorg, rdir;
593 >
594 >        if (!gotview) {         /* no view, no can do */
595 >                XBell(thedisplay, 0);
596 >                return(-1);
597 >        }
598 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
599 >        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
600 >                return(-1);
601 >        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
602 >        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
603 >        fflush(stdout);
604 >        return(0);
605 > }
606 >
607 >
608 > docom(ekey)                             /* execute command */
609   XKeyPressedEvent  *ekey;
610   {
611          char  buf[80];
# Line 406 | Line 613 | XKeyPressedEvent  *ekey;
613          XColor  cvx;
614          int  com, n;
615          double  comp;
616 <        FVECT  rorg, rdir;
616 >        FLOAT  hv[2];
617  
618          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
619          if (n == 0)
# Line 414 | Line 621 | XKeyPressedEvent  *ekey;
621          com = buf[0];
622          switch (com) {                  /* interpret command */
623          case 'q':
624 <        case CTRL(D):                           /* quit */
625 <                quit(0);
624 >        case 'Q':
625 >        case CTRL('D'):                         /* quit */
626 >                quiterr(NULL);
627          case '\n':
628          case '\r':
629          case 'l':
# Line 454 | Line 662 | XKeyPressedEvent  *ekey;
662                  XStoreColor(thedisplay, ourras->cmap, &cvx);
663                  return(0);
664          case 'p':                               /* position */
665 <                sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
665 >                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
666 >                sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr),
667 >                                (int)(hv[1]*inpres.yr));
668                  XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
669                                          buf, strlen(buf));
670                  return(0);
671          case 't':                               /* trace */
672 <                if (!gotview) {
463 <                        XBell(thedisplay, 0);
464 <                        return(-1);
465 <                }
466 <                if (viewray(rorg, rdir, &ourview,
467 <                                (ekey->x-xoff+.5)/xmax,
468 <                                (ymax-1-ekey->y+yoff+.5)/ymax) < 0)
469 <                        return(-1);
470 <                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
471 <                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
472 <                fflush(stdout);
473 <                return(0);
672 >                return(traceray(ekey->x, ekey->y));
673          case '=':                               /* adjust exposure */
674 +        case '@':                               /* adaptation level */
675                  if (avgbox(cval) == -1)
676                          return(-1);
677 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
677 >                comp = com=='@'
678 >                ? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure
679 >                : .5/bright(cval) ;
680 >                comp = log(comp)/.69315 - scale;
681 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
682                  if (n == 0)
683                          return(0);
684                  scale_rcolors(ourras, pow(2.0, (double)n));
# Line 487 | Line 691 | XKeyPressedEvent  *ekey;
691                  free_raster(ourras);
692                  getras();
693          /* fall through */
694 <        case CTRL(R):                           /* redraw */
695 <        case CTRL(L):
694 >        case CTRL('R'):                         /* redraw */
695 >        case CTRL('L'):
696                  unmap_rcolors(ourras);
697                  XClearWindow(thedisplay, wind);
698                  map_rcolors(ourras, wind);
699                  if (fast)
700 <                        make_rpixmap(ourras);
700 >                        make_rpixmap(ourras, wind);
701                  redraw(0, 0, width, height);
702                  return(0);
703 +        case 'f':                               /* turn on fast redraw */
704 +                fast = 1;
705 +                make_rpixmap(ourras, wind);
706 +                return(0);
707 +        case 'F':                               /* turn off fast redraw */
708 +                fast = 0;
709 +                free_rpixmap(ourras);
710 +                return(0);
711 +        case '0':                               /* recenter origin */
712 +                if (xoff == 0 & yoff == 0)
713 +                        return(0);
714 +                xoff = yoff = 0;
715 +                XClearWindow(thedisplay, wind);
716 +                redraw(0, 0, width, height);
717 +                return(0);
718          case ' ':                               /* clear */
719                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
720                  return(0);
# Line 509 | Line 728 | XKeyPressedEvent  *ekey;
728   moveimage(ebut)                         /* shift the image */
729   XButtonPressedEvent  *ebut;
730   {
731 <        union {
513 <                XEvent  u;
514 <                XButtonReleasedEvent  b;
515 <                XPointerMovedEvent  m;
516 <        }  e;
731 >        XEvent  e;
732          int     mxo, myo;
733  
734 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
735 <        while (e.u.type == MotionNotify) {
736 <                mxo = e.m.x;
737 <                myo = e.m.y;
734 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
735 >        while (e.type == MotionNotify) {
736 >                mxo = e.xmotion.x;
737 >                myo = e.xmotion.y;
738                  revline(ebut->x, ebut->y, mxo, myo);
739                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
740                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
741 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
741 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
742                  revline(ebut->x, ebut->y, mxo, myo);
743                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
744                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
745          }
746 <        xoff += e.b.x - ebut->x;
747 <        yoff += e.b.y - ebut->y;
746 >        xoff += e.xbutton.x - ebut->x;
747 >        yoff += e.xbutton.y - ebut->y;
748          XClearWindow(thedisplay, wind);
749          redraw(0, 0, width, height);
750   }
# Line 538 | Line 753 | XButtonPressedEvent  *ebut;
753   getbox(ebut)                            /* get new box */
754   XButtonPressedEvent  *ebut;
755   {
756 <        union {
542 <                XEvent  u;
543 <                XButtonReleasedEvent  b;
544 <                XPointerMovedEvent  m;
545 <        }  e;
756 >        XEvent  e;
757  
758 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
759 <        while (e.u.type == MotionNotify) {
760 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
761 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
758 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
759 >        while (e.type == MotionNotify) {
760 >                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
761 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
762                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
763          }
764 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
765 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
764 >        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
765 >        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
766          if (box.xmin > ebut->x) {
767                  box.xsiz = box.xmin - ebut->x + 1;
768                  box.xmin = ebut->x;
# Line 580 | Line 791 | int  x0, y0, x1, y1;
791   avgbox(clr)                             /* average color over current box */
792   COLOR  clr;
793   {
794 +        static COLOR  lc;
795 +        static int  ll, lr, lt, lb;
796          int  left, right, top, bottom;
797          int  y;
798          double  d;
# Line 603 | Line 816 | COLOR  clr;
816                  bottom = ymax;
817          if (top >= bottom)
818                  return(-1);
819 +        if (left == ll && right == lr && top == lt && bottom == lb) {
820 +                copycolor(clr, lc);
821 +                return(0);
822 +        }
823          for (y = top; y < bottom; y++) {
824                  if (getscan(y) == -1)
825                          return(-1);
# Line 613 | Line 830 | COLOR  clr;
830          }
831          d = 1.0/((right-left)*(bottom-top));
832          scalecolor(clr, d);
833 +        ll = left; lr = right; lt = top; lb = bottom;
834 +        copycolor(lc, clr);
835          return(0);
836   }
837  
# Line 628 | Line 847 | getmono()                      /* get monochrome data */
847                  quiterr("out of memory in getmono");
848          dp = ourdata - 1;
849          for (y = 0; y < ymax; y++) {
850 <                if (getscan(y) < 0)
632 <                        quiterr("seek error in getmono");
633 <                normcolrs(scanline, xmax, scale);
850 >                getscan(y);
851                  add2icon(y, scanline);
852 +                normcolrs(scanline, xmax, scale);
853                  err = 0;
854                  for (x = 0; x < xmax; x++) {
855                          if (!(x&7))
# Line 657 | Line 875 | COLR  *scan;
875          static short  cerr[ICONSIZ];
876          static int  ynext;
877          static char  *dp;
878 +        COLR  clr;
879          register int  err;
880          register int    x, ti;
881          int  errp;
# Line 687 | Line 906 | COLR  *scan;
906                          *++dp = 0;
907                  errp = err;
908                  ti = x*xmax/iconwidth;
909 <                err += normbright(scan[ti]) + cerr[x];
909 >                copycolr(clr, scan[ti]);
910 >                normcolrs(clr, 1, scale);
911 >                err += normbright(clr) + cerr[x];
912                  if (err > 127)
913                          err -= 255;
914                  else
# Line 702 | Line 923 | COLR  *scan;
923   getfull()                       /* get full (24-bit) data */
924   {
925          int     y;
926 +        register unsigned int4  *dp;
927 +        register int    x;
928 +                                        /* set gamma correction */
929 +        setcolrgam(gamcor);
930 +                                        /* read and convert file */
931 +        dp = (unsigned int4 *)ourdata;
932 +        for (y = 0; y < ymax; y++) {
933 +                getscan(y);
934 +                add2icon(y, scanline);
935 +                if (scale)
936 +                        shiftcolrs(scanline, xmax, scale);
937 +                colrs_gambs(scanline, xmax);
938 +                if (ourras->image->blue_mask & 1)
939 +                        for (x = 0; x < xmax; x++)
940 +                                *dp++ = scanline[x][RED] << 16 |
941 +                                        scanline[x][GRN] << 8 |
942 +                                        scanline[x][BLU] ;
943 +                else
944 +                        for (x = 0; x < xmax; x++)
945 +                                *dp++ = scanline[x][RED] |
946 +                                        scanline[x][GRN] << 8 |
947 +                                        scanline[x][BLU] << 16 ;
948 +        }
949 + }
950 +
951 +
952 + getgrey()                       /* get greyscale data */
953 + {
954 +        int     y;
955          register unsigned char  *dp;
956          register int    x;
957                                          /* set gamma correction */
# Line 709 | Line 959 | getfull()                      /* get full (24-bit) data */
959                                          /* read and convert file */
960          dp = ourdata;
961          for (y = 0; y < ymax; y++) {
962 <                if (getscan(y) < 0)
963 <                        quiterr("seek error in getfull");
962 >                getscan(y);
963 >                add2icon(y, scanline);
964                  if (scale)
965                          shiftcolrs(scanline, xmax, scale);
966 +                for (x = 0; x < xmax; x++)
967 +                        scanline[x][GRN] = normbright(scanline[x]);
968                  colrs_gambs(scanline, xmax);
969 +                if (maxcolors < 256)
970 +                        for (x = 0; x < xmax; x++)
971 +                                *dp++ = ((long)scanline[x][GRN] *
972 +                                        maxcolors + maxcolors/2) >> 8;
973 +                else
974 +                        for (x = 0; x < xmax; x++)
975 +                                *dp++ = scanline[x][GRN];
976 +        }
977 +        for (x = 0; x < maxcolors; x++)
978 +                clrtab[x][RED] = clrtab[x][GRN] =
979 +                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
980 + }
981 +
982 +
983 + getmapped()                     /* get color-mapped data */
984 + {
985 +        int     y;
986 +                                        /* make sure we can do it first */
987 +        if (fname == NULL)
988 +                quiterr("cannot map colors from standard input");
989 +                                        /* set gamma correction */
990 +        setcolrgam(gamcor);
991 +                                        /* make histogram */
992 +        new_histo();
993 +        for (y = 0; y < ymax; y++) {
994 +                if (getscan(y) < 0)
995 +                        break;
996                  add2icon(y, scanline);
997 <                for (x = 0; x < xmax; x++) {
998 <                        *dp++ = scanline[x][RED];
999 <                        *dp++ = scanline[x][GRN];
1000 <                        *dp++ = scanline[x][BLU];
722 <                }
997 >                if (scale)
998 >                        shiftcolrs(scanline, xmax, scale);
999 >                colrs_gambs(scanline, xmax);
1000 >                cnt_colrs(scanline, xmax);
1001          }
1002 +                                        /* map pixels */
1003 +        if (!new_clrtab(maxcolors))
1004 +                quiterr("cannot create color map");
1005 +        for (y = 0; y < ymax; y++) {
1006 +                getscan(y);
1007 +                if (scale)
1008 +                        shiftcolrs(scanline, xmax, scale);
1009 +                colrs_gambs(scanline, xmax);
1010 +                if (dither)
1011 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
1012 +                else
1013 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
1014 +        }
1015   }
1016  
1017  
# Line 755 | Line 1046 | double sf;
1046   getscan(y)
1047   int  y;
1048   {
1049 +        static int  trunced = -1;               /* truncated file? */
1050 + skipit:
1051 +        if (trunced >= 0 && y >= trunced) {
1052 +                bzero(scanline, xmax*sizeof(COLR));
1053 +                return(-1);
1054 +        }
1055          if (y != cury) {
1056                  if (scanpos == NULL || scanpos[y] == -1)
1057                          return(-1);
1058                  if (fseek(fin, scanpos[y], 0) == -1)
1059                          quiterr("fseek error");
1060                  cury = y;
1061 <        } else if (scanpos != NULL)
1061 >        } else if (scanpos != NULL && scanpos[y] == -1)
1062                  scanpos[y] = ftell(fin);
1063  
1064 <        if (freadcolrs(scanline, xmax, fin) < 0)
1065 <                quiterr("read error");
1066 <
1064 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1065 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1066 >                                progname, fname==NULL?"<stdin>":fname);
1067 >                trunced = y;
1068 >                goto skipit;
1069 >        }
1070          cury++;
1071          return(0);
772 }
773
774
775 picreadline3(y, l3)                     /* read in 3-byte scanline */
776 int  y;
777 register rgbpixel  *l3;
778 {
779        register int    i;
780                                                        /* read scanline */
781        if (getscan(y) < 0)
782                quiterr("cannot seek for picreadline");
783                                                        /* convert scanline */
784        normcolrs(scanline, xmax, scale);
785        add2icon(y, scanline);
786        for (i = 0; i < xmax; i++) {
787                l3[i].r = scanline[i][RED];
788                l3[i].g = scanline[i][GRN];
789                l3[i].b = scanline[i][BLU];
790        }
791 }
792
793
794 picwriteline(y, l)              /* add 8-bit scanline to image */
795 int  y;
796 pixel  *l;
797 {
798        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
799 }
800
801
802 picreadcm(map)                  /* do gamma correction */
803 colormap  map;
804 {
805        extern double  pow();
806        register int  i, val;
807
808        for (i = 0; i < 256; i++) {
809                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
810                map[0][i] = map[1][i] = map[2][i] = val;
811        }
812 }
813
814
815 picwritecm(map)                 /* handled elsewhere */
816 colormap  map;
817 {
818 #ifdef DEBUG
819        register int i;
820
821        for (i = 0; i < 256; i++)
822                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
823 #endif
1072   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines