ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pflip.c
Revision: 2.5
Committed: Sat Feb 22 02:07:27 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.4: +6 -9 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

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