ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.38
Committed: Tue Sep 20 15:48:15 1994 UTC (29 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.37: +1 -1 lines
Log Message:
changed GAMMA environment variable to DISPLAY_GAMMA

File Contents

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