ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ximage.c
Revision: 1.23
Committed: Fri Jan 26 08:17:43 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.22: +1 -5 lines
Log Message:
added scale factor to normcolrs()

File Contents

# Content
1 /* Copyright (c) 1987 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * ximage.c - driver for X-windows
9 *
10 * 4/30/87
11 * 3/3/88 Added calls to xraster & Paul Heckbert's ciq routines
12 */
13
14 #include "standard.h"
15
16 #include <X/Xlib.h>
17 #include <X/cursors/bcross.cursor>
18 #include <X/cursors/bcross_mask.cursor>
19
20 #include <sys/types.h>
21
22 #include <ctype.h>
23
24 #include "color.h"
25
26 #include "xraster.h"
27
28 #include "view.h"
29
30 #include "pic.h"
31
32 #include "random.h"
33
34 #define controlshift(e) (((XButtonEvent *)(e))->detail & (ShiftMask|ControlMask))
35
36 #define FONTNAME "9x15" /* text font we'll use */
37
38 #define CTRL(c) ('c'-'@')
39
40 #define BORWIDTH 5 /* border width */
41 #define BARHEIGHT 25 /* menu bar size */
42
43 double gamcor = 2.2; /* gamma correction */
44
45 XRASTER *ourras = NULL; /* our stored raster image */
46
47 int dither = 1; /* dither colors? */
48 int fast = 0; /* keep picture in Pixmap? */
49
50 Window wind = 0; /* our output window */
51 Font fontid; /* our font */
52
53 int maxcolors = 0; /* maximum colors */
54 int greyscale = 0; /* in grey */
55
56 int scale = 0; /* scalefactor; power of two */
57
58 int xoff = 0; /* x image offset */
59 int yoff = 0; /* y image offset */
60
61 VIEW ourview = STDVIEW; /* image view parameters */
62 int gotview = 0; /* got parameters from file */
63
64 COLR *scanline; /* scan line buffer */
65
66 int xmax, ymax; /* picture resolution */
67 int width, height; /* window size */
68 char *fname = NULL; /* input file name */
69 FILE *fin = stdin; /* input file */
70 long *scanpos = NULL; /* scan line positions in file */
71 int cury = 0; /* current scan location */
72
73 double exposure = 1.0; /* exposure compensation used */
74
75 struct {
76 int xmin, ymin, xsiz, ysiz;
77 } box = {0, 0, 0, 0}; /* current box */
78
79 char *geometry = NULL; /* geometry specification */
80
81 char *progname;
82
83 char errmsg[128];
84
85 extern long ftell();
86
87 extern char *malloc(), *calloc();
88
89 extern double atof(), pow(), log();
90
91
92 main(argc, argv)
93 int argc;
94 char *argv[];
95 {
96 int headline();
97 int i;
98
99 progname = argv[0];
100
101 for (i = 1; i < argc; i++)
102 if (argv[i][0] == '-')
103 switch (argv[i][1]) {
104 case 'c':
105 maxcolors = atoi(argv[++i]);
106 break;
107 case 'b':
108 greyscale = !greyscale;
109 break;
110 case 'm':
111 maxcolors = 2;
112 break;
113 case 'd':
114 dither = !dither;
115 break;
116 case 'f':
117 fast = !fast;
118 break;
119 case 'e':
120 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
121 goto userr;
122 scale = atoi(argv[++i]);
123 break;
124 case 'g':
125 gamcor = atof(argv[++i]);
126 break;
127 default:
128 goto userr;
129 }
130 else if (argv[i][0] == '=')
131 geometry = argv[i];
132 else
133 break;
134
135 if (argc-i == 1) {
136 fname = argv[i];
137 fin = fopen(fname, "r");
138 if (fin == NULL) {
139 sprintf(errmsg, "can't open file \"%s\"", fname);
140 quiterr(errmsg);
141 }
142 }
143 /* get header */
144 getheader(fin, headline);
145 /* get picture dimensions */
146 if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
147 quiterr("bad picture size");
148 /* set view parameters */
149 if (gotview && setview(&ourview) != NULL)
150 gotview = 0;
151 if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
152 quiterr("out of memory");
153
154 init(); /* get file and open window */
155
156 for ( ; ; )
157 getevent(); /* main loop */
158 userr:
159 fprintf(stderr,
160 "Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
161 progname);
162 quit(1);
163 }
164
165
166 headline(s) /* get relevant info from header */
167 char *s;
168 {
169 static char *altname[] = {"rview","rpict",VIEWSTR,NULL};
170 register char **an;
171
172 if (isexpos(s))
173 exposure *= exposval(s);
174 else
175 for (an = altname; *an != NULL; an++)
176 if (!strncmp(*an, s, strlen(*an))) {
177 if (sscanview(&ourview, s+strlen(*an)) > 0)
178 gotview++;
179 return;
180 }
181 }
182
183
184 init() /* get data and open window */
185 {
186 register int i;
187 OpaqueFrame mainframe;
188 char defgeom[32];
189
190 if (fname != NULL) {
191 scanpos = (long *)malloc(ymax*sizeof(long));
192 if (scanpos == NULL)
193 goto memerr;
194 for (i = 0; i < ymax; i++)
195 scanpos[i] = -1;
196 }
197 if (XOpenDisplay(NULL) == NULL)
198 quiterr("can't open display; DISPLAY variable set?");
199 #ifdef notdef
200 if (DisplayWidth() - 2*BORWIDTH < xmax ||
201 DisplayHeight() - 2*BORWIDTH - BARHEIGHT < ymax)
202 quiterr("resolution mismatch");
203 #endif
204 if (maxcolors == 0) { /* get number of available colors */
205 maxcolors = 1<<DisplayPlanes();
206 if (maxcolors > 4) maxcolors -= 2;
207 }
208 /* store image */
209 getras();
210
211 mainframe.bdrwidth = BORWIDTH;
212 mainframe.border = WhitePixmap;
213 mainframe.background = BlackPixmap;
214 sprintf(defgeom, "=%dx%d+0+22", xmax, ymax);
215 wind = XCreate("Radiance Image", progname, geometry, defgeom,
216 &mainframe, 16, 16);
217 if (wind == 0)
218 quiterr("can't create window");
219 width = mainframe.width;
220 height = mainframe.height;
221 fontid = XGetFont(FONTNAME);
222 if (fontid == 0)
223 quiterr("can't get font");
224 XStoreName(wind, fname == NULL ? progname : fname);
225 XDefineCursor(wind, XCreateCursor(bcross_width, bcross_height,
226 bcross_bits, bcross_mask_bits,
227 bcross_x_hot, bcross_y_hot,
228 BlackPixel, WhitePixel, GXcopy));
229 XSelectInput(wind, ButtonPressed|ButtonReleased|UnmapWindow
230 |RightDownMotion|MiddleDownMotion|LeftDownMotion
231 |KeyPressed|ExposeWindow|ExposeRegion);
232 XMapWindow(wind);
233 return;
234 memerr:
235 quiterr("out of memory");
236 }
237
238
239 quiterr(err) /* print message and exit */
240 char *err;
241 {
242 if (err != NULL)
243 fprintf(stderr, "%s: %s\n", progname, err);
244
245 exit(err == NULL ? 0 : 1);
246 }
247
248
249 eputs(s)
250 char *s;
251 {
252 fputs(s, stderr);
253 }
254
255
256 quit(code)
257 int code;
258 {
259 exit(code);
260 }
261
262
263 getras() /* get raster file */
264 {
265 colormap ourmap;
266
267 ourras = (XRASTER *)calloc(1, sizeof(XRASTER));
268 if (ourras == NULL)
269 goto memerr;
270 ourras->width = xmax;
271 ourras->height = ymax;
272 if (maxcolors <= 2) { /* monochrome */
273 ourras->data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax));
274 if (ourras->data.m == NULL)
275 goto memerr;
276 getmono();
277 } else {
278 ourras->data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax));
279 if (ourras->data.bz == NULL)
280 goto memerr;
281 if (greyscale)
282 biq(dither,maxcolors,1,ourmap);
283 else
284 ciq(dither,maxcolors,1,ourmap);
285 if (init_rcolors(ourras, ourmap) == 0)
286 goto memerr;
287 }
288 return;
289 memerr:
290 quit("out of memory");
291 }
292
293
294 getevent() /* process the next event */
295 {
296 WindowInfo info;
297 XEvent e;
298
299 XNextEvent(&e);
300 switch (e.type) {
301 case KeyPressed:
302 docom(&e);
303 break;
304 case ExposeWindow:
305 XQueryWindow(wind, &info); /* in case resized */
306 width = info.width;
307 height = info.height;
308 /* fall through */
309 case ExposeRegion:
310 fixwindow(&e);
311 break;
312 case UnmapWindow:
313 unmap_rcolors(ourras);
314 break;
315 case ButtonPressed:
316 if (controlshift(&e))
317 moveimage(&e);
318 else
319 getbox(&e);
320 break;
321 }
322 }
323
324
325 fixwindow(eexp) /* fix new or stepped-on window */
326 XExposeEvent *eexp;
327 {
328 redraw(eexp->x, eexp->y, eexp->width, eexp->height);
329 }
330
331
332 redraw(x, y, w, h) /* redraw section of window */
333 int x, y;
334 int w, h;
335 {
336 if (ourras->ncolors && map_rcolors(ourras) == NULL) {
337 fprintf(stderr, "%s: cannot allocate colors\n", progname);
338 return(-1);
339 }
340 if (fast)
341 make_rpixmap(ourras);
342 return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras) ? 0 : -1);
343 }
344
345
346 docom(ekey) /* execute command */
347 XKeyEvent *ekey;
348 {
349 char buf[80];
350 COLOR cval;
351 Color cvx;
352 char *cp;
353 int n;
354 double comp;
355 FVECT rorg, rdir;
356
357 cp = XLookupMapping(ekey, &n);
358 if (n == 0)
359 return(0);
360 switch (*cp) { /* interpret command */
361 case 'q':
362 case CTRL(D): /* quit */
363 quit(0);
364 case '\n':
365 case '\r':
366 case 'l':
367 case 'c': /* value */
368 if (avgbox(cval) == -1)
369 return(-1);
370 switch (*cp) {
371 case '\n':
372 case '\r': /* radiance */
373 sprintf(buf, "%.3f", intens(cval)/exposure);
374 break;
375 case 'l': /* luminance */
376 sprintf(buf, "%.0fn", bright(cval)*683.0/exposure);
377 break;
378 case 'c': /* color */
379 comp = pow(2.0, (double)scale);
380 sprintf(buf, "(%.2f,%.2f,%.2f)",
381 colval(cval,RED)*comp,
382 colval(cval,GRN)*comp,
383 colval(cval,BLU)*comp);
384 break;
385 }
386 XText(wind, box.xmin, box.ymin, buf, strlen(buf),
387 fontid, BlackPixel, WhitePixel);
388 return(0);
389 case 'i': /* identify (contour) */
390 if (ourras->pixels == NULL)
391 return(-1);
392 n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)];
393 n = ourras->pmap[n];
394 cvx.pixel = ourras->cdefs[n].pixel;
395 cvx.red = random() & 65535;
396 cvx.green = random() & 65535;
397 cvx.blue = random() & 65535;
398 XStoreColor(&cvx);
399 return(0);
400 case 'p': /* position */
401 sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
402 XText(wind, ekey->x, ekey->y, buf, strlen(buf),
403 fontid, BlackPixel, WhitePixel);
404 return(0);
405 case 't': /* trace */
406 if (!gotview) {
407 XFeep(0);
408 return(-1);
409 }
410 viewray(rorg, rdir, &ourview, (ekey->x-xoff+.5)/xmax,
411 (ymax-1-ekey->y+yoff+.5)/ymax);
412 printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
413 printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
414 fflush(stdout);
415 return(0);
416 case '=': /* adjust exposure */
417 if (avgbox(cval) == -1)
418 return(-1);
419 n = log(.5/bright(cval))/.69315 - scale; /* truncate */
420 if (n == 0)
421 return(0);
422 scale_rcolors(ourras, pow(2.0, (double)n));
423 scale += n;
424 sprintf(buf, "%+d", scale);
425 XText(wind, box.xmin, box.ymin, buf, strlen(buf),
426 fontid, BlackPixel, WhitePixel);
427 XFlush();
428 free_raster(ourras);
429 getras();
430 /* fall through */
431 case CTRL(R): /* redraw */
432 case CTRL(L):
433 unmap_rcolors(ourras);
434 XClear(wind);
435 return(redraw(0, 0, width, height));
436 case ' ': /* clear */
437 return(redraw(box.xmin, box.ymin, box.xsiz, box.ysiz));
438 default:
439 XFeep(0);
440 return(-1);
441 }
442 }
443
444
445 moveimage(ep) /* shift the image */
446 XButtonEvent *ep;
447 {
448 XButtonEvent eb;
449
450 XMaskEvent(ButtonReleased, &eb);
451 xoff += eb.x - ep->x;
452 yoff += eb.y - ep->y;
453 XClear(wind);
454 return(redraw(0, 0, width, height));
455 }
456
457
458 getbox(ebut) /* get new box */
459 XButtonEvent *ebut;
460 {
461 union {
462 XEvent e;
463 XButtonEvent b;
464 XMouseMovedEvent m;
465 } e;
466
467 XMaskEvent(ButtonReleased|MouseMoved, &e.e);
468 while (e.e.type == MouseMoved) {
469 revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
470 XMaskEvent(ButtonReleased|MouseMoved, &e.e);
471 revbox(ebut->x, ebut->y, box.xmin, box.ymin);
472 }
473 box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
474 box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
475 if (box.xmin > ebut->x) {
476 box.xsiz = box.xmin - ebut->x + 1;
477 box.xmin = ebut->x;
478 } else {
479 box.xsiz = ebut->x - box.xmin + 1;
480 }
481 if (box.ymin > ebut->y) {
482 box.ysiz = box.ymin - ebut->y + 1;
483 box.ymin = ebut->y;
484 } else {
485 box.ysiz = ebut->y - box.ymin + 1;
486 }
487 }
488
489
490 revbox(x0, y0, x1, y1) /* draw box with reversed lines */
491 int x0, y0, x1, y1;
492 {
493 XLine(wind, x0, y0, x1, y0, 1, 1, 0, GXinvert, AllPlanes);
494 XLine(wind, x0, y1, x1, y1, 1, 1, 0, GXinvert, AllPlanes);
495 XLine(wind, x0, y0, x0, y1, 1, 1, 0, GXinvert, AllPlanes);
496 XLine(wind, x1, y0, x1, y1, 1, 1, 0, GXinvert, AllPlanes);
497 }
498
499
500 avgbox(clr) /* average color over current box */
501 COLOR clr;
502 {
503 int left, right, top, bottom;
504 int y;
505 double d;
506 COLOR ctmp;
507 register int x;
508
509 setcolor(clr, 0.0, 0.0, 0.0);
510 left = box.xmin - xoff;
511 right = left + box.xsiz;
512 if (left < 0)
513 left = 0;
514 if (right > xmax)
515 right = xmax;
516 if (left >= right)
517 return(-1);
518 top = box.ymin - yoff;
519 bottom = top + box.ysiz;
520 if (top < 0)
521 top = 0;
522 if (bottom > ymax)
523 bottom = ymax;
524 if (top >= bottom)
525 return(-1);
526 for (y = top; y < bottom; y++) {
527 if (getscan(y) == -1)
528 return(-1);
529 for (x = left; x < right; x++) {
530 colr_color(ctmp, scanline[x]);
531 addcolor(clr, ctmp);
532 }
533 }
534 d = 1.0/((right-left)*(bottom-top));
535 scalecolor(clr, d);
536 return(0);
537 }
538
539
540 getmono() /* get monochrome data */
541 {
542 register unsigned short *dp;
543 register int x, err;
544 int y;
545 rgbpixel *inline;
546 short *cerr;
547
548 if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
549 || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
550 quit("out of memory in getmono");
551 dp = ourras->data.m - 1;
552 for (y = 0; y < ymax; y++) {
553 picreadline3(y, inline);
554 err = 0;
555 for (x = 0; x < xmax; x++) {
556 if (!(x&0xf))
557 *++dp = 0;
558 err += rgb_bright(&inline[x]) + cerr[x];
559 if (err > 127)
560 err -= 255;
561 else
562 *dp |= 1<<(x&0xf);
563 cerr[x] = err >>= 1;
564 }
565 }
566 free((char *)inline);
567 free((char *)cerr);
568 }
569
570
571 init_rcolors(xr, cmap) /* (re)assign color values */
572 register XRASTER *xr;
573 colormap cmap;
574 {
575 register int i;
576 register unsigned char *p;
577
578 xr->pmap = (int *)malloc(256*sizeof(int));
579 if (xr->pmap == NULL)
580 return(0);
581 xr->cdefs = (Color *)malloc(256*sizeof(Color));
582 if (xr->cdefs == NULL)
583 return(0);
584 for (i = 0; i < 256; i++)
585 xr->pmap[i] = -1;
586 xr->ncolors = 0;
587 for (p = xr->data.bz, i = BZPixmapSize(xr->width,xr->height); i--; p++)
588 if (xr->pmap[*p] == -1) {
589 xr->cdefs[xr->ncolors].red = cmap[0][*p] << 8;
590 xr->cdefs[xr->ncolors].green = cmap[1][*p] << 8;
591 xr->cdefs[xr->ncolors].blue = cmap[2][*p] << 8;
592 xr->cdefs[xr->ncolors].pixel = *p;
593 xr->pmap[*p] = xr->ncolors++;
594 }
595 xr->cdefs = (Color *)realloc((char *)xr->cdefs, xr->ncolors*sizeof(Color));
596 if (xr->cdefs == NULL)
597 return(0);
598 return(1);
599 }
600
601
602 scale_rcolors(xr, sf) /* scale color map */
603 register XRASTER *xr;
604 double sf;
605 {
606 register int i;
607 long maxv;
608
609 if (xr->pixels == NULL)
610 return;
611
612 sf = pow(sf, 1.0/gamcor);
613 maxv = 65535/sf;
614
615 for (i = xr->ncolors; i--; ) {
616 xr->cdefs[i].red = xr->cdefs[i].red > maxv ?
617 65535 :
618 xr->cdefs[i].red * sf;
619 xr->cdefs[i].green = xr->cdefs[i].green > maxv ?
620 65535 :
621 xr->cdefs[i].green * sf;
622 xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ?
623 65535 :
624 xr->cdefs[i].blue * sf;
625 }
626 XStoreColors(xr->ncolors, xr->cdefs);
627 }
628
629
630 getscan(y)
631 int y;
632 {
633 if (y != cury) {
634 if (scanpos == NULL || scanpos[y] == -1)
635 return(-1);
636 if (fseek(fin, scanpos[y], 0) == -1)
637 quit("fseek error");
638 cury = y;
639 } else if (scanpos != NULL)
640 scanpos[y] = ftell(fin);
641
642 if (freadcolrs(scanline, xmax, fin) < 0)
643 quiterr("read error");
644
645 cury++;
646 return(0);
647 }
648
649
650 picreadline3(y, l3) /* read in 3-byte scanline */
651 int y;
652 register rgbpixel *l3;
653 {
654 register int i;
655 /* read scanline */
656 if (getscan(y) < 0)
657 quiterr("cannot seek for picreadline");
658 /* convert scanline */
659 normcolrs(scanline, xmax, scale);
660 for (i = 0; i < xmax; i++) {
661 l3[i].r = scanline[i][RED];
662 l3[i].g = scanline[i][GRN];
663 l3[i].b = scanline[i][BLU];
664 }
665 }
666
667
668 picwriteline(y, l) /* add 8-bit scanline to image */
669 int y;
670 pixel *l;
671 {
672 bcopy((char *)l, (char *)ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1));
673 }
674
675
676 picreadcm(map) /* do gamma correction */
677 colormap map;
678 {
679 extern double pow();
680 register int i, val;
681
682 for (i = 0; i < 256; i++) {
683 val = pow(i/256.0, 1.0/gamcor) * 256.0;
684 map[0][i] = map[1][i] = map[2][i] = val;
685 }
686 }
687
688
689 picwritecm(map) /* handled elsewhere */
690 colormap map;
691 {
692 #ifdef DEBUG
693 register int i;
694
695 for (i = 0; i < 256; i++)
696 printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
697 #endif
698 }