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 <stdio.h> |
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 */ |
17 |
– |
double inpaspect = 1.0; /* input aspect ratio */ |
20 |
|
|
21 |
+ |
int correctorder = 0; /* order correction? */ |
22 |
+ |
int ccw = 0; /* rotate CCW? */ |
23 |
+ |
|
24 |
+ |
#ifdef SMLMEM |
25 |
|
char buf[1<<20]; /* output buffer */ |
26 |
+ |
#else |
27 |
+ |
char buf[1<<22]; /* output buffer */ |
28 |
+ |
#endif |
29 |
|
|
30 |
|
int nrows; /* number of rows output at once */ |
31 |
|
|
33 |
|
|
34 |
|
char *progname; |
35 |
|
|
36 |
+ |
short ordertab[4][2] = { |
37 |
+ |
{0,XDECR}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,0} |
38 |
+ |
}; |
39 |
|
|
40 |
< |
headline(s) /* process line from header */ |
41 |
< |
char *s; |
40 |
> |
static int neworder(void); |
41 |
> |
static void rotatecw(FILE *fp); |
42 |
> |
static void rotateccw(FILE *fp); |
43 |
> |
|
44 |
> |
|
45 |
> |
|
46 |
> |
static int |
47 |
> |
neworder(void) /* return corrected order */ |
48 |
|
{ |
49 |
< |
fputs(s, stdout); |
50 |
< |
if (isaspect(s)) |
51 |
< |
inpaspect *= aspectval(s); |
49 |
> |
register int i; |
50 |
> |
|
51 |
> |
if (correctorder) |
52 |
> |
return(order); |
53 |
> |
for (i = 4; i--; ) |
54 |
> |
if ((order&~YMAJOR) == ordertab[i][ccw]) |
55 |
> |
return(ordertab[i][1-ccw] | ((order&YMAJOR)^YMAJOR)); |
56 |
> |
fputs("Order botch!\n", stderr); |
57 |
> |
exit(2); |
58 |
|
} |
59 |
|
|
60 |
< |
|
61 |
< |
main(argc, argv) |
62 |
< |
int argc; |
63 |
< |
char *argv[]; |
60 |
> |
int |
61 |
> |
main( |
62 |
> |
int argc, |
63 |
> |
char *argv[] |
64 |
> |
) |
65 |
|
{ |
66 |
+ |
static char picfmt[LPICFMT+1] = PICFMT; |
67 |
+ |
int rval; |
68 |
|
FILE *fin; |
69 |
|
|
70 |
|
progname = argv[0]; |
71 |
|
|
72 |
< |
if (argc != 2 && argc != 3) { |
73 |
< |
fprintf(stderr, "Usage: %s infile [outfile]\n", progname); |
74 |
< |
exit(1); |
72 |
> |
while (argc > 2 && argv[1][0] == '-') { |
73 |
> |
switch (argv[1][1]) { |
74 |
> |
case 'c': |
75 |
> |
correctorder = 1; |
76 |
> |
break; |
77 |
> |
case 'r': |
78 |
> |
ccw = 1; |
79 |
> |
break; |
80 |
> |
default: |
81 |
> |
goto userr; |
82 |
> |
} |
83 |
> |
argc--; argv++; |
84 |
|
} |
85 |
+ |
if (argc != 2 && argc != 3) |
86 |
+ |
goto userr; |
87 |
|
if ((fin = fopen(argv[1], "r")) == NULL) { |
88 |
|
fprintf(stderr, "%s: cannot open\n", argv[1]); |
89 |
|
exit(1); |
93 |
|
exit(1); |
94 |
|
} |
95 |
|
/* transfer header */ |
96 |
< |
getheader(fin, headline); |
96 |
> |
if ((rval = checkheader(fin, picfmt, stdout)) < 0) { |
97 |
> |
fprintf(stderr, "%s: not a Radiance picture\n", progname); |
98 |
> |
exit(1); |
99 |
> |
} |
100 |
> |
if (rval) |
101 |
> |
fputformat(picfmt, stdout); |
102 |
|
/* add new header info. */ |
103 |
< |
if (inpaspect < .99 || inpaspect > 1.01) |
104 |
< |
fputaspect(1./inpaspect/inpaspect, stdout); |
105 |
< |
printf("%s\n\n", progname); |
103 |
> |
fputs(progname, stdout); |
104 |
> |
if (ccw) fputs(" -r", stdout); |
105 |
> |
if (correctorder) fputs(" -c", stdout); |
106 |
> |
fputs("\n\n", stdout); |
107 |
|
/* get picture size */ |
108 |
< |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
108 |
> |
if ((order = fgetresolu(&xres, &yres, fin)) < 0) { |
109 |
|
fprintf(stderr, "%s: bad picture size\n", progname); |
110 |
|
exit(1); |
111 |
|
} |
112 |
|
/* write new picture size */ |
113 |
< |
fputresolu(YMAJOR|YDECR, yres, xres, stdout); |
113 |
> |
fputresolu(neworder(), yres, xres, stdout); |
114 |
|
/* compute buffer capacity */ |
115 |
|
nrows = sizeof(buf)/sizeof(COLR)/yres; |
116 |
< |
rotate(fin); /* rotate the image */ |
116 |
> |
if (ccw) /* rotate the image */ |
117 |
> |
rotateccw(fin); |
118 |
> |
else |
119 |
> |
rotatecw(fin); |
120 |
|
exit(0); |
121 |
+ |
userr: |
122 |
+ |
fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname); |
123 |
+ |
exit(1); |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
< |
rotate(fp) /* rotate picture */ |
128 |
< |
FILE *fp; |
127 |
> |
static void |
128 |
> |
rotatecw( /* rotate picture clockwise */ |
129 |
> |
FILE *fp |
130 |
> |
) |
131 |
|
{ |
132 |
|
register COLR *inln; |
133 |
|
register int xoff, inx, iny; |
149 |
|
exit(1); |
150 |
|
} |
151 |
|
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
152 |
< |
bcopy((char *)inln[xoff+inx], |
153 |
< |
(char *)scanbar[inx*yres+iny], |
102 |
< |
sizeof(COLR)); |
152 |
> |
copycolr(scanbar[inx*yres+iny], |
153 |
> |
inln[xoff+inx]); |
154 |
|
} |
155 |
|
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
156 |
|
if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { |
158 |
|
exit(1); |
159 |
|
} |
160 |
|
} |
161 |
< |
free((char *)inln); |
161 |
> |
free((void *)inln); |
162 |
> |
} |
163 |
> |
|
164 |
> |
|
165 |
> |
static void |
166 |
> |
rotateccw( /* rotate picture counter-clockwise */ |
167 |
> |
FILE *fp |
168 |
> |
) |
169 |
> |
{ |
170 |
> |
register COLR *inln; |
171 |
> |
register int xoff, inx, iny; |
172 |
> |
long start, ftell(); |
173 |
> |
|
174 |
> |
if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) { |
175 |
> |
fprintf(stderr, "%s: out of memory\n", progname); |
176 |
> |
exit(1); |
177 |
> |
} |
178 |
> |
start = ftell(fp); |
179 |
> |
for (xoff = xres-1; xoff >= 0; xoff -= nrows) { |
180 |
> |
if (fseek(fp, start, 0) < 0) { |
181 |
> |
fprintf(stderr, "%s: seek error\n", progname); |
182 |
> |
exit(1); |
183 |
> |
} |
184 |
> |
for (iny = 0; iny < yres; iny++) { |
185 |
> |
if (freadcolrs(inln, xres, fp) < 0) { |
186 |
> |
fprintf(stderr, "%s: read error\n", progname); |
187 |
> |
exit(1); |
188 |
> |
} |
189 |
> |
for (inx = 0; inx < nrows && xoff-inx >= 0; inx++) |
190 |
> |
copycolr(scanbar[inx*yres+iny], |
191 |
> |
inln[xoff-inx]); |
192 |
> |
} |
193 |
> |
for (inx = 0; inx < nrows && xoff-inx >= 0; inx++) |
194 |
> |
if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { |
195 |
> |
fprintf(stderr, "%s: write error\n", progname); |
196 |
> |
exit(1); |
197 |
> |
} |
198 |
> |
} |
199 |
> |
free((void *)inln); |
200 |
|
} |