ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pflip.c
Revision: 2.12
Committed: Fri Jul 19 17:37:56 2019 UTC (4 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, rad5R3, HEAD
Changes since 2.11: +2 -5 lines
Log Message:
Moved declarations and definitions for header.c from resolu.h to rtio.h

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.12 static const char RCSid[] = "$Id: pflip.c,v 2.11 2018/08/02 18:33:44 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * flip picture file horizontally and/or vertically
6     */
7    
8 greg 2.12 #include "rtio.h"
9 schorsch 2.6 #include "platform.h"
10 greg 1.1 #include "color.h"
11 greg 1.5 #include "resolu.h"
12 greg 1.1
13 greg 1.5 int order; /* input orientation */
14     int xres, yres; /* resolution (scanlen, nscans) */
15    
16 greg 1.1 long *scanpos; /* scanline positions */
17    
18 greg 1.5 int fhoriz=0, fvert=0; /* flip flags */
19 greg 1.1
20 greg 1.5 int correctorder = 0; /* correcting orientation? */
21    
22 greg 1.1 FILE *fin; /* input file */
23    
24     char *progname;
25    
26 greg 2.2
27 schorsch 2.8 static void memerr(void);
28     static void scanfile(void);
29     static void flip(void);
30    
31    
32     static int
33     neworder(void) /* figure out new order from old */
34 greg 1.5 {
35     register int no;
36    
37     if (correctorder)
38     return(order); /* just leave it */
39     if ((no = order) & YMAJOR) {
40     if (fhoriz) no ^= XDECR;
41     if (fvert) no ^= YDECR;
42     } else {
43     if (fhoriz) no ^= YDECR;
44     if (fvert) no ^= XDECR;
45     }
46     return(no);
47     }
48    
49 schorsch 2.8 int
50     main(
51     int argc,
52     char *argv[]
53     )
54 greg 1.1 {
55 greg 2.11 static char picfmt[MAXFMTLEN] = PICFMT;
56 greg 2.4 int i, rval;
57 schorsch 2.6 SET_DEFAULT_BINARY();
58     SET_FILE_BINARY(stdout);
59 greg 1.1 progname = argv[0];
60    
61     for (i = 1; i < argc; i++)
62     if (!strcmp(argv[i], "-h"))
63     fhoriz++;
64     else if (!strcmp(argv[i], "-v"))
65     fvert++;
66 greg 1.5 else if (!strcmp(argv[i], "-c"))
67     correctorder++;
68 greg 1.1 else
69     break;
70 greg 2.10 if (i < argc-2)
71     goto userr;
72     if (!fhoriz && !fvert)
73     fprintf(stderr, "%s: warning - no operation\n", argv[0]);
74 greg 1.1 if (i >= argc || argv[i][0] == '-') {
75 greg 2.10 if (fvert)
76     goto userr;
77     SET_FILE_BINARY(stdin);
78     fin = stdin;
79     } else if ((fin = fopen(argv[i], "r")) == NULL) {
80 greg 2.9 fprintf(stderr, "%s: cannot open\n", argv[i]);
81 greg 1.1 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 greg 2.10 userr:
111     fprintf(stderr, "Usage: %s [-h][-v][-c] infile [outfile]\n",
112     progname);
113     exit(1);
114 greg 1.1 }
115    
116    
117 schorsch 2.8 static void
118     memerr(void)
119 greg 1.1 {
120     fprintf(stderr, "%s: out of memory\n", progname);
121     exit(1);
122     }
123    
124    
125 schorsch 2.8 static void
126     scanfile(void) /* scan to the end of file */
127 greg 1.1 {
128     extern long ftell();
129     COLR *scanin;
130     int y;
131    
132     if ((scanpos = (long *)malloc(yres*sizeof(long))) == NULL)
133     memerr();
134     if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
135     memerr();
136 greg 2.3 for (y = yres-1; y > 0; y--) {
137 greg 1.1 scanpos[y] = ftell(fin);
138     if (freadcolrs(scanin, xres, fin) < 0) {
139     fprintf(stderr, "%s: read error\n", progname);
140     exit(1);
141     }
142     }
143 greg 2.3 scanpos[0] = ftell(fin);
144 greg 2.5 free((void *)scanin);
145 greg 1.1 }
146    
147    
148 schorsch 2.8 static void
149     flip(void) /* flip the picture */
150 greg 1.1 {
151     COLR *scanin, *scanout;
152     int y;
153     register int x;
154    
155     if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
156     memerr();
157     if (fhoriz) {
158     if ((scanout = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
159     memerr();
160     } else
161     scanout = scanin;
162     for (y = yres-1; y >= 0; y--) {
163     if (fvert && fseek(fin, scanpos[yres-1-y], 0) == EOF) {
164     fprintf(stderr, "%s: seek error\n", progname);
165     exit(1);
166     }
167     if (freadcolrs(scanin, xres, fin) < 0) {
168     fprintf(stderr, "%s: read error\n", progname);
169     exit(1);
170     }
171     if (fhoriz)
172     for (x = 0; x < xres; x++)
173     copycolr(scanout[x], scanin[xres-1-x]);
174     if (fwritecolrs(scanout, xres, stdout) < 0) {
175     fprintf(stderr, "%s: write error\n", progname);
176     exit(1);
177     }
178     }
179 greg 2.5 free((void *)scanin);
180 greg 2.3 if (fhoriz)
181 greg 2.5 free((void *)scanout);
182 greg 1.1 }