ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/protate.c
Revision: 2.11
Committed: Wed Feb 8 04:18:13 2012 UTC (12 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad5R1, rad4R2, rad4R2P1
Changes since 2.10: +5 -1 lines
Log Message:
Fixed bug for Windows (forgot binary mode)

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.11 static const char RCSid[] = "$Id: protate.c,v 2.10 2004/04/30 20:29:24 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * prot.c - program to rotate picture file 90 degrees clockwise.
6     *
7     * 2/26/88
8     */
9    
10 greg 2.11 #include "platform.h"
11 greg 1.7 #include "standard.h"
12 greg 1.1
13     #include "color.h"
14    
15 greg 2.7 #include <time.h>
16    
17 greg 1.9 #include "resolu.h"
18    
19     int order; /* input scanline order */
20 greg 1.1 int xres, yres; /* input resolution */
21    
22 greg 1.9 int correctorder = 0; /* order correction? */
23 gwlarson 2.5 int ccw = 0; /* rotate CCW? */
24 greg 1.9
25 greg 2.8 #ifdef SMLMEM
26     char buf[1<<20]; /* output buffer */
27     #else
28 greg 2.2 char buf[1<<22]; /* output buffer */
29     #endif
30 greg 1.1
31     int nrows; /* number of rows output at once */
32    
33     #define scanbar ((COLR *)buf)
34    
35     char *progname;
36    
37 gwlarson 2.5 short ordertab[4][2] = {
38 greg 2.10 {0,0}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,XDECR}
39 gwlarson 2.5 };
40 greg 1.1
41 greg 2.10
42 schorsch 2.9 static int neworder(void);
43     static void rotatecw(FILE *fp);
44     static void rotateccw(FILE *fp);
45 greg 1.8
46 schorsch 2.9
47    
48     static int
49     neworder(void) /* return corrected order */
50 gwlarson 2.5 {
51 greg 2.10 static short ordercw[8];
52 gwlarson 2.5 register int i;
53    
54     if (correctorder)
55     return(order);
56 greg 2.10 if (!ordercw[0]) {
57     ordercw[YMAJOR|YDECR] = 0;
58     ordercw[0] = YMAJOR|XDECR;
59     ordercw[YMAJOR|XDECR] = XDECR|YDECR;
60     ordercw[XDECR|YDECR] = YMAJOR|YDECR;
61     ordercw[YMAJOR|XDECR|YDECR] = XDECR;
62     ordercw[XDECR] = YMAJOR;
63     ordercw[YMAJOR] = YDECR;
64     ordercw[YDECR] = YMAJOR|XDECR|YDECR;
65     }
66     if (!ccw)
67     return(ordercw[order]);
68     for (i = 8; i--; )
69     if (ordercw[i] == order)
70     return(i);
71 gwlarson 2.5 fputs("Order botch!\n", stderr);
72     exit(2);
73     }
74    
75 schorsch 2.9 int
76     main(
77     int argc,
78     char *argv[]
79     )
80 greg 1.1 {
81 greg 2.4 static char picfmt[LPICFMT+1] = PICFMT;
82     int rval;
83 greg 1.1 FILE *fin;
84    
85 greg 2.11 SET_DEFAULT_BINARY();
86     SET_FILE_BINARY(stdout);
87    
88 greg 1.1 progname = argv[0];
89    
90 gwlarson 2.5 while (argc > 2 && argv[1][0] == '-') {
91     switch (argv[1][1]) {
92     case 'c':
93     correctorder = 1;
94     break;
95     case 'r':
96     ccw = 1;
97     break;
98     default:
99     goto userr;
100     }
101 greg 1.9 argc--; argv++;
102     }
103 gwlarson 2.5 if (argc != 2 && argc != 3)
104     goto userr;
105 greg 1.1 if ((fin = fopen(argv[1], "r")) == NULL) {
106     fprintf(stderr, "%s: cannot open\n", argv[1]);
107     exit(1);
108     }
109     if (argc == 3 && freopen(argv[2], "w", stdout) == NULL) {
110     fprintf(stderr, "%s: cannot open\n", argv[2]);
111     exit(1);
112     }
113 greg 1.6 /* transfer header */
114 greg 2.4 if ((rval = checkheader(fin, picfmt, stdout)) < 0) {
115 greg 1.9 fprintf(stderr, "%s: not a Radiance picture\n", progname);
116 greg 1.8 exit(1);
117     }
118 greg 2.4 if (rval)
119     fputformat(picfmt, stdout);
120 greg 1.1 /* add new header info. */
121 gwlarson 2.5 fputs(progname, stdout);
122     if (ccw) fputs(" -r", stdout);
123     if (correctorder) fputs(" -c", stdout);
124     fputs("\n\n", stdout);
125 greg 1.1 /* get picture size */
126 greg 1.9 if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
127 greg 1.1 fprintf(stderr, "%s: bad picture size\n", progname);
128     exit(1);
129     }
130     /* write new picture size */
131 greg 1.9 fputresolu(neworder(), yres, xres, stdout);
132 greg 1.1 /* compute buffer capacity */
133     nrows = sizeof(buf)/sizeof(COLR)/yres;
134 gwlarson 2.5 if (ccw) /* rotate the image */
135     rotateccw(fin);
136     else
137     rotatecw(fin);
138 greg 1.1 exit(0);
139 gwlarson 2.5 userr:
140     fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname);
141     exit(1);
142 greg 1.1 }
143    
144    
145 schorsch 2.9 static void
146     rotatecw( /* rotate picture clockwise */
147     FILE *fp
148     )
149 greg 1.1 {
150 greg 1.5 register COLR *inln;
151 greg 1.1 register int xoff, inx, iny;
152     long start, ftell();
153    
154 greg 1.5 if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
155 greg 1.1 fprintf(stderr, "%s: out of memory\n", progname);
156     exit(1);
157     }
158     start = ftell(fp);
159     for (xoff = 0; xoff < xres; xoff += nrows) {
160     if (fseek(fp, start, 0) < 0) {
161     fprintf(stderr, "%s: seek error\n", progname);
162     exit(1);
163     }
164     for (iny = yres-1; iny >= 0; iny--) {
165 greg 1.5 if (freadcolrs(inln, xres, fp) < 0) {
166 greg 1.1 fprintf(stderr, "%s: read error\n", progname);
167     exit(1);
168     }
169     for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
170 gwlarson 2.6 copycolr(scanbar[inx*yres+iny],
171     inln[xoff+inx]);
172 greg 1.1 }
173     for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
174 gwlarson 2.5 if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
175     fprintf(stderr, "%s: write error\n", progname);
176     exit(1);
177     }
178     }
179 greg 2.7 free((void *)inln);
180 gwlarson 2.5 }
181    
182    
183 schorsch 2.9 static void
184     rotateccw( /* rotate picture counter-clockwise */
185     FILE *fp
186     )
187 gwlarson 2.5 {
188     register COLR *inln;
189     register int xoff, inx, iny;
190     long start, ftell();
191    
192     if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
193     fprintf(stderr, "%s: out of memory\n", progname);
194     exit(1);
195     }
196     start = ftell(fp);
197     for (xoff = xres-1; xoff >= 0; xoff -= nrows) {
198     if (fseek(fp, start, 0) < 0) {
199     fprintf(stderr, "%s: seek error\n", progname);
200     exit(1);
201     }
202     for (iny = 0; iny < yres; iny++) {
203     if (freadcolrs(inln, xres, fp) < 0) {
204     fprintf(stderr, "%s: read error\n", progname);
205     exit(1);
206     }
207     for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
208 gwlarson 2.6 copycolr(scanbar[inx*yres+iny],
209     inln[xoff-inx]);
210 gwlarson 2.5 }
211     for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
212 greg 1.1 if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
213     fprintf(stderr, "%s: write error\n", progname);
214     exit(1);
215     }
216     }
217 greg 2.7 free((void *)inln);
218 greg 1.1 }