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.8 by greg, Sun Feb 27 10:17:15 1994 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines