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 (20 years 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

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