ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.40
Committed: Wed Nov 23 14:24:34 1994 UTC (29 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.39: +8 -4 lines
Log Message:
fixed unnecessary color table allocation

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