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 |
* flip picture file horizontally and/or vertically |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
|
13 |
#include "color.h" |
14 |
|
15 |
int xres, yres; /* input resolution */ |
16 |
|
17 |
long *scanpos; /* scanline positions */ |
18 |
|
19 |
int fhoriz, fvert; /* flip flags */ |
20 |
|
21 |
FILE *fin; /* input file */ |
22 |
|
23 |
char *progname; |
24 |
|
25 |
|
26 |
main(argc, argv) |
27 |
int argc; |
28 |
char *argv[]; |
29 |
{ |
30 |
int i; |
31 |
|
32 |
progname = argv[0]; |
33 |
|
34 |
for (i = 1; i < argc; i++) |
35 |
if (!strcmp(argv[i], "-h")) |
36 |
fhoriz++; |
37 |
else if (!strcmp(argv[i], "-v")) |
38 |
fvert++; |
39 |
else |
40 |
break; |
41 |
if (i >= argc || argv[i][0] == '-') { |
42 |
fprintf(stderr, "Usage: %s [-h][-v] infile [outfile]\n", |
43 |
progname); |
44 |
exit(1); |
45 |
} |
46 |
if ((fin = fopen(argv[i], "r")) == NULL) { |
47 |
fprintf(stderr, "%s: cannot open\n", argv[1]); |
48 |
exit(1); |
49 |
} |
50 |
if (i < argc-1 && freopen(argv[i+1], "w", stdout) == NULL) { |
51 |
fprintf(stderr, "%s: cannot open\n", argv[i+1]); |
52 |
exit(1); |
53 |
} |
54 |
/* transfer header */ |
55 |
if (checkheader(fin, COLRFMT, stdout) < 0) { |
56 |
fprintf(stderr, "%s: input not a Radiance picture\n", |
57 |
progname); |
58 |
exit(1); |
59 |
} |
60 |
/* add new header info. */ |
61 |
printargs(i, argv, stdout); |
62 |
fputformat(COLRFMT, stdout); |
63 |
putchar('\n'); |
64 |
/* get picture size */ |
65 |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
66 |
fprintf(stderr, "%s: bad picture size\n", progname); |
67 |
exit(1); |
68 |
} |
69 |
/* write new picture size */ |
70 |
fputresolu(YMAJOR|YDECR, xres, yres, stdout); |
71 |
/* goto end if vertical flip */ |
72 |
if (fvert) |
73 |
scanfile(); |
74 |
flip(); /* flip the image */ |
75 |
exit(0); |
76 |
} |
77 |
|
78 |
|
79 |
memerr() |
80 |
{ |
81 |
fprintf(stderr, "%s: out of memory\n", progname); |
82 |
exit(1); |
83 |
} |
84 |
|
85 |
|
86 |
scanfile() /* scan to the end of file */ |
87 |
{ |
88 |
extern long ftell(); |
89 |
COLR *scanin; |
90 |
int y; |
91 |
|
92 |
if ((scanpos = (long *)malloc(yres*sizeof(long))) == NULL) |
93 |
memerr(); |
94 |
if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL) |
95 |
memerr(); |
96 |
for (y = yres-1; y >= 0; y--) { |
97 |
scanpos[y] = ftell(fin); |
98 |
if (freadcolrs(scanin, xres, fin) < 0) { |
99 |
fprintf(stderr, "%s: read error\n", progname); |
100 |
exit(1); |
101 |
} |
102 |
} |
103 |
free((char *)scanin); |
104 |
} |
105 |
|
106 |
|
107 |
flip() /* flip the picture */ |
108 |
{ |
109 |
COLR *scanin, *scanout; |
110 |
int y; |
111 |
register int x; |
112 |
|
113 |
if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL) |
114 |
memerr(); |
115 |
if (fhoriz) { |
116 |
if ((scanout = (COLR *)malloc(xres*sizeof(COLR))) == NULL) |
117 |
memerr(); |
118 |
} else |
119 |
scanout = scanin; |
120 |
for (y = yres-1; y >= 0; y--) { |
121 |
if (fvert && fseek(fin, scanpos[yres-1-y], 0) == EOF) { |
122 |
fprintf(stderr, "%s: seek error\n", progname); |
123 |
exit(1); |
124 |
} |
125 |
if (freadcolrs(scanin, xres, fin) < 0) { |
126 |
fprintf(stderr, "%s: read error\n", progname); |
127 |
exit(1); |
128 |
} |
129 |
if (fhoriz) |
130 |
for (x = 0; x < xres; x++) |
131 |
copycolr(scanout[x], scanin[xres-1-x]); |
132 |
if (fwritecolrs(scanout, xres, stdout) < 0) { |
133 |
fprintf(stderr, "%s: write error\n", progname); |
134 |
exit(1); |
135 |
} |
136 |
} |
137 |
free((char *)scanin); |
138 |
free((char *)scanout); |
139 |
} |