ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pflip.c
Revision: 1.5
Committed: Mon Nov 11 14:02:02 1991 UTC (32 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +30 -5 lines
Log Message:
Improved handling of scanline ordering

File Contents

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