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

Comparing ray/src/px/ra_pr24.c (file contents):
Revision 1.1 by greg, Thu Oct 18 12:00:48 1990 UTC vs.
Revision 2.10 by schorsch, Thu Jun 5 19:29:34 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1990 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   *  program to convert between RADIANCE and 24-bit rasterfiles.
6   */
7  
8   #include  <stdio.h>
9 + #include  <time.h>
10 + #include  <math.h>
11  
12 + #include  "platform.h"
13   #include  "rasterfile.h"
14
14   #include  "color.h"
15 + #include  "resolu.h"
16  
17 < extern double  atof(), pow();
17 > double  gamcor = 2.2;                   /* gamma correction */
18  
19 < double  gamma = 2.0;                    /* gamma correction */
19 > int  bradj = 0;                         /* brightness adjustment */
20  
21   char  *progname;
22  
# Line 30 | Line 30 | char  *argv[];
30          struct rasterfile  head;
31          int  reverse = 0;
32          int  i;
33 <        
33 >        SET_DEFAULT_BINARY();
34 >        SET_FILE_BINARY(stdin);
35 >        SET_FILE_BINARY(stdout);
36          progname = argv[0];
37  
38 +        head.ras_type = RT_STANDARD;
39          for (i = 1; i < argc; i++)
40                  if (argv[i][0] == '-')
41                          switch (argv[i][1]) {
42                          case 'g':
43 <                                gamma = atof(argv[++i]);
43 >                                gamcor = atof(argv[++i]);
44                                  break;
45 +                        case 'e':
46 +                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
47 +                                        goto userr;
48 +                                bradj = atoi(argv[++i]);
49 +                                break;
50                          case 'r':
51 <                                reverse = !reverse;
51 >                                if (!strcmp(argv[i], "-rgb"))
52 >                                        head.ras_type = RT_FORMAT_RGB;
53 >                                else
54 >                                        reverse = 1;
55                                  break;
56                          default:
57                                  goto userr;
# Line 56 | Line 67 | char  *argv[];
67                  exit(1);
68          }
69          if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
70 <                fprintf(stderr, "can't open output \"%s\"\n",
70 >                fprintf(stderr, "%s: can't open output \"%s\"\n",
71                                  progname, argv[i+1]);
72                  exit(1);
73          }
74 +        setcolrgam(gamcor);
75          if (reverse) {
76                                          /* get header */
77                  if (fread((char *)&head, sizeof(head), 1, stdin) != 1)
# Line 68 | Line 80 | char  *argv[];
80                          quiterr("bad raster format");
81                  xmax = head.ras_width;
82                  ymax = head.ras_height;
83 <                if (head.ras_type != RT_STANDARD ||
84 <                                head.ras_maptype != RMT_NONE ||
85 <                                head.ras_depth != 24)
83 >                if ((head.ras_type != RT_STANDARD
84 >                                        && head.ras_type != RT_FORMAT_RGB)
85 >                                || head.ras_maptype != RMT_NONE
86 >                                || head.ras_depth != 24)
87                          quiterr("incompatible format");
88                                          /* put header */
89 +                newheader("RADIANCE", stdout);
90                  printargs(i, argv, stdout);
91 +                fputformat(COLRFMT, stdout);
92                  putchar('\n');
93 <                fputresolu(YMAJOR|YDECR, xmax, ymax, stdout);
93 >                fprtresolu(xmax, ymax, stdout);
94                                          /* convert file */
95 <                pr2ra();
95 >                pr2ra(head.ras_type, head.ras_length/ymax - xmax*3);
96          } else {
97 <                                        /* discard input header */
98 <                getheader(stdin, NULL);
99 <                                        /* get resolution */
100 <                if (fgetresolu(&xmax, &ymax, stdin) != (YMAJOR|YDECR))
86 <                        quiterr("bad picture size");
97 >                                        /* get header info. */
98 >                if (checkheader(stdin, COLRFMT, NULL) < 0 ||
99 >                                fgetresolu(&xmax, &ymax, stdin) < 0)
100 >                        quiterr("bad picture format");
101                                          /* write rasterfile header */
102                  head.ras_magic = RAS_MAGIC;
103 <                head.ras_width = xmax;
103 >                head.ras_width = xmax + (xmax&1);
104                  head.ras_height = ymax;
105                  head.ras_depth = 24;
106 <                head.ras_length = xmax*ymax*3;
93 <                head.ras_type = RT_STANDARD;
106 >                head.ras_length = head.ras_width*head.ras_height*3;
107                  head.ras_maptype = RMT_NONE;
108                  head.ras_maplength = 0;
109                  fwrite((char *)&head, sizeof(head), 1, stdout);
110                                          /* convert file */
111 <                ra2pr();
111 >                ra2pr(head.ras_type, head.ras_length/ymax - xmax*3);
112          }
113          exit(0);
114   userr:
115 <        fprintf(stderr, "Usage: %s [-r][-g gamma] [input [output]]\n",
115 >        fprintf(stderr, "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
116                          progname);
117          exit(1);
118   }
# Line 116 | Line 129 | char  *err;
129   }
130  
131  
132 < pr2ra()                 /* convert 24-bit scanlines to Radiance picture */
132 > pr2ra(rf, pad)          /* convert 24-bit scanlines to Radiance picture */
133 > int     rf;
134 > int     pad;
135   {
136 <        float   gmap[256];
122 <        int     r, g, b;
123 <        COLOR   *scanout;
136 >        COLR    *scanout;
137          register int    x;
138          int     y;
139                                                  /* allocate scanline */
140 <        scanout = (COLOR *)malloc(xmax*sizeof(COLOR));
140 >        scanout = (COLR *)malloc(xmax*sizeof(COLR));
141          if (scanout == NULL)
142                  quiterr("out of memory in pr2ra");
130                                                /* compute gamma correction */
131        for (x = 0; x < 256; x++)
132                gmap[x] = pow((x+.5)/256., gamma);
143                                                  /* convert image */
144          for (y = ymax-1; y >= 0; y--) {
145 <                for (x = 0; x < xmax; x++) {
146 <                        r = getc(stdin); g = getc(stdin);
147 <                        if ((b = getc(stdin)) == EOF)
148 <                                quiterr("error reading rasterfile");
149 <                        setcolor(scanout[x], gmap[r], gmap[g], gmap[b]);
150 <                }
151 <                if (fwritescan(scanout, xmax, stdout) < 0)
145 >                if (rf == RT_FORMAT_RGB)
146 >                        for (x = 0; x < xmax; x++) {
147 >                                scanout[x][RED] = getc(stdin);
148 >                                scanout[x][GRN] = getc(stdin);
149 >                                scanout[x][BLU] = getc(stdin);
150 >                        }
151 >                else
152 >                        for (x = 0; x < xmax; x++) {
153 >                                scanout[x][BLU] = getc(stdin);
154 >                                scanout[x][GRN] = getc(stdin);
155 >                                scanout[x][RED] = getc(stdin);
156 >                        }
157 >                for (x = pad; x--; getc(stdin));
158 >                if (feof(stdin) || ferror(stdin))
159 >                        quiterr("error reading rasterfile");
160 >                gambs_colrs(scanout, xmax);
161 >                if (bradj)
162 >                        shiftcolrs(scanout, xmax, bradj);
163 >                if (fwritecolrs(scanout, xmax, stdout) < 0)
164                          quiterr("error writing Radiance picture");
165          }
166                                                  /* free scanline */
167 <        free((char *)scanout);
167 >        free((void *)scanout);
168   }
169  
170  
171 < ra2pr()                 /* convert Radiance scanlines to 24-bit rasterfile */
171 > ra2pr(rf, pad)          /* convert Radiance scanlines to 24-bit rasterfile */
172 > int     rf;
173 > int     pad;
174   {
175 < #define map(v)  ((v)>=1.0 ? 255 : gmap[(int)(1024.*(v))])
176 <        unsigned char   gmap[1024];
153 <        COLOR   *scanin;
175 >        int     ord[3];
176 >        COLR    *scanin;
177          register int    x;
155        register int    c;
178          int     y;
179                                                  /* allocate scanline */
180 <        scanin = (COLOR *)malloc(xmax*sizeof(COLOR));
180 >        scanin = (COLR *)malloc(xmax*sizeof(COLR));
181          if (scanin == NULL)
182 <                quiterr("out of memory in pr2ra");
183 <                                                /* compute gamma correction */
184 <        for (x = 0; x < 256; x++)
185 <                gmap[x] = 256.*pow((x+.5)/1024., 1./gamma);
182 >                quiterr("out of memory in ra2pr");
183 >        if (rf == RT_FORMAT_RGB) {
184 >                ord[0] = RED; ord[1] = GRN; ord[2] = BLU;
185 >        } else {
186 >                ord[0] = BLU; ord[1] = GRN; ord[2] = RED;
187 >        }
188                                                  /* convert image */
189          for (y = ymax-1; y >= 0; y--) {
190 <                if (freadscan(scanin, xmax, stdin) < 0)
190 >                if (freadcolrs(scanin, xmax, stdin) < 0)
191                          quiterr("error reading Radiance picture");
192 <                for (x = 0; x < xmax; x++) {
193 <                        c = map(colval(scanin[x],RED));
194 <                        putc(c, stdout);
195 <                        c = map(colval(scanin[x],GRN));
196 <                        putc(c, stdout);
197 <                        c = map(colval(scanin[x],BLU));
198 <                        putc(c, stdout);
199 <                }
192 >                if (bradj)
193 >                        shiftcolrs(scanin, xmax, bradj);
194 >                colrs_gambs(scanin, xmax);
195 >                if (rf == RT_FORMAT_RGB)
196 >                        for (x = 0; x < xmax; x++) {
197 >                                putc(scanin[x][RED], stdout);
198 >                                putc(scanin[x][GRN], stdout);
199 >                                putc(scanin[x][BLU], stdout);
200 >                        }
201 >                else
202 >                        for (x = 0; x < xmax; x++) {
203 >                                putc(scanin[x][BLU], stdout);
204 >                                putc(scanin[x][GRN], stdout);
205 >                                putc(scanin[x][RED], stdout);
206 >                        }
207 >                for (x = 0; x < pad; x++)
208 >                        putc(scanin[xmax-1][ord[x%3]], stdout);
209                  if (ferror(stdout))
210                          quiterr("error writing rasterfile");
211          }
212                                                  /* free scanline */
213 <        free((char *)scanin);
181 < #undef map
213 >        free((void *)scanin);
214   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines