ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pflip.c
Revision: 2.4
Committed: Mon Oct 16 11:40:11 1995 UTC (28 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +5 -3 lines
Log Message:
added compatibility with alternate RADIANCE picture formats

File Contents

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