ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.28
Committed: Mon Jul 19 15:18:05 1993 UTC (30 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.27: +11 -4 lines
Log Message:
fixes for DEC Alpha running OSF (sizeof long == 8)

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