ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.39
Committed: Mon Nov 21 09:18:48 1994 UTC (29 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.38: +4 -5 lines
Log Message:
made child notify parent only after window is mapped

File Contents

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