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

Comparing ray/src/px/ra_pr.c (file contents):
Revision 1.7 by greg, Fri Feb 9 13:59:19 1990 UTC vs.
Revision 2.7 by schorsch, Sun Mar 28 20:33:14 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 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   *  ra_pr.c - program to convert between RADIANCE and pixrect picture format.
6   *
# Line 12 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   */
10  
11   #include  <stdio.h>
12 + #include  <math.h>
13 + #include  <time.h>
14  
15 + #include  "rtmisc.h"
16   #include  "rasterfile.h"
17
17   #include  "color.h"
18 <
18 > #include  "resolu.h"
19   #include  "pic.h"
20  
21                          /* descriptor for a picture file or frame buffer */
# Line 31 | Line 30 | typedef struct {
30          } pos;                          /* position(s) */
31   } pic;
32  
34 extern pic      *openinput(), *openoutput();
33  
34 < extern char     *ecalloc(), *emalloc();
35 <
38 < extern long  ftell();
39 <
40 < extern double  atof(), pow();
41 <
42 < double  gamma = 2.0;                    /* gamma correction */
43 <
34 > double  gamcor = 2.2;                   /* gamma correction */
35 > int  bradj = 0;                         /* brightness adjustment */
36   pic     *inpic, *outpic;
45
37   char  *progname;
47
38   char  errmsg[128];
39 <
50 < COLR    *inline;
51 <
39 > COLR    *inl;
40   int  xmax, ymax;
41  
42 + static void quiterr(char  *err);
43 + static pic * openinput(char  *fname, struct rasterfile  *h);
44 + static pic * openoutput(char  *fname, struct rasterfile  *h);
45 + static void pr2ra(struct rasterfile  *h);
46  
47 < main(argc, argv)
48 < int  argc;
49 < char  *argv[];
47 >
48 > int
49 > main(int  argc, char  *argv[])
50   {
51          colormap  rasmap;
52          struct rasterfile  head;
# Line 73 | Line 65 | char  *argv[];
65                                  dither = !dither;
66                                  break;
67                          case 'g':
68 <                                gamma = atof(argv[++i]);
68 >                                gamcor = atof(argv[++i]);
69                                  break;
70                          case 'b':
71                                  greyscale = !greyscale;
72                                  break;
73 +                        case 'e':
74 +                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
75 +                                        goto userr;
76 +                                bradj = atoi(argv[++i]);
77 +                                break;
78                          case 'r':
79                                  reverse = !reverse;
80                                  break;
# Line 113 | Line 110 | char  *argv[];
110                                  head.ras_depth != 8)
111                          quiterr("incompatible format");
112                                          /* put header */
113 +                newheader("RADIANCE", stdout);
114                  printargs(i, argv, stdout);
115 +                fputformat(COLRFMT, stdout);
116                  putchar('\n');
117 <                fputresolu(YMAJOR|YDECR, xmax, ymax, stdout);
117 >                fprtresolu(xmax, ymax, stdout);
118                                          /* convert file */
119                  pr2ra(&head);
120          } else {
121 <                if (i > argc-1 || i < argc-2)
121 >                if (i < argc-2 || (!greyscale && i > argc-1))
122                          goto userr;
123                  if ((inpic = openinput(argv[i], &head)) == NULL) {
124                          sprintf(errmsg, "can't open input \"%s\"", argv[i]);
# Line 139 | Line 138 | char  *argv[];
138          quiterr(NULL);
139   userr:
140          fprintf(stderr,
141 <        "Usage: %s [-d][-c ncolors][-b][-g gamma] input [output]\n",
141 >        "Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n",
142                          progname);
143 <        fprintf(stderr, "   Or: %s -r [-g gamma] [input [output]]\n",
143 >        fprintf(stderr, "   Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n",
144                          progname);
145          exit(1);
146   }
147  
148  
149 < quiterr(err)            /* print message and exit */
150 < char  *err;
149 > static void
150 > quiterr(                /* print message and exit */
151 >        char  *err
152 > )
153   {
154          if (err != NULL) {
155                  fprintf(stderr, "%s: %s\n", progname, err);
# Line 158 | Line 159 | char  *err;
159   }
160  
161  
162 + void
163   eputs(s)
164   char *s;
165   {
# Line 165 | Line 167 | char *s;
167   }
168  
169  
170 + void
171   quit(code)
172   int code;
173   {
# Line 172 | Line 175 | int code;
175   }
176  
177  
178 < pic *
179 < openinput(fname, h)             /* open RADIANCE input file */
180 < char  *fname;
181 < register struct rasterfile  *h;
178 > static pic *
179 > openinput(              /* open RADIANCE input file */
180 >        char  *fname,
181 >        register struct rasterfile  *h
182 > )
183   {
184          register pic  *p;
185  
# Line 185 | Line 189 | register struct rasterfile  *h;
189                  p->fp = stdin;
190          else if ((p->fp = fopen(fname, "r")) == NULL)
191                  return(NULL);
192 <                                        /* discard header */
193 <        getheader(p->fp, NULL);
194 <        if (fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR))
195 <                quiterr("bad picture size");
192 >                                        /* check header */
193 >        if (checkheader(p->fp, COLRFMT, NULL) < 0 ||
194 >                        fgetresolu(&xmax, &ymax, p->fp) < 0)
195 >                quiterr("bad picture format");
196          p->nexty = 0;
197          p->bytes_line = 0;              /* variable length lines */
198          p->pos.y = (long *)ecalloc(ymax, sizeof(long));
# Line 203 | Line 207 | register struct rasterfile  *h;
207          h->ras_maptype = RMT_EQUAL_RGB;
208          h->ras_maplength = 256*3;
209                                          /* allocate scanline */
210 <        inline = (COLR *)emalloc(xmax*sizeof(COLR));
210 >        inl = (COLR *)emalloc(xmax*sizeof(COLR));
211  
212          return(p);
213   }
214  
215  
216 < pic *
217 < openoutput(fname, h)            /* open output rasterfile */
218 < char  *fname;
219 < register struct rasterfile  *h;
216 > static pic *
217 > openoutput(             /* open output rasterfile */
218 >        char  *fname,
219 >        register struct rasterfile  *h
220 > )
221   {
222          register pic  *p;
223  
# Line 232 | Line 237 | register struct rasterfile  *h;
237   }
238  
239  
240 < pr2ra(h)                        /* pixrect file to RADIANCE file */
241 < struct rasterfile  *h;
240 > static void
241 > pr2ra(                  /* pixrect file to RADIANCE file */
242 >        struct rasterfile  *h
243 > )
244   {
245          BYTE  cmap[3][256];
246          COLR  ctab[256];
# Line 248 | Line 255 | struct rasterfile  *h;
255                                          /* convert table */
256          for (i = 0; i < h->ras_maplength/3; i++)
257                  setcolr(ctab[i],
258 <                                pow((cmap[0][i]+.5)/256.,gamma),
259 <                                pow((cmap[1][i]+.5)/256.,gamma),
260 <                                pow((cmap[2][i]+.5)/256.,gamma));
258 >                                pow((cmap[0][i]+.5)/256.,gamcor),
259 >                                pow((cmap[1][i]+.5)/256.,gamcor),
260 >                                pow((cmap[2][i]+.5)/256.,gamcor));
261 >        if (bradj)
262 >                shiftcolrs(ctab, 256, bradj);
263                                          /* convert file */
264          for (i = 0; i < ymax; i++) {
265                  for (j = 0; j < xmax; j++) {
# Line 263 | Line 272 | struct rasterfile  *h;
272                  if (fwritecolrs(scanline, xmax, stdout) < 0)
273                          quiterr("error writing RADIANCE file");
274          }
275 <        free((char *)scanline);
275 >        free((void *)scanline);
276   }
277  
278  
279 < picreadline3(y, l3)                     /* read in 3-byte scanline */
280 < int  y;
281 < register rgbpixel  *l3;
279 > extern void
280 > picreadline3(                   /* read in 3-byte scanline */
281 >        int  y,
282 >        register rgbpixel  *l3
283 > )
284   {
285          register int    i;
286  
# Line 277 | Line 288 | register rgbpixel  *l3;
288                  if (inpic->bytes_line == 0) {
289                          if (inpic->pos.y[y] == 0) {
290                                  while (inpic->nexty < y) {
291 <                                        if (freadcolrs(inline, xmax, inpic->fp) < 0)
291 >                                        if (freadcolrs(inl, xmax, inpic->fp) < 0)
292                                                  quiterr("read error in picreadline3");
293                                          inpic->pos.y[++inpic->nexty] = ftell(inpic->fp);
294                                  }
# Line 287 | Line 298 | register rgbpixel  *l3;
298                          quiterr("seek error in picreadline3");
299          } else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0)
300                  inpic->pos.y[inpic->nexty] = ftell(inpic->fp);
301 <        if (freadcolrs(inline, xmax, inpic->fp) < 0)    /* read scanline */
301 >        if (freadcolrs(inl, xmax, inpic->fp) < 0)       /* read scanline */
302                  quiterr("read error in picreadline3");
303          inpic->nexty = y+1;
304                                                          /* convert scanline */
305 <        normcolrs(inline, xmax, 0);
305 >        normcolrs(inl, xmax, bradj);
306          for (i = 0; i < xmax; i++) {
307 <                l3[i].r = inline[i][RED];
308 <                l3[i].g = inline[i][GRN];
309 <                l3[i].b = inline[i][BLU];
307 >                l3[i].r = inl[i][RED];
308 >                l3[i].g = inl[i][GRN];
309 >                l3[i].b = inl[i][BLU];
310          }
311   }
312  
313  
314 < picwriteline(y, l)                      /* write out scanline */
315 < int  y;
316 < register pixel  *l;
314 > extern void
315 > picwriteline(                   /* write out scanline */
316 >        int  y,
317 >        register pixel  *l
318 > )
319   {
320          if (outpic->nexty != y) {                       /* seek to scanline */
321                  if (outpic->bytes_line == 0) {
# Line 324 | Line 337 | register pixel  *l;
337   }
338  
339  
340 < picwritecm(cm)                  /* write out color map */
341 < colormap  cm;
340 > extern void
341 > picwritecm(                     /* write out color map */
342 >        colormap  cm
343 > )
344   {
345          register int  i, j;
346  
# Line 343 | Line 358 | colormap  cm;
358   }
359  
360  
361 < picreadcm(map)                  /* do gamma correction if requested */
362 < colormap  map;
361 > extern void
362 > picreadcm(                      /* do gamma correction if requested */
363 >        colormap  map
364 > )
365   {
366          register int  i, val;
367  
368          for (i = 0; i < 256; i++) {
369 <                val = pow(i/256.0, 1.0/gamma) * 256.0;
369 >                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
370                  map[0][i] = map[1][i] = map[2][i] = val;
371          }
372   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines