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, 1 month 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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: protate.c,v 2.10 2004/04/30 20:29:24 greg Exp $";
3 #endif
4 /*
5 * prot.c - program to rotate picture file 90 degrees clockwise.
6 *
7 * 2/26/88
8 */
9
10 #include "platform.h"
11 #include "standard.h"
12
13 #include "color.h"
14
15 #include <time.h>
16
17 #include "resolu.h"
18
19 int order; /* input scanline order */
20 int xres, yres; /* input resolution */
21
22 int correctorder = 0; /* order correction? */
23 int ccw = 0; /* rotate CCW? */
24
25 #ifdef SMLMEM
26 char buf[1<<20]; /* output buffer */
27 #else
28 char buf[1<<22]; /* output buffer */
29 #endif
30
31 int nrows; /* number of rows output at once */
32
33 #define scanbar ((COLR *)buf)
34
35 char *progname;
36
37 short ordertab[4][2] = {
38 {0,0}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,XDECR}
39 };
40
41
42 static int neworder(void);
43 static void rotatecw(FILE *fp);
44 static void rotateccw(FILE *fp);
45
46
47
48 static int
49 neworder(void) /* return corrected order */
50 {
51 static short ordercw[8];
52 register int i;
53
54 if (correctorder)
55 return(order);
56 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 fputs("Order botch!\n", stderr);
72 exit(2);
73 }
74
75 int
76 main(
77 int argc,
78 char *argv[]
79 )
80 {
81 static char picfmt[LPICFMT+1] = PICFMT;
82 int rval;
83 FILE *fin;
84
85 SET_DEFAULT_BINARY();
86 SET_FILE_BINARY(stdout);
87
88 progname = argv[0];
89
90 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 argc--; argv++;
102 }
103 if (argc != 2 && argc != 3)
104 goto userr;
105 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 /* transfer header */
114 if ((rval = checkheader(fin, picfmt, stdout)) < 0) {
115 fprintf(stderr, "%s: not a Radiance picture\n", progname);
116 exit(1);
117 }
118 if (rval)
119 fputformat(picfmt, stdout);
120 /* add new header info. */
121 fputs(progname, stdout);
122 if (ccw) fputs(" -r", stdout);
123 if (correctorder) fputs(" -c", stdout);
124 fputs("\n\n", stdout);
125 /* get picture size */
126 if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
127 fprintf(stderr, "%s: bad picture size\n", progname);
128 exit(1);
129 }
130 /* write new picture size */
131 fputresolu(neworder(), yres, xres, stdout);
132 /* compute buffer capacity */
133 nrows = sizeof(buf)/sizeof(COLR)/yres;
134 if (ccw) /* rotate the image */
135 rotateccw(fin);
136 else
137 rotatecw(fin);
138 exit(0);
139 userr:
140 fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname);
141 exit(1);
142 }
143
144
145 static void
146 rotatecw( /* rotate picture clockwise */
147 FILE *fp
148 )
149 {
150 register COLR *inln;
151 register int xoff, inx, iny;
152 long start, ftell();
153
154 if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
155 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 if (freadcolrs(inln, xres, fp) < 0) {
166 fprintf(stderr, "%s: read error\n", progname);
167 exit(1);
168 }
169 for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
170 copycolr(scanbar[inx*yres+iny],
171 inln[xoff+inx]);
172 }
173 for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
174 if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
175 fprintf(stderr, "%s: write error\n", progname);
176 exit(1);
177 }
178 }
179 free((void *)inln);
180 }
181
182
183 static void
184 rotateccw( /* rotate picture counter-clockwise */
185 FILE *fp
186 )
187 {
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 copycolr(scanbar[inx*yres+iny],
209 inln[xoff-inx]);
210 }
211 for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
212 if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
213 fprintf(stderr, "%s: write error\n", progname);
214 exit(1);
215 }
216 }
217 free((void *)inln);
218 }