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.13 by schorsch, Fri Jan 2 12:47:01 2004 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 18 | Line 15 | static char SCCSid[] = "$SunId$ LBL";
15   #include  <X/cursors/bcross_mask.cursor>
16  
17   #include  <sys/types.h>
18 <
18 > #include  <string.h>
19   #include  <ctype.h>
20 + #include  <time.h>
21  
22   #include  "color.h"
23 <
23 > #include  "resolu.h"
24   #include  "xraster.h"
27
25   #include  "view.h"
29
26   #include  "pic.h"
27 + #include  "random.h"
28  
29   #define  controlshift(e)        (((XButtonEvent *)(e))->detail & (ShiftMask|ControlMask))
30  
31   #define  FONTNAME       "9x15"          /* text font we'll use */
32  
33 < #define  CTRL(c)        ('c'-'@')
33 > #define  CTRL(c)        ((c)-'@')
34  
35   #define  BORWIDTH       5               /* border width */
36   #define  BARHEIGHT      25              /* menu bar size */
37  
38 < double  gamcor = 2.0;                   /* gamma correction */
38 > double  gamcor = 2.2;                   /* gamma correction */
39  
40   XRASTER  *ourras = NULL;                /* our stored raster image */
41  
# Line 56 | Line 53 | int  scale = 0;                                /* scalefactor; power of two */
53   int  xoff = 0;                          /* x image offset */
54   int  yoff = 0;                          /* y image offset */
55  
56 < VIEW  ourview = STDVIEW(0);             /* image view parameters */
56 > VIEW  ourview = STDVIEW;                /* image view parameters */
57   int  gotview = 0;                       /* got parameters from file */
58  
59   COLR  *scanline;                        /* scan line buffer */
# Line 70 | Line 67 | int  cury = 0;                         /* current scan location */
67  
68   double  exposure = 1.0;                 /* exposure compensation used */
69  
70 + int  wrongformat = 0;                   /* input in another format */
71 +
72   struct {
73          int  xmin, ymin, xsiz, ysiz;
74   }  box = {0, 0, 0, 0};                  /* current box */
# Line 80 | Line 79 | char  *progname;
79  
80   char  errmsg[128];
81  
82 < extern long  ftell();
82 > static gethfunc headline;
83  
85 extern char  *malloc(), *calloc();
84  
87 extern double  atof(), pow(), log();
88
89
85   main(argc, argv)
86   int  argc;
87   char  *argv[];
88   {
89 <        int  headline();
89 >        extern char  *getenv();
90 >        char  *gv;
91          int  i;
92          
93          progname = argv[0];
94 +        if ((gv = getenv("DISPLAY_GAMMA")) != NULL)
95 +                gamcor = atof(gv);
96  
97          for (i = 1; i < argc; i++)
98                  if (argv[i][0] == '-')
# Line 137 | Line 135 | char  *argv[];
135                          sprintf(errmsg, "can't open file \"%s\"", fname);
136                          quiterr(errmsg);
137                  }
138 <        } else
141 <                goto userr;
142 <
138 >        }
139                                  /* get header */
140 <        getheader(fin, headline);
140 >        getheader(fin, headline, NULL);
141                                  /* get picture dimensions */
142 <        if (fscanf(fin, "-Y %d +X %d\n", &ymax, &xmax) != 2)
142 >        if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
143                  quiterr("bad picture size");
144                                  /* set view parameters */
145 <        if (gotview) {
146 <                ourview.hresolu = xmax;
151 <                ourview.vresolu = ymax;
152 <                if (setview(&ourview) != NULL)
153 <                        gotview = 0;
154 <        }
145 >        if (gotview && setview(&ourview) != NULL)
146 >                gotview = 0;
147          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
148                  quiterr("out of memory");
149  
# Line 167 | Line 159 | userr:
159   }
160  
161  
162 < headline(s)             /* get relevant info from header */
163 < char  *s;
162 > static int
163 > headline(               /* get relevant info from header */
164 >        char    *s,
165 >        void    *p
166 > )
167   {
168 <        static char  *altname[] = {"rview","rpict","VIEW=",NULL};
174 <        register char  **an;
168 >        char  fmt[32];
169  
170 <        if (!strncmp(s, "EXPOSURE=", 9))
171 <                exposure *= atof(s+9);
172 <        else
173 <                for (an = altname; *an != NULL; an++)
174 <                        if (!strncmp(*an, s, strlen(*an))) {
175 <                                if (sscanview(&ourview, s+strlen(*an)) == 0)
176 <                                        gotview++;
177 <                                return;
184 <                        }
170 >        if (isexpos(s))
171 >                exposure *= exposval(s);
172 >        else if (isformat(s)) {
173 >                formatval(fmt, s);
174 >                wrongformat = strcmp(fmt, COLRFMT);
175 >        } else if (isview(s) && sscanview(&ourview, s) > 0)
176 >                gotview++;
177 >        return(0);
178   }
179  
180  
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
181   init()                  /* get data and open window */
182   {
183          register int  i;
# Line 260 | Line 243 | char  *err;
243   }
244  
245  
246 + void
247   eputs(s)
248   char    *s;
249   {
# Line 267 | Line 251 | char   *s;
251   }
252  
253  
254 + void
255   quit(code)
256   int  code;
257   {
# Line 301 | Line 286 | getras()                               /* get raster file */
286          }
287          return;
288   memerr:
289 <        quit("out of memory");
289 >        quiterr("out of memory");
290   }
291  
292  
# Line 347 | Line 332 | redraw(x, y, w, h)                     /* redraw section of window */
332   int  x, y;
333   int  w, h;
334   {
335 <        if (map_rcolors(ourras) == NULL) {
335 >        if (ourras->ncolors && map_rcolors(ourras) == NULL) {
336                  fprintf(stderr, "%s: cannot allocate colors\n", progname);
337                  return(-1);
338          }
# Line 362 | Line 347 | XKeyEvent  *ekey;
347   {
348          char  buf[80];
349          COLOR  cval;
350 +        Color  cvx;
351          char  *cp;
352          int  n;
353          double  comp;
# Line 372 | Line 358 | XKeyEvent  *ekey;
358                  return(0);
359          switch (*cp) {                  /* interpret command */
360          case 'q':
361 <        case CTRL(D):                           /* quiterr */
361 >        case CTRL('D'):                         /* quit */
362                  quit(0);
363          case '\n':
364          case '\r':
# Line 383 | Line 369 | XKeyEvent  *ekey;
369                  switch (*cp) {
370                  case '\n':
371                  case '\r':                              /* radiance */
372 <                        sprintf(buf, "%-3g", intens(cval)/exposure);
372 >                        sprintf(buf, "%.3f", intens(cval)/exposure);
373                          break;
374                  case 'l':                               /* luminance */
375 <                        sprintf(buf, "%-3gL", bright(cval)*683.0/exposure);
375 >                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
376                          break;
377                  case 'c':                               /* color */
378                          comp = pow(2.0, (double)scale);
# Line 399 | Line 385 | XKeyEvent  *ekey;
385                  XText(wind, box.xmin, box.ymin, buf, strlen(buf),
386                                  fontid, BlackPixel, WhitePixel);
387                  return(0);
388 +        case 'i':                               /* identify (contour) */
389 +                if (ourras->pixels == NULL)
390 +                        return(-1);
391 +                n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)];
392 +                n = ourras->pmap[n];
393 +                cvx.pixel = ourras->cdefs[n].pixel;
394 +                cvx.red = random() & 65535;
395 +                cvx.green = random() & 65535;
396 +                cvx.blue = random() & 65535;
397 +                XStoreColor(&cvx);
398 +                return(0);
399          case 'p':                               /* position */
400                  sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
401                  XText(wind, ekey->x, ekey->y, buf, strlen(buf),
# Line 409 | Line 406 | XKeyEvent  *ekey;
406                          XFeep(0);
407                          return(-1);
408                  }
409 <                rayview(rorg, rdir, &ourview,
410 <                                ekey->x-xoff + .5, ymax-1-ekey->y+yoff + .5);
409 >                if (viewray(rorg, rdir, &ourview, (ekey->x-xoff+.5)/xmax,
410 >                                (ymax-1-ekey->y+yoff+.5)/ymax) < 0)
411 >                        return(-1);
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);
# Line 419 | Line 417 | XKeyEvent  *ekey;
417                  if (avgbox(cval) == -1)
418                          return(-1);
419                  n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
420 <                if (n == 0) {
423 <                        XFeep(0);
420 >                if (n == 0)
421                          return(0);
425                }
422                  scale_rcolors(ourras, pow(2.0, (double)n));
423                  scale += n;
424                  sprintf(buf, "%+d", scale);
# Line 431 | Line 427 | XKeyEvent  *ekey;
427                  XFlush();
428                  free_raster(ourras);
429                  getras();
430 <                return(redraw(0, 0, width, height));
431 <        case CTRL(R):                           /* redraw */
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 */
# Line 543 | Line 541 | getmono()                      /* get monochrome data */
541   {
542          register unsigned short *dp;
543          register int    x, err;
544 <        int     y;
545 <        rgbpixel        *inline;
544 >        int     y, errp;
545 >        rgbpixel        *inl;
546          short   *cerr;
547  
548 <        if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
548 >        if ((inl = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
549                          || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
550 <                quit("out of memory in getmono");
550 >                quiterr("out of memory in getmono");
551          dp = ourras->data.m - 1;
552          for (y = 0; y < ymax; y++) {
553 <                picreadline3(y, inline);
553 >                picreadline3(y, inl);
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];
558 >                        errp = err;
559 >                        err += rgb_bright(&inl[x]) + cerr[x];
560                          if (err > 127)
561                                  err -= 255;
562                          else
563                                  *dp |= 1<<(x&0xf);
564 <                        cerr[x] = err >>= 1;
564 >                        err /= 3;
565 >                        cerr[x] = err + errp;
566                  }
567          }
568 <        free((char *)inline);
569 <        free((char *)cerr);
568 >        free((void *)inl);
569 >        free((void *)cerr);
570   }
571  
572  
# Line 594 | Line 594 | colormap       cmap;
594                          xr->cdefs[xr->ncolors].pixel = *p;
595                          xr->pmap[*p] = xr->ncolors++;
596                  }
597 <        xr->cdefs = (Color *)realloc((char *)xr->cdefs, xr->ncolors*sizeof(Color));
597 >        xr->cdefs = (Color *)realloc((void *)xr->cdefs, xr->ncolors*sizeof(Color));
598          if (xr->cdefs == NULL)
599                  return(0);
600          return(1);
# Line 606 | Line 606 | register XRASTER       *xr;
606   double  sf;
607   {
608          register int    i;
609 <        int     maxv;
609 >        long    maxv;
610  
611 +        if (xr->pixels == NULL)
612 +                return;
613 +
614          sf = pow(sf, 1.0/gamcor);
615 <        maxv = (1<<16) / sf;
615 >        maxv = 65535/sf;
616  
617          for (i = xr->ncolors; i--; ) {
618 <                xr->cdefs[i].red = xr->cdefs[i].red >= maxv ?
619 <                                (1<<16)-1 :
618 >                xr->cdefs[i].red = xr->cdefs[i].red > maxv ?
619 >                                65535 :
620                                  xr->cdefs[i].red * sf;
621 <                xr->cdefs[i].green = xr->cdefs[i].green >= maxv ?
622 <                                (1<<16)-1 :
621 >                xr->cdefs[i].green = xr->cdefs[i].green > maxv ?
622 >                                65535 :
623                                  xr->cdefs[i].green * sf;
624 <                xr->cdefs[i].blue = xr->cdefs[i].blue >= maxv ?
625 <                                (1<<16)-1 :
624 >                xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ?
625 >                                65535 :
626                                  xr->cdefs[i].blue * sf;
627          }
628 <        if (xr->pixels != NULL)
626 <                XStoreColors(xr->ncolors, xr->cdefs);
628 >        XStoreColors(xr->ncolors, xr->cdefs);
629   }
630  
631  
# Line 634 | Line 636 | int  y;
636                  if (scanpos == NULL || scanpos[y] == -1)
637                          return(-1);
638                  if (fseek(fin, scanpos[y], 0) == -1)
639 <                        quit("fseek error");
639 >                        quiterr("fseek error");
640                  cury = y;
641 <        } else if (scanpos != NULL)
641 >        } else if (scanpos != NULL && scanpos[y] == -1)
642                  scanpos[y] = ftell(fin);
643  
644          if (freadcolrs(scanline, xmax, fin) < 0)
# Line 651 | Line 653 | picreadline3(y, l3)                    /* read in 3-byte scanline */
653   int  y;
654   register rgbpixel  *l3;
655   {
656 <        register BYTE   *l4;
657 <        register int    shift, c;
656 <        int     i;
657 <
656 >        register int    i;
657 >                                                        /* read scanline */
658          if (getscan(y) < 0)
659                  quiterr("cannot seek for picreadline");
660                                                          /* convert scanline */
661 <        for (l4=scanline[0], i=xmax; i--; l4+=4, l3++) {
662 <                shift = l4[EXP] - COLXS + scale;
663 <                if (shift >= 8) {
664 <                        l3->r = l3->g = l3->b = 255;
665 <                } 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 <                }
661 >        normcolrs(scanline, xmax, scale);
662 >        for (i = 0; i < xmax; i++) {
663 >                l3[i].r = scanline[i][RED];
664 >                l3[i].g = scanline[i][GRN];
665 >                l3[i].b = scanline[i][BLU];
666          }
667   }
668  
# Line 688 | Line 671 | picwriteline(y, l)             /* add 8-bit scanline to image */
671   int  y;
672   pixel  *l;
673   {
674 <        bcopy(l, ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1));
674 >        memcpy((void *)ourras->data.bz+BZPixmapSize(xmax,y), (void *)l, BZPixmapSize(xmax,1));
675   }
676  
677  
678 < picreadcm(map)                  /* do gamcor correction */
678 > picreadcm(map)                  /* do gamma correction */
679   colormap  map;
680   {
681          extern double  pow();
682          register int  i, val;
683  
684          for (i = 0; i < 256; i++) {
685 <                val = pow(i/256.0, 1.0/gamcor) * 256.0;
685 >                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
686                  map[0][i] = map[1][i] = map[2][i] = val;
687          }
688   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines