ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/protate.c
Revision: 2.5
Committed: Tue Oct 27 12:34:39 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.4: +81 -11 lines
Log Message:
added -r option to rotate CCW and fixed bug in pixel order reporting

File Contents

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