ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.24
Committed: Tue Mar 30 14:39:29 1993 UTC (31 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.23: +104 -91 lines
Log Message:
fixes by Philip Thomson in init()

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