ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.61
Committed: Mon Jun 30 14:59:12 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.60: +2 -1 lines
Log Message:
Replaced most outdated BSD function calls with their posix equivalents, and cleaned up a few other platform dependencies.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.57 static const char RCSid[] = "$Id";
3 greg 1.1 #endif
4     /*
5     * x11image.c - driver for X-windows
6     *
7     * 3/1/90
8     */
9    
10     /*
11     * Modified for X11
12     *
13     * January 1990
14     *
15     * Anat Grynberg and Greg Ward
16     */
17    
18    
19     #include "standard.h"
20    
21 schorsch 2.61 #include <string.h>
22 greg 2.29 #include <signal.h>
23 greg 1.1 #include <X11/Xlib.h>
24     #include <X11/cursorfont.h>
25     #include <X11/Xutil.h>
26 greg 2.24 #include <X11/Xatom.h>
27 greg 1.1
28 gwlarson 2.52 #include "color.h"
29 greg 2.44 #include "tonemap.h"
30 greg 1.1 #include "view.h"
31     #include "x11raster.h"
32     #include "random.h"
33 greg 2.28
34 greg 1.1 #define FONTNAME "8x13" /* text font we'll use */
35    
36 greg 2.3 #define CTRL(c) ((c)-'@')
37 greg 1.1
38     #define BORWIDTH 5 /* border width */
39    
40 greg 1.18 #define ICONSIZ (8*10) /* maximum icon dimension (even 8) */
41 greg 1.17
42 gregl 2.50 #define FIXWEIGHT 20 /* weight to add for fixation points */
43    
44 greg 1.1 #define ourscreen DefaultScreen(thedisplay)
45     #define ourroot RootWindow(thedisplay,ourscreen)
46    
47 greg 1.5 #define revline(x0,y0,x1,y1) XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1)
48    
49 greg 1.2 #define redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
50    
51 greg 1.1 double gamcor = 2.2; /* gamma correction */
52 greg 2.35 char *gamstr = NULL; /* gamma value override */
53 greg 1.1
54     int dither = 1; /* dither colors? */
55     int fast = 0; /* keep picture in Pixmap? */
56    
57 greg 2.6 char *dispname = NULL; /* our display name */
58    
59 greg 1.1 Window wind = 0; /* our output window */
60 greg 2.9 unsigned long ourblack=0, ourwhite=1; /* black and white for this visual */
61 greg 1.2 int maxcolors = 0; /* maximum colors */
62 greg 1.1 int greyscale = 0; /* in grey */
63    
64     int scale = 0; /* scalefactor; power of two */
65    
66     int xoff = 0; /* x image offset */
67     int yoff = 0; /* y image offset */
68    
69 greg 2.29 int parent = 0; /* number of children, -1 if child */
70 greg 2.41 int sequential = 0; /* display images in sequence */
71 greg 2.29
72 greg 2.42 char *tout = "od"; /* output of 't' command */
73 greg 2.43 int tinterv = 0; /* interval between mouse reports */
74 greg 2.42
75 gregl 2.50 int tmflags = TM_F_LINEAR; /* tone mapping flags */
76 greg 2.44
77 greg 1.1 VIEW ourview = STDVIEW; /* image view parameters */
78     int gotview = 0; /* got parameters from file */
79    
80     COLR *scanline; /* scan line buffer */
81 greg 2.44 TMbright *lscan; /* encoded luminance scanline */
82     BYTE *cscan; /* encoded chroma scanline */
83     BYTE *pscan; /* compute pixel scanline */
84 greg 1.1
85 greg 1.26 RESOLU inpres; /* input resolution and ordering */
86     int xmax, ymax; /* picture dimensions */
87 greg 1.1 int width, height; /* window size */
88     char *fname = NULL; /* input file name */
89 gwlarson 2.54 FILE *fin = NULL; /* input file */
90 greg 1.1 long *scanpos = NULL; /* scan line positions in file */
91     int cury = 0; /* current scan location */
92    
93     double exposure = 1.0; /* exposure compensation used */
94    
95 greg 1.14 int wrongformat = 0; /* input in another format? */
96    
97 greg 2.6 GC ourgc; /* standard graphics context */
98 greg 1.5 GC revgc; /* graphics context with GXinvert */
99    
100 greg 2.6 int *ourrank; /* our visual class ranking */
101     XVisualInfo ourvis; /* our visual */
102     XRASTER *ourras; /* our stored image */
103 greg 1.1 unsigned char *ourdata; /* our image data */
104    
105     struct {
106     int xmin, ymin, xsiz, ysiz;
107 greg 2.56 } bbox = {0, 0, 0, 0}; /* current bbox */
108 greg 1.1
109     char *geometry = NULL; /* geometry specification */
110    
111 greg 1.18 char icondata[ICONSIZ*ICONSIZ/8]; /* icon bitmap data */
112 greg 1.17 int iconwidth = 0, iconheight = 0;
113    
114 greg 1.1 char *progname;
115    
116     char errmsg[128];
117    
118 greg 2.37 BYTE clrtab[256][3]; /* global color map */
119 greg 2.13
120 greg 1.1 extern long ftell();
121    
122 greg 2.35 extern char *getenv();
123    
124 greg 1.1 Display *thedisplay;
125 greg 2.24 Atom closedownAtom, wmProtocolsAtom;
126 greg 1.1
127 greg 2.32 int sigrecv;
128 greg 2.14
129 schorsch 2.58 void onsig(int i) { sigrecv++; }
130 greg 2.29
131 greg 2.32
132 greg 1.1 main(argc, argv)
133     int argc;
134     char *argv[];
135     {
136     int headline();
137     int i;
138 greg 2.29 int pid;
139 greg 1.1
140     progname = argv[0];
141 gwlarson 2.54 fin = stdin;
142 greg 1.1
143     for (i = 1; i < argc; i++)
144     if (argv[i][0] == '-')
145     switch (argv[i][1]) {
146 greg 2.42 case 'c': /* number of colors */
147 greg 1.1 maxcolors = atoi(argv[++i]);
148     break;
149 greg 2.42 case 'b': /* greyscale only */
150 greg 1.1 greyscale = !greyscale;
151     break;
152 greg 2.42 case 'm': /* monochrome */
153 greg 2.40 greyscale = 1;
154 greg 1.1 maxcolors = 2;
155     break;
156 greg 2.42 case 'd': /* display or dither */
157 greg 2.7 if (argv[i][2] == 'i')
158 greg 2.6 dispname = argv[++i];
159 greg 2.7 else
160     dither = !dither;
161 greg 1.1 break;
162 greg 2.42 case 'f': /* save pixmap */
163 greg 1.1 fast = !fast;
164     break;
165 greg 2.42 case 's': /* one at a time */
166 greg 2.41 sequential = !sequential;
167     break;
168 greg 2.42 case 'o': /* 't' output */
169     tout = argv[i]+2;
170     break;
171 greg 2.43 case 't': /* msec interval */
172     tinterv = atoi(argv[++i]);
173     break;
174 greg 2.42 case 'e': /* exposure comp. */
175 greg 2.44 i++;
176 greg 2.47 if (argv[i][0] == 'a') {
177 greg 2.44 tmflags = TM_F_CAMERA;
178     break;
179     }
180 greg 2.47 if (argv[i][0] == 'h') {
181 greg 2.44 tmflags = TM_F_HUMAN;
182     break;
183     }
184     if (argv[i][0] != '+' && argv[i][0] != '-')
185 greg 1.1 goto userr;
186 greg 2.44 scale = atoi(argv[i]);
187 greg 1.1 break;
188 greg 2.42 case 'g': /* gamma comp. */
189 greg 2.7 if (argv[i][2] == 'e')
190 greg 1.1 geometry = argv[++i];
191     else
192 greg 2.35 gamstr = argv[++i];
193 greg 1.1 break;
194     default:
195     goto userr;
196     }
197 greg 1.3 else if (argv[i][0] == '=')
198     geometry = argv[i];
199 greg 1.1 else
200     break;
201    
202 greg 2.29 if (i > argc)
203     goto userr;
204     while (i < argc-1) {
205 greg 2.32 sigrecv = 0;
206     signal(SIGCONT, onsig);
207 greg 2.29 if ((pid=fork()) == 0) { /* a child for each picture */
208     parent = -1;
209     break;
210     }
211     if (pid < 0)
212     quiterr("fork failed");
213     parent++;
214 greg 2.32 while (!sigrecv)
215     pause(); /* wait for wake-up call */
216 greg 2.29 i++;
217     }
218     if (i < argc) { /* open picture file */
219 greg 1.1 fname = argv[i];
220     fin = fopen(fname, "r");
221 greg 2.33 if (fin == NULL)
222     quiterr("cannot open picture file");
223 greg 2.29 }
224 greg 1.1 /* get header */
225 greg 1.14 getheader(fin, headline, NULL);
226 greg 1.1 /* get picture dimensions */
227 greg 1.26 if (wrongformat || !fgetsresolu(&inpres, fin))
228 greg 1.14 quiterr("bad picture format");
229 greg 1.26 xmax = scanlen(&inpres);
230     ymax = numscans(&inpres);
231 greg 1.1 /* set view parameters */
232     if (gotview && setview(&ourview) != NULL)
233     gotview = 0;
234     if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
235     quiterr("out of memory");
236    
237 greg 2.24 init(argc, argv); /* get file and open window */
238 greg 1.1
239     for ( ; ; )
240     getevent(); /* main loop */
241     userr:
242     fprintf(stderr,
243 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",
244 greg 1.1 progname);
245 greg 2.21 exit(1);
246 greg 1.1 }
247    
248    
249 gwlarson 2.53 int
250 greg 1.1 headline(s) /* get relevant info from header */
251     char *s;
252     {
253 greg 1.14 char fmt[32];
254 greg 1.1
255 greg 1.2 if (isexpos(s))
256     exposure *= exposval(s);
257 greg 2.44 else if (formatval(fmt, s))
258 greg 1.14 wrongformat = strcmp(fmt, COLRFMT);
259 greg 2.44 else if (isview(s) && sscanview(&ourview, s) > 0)
260 greg 2.4 gotview++;
261 gwlarson 2.53 return(0);
262 greg 1.1 }
263    
264    
265 greg 2.24 init(argc, argv) /* get data and open window */
266     int argc;
267     char **argv;
268 greg 1.1 {
269 greg 1.4 XSetWindowAttributes ourwinattr;
270 greg 2.24 XClassHint xclshints;
271     XWMHints xwmhints;
272     XSizeHints xszhints;
273     XTextProperty windowName, iconName;
274     XGCValues xgcv;
275     char *name;
276 greg 2.6 register int i;
277 greg 1.1
278     if (fname != NULL) {
279     scanpos = (long *)malloc(ymax*sizeof(long));
280     if (scanpos == NULL)
281 greg 2.24 quiterr("out of memory");
282 greg 1.1 for (i = 0; i < ymax; i++)
283     scanpos[i] = -1;
284 greg 2.24 name = fname;
285     } else
286     name = progname;
287     /* remove directory prefix from name */
288     for (i = strlen(name); i-- > 0; )
289     if (name[i] == '/')
290     break;
291     name += i+1;
292 greg 2.6 if ((thedisplay = XOpenDisplay(dispname)) == NULL)
293     quiterr("cannot open display");
294 greg 2.35 /* set gamma value */
295     if (gamstr == NULL) /* get it from the X server */
296     gamstr = XGetDefault(thedisplay, "radiance", "gamma");
297     if (gamstr == NULL) /* get it from the environment */
298 greg 2.38 gamstr = getenv("DISPLAY_GAMMA");
299 greg 2.35 if (gamstr != NULL)
300     gamcor = atof(gamstr);
301 greg 2.6 /* get best visual for default screen */
302     getbestvis();
303 greg 1.4 /* store image */
304     getras();
305 greg 2.20 /* get size and position */
306 greg 2.24 xszhints.flags = 0;
307     xszhints.width = xmax; xszhints.height = ymax;
308 greg 2.20 if (geometry != NULL) {
309 greg 2.24 i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
310     (unsigned *)&xszhints.width,
311     (unsigned *)&xszhints.height);
312 greg 2.20 if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
313 greg 2.24 xszhints.flags |= USSize;
314 greg 2.20 else
315 greg 2.24 xszhints.flags |= PSize;
316 greg 2.20 if ((i&(XValue|YValue)) == (XValue|YValue)) {
317 greg 2.24 xszhints.flags |= USPosition;
318 greg 2.20 if (i & XNegative)
319 greg 2.24 xszhints.x += DisplayWidth(thedisplay,
320     ourscreen)-1-xszhints.width-2*BORWIDTH;
321 greg 2.20 if (i & YNegative)
322 greg 2.24 xszhints.y += DisplayHeight(thedisplay,
323     ourscreen)-1-xszhints.height-2*BORWIDTH;
324 greg 2.20 }
325     }
326 greg 2.24 /* open window */
327 greg 2.40 i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
328 greg 2.23 ourwinattr.border_pixel = ourwhite;
329     ourwinattr.background_pixel = ourblack;
330 greg 2.40 if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
331     ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
332     i |= CWColormap;
333     }
334 greg 2.24 ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
335     ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
336     ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
337     wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
338     xszhints.width, xszhints.height, BORWIDTH,
339 greg 2.40 ourvis.depth, InputOutput, ourvis.visual,
340     i, &ourwinattr);
341 greg 1.4 if (wind == 0)
342 greg 2.6 quiterr("cannot create window");
343 greg 1.5 width = xmax;
344     height = ymax;
345 greg 2.32 /* prepare graphics drawing context */
346     if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
347     quiterr("cannot get font");
348 greg 2.24 xgcv.foreground = ourblack;
349     xgcv.background = ourwhite;
350     ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
351     GCFont, &xgcv);
352     xgcv.function = GXinvert;
353     revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
354     GCFunction, &xgcv);
355    
356     /* set up the window manager */
357     xwmhints.flags = InputHint|IconPixmapHint;
358     xwmhints.input = True;
359     xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
360 greg 1.17 wind, icondata, iconwidth, iconheight);
361 greg 2.24
362     windowName.encoding = iconName.encoding = XA_STRING;
363     windowName.format = iconName.format = 8;
364     windowName.value = (u_char *)name;
365     windowName.nitems = strlen(windowName.value);
366     iconName.value = (u_char *)name;
367     iconName.nitems = strlen(windowName.value);
368    
369     xclshints.res_name = NULL;
370     xclshints.res_class = "Ximage";
371     XSetWMProperties(thedisplay, wind, &windowName, &iconName,
372     argv, argc, &xszhints, &xwmhints, &xclshints);
373     closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
374     wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
375     XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
376    
377 greg 1.1 XMapWindow(thedisplay, wind);
378     } /* end of init */
379    
380    
381     quiterr(err) /* print message and exit */
382     char *err;
383     {
384 greg 2.31 register int es;
385     int cs;
386    
387     if (es = err != NULL)
388 greg 2.29 fprintf(stderr, "%s: %s: %s\n", progname,
389     fname==NULL?"<stdin>":fname, err);
390 greg 2.36 if (thedisplay != NULL)
391     XCloseDisplay(thedisplay);
392     if (parent < 0 & sigrecv == 0)
393 greg 2.32 kill(getppid(), SIGCONT);
394 greg 2.31 while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
395     if (es == 0)
396     es = cs>>8 & 0xff;
397 greg 2.29 parent--;
398 greg 2.31 }
399     exit(es);
400 greg 1.1 }
401    
402    
403 greg 2.6 static int
404     viscmp(v1,v2) /* compare visual to see which is better, descending */
405     register XVisualInfo *v1, *v2;
406     {
407     int bad1 = 0, bad2 = 0;
408     register int *rp;
409    
410     if (v1->class == v2->class) {
411     if (v1->class == TrueColor || v1->class == DirectColor) {
412     /* prefer 24-bit to 32-bit */
413     if (v1->depth == 24 && v2->depth == 32)
414     return(-1);
415     if (v1->depth == 32 && v2->depth == 24)
416     return(1);
417 gwlarson 2.55 /* go for maximum depth otherwise */
418     return(v2->depth - v1->depth);
419 greg 2.6 }
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 gwlarson 2.55 /* prefer Pseudo when < 15-bit */
426 greg 2.6 if ((v1->class == TrueColor || v1->class == DirectColor) &&
427 gwlarson 2.55 v1->depth < 15)
428 greg 2.6 bad1 = 1;
429     if ((v2->class == TrueColor || v2->class == DirectColor) &&
430 gwlarson 2.55 v2->depth < 15)
431 greg 2.6 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.59 int datsiz = ourvis.depth>16 ? sizeof(int32) : sizeof(int16);
542 gwlarson 2.55 ourdata = (unsigned char *)malloc(datsiz*xmax*ymax);
543 greg 1.1 if (ourdata == NULL)
544     goto fail;
545 gwlarson 2.55 ourras = make_raster(thedisplay, &ourvis, datsiz*8,
546     ourdata, xmax, ymax, datsiz*8);
547 greg 1.1 if (ourras == NULL)
548     goto fail;
549     getfull();
550     } else {
551     ourdata = (unsigned char *)malloc(xmax*ymax);
552     if (ourdata == NULL)
553     goto fail;
554 greg 2.6 ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
555 greg 1.1 xmax, ymax, 8);
556     if (ourras == NULL)
557     goto fail;
558 greg 2.44 if (greyscale)
559 greg 2.7 getgrey();
560 greg 2.13 else
561     getmapped();
562     if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
563     goto fail;
564 greg 1.1 }
565 greg 2.13 return;
566 greg 1.1 fail:
567 greg 1.9 quiterr("could not create raster image");
568 greg 1.1 }
569    
570    
571     getevent() /* process the next event */
572     {
573 greg 2.24 XEvent xev;
574 greg 1.1
575 greg 2.24 XNextEvent(thedisplay, &xev);
576     switch ((int)xev.type) {
577 greg 1.1 case KeyPress:
578 greg 2.24 docom(&xev.xkey);
579 greg 1.1 break;
580     case ConfigureNotify:
581 greg 2.24 width = xev.xconfigure.width;
582     height = xev.xconfigure.height;
583 greg 1.1 break;
584     case MapNotify:
585     map_rcolors(ourras, wind);
586     if (fast)
587 greg 2.6 make_rpixmap(ourras, wind);
588 greg 2.41 if (!sequential & parent < 0 & sigrecv == 0) {
589 greg 2.39 kill(getppid(), SIGCONT);
590     sigrecv--;
591     }
592 greg 1.1 break;
593     case UnmapNotify:
594 greg 2.7 if (!fast)
595     unmap_rcolors(ourras);
596 greg 1.1 break;
597     case Expose:
598 greg 2.24 redraw(xev.xexpose.x, xev.xexpose.y,
599     xev.xexpose.width, xev.xexpose.height);
600 greg 1.1 break;
601     case ButtonPress:
602 greg 2.24 if (xev.xbutton.state & (ShiftMask|ControlMask))
603     moveimage(&xev.xbutton);
604 greg 1.1 else
605 greg 2.43 switch (xev.xbutton.button) {
606     case Button1:
607     getbox(&xev.xbutton);
608     break;
609     case Button2:
610     traceray(xev.xbutton.x, xev.xbutton.y);
611     break;
612     case Button3:
613     trackrays(&xev.xbutton);
614     break;
615     }
616 greg 1.1 break;
617 greg 2.29 case ClientMessage:
618 greg 2.24 if ((xev.xclient.message_type == wmProtocolsAtom) &&
619     (xev.xclient.data.l[0] == closedownAtom))
620     quiterr(NULL);
621     break;
622 greg 1.1 }
623     }
624    
625    
626 greg 2.42 traceray(xpos, ypos) /* print requested pixel data */
627 greg 2.22 int xpos, ypos;
628     {
629 schorsch 2.60 RREAL hv[2];
630 greg 2.22 FVECT rorg, rdir;
631 greg 2.42 COLOR cval;
632     register char *cp;
633 greg 2.22
634 greg 2.56 bbox.xmin = xpos; bbox.xsiz = 1;
635     bbox.ymin = ypos; bbox.ysiz = 1;
636 greg 2.42 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 schorsch 2.60 RREAL 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 gregl 2.50 if (!avgbox(cval))
693 greg 1.1 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 2.56 sprintf(buf, "%.1fL", 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 greg 2.56 bbox.xmin, bbox.ymin+bbox.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 2.46 case 'a': /* auto exposure */
735 gwlarson 2.51 if (fname == NULL)
736     return(-1);
737 greg 2.46 tmflags = TM_F_CAMERA;
738     strcpy(buf, "auto exposure...");
739     goto remap;
740     case 'h': /* human response */
741 gwlarson 2.51 if (fname == NULL)
742     return(-1);
743 greg 2.46 tmflags = TM_F_HUMAN;
744     strcpy(buf, "human exposure...");
745     goto remap;
746 greg 1.1 case '=': /* adjust exposure */
747 greg 2.25 case '@': /* adaptation level */
748 gregl 2.50 if (!avgbox(cval))
749 greg 1.1 return(-1);
750 greg 2.34 comp = bright(cval);
751     if (comp < 1e-20) {
752     XBell(thedisplay, 0);
753     return(-1);
754     }
755     if (com == '@')
756     comp = 106./exposure/
757     pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
758     else
759     comp = .5/comp;
760 greg 2.25 comp = log(comp)/.69315 - scale;
761     n = comp < 0 ? comp-.5 : comp+.5 ; /* round */
762 gregl 2.50 if (tmflags != TM_F_LINEAR)
763     tmflags = TM_F_LINEAR; /* turn off tone mapping */
764 greg 2.44 else {
765     if (n == 0) /* else check if any change */
766     return(0);
767     scale_rcolors(ourras, pow(2.0, (double)n));
768     }
769 greg 1.1 scale += n;
770     sprintf(buf, "%+d", scale);
771 greg 2.46 remap:
772 greg 1.3 XDrawImageString(thedisplay, wind, ourgc,
773 greg 2.56 bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
774 greg 1.1 XFlush(thedisplay);
775 greg 2.56 /* free(ourdata); This is done in XDestroyImage()! */
776 greg 1.1 free_raster(ourras);
777     getras();
778     /* fall through */
779 greg 2.3 case CTRL('R'): /* redraw */
780     case CTRL('L'):
781 greg 1.1 unmap_rcolors(ourras);
782     XClearWindow(thedisplay, wind);
783     map_rcolors(ourras, wind);
784     if (fast)
785 greg 2.12 make_rpixmap(ourras, wind);
786 greg 1.1 redraw(0, 0, width, height);
787     return(0);
788 greg 2.27 case 'f': /* turn on fast redraw */
789     fast = 1;
790     make_rpixmap(ourras, wind);
791     return(0);
792     case 'F': /* turn off fast redraw */
793     fast = 0;
794     free_rpixmap(ourras);
795     return(0);
796 greg 2.19 case '0': /* recenter origin */
797     if (xoff == 0 & yoff == 0)
798     return(0);
799     xoff = yoff = 0;
800     XClearWindow(thedisplay, wind);
801     redraw(0, 0, width, height);
802     return(0);
803 greg 1.1 case ' ': /* clear */
804 greg 2.56 redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz);
805 greg 1.1 return(0);
806     default:
807 greg 1.2 XBell(thedisplay, 0);
808 greg 1.1 return(-1);
809     }
810     }
811    
812    
813 greg 1.2 moveimage(ebut) /* shift the image */
814     XButtonPressedEvent *ebut;
815 greg 1.1 {
816 greg 2.24 XEvent e;
817 greg 1.5 int mxo, myo;
818 greg 1.1
819 greg 2.24 XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
820     while (e.type == MotionNotify) {
821     mxo = e.xmotion.x;
822     myo = e.xmotion.y;
823 greg 1.5 revline(ebut->x, ebut->y, mxo, myo);
824     revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
825     xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
826 greg 2.24 XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
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 1.3 }
831 greg 2.24 xoff += e.xbutton.x - ebut->x;
832     yoff += e.xbutton.y - ebut->y;
833 greg 1.1 XClearWindow(thedisplay, wind);
834     redraw(0, 0, width, height);
835     }
836    
837    
838 greg 2.56 getbox(ebut) /* get new bbox */
839 greg 1.1 XButtonPressedEvent *ebut;
840     {
841 greg 2.24 XEvent e;
842 greg 1.1
843 greg 2.24 XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
844     while (e.type == MotionNotify) {
845 greg 2.56 revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y);
846 greg 2.24 XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
847 greg 2.56 revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin);
848 greg 1.1 }
849 greg 2.56 bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
850     bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
851     if (bbox.xmin > ebut->x) {
852     bbox.xsiz = bbox.xmin - ebut->x + 1;
853     bbox.xmin = ebut->x;
854 greg 1.1 } else {
855 greg 2.56 bbox.xsiz = ebut->x - bbox.xmin + 1;
856 greg 1.1 }
857 greg 2.56 if (bbox.ymin > ebut->y) {
858     bbox.ysiz = bbox.ymin - ebut->y + 1;
859     bbox.ymin = ebut->y;
860 greg 1.1 } else {
861 greg 2.56 bbox.ysiz = ebut->y - bbox.ymin + 1;
862 greg 2.43 }
863     }
864    
865    
866     trackrays(ebut) /* trace rays as mouse moves */
867     XButtonPressedEvent *ebut;
868     {
869     XEvent e;
870     unsigned long lastrept;
871    
872     traceray(ebut->x, ebut->y);
873     lastrept = ebut->time;
874     XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
875     while (e.type == MotionNotify) {
876     if (e.xmotion.time >= lastrept + tinterv) {
877     traceray(e.xmotion.x, e.xmotion.y);
878     lastrept = e.xmotion.time;
879     }
880     XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
881 greg 1.1 }
882     }
883    
884    
885 greg 2.56 revbox(x0, y0, x1, y1) /* draw bbox with reversed lines */
886 greg 1.1 int x0, y0, x1, y1;
887     {
888 greg 1.5 revline(x0, y0, x1, y0);
889     revline(x0, y1, x1, y1);
890     revline(x0, y0, x0, y1);
891     revline(x1, y0, x1, y1);
892     }
893 greg 1.1
894    
895 gregl 2.50 int
896     colavg(scn, n, cavg)
897     register COLR *scn;
898     register int n;
899     COLOR cavg;
900 greg 1.1 {
901 gregl 2.50 COLOR col;
902    
903     while (n--) {
904 greg 2.56 colr_color(col, *scn++);
905 gregl 2.50 addcolor(cavg, col);
906     }
907     }
908    
909    
910     int
911 greg 2.56 avgbox(cavg) /* average color over current bbox */
912 gregl 2.50 COLOR cavg;
913     {
914     double d;
915     register int rval;
916    
917     setcolor(cavg, 0., 0., 0.);
918     rval = dobox(colavg, (char *)cavg);
919     if (rval > 0) {
920     d = 1./rval;
921     scalecolor(cavg, d);
922     }
923     return(rval);
924     }
925    
926    
927     int
928 greg 2.56 dobox(f, p) /* run function over bbox */
929 gregl 2.50 int (*f)(); /* function to call for each subscan */
930     char *p; /* pointer to private data */
931     {
932 greg 1.1 int left, right, top, bottom;
933     int y;
934    
935 greg 2.56 left = bbox.xmin - xoff;
936     right = left + bbox.xsiz;
937 greg 1.1 if (left < 0)
938     left = 0;
939     if (right > xmax)
940     right = xmax;
941     if (left >= right)
942 gregl 2.50 return(0);
943 greg 2.56 top = bbox.ymin - yoff;
944     bottom = top + bbox.ysiz;
945 greg 1.1 if (top < 0)
946     top = 0;
947     if (bottom > ymax)
948     bottom = ymax;
949     if (top >= bottom)
950 greg 2.15 return(0);
951 greg 1.1 for (y = top; y < bottom; y++) {
952     if (getscan(y) == -1)
953     return(-1);
954 gregl 2.50 (*f)(scanline+left, right-left, p);
955 greg 1.1 }
956 gregl 2.50 return((right-left)*(bottom-top));
957 greg 1.1 }
958    
959    
960 gregl 2.50 int
961     addfix(scn, n) /* add fixation points to histogram */
962     COLR *scn;
963     int n;
964     {
965     if (tmCvColrs(lscan, TM_NOCHROM, scn, n))
966     goto tmerr;
967     if (tmAddHisto(lscan, n, FIXWEIGHT))
968     goto tmerr;
969     return;
970     tmerr:
971     quiterr("tone mapping error");
972     }
973    
974    
975 greg 2.44 make_tonemap() /* initialize tone mapping */
976     {
977 greg 2.48 int flags, y;
978 greg 2.44
979 gregl 2.50 if (tmflags != TM_F_LINEAR && fname == NULL) {
980 greg 2.44 fprintf(stderr, "%s: cannot adjust tone of standard input\n",
981     progname);
982 gregl 2.50 tmflags = TM_F_LINEAR;
983 greg 2.44 }
984 gregl 2.50 if (tmflags == TM_F_LINEAR) { /* linear with clamping */
985 greg 2.44 setcolrcor(pow, 1.0/gamcor);
986     return;
987     }
988 greg 2.49 flags = tmflags; /* histogram adjustment */
989 greg 2.48 if (greyscale) flags |= TM_F_BW;
990 greg 2.49 if (tmTop != NULL) { /* reuse old histogram if one */
991 gregl 2.50 tmDone(tmTop);
992 greg 2.49 tmTop->flags = flags;
993     } else { /* else initialize */
994     if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
995 greg 2.44 goto memerr;
996 greg 2.49 if (greyscale) {
997     cscan = TM_NOCHROM;
998     if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
999     goto memerr;
1000     } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax))
1001     == NULL)
1002     goto memerr;
1003     /* initialize tm library */
1004     if (tmInit(flags, stdprims, gamcor) == NULL)
1005     goto memerr;
1006     if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
1007 greg 2.44 goto tmerr;
1008 greg 2.49 /* compute picture histogram */
1009     for (y = 0; y < ymax; y++) {
1010     getscan(y);
1011     if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
1012     goto tmerr;
1013     if (tmAddHisto(lscan, xmax, 1))
1014     goto tmerr;
1015     }
1016 greg 2.44 }
1017 gregl 2.50 tmDup(); /* add fixations to duplicate map */
1018     dobox(addfix, NULL);
1019 greg 2.49 /* (re)compute tone mapping */
1020 greg 2.44 if (tmComputeMapping(gamcor, 0., 0.))
1021     goto tmerr;
1022     return;
1023     memerr:
1024     quiterr("out of memory in make_tonemap");
1025     tmerr:
1026     quiterr("tone mapping error");
1027     }
1028    
1029    
1030     tmap_colrs(scn, len) /* apply tone mapping to scanline */
1031     register COLR *scn;
1032     int len;
1033     {
1034     register BYTE *ps;
1035    
1036 gregl 2.50 if (tmflags == TM_F_LINEAR) {
1037 greg 2.44 if (scale)
1038     shiftcolrs(scn, len, scale);
1039     colrs_gambs(scn, len);
1040     return;
1041     }
1042     if (len > xmax)
1043     quiterr("code error 1 in tmap_colrs");
1044     if (tmCvColrs(lscan, cscan, scn, len))
1045     goto tmerr;
1046     if (tmMapPixels(pscan, lscan, cscan, len))
1047     goto tmerr;
1048     ps = pscan;
1049 greg 2.48 if (greyscale)
1050 greg 2.44 while (len--) {
1051     scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1052     scn[0][EXP] = COLXS;
1053     scn++;
1054     }
1055     else
1056     while (len--) {
1057     scn[0][RED] = *ps++;
1058     scn[0][GRN] = *ps++;
1059     scn[0][BLU] = *ps++;
1060     scn[0][EXP] = COLXS;
1061     scn++;
1062     }
1063     return;
1064     tmerr:
1065     quiterr("tone mapping error");
1066     }
1067    
1068    
1069 greg 1.1 getmono() /* get monochrome data */
1070     {
1071     register unsigned char *dp;
1072     register int x, err;
1073 greg 1.23 int y, errp;
1074 greg 1.1 short *cerr;
1075    
1076 greg 1.10 if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
1077 greg 1.9 quiterr("out of memory in getmono");
1078 greg 1.1 dp = ourdata - 1;
1079     for (y = 0; y < ymax; y++) {
1080 greg 2.15 getscan(y);
1081 greg 2.16 add2icon(y, scanline);
1082 greg 1.10 normcolrs(scanline, xmax, scale);
1083 greg 1.1 err = 0;
1084     for (x = 0; x < xmax; x++) {
1085     if (!(x&7))
1086     *++dp = 0;
1087 greg 1.23 errp = err;
1088 greg 1.10 err += normbright(scanline[x]) + cerr[x];
1089 greg 1.1 if (err > 127)
1090     err -= 255;
1091     else
1092 greg 1.3 *dp |= 1<<(7-(x&07));
1093 greg 1.23 err /= 3;
1094     cerr[x] = err + errp;
1095 greg 1.1 }
1096     }
1097 greg 2.56 free((void *)cerr);
1098 greg 1.1 }
1099    
1100    
1101 greg 1.17 add2icon(y, scan) /* add a scanline to our icon data */
1102     int y;
1103     COLR *scan;
1104     {
1105     static short cerr[ICONSIZ];
1106 greg 1.20 static int ynext;
1107     static char *dp;
1108 greg 2.17 COLR clr;
1109 greg 1.17 register int err;
1110 greg 1.20 register int x, ti;
1111 greg 1.23 int errp;
1112 greg 1.17
1113     if (iconheight == 0) { /* initialize */
1114 greg 1.18 if (xmax <= ICONSIZ && ymax <= ICONSIZ) {
1115 greg 1.17 iconwidth = xmax;
1116     iconheight = ymax;
1117     } else if (xmax > ymax) {
1118     iconwidth = ICONSIZ;
1119     iconheight = ICONSIZ*ymax/xmax;
1120 greg 1.24 if (iconheight < 1)
1121     iconheight = 1;
1122 greg 1.17 } else {
1123     iconwidth = ICONSIZ*xmax/ymax;
1124 greg 1.24 if (iconwidth < 1)
1125     iconwidth = 1;
1126 greg 1.17 iconheight = ICONSIZ;
1127     }
1128 greg 1.20 ynext = 0;
1129 greg 1.17 dp = icondata - 1;
1130     }
1131 greg 1.20 if (y < ynext*ymax/iconheight) /* skip this one */
1132 greg 1.17 return;
1133     err = 0;
1134     for (x = 0; x < iconwidth; x++) {
1135     if (!(x&7))
1136     *++dp = 0;
1137 greg 1.23 errp = err;
1138 greg 1.20 ti = x*xmax/iconwidth;
1139 greg 2.17 copycolr(clr, scan[ti]);
1140 greg 2.56 normcolrs(&clr, 1, scale);
1141 greg 2.17 err += normbright(clr) + cerr[x];
1142 greg 1.17 if (err > 127)
1143     err -= 255;
1144     else
1145     *dp |= 1<<(x&07);
1146 greg 1.23 err /= 3;
1147     cerr[x] = err + errp;
1148 greg 1.17 }
1149 greg 1.20 ynext++;
1150 greg 1.17 }
1151    
1152    
1153 greg 1.1 getfull() /* get full (24-bit) data */
1154     {
1155     int y;
1156 greg 2.59 register uint32 *dp;
1157     register uint16 *dph;
1158 greg 1.10 register int x;
1159 greg 2.44 /* initialize tone mapping */
1160     make_tonemap();
1161 greg 1.10 /* read and convert file */
1162 greg 2.59 dp = (uint32 *)ourdata;
1163     dph = (uint16 *)ourdata;
1164 greg 1.10 for (y = 0; y < ymax; y++) {
1165 greg 2.15 getscan(y);
1166 greg 2.16 add2icon(y, scanline);
1167 greg 2.44 tmap_colrs(scanline, xmax);
1168 gwlarson 2.55 switch (ourras->image->blue_mask) {
1169     case 0xff: /* 24-bit RGB */
1170 greg 2.6 for (x = 0; x < xmax; x++)
1171 greg 2.59 *dp++ = (uint32)scanline[x][RED] << 16 |
1172     (uint32)scanline[x][GRN] << 8 |
1173     (uint32)scanline[x][BLU] ;
1174 gwlarson 2.55 break;
1175     case 0xff0000: /* 24-bit BGR */
1176 greg 2.6 for (x = 0; x < xmax; x++)
1177 greg 2.59 *dp++ = (uint32)scanline[x][RED] |
1178     (uint32)scanline[x][GRN] << 8 |
1179     (uint32)scanline[x][BLU] << 16 ;
1180 gwlarson 2.55 break;
1181     #if 0
1182     case 0x1f: /* 15-bit RGB */
1183     for (x = 0; x < xmax; x++)
1184     *dph++ = (scanline[x][RED] << 7 & 0x7c00) |
1185     (scanline[x][GRN] << 2 & 0x3e0) |
1186     (unsigned)scanline[x][BLU] >> 3 ;
1187     break;
1188     case 0x7c00: /* 15-bit BGR */
1189     for (x = 0; x < xmax; x++)
1190     *dph++ = (unsigned)scanline[x][RED] >> 3 |
1191     (scanline[x][GRN] << 2 & 0x3e0) |
1192     (scanline[x][BLU] << 7 & 0x7c00) ;
1193     break;
1194     #endif
1195     default: /* unknown */
1196     if (ourvis.depth > 16)
1197     for (x = 0; x < xmax; x++) {
1198     *dp = ourras->image->red_mask &
1199     ourras->image->red_mask*scanline[x][RED]/255;
1200     *dp |= ourras->image->green_mask &
1201     ourras->image->green_mask*scanline[x][GRN]/255;
1202     *dp++ |= ourras->image->blue_mask &
1203     ourras->image->blue_mask*scanline[x][BLU]/255;
1204     }
1205     else
1206     for (x = 0; x < xmax; x++) {
1207     *dph = ourras->image->red_mask &
1208     ourras->image->red_mask*scanline[x][RED]/255;
1209     *dph |= ourras->image->green_mask &
1210     ourras->image->green_mask*scanline[x][GRN]/255;
1211     *dph++ |= ourras->image->blue_mask &
1212     ourras->image->blue_mask*scanline[x][BLU]/255;
1213     }
1214     break;
1215     }
1216 greg 1.10 }
1217 greg 1.1 }
1218    
1219    
1220 greg 2.7 getgrey() /* get greyscale data */
1221     {
1222     int y;
1223     register unsigned char *dp;
1224     register int x;
1225 greg 2.44 /* initialize tone mapping */
1226     make_tonemap();
1227 greg 2.7 /* read and convert file */
1228     dp = ourdata;
1229     for (y = 0; y < ymax; y++) {
1230 greg 2.15 getscan(y);
1231 greg 2.16 add2icon(y, scanline);
1232 greg 2.44 tmap_colrs(scanline, xmax);
1233 greg 2.13 if (maxcolors < 256)
1234 greg 2.7 for (x = 0; x < xmax; x++)
1235 greg 2.59 *dp++ = ((int32)scanline[x][GRN] *
1236 greg 2.18 maxcolors + maxcolors/2) >> 8;
1237 greg 2.7 else
1238     for (x = 0; x < xmax; x++)
1239 greg 2.18 *dp++ = scanline[x][GRN];
1240 greg 2.7 }
1241 greg 2.13 for (x = 0; x < maxcolors; x++)
1242     clrtab[x][RED] = clrtab[x][GRN] =
1243 greg 2.59 clrtab[x][BLU] = ((int32)x*256 + 128)/maxcolors;
1244 greg 2.7 }
1245    
1246    
1247 greg 2.13 getmapped() /* get color-mapped data */
1248     {
1249     int y;
1250 greg 2.30 /* make sure we can do it first */
1251     if (fname == NULL)
1252     quiterr("cannot map colors from standard input");
1253 greg 2.44 /* initialize tone mapping */
1254     make_tonemap();
1255 greg 2.13 /* make histogram */
1256 greg 2.59 if (new_histo((int32)xmax*ymax) == -1)
1257 greg 2.37 quiterr("cannot initialize histogram");
1258 greg 2.13 for (y = 0; y < ymax; y++) {
1259     if (getscan(y) < 0)
1260 greg 2.30 break;
1261 greg 2.16 add2icon(y, scanline);
1262 greg 2.44 tmap_colrs(scanline, xmax);
1263 greg 2.13 cnt_colrs(scanline, xmax);
1264     }
1265     /* map pixels */
1266     if (!new_clrtab(maxcolors))
1267     quiterr("cannot create color map");
1268     for (y = 0; y < ymax; y++) {
1269 greg 2.30 getscan(y);
1270 greg 2.44 tmap_colrs(scanline, xmax);
1271 greg 2.13 if (dither)
1272     dith_colrs(ourdata+y*xmax, scanline, xmax);
1273     else
1274     map_colrs(ourdata+y*xmax, scanline, xmax);
1275     }
1276     }
1277    
1278    
1279 greg 1.1 scale_rcolors(xr, sf) /* scale color map */
1280     register XRASTER *xr;
1281     double sf;
1282     {
1283     register int i;
1284     long maxv;
1285    
1286     if (xr->pixels == NULL)
1287     return;
1288    
1289     sf = pow(sf, 1.0/gamcor);
1290     maxv = 65535/sf;
1291    
1292     for (i = xr->ncolors; i--; ) {
1293     xr->cdefs[i].red = xr->cdefs[i].red > maxv ?
1294     65535 :
1295     xr->cdefs[i].red * sf;
1296     xr->cdefs[i].green = xr->cdefs[i].green > maxv ?
1297     65535 :
1298     xr->cdefs[i].green * sf;
1299     xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ?
1300     65535 :
1301     xr->cdefs[i].blue * sf;
1302     }
1303     XStoreColors(thedisplay, xr->cmap, xr->cdefs, xr->ncolors);
1304     }
1305    
1306    
1307     getscan(y)
1308     int y;
1309     {
1310 greg 2.30 static int trunced = -1; /* truncated file? */
1311     skipit:
1312     if (trunced >= 0 && y >= trunced) {
1313 schorsch 2.61 memset(scanline, '\0', xmax*sizeof(COLR));
1314 greg 2.30 return(-1);
1315     }
1316 greg 1.1 if (y != cury) {
1317     if (scanpos == NULL || scanpos[y] == -1)
1318     return(-1);
1319     if (fseek(fin, scanpos[y], 0) == -1)
1320 greg 1.9 quiterr("fseek error");
1321 greg 1.1 cury = y;
1322 greg 2.11 } else if (scanpos != NULL && scanpos[y] == -1)
1323 greg 1.1 scanpos[y] = ftell(fin);
1324    
1325 greg 2.30 if (freadcolrs(scanline, xmax, fin) < 0) {
1326     fprintf(stderr, "%s: %s: unfinished picture\n",
1327     progname, fname==NULL?"<stdin>":fname);
1328     trunced = y;
1329     goto skipit;
1330     }
1331 greg 1.1 cury++;
1332     return(0);
1333     }