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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines