ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.65
Committed: Fri Jan 2 12:47:01 2004 UTC (20 years, 3 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.64: +7 -4 lines
Log Message:
Fixed typing/prototype of getheader() and its callback.

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