1 |
greg |
1.1 |
#ifndef lint |
2 |
schorsch |
2.7 |
static const char RCSid[] = "$Id: pflip.c,v 2.6 2003/06/05 19:29:34 schorsch Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* flip picture file horizontally and/or vertically |
6 |
|
|
*/ |
7 |
|
|
|
8 |
greg |
1.3 |
#include <stdio.h> |
9 |
greg |
2.5 |
#include <time.h> |
10 |
schorsch |
2.7 |
#include <string.h> |
11 |
greg |
2.5 |
|
12 |
schorsch |
2.6 |
#include "platform.h" |
13 |
greg |
1.1 |
#include "color.h" |
14 |
greg |
1.5 |
#include "resolu.h" |
15 |
greg |
1.1 |
|
16 |
greg |
1.5 |
int order; /* input orientation */ |
17 |
|
|
int xres, yres; /* resolution (scanlen, nscans) */ |
18 |
|
|
|
19 |
greg |
1.1 |
long *scanpos; /* scanline positions */ |
20 |
|
|
|
21 |
greg |
1.5 |
int fhoriz=0, fvert=0; /* flip flags */ |
22 |
greg |
1.1 |
|
23 |
greg |
1.5 |
int correctorder = 0; /* correcting orientation? */ |
24 |
|
|
|
25 |
greg |
1.1 |
FILE *fin; /* input file */ |
26 |
|
|
|
27 |
|
|
char *progname; |
28 |
|
|
|
29 |
greg |
2.2 |
|
30 |
greg |
1.5 |
int |
31 |
|
|
neworder() /* figure out new order from old */ |
32 |
|
|
{ |
33 |
|
|
register int no; |
34 |
|
|
|
35 |
|
|
if (correctorder) |
36 |
|
|
return(order); /* just leave it */ |
37 |
|
|
if ((no = order) & YMAJOR) { |
38 |
|
|
if (fhoriz) no ^= XDECR; |
39 |
|
|
if (fvert) no ^= YDECR; |
40 |
|
|
} else { |
41 |
|
|
if (fhoriz) no ^= YDECR; |
42 |
|
|
if (fvert) no ^= XDECR; |
43 |
|
|
} |
44 |
|
|
return(no); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
|
48 |
greg |
1.1 |
main(argc, argv) |
49 |
|
|
int argc; |
50 |
|
|
char *argv[]; |
51 |
|
|
{ |
52 |
greg |
2.4 |
static char picfmt[LPICFMT+1] = PICFMT; |
53 |
|
|
int i, rval; |
54 |
schorsch |
2.6 |
SET_DEFAULT_BINARY(); |
55 |
|
|
SET_FILE_BINARY(stdout); |
56 |
greg |
1.1 |
progname = argv[0]; |
57 |
|
|
|
58 |
|
|
for (i = 1; i < argc; i++) |
59 |
|
|
if (!strcmp(argv[i], "-h")) |
60 |
|
|
fhoriz++; |
61 |
|
|
else if (!strcmp(argv[i], "-v")) |
62 |
|
|
fvert++; |
63 |
greg |
1.5 |
else if (!strcmp(argv[i], "-c")) |
64 |
|
|
correctorder++; |
65 |
greg |
1.1 |
else |
66 |
|
|
break; |
67 |
|
|
if (i >= argc || argv[i][0] == '-') { |
68 |
greg |
1.5 |
fprintf(stderr, "Usage: %s [-h][-v][-c] infile [outfile]\n", |
69 |
greg |
1.1 |
progname); |
70 |
|
|
exit(1); |
71 |
|
|
} |
72 |
|
|
if ((fin = fopen(argv[i], "r")) == NULL) { |
73 |
|
|
fprintf(stderr, "%s: cannot open\n", argv[1]); |
74 |
|
|
exit(1); |
75 |
|
|
} |
76 |
|
|
if (i < argc-1 && freopen(argv[i+1], "w", stdout) == NULL) { |
77 |
|
|
fprintf(stderr, "%s: cannot open\n", argv[i+1]); |
78 |
|
|
exit(1); |
79 |
|
|
} |
80 |
|
|
/* transfer header */ |
81 |
greg |
2.4 |
if ((rval = checkheader(fin, picfmt, stdout)) < 0) { |
82 |
greg |
1.4 |
fprintf(stderr, "%s: input not a Radiance picture\n", |
83 |
|
|
progname); |
84 |
|
|
exit(1); |
85 |
|
|
} |
86 |
greg |
2.4 |
if (rval) |
87 |
|
|
fputformat(picfmt, stdout); |
88 |
greg |
1.1 |
/* add new header info. */ |
89 |
greg |
1.2 |
printargs(i, argv, stdout); |
90 |
greg |
1.1 |
putchar('\n'); |
91 |
|
|
/* get picture size */ |
92 |
greg |
1.5 |
if ((order = fgetresolu(&xres, &yres, fin)) < 0) { |
93 |
greg |
1.1 |
fprintf(stderr, "%s: bad picture size\n", progname); |
94 |
|
|
exit(1); |
95 |
|
|
} |
96 |
|
|
/* write new picture size */ |
97 |
greg |
1.5 |
fputresolu(neworder(), xres, yres, stdout); |
98 |
greg |
1.1 |
/* goto end if vertical flip */ |
99 |
|
|
if (fvert) |
100 |
|
|
scanfile(); |
101 |
|
|
flip(); /* flip the image */ |
102 |
|
|
exit(0); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
|
106 |
|
|
memerr() |
107 |
|
|
{ |
108 |
|
|
fprintf(stderr, "%s: out of memory\n", progname); |
109 |
|
|
exit(1); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
|
113 |
|
|
scanfile() /* scan to the end of file */ |
114 |
|
|
{ |
115 |
|
|
extern long ftell(); |
116 |
|
|
COLR *scanin; |
117 |
|
|
int y; |
118 |
|
|
|
119 |
|
|
if ((scanpos = (long *)malloc(yres*sizeof(long))) == NULL) |
120 |
|
|
memerr(); |
121 |
|
|
if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL) |
122 |
|
|
memerr(); |
123 |
greg |
2.3 |
for (y = yres-1; y > 0; y--) { |
124 |
greg |
1.1 |
scanpos[y] = ftell(fin); |
125 |
|
|
if (freadcolrs(scanin, xres, fin) < 0) { |
126 |
|
|
fprintf(stderr, "%s: read error\n", progname); |
127 |
|
|
exit(1); |
128 |
|
|
} |
129 |
|
|
} |
130 |
greg |
2.3 |
scanpos[0] = ftell(fin); |
131 |
greg |
2.5 |
free((void *)scanin); |
132 |
greg |
1.1 |
} |
133 |
|
|
|
134 |
|
|
|
135 |
|
|
flip() /* flip the picture */ |
136 |
|
|
{ |
137 |
|
|
COLR *scanin, *scanout; |
138 |
|
|
int y; |
139 |
|
|
register int x; |
140 |
|
|
|
141 |
|
|
if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL) |
142 |
|
|
memerr(); |
143 |
|
|
if (fhoriz) { |
144 |
|
|
if ((scanout = (COLR *)malloc(xres*sizeof(COLR))) == NULL) |
145 |
|
|
memerr(); |
146 |
|
|
} else |
147 |
|
|
scanout = scanin; |
148 |
|
|
for (y = yres-1; y >= 0; y--) { |
149 |
|
|
if (fvert && fseek(fin, scanpos[yres-1-y], 0) == EOF) { |
150 |
|
|
fprintf(stderr, "%s: seek error\n", progname); |
151 |
|
|
exit(1); |
152 |
|
|
} |
153 |
|
|
if (freadcolrs(scanin, xres, fin) < 0) { |
154 |
|
|
fprintf(stderr, "%s: read error\n", progname); |
155 |
|
|
exit(1); |
156 |
|
|
} |
157 |
|
|
if (fhoriz) |
158 |
|
|
for (x = 0; x < xres; x++) |
159 |
|
|
copycolr(scanout[x], scanin[xres-1-x]); |
160 |
|
|
if (fwritecolrs(scanout, xres, stdout) < 0) { |
161 |
|
|
fprintf(stderr, "%s: write error\n", progname); |
162 |
|
|
exit(1); |
163 |
|
|
} |
164 |
|
|
} |
165 |
greg |
2.5 |
free((void *)scanin); |
166 |
greg |
2.3 |
if (fhoriz) |
167 |
greg |
2.5 |
free((void *)scanout); |
168 |
greg |
1.1 |
} |