ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/protate.c
Revision: 2.9
Committed: Sun Mar 28 20:33:14 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.8: +21 -11 lines
Log Message:
Continued ANSIfication, and other fixes and clarifications.

File Contents

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