ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.41
Committed: Wed Jan 25 10:53:02 1995 UTC (29 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.40: +7 -3 lines
Log Message:
added -s option for sequential viewing

File Contents

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