1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* prot.c - program to rotate picture file 90 degrees clockwise. |
9 |
* |
10 |
* 2/26/88 |
11 |
*/ |
12 |
|
13 |
#include "standard.h" |
14 |
|
15 |
#include "color.h" |
16 |
|
17 |
#include "resolu.h" |
18 |
|
19 |
int order; /* input scanline order */ |
20 |
int xres, yres; /* input resolution */ |
21 |
|
22 |
int correctorder = 0; /* order correction? */ |
23 |
int ccw = 0; /* rotate CCW? */ |
24 |
|
25 |
#ifdef BIGMEM |
26 |
char buf[1<<22]; /* output buffer */ |
27 |
#else |
28 |
char buf[1<<20]; /* output buffer */ |
29 |
#endif |
30 |
|
31 |
int nrows; /* number of rows output at once */ |
32 |
|
33 |
#define scanbar ((COLR *)buf) |
34 |
|
35 |
char *progname; |
36 |
|
37 |
short ordertab[4][2] = { |
38 |
{0,XDECR}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,0} |
39 |
}; |
40 |
|
41 |
|
42 |
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 |
main(argc, argv) |
58 |
int argc; |
59 |
char *argv[]; |
60 |
{ |
61 |
static char picfmt[LPICFMT+1] = PICFMT; |
62 |
int rval; |
63 |
FILE *fin; |
64 |
|
65 |
progname = argv[0]; |
66 |
|
67 |
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 |
argc--; argv++; |
79 |
} |
80 |
if (argc != 2 && argc != 3) |
81 |
goto userr; |
82 |
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 |
/* transfer header */ |
91 |
if ((rval = checkheader(fin, picfmt, stdout)) < 0) { |
92 |
fprintf(stderr, "%s: not a Radiance picture\n", progname); |
93 |
exit(1); |
94 |
} |
95 |
if (rval) |
96 |
fputformat(picfmt, stdout); |
97 |
/* add new header info. */ |
98 |
fputs(progname, stdout); |
99 |
if (ccw) fputs(" -r", stdout); |
100 |
if (correctorder) fputs(" -c", stdout); |
101 |
fputs("\n\n", stdout); |
102 |
/* get picture size */ |
103 |
if ((order = fgetresolu(&xres, &yres, fin)) < 0) { |
104 |
fprintf(stderr, "%s: bad picture size\n", progname); |
105 |
exit(1); |
106 |
} |
107 |
/* write new picture size */ |
108 |
fputresolu(neworder(), yres, xres, stdout); |
109 |
/* compute buffer capacity */ |
110 |
nrows = sizeof(buf)/sizeof(COLR)/yres; |
111 |
if (ccw) /* rotate the image */ |
112 |
rotateccw(fin); |
113 |
else |
114 |
rotatecw(fin); |
115 |
exit(0); |
116 |
userr: |
117 |
fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname); |
118 |
exit(1); |
119 |
} |
120 |
|
121 |
|
122 |
rotatecw(fp) /* rotate picture clockwise */ |
123 |
FILE *fp; |
124 |
{ |
125 |
register COLR *inln; |
126 |
register int xoff, inx, iny; |
127 |
long start, ftell(); |
128 |
|
129 |
if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) { |
130 |
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 |
if (freadcolrs(inln, xres, fp) < 0) { |
141 |
fprintf(stderr, "%s: read error\n", progname); |
142 |
exit(1); |
143 |
} |
144 |
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
145 |
copycolr(scanbar[inx*yres+iny], |
146 |
inln[xoff+inx]); |
147 |
} |
148 |
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
149 |
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 |
copycolr(scanbar[inx*yres+iny], |
182 |
inln[xoff-inx]); |
183 |
} |
184 |
for (inx = 0; inx < nrows && xoff-inx >= 0; inx++) |
185 |
if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { |
186 |
fprintf(stderr, "%s: write error\n", progname); |
187 |
exit(1); |
188 |
} |
189 |
} |
190 |
free((char *)inln); |
191 |
} |