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

# Content
1 /* Copyright (c) 1992 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * flip picture file horizontally and/or vertically
9 */
10
11 #include <stdio.h>
12
13 #ifdef MSDOS
14 #include <fcntl.h>
15 #endif
16
17 #include "color.h"
18
19 #include "resolu.h"
20
21 int order; /* input orientation */
22 int xres, yres; /* resolution (scanlen, nscans) */
23
24 long *scanpos; /* scanline positions */
25
26 int fhoriz=0, fvert=0; /* flip flags */
27
28 int correctorder = 0; /* correcting orientation? */
29
30 FILE *fin; /* input file */
31
32 char *progname;
33
34 extern char *malloc();
35
36
37 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 main(argc, argv)
56 int argc;
57 char *argv[];
58 {
59 static char picfmt[LPICFMT+1] = PICFMT;
60 int i, rval;
61 #ifdef MSDOS
62 extern int _fmode;
63 _fmode = O_BINARY;
64 setmode(fileno(stdout), O_BINARY);
65 #endif
66 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 else if (!strcmp(argv[i], "-c"))
74 correctorder++;
75 else
76 break;
77 if (i >= argc || argv[i][0] == '-') {
78 fprintf(stderr, "Usage: %s [-h][-v][-c] infile [outfile]\n",
79 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 if ((rval = checkheader(fin, picfmt, stdout)) < 0) {
92 fprintf(stderr, "%s: input not a Radiance picture\n",
93 progname);
94 exit(1);
95 }
96 if (rval)
97 fputformat(picfmt, stdout);
98 /* add new header info. */
99 printargs(i, argv, stdout);
100 putchar('\n');
101 /* get picture size */
102 if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
103 fprintf(stderr, "%s: bad picture size\n", progname);
104 exit(1);
105 }
106 /* write new picture size */
107 fputresolu(neworder(), xres, yres, stdout);
108 /* 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 for (y = yres-1; y > 0; y--) {
134 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 scanpos[0] = ftell(fin);
141 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 if (fhoriz)
177 free((char *)scanout);
178 }