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.2 by greg, Thu Dec 19 14:52:31 1991 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 */
# Line 38 | Line 42 | static char SCCSid[] = "$SunId$ LBL";
42   #define  BORWIDTH       5               /* border width */
43   #define  BARHEIGHT      25              /* menu bar size */
44  
45 < double  gamcor = 2.0;                   /* gamcor correction */
45 > double  gamcor = 2.2;                   /* gamma correction */
46  
47 < XRASTER  ourras;                        /* our stored raster image */
47 > XRASTER  *ourras = NULL;                /* our stored raster image */
48  
49   int  dither = 1;                        /* dither colors? */
50   int  fast = 0;                          /* keep picture in Pixmap? */
# Line 51 | Line 55 | Font  fontid;                          /* our font */
55   int  maxcolors = 0;                     /* maximum colors */
56   int  greyscale = 0;                     /* in grey */
57  
58 + int  scale = 0;                         /* scalefactor; power of two */
59 +
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 68 | 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 82 | Line 90 | extern long  ftell();
90  
91   extern char  *malloc(), *calloc();
92  
93 < extern double  atof();
93 > extern double  pow(), log();
94  
95  
96   main(argc, argv)
# Line 112 | Line 120 | char  *argv[];
120                          case 'f':
121                                  fast = !fast;
122                                  break;
123 +                        case 'e':
124 +                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
125 +                                        goto userr;
126 +                                scale = atoi(argv[++i]);
127 +                                break;
128                          case 'g':
129                                  gamcor = atof(argv[++i]);
130                                  break;
# Line 130 | Line 143 | char  *argv[];
143                          sprintf(errmsg, "can't open file \"%s\"", fname);
144                          quiterr(errmsg);
145                  }
146 <        } else
134 <                goto userr;
135 <
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;
144 <                ourview.vresolu = ymax;
145 <                if (setview(&ourview) != NULL)
146 <                        gotview = 0;
147 <        }
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 154 | Line 161 | char  *argv[];
161                  getevent();             /* main loop */
162   userr:
163          fprintf(stderr,
164 <        "Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors] file\n",
164 >        "Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
165                          progname);
166          quit(1);
167   }
# Line 163 | Line 170 | userr:
170   headline(s)             /* get relevant info from header */
171   char  *s;
172   {
173 <        static char  *altname[] = {"rview","rpict","VIEW=",NULL};
173 >        static char  *altname[] = {"rview","rpict",VIEWSTR,NULL};
174          register char  **an;
175 +        char  fmt[32];
176  
177 <        if (!strncmp(s, "EXPOSURE=", 9))
178 <                exposure *= atof(s+9);
179 <        else
177 >        if (isexpos(s))
178 >                exposure *= exposval(s);
179 >        else if (isformat(s)) {
180 >                formatval(fmt, s);
181 >                wrongformat = strcmp(fmt, COLRFMT);
182 >        } else
183                  for (an = altname; *an != NULL; an++)
184                          if (!strncmp(*an, s, strlen(*an))) {
185 <                                if (sscanview(&ourview, s+strlen(*an)) == 0)
185 >                                if (sscanview(&ourview, s+strlen(*an)) > 0)
186                                          gotview++;
187                                  return;
188                          }
189   }
190  
191  
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
192   init()                  /* get data and open window */
193   {
194          register int  i;
194        colormap  ourmap;
195          OpaqueFrame  mainframe;
196          char  defgeom[32];
197          
# Line 213 | Line 213 | init()                 /* get data and open window */
213                  maxcolors = 1<<DisplayPlanes();
214                  if (maxcolors > 4) maxcolors -= 2;
215          }
216        ourras.width = xmax;
217        ourras.height = ymax;
216                                  /* store image */
217 <        if (maxcolors <= 2) {           /* monochrome */
218 <                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 <        }
217 >        getras();
218 >
219          mainframe.bdrwidth = BORWIDTH;
220          mainframe.border = WhitePixmap;
221          mainframe.background = BlackPixmap;
# Line 284 | Line 268 | int  code;
268   }
269  
270  
271 + getras()                                /* get raster file */
272 + {
273 +        colormap        ourmap;
274 +
275 +        ourras = (XRASTER *)calloc(1, sizeof(XRASTER));
276 +        if (ourras == NULL)
277 +                goto memerr;
278 +        ourras->width = xmax;
279 +        ourras->height = ymax;
280 +        if (maxcolors <= 2) {           /* monochrome */
281 +                ourras->data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax));
282 +                if (ourras->data.m == NULL)
283 +                        goto memerr;
284 +                getmono();
285 +        } else {
286 +                ourras->data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax));
287 +                if (ourras->data.bz == NULL)
288 +                        goto memerr;
289 +                if (greyscale)
290 +                        biq(dither,maxcolors,1,ourmap);
291 +                else
292 +                        ciq(dither,maxcolors,1,ourmap);
293 +                if (init_rcolors(ourras, ourmap) == 0)
294 +                        goto memerr;
295 +        }
296 +        return;
297 + memerr:
298 +        quiterr("out of memory");
299 + }
300 +
301 +
302   getevent()                              /* process the next event */
303   {
304          WindowInfo  info;
# Line 303 | Line 318 | getevent()                             /* process the next event */
318                  fixwindow(&e);
319                  break;
320          case UnmapWindow:
321 <                unmap_rcolors(&ourras);
321 >                unmap_rcolors(ourras);
322                  break;
323          case ButtonPressed:
324                  if (controlshift(&e))
# Line 326 | Line 341 | redraw(x, y, w, h)                     /* redraw section of window */
341   int  x, y;
342   int  w, h;
343   {
344 <        map_rcolors(&ourras);
344 >        if (ourras->ncolors && map_rcolors(ourras) == NULL) {
345 >                fprintf(stderr, "%s: cannot allocate colors\n", progname);
346 >                return(-1);
347 >        }
348          if (fast)
349 <                make_rpixmap(&ourras);
350 <        return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,&ourras) ? 0 : -1);
349 >                make_rpixmap(ourras);
350 >        return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras) ? 0 : -1);
351   }
352  
353  
# Line 338 | Line 356 | XKeyEvent  *ekey;
356   {
357          char  buf[80];
358          COLOR  cval;
359 +        Color  cvx;
360          char  *cp;
361          int  n;
362 +        double  comp;
363          FVECT  rorg, rdir;
364  
365          cp = XLookupMapping(ekey, &n);
# Line 347 | Line 367 | XKeyEvent  *ekey;
367                  return(0);
368          switch (*cp) {                  /* interpret command */
369          case 'q':
370 <        case CTRL(D):                           /* quiterr */
370 >        case CTRL(D):                           /* quit */
371                  quit(0);
372          case '\n':
373          case '\r':
# Line 358 | Line 378 | XKeyEvent  *ekey;
378                  switch (*cp) {
379                  case '\n':
380                  case '\r':                              /* radiance */
381 <                        sprintf(buf, "%-3g", intens(cval)/exposure);
381 >                        sprintf(buf, "%.3f", intens(cval)/exposure);
382                          break;
383                  case 'l':                               /* luminance */
384 <                        sprintf(buf, "%-3gL", bright(cval)*683.0/exposure);
384 >                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
385                          break;
386                  case 'c':                               /* color */
387 +                        comp = pow(2.0, (double)scale);
388                          sprintf(buf, "(%.2f,%.2f,%.2f)",
389 <                                        colval(cval,RED),
390 <                                        colval(cval,GRN),
391 <                                        colval(cval,BLU));
389 >                                        colval(cval,RED)*comp,
390 >                                        colval(cval,GRN)*comp,
391 >                                        colval(cval,BLU)*comp);
392                          break;
393                  }
394                  XText(wind, box.xmin, box.ymin, buf, strlen(buf),
395                                  fontid, BlackPixel, WhitePixel);
396                  return(0);
397 +        case 'i':                               /* identify (contour) */
398 +                if (ourras->pixels == NULL)
399 +                        return(-1);
400 +                n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)];
401 +                n = ourras->pmap[n];
402 +                cvx.pixel = ourras->cdefs[n].pixel;
403 +                cvx.red = random() & 65535;
404 +                cvx.green = random() & 65535;
405 +                cvx.blue = random() & 65535;
406 +                XStoreColor(&cvx);
407 +                return(0);
408          case 'p':                               /* position */
409                  sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
410                  XText(wind, ekey->x, ekey->y, buf, strlen(buf),
# Line 383 | Line 415 | XKeyEvent  *ekey;
415                          XFeep(0);
416                          return(-1);
417                  }
418 <                rayview(rorg, rdir, &ourview,
419 <                                ekey->x-xoff + .5, ymax-1-ekey->y+yoff + .5);
418 >                if (viewray(rorg, rdir, &ourview, (ekey->x-xoff+.5)/xmax,
419 >                                (ymax-1-ekey->y+yoff+.5)/ymax) < 0)
420 >                        return(-1);
421                  printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
422                  printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
423                  fflush(stdout);
424                  return(0);
425 +        case '=':                               /* adjust exposure */
426 +                if (avgbox(cval) == -1)
427 +                        return(-1);
428 +                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
429 +                if (n == 0)
430 +                        return(0);
431 +                scale_rcolors(ourras, pow(2.0, (double)n));
432 +                scale += n;
433 +                sprintf(buf, "%+d", scale);
434 +                XText(wind, box.xmin, box.ymin, buf, strlen(buf),
435 +                                fontid, BlackPixel, WhitePixel);
436 +                XFlush();
437 +                free_raster(ourras);
438 +                getras();
439 +        /* fall through */
440          case CTRL(R):                           /* redraw */
441 +        case CTRL(L):
442 +                unmap_rcolors(ourras);
443                  XClear(wind);
444                  return(redraw(0, 0, width, height));
445          case ' ':                               /* clear */
# Line 500 | Line 550 | getmono()                      /* get monochrome data */
550   {
551          register unsigned short *dp;
552          register int    x, err;
553 <        int     y;
554 <        rgbpixel        *inline;
553 >        int     y, errp;
554 >        rgbpixel        *inl;
555          short   *cerr;
556  
557 <        if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
557 >        if ((inl = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
558                          || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
559 <                quit("out of memory in getmono");
560 <        dp = ourras.data.m - 1;
559 >                quiterr("out of memory in getmono");
560 >        dp = ourras->data.m - 1;
561          for (y = 0; y < ymax; y++) {
562 <                picreadline3(y, inline);
562 >                picreadline3(y, inl);
563                  err = 0;
564                  for (x = 0; x < xmax; x++) {
565                          if (!(x&0xf))
566                                  *++dp = 0;
567 <                        err += rgb_bright(&inline[x]) + cerr[x];
567 >                        errp = err;
568 >                        err += rgb_bright(&inl[x]) + cerr[x];
569                          if (err > 127)
570                                  err -= 255;
571                          else
572                                  *dp |= 1<<(x&0xf);
573 <                        cerr[x] = err >>= 1;
573 >                        err /= 3;
574 >                        cerr[x] = err + errp;
575                  }
576          }
577 <        free((char *)inline);
577 >        free((char *)inl);
578          free((char *)cerr);
579   }
580  
581  
582 < init_rcolors(xr, cmap)                          /* assign color values */
582 > init_rcolors(xr, cmap)                          /* (re)assign color values */
583   register XRASTER        *xr;
584   colormap        cmap;
585   {
# Line 558 | Line 610 | colormap       cmap;
610   }
611  
612  
613 + scale_rcolors(xr, sf)                   /* scale color map */
614 + register XRASTER        *xr;
615 + double  sf;
616 + {
617 +        register int    i;
618 +        long    maxv;
619 +
620 +        if (xr->pixels == NULL)
621 +                return;
622 +
623 +        sf = pow(sf, 1.0/gamcor);
624 +        maxv = 65535/sf;
625 +
626 +        for (i = xr->ncolors; i--; ) {
627 +                xr->cdefs[i].red = xr->cdefs[i].red > maxv ?
628 +                                65535 :
629 +                                xr->cdefs[i].red * sf;
630 +                xr->cdefs[i].green = xr->cdefs[i].green > maxv ?
631 +                                65535 :
632 +                                xr->cdefs[i].green * sf;
633 +                xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ?
634 +                                65535 :
635 +                                xr->cdefs[i].blue * sf;
636 +        }
637 +        XStoreColors(xr->ncolors, xr->cdefs);
638 + }
639 +
640 +
641   getscan(y)
642   int  y;
643   {
# Line 565 | Line 645 | int  y;
645                  if (scanpos == NULL || scanpos[y] == -1)
646                          return(-1);
647                  if (fseek(fin, scanpos[y], 0) == -1)
648 <                        quit("fseek error");
648 >                        quiterr("fseek error");
649                  cury = y;
650          } else if (scanpos != NULL)
651                  scanpos[y] = ftell(fin);
# Line 582 | Line 662 | picreadline3(y, l3)                    /* read in 3-byte scanline */
662   int  y;
663   register rgbpixel  *l3;
664   {
665 <        register BYTE   *l4;
666 <        register int    shift, c;
587 <        int     i;
588 <
665 >        register int    i;
666 >                                                        /* read scanline */
667          if (getscan(y) < 0)
668                  quiterr("cannot seek for picreadline");
669                                                          /* convert scanline */
670 <        for (l4=scanline[0], i=xmax; i--; l4+=4, l3++) {
671 <                shift = l4[EXP] - COLXS;
672 <                if (shift >= 8) {
673 <                        l3->r = l3->g = l3->b = 255;
674 <                } 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 <                }
670 >        normcolrs(scanline, xmax, scale);
671 >        for (i = 0; i < xmax; i++) {
672 >                l3[i].r = scanline[i][RED];
673 >                l3[i].g = scanline[i][GRN];
674 >                l3[i].b = scanline[i][BLU];
675          }
676   }
677  
# Line 619 | Line 680 | picwriteline(y, l)             /* add 8-bit scanline to image */
680   int  y;
681   pixel  *l;
682   {
683 <        bcopy(l, ourras.data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1));
683 >        bcopy((char *)l, (char *)ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1));
684   }
685  
686  
687 < picreadcm(map)                  /* do gamcor correction */
687 > picreadcm(map)                  /* do gamma correction */
688   colormap  map;
689   {
690          extern double  pow();
691          register int  i, val;
692  
693          for (i = 0; i < 256; i++) {
694 <                val = pow(i/256.0, 1.0/gamcor) * 256.0;
694 >                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
695                  map[0][i] = map[1][i] = map[2][i] = val;
696          }
697   }
# Line 639 | Line 700 | colormap  map;
700   picwritecm(map)                 /* handled elsewhere */
701   colormap  map;
702   {
703 + #ifdef DEBUG
704 +        register int i;
705 +
706 +        for (i = 0; i < 256; i++)
707 +                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
708 + #endif
709   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines