ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/protate.c
Revision: 2.10
Committed: Fri Apr 30 20:29:24 2004 UTC (19 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad4R1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9
Changes since 2.9: +19 -5 lines
Log Message:
Fixed bug in pixel ordering out of protate

File Contents

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