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.2 by greg, Thu Oct 18 12:21:33 1990 UTC vs.
Revision 2.9 by greg, Sat Feb 22 02:07:28 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  
10 + #ifdef MSDOS
11 + #include  <fcntl.h>
12 + #endif
13 +
14 + #include  <time.h>
15 +
16 + #include  <math.h>
17 +
18   #include  "rasterfile.h"
19  
20   #include  "color.h"
21  
22 < extern double  atof(), pow();
22 > #include  "resolu.h"
23  
24 < double  gamma = 2.0;                    /* gamma correction */
24 > double  gamcor = 2.2;                   /* gamma correction */
25  
26 + int  bradj = 0;                         /* brightness adjustment */
27 +
28   char  *progname;
29  
30   int  xmax, ymax;
# Line 30 | Line 37 | char  *argv[];
37          struct rasterfile  head;
38          int  reverse = 0;
39          int  i;
40 <        
40 > #ifdef MSDOS
41 >        extern int  _fmode;
42 >        _fmode = O_BINARY;
43 >        setmode(fileno(stdin), O_BINARY);
44 >        setmode(fileno(stdout), O_BINARY);
45 > #endif
46          progname = argv[0];
47  
48 +        head.ras_type = RT_STANDARD;
49          for (i = 1; i < argc; i++)
50                  if (argv[i][0] == '-')
51                          switch (argv[i][1]) {
52                          case 'g':
53 <                                gamma = atof(argv[++i]);
53 >                                gamcor = atof(argv[++i]);
54                                  break;
55 +                        case 'e':
56 +                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
57 +                                        goto userr;
58 +                                bradj = atoi(argv[++i]);
59 +                                break;
60                          case 'r':
61 <                                reverse = !reverse;
61 >                                if (!strcmp(argv[i], "-rgb"))
62 >                                        head.ras_type = RT_FORMAT_RGB;
63 >                                else
64 >                                        reverse = 1;
65                                  break;
66                          default:
67                                  goto userr;
# Line 56 | Line 77 | char  *argv[];
77                  exit(1);
78          }
79          if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
80 <                fprintf(stderr, "can't open output \"%s\"\n",
80 >                fprintf(stderr, "%s: can't open output \"%s\"\n",
81                                  progname, argv[i+1]);
82                  exit(1);
83          }
84 +        setcolrgam(gamcor);
85          if (reverse) {
86                                          /* get header */
87                  if (fread((char *)&head, sizeof(head), 1, stdin) != 1)
# Line 68 | Line 90 | char  *argv[];
90                          quiterr("bad raster format");
91                  xmax = head.ras_width;
92                  ymax = head.ras_height;
93 <                if (head.ras_type != RT_STANDARD ||
94 <                                head.ras_maptype != RMT_NONE ||
95 <                                head.ras_depth != 24)
93 >                if ((head.ras_type != RT_STANDARD
94 >                                        && head.ras_type != RT_FORMAT_RGB)
95 >                                || head.ras_maptype != RMT_NONE
96 >                                || head.ras_depth != 24)
97                          quiterr("incompatible format");
98                                          /* put header */
99 +                newheader("RADIANCE", stdout);
100                  printargs(i, argv, stdout);
101 +                fputformat(COLRFMT, stdout);
102                  putchar('\n');
103 <                fputresolu(YMAJOR|YDECR, xmax, ymax, stdout);
103 >                fprtresolu(xmax, ymax, stdout);
104                                          /* convert file */
105 <                pr2ra();
105 >                pr2ra(head.ras_type, head.ras_length/ymax - xmax*3);
106          } else {
107 <                                        /* discard input header */
108 <                getheader(stdin, NULL);
109 <                                        /* get resolution */
110 <                if (fgetresolu(&xmax, &ymax, stdin) != (YMAJOR|YDECR))
86 <                        quiterr("bad picture size");
107 >                                        /* get header info. */
108 >                if (checkheader(stdin, COLRFMT, NULL) < 0 ||
109 >                                fgetresolu(&xmax, &ymax, stdin) < 0)
110 >                        quiterr("bad picture format");
111                                          /* write rasterfile header */
112                  head.ras_magic = RAS_MAGIC;
113 <                head.ras_width = xmax;
113 >                head.ras_width = xmax + (xmax&1);
114                  head.ras_height = ymax;
115                  head.ras_depth = 24;
116 <                head.ras_length = xmax*ymax*3;
93 <                head.ras_type = RT_STANDARD;
116 >                head.ras_length = head.ras_width*head.ras_height*3;
117                  head.ras_maptype = RMT_NONE;
118                  head.ras_maplength = 0;
119                  fwrite((char *)&head, sizeof(head), 1, stdout);
120                                          /* convert file */
121 <                ra2pr();
121 >                ra2pr(head.ras_type, head.ras_length/ymax - xmax*3);
122          }
123          exit(0);
124   userr:
125 <        fprintf(stderr, "Usage: %s [-r][-g gamma] [input [output]]\n",
125 >        fprintf(stderr, "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
126                          progname);
127          exit(1);
128   }
# Line 116 | Line 139 | char  *err;
139   }
140  
141  
142 < pr2ra()                 /* convert 24-bit scanlines to Radiance picture */
142 > pr2ra(rf, pad)          /* convert 24-bit scanlines to Radiance picture */
143 > int     rf;
144 > int     pad;
145   {
146 <        float   gmap[256];
122 <        int     r, g, b;
123 <        COLOR   *scanout;
146 >        COLR    *scanout;
147          register int    x;
148          int     y;
149                                                  /* allocate scanline */
150 <        scanout = (COLOR *)malloc(xmax*sizeof(COLOR));
150 >        scanout = (COLR *)malloc(xmax*sizeof(COLR));
151          if (scanout == NULL)
152                  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);
153                                                  /* convert image */
154          for (y = ymax-1; y >= 0; y--) {
155 <                for (x = 0; x < xmax; x++) {
156 <                        r = getc(stdin); g = getc(stdin);
157 <                        if ((b = getc(stdin)) == EOF)
158 <                                quiterr("error reading rasterfile");
159 <                        setcolor(scanout[x], gmap[r], gmap[g], gmap[b]);
160 <                }
161 <                if (fwritescan(scanout, xmax, stdout) < 0)
155 >                if (rf == RT_FORMAT_RGB)
156 >                        for (x = 0; x < xmax; x++) {
157 >                                scanout[x][RED] = getc(stdin);
158 >                                scanout[x][GRN] = getc(stdin);
159 >                                scanout[x][BLU] = getc(stdin);
160 >                        }
161 >                else
162 >                        for (x = 0; x < xmax; x++) {
163 >                                scanout[x][BLU] = getc(stdin);
164 >                                scanout[x][GRN] = getc(stdin);
165 >                                scanout[x][RED] = getc(stdin);
166 >                        }
167 >                for (x = pad; x--; getc(stdin));
168 >                if (feof(stdin) || ferror(stdin))
169 >                        quiterr("error reading rasterfile");
170 >                gambs_colrs(scanout, xmax);
171 >                if (bradj)
172 >                        shiftcolrs(scanout, xmax, bradj);
173 >                if (fwritecolrs(scanout, xmax, stdout) < 0)
174                          quiterr("error writing Radiance picture");
175          }
176                                                  /* free scanline */
177 <        free((char *)scanout);
177 >        free((void *)scanout);
178   }
179  
180  
181 < ra2pr()                 /* convert Radiance scanlines to 24-bit rasterfile */
181 > ra2pr(rf, pad)          /* convert Radiance scanlines to 24-bit rasterfile */
182 > int     rf;
183 > int     pad;
184   {
185 < #define map(v)  ((v)>=1.0 ? 255 : gmap[(int)(1024.*(v))])
186 <        unsigned char   gmap[1024];
153 <        COLOR   *scanin;
185 >        int     ord[3];
186 >        COLR    *scanin;
187          register int    x;
155        register int    c;
188          int     y;
189                                                  /* allocate scanline */
190 <        scanin = (COLOR *)malloc(xmax*sizeof(COLOR));
190 >        scanin = (COLR *)malloc(xmax*sizeof(COLR));
191          if (scanin == NULL)
192 <                quiterr("out of memory in pr2ra");
193 <                                                /* compute gamma correction */
194 <        for (x = 0; x < 1024; x++)
195 <                gmap[x] = 256.*pow((x+.5)/1024., 1./gamma);
192 >                quiterr("out of memory in ra2pr");
193 >        if (rf == RT_FORMAT_RGB) {
194 >                ord[0] = RED; ord[1] = GRN; ord[2] = BLU;
195 >        } else {
196 >                ord[0] = BLU; ord[1] = GRN; ord[2] = RED;
197 >        }
198                                                  /* convert image */
199          for (y = ymax-1; y >= 0; y--) {
200 <                if (freadscan(scanin, xmax, stdin) < 0)
200 >                if (freadcolrs(scanin, xmax, stdin) < 0)
201                          quiterr("error reading Radiance picture");
202 <                for (x = 0; x < xmax; x++) {
203 <                        c = map(colval(scanin[x],RED));
204 <                        putc(c, stdout);
205 <                        c = map(colval(scanin[x],GRN));
206 <                        putc(c, stdout);
207 <                        c = map(colval(scanin[x],BLU));
208 <                        putc(c, stdout);
209 <                }
202 >                if (bradj)
203 >                        shiftcolrs(scanin, xmax, bradj);
204 >                colrs_gambs(scanin, xmax);
205 >                if (rf == RT_FORMAT_RGB)
206 >                        for (x = 0; x < xmax; x++) {
207 >                                putc(scanin[x][RED], stdout);
208 >                                putc(scanin[x][GRN], stdout);
209 >                                putc(scanin[x][BLU], stdout);
210 >                        }
211 >                else
212 >                        for (x = 0; x < xmax; x++) {
213 >                                putc(scanin[x][BLU], stdout);
214 >                                putc(scanin[x][GRN], stdout);
215 >                                putc(scanin[x][RED], stdout);
216 >                        }
217 >                for (x = 0; x < pad; x++)
218 >                        putc(scanin[xmax-1][ord[x%3]], stdout);
219                  if (ferror(stdout))
220                          quiterr("error writing rasterfile");
221          }
222                                                  /* free scanline */
223 <        free((char *)scanin);
181 < #undef map
223 >        free((void *)scanin);
224   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines