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