ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.44
Committed: Tue Apr 15 16:53:58 1997 UTC (27 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.43: +143 -38 lines
Log Message:
added tone mapping functions (-e auto and -e human options)

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