1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
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 BIGMEM |
25 |
char buf[1<<22]; /* output buffer */ |
26 |
#else |
27 |
char buf[1<<20]; /* 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,XDECR}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,0} |
38 |
}; |
39 |
|
40 |
|
41 |
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 |
main(argc, argv) |
57 |
int argc; |
58 |
char *argv[]; |
59 |
{ |
60 |
static char picfmt[LPICFMT+1] = PICFMT; |
61 |
int rval; |
62 |
FILE *fin; |
63 |
|
64 |
progname = argv[0]; |
65 |
|
66 |
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 |
argc--; argv++; |
78 |
} |
79 |
if (argc != 2 && argc != 3) |
80 |
goto userr; |
81 |
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 |
/* transfer header */ |
90 |
if ((rval = checkheader(fin, picfmt, stdout)) < 0) { |
91 |
fprintf(stderr, "%s: not a Radiance picture\n", progname); |
92 |
exit(1); |
93 |
} |
94 |
if (rval) |
95 |
fputformat(picfmt, stdout); |
96 |
/* add new header info. */ |
97 |
fputs(progname, stdout); |
98 |
if (ccw) fputs(" -r", stdout); |
99 |
if (correctorder) fputs(" -c", stdout); |
100 |
fputs("\n\n", stdout); |
101 |
/* get picture size */ |
102 |
if ((order = fgetresolu(&xres, &yres, fin)) < 0) { |
103 |
fprintf(stderr, "%s: bad picture size\n", progname); |
104 |
exit(1); |
105 |
} |
106 |
/* write new picture size */ |
107 |
fputresolu(neworder(), yres, xres, stdout); |
108 |
/* compute buffer capacity */ |
109 |
nrows = sizeof(buf)/sizeof(COLR)/yres; |
110 |
if (ccw) /* rotate the image */ |
111 |
rotateccw(fin); |
112 |
else |
113 |
rotatecw(fin); |
114 |
exit(0); |
115 |
userr: |
116 |
fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname); |
117 |
exit(1); |
118 |
} |
119 |
|
120 |
|
121 |
rotatecw(fp) /* rotate picture clockwise */ |
122 |
FILE *fp; |
123 |
{ |
124 |
register COLR *inln; |
125 |
register int xoff, inx, iny; |
126 |
long start, ftell(); |
127 |
|
128 |
if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) { |
129 |
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 |
if (freadcolrs(inln, xres, fp) < 0) { |
140 |
fprintf(stderr, "%s: read error\n", progname); |
141 |
exit(1); |
142 |
} |
143 |
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
144 |
copycolr(scanbar[inx*yres+iny], |
145 |
inln[xoff+inx]); |
146 |
} |
147 |
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
148 |
if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { |
149 |
fprintf(stderr, "%s: write error\n", progname); |
150 |
exit(1); |
151 |
} |
152 |
} |
153 |
free((void *)inln); |
154 |
} |
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 |
copycolr(scanbar[inx*yres+iny], |
181 |
inln[xoff-inx]); |
182 |
} |
183 |
for (inx = 0; inx < nrows && xoff-inx >= 0; inx++) |
184 |
if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { |
185 |
fprintf(stderr, "%s: write error\n", progname); |
186 |
exit(1); |
187 |
} |
188 |
} |
189 |
free((void *)inln); |
190 |
} |