ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/protate.c
Revision: 2.7
Committed: Sat Feb 22 02:07:27 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.6: +5 -6 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

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