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.2 by greg, Fri Apr 7 16:36:10 1989 UTC vs.
Revision 2.4 by greg, Tue Apr 28 09:40:36 1992 UTC

# Line 23 | Line 23 | static char SCCSid[] = "$SunId$ LBL";
23  
24   #include  "color.h"
25  
26 + #include  "resolu.h"
27 +
28   #include  "xraster.h"
29  
30   #include  "view.h"
31  
32   #include  "pic.h"
33  
34 + #include  "random.h"
35 +
36   #define  controlshift(e)        (((XButtonEvent *)(e))->detail & (ShiftMask|ControlMask))
37  
38   #define  FONTNAME       "9x15"          /* text font we'll use */
39  
40 < #define  CTRL(c)        ('c'-'@')
40 > #define  CTRL(c)        ((c)-'@')
41  
42   #define  BORWIDTH       5               /* border width */
43   #define  BARHEIGHT      25              /* menu bar size */
44  
45 < double  gamcor = 2.0;                   /* gamma correction */
45 > double  gamcor = 2.2;                   /* gamma correction */
46  
47   XRASTER  *ourras = NULL;                /* our stored raster image */
48  
# Line 56 | Line 60 | int  scale = 0;                                /* scalefactor; power of two */
60   int  xoff = 0;                          /* x image offset */
61   int  yoff = 0;                          /* y image offset */
62  
63 < VIEW  ourview = STDVIEW(0);             /* image view parameters */
63 > VIEW  ourview = STDVIEW;                /* image view parameters */
64   int  gotview = 0;                       /* got parameters from file */
65  
66   COLR  *scanline;                        /* scan line buffer */
# Line 70 | Line 74 | int  cury = 0;                         /* current scan location */
74  
75   double  exposure = 1.0;                 /* exposure compensation used */
76  
77 + int  wrongformat = 0;                   /* input in another format */
78 +
79   struct {
80          int  xmin, ymin, xsiz, ysiz;
81   }  box = {0, 0, 0, 0};                  /* current box */
# Line 84 | Line 90 | extern long  ftell();
90  
91   extern char  *malloc(), *calloc();
92  
93 < extern double  atof(), pow(), log();
93 > extern double  pow(), log();
94  
95  
96   main(argc, argv)
# Line 137 | Line 143 | char  *argv[];
143                          sprintf(errmsg, "can't open file \"%s\"", fname);
144                          quiterr(errmsg);
145                  }
146 <        } else
141 <                goto userr;
142 <
146 >        }
147                                  /* get header */
148 <        getheader(fin, headline);
148 >        getheader(fin, headline, NULL);
149                                  /* get picture dimensions */
150 <        if (fscanf(fin, "-Y %d +X %d\n", &ymax, &xmax) != 2)
150 >        if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
151                  quiterr("bad picture size");
152                                  /* set view parameters */
153 <        if (gotview) {
154 <                ourview.hresolu = xmax;
151 <                ourview.vresolu = ymax;
152 <                if (setview(&ourview) != NULL)
153 <                        gotview = 0;
154 <        }
153 >        if (gotview && setview(&ourview) != NULL)
154 >                gotview = 0;
155          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
156                  quiterr("out of memory");
157  
# Line 170 | Line 170 | userr:
170   headline(s)             /* get relevant info from header */
171   char  *s;
172   {
173 <        static char  *altname[] = {"rview","rpict","VIEW=",NULL};
174 <        register char  **an;
173 >        char  fmt[32];
174  
175 <        if (!strncmp(s, "EXPOSURE=", 9))
176 <                exposure *= atof(s+9);
177 <        else
178 <                for (an = altname; *an != NULL; an++)
179 <                        if (!strncmp(*an, s, strlen(*an))) {
180 <                                if (sscanview(&ourview, s+strlen(*an)) == 0)
181 <                                        gotview++;
183 <                                return;
184 <                        }
175 >        if (isexpos(s))
176 >                exposure *= exposval(s);
177 >        else if (isformat(s)) {
178 >                formatval(fmt, s);
179 >                wrongformat = strcmp(fmt, COLRFMT);
180 >        } else if (isview(s) && sscanview(&ourview, s) > 0)
181 >                gotview++;
182   }
183  
184  
188 char *
189 sskip(s)                /* skip a word */
190 register char  *s;
191 {
192        while (isspace(*s)) s++;
193        while (*s && !isspace(*s)) s++;
194        return(s);
195 }
196
197
185   init()                  /* get data and open window */
186   {
187          register int  i;
# Line 301 | Line 288 | getras()                               /* get raster file */
288          }
289          return;
290   memerr:
291 <        quit("out of memory");
291 >        quiterr("out of memory");
292   }
293  
294  
# Line 347 | Line 334 | redraw(x, y, w, h)                     /* redraw section of window */
334   int  x, y;
335   int  w, h;
336   {
337 <        if (map_rcolors(ourras) == NULL) {
337 >        if (ourras->ncolors && map_rcolors(ourras) == NULL) {
338                  fprintf(stderr, "%s: cannot allocate colors\n", progname);
339                  return(-1);
340          }
# Line 362 | Line 349 | XKeyEvent  *ekey;
349   {
350          char  buf[80];
351          COLOR  cval;
352 +        Color  cvx;
353          char  *cp;
354          int  n;
355          double  comp;
# Line 372 | Line 360 | XKeyEvent  *ekey;
360                  return(0);
361          switch (*cp) {                  /* interpret command */
362          case 'q':
363 <        case CTRL(D):                           /* quiterr */
363 >        case CTRL('D'):                         /* quit */
364                  quit(0);
365          case '\n':
366          case '\r':
# Line 383 | Line 371 | XKeyEvent  *ekey;
371                  switch (*cp) {
372                  case '\n':
373                  case '\r':                              /* radiance */
374 <                        sprintf(buf, "%-3g", intens(cval)/exposure);
374 >                        sprintf(buf, "%.3f", intens(cval)/exposure);
375                          break;
376                  case 'l':                               /* luminance */
377 <                        sprintf(buf, "%-3gL", bright(cval)*683.0/exposure);
377 >                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
378                          break;
379                  case 'c':                               /* color */
380                          comp = pow(2.0, (double)scale);
# Line 399 | Line 387 | XKeyEvent  *ekey;
387                  XText(wind, box.xmin, box.ymin, buf, strlen(buf),
388                                  fontid, BlackPixel, WhitePixel);
389                  return(0);
390 +        case 'i':                               /* identify (contour) */
391 +                if (ourras->pixels == NULL)
392 +                        return(-1);
393 +                n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)];
394 +                n = ourras->pmap[n];
395 +                cvx.pixel = ourras->cdefs[n].pixel;
396 +                cvx.red = random() & 65535;
397 +                cvx.green = random() & 65535;
398 +                cvx.blue = random() & 65535;
399 +                XStoreColor(&cvx);
400 +                return(0);
401          case 'p':                               /* position */
402                  sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
403                  XText(wind, ekey->x, ekey->y, buf, strlen(buf),
# Line 409 | Line 408 | XKeyEvent  *ekey;
408                          XFeep(0);
409                          return(-1);
410                  }
411 <                rayview(rorg, rdir, &ourview,
412 <                                ekey->x-xoff + .5, ymax-1-ekey->y+yoff + .5);
411 >                if (viewray(rorg, rdir, &ourview, (ekey->x-xoff+.5)/xmax,
412 >                                (ymax-1-ekey->y+yoff+.5)/ymax) < 0)
413 >                        return(-1);
414                  printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
415                  printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
416                  fflush(stdout);
# Line 419 | Line 419 | XKeyEvent  *ekey;
419                  if (avgbox(cval) == -1)
420                          return(-1);
421                  n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
422 <                if (n == 0) {
423 <                        XFeep(0);
422 >                if (n == 0)
423                          return(0);
425                }
424                  scale_rcolors(ourras, pow(2.0, (double)n));
425                  scale += n;
426                  sprintf(buf, "%+d", scale);
# Line 431 | Line 429 | XKeyEvent  *ekey;
429                  XFlush();
430                  free_raster(ourras);
431                  getras();
432 <                return(redraw(0, 0, width, height));
433 <        case CTRL(R):                           /* redraw */
432 >        /* fall through */
433 >        case CTRL('R'):                         /* redraw */
434 >        case CTRL('L'):
435 >                unmap_rcolors(ourras);
436                  XClear(wind);
437                  return(redraw(0, 0, width, height));
438          case ' ':                               /* clear */
# Line 543 | Line 543 | getmono()                      /* get monochrome data */
543   {
544          register unsigned short *dp;
545          register int    x, err;
546 <        int     y;
547 <        rgbpixel        *inline;
546 >        int     y, errp;
547 >        rgbpixel        *inl;
548          short   *cerr;
549  
550 <        if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
550 >        if ((inl = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
551                          || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
552 <                quit("out of memory in getmono");
552 >                quiterr("out of memory in getmono");
553          dp = ourras->data.m - 1;
554          for (y = 0; y < ymax; y++) {
555 <                picreadline3(y, inline);
555 >                picreadline3(y, inl);
556                  err = 0;
557                  for (x = 0; x < xmax; x++) {
558                          if (!(x&0xf))
559                                  *++dp = 0;
560 <                        err += rgb_bright(&inline[x]) + cerr[x];
560 >                        errp = err;
561 >                        err += rgb_bright(&inl[x]) + cerr[x];
562                          if (err > 127)
563                                  err -= 255;
564                          else
565                                  *dp |= 1<<(x&0xf);
566 <                        cerr[x] = err >>= 1;
566 >                        err /= 3;
567 >                        cerr[x] = err + errp;
568                  }
569          }
570 <        free((char *)inline);
570 >        free((char *)inl);
571          free((char *)cerr);
572   }
573  
# Line 606 | Line 608 | register XRASTER       *xr;
608   double  sf;
609   {
610          register int    i;
611 <        int     maxv;
611 >        long    maxv;
612  
613 +        if (xr->pixels == NULL)
614 +                return;
615 +
616          sf = pow(sf, 1.0/gamcor);
617 <        maxv = (1<<16) / sf;
617 >        maxv = 65535/sf;
618  
619          for (i = xr->ncolors; i--; ) {
620 <                xr->cdefs[i].red = xr->cdefs[i].red >= maxv ?
621 <                                (1<<16)-1 :
620 >                xr->cdefs[i].red = xr->cdefs[i].red > maxv ?
621 >                                65535 :
622                                  xr->cdefs[i].red * sf;
623 <                xr->cdefs[i].green = xr->cdefs[i].green >= maxv ?
624 <                                (1<<16)-1 :
623 >                xr->cdefs[i].green = xr->cdefs[i].green > maxv ?
624 >                                65535 :
625                                  xr->cdefs[i].green * sf;
626 <                xr->cdefs[i].blue = xr->cdefs[i].blue >= maxv ?
627 <                                (1<<16)-1 :
626 >                xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ?
627 >                                65535 :
628                                  xr->cdefs[i].blue * sf;
629          }
630 <        if (xr->pixels != NULL)
626 <                XStoreColors(xr->ncolors, xr->cdefs);
630 >        XStoreColors(xr->ncolors, xr->cdefs);
631   }
632  
633  
# Line 634 | Line 638 | int  y;
638                  if (scanpos == NULL || scanpos[y] == -1)
639                          return(-1);
640                  if (fseek(fin, scanpos[y], 0) == -1)
641 <                        quit("fseek error");
641 >                        quiterr("fseek error");
642                  cury = y;
643          } else if (scanpos != NULL)
644                  scanpos[y] = ftell(fin);
# Line 651 | Line 655 | picreadline3(y, l3)                    /* read in 3-byte scanline */
655   int  y;
656   register rgbpixel  *l3;
657   {
658 <        register BYTE   *l4;
659 <        register int    shift, c;
656 <        int     i;
657 <
658 >        register int    i;
659 >                                                        /* read scanline */
660          if (getscan(y) < 0)
661                  quiterr("cannot seek for picreadline");
662                                                          /* convert scanline */
663 <        for (l4=scanline[0], i=xmax; i--; l4+=4, l3++) {
664 <                shift = l4[EXP] - COLXS + scale;
665 <                if (shift >= 8) {
666 <                        l3->r = l3->g = l3->b = 255;
667 <                } else if (shift <= -8) {
666 <                        l3->r = l3->g = l3->b = 0;
667 <                } else if (shift > 0) {
668 <                        c = l4[RED] << shift;
669 <                        l3->r = c > 255 ? 255 : c;
670 <                        c = l4[GRN] << shift;
671 <                        l3->g = c > 255 ? 255 : c;
672 <                        c = l4[BLU] << shift;
673 <                        l3->b = c > 255 ? 255 : c;
674 <                } else if (shift < 0) {
675 <                        l3->r = l4[RED] >> -shift;
676 <                        l3->g = l4[GRN] >> -shift;
677 <                        l3->b = l4[BLU] >> -shift;
678 <                } else {
679 <                        l3->r = l4[RED];
680 <                        l3->g = l4[GRN];
681 <                        l3->b = l4[BLU];
682 <                }
663 >        normcolrs(scanline, xmax, scale);
664 >        for (i = 0; i < xmax; i++) {
665 >                l3[i].r = scanline[i][RED];
666 >                l3[i].g = scanline[i][GRN];
667 >                l3[i].b = scanline[i][BLU];
668          }
669   }
670  
# Line 688 | Line 673 | picwriteline(y, l)             /* add 8-bit scanline to image */
673   int  y;
674   pixel  *l;
675   {
676 <        bcopy(l, ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1));
676 >        bcopy((char *)l, (char *)ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1));
677   }
678  
679  
680 < picreadcm(map)                  /* do gamcor correction */
680 > picreadcm(map)                  /* do gamma correction */
681   colormap  map;
682   {
683          extern double  pow();
684          register int  i, val;
685  
686          for (i = 0; i < 256; i++) {
687 <                val = pow(i/256.0, 1.0/gamcor) * 256.0;
687 >                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
688                  map[0][i] = map[1][i] = map[2][i] = val;
689          }
690   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines