ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pflip.c
Revision: 2.6
Committed: Thu Jun 5 19:29:34 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.5: +4 -12 lines
Log Message:
Macros for setting binary file mode. Replacing MSDOS by _WIN32.

File Contents

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