1 |
– |
/* Copyright (c) 1988 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
|
/* |
5 |
|
* prot.c - program to rotate picture file 90 degrees clockwise. |
7 |
|
* 2/26/88 |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#include "platform.h" |
11 |
|
#include "standard.h" |
12 |
|
|
13 |
|
#include "color.h" |
14 |
|
|
15 |
+ |
#include <time.h> |
16 |
+ |
|
17 |
+ |
#include "resolu.h" |
18 |
+ |
|
19 |
+ |
int order; /* input scanline order */ |
20 |
|
int xres, yres; /* input resolution */ |
17 |
– |
double inpaspect = 1.0; /* input aspect ratio */ |
21 |
|
|
22 |
+ |
int correctorder = 0; /* order correction? */ |
23 |
+ |
int ccw = 0; /* rotate CCW? */ |
24 |
+ |
|
25 |
+ |
#ifdef SMLMEM |
26 |
|
char buf[1<<20]; /* output buffer */ |
27 |
+ |
#else |
28 |
+ |
char buf[1<<22]; /* output buffer */ |
29 |
+ |
#endif |
30 |
|
|
31 |
|
int nrows; /* number of rows output at once */ |
32 |
|
|
34 |
|
|
35 |
|
char *progname; |
36 |
|
|
37 |
+ |
short ordertab[4][2] = { |
38 |
+ |
{0,0}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,XDECR} |
39 |
+ |
}; |
40 |
|
|
41 |
< |
headline(s) /* process line from header */ |
42 |
< |
char *s; |
41 |
> |
|
42 |
> |
static int neworder(void); |
43 |
> |
static void rotatecw(FILE *fp); |
44 |
> |
static void rotateccw(FILE *fp); |
45 |
> |
|
46 |
> |
|
47 |
> |
|
48 |
> |
static int |
49 |
> |
neworder(void) /* return corrected order */ |
50 |
|
{ |
51 |
< |
fputs(s, stdout); |
52 |
< |
if (isaspect(s)) |
53 |
< |
inpaspect *= aspectval(s); |
51 |
> |
static short ordercw[8]; |
52 |
> |
register int i; |
53 |
> |
|
54 |
> |
if (correctorder) |
55 |
> |
return(order); |
56 |
> |
if (!ordercw[0]) { |
57 |
> |
ordercw[YMAJOR|YDECR] = 0; |
58 |
> |
ordercw[0] = YMAJOR|XDECR; |
59 |
> |
ordercw[YMAJOR|XDECR] = XDECR|YDECR; |
60 |
> |
ordercw[XDECR|YDECR] = YMAJOR|YDECR; |
61 |
> |
ordercw[YMAJOR|XDECR|YDECR] = XDECR; |
62 |
> |
ordercw[XDECR] = YMAJOR; |
63 |
> |
ordercw[YMAJOR] = YDECR; |
64 |
> |
ordercw[YDECR] = YMAJOR|XDECR|YDECR; |
65 |
> |
} |
66 |
> |
if (!ccw) |
67 |
> |
return(ordercw[order]); |
68 |
> |
for (i = 8; i--; ) |
69 |
> |
if (ordercw[i] == order) |
70 |
> |
return(i); |
71 |
> |
fputs("Order botch!\n", stderr); |
72 |
> |
exit(2); |
73 |
|
} |
74 |
|
|
75 |
< |
|
76 |
< |
main(argc, argv) |
77 |
< |
int argc; |
78 |
< |
char *argv[]; |
75 |
> |
int |
76 |
> |
main( |
77 |
> |
int argc, |
78 |
> |
char *argv[] |
79 |
> |
) |
80 |
|
{ |
81 |
+ |
static char picfmt[MAXFMTLEN] = PICFMT; |
82 |
+ |
int rval; |
83 |
|
FILE *fin; |
84 |
|
|
85 |
+ |
SET_DEFAULT_BINARY(); |
86 |
+ |
SET_FILE_BINARY(stdout); |
87 |
+ |
|
88 |
|
progname = argv[0]; |
89 |
|
|
90 |
< |
if (argc != 2 && argc != 3) { |
91 |
< |
fprintf(stderr, "Usage: %s infile [outfile]\n", progname); |
92 |
< |
exit(1); |
90 |
> |
while (argc > 2 && argv[1][0] == '-') { |
91 |
> |
switch (argv[1][1]) { |
92 |
> |
case 'c': |
93 |
> |
correctorder = 1; |
94 |
> |
break; |
95 |
> |
case 'r': |
96 |
> |
ccw = 1; |
97 |
> |
break; |
98 |
> |
default: |
99 |
> |
goto userr; |
100 |
> |
} |
101 |
> |
argc--; argv++; |
102 |
|
} |
103 |
+ |
if (argc != 2 && argc != 3) |
104 |
+ |
goto userr; |
105 |
|
if ((fin = fopen(argv[1], "r")) == NULL) { |
106 |
|
fprintf(stderr, "%s: cannot open\n", argv[1]); |
107 |
|
exit(1); |
111 |
|
exit(1); |
112 |
|
} |
113 |
|
/* transfer header */ |
114 |
< |
getheader(fin, headline); |
114 |
> |
if ((rval = checkheader(fin, picfmt, stdout)) < 0) { |
115 |
> |
fprintf(stderr, "%s: not a Radiance picture\n", progname); |
116 |
> |
exit(1); |
117 |
> |
} |
118 |
> |
if (rval) |
119 |
> |
fputformat(picfmt, stdout); |
120 |
|
/* add new header info. */ |
121 |
< |
if (inpaspect < .99 || inpaspect > 1.01) |
122 |
< |
fputaspect(1./inpaspect/inpaspect, stdout); |
123 |
< |
printf("%s\n\n", progname); |
121 |
> |
fputs(progname, stdout); |
122 |
> |
if (ccw) fputs(" -r", stdout); |
123 |
> |
if (correctorder) fputs(" -c", stdout); |
124 |
> |
fputs("\n\n", stdout); |
125 |
|
/* get picture size */ |
126 |
< |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
126 |
> |
if ((order = fgetresolu(&xres, &yres, fin)) < 0) { |
127 |
|
fprintf(stderr, "%s: bad picture size\n", progname); |
128 |
|
exit(1); |
129 |
|
} |
130 |
|
/* write new picture size */ |
131 |
< |
fputresolu(YMAJOR|YDECR, yres, xres, stdout); |
131 |
> |
fputresolu(neworder(), yres, xres, stdout); |
132 |
|
/* compute buffer capacity */ |
133 |
|
nrows = sizeof(buf)/sizeof(COLR)/yres; |
134 |
< |
rotate(fin); /* rotate the image */ |
134 |
> |
if (ccw) /* rotate the image */ |
135 |
> |
rotateccw(fin); |
136 |
> |
else |
137 |
> |
rotatecw(fin); |
138 |
|
exit(0); |
139 |
+ |
userr: |
140 |
+ |
fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname); |
141 |
+ |
exit(1); |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
< |
rotate(fp) /* rotate picture */ |
146 |
< |
FILE *fp; |
145 |
> |
static void |
146 |
> |
rotatecw( /* rotate picture clockwise */ |
147 |
> |
FILE *fp |
148 |
> |
) |
149 |
|
{ |
150 |
|
register COLR *inln; |
151 |
|
register int xoff, inx, iny; |
167 |
|
exit(1); |
168 |
|
} |
169 |
|
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
170 |
< |
bcopy((char *)inln[xoff+inx], |
171 |
< |
(char *)scanbar[inx*yres+iny], |
102 |
< |
sizeof(COLR)); |
170 |
> |
copycolr(scanbar[inx*yres+iny], |
171 |
> |
inln[xoff+inx]); |
172 |
|
} |
173 |
|
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
174 |
|
if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { |
176 |
|
exit(1); |
177 |
|
} |
178 |
|
} |
179 |
< |
free((char *)inln); |
179 |
> |
free((void *)inln); |
180 |
> |
} |
181 |
> |
|
182 |
> |
|
183 |
> |
static void |
184 |
> |
rotateccw( /* rotate picture counter-clockwise */ |
185 |
> |
FILE *fp |
186 |
> |
) |
187 |
> |
{ |
188 |
> |
register COLR *inln; |
189 |
> |
register int xoff, inx, iny; |
190 |
> |
long start, ftell(); |
191 |
> |
|
192 |
> |
if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) { |
193 |
> |
fprintf(stderr, "%s: out of memory\n", progname); |
194 |
> |
exit(1); |
195 |
> |
} |
196 |
> |
start = ftell(fp); |
197 |
> |
for (xoff = xres-1; xoff >= 0; xoff -= nrows) { |
198 |
> |
if (fseek(fp, start, 0) < 0) { |
199 |
> |
fprintf(stderr, "%s: seek error\n", progname); |
200 |
> |
exit(1); |
201 |
> |
} |
202 |
> |
for (iny = 0; iny < yres; iny++) { |
203 |
> |
if (freadcolrs(inln, xres, fp) < 0) { |
204 |
> |
fprintf(stderr, "%s: read error\n", progname); |
205 |
> |
exit(1); |
206 |
> |
} |
207 |
> |
for (inx = 0; inx < nrows && xoff-inx >= 0; inx++) |
208 |
> |
copycolr(scanbar[inx*yres+iny], |
209 |
> |
inln[xoff-inx]); |
210 |
> |
} |
211 |
> |
for (inx = 0; inx < nrows && xoff-inx >= 0; inx++) |
212 |
> |
if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { |
213 |
> |
fprintf(stderr, "%s: write error\n", progname); |
214 |
> |
exit(1); |
215 |
> |
} |
216 |
> |
} |
217 |
> |
free((void *)inln); |
218 |
|
} |