ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.41
Committed: Wed Jan 25 10:53:02 1995 UTC (29 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.40: +7 -3 lines
Log Message:
added -s option for sequential viewing

File Contents

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