ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.55
Committed: Tue Jul 20 14:07:38 1999 UTC (24 years, 9 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.54: +52 -10 lines
Log Message:
added support for 15-bit TrueColor visual for Linux

File Contents

# User Rev Content
1 gwlarson 2.55 /* Copyright (c) 1999 Regents of the University of California */
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 gwlarson 2.55 /* go for maximum depth otherwise */
423     return(v2->depth - v1->depth);
424 greg 2.6 }
425     /* don't be too greedy */
426     if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
427     return(v1->depth - v2->depth);
428     return(v2->depth - v1->depth);
429     }
430 gwlarson 2.55 /* prefer Pseudo when < 15-bit */
431 greg 2.6 if ((v1->class == TrueColor || v1->class == DirectColor) &&
432 gwlarson 2.55 v1->depth < 15)
433 greg 2.6 bad1 = 1;
434     if ((v2->class == TrueColor || v2->class == DirectColor) &&
435 gwlarson 2.55 v2->depth < 15)
436 greg 2.6 bad2 = -1;
437     if (bad1 | bad2)
438     return(bad1+bad2);
439     /* otherwise, use class ranking */
440     for (rp = ourrank; *rp != -1; rp++) {
441     if (v1->class == *rp)
442     return(-1);
443     if (v2->class == *rp)
444     return(1);
445     }
446     return(0);
447     }
448    
449    
450     getbestvis() /* get the best visual for this screen */
451     {
452 greg 2.7 #ifdef DEBUG
453 greg 2.6 static char vistype[][12] = {
454     "StaticGray",
455     "GrayScale",
456     "StaticColor",
457     "PseudoColor",
458     "TrueColor",
459     "DirectColor"
460     };
461 greg 2.7 #endif
462 greg 2.6 static int rankings[3][6] = {
463     {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
464 greg 2.7 {PseudoColor,GrayScale,StaticGray,-1},
465     {PseudoColor,GrayScale,StaticGray,-1}
466 greg 2.6 };
467     XVisualInfo *xvi;
468     int vismatched;
469     register int i, j;
470    
471     if (greyscale) {
472     ourrank = rankings[2];
473     if (maxcolors < 2) maxcolors = 256;
474     } else if (maxcolors >= 2 && maxcolors <= 256)
475     ourrank = rankings[1];
476     else {
477     ourrank = rankings[0];
478     maxcolors = 256;
479     }
480     /* find best visual */
481     ourvis.screen = ourscreen;
482     xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
483     if (xvi == NULL)
484     quiterr("no visuals for this screen!");
485 greg 2.7 #ifdef DEBUG
486     fprintf(stderr, "Supported visuals:\n");
487     for (i = 0; i < vismatched; i++)
488     fprintf(stderr, "\ttype %s, depth %d\n",
489     vistype[xvi[i].class], xvi[i].depth);
490     #endif
491 greg 2.6 for (i = 0, j = 1; j < vismatched; j++)
492     if (viscmp(&xvi[i],&xvi[j]) > 0)
493     i = j;
494     /* compare to least acceptable */
495     for (j = 0; ourrank[j++] != -1; )
496     ;
497     ourvis.class = ourrank[--j];
498     ourvis.depth = 1;
499     if (viscmp(&xvi[i],&ourvis) > 0)
500     quiterr("inadequate visuals on this screen");
501     /* OK, we'll use it */
502     copystruct(&ourvis, &xvi[i]);
503 greg 2.7 #ifdef DEBUG
504     fprintf(stderr, "Selected visual type %s, depth %d\n",
505     vistype[ourvis.class], ourvis.depth);
506     #endif
507 greg 2.8 /* make appropriate adjustments */
508 greg 2.6 if (ourvis.class == GrayScale || ourvis.class == StaticGray)
509     greyscale = 1;
510 greg 2.7 if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
511     maxcolors = ourvis.colormap_size;
512 greg 2.8 if (ourvis.class == StaticGray) {
513     ourblack = 0;
514     ourwhite = 255;
515     } else if (ourvis.class == PseudoColor) {
516     ourblack = BlackPixel(thedisplay,ourscreen);
517     ourwhite = WhitePixel(thedisplay,ourscreen);
518 greg 2.9 if ((ourblack|ourwhite) & ~255L) {
519     ourblack = 0;
520     ourwhite = 1;
521     }
522 greg 2.13 if (maxcolors > 4)
523     maxcolors -= 2;
524 greg 2.8 } else {
525     ourblack = 0;
526 greg 2.9 ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
527 greg 2.8 }
528 greg 2.6 XFree((char *)xvi);
529     }
530    
531    
532 greg 1.1 getras() /* get raster file */
533     {
534     XVisualInfo vinfo;
535    
536     if (maxcolors <= 2) { /* monochrome */
537     ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
538     if (ourdata == NULL)
539     goto fail;
540 greg 2.6 ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
541 greg 1.1 xmax, ymax, 8);
542     if (ourras == NULL)
543     goto fail;
544     getmono();
545 greg 2.13 } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
546 gwlarson 2.55 int datsiz = ourvis.depth>16 ? sizeof(int4) : sizeof(int2);
547     ourdata = (unsigned char *)malloc(datsiz*xmax*ymax);
548 greg 1.1 if (ourdata == NULL)
549     goto fail;
550 gwlarson 2.55 ourras = make_raster(thedisplay, &ourvis, datsiz*8,
551     ourdata, xmax, ymax, datsiz*8);
552 greg 1.1 if (ourras == NULL)
553     goto fail;
554     getfull();
555     } else {
556     ourdata = (unsigned char *)malloc(xmax*ymax);
557     if (ourdata == NULL)
558     goto fail;
559 greg 2.6 ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
560 greg 1.1 xmax, ymax, 8);
561     if (ourras == NULL)
562     goto fail;
563 greg 2.44 if (greyscale)
564 greg 2.7 getgrey();
565 greg 2.13 else
566     getmapped();
567     if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
568     goto fail;
569 greg 1.1 }
570 greg 2.13 return;
571 greg 1.1 fail:
572 greg 1.9 quiterr("could not create raster image");
573 greg 1.1 }
574    
575    
576     getevent() /* process the next event */
577     {
578 greg 2.24 XEvent xev;
579 greg 1.1
580 greg 2.24 XNextEvent(thedisplay, &xev);
581     switch ((int)xev.type) {
582 greg 1.1 case KeyPress:
583 greg 2.24 docom(&xev.xkey);
584 greg 1.1 break;
585     case ConfigureNotify:
586 greg 2.24 width = xev.xconfigure.width;
587     height = xev.xconfigure.height;
588 greg 1.1 break;
589     case MapNotify:
590     map_rcolors(ourras, wind);
591     if (fast)
592 greg 2.6 make_rpixmap(ourras, wind);
593 greg 2.41 if (!sequential & parent < 0 & sigrecv == 0) {
594 greg 2.39 kill(getppid(), SIGCONT);
595     sigrecv--;
596     }
597 greg 1.1 break;
598     case UnmapNotify:
599 greg 2.7 if (!fast)
600     unmap_rcolors(ourras);
601 greg 1.1 break;
602     case Expose:
603 greg 2.24 redraw(xev.xexpose.x, xev.xexpose.y,
604     xev.xexpose.width, xev.xexpose.height);
605 greg 1.1 break;
606     case ButtonPress:
607 greg 2.24 if (xev.xbutton.state & (ShiftMask|ControlMask))
608     moveimage(&xev.xbutton);
609 greg 1.1 else
610 greg 2.43 switch (xev.xbutton.button) {
611     case Button1:
612     getbox(&xev.xbutton);
613     break;
614     case Button2:
615     traceray(xev.xbutton.x, xev.xbutton.y);
616     break;
617     case Button3:
618     trackrays(&xev.xbutton);
619     break;
620     }
621 greg 1.1 break;
622 greg 2.29 case ClientMessage:
623 greg 2.24 if ((xev.xclient.message_type == wmProtocolsAtom) &&
624     (xev.xclient.data.l[0] == closedownAtom))
625     quiterr(NULL);
626     break;
627 greg 1.1 }
628     }
629    
630    
631 greg 2.42 traceray(xpos, ypos) /* print requested pixel data */
632 greg 2.22 int xpos, ypos;
633     {
634 greg 2.42 extern char *index();
635 greg 2.22 FLOAT hv[2];
636     FVECT rorg, rdir;
637 greg 2.42 COLOR cval;
638     register char *cp;
639 greg 2.22
640 greg 2.42 box.xmin = xpos; box.xsiz = 1;
641     box.ymin = ypos; box.ysiz = 1;
642     avgbox(cval);
643     scalecolor(cval, 1./exposure);
644 greg 2.22 pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
645 greg 2.42 if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
646     rorg[0] = rorg[1] = rorg[2] =
647     rdir[0] = rdir[1] = rdir[2] = 0.;
648    
649     for (cp = tout; *cp; cp++) /* print what they asked for */
650     switch (*cp) {
651     case 'o': /* origin */
652     printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
653     break;
654     case 'd': /* direction */
655     printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
656     break;
657     case 'v': /* radiance value */
658     printf("%e %e %e ", colval(cval,RED),
659     colval(cval,GRN), colval(cval,BLU));
660     break;
661     case 'l': /* luminance */
662     printf("%e ", luminance(cval));
663     break;
664     case 'p': /* pixel position */
665     printf("%d %d ", (int)(hv[0]*inpres.xr),
666     (int)(hv[1]*inpres.yr));
667     break;
668     }
669     putchar('\n');
670 greg 2.22 fflush(stdout);
671     return(0);
672     }
673    
674    
675     docom(ekey) /* execute command */
676 greg 1.1 XKeyPressedEvent *ekey;
677     {
678     char buf[80];
679     COLOR cval;
680     XColor cvx;
681     int com, n;
682     double comp;
683 greg 1.26 FLOAT hv[2];
684 greg 1.1
685     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
686     if (n == 0)
687     return(0);
688     com = buf[0];
689     switch (com) { /* interpret command */
690     case 'q':
691 greg 2.22 case 'Q':
692 greg 2.3 case CTRL('D'): /* quit */
693 greg 2.21 quiterr(NULL);
694 greg 1.1 case '\n':
695     case '\r':
696     case 'l':
697     case 'c': /* value */
698 gregl 2.50 if (!avgbox(cval))
699 greg 1.1 return(-1);
700     switch (com) {
701     case '\n':
702     case '\r': /* radiance */
703     sprintf(buf, "%.3f", intens(cval)/exposure);
704     break;
705     case 'l': /* luminance */
706 greg 1.16 sprintf(buf, "%.0fL", luminance(cval)/exposure);
707 greg 1.1 break;
708     case 'c': /* color */
709     comp = pow(2.0, (double)scale);
710     sprintf(buf, "(%.2f,%.2f,%.2f)",
711     colval(cval,RED)*comp,
712     colval(cval,GRN)*comp,
713     colval(cval,BLU)*comp);
714     break;
715     }
716 greg 1.3 XDrawImageString(thedisplay, wind, ourgc,
717     box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
718 greg 1.1 return(0);
719     case 'i': /* identify (contour) */
720     if (ourras->pixels == NULL)
721     return(-1);
722     n = ourdata[ekey->x-xoff+xmax*(ekey->y-yoff)];
723     n = ourras->pmap[n];
724     cvx.pixel = ourras->cdefs[n].pixel;
725     cvx.red = random() & 65535;
726     cvx.green = random() & 65535;
727     cvx.blue = random() & 65535;
728 greg 1.2 cvx.flags = DoRed|DoGreen|DoBlue;
729     XStoreColor(thedisplay, ourras->cmap, &cvx);
730 greg 1.1 return(0);
731     case 'p': /* position */
732 greg 1.26 pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
733     sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr),
734     (int)(hv[1]*inpres.yr));
735 greg 1.1 XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
736     buf, strlen(buf));
737     return(0);
738     case 't': /* trace */
739 greg 2.22 return(traceray(ekey->x, ekey->y));
740 greg 2.46 case 'a': /* auto exposure */
741 gwlarson 2.51 if (fname == NULL)
742     return(-1);
743 greg 2.46 tmflags = TM_F_CAMERA;
744     strcpy(buf, "auto exposure...");
745     goto remap;
746     case 'h': /* human response */
747 gwlarson 2.51 if (fname == NULL)
748     return(-1);
749 greg 2.46 tmflags = TM_F_HUMAN;
750     strcpy(buf, "human exposure...");
751     goto remap;
752 greg 1.1 case '=': /* adjust exposure */
753 greg 2.25 case '@': /* adaptation level */
754 gregl 2.50 if (!avgbox(cval))
755 greg 1.1 return(-1);
756 greg 2.34 comp = bright(cval);
757     if (comp < 1e-20) {
758     XBell(thedisplay, 0);
759     return(-1);
760     }
761     if (com == '@')
762     comp = 106./exposure/
763     pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
764     else
765     comp = .5/comp;
766 greg 2.25 comp = log(comp)/.69315 - scale;
767     n = comp < 0 ? comp-.5 : comp+.5 ; /* round */
768 gregl 2.50 if (tmflags != TM_F_LINEAR)
769     tmflags = TM_F_LINEAR; /* turn off tone mapping */
770 greg 2.44 else {
771     if (n == 0) /* else check if any change */
772     return(0);
773     scale_rcolors(ourras, pow(2.0, (double)n));
774     }
775 greg 1.1 scale += n;
776     sprintf(buf, "%+d", scale);
777 greg 2.46 remap:
778 greg 1.3 XDrawImageString(thedisplay, wind, ourgc,
779     box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
780 greg 1.1 XFlush(thedisplay);
781     free(ourdata);
782     free_raster(ourras);
783     getras();
784     /* fall through */
785 greg 2.3 case CTRL('R'): /* redraw */
786     case CTRL('L'):
787 greg 1.1 unmap_rcolors(ourras);
788     XClearWindow(thedisplay, wind);
789     map_rcolors(ourras, wind);
790     if (fast)
791 greg 2.12 make_rpixmap(ourras, wind);
792 greg 1.1 redraw(0, 0, width, height);
793     return(0);
794 greg 2.27 case 'f': /* turn on fast redraw */
795     fast = 1;
796     make_rpixmap(ourras, wind);
797     return(0);
798     case 'F': /* turn off fast redraw */
799     fast = 0;
800     free_rpixmap(ourras);
801     return(0);
802 greg 2.19 case '0': /* recenter origin */
803     if (xoff == 0 & yoff == 0)
804     return(0);
805     xoff = yoff = 0;
806     XClearWindow(thedisplay, wind);
807     redraw(0, 0, width, height);
808     return(0);
809 greg 1.1 case ' ': /* clear */
810     redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
811     return(0);
812     default:
813 greg 1.2 XBell(thedisplay, 0);
814 greg 1.1 return(-1);
815     }
816     }
817    
818    
819 greg 1.2 moveimage(ebut) /* shift the image */
820     XButtonPressedEvent *ebut;
821 greg 1.1 {
822 greg 2.24 XEvent e;
823 greg 1.5 int mxo, myo;
824 greg 1.1
825 greg 2.24 XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
826     while (e.type == MotionNotify) {
827     mxo = e.xmotion.x;
828     myo = e.xmotion.y;
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 2.24 XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
833 greg 1.5 revline(ebut->x, ebut->y, mxo, myo);
834     revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
835     xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
836 greg 1.3 }
837 greg 2.24 xoff += e.xbutton.x - ebut->x;
838     yoff += e.xbutton.y - ebut->y;
839 greg 1.1 XClearWindow(thedisplay, wind);
840     redraw(0, 0, width, height);
841     }
842    
843    
844     getbox(ebut) /* get new box */
845     XButtonPressedEvent *ebut;
846     {
847 greg 2.24 XEvent e;
848 greg 1.1
849 greg 2.24 XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
850     while (e.type == MotionNotify) {
851     revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
852     XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
853 greg 1.1 revbox(ebut->x, ebut->y, box.xmin, box.ymin);
854     }
855 greg 2.24 box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
856     box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
857 greg 1.1 if (box.xmin > ebut->x) {
858     box.xsiz = box.xmin - ebut->x + 1;
859     box.xmin = ebut->x;
860     } else {
861     box.xsiz = ebut->x - box.xmin + 1;
862     }
863     if (box.ymin > ebut->y) {
864     box.ysiz = box.ymin - ebut->y + 1;
865     box.ymin = ebut->y;
866     } else {
867     box.ysiz = ebut->y - box.ymin + 1;
868 greg 2.43 }
869     }
870    
871    
872     trackrays(ebut) /* trace rays as mouse moves */
873     XButtonPressedEvent *ebut;
874     {
875     XEvent e;
876     unsigned long lastrept;
877    
878     traceray(ebut->x, ebut->y);
879     lastrept = ebut->time;
880     XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
881     while (e.type == MotionNotify) {
882     if (e.xmotion.time >= lastrept + tinterv) {
883     traceray(e.xmotion.x, e.xmotion.y);
884     lastrept = e.xmotion.time;
885     }
886     XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
887 greg 1.1 }
888     }
889    
890    
891     revbox(x0, y0, x1, y1) /* draw box with reversed lines */
892     int x0, y0, x1, y1;
893     {
894 greg 1.5 revline(x0, y0, x1, y0);
895     revline(x0, y1, x1, y1);
896     revline(x0, y0, x0, y1);
897     revline(x1, y0, x1, y1);
898     }
899 greg 1.1
900    
901 gregl 2.50 int
902     colavg(scn, n, cavg)
903     register COLR *scn;
904     register int n;
905     COLOR cavg;
906 greg 1.1 {
907 gregl 2.50 COLOR col;
908    
909     while (n--) {
910     colr_color(col, scn++);
911     addcolor(cavg, col);
912     }
913     }
914    
915    
916     int
917     avgbox(cavg) /* average color over current box */
918     COLOR cavg;
919     {
920     double d;
921     register int rval;
922    
923     setcolor(cavg, 0., 0., 0.);
924     rval = dobox(colavg, (char *)cavg);
925     if (rval > 0) {
926     d = 1./rval;
927     scalecolor(cavg, d);
928     }
929     return(rval);
930     }
931    
932    
933     int
934     dobox(f, p) /* run function over box */
935     int (*f)(); /* function to call for each subscan */
936     char *p; /* pointer to private data */
937     {
938 greg 1.1 int left, right, top, bottom;
939     int y;
940    
941     left = box.xmin - xoff;
942     right = left + box.xsiz;
943     if (left < 0)
944     left = 0;
945     if (right > xmax)
946     right = xmax;
947     if (left >= right)
948 gregl 2.50 return(0);
949 greg 1.1 top = box.ymin - yoff;
950     bottom = top + box.ysiz;
951     if (top < 0)
952     top = 0;
953     if (bottom > ymax)
954     bottom = ymax;
955     if (top >= bottom)
956 greg 2.15 return(0);
957 greg 1.1 for (y = top; y < bottom; y++) {
958     if (getscan(y) == -1)
959     return(-1);
960 gregl 2.50 (*f)(scanline+left, right-left, p);
961 greg 1.1 }
962 gregl 2.50 return((right-left)*(bottom-top));
963 greg 1.1 }
964    
965    
966 gregl 2.50 int
967     addfix(scn, n) /* add fixation points to histogram */
968     COLR *scn;
969     int n;
970     {
971     if (tmCvColrs(lscan, TM_NOCHROM, scn, n))
972     goto tmerr;
973     if (tmAddHisto(lscan, n, FIXWEIGHT))
974     goto tmerr;
975     return;
976     tmerr:
977     quiterr("tone mapping error");
978     }
979    
980    
981 greg 2.44 make_tonemap() /* initialize tone mapping */
982     {
983 greg 2.48 int flags, y;
984 greg 2.44
985 gregl 2.50 if (tmflags != TM_F_LINEAR && fname == NULL) {
986 greg 2.44 fprintf(stderr, "%s: cannot adjust tone of standard input\n",
987     progname);
988 gregl 2.50 tmflags = TM_F_LINEAR;
989 greg 2.44 }
990 gregl 2.50 if (tmflags == TM_F_LINEAR) { /* linear with clamping */
991 greg 2.44 setcolrcor(pow, 1.0/gamcor);
992     return;
993     }
994 greg 2.49 flags = tmflags; /* histogram adjustment */
995 greg 2.48 if (greyscale) flags |= TM_F_BW;
996 greg 2.49 if (tmTop != NULL) { /* reuse old histogram if one */
997 gregl 2.50 tmDone(tmTop);
998 greg 2.49 tmTop->flags = flags;
999     } else { /* else initialize */
1000     if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
1001 greg 2.44 goto memerr;
1002 greg 2.49 if (greyscale) {
1003     cscan = TM_NOCHROM;
1004     if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
1005     goto memerr;
1006     } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax))
1007     == NULL)
1008     goto memerr;
1009     /* initialize tm library */
1010     if (tmInit(flags, stdprims, gamcor) == NULL)
1011     goto memerr;
1012     if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
1013 greg 2.44 goto tmerr;
1014 greg 2.49 /* compute picture histogram */
1015     for (y = 0; y < ymax; y++) {
1016     getscan(y);
1017     if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
1018     goto tmerr;
1019     if (tmAddHisto(lscan, xmax, 1))
1020     goto tmerr;
1021     }
1022 greg 2.44 }
1023 gregl 2.50 tmDup(); /* add fixations to duplicate map */
1024     dobox(addfix, NULL);
1025 greg 2.49 /* (re)compute tone mapping */
1026 greg 2.44 if (tmComputeMapping(gamcor, 0., 0.))
1027     goto tmerr;
1028     return;
1029     memerr:
1030     quiterr("out of memory in make_tonemap");
1031     tmerr:
1032     quiterr("tone mapping error");
1033     }
1034    
1035    
1036     tmap_colrs(scn, len) /* apply tone mapping to scanline */
1037     register COLR *scn;
1038     int len;
1039     {
1040     register BYTE *ps;
1041    
1042 gregl 2.50 if (tmflags == TM_F_LINEAR) {
1043 greg 2.44 if (scale)
1044     shiftcolrs(scn, len, scale);
1045     colrs_gambs(scn, len);
1046     return;
1047     }
1048     if (len > xmax)
1049     quiterr("code error 1 in tmap_colrs");
1050     if (tmCvColrs(lscan, cscan, scn, len))
1051     goto tmerr;
1052     if (tmMapPixels(pscan, lscan, cscan, len))
1053     goto tmerr;
1054     ps = pscan;
1055 greg 2.48 if (greyscale)
1056 greg 2.44 while (len--) {
1057     scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1058     scn[0][EXP] = COLXS;
1059     scn++;
1060     }
1061     else
1062     while (len--) {
1063     scn[0][RED] = *ps++;
1064     scn[0][GRN] = *ps++;
1065     scn[0][BLU] = *ps++;
1066     scn[0][EXP] = COLXS;
1067     scn++;
1068     }
1069     return;
1070     tmerr:
1071     quiterr("tone mapping error");
1072     }
1073    
1074    
1075 greg 1.1 getmono() /* get monochrome data */
1076     {
1077     register unsigned char *dp;
1078     register int x, err;
1079 greg 1.23 int y, errp;
1080 greg 1.1 short *cerr;
1081    
1082 greg 1.10 if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
1083 greg 1.9 quiterr("out of memory in getmono");
1084 greg 1.1 dp = ourdata - 1;
1085     for (y = 0; y < ymax; y++) {
1086 greg 2.15 getscan(y);
1087 greg 2.16 add2icon(y, scanline);
1088 greg 1.10 normcolrs(scanline, xmax, scale);
1089 greg 1.1 err = 0;
1090     for (x = 0; x < xmax; x++) {
1091     if (!(x&7))
1092     *++dp = 0;
1093 greg 1.23 errp = err;
1094 greg 1.10 err += normbright(scanline[x]) + cerr[x];
1095 greg 1.1 if (err > 127)
1096     err -= 255;
1097     else
1098 greg 1.3 *dp |= 1<<(7-(x&07));
1099 greg 1.23 err /= 3;
1100     cerr[x] = err + errp;
1101 greg 1.1 }
1102     }
1103     free((char *)cerr);
1104     }
1105    
1106    
1107 greg 1.17 add2icon(y, scan) /* add a scanline to our icon data */
1108     int y;
1109     COLR *scan;
1110     {
1111     static short cerr[ICONSIZ];
1112 greg 1.20 static int ynext;
1113     static char *dp;
1114 greg 2.17 COLR clr;
1115 greg 1.17 register int err;
1116 greg 1.20 register int x, ti;
1117 greg 1.23 int errp;
1118 greg 1.17
1119     if (iconheight == 0) { /* initialize */
1120 greg 1.18 if (xmax <= ICONSIZ && ymax <= ICONSIZ) {
1121 greg 1.17 iconwidth = xmax;
1122     iconheight = ymax;
1123     } else if (xmax > ymax) {
1124     iconwidth = ICONSIZ;
1125     iconheight = ICONSIZ*ymax/xmax;
1126 greg 1.24 if (iconheight < 1)
1127     iconheight = 1;
1128 greg 1.17 } else {
1129     iconwidth = ICONSIZ*xmax/ymax;
1130 greg 1.24 if (iconwidth < 1)
1131     iconwidth = 1;
1132 greg 1.17 iconheight = ICONSIZ;
1133     }
1134 greg 1.20 ynext = 0;
1135 greg 1.17 dp = icondata - 1;
1136     }
1137 greg 1.20 if (y < ynext*ymax/iconheight) /* skip this one */
1138 greg 1.17 return;
1139     err = 0;
1140     for (x = 0; x < iconwidth; x++) {
1141     if (!(x&7))
1142     *++dp = 0;
1143 greg 1.23 errp = err;
1144 greg 1.20 ti = x*xmax/iconwidth;
1145 greg 2.17 copycolr(clr, scan[ti]);
1146     normcolrs(clr, 1, scale);
1147     err += normbright(clr) + cerr[x];
1148 greg 1.17 if (err > 127)
1149     err -= 255;
1150     else
1151     *dp |= 1<<(x&07);
1152 greg 1.23 err /= 3;
1153     cerr[x] = err + errp;
1154 greg 1.17 }
1155 greg 1.20 ynext++;
1156 greg 1.17 }
1157    
1158    
1159 greg 1.1 getfull() /* get full (24-bit) data */
1160     {
1161     int y;
1162 greg 2.28 register unsigned int4 *dp;
1163 gwlarson 2.55 register unsigned int2 *dph;
1164 greg 1.10 register int x;
1165 greg 2.44 /* initialize tone mapping */
1166     make_tonemap();
1167 greg 1.10 /* read and convert file */
1168 greg 2.28 dp = (unsigned int4 *)ourdata;
1169 gwlarson 2.55 dph = (unsigned int2 *)ourdata;
1170 greg 1.10 for (y = 0; y < ymax; y++) {
1171 greg 2.15 getscan(y);
1172 greg 2.16 add2icon(y, scanline);
1173 greg 2.44 tmap_colrs(scanline, xmax);
1174 gwlarson 2.55 switch (ourras->image->blue_mask) {
1175     case 0xff: /* 24-bit RGB */
1176 greg 2.6 for (x = 0; x < xmax; x++)
1177 greg 2.35 *dp++ = (unsigned int4)scanline[x][RED] << 16 |
1178     (unsigned int4)scanline[x][GRN] << 8 |
1179     (unsigned int4)scanline[x][BLU] ;
1180 gwlarson 2.55 break;
1181     case 0xff0000: /* 24-bit BGR */
1182 greg 2.6 for (x = 0; x < xmax; x++)
1183 greg 2.35 *dp++ = (unsigned int4)scanline[x][RED] |
1184     (unsigned int4)scanline[x][GRN] << 8 |
1185     (unsigned int4)scanline[x][BLU] << 16 ;
1186 gwlarson 2.55 break;
1187     #if 0
1188     case 0x1f: /* 15-bit RGB */
1189     for (x = 0; x < xmax; x++)
1190     *dph++ = (scanline[x][RED] << 7 & 0x7c00) |
1191     (scanline[x][GRN] << 2 & 0x3e0) |
1192     (unsigned)scanline[x][BLU] >> 3 ;
1193     break;
1194     case 0x7c00: /* 15-bit BGR */
1195     for (x = 0; x < xmax; x++)
1196     *dph++ = (unsigned)scanline[x][RED] >> 3 |
1197     (scanline[x][GRN] << 2 & 0x3e0) |
1198     (scanline[x][BLU] << 7 & 0x7c00) ;
1199     break;
1200     #endif
1201     default: /* unknown */
1202     if (ourvis.depth > 16)
1203     for (x = 0; x < xmax; x++) {
1204     *dp = ourras->image->red_mask &
1205     ourras->image->red_mask*scanline[x][RED]/255;
1206     *dp |= ourras->image->green_mask &
1207     ourras->image->green_mask*scanline[x][GRN]/255;
1208     *dp++ |= ourras->image->blue_mask &
1209     ourras->image->blue_mask*scanline[x][BLU]/255;
1210     }
1211     else
1212     for (x = 0; x < xmax; x++) {
1213     *dph = ourras->image->red_mask &
1214     ourras->image->red_mask*scanline[x][RED]/255;
1215     *dph |= ourras->image->green_mask &
1216     ourras->image->green_mask*scanline[x][GRN]/255;
1217     *dph++ |= ourras->image->blue_mask &
1218     ourras->image->blue_mask*scanline[x][BLU]/255;
1219     }
1220     break;
1221     }
1222 greg 1.10 }
1223 greg 1.1 }
1224    
1225    
1226 greg 2.7 getgrey() /* get greyscale data */
1227     {
1228     int y;
1229     register unsigned char *dp;
1230     register int x;
1231 greg 2.44 /* initialize tone mapping */
1232     make_tonemap();
1233 greg 2.7 /* read and convert file */
1234     dp = ourdata;
1235     for (y = 0; y < ymax; y++) {
1236 greg 2.15 getscan(y);
1237 greg 2.16 add2icon(y, scanline);
1238 greg 2.44 tmap_colrs(scanline, xmax);
1239 greg 2.13 if (maxcolors < 256)
1240 greg 2.7 for (x = 0; x < xmax; x++)
1241 greg 2.44 *dp++ = ((int4)scanline[x][GRN] *
1242 greg 2.18 maxcolors + maxcolors/2) >> 8;
1243 greg 2.7 else
1244     for (x = 0; x < xmax; x++)
1245 greg 2.18 *dp++ = scanline[x][GRN];
1246 greg 2.7 }
1247 greg 2.13 for (x = 0; x < maxcolors; x++)
1248     clrtab[x][RED] = clrtab[x][GRN] =
1249 greg 2.44 clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors;
1250 greg 2.7 }
1251    
1252    
1253 greg 2.13 getmapped() /* get color-mapped data */
1254     {
1255     int y;
1256 greg 2.30 /* make sure we can do it first */
1257     if (fname == NULL)
1258     quiterr("cannot map colors from standard input");
1259 greg 2.44 /* initialize tone mapping */
1260     make_tonemap();
1261 greg 2.13 /* make histogram */
1262 greg 2.44 if (new_histo((int4)xmax*ymax) == -1)
1263 greg 2.37 quiterr("cannot initialize histogram");
1264 greg 2.13 for (y = 0; y < ymax; y++) {
1265     if (getscan(y) < 0)
1266 greg 2.30 break;
1267 greg 2.16 add2icon(y, scanline);
1268 greg 2.44 tmap_colrs(scanline, xmax);
1269 greg 2.13 cnt_colrs(scanline, xmax);
1270     }
1271     /* map pixels */
1272     if (!new_clrtab(maxcolors))
1273     quiterr("cannot create color map");
1274     for (y = 0; y < ymax; y++) {
1275 greg 2.30 getscan(y);
1276 greg 2.44 tmap_colrs(scanline, xmax);
1277 greg 2.13 if (dither)
1278     dith_colrs(ourdata+y*xmax, scanline, xmax);
1279     else
1280     map_colrs(ourdata+y*xmax, scanline, xmax);
1281     }
1282     }
1283    
1284    
1285 greg 1.1 scale_rcolors(xr, sf) /* scale color map */
1286     register XRASTER *xr;
1287     double sf;
1288     {
1289     register int i;
1290     long maxv;
1291    
1292     if (xr->pixels == NULL)
1293     return;
1294    
1295     sf = pow(sf, 1.0/gamcor);
1296     maxv = 65535/sf;
1297    
1298     for (i = xr->ncolors; i--; ) {
1299     xr->cdefs[i].red = xr->cdefs[i].red > maxv ?
1300     65535 :
1301     xr->cdefs[i].red * sf;
1302     xr->cdefs[i].green = xr->cdefs[i].green > maxv ?
1303     65535 :
1304     xr->cdefs[i].green * sf;
1305     xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ?
1306     65535 :
1307     xr->cdefs[i].blue * sf;
1308     }
1309     XStoreColors(thedisplay, xr->cmap, xr->cdefs, xr->ncolors);
1310     }
1311    
1312    
1313     getscan(y)
1314     int y;
1315     {
1316 greg 2.30 static int trunced = -1; /* truncated file? */
1317     skipit:
1318     if (trunced >= 0 && y >= trunced) {
1319     bzero(scanline, xmax*sizeof(COLR));
1320     return(-1);
1321     }
1322 greg 1.1 if (y != cury) {
1323     if (scanpos == NULL || scanpos[y] == -1)
1324     return(-1);
1325     if (fseek(fin, scanpos[y], 0) == -1)
1326 greg 1.9 quiterr("fseek error");
1327 greg 1.1 cury = y;
1328 greg 2.11 } else if (scanpos != NULL && scanpos[y] == -1)
1329 greg 1.1 scanpos[y] = ftell(fin);
1330    
1331 greg 2.30 if (freadcolrs(scanline, xmax, fin) < 0) {
1332     fprintf(stderr, "%s: %s: unfinished picture\n",
1333     progname, fname==NULL?"<stdin>":fname);
1334     trunced = y;
1335     goto skipit;
1336     }
1337 greg 1.1 cury++;
1338     return(0);
1339     }