ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pflip.c
Revision: 2.2
Committed: Mon Sep 21 12:14:23 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +12 -2 lines
Log Message:
Changes for PC port

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     int i;
60 greg 2.2 #ifdef MSDOS
61     extern int _fmode;
62     _fmode = O_BINARY;
63     setmode(fileno(stdout), O_BINARY);
64     #endif
65 greg 1.1 progname = argv[0];
66    
67     for (i = 1; i < argc; i++)
68     if (!strcmp(argv[i], "-h"))
69     fhoriz++;
70     else if (!strcmp(argv[i], "-v"))
71     fvert++;
72 greg 1.5 else if (!strcmp(argv[i], "-c"))
73     correctorder++;
74 greg 1.1 else
75     break;
76     if (i >= argc || argv[i][0] == '-') {
77 greg 1.5 fprintf(stderr, "Usage: %s [-h][-v][-c] infile [outfile]\n",
78 greg 1.1 progname);
79     exit(1);
80     }
81     if ((fin = fopen(argv[i], "r")) == NULL) {
82     fprintf(stderr, "%s: cannot open\n", argv[1]);
83     exit(1);
84     }
85     if (i < argc-1 && freopen(argv[i+1], "w", stdout) == NULL) {
86     fprintf(stderr, "%s: cannot open\n", argv[i+1]);
87     exit(1);
88     }
89     /* transfer header */
90 greg 1.4 if (checkheader(fin, COLRFMT, stdout) < 0) {
91     fprintf(stderr, "%s: input not a Radiance picture\n",
92     progname);
93     exit(1);
94     }
95 greg 1.1 /* add new header info. */
96 greg 1.2 printargs(i, argv, stdout);
97 greg 1.4 fputformat(COLRFMT, stdout);
98 greg 1.1 putchar('\n');
99     /* get picture size */
100 greg 1.5 if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
101 greg 1.1 fprintf(stderr, "%s: bad picture size\n", progname);
102     exit(1);
103     }
104     /* write new picture size */
105 greg 1.5 fputresolu(neworder(), xres, yres, stdout);
106 greg 1.1 /* goto end if vertical flip */
107     if (fvert)
108     scanfile();
109     flip(); /* flip the image */
110     exit(0);
111     }
112    
113    
114     memerr()
115     {
116     fprintf(stderr, "%s: out of memory\n", progname);
117     exit(1);
118     }
119    
120    
121     scanfile() /* scan to the end of file */
122     {
123     extern long ftell();
124     COLR *scanin;
125     int y;
126    
127     if ((scanpos = (long *)malloc(yres*sizeof(long))) == NULL)
128     memerr();
129     if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
130     memerr();
131     for (y = yres-1; y >= 0; y--) {
132     scanpos[y] = ftell(fin);
133     if (freadcolrs(scanin, xres, fin) < 0) {
134     fprintf(stderr, "%s: read error\n", progname);
135     exit(1);
136     }
137     }
138     free((char *)scanin);
139     }
140    
141    
142     flip() /* flip the picture */
143     {
144     COLR *scanin, *scanout;
145     int y;
146     register int x;
147    
148     if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
149     memerr();
150     if (fhoriz) {
151     if ((scanout = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
152     memerr();
153     } else
154     scanout = scanin;
155     for (y = yres-1; y >= 0; y--) {
156     if (fvert && fseek(fin, scanpos[yres-1-y], 0) == EOF) {
157     fprintf(stderr, "%s: seek error\n", progname);
158     exit(1);
159     }
160     if (freadcolrs(scanin, xres, fin) < 0) {
161     fprintf(stderr, "%s: read error\n", progname);
162     exit(1);
163     }
164     if (fhoriz)
165     for (x = 0; x < xres; x++)
166     copycolr(scanout[x], scanin[xres-1-x]);
167     if (fwritecolrs(scanout, xres, stdout) < 0) {
168     fprintf(stderr, "%s: write error\n", progname);
169     exit(1);
170     }
171     }
172     free((char *)scanin);
173     free((char *)scanout);
174     }