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

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.6 static const char RCSid[] = "$Id: pflip.c,v 2.5 2003/02/22 02:07:27 greg 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    
11 schorsch 2.6 #include "platform.h"
12 greg 1.1 #include "color.h"
13 greg 1.5 #include "resolu.h"
14 greg 1.1
15 greg 1.5 int order; /* input orientation */
16     int xres, yres; /* resolution (scanlen, nscans) */
17    
18 greg 1.1 long *scanpos; /* scanline positions */
19    
20 greg 1.5 int fhoriz=0, fvert=0; /* flip flags */
21 greg 1.1
22 greg 1.5 int correctorder = 0; /* correcting orientation? */
23    
24 greg 1.1 FILE *fin; /* input file */
25    
26     char *progname;
27    
28 greg 2.2
29 greg 1.5 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 greg 1.1 main(argc, argv)
48     int argc;
49     char *argv[];
50     {
51 greg 2.4 static char picfmt[LPICFMT+1] = PICFMT;
52     int i, rval;
53 schorsch 2.6 SET_DEFAULT_BINARY();
54     SET_FILE_BINARY(stdout);
55 greg 1.1 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 greg 1.5 else if (!strcmp(argv[i], "-c"))
63     correctorder++;
64 greg 1.1 else
65     break;
66     if (i >= argc || argv[i][0] == '-') {
67 greg 1.5 fprintf(stderr, "Usage: %s [-h][-v][-c] infile [outfile]\n",
68 greg 1.1 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 greg 2.4 if ((rval = checkheader(fin, picfmt, stdout)) < 0) {
81 greg 1.4 fprintf(stderr, "%s: input not a Radiance picture\n",
82     progname);
83     exit(1);
84     }
85 greg 2.4 if (rval)
86     fputformat(picfmt, stdout);
87 greg 1.1 /* add new header info. */
88 greg 1.2 printargs(i, argv, stdout);
89 greg 1.1 putchar('\n');
90     /* get picture size */
91 greg 1.5 if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
92 greg 1.1 fprintf(stderr, "%s: bad picture size\n", progname);
93     exit(1);
94     }
95     /* write new picture size */
96 greg 1.5 fputresolu(neworder(), xres, yres, stdout);
97 greg 1.1 /* 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 greg 2.3 for (y = yres-1; y > 0; y--) {
123 greg 1.1 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 greg 2.3 scanpos[0] = ftell(fin);
130 greg 2.5 free((void *)scanin);
131 greg 1.1 }
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 greg 2.5 free((void *)scanin);
165 greg 2.3 if (fhoriz)
166 greg 2.5 free((void *)scanout);
167 greg 1.1 }