ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.31
Committed: Thu Oct 28 10:00:58 1993 UTC (30 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.30: +10 -4 lines
Log Message:
minor improvement in quiterr()

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