ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.54
Committed: Wed Jun 16 08:15:17 1999 UTC (24 years, 10 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.53: +2 -1 lines
Log Message:
moved initialization of fin to main() for Linux

File Contents

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