ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/protate.c
Revision: 2.6
Committed: Tue Oct 27 16:32:33 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.5: +4 -6 lines
Log Message:
replaced bcopy() with copycolr()

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 gwlarson 2.6 copycolr(scanbar[inx*yres+iny],
146     inln[xoff+inx]);
147 greg 1.1 }
148     for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
149 gwlarson 2.5 if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
150     fprintf(stderr, "%s: write error\n", progname);
151     exit(1);
152     }
153     }
154     free((char *)inln);
155     }
156    
157    
158     rotateccw(fp) /* rotate picture counter-clockwise */
159     FILE *fp;
160     {
161     register COLR *inln;
162     register int xoff, inx, iny;
163     long start, ftell();
164    
165     if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
166     fprintf(stderr, "%s: out of memory\n", progname);
167     exit(1);
168     }
169     start = ftell(fp);
170     for (xoff = xres-1; xoff >= 0; xoff -= nrows) {
171     if (fseek(fp, start, 0) < 0) {
172     fprintf(stderr, "%s: seek error\n", progname);
173     exit(1);
174     }
175     for (iny = 0; iny < yres; iny++) {
176     if (freadcolrs(inln, xres, fp) < 0) {
177     fprintf(stderr, "%s: read error\n", progname);
178     exit(1);
179     }
180     for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
181 gwlarson 2.6 copycolr(scanbar[inx*yres+iny],
182     inln[xoff-inx]);
183 gwlarson 2.5 }
184     for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
185 greg 1.1 if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
186     fprintf(stderr, "%s: write error\n", progname);
187     exit(1);
188     }
189     }
190 greg 1.5 free((char *)inln);
191 greg 1.1 }