ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.47
Committed: Mon Apr 21 13:10:12 1997 UTC (27 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.46: +2 -2 lines
Log Message:
changed -e option to only check first letter

File Contents

# User Rev Content
1 greg 2.43 /* Copyright (c) 1997 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 greg 2.45 #undef NOPROTO
31     #define NOPROTO 1
32 greg 2.44 #include "tonemap.h"
33 greg 1.1 #include "view.h"
34     #include "x11raster.h"
35     #include "random.h"
36 greg 1.26 #include "resolu.h"
37 greg 1.1
38 greg 2.28 #ifndef int4
39 greg 2.44 #define int4 int /* most int's are 32-bit */
40 greg 2.28 #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 greg 2.35 char *gamstr = NULL; /* gamma value override */
59 greg 1.1
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 greg 2.41 int sequential = 0; /* display images in sequence */
77 greg 2.29
78 greg 2.42 char *tout = "od"; /* output of 't' command */
79 greg 2.43 int tinterv = 0; /* interval between mouse reports */
80 greg 2.42
81 greg 2.44 int tmflags = -1; /* tone mapping flags (-1 for none) */
82    
83 greg 1.1 VIEW ourview = STDVIEW; /* image view parameters */
84     int gotview = 0; /* got parameters from file */
85    
86     COLR *scanline; /* scan line buffer */
87 greg 2.44 TMbright *lscan; /* encoded luminance scanline */
88     BYTE *cscan; /* encoded chroma scanline */
89     BYTE *pscan; /* compute pixel scanline */
90 greg 1.1
91 greg 1.26 RESOLU inpres; /* input resolution and ordering */
92     int xmax, ymax; /* picture dimensions */
93 greg 1.1 int width, height; /* window size */
94     char *fname = NULL; /* input file name */
95     FILE *fin = stdin; /* input file */
96     long *scanpos = NULL; /* scan line positions in file */
97     int cury = 0; /* current scan location */
98    
99     double exposure = 1.0; /* exposure compensation used */
100    
101 greg 1.14 int wrongformat = 0; /* input in another format? */
102    
103 greg 2.6 GC ourgc; /* standard graphics context */
104 greg 1.5 GC revgc; /* graphics context with GXinvert */
105    
106 greg 2.6 int *ourrank; /* our visual class ranking */
107     XVisualInfo ourvis; /* our visual */
108     XRASTER *ourras; /* our stored image */
109 greg 1.1 unsigned char *ourdata; /* our image data */
110    
111     struct {
112     int xmin, ymin, xsiz, ysiz;
113     } box = {0, 0, 0, 0}; /* current box */
114    
115     char *geometry = NULL; /* geometry specification */
116    
117 greg 1.18 char icondata[ICONSIZ*ICONSIZ/8]; /* icon bitmap data */
118 greg 1.17 int iconwidth = 0, iconheight = 0;
119    
120 greg 1.1 char *progname;
121    
122     char errmsg[128];
123    
124 greg 2.37 BYTE clrtab[256][3]; /* global color map */
125 greg 2.13
126 greg 1.1 extern long ftell();
127    
128 greg 2.35 extern char *getenv();
129    
130 greg 1.1 Display *thedisplay;
131 greg 2.24 Atom closedownAtom, wmProtocolsAtom;
132 greg 1.1
133 greg 2.32 int sigrecv;
134 greg 2.14
135 greg 2.32 int onsig() { sigrecv++; }
136 greg 2.29
137 greg 2.32
138 greg 1.1 main(argc, argv)
139     int argc;
140     char *argv[];
141     {
142     int headline();
143     int i;
144 greg 2.29 int pid;
145 greg 1.1
146     progname = argv[0];
147    
148     for (i = 1; i < argc; i++)
149     if (argv[i][0] == '-')
150     switch (argv[i][1]) {
151 greg 2.42 case 'c': /* number of colors */
152 greg 1.1 maxcolors = atoi(argv[++i]);
153     break;
154 greg 2.42 case 'b': /* greyscale only */
155 greg 1.1 greyscale = !greyscale;
156     break;
157 greg 2.42 case 'm': /* monochrome */
158 greg 2.40 greyscale = 1;
159 greg 1.1 maxcolors = 2;
160     break;
161 greg 2.42 case 'd': /* display or dither */
162 greg 2.7 if (argv[i][2] == 'i')
163 greg 2.6 dispname = argv[++i];
164 greg 2.7 else
165     dither = !dither;
166 greg 1.1 break;
167 greg 2.42 case 'f': /* save pixmap */
168 greg 1.1 fast = !fast;
169     break;
170 greg 2.42 case 's': /* one at a time */
171 greg 2.41 sequential = !sequential;
172     break;
173 greg 2.42 case 'o': /* 't' output */
174     tout = argv[i]+2;
175     break;
176 greg 2.43 case 't': /* msec interval */
177     tinterv = atoi(argv[++i]);
178     break;
179 greg 2.42 case 'e': /* exposure comp. */
180 greg 2.44 i++;
181 greg 2.47 if (argv[i][0] == 'a') {
182 greg 2.44 tmflags = TM_F_CAMERA;
183     break;
184     }
185 greg 2.47 if (argv[i][0] == 'h') {
186 greg 2.44 tmflags = TM_F_HUMAN;
187     break;
188     }
189     if (argv[i][0] != '+' && argv[i][0] != '-')
190 greg 1.1 goto userr;
191 greg 2.44 scale = atoi(argv[i]);
192 greg 1.1 break;
193 greg 2.42 case 'g': /* gamma comp. */
194 greg 2.7 if (argv[i][2] == 'e')
195 greg 1.1 geometry = argv[++i];
196     else
197 greg 2.35 gamstr = argv[++i];
198 greg 1.1 break;
199     default:
200     goto userr;
201     }
202 greg 1.3 else if (argv[i][0] == '=')
203     geometry = argv[i];
204 greg 1.1 else
205     break;
206    
207 greg 2.29 if (i > argc)
208     goto userr;
209     while (i < argc-1) {
210 greg 2.32 sigrecv = 0;
211     signal(SIGCONT, onsig);
212 greg 2.29 if ((pid=fork()) == 0) { /* a child for each picture */
213     parent = -1;
214     break;
215     }
216     if (pid < 0)
217     quiterr("fork failed");
218     parent++;
219 greg 2.32 while (!sigrecv)
220     pause(); /* wait for wake-up call */
221 greg 2.29 i++;
222     }
223     if (i < argc) { /* open picture file */
224 greg 1.1 fname = argv[i];
225     fin = fopen(fname, "r");
226 greg 2.33 if (fin == NULL)
227     quiterr("cannot open picture file");
228 greg 2.29 }
229 greg 1.1 /* get header */
230 greg 1.14 getheader(fin, headline, NULL);
231 greg 1.1 /* get picture dimensions */
232 greg 1.26 if (wrongformat || !fgetsresolu(&inpres, fin))
233 greg 1.14 quiterr("bad picture format");
234 greg 1.26 xmax = scanlen(&inpres);
235     ymax = numscans(&inpres);
236 greg 1.1 /* set view parameters */
237     if (gotview && setview(&ourview) != NULL)
238     gotview = 0;
239     if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
240     quiterr("out of memory");
241    
242 greg 2.24 init(argc, argv); /* get file and open window */
243 greg 1.1
244     for ( ; ; )
245     getevent(); /* main loop */
246     userr:
247     fprintf(stderr,
248 greg 2.44 "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n",
249 greg 1.1 progname);
250 greg 2.21 exit(1);
251 greg 1.1 }
252    
253    
254     headline(s) /* get relevant info from header */
255     char *s;
256     {
257 greg 1.14 char fmt[32];
258 greg 1.1
259 greg 1.2 if (isexpos(s))
260     exposure *= exposval(s);
261 greg 2.44 else if (formatval(fmt, s))
262 greg 1.14 wrongformat = strcmp(fmt, COLRFMT);
263 greg 2.44 else if (isview(s) && sscanview(&ourview, s) > 0)
264 greg 2.4 gotview++;
265 greg 1.1 }
266    
267    
268 greg 2.24 init(argc, argv) /* get data and open window */
269     int argc;
270     char **argv;
271 greg 1.1 {
272 greg 1.4 XSetWindowAttributes ourwinattr;
273 greg 2.24 XClassHint xclshints;
274     XWMHints xwmhints;
275     XSizeHints xszhints;
276     XTextProperty windowName, iconName;
277     XGCValues xgcv;
278     char *name;
279 greg 2.6 register int i;
280 greg 1.1
281     if (fname != NULL) {
282     scanpos = (long *)malloc(ymax*sizeof(long));
283     if (scanpos == NULL)
284 greg 2.24 quiterr("out of memory");
285 greg 1.1 for (i = 0; i < ymax; i++)
286     scanpos[i] = -1;
287 greg 2.24 name = fname;
288     } else
289     name = progname;
290     /* remove directory prefix from name */
291     for (i = strlen(name); i-- > 0; )
292     if (name[i] == '/')
293     break;
294     name += i+1;
295 greg 2.6 if ((thedisplay = XOpenDisplay(dispname)) == NULL)
296     quiterr("cannot open display");
297 greg 2.35 /* set gamma value */
298     if (gamstr == NULL) /* get it from the X server */
299     gamstr = XGetDefault(thedisplay, "radiance", "gamma");
300     if (gamstr == NULL) /* get it from the environment */
301 greg 2.38 gamstr = getenv("DISPLAY_GAMMA");
302 greg 2.35 if (gamstr != NULL)
303     gamcor = atof(gamstr);
304 greg 2.6 /* get best visual for default screen */
305     getbestvis();
306 greg 1.4 /* store image */
307     getras();
308 greg 2.20 /* get size and position */
309 greg 2.24 xszhints.flags = 0;
310     xszhints.width = xmax; xszhints.height = ymax;
311 greg 2.20 if (geometry != NULL) {
312 greg 2.24 i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
313     (unsigned *)&xszhints.width,
314     (unsigned *)&xszhints.height);
315 greg 2.20 if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
316 greg 2.24 xszhints.flags |= USSize;
317 greg 2.20 else
318 greg 2.24 xszhints.flags |= PSize;
319 greg 2.20 if ((i&(XValue|YValue)) == (XValue|YValue)) {
320 greg 2.24 xszhints.flags |= USPosition;
321 greg 2.20 if (i & XNegative)
322 greg 2.24 xszhints.x += DisplayWidth(thedisplay,
323     ourscreen)-1-xszhints.width-2*BORWIDTH;
324 greg 2.20 if (i & YNegative)
325 greg 2.24 xszhints.y += DisplayHeight(thedisplay,
326     ourscreen)-1-xszhints.height-2*BORWIDTH;
327 greg 2.20 }
328     }
329 greg 2.24 /* open window */
330 greg 2.40 i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
331 greg 2.23 ourwinattr.border_pixel = ourwhite;
332     ourwinattr.background_pixel = ourblack;
333 greg 2.40 if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
334     ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
335     i |= CWColormap;
336     }
337 greg 2.24 ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
338     ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
339     ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
340     wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
341     xszhints.width, xszhints.height, BORWIDTH,
342 greg 2.40 ourvis.depth, InputOutput, ourvis.visual,
343     i, &ourwinattr);
344 greg 1.4 if (wind == 0)
345 greg 2.6 quiterr("cannot create window");
346 greg 1.5 width = xmax;
347     height = ymax;
348 greg 2.32 /* prepare graphics drawing context */
349     if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
350     quiterr("cannot get font");
351 greg 2.24 xgcv.foreground = ourblack;
352     xgcv.background = ourwhite;
353     ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
354     GCFont, &xgcv);
355     xgcv.function = GXinvert;
356     revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
357     GCFunction, &xgcv);
358    
359     /* set up the window manager */
360     xwmhints.flags = InputHint|IconPixmapHint;
361     xwmhints.input = True;
362     xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
363 greg 1.17 wind, icondata, iconwidth, iconheight);
364 greg 2.24
365     windowName.encoding = iconName.encoding = XA_STRING;
366     windowName.format = iconName.format = 8;
367     windowName.value = (u_char *)name;
368     windowName.nitems = strlen(windowName.value);
369     iconName.value = (u_char *)name;
370     iconName.nitems = strlen(windowName.value);
371    
372     xclshints.res_name = NULL;
373     xclshints.res_class = "Ximage";
374     XSetWMProperties(thedisplay, wind, &windowName, &iconName,
375     argv, argc, &xszhints, &xwmhints, &xclshints);
376     closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
377     wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
378     XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
379    
380 greg 1.1 XMapWindow(thedisplay, wind);
381     } /* end of init */
382    
383    
384     quiterr(err) /* print message and exit */
385     char *err;
386     {
387 greg 2.31 register int es;
388     int cs;
389    
390     if (es = err != NULL)
391 greg 2.29 fprintf(stderr, "%s: %s: %s\n", progname,
392     fname==NULL?"<stdin>":fname, err);
393 greg 2.36 if (thedisplay != NULL)
394     XCloseDisplay(thedisplay);
395     if (parent < 0 & sigrecv == 0)
396 greg 2.32 kill(getppid(), SIGCONT);
397 greg 2.31 while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
398     if (es == 0)
399     es = cs>>8 & 0xff;
400 greg 2.29 parent--;
401 greg 2.31 }
402     exit(es);
403 greg 1.1 }
404    
405    
406 greg 2.6 static int
407     viscmp(v1,v2) /* compare visual to see which is better, descending */
408     register XVisualInfo *v1, *v2;
409     {
410     int bad1 = 0, bad2 = 0;
411     register int *rp;
412    
413     if (v1->class == v2->class) {
414     if (v1->class == TrueColor || v1->class == DirectColor) {
415     /* prefer 24-bit to 32-bit */
416     if (v1->depth == 24 && v2->depth == 32)
417     return(-1);
418     if (v1->depth == 32 && v2->depth == 24)
419     return(1);
420     return(0);
421     }
422     /* don't be too greedy */
423     if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
424     return(v1->depth - v2->depth);
425     return(v2->depth - v1->depth);
426     }
427     /* prefer Pseudo when < 24-bit */
428     if ((v1->class == TrueColor || v1->class == DirectColor) &&
429     v1->depth < 24)
430     bad1 = 1;
431     if ((v2->class == TrueColor || v2->class == DirectColor) &&
432     v2->depth < 24)
433     bad2 = -1;
434     if (bad1 | bad2)
435     return(bad1+bad2);
436     /* otherwise, use class ranking */
437     for (rp = ourrank; *rp != -1; rp++) {
438     if (v1->class == *rp)
439     return(-1);
440     if (v2->class == *rp)
441     return(1);
442     }
443     return(0);
444     }
445    
446    
447     getbestvis() /* get the best visual for this screen */
448     {
449 greg 2.7 #ifdef DEBUG
450 greg 2.6 static char vistype[][12] = {
451     "StaticGray",
452     "GrayScale",
453     "StaticColor",
454     "PseudoColor",
455     "TrueColor",
456     "DirectColor"
457     };
458 greg 2.7 #endif
459 greg 2.6 static int rankings[3][6] = {
460     {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
461 greg 2.7 {PseudoColor,GrayScale,StaticGray,-1},
462     {PseudoColor,GrayScale,StaticGray,-1}
463 greg 2.6 };
464     XVisualInfo *xvi;
465     int vismatched;
466     register int i, j;
467    
468     if (greyscale) {
469     ourrank = rankings[2];
470     if (maxcolors < 2) maxcolors = 256;
471     } else if (maxcolors >= 2 && maxcolors <= 256)
472     ourrank = rankings[1];
473     else {
474     ourrank = rankings[0];
475     maxcolors = 256;
476     }
477     /* find best visual */
478     ourvis.screen = ourscreen;
479     xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
480     if (xvi == NULL)
481     quiterr("no visuals for this screen!");
482 greg 2.7 #ifdef DEBUG
483     fprintf(stderr, "Supported visuals:\n");
484     for (i = 0; i < vismatched; i++)
485     fprintf(stderr, "\ttype %s, depth %d\n",
486     vistype[xvi[i].class], xvi[i].depth);
487     #endif
488 greg 2.6 for (i = 0, j = 1; j < vismatched; j++)
489     if (viscmp(&xvi[i],&xvi[j]) > 0)
490     i = j;
491     /* compare to least acceptable */
492     for (j = 0; ourrank[j++] != -1; )
493     ;
494     ourvis.class = ourrank[--j];
495     ourvis.depth = 1;
496     if (viscmp(&xvi[i],&ourvis) > 0)
497     quiterr("inadequate visuals on this screen");
498     /* OK, we'll use it */
499     copystruct(&ourvis, &xvi[i]);
500 greg 2.7 #ifdef DEBUG
501     fprintf(stderr, "Selected visual type %s, depth %d\n",
502     vistype[ourvis.class], ourvis.depth);
503     #endif
504 greg 2.8 /* make appropriate adjustments */
505 greg 2.6 if (ourvis.class == GrayScale || ourvis.class == StaticGray)
506     greyscale = 1;
507 greg 2.7 if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
508     maxcolors = ourvis.colormap_size;
509 greg 2.8 if (ourvis.class == StaticGray) {
510     ourblack = 0;
511     ourwhite = 255;
512     } else if (ourvis.class == PseudoColor) {
513     ourblack = BlackPixel(thedisplay,ourscreen);
514     ourwhite = WhitePixel(thedisplay,ourscreen);
515 greg 2.9 if ((ourblack|ourwhite) & ~255L) {
516     ourblack = 0;
517     ourwhite = 1;
518     }
519 greg 2.13 if (maxcolors > 4)
520     maxcolors -= 2;
521 greg 2.8 } else {
522     ourblack = 0;
523 greg 2.9 ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
524 greg 2.8 }
525 greg 2.6 XFree((char *)xvi);
526     }
527    
528    
529 greg 1.1 getras() /* get raster file */
530     {
531     XVisualInfo vinfo;
532    
533     if (maxcolors <= 2) { /* monochrome */
534     ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
535     if (ourdata == NULL)
536     goto fail;
537 greg 2.6 ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
538 greg 1.1 xmax, ymax, 8);
539     if (ourras == NULL)
540     goto fail;
541     getmono();
542 greg 2.13 } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
543 greg 2.28 ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
544 greg 1.1 if (ourdata == NULL)
545     goto fail;
546 greg 2.28 ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
547 greg 2.6 ourdata, xmax, ymax, 32);
548 greg 1.1 if (ourras == NULL)
549     goto fail;
550     getfull();
551     } else {
552     ourdata = (unsigned char *)malloc(xmax*ymax);
553     if (ourdata == NULL)
554     goto fail;
555 greg 2.6 ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
556 greg 1.1 xmax, ymax, 8);
557     if (ourras == NULL)
558     goto fail;
559 greg 2.44 if (greyscale)
560 greg 2.7 getgrey();
561 greg 2.13 else
562     getmapped();
563     if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
564     goto fail;
565 greg 1.1 }
566 greg 2.13 return;
567 greg 1.1 fail:
568 greg 1.9 quiterr("could not create raster image");
569 greg 1.1 }
570    
571    
572     getevent() /* process the next event */
573     {
574 greg 2.24 XEvent xev;
575 greg 1.1
576 greg 2.24 XNextEvent(thedisplay, &xev);
577     switch ((int)xev.type) {
578 greg 1.1 case KeyPress:
579 greg 2.24 docom(&xev.xkey);
580 greg 1.1 break;
581     case ConfigureNotify:
582 greg 2.24 width = xev.xconfigure.width;
583     height = xev.xconfigure.height;
584 greg 1.1 break;
585     case MapNotify:
586     map_rcolors(ourras, wind);
587     if (fast)
588 greg 2.6 make_rpixmap(ourras, wind);
589 greg 2.41 if (!sequential & parent < 0 & sigrecv == 0) {
590 greg 2.39 kill(getppid(), SIGCONT);
591     sigrecv--;
592     }
593 greg 1.1 break;
594     case UnmapNotify:
595 greg 2.7 if (!fast)
596     unmap_rcolors(ourras);
597 greg 1.1 break;
598     case Expose:
599 greg 2.24 redraw(xev.xexpose.x, xev.xexpose.y,
600     xev.xexpose.width, xev.xexpose.height);
601 greg 1.1 break;
602     case ButtonPress:
603 greg 2.24 if (xev.xbutton.state & (ShiftMask|ControlMask))
604     moveimage(&xev.xbutton);
605 greg 1.1 else
606 greg 2.43 switch (xev.xbutton.button) {
607     case Button1:
608     getbox(&xev.xbutton);
609     break;
610     case Button2:
611     traceray(xev.xbutton.x, xev.xbutton.y);
612     break;
613     case Button3:
614     trackrays(&xev.xbutton);
615     break;
616     }
617 greg 1.1 break;
618 greg 2.29 case ClientMessage:
619 greg 2.24 if ((xev.xclient.message_type == wmProtocolsAtom) &&
620     (xev.xclient.data.l[0] == closedownAtom))
621     quiterr(NULL);
622     break;
623 greg 1.1 }
624     }
625    
626    
627 greg 2.42 traceray(xpos, ypos) /* print requested pixel data */
628 greg 2.22 int xpos, ypos;
629     {
630 greg 2.42 extern char *index();
631 greg 2.22 FLOAT hv[2];
632     FVECT rorg, rdir;
633 greg 2.42 COLOR cval;
634     register char *cp;
635 greg 2.22
636 greg 2.42 box.xmin = xpos; box.xsiz = 1;
637     box.ymin = ypos; box.ysiz = 1;
638     avgbox(cval);
639     scalecolor(cval, 1./exposure);
640 greg 2.22 pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
641 greg 2.42 if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
642     rorg[0] = rorg[1] = rorg[2] =
643     rdir[0] = rdir[1] = rdir[2] = 0.;
644    
645     for (cp = tout; *cp; cp++) /* print what they asked for */
646     switch (*cp) {
647     case 'o': /* origin */
648     printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
649     break;
650     case 'd': /* direction */
651     printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
652     break;
653     case 'v': /* radiance value */
654     printf("%e %e %e ", colval(cval,RED),
655     colval(cval,GRN), colval(cval,BLU));
656     break;
657     case 'l': /* luminance */
658     printf("%e ", luminance(cval));
659     break;
660     case 'p': /* pixel position */
661     printf("%d %d ", (int)(hv[0]*inpres.xr),
662     (int)(hv[1]*inpres.yr));
663     break;
664     }
665     putchar('\n');
666 greg 2.22 fflush(stdout);
667     return(0);
668     }
669    
670    
671     docom(ekey) /* execute command */
672 greg 1.1 XKeyPressedEvent *ekey;
673     {
674     char buf[80];
675     COLOR cval;
676     XColor cvx;
677     int com, n;
678     double comp;
679 greg 1.26 FLOAT hv[2];
680 greg 1.1
681     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
682     if (n == 0)
683     return(0);
684     com = buf[0];
685     switch (com) { /* interpret command */
686     case 'q':
687 greg 2.22 case 'Q':
688 greg 2.3 case CTRL('D'): /* quit */
689 greg 2.21 quiterr(NULL);
690 greg 1.1 case '\n':
691     case '\r':
692     case 'l':
693     case 'c': /* value */
694     if (avgbox(cval) == -1)
695     return(-1);
696     switch (com) {
697     case '\n':
698     case '\r': /* radiance */
699     sprintf(buf, "%.3f", intens(cval)/exposure);
700     break;
701     case 'l': /* luminance */
702 greg 1.16 sprintf(buf, "%.0fL", luminance(cval)/exposure);
703 greg 1.1 break;
704     case 'c': /* color */
705     comp = pow(2.0, (double)scale);
706     sprintf(buf, "(%.2f,%.2f,%.2f)",
707     colval(cval,RED)*comp,
708     colval(cval,GRN)*comp,
709     colval(cval,BLU)*comp);
710     break;
711     }
712 greg 1.3 XDrawImageString(thedisplay, wind, ourgc,
713     box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
714 greg 1.1 return(0);
715     case 'i': /* identify (contour) */
716     if (ourras->pixels == NULL)
717     return(-1);
718     n = ourdata[ekey->x-xoff+xmax*(ekey->y-yoff)];
719     n = ourras->pmap[n];
720     cvx.pixel = ourras->cdefs[n].pixel;
721     cvx.red = random() & 65535;
722     cvx.green = random() & 65535;
723     cvx.blue = random() & 65535;
724 greg 1.2 cvx.flags = DoRed|DoGreen|DoBlue;
725     XStoreColor(thedisplay, ourras->cmap, &cvx);
726 greg 1.1 return(0);
727     case 'p': /* position */
728 greg 1.26 pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
729     sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr),
730     (int)(hv[1]*inpres.yr));
731 greg 1.1 XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
732     buf, strlen(buf));
733     return(0);
734     case 't': /* trace */
735 greg 2.22 return(traceray(ekey->x, ekey->y));
736 greg 2.46 case 'a': /* auto exposure */
737     if (tmflags == TM_F_CAMERA)
738     return(0);
739     tmflags = TM_F_CAMERA;
740     strcpy(buf, "auto exposure...");
741     goto remap;
742     case 'h': /* human response */
743     if (tmflags == TM_F_HUMAN)
744     return(0);
745     tmflags = TM_F_HUMAN;
746     strcpy(buf, "human exposure...");
747     goto remap;
748 greg 1.1 case '=': /* adjust exposure */
749 greg 2.25 case '@': /* adaptation level */
750 greg 1.1 if (avgbox(cval) == -1)
751     return(-1);
752 greg 2.34 comp = bright(cval);
753     if (comp < 1e-20) {
754     XBell(thedisplay, 0);
755     return(-1);
756     }
757     if (com == '@')
758     comp = 106./exposure/
759     pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
760     else
761     comp = .5/comp;
762 greg 2.25 comp = log(comp)/.69315 - scale;
763     n = comp < 0 ? comp-.5 : comp+.5 ; /* round */
764 greg 2.44 if (tmflags != -1)
765     tmflags = -1; /* turn off tone mapping */
766     else {
767     if (n == 0) /* else check if any change */
768     return(0);
769     scale_rcolors(ourras, pow(2.0, (double)n));
770     }
771 greg 1.1 scale += n;
772     sprintf(buf, "%+d", scale);
773 greg 2.46 remap:
774 greg 1.3 XDrawImageString(thedisplay, wind, ourgc,
775     box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
776 greg 1.1 XFlush(thedisplay);
777     free(ourdata);
778     free_raster(ourras);
779     getras();
780     /* fall through */
781 greg 2.3 case CTRL('R'): /* redraw */
782     case CTRL('L'):
783 greg 1.1 unmap_rcolors(ourras);
784     XClearWindow(thedisplay, wind);
785     map_rcolors(ourras, wind);
786     if (fast)
787 greg 2.12 make_rpixmap(ourras, wind);
788 greg 1.1 redraw(0, 0, width, height);
789     return(0);
790 greg 2.27 case 'f': /* turn on fast redraw */
791     fast = 1;
792     make_rpixmap(ourras, wind);
793     return(0);
794     case 'F': /* turn off fast redraw */
795     fast = 0;
796     free_rpixmap(ourras);
797     return(0);
798 greg 2.19 case '0': /* recenter origin */
799     if (xoff == 0 & yoff == 0)
800     return(0);
801     xoff = yoff = 0;
802     XClearWindow(thedisplay, wind);
803     redraw(0, 0, width, height);
804     return(0);
805 greg 1.1 case ' ': /* clear */
806     redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
807     return(0);
808     default:
809 greg 1.2 XBell(thedisplay, 0);
810 greg 1.1 return(-1);
811     }
812     }
813    
814    
815 greg 1.2 moveimage(ebut) /* shift the image */
816     XButtonPressedEvent *ebut;
817 greg 1.1 {
818 greg 2.24 XEvent e;
819 greg 1.5 int mxo, myo;
820 greg 1.1
821 greg 2.24 XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
822     while (e.type == MotionNotify) {
823     mxo = e.xmotion.x;
824     myo = e.xmotion.y;
825 greg 1.5 revline(ebut->x, ebut->y, mxo, myo);
826     revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
827     xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
828 greg 2.24 XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
829 greg 1.5 revline(ebut->x, ebut->y, mxo, myo);
830     revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
831     xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
832 greg 1.3 }
833 greg 2.24 xoff += e.xbutton.x - ebut->x;
834     yoff += e.xbutton.y - ebut->y;
835 greg 1.1 XClearWindow(thedisplay, wind);
836     redraw(0, 0, width, height);
837     }
838    
839    
840     getbox(ebut) /* get new box */
841     XButtonPressedEvent *ebut;
842     {
843 greg 2.24 XEvent e;
844 greg 1.1
845 greg 2.24 XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
846     while (e.type == MotionNotify) {
847     revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
848     XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
849 greg 1.1 revbox(ebut->x, ebut->y, box.xmin, box.ymin);
850     }
851 greg 2.24 box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
852     box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
853 greg 1.1 if (box.xmin > ebut->x) {
854     box.xsiz = box.xmin - ebut->x + 1;
855     box.xmin = ebut->x;
856     } else {
857     box.xsiz = ebut->x - box.xmin + 1;
858     }
859     if (box.ymin > ebut->y) {
860     box.ysiz = box.ymin - ebut->y + 1;
861     box.ymin = ebut->y;
862     } else {
863     box.ysiz = ebut->y - box.ymin + 1;
864 greg 2.43 }
865     }
866    
867    
868     trackrays(ebut) /* trace rays as mouse moves */
869     XButtonPressedEvent *ebut;
870     {
871     XEvent e;
872     unsigned long lastrept;
873    
874     traceray(ebut->x, ebut->y);
875     lastrept = ebut->time;
876     XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
877     while (e.type == MotionNotify) {
878     if (e.xmotion.time >= lastrept + tinterv) {
879     traceray(e.xmotion.x, e.xmotion.y);
880     lastrept = e.xmotion.time;
881     }
882     XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
883 greg 1.1 }
884     }
885    
886    
887     revbox(x0, y0, x1, y1) /* draw box with reversed lines */
888     int x0, y0, x1, y1;
889     {
890 greg 1.5 revline(x0, y0, x1, y0);
891     revline(x0, y1, x1, y1);
892     revline(x0, y0, x0, y1);
893     revline(x1, y0, x1, y1);
894     }
895 greg 1.1
896    
897     avgbox(clr) /* average color over current box */
898     COLOR clr;
899     {
900 greg 1.25 static COLOR lc;
901     static int ll, lr, lt, lb;
902 greg 1.1 int left, right, top, bottom;
903     int y;
904     double d;
905     COLOR ctmp;
906     register int x;
907    
908     setcolor(clr, 0.0, 0.0, 0.0);
909     left = box.xmin - xoff;
910     right = left + box.xsiz;
911     if (left < 0)
912     left = 0;
913     if (right > xmax)
914     right = xmax;
915     if (left >= right)
916     return(-1);
917     top = box.ymin - yoff;
918     bottom = top + box.ysiz;
919     if (top < 0)
920     top = 0;
921     if (bottom > ymax)
922     bottom = ymax;
923     if (top >= bottom)
924     return(-1);
925 greg 1.25 if (left == ll && right == lr && top == lt && bottom == lb) {
926     copycolor(clr, lc);
927 greg 2.15 return(0);
928 greg 1.25 }
929 greg 1.1 for (y = top; y < bottom; y++) {
930     if (getscan(y) == -1)
931     return(-1);
932     for (x = left; x < right; x++) {
933     colr_color(ctmp, scanline[x]);
934     addcolor(clr, ctmp);
935     }
936     }
937     d = 1.0/((right-left)*(bottom-top));
938     scalecolor(clr, d);
939 greg 1.25 ll = left; lr = right; lt = top; lb = bottom;
940     copycolor(lc, clr);
941 greg 1.1 return(0);
942     }
943    
944    
945 greg 2.44 make_tonemap() /* initialize tone mapping */
946     {
947     int y;
948    
949     if (tmflags != -1 && fname == NULL) {
950     fprintf(stderr, "%s: cannot adjust tone of standard input\n",
951     progname);
952     tmflags = -1;
953     }
954     if (tmflags == -1) {
955     setcolrcor(pow, 1.0/gamcor);
956     return;
957     }
958     if (greyscale)
959     tmflags |= TM_F_BW;
960     /* initialize tm library */
961     if (tmInit(tmflags, stdprims, gamcor) == NULL)
962     goto memerr;
963     if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
964     goto tmerr;
965     /* allocate encoding buffers */
966     if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
967     goto memerr;
968     if (tmflags & TM_F_BW) {
969     cscan = TM_NOCHROM;
970     if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
971     goto memerr;
972     } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) == NULL)
973     goto memerr;
974     /* compute picture histogram */
975     for (y = 0; y < ymax; y++) {
976     getscan(y);
977     if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
978     goto tmerr;
979     if (tmAddHisto(lscan, xmax, 1))
980     goto tmerr;
981     }
982     /* compute tone mapping */
983     if (tmComputeMapping(gamcor, 0., 0.))
984     goto tmerr;
985     free((char *)lscan);
986     return;
987     memerr:
988     quiterr("out of memory in make_tonemap");
989     tmerr:
990     quiterr("tone mapping error");
991     }
992    
993    
994     tmap_colrs(scn, len) /* apply tone mapping to scanline */
995     register COLR *scn;
996     int len;
997     {
998     register BYTE *ps;
999    
1000     if (tmflags == -1) {
1001     if (scale)
1002     shiftcolrs(scn, len, scale);
1003     colrs_gambs(scn, len);
1004     return;
1005     }
1006     if (len > xmax)
1007     quiterr("code error 1 in tmap_colrs");
1008     if (tmCvColrs(lscan, cscan, scn, len))
1009     goto tmerr;
1010     if (tmMapPixels(pscan, lscan, cscan, len))
1011     goto tmerr;
1012     ps = pscan;
1013     if (tmflags & TM_F_BW)
1014     while (len--) {
1015     scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1016     scn[0][EXP] = COLXS;
1017     scn++;
1018     }
1019     else
1020     while (len--) {
1021     scn[0][RED] = *ps++;
1022     scn[0][GRN] = *ps++;
1023     scn[0][BLU] = *ps++;
1024     scn[0][EXP] = COLXS;
1025     scn++;
1026     }
1027     return;
1028     tmerr:
1029     quiterr("tone mapping error");
1030     }
1031    
1032    
1033     done_tonemap() /* clean up after tone mapping is done */
1034     {
1035     if (tmflags == -1 || tmTop == NULL)
1036     return;
1037     tmDone(tmTop); /* clear old mapping */
1038     free((char *)lscan); /* free memory */
1039     free((char *)pscan);
1040     }
1041    
1042    
1043 greg 1.1 getmono() /* get monochrome data */
1044     {
1045     register unsigned char *dp;
1046     register int x, err;
1047 greg 1.23 int y, errp;
1048 greg 1.1 short *cerr;
1049    
1050 greg 1.10 if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
1051 greg 1.9 quiterr("out of memory in getmono");
1052 greg 1.1 dp = ourdata - 1;
1053     for (y = 0; y < ymax; y++) {
1054 greg 2.15 getscan(y);
1055 greg 2.16 add2icon(y, scanline);
1056 greg 1.10 normcolrs(scanline, xmax, scale);
1057 greg 1.1 err = 0;
1058     for (x = 0; x < xmax; x++) {
1059     if (!(x&7))
1060     *++dp = 0;
1061 greg 1.23 errp = err;
1062 greg 1.10 err += normbright(scanline[x]) + cerr[x];
1063 greg 1.1 if (err > 127)
1064     err -= 255;
1065     else
1066 greg 1.3 *dp |= 1<<(7-(x&07));
1067 greg 1.23 err /= 3;
1068     cerr[x] = err + errp;
1069 greg 1.1 }
1070     }
1071     free((char *)cerr);
1072     }
1073    
1074    
1075 greg 1.17 add2icon(y, scan) /* add a scanline to our icon data */
1076     int y;
1077     COLR *scan;
1078     {
1079     static short cerr[ICONSIZ];
1080 greg 1.20 static int ynext;
1081     static char *dp;
1082 greg 2.17 COLR clr;
1083 greg 1.17 register int err;
1084 greg 1.20 register int x, ti;
1085 greg 1.23 int errp;
1086 greg 1.17
1087     if (iconheight == 0) { /* initialize */
1088 greg 1.18 if (xmax <= ICONSIZ && ymax <= ICONSIZ) {
1089 greg 1.17 iconwidth = xmax;
1090     iconheight = ymax;
1091     } else if (xmax > ymax) {
1092     iconwidth = ICONSIZ;
1093     iconheight = ICONSIZ*ymax/xmax;
1094 greg 1.24 if (iconheight < 1)
1095     iconheight = 1;
1096 greg 1.17 } else {
1097     iconwidth = ICONSIZ*xmax/ymax;
1098 greg 1.24 if (iconwidth < 1)
1099     iconwidth = 1;
1100 greg 1.17 iconheight = ICONSIZ;
1101     }
1102 greg 1.20 ynext = 0;
1103 greg 1.17 dp = icondata - 1;
1104     }
1105 greg 1.20 if (y < ynext*ymax/iconheight) /* skip this one */
1106 greg 1.17 return;
1107     err = 0;
1108     for (x = 0; x < iconwidth; x++) {
1109     if (!(x&7))
1110     *++dp = 0;
1111 greg 1.23 errp = err;
1112 greg 1.20 ti = x*xmax/iconwidth;
1113 greg 2.17 copycolr(clr, scan[ti]);
1114     normcolrs(clr, 1, scale);
1115     err += normbright(clr) + cerr[x];
1116 greg 1.17 if (err > 127)
1117     err -= 255;
1118     else
1119     *dp |= 1<<(x&07);
1120 greg 1.23 err /= 3;
1121     cerr[x] = err + errp;
1122 greg 1.17 }
1123 greg 1.20 ynext++;
1124 greg 1.17 }
1125    
1126    
1127 greg 1.1 getfull() /* get full (24-bit) data */
1128     {
1129     int y;
1130 greg 2.28 register unsigned int4 *dp;
1131 greg 1.10 register int x;
1132 greg 2.44 /* initialize tone mapping */
1133     make_tonemap();
1134 greg 1.10 /* read and convert file */
1135 greg 2.28 dp = (unsigned int4 *)ourdata;
1136 greg 1.10 for (y = 0; y < ymax; y++) {
1137 greg 2.15 getscan(y);
1138 greg 2.16 add2icon(y, scanline);
1139 greg 2.44 tmap_colrs(scanline, xmax);
1140 greg 2.6 if (ourras->image->blue_mask & 1)
1141     for (x = 0; x < xmax; x++)
1142 greg 2.35 *dp++ = (unsigned int4)scanline[x][RED] << 16 |
1143     (unsigned int4)scanline[x][GRN] << 8 |
1144     (unsigned int4)scanline[x][BLU] ;
1145 greg 2.6 else
1146     for (x = 0; x < xmax; x++)
1147 greg 2.35 *dp++ = (unsigned int4)scanline[x][RED] |
1148     (unsigned int4)scanline[x][GRN] << 8 |
1149     (unsigned int4)scanline[x][BLU] << 16 ;
1150 greg 1.10 }
1151 greg 2.44 done_tonemap();
1152 greg 1.1 }
1153    
1154    
1155 greg 2.7 getgrey() /* get greyscale data */
1156     {
1157     int y;
1158     register unsigned char *dp;
1159     register int x;
1160 greg 2.44 /* initialize tone mapping */
1161     make_tonemap();
1162 greg 2.7 /* read and convert file */
1163     dp = ourdata;
1164     for (y = 0; y < ymax; y++) {
1165 greg 2.15 getscan(y);
1166 greg 2.16 add2icon(y, scanline);
1167 greg 2.44 tmap_colrs(scanline, xmax);
1168 greg 2.13 if (maxcolors < 256)
1169 greg 2.7 for (x = 0; x < xmax; x++)
1170 greg 2.44 *dp++ = ((int4)scanline[x][GRN] *
1171 greg 2.18 maxcolors + maxcolors/2) >> 8;
1172 greg 2.7 else
1173     for (x = 0; x < xmax; x++)
1174 greg 2.18 *dp++ = scanline[x][GRN];
1175 greg 2.7 }
1176 greg 2.13 for (x = 0; x < maxcolors; x++)
1177     clrtab[x][RED] = clrtab[x][GRN] =
1178 greg 2.44 clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors;
1179     done_tonemap();
1180 greg 2.7 }
1181    
1182    
1183 greg 2.13 getmapped() /* get color-mapped data */
1184     {
1185     int y;
1186 greg 2.30 /* make sure we can do it first */
1187     if (fname == NULL)
1188     quiterr("cannot map colors from standard input");
1189 greg 2.44 /* initialize tone mapping */
1190     make_tonemap();
1191 greg 2.13 /* make histogram */
1192 greg 2.44 if (new_histo((int4)xmax*ymax) == -1)
1193 greg 2.37 quiterr("cannot initialize histogram");
1194 greg 2.13 for (y = 0; y < ymax; y++) {
1195     if (getscan(y) < 0)
1196 greg 2.30 break;
1197 greg 2.16 add2icon(y, scanline);
1198 greg 2.44 tmap_colrs(scanline, xmax);
1199 greg 2.13 cnt_colrs(scanline, xmax);
1200     }
1201     /* map pixels */
1202     if (!new_clrtab(maxcolors))
1203     quiterr("cannot create color map");
1204     for (y = 0; y < ymax; y++) {
1205 greg 2.30 getscan(y);
1206 greg 2.44 tmap_colrs(scanline, xmax);
1207 greg 2.13 if (dither)
1208     dith_colrs(ourdata+y*xmax, scanline, xmax);
1209     else
1210     map_colrs(ourdata+y*xmax, scanline, xmax);
1211     }
1212 greg 2.44 done_tonemap();
1213 greg 2.13 }
1214    
1215    
1216 greg 1.1 scale_rcolors(xr, sf) /* scale color map */
1217     register XRASTER *xr;
1218     double sf;
1219     {
1220     register int i;
1221     long maxv;
1222    
1223     if (xr->pixels == NULL)
1224     return;
1225    
1226     sf = pow(sf, 1.0/gamcor);
1227     maxv = 65535/sf;
1228    
1229     for (i = xr->ncolors; i--; ) {
1230     xr->cdefs[i].red = xr->cdefs[i].red > maxv ?
1231     65535 :
1232     xr->cdefs[i].red * sf;
1233     xr->cdefs[i].green = xr->cdefs[i].green > maxv ?
1234     65535 :
1235     xr->cdefs[i].green * sf;
1236     xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ?
1237     65535 :
1238     xr->cdefs[i].blue * sf;
1239     }
1240     XStoreColors(thedisplay, xr->cmap, xr->cdefs, xr->ncolors);
1241     }
1242    
1243    
1244     getscan(y)
1245     int y;
1246     {
1247 greg 2.30 static int trunced = -1; /* truncated file? */
1248     skipit:
1249     if (trunced >= 0 && y >= trunced) {
1250     bzero(scanline, xmax*sizeof(COLR));
1251     return(-1);
1252     }
1253 greg 1.1 if (y != cury) {
1254     if (scanpos == NULL || scanpos[y] == -1)
1255     return(-1);
1256     if (fseek(fin, scanpos[y], 0) == -1)
1257 greg 1.9 quiterr("fseek error");
1258 greg 1.1 cury = y;
1259 greg 2.11 } else if (scanpos != NULL && scanpos[y] == -1)
1260 greg 1.1 scanpos[y] = ftell(fin);
1261    
1262 greg 2.30 if (freadcolrs(scanline, xmax, fin) < 0) {
1263     fprintf(stderr, "%s: %s: unfinished picture\n",
1264     progname, fname==NULL?"<stdin>":fname);
1265     trunced = y;
1266     goto skipit;
1267     }
1268 greg 1.1 cury++;
1269     return(0);
1270     }