ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pflip.c
Revision: 2.14
Committed: Sat Jun 7 05:09:46 2025 UTC (12 days, 7 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.13: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

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