ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.33
Committed: Thu Nov 4 12:04:55 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.32: +2 -4 lines
Log Message:
simplified error message

File Contents

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