ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ximage.c
(Generate patch)

Comparing ray/src/px/ximage.c (file contents):
Revision 1.1 by greg, Thu Feb 2 10:49:43 1989 UTC vs.
Revision 2.11 by greg, Tue May 13 17:58:33 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1987 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  ximage.c - driver for X-windows
6   *
# Line 21 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  <ctype.h>
20  
21 + #include  <time.h>
22 +
23   #include  "color.h"
24  
25 + #include  "resolu.h"
26 +
27   #include  "xraster.h"
28  
29   #include  "view.h"
30  
31   #include  "pic.h"
32  
33 + #include  "random.h"
34 +
35   #define  controlshift(e)        (((XButtonEvent *)(e))->detail & (ShiftMask|ControlMask))
36  
37   #define  FONTNAME       "9x15"          /* text font we'll use */
38  
39 < #define  CTRL(c)        ('c'-'@')
39 > #define  CTRL(c)        ((c)-'@')
40  
41   #define  BORWIDTH       5               /* border width */
42   #define  BARHEIGHT      25              /* menu bar size */
43  
44 < double  gamcor = 2.0;                   /* gamcor correction */
44 > double  gamcor = 2.2;                   /* gamma correction */
45  
46 < XRASTER  ourras;                        /* our stored raster image */
46 > XRASTER  *ourras = NULL;                /* our stored raster image */
47  
48   int  dither = 1;                        /* dither colors? */
49   int  fast = 0;                          /* keep picture in Pixmap? */
# Line 51 | Line 54 | Font  fontid;                          /* our font */
54   int  maxcolors = 0;                     /* maximum colors */
55   int  greyscale = 0;                     /* in grey */
56  
57 + int  scale = 0;                         /* scalefactor; power of two */
58 +
59   int  xoff = 0;                          /* x image offset */
60   int  yoff = 0;                          /* y image offset */
61  
62 < VIEW  ourview = STDVIEW(0);             /* image view parameters */
62 > VIEW  ourview = STDVIEW;                /* image view parameters */
63   int  gotview = 0;                       /* got parameters from file */
64  
65   COLR  *scanline;                        /* scan line buffer */
# Line 68 | Line 73 | int  cury = 0;                         /* current scan location */
73  
74   double  exposure = 1.0;                 /* exposure compensation used */
75  
76 + int  wrongformat = 0;                   /* input in another format */
77 +
78   struct {
79          int  xmin, ymin, xsiz, ysiz;
80   }  box = {0, 0, 0, 0};                  /* current box */
# Line 78 | Line 85 | char  *progname;
85  
86   char  errmsg[128];
87  
81 extern long  ftell();
88  
83 extern char  *malloc(), *calloc();
84
85 extern double  atof();
86
87
89   main(argc, argv)
90   int  argc;
91   char  *argv[];
92   {
93 +        extern char  *getenv();
94 +        char  *gv;
95          int  headline();
96          int  i;
97          
98          progname = argv[0];
99 +        if ((gv = getenv("DISPLAY_GAMMA")) != NULL)
100 +                gamcor = atof(gv);
101  
102          for (i = 1; i < argc; i++)
103                  if (argv[i][0] == '-')
# Line 112 | Line 117 | char  *argv[];
117                          case 'f':
118                                  fast = !fast;
119                                  break;
120 +                        case 'e':
121 +                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
122 +                                        goto userr;
123 +                                scale = atoi(argv[++i]);
124 +                                break;
125                          case 'g':
126                                  gamcor = atof(argv[++i]);
127                                  break;
# Line 130 | Line 140 | char  *argv[];
140                          sprintf(errmsg, "can't open file \"%s\"", fname);
141                          quiterr(errmsg);
142                  }
143 <        } else
134 <                goto userr;
135 <
143 >        }
144                                  /* get header */
145 <        getheader(fin, headline);
145 >        getheader(fin, headline, NULL);
146                                  /* get picture dimensions */
147 <        if (fscanf(fin, "-Y %d +X %d\n", &ymax, &xmax) != 2)
147 >        if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
148                  quiterr("bad picture size");
149                                  /* set view parameters */
150 <        if (gotview) {
151 <                ourview.hresolu = xmax;
144 <                ourview.vresolu = ymax;
145 <                if (setview(&ourview) != NULL)
146 <                        gotview = 0;
147 <        }
150 >        if (gotview && setview(&ourview) != NULL)
151 >                gotview = 0;
152          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
153                  quiterr("out of memory");
154  
# Line 154 | Line 158 | char  *argv[];
158                  getevent();             /* main loop */
159   userr:
160          fprintf(stderr,
161 <        "Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors] file\n",
161 >        "Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
162                          progname);
163          quit(1);
164   }
165  
166  
167 + int
168   headline(s)             /* get relevant info from header */
169   char  *s;
170   {
171 <        static char  *altname[] = {"rview","rpict","VIEW=",NULL};
167 <        register char  **an;
171 >        char  fmt[32];
172  
173 <        if (!strncmp(s, "EXPOSURE=", 9))
174 <                exposure *= atof(s+9);
175 <        else
176 <                for (an = altname; *an != NULL; an++)
177 <                        if (!strncmp(*an, s, strlen(*an))) {
178 <                                if (sscanview(&ourview, s+strlen(*an)) == 0)
179 <                                        gotview++;
180 <                                return;
177 <                        }
173 >        if (isexpos(s))
174 >                exposure *= exposval(s);
175 >        else if (isformat(s)) {
176 >                formatval(fmt, s);
177 >                wrongformat = strcmp(fmt, COLRFMT);
178 >        } else if (isview(s) && sscanview(&ourview, s) > 0)
179 >                gotview++;
180 >        return(0);
181   }
182  
183  
181 char *
182 sskip(s)                /* skip a word */
183 register char  *s;
184 {
185        while (isspace(*s)) s++;
186        while (*s && !isspace(*s)) s++;
187        return(s);
188 }
189
190
184   init()                  /* get data and open window */
185   {
186          register int  i;
194        colormap  ourmap;
187          OpaqueFrame  mainframe;
188          char  defgeom[32];
189          
# Line 213 | Line 205 | init()                 /* get data and open window */
205                  maxcolors = 1<<DisplayPlanes();
206                  if (maxcolors > 4) maxcolors -= 2;
207          }
216        ourras.width = xmax;
217        ourras.height = ymax;
208                                  /* store image */
209 <        if (maxcolors <= 2) {           /* monochrome */
210 <                ourras.data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax));
221 <                if (ourras.data.m == NULL)
222 <                        goto memerr;
223 <                getmono();
224 <        } else {
225 <                ourras.data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax));
226 <                if (ourras.data.bz == NULL)
227 <                        goto memerr;
228 <                if (greyscale)
229 <                        biq(dither,maxcolors,1,ourmap);
230 <                else
231 <                        ciq(dither,maxcolors,1,ourmap);
232 <                if (init_rcolors(&ourras, ourmap) == 0)
233 <                        goto memerr;
234 <        }
209 >        getras();
210 >
211          mainframe.bdrwidth = BORWIDTH;
212          mainframe.border = WhitePixmap;
213          mainframe.background = BlackPixmap;
# Line 270 | Line 246 | char  *err;
246   }
247  
248  
249 + void
250   eputs(s)
251   char    *s;
252   {
# Line 277 | Line 254 | char   *s;
254   }
255  
256  
257 + void
258   quit(code)
259   int  code;
260   {
# Line 284 | Line 262 | int  code;
262   }
263  
264  
265 + getras()                                /* get raster file */
266 + {
267 +        colormap        ourmap;
268 +
269 +        ourras = (XRASTER *)calloc(1, sizeof(XRASTER));
270 +        if (ourras == NULL)
271 +                goto memerr;
272 +        ourras->width = xmax;
273 +        ourras->height = ymax;
274 +        if (maxcolors <= 2) {           /* monochrome */
275 +                ourras->data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax));
276 +                if (ourras->data.m == NULL)
277 +                        goto memerr;
278 +                getmono();
279 +        } else {
280 +                ourras->data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax));
281 +                if (ourras->data.bz == NULL)
282 +                        goto memerr;
283 +                if (greyscale)
284 +                        biq(dither,maxcolors,1,ourmap);
285 +                else
286 +                        ciq(dither,maxcolors,1,ourmap);
287 +                if (init_rcolors(ourras, ourmap) == 0)
288 +                        goto memerr;
289 +        }
290 +        return;
291 + memerr:
292 +        quiterr("out of memory");
293 + }
294 +
295 +
296   getevent()                              /* process the next event */
297   {
298          WindowInfo  info;
# Line 303 | Line 312 | getevent()                             /* process the next event */
312                  fixwindow(&e);
313                  break;
314          case UnmapWindow:
315 <                unmap_rcolors(&ourras);
315 >                unmap_rcolors(ourras);
316                  break;
317          case ButtonPressed:
318                  if (controlshift(&e))
# Line 326 | Line 335 | redraw(x, y, w, h)                     /* redraw section of window */
335   int  x, y;
336   int  w, h;
337   {
338 <        map_rcolors(&ourras);
338 >        if (ourras->ncolors && map_rcolors(ourras) == NULL) {
339 >                fprintf(stderr, "%s: cannot allocate colors\n", progname);
340 >                return(-1);
341 >        }
342          if (fast)
343 <                make_rpixmap(&ourras);
344 <        return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,&ourras) ? 0 : -1);
343 >                make_rpixmap(ourras);
344 >        return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras) ? 0 : -1);
345   }
346  
347  
# Line 338 | Line 350 | XKeyEvent  *ekey;
350   {
351          char  buf[80];
352          COLOR  cval;
353 +        Color  cvx;
354          char  *cp;
355          int  n;
356 +        double  comp;
357          FVECT  rorg, rdir;
358  
359          cp = XLookupMapping(ekey, &n);
# Line 347 | Line 361 | XKeyEvent  *ekey;
361                  return(0);
362          switch (*cp) {                  /* interpret command */
363          case 'q':
364 <        case CTRL(D):                           /* quiterr */
364 >        case CTRL('D'):                         /* quit */
365                  quit(0);
366          case '\n':
367          case '\r':
# Line 358 | Line 372 | XKeyEvent  *ekey;
372                  switch (*cp) {
373                  case '\n':
374                  case '\r':                              /* radiance */
375 <                        sprintf(buf, "%-3g", intens(cval)/exposure);
375 >                        sprintf(buf, "%.3f", intens(cval)/exposure);
376                          break;
377                  case 'l':                               /* luminance */
378 <                        sprintf(buf, "%-3gL", bright(cval)*683.0/exposure);
378 >                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
379                          break;
380                  case 'c':                               /* color */
381 +                        comp = pow(2.0, (double)scale);
382                          sprintf(buf, "(%.2f,%.2f,%.2f)",
383 <                                        colval(cval,RED),
384 <                                        colval(cval,GRN),
385 <                                        colval(cval,BLU));
383 >                                        colval(cval,RED)*comp,
384 >                                        colval(cval,GRN)*comp,
385 >                                        colval(cval,BLU)*comp);
386                          break;
387                  }
388                  XText(wind, box.xmin, box.ymin, buf, strlen(buf),
389                                  fontid, BlackPixel, WhitePixel);
390                  return(0);
391 +        case 'i':                               /* identify (contour) */
392 +                if (ourras->pixels == NULL)
393 +                        return(-1);
394 +                n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)];
395 +                n = ourras->pmap[n];
396 +                cvx.pixel = ourras->cdefs[n].pixel;
397 +                cvx.red = random() & 65535;
398 +                cvx.green = random() & 65535;
399 +                cvx.blue = random() & 65535;
400 +                XStoreColor(&cvx);
401 +                return(0);
402          case 'p':                               /* position */
403                  sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
404                  XText(wind, ekey->x, ekey->y, buf, strlen(buf),
# Line 383 | Line 409 | XKeyEvent  *ekey;
409                          XFeep(0);
410                          return(-1);
411                  }
412 <                rayview(rorg, rdir, &ourview,
413 <                                ekey->x-xoff + .5, ymax-1-ekey->y+yoff + .5);
412 >                if (viewray(rorg, rdir, &ourview, (ekey->x-xoff+.5)/xmax,
413 >                                (ymax-1-ekey->y+yoff+.5)/ymax) < 0)
414 >                        return(-1);
415                  printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
416                  printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
417                  fflush(stdout);
418                  return(0);
419 <        case CTRL(R):                           /* redraw */
419 >        case '=':                               /* adjust exposure */
420 >                if (avgbox(cval) == -1)
421 >                        return(-1);
422 >                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
423 >                if (n == 0)
424 >                        return(0);
425 >                scale_rcolors(ourras, pow(2.0, (double)n));
426 >                scale += n;
427 >                sprintf(buf, "%+d", scale);
428 >                XText(wind, box.xmin, box.ymin, buf, strlen(buf),
429 >                                fontid, BlackPixel, WhitePixel);
430 >                XFlush();
431 >                free_raster(ourras);
432 >                getras();
433 >        /* fall through */
434 >        case CTRL('R'):                         /* redraw */
435 >        case CTRL('L'):
436 >                unmap_rcolors(ourras);
437                  XClear(wind);
438                  return(redraw(0, 0, width, height));
439          case ' ':                               /* clear */
# Line 500 | Line 544 | getmono()                      /* get monochrome data */
544   {
545          register unsigned short *dp;
546          register int    x, err;
547 <        int     y;
548 <        rgbpixel        *inline;
547 >        int     y, errp;
548 >        rgbpixel        *inl;
549          short   *cerr;
550  
551 <        if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
551 >        if ((inl = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
552                          || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
553 <                quit("out of memory in getmono");
554 <        dp = ourras.data.m - 1;
553 >                quiterr("out of memory in getmono");
554 >        dp = ourras->data.m - 1;
555          for (y = 0; y < ymax; y++) {
556 <                picreadline3(y, inline);
556 >                picreadline3(y, inl);
557                  err = 0;
558                  for (x = 0; x < xmax; x++) {
559                          if (!(x&0xf))
560                                  *++dp = 0;
561 <                        err += rgb_bright(&inline[x]) + cerr[x];
561 >                        errp = err;
562 >                        err += rgb_bright(&inl[x]) + cerr[x];
563                          if (err > 127)
564                                  err -= 255;
565                          else
566                                  *dp |= 1<<(x&0xf);
567 <                        cerr[x] = err >>= 1;
567 >                        err /= 3;
568 >                        cerr[x] = err + errp;
569                  }
570          }
571 <        free((char *)inline);
572 <        free((char *)cerr);
571 >        free((void *)inl);
572 >        free((void *)cerr);
573   }
574  
575  
576 < init_rcolors(xr, cmap)                          /* assign color values */
576 > init_rcolors(xr, cmap)                          /* (re)assign color values */
577   register XRASTER        *xr;
578   colormap        cmap;
579   {
# Line 551 | Line 597 | colormap       cmap;
597                          xr->cdefs[xr->ncolors].pixel = *p;
598                          xr->pmap[*p] = xr->ncolors++;
599                  }
600 <        xr->cdefs = (Color *)realloc((char *)xr->cdefs, xr->ncolors*sizeof(Color));
600 >        xr->cdefs = (Color *)realloc((void *)xr->cdefs, xr->ncolors*sizeof(Color));
601          if (xr->cdefs == NULL)
602                  return(0);
603          return(1);
604   }
605  
606  
607 + scale_rcolors(xr, sf)                   /* scale color map */
608 + register XRASTER        *xr;
609 + double  sf;
610 + {
611 +        register int    i;
612 +        long    maxv;
613 +
614 +        if (xr->pixels == NULL)
615 +                return;
616 +
617 +        sf = pow(sf, 1.0/gamcor);
618 +        maxv = 65535/sf;
619 +
620 +        for (i = xr->ncolors; i--; ) {
621 +                xr->cdefs[i].red = xr->cdefs[i].red > maxv ?
622 +                                65535 :
623 +                                xr->cdefs[i].red * sf;
624 +                xr->cdefs[i].green = xr->cdefs[i].green > maxv ?
625 +                                65535 :
626 +                                xr->cdefs[i].green * sf;
627 +                xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ?
628 +                                65535 :
629 +                                xr->cdefs[i].blue * sf;
630 +        }
631 +        XStoreColors(xr->ncolors, xr->cdefs);
632 + }
633 +
634 +
635   getscan(y)
636   int  y;
637   {
# Line 565 | Line 639 | int  y;
639                  if (scanpos == NULL || scanpos[y] == -1)
640                          return(-1);
641                  if (fseek(fin, scanpos[y], 0) == -1)
642 <                        quit("fseek error");
642 >                        quiterr("fseek error");
643                  cury = y;
644 <        } else if (scanpos != NULL)
644 >        } else if (scanpos != NULL && scanpos[y] == -1)
645                  scanpos[y] = ftell(fin);
646  
647          if (freadcolrs(scanline, xmax, fin) < 0)
# Line 582 | Line 656 | picreadline3(y, l3)                    /* read in 3-byte scanline */
656   int  y;
657   register rgbpixel  *l3;
658   {
659 <        register BYTE   *l4;
660 <        register int    shift, c;
587 <        int     i;
588 <
659 >        register int    i;
660 >                                                        /* read scanline */
661          if (getscan(y) < 0)
662                  quiterr("cannot seek for picreadline");
663                                                          /* convert scanline */
664 <        for (l4=scanline[0], i=xmax; i--; l4+=4, l3++) {
665 <                shift = l4[EXP] - COLXS;
666 <                if (shift >= 8) {
667 <                        l3->r = l3->g = l3->b = 255;
668 <                } else if (shift <= -8) {
597 <                        l3->r = l3->g = l3->b = 0;
598 <                } else if (shift > 0) {
599 <                        c = l4[RED] << shift;
600 <                        l3->r = c > 255 ? 255 : c;
601 <                        c = l4[GRN] << shift;
602 <                        l3->g = c > 255 ? 255 : c;
603 <                        c = l4[BLU] << shift;
604 <                        l3->b = c > 255 ? 255 : c;
605 <                } else if (shift < 0) {
606 <                        l3->r = l4[RED] >> -shift;
607 <                        l3->g = l4[GRN] >> -shift;
608 <                        l3->b = l4[BLU] >> -shift;
609 <                } else {
610 <                        l3->r = l4[RED];
611 <                        l3->g = l4[GRN];
612 <                        l3->b = l4[BLU];
613 <                }
664 >        normcolrs(scanline, xmax, scale);
665 >        for (i = 0; i < xmax; i++) {
666 >                l3[i].r = scanline[i][RED];
667 >                l3[i].g = scanline[i][GRN];
668 >                l3[i].b = scanline[i][BLU];
669          }
670   }
671  
# Line 619 | Line 674 | picwriteline(y, l)             /* add 8-bit scanline to image */
674   int  y;
675   pixel  *l;
676   {
677 <        bcopy(l, ourras.data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1));
677 >        bcopy((void *)l, (void *)ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1));
678   }
679  
680  
681 < picreadcm(map)                  /* do gamcor correction */
681 > picreadcm(map)                  /* do gamma correction */
682   colormap  map;
683   {
684          extern double  pow();
685          register int  i, val;
686  
687          for (i = 0; i < 256; i++) {
688 <                val = pow(i/256.0, 1.0/gamcor) * 256.0;
688 >                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
689                  map[0][i] = map[1][i] = map[2][i] = val;
690          }
691   }
# Line 639 | Line 694 | colormap  map;
694   picwritecm(map)                 /* handled elsewhere */
695   colormap  map;
696   {
697 + #ifdef DEBUG
698 +        register int i;
699 +
700 +        for (i = 0; i < 256; i++)
701 +                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
702 + #endif
703   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines