ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pflip.c
(Generate patch)

Comparing ray/src/px/pflip.c (file contents):
Revision 1.3 by greg, Wed Jan 9 12:21:09 1991 UTC vs.
Revision 2.13 by greg, Fri Jun 6 19:11:21 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * flip picture file horizontally and/or vertically
6   */
7  
8 < #include <stdio.h>
9 <
8 > #include "rtio.h"
9 > #include "paths.h"
10 > #include "platform.h"
11   #include "color.h"
12 + #include "resolu.h"
13  
14 < int     xres, yres;                     /* input resolution */
14 > int     order;                          /* input orientation */
15 > int     xres, yres;                     /* resolution (scanlen, nscans) */
16  
17   long    *scanpos;                       /* scanline positions */
18  
19 < int     fhoriz, fvert;                  /* flip flags */
19 > int     fhoriz=0, fvert=0;              /* flip flags */
20  
21 + int     correctorder = 0;               /* correcting orientation? */
22 +
23   FILE    *fin;                           /* input file */
24  
23 char    *progname;
25  
26 + static void memerr(void);
27 + static void scanfile(void);
28 + static void flip(void);
29  
30 < main(argc, argv)
31 < int     argc;
32 < char    *argv[];
30 >
31 > static int
32 > neworder(void)                  /* figure out new order from old */
33   {
34 <        int     i;
34 >        register int  no;
35  
36 <        progname = argv[0];
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 + int
49 + main(
50 +        int     argc,
51 +        char    *argv[]
52 + )
53 + {
54 +        static char     picfmt[MAXFMTLEN] = PICFMT;
55 +        int     i, rval;
56 +        
57 +        SET_DEFAULT_BINARY();
58 +        SET_FILE_BINARY(stdout);
59 +        fixargv0(argv[0]);
60 +
61          for (i = 1; i < argc; i++)
62                  if (!strcmp(argv[i], "-h"))
63                          fhoriz++;
64                  else if (!strcmp(argv[i], "-v"))
65                          fvert++;
66 +                else if (!strcmp(argv[i], "-c"))
67 +                        correctorder++;
68                  else
69                          break;
70 +        if (i < argc-2)
71 +                goto userr;
72 +        if (!fhoriz && !fvert)
73 +                fprintf(stderr, "%s: warning - no operation\n", argv[0]);
74          if (i >= argc || argv[i][0] == '-') {
75 <                fprintf(stderr, "Usage: %s [-h][-v] infile [outfile]\n",
76 <                                progname);
75 >                if (fvert)
76 >                        goto userr;
77 >                SET_FILE_BINARY(stdin);
78 >                fin = stdin;
79 >        } else if ((fin = fopen(argv[i], "r")) == NULL) {
80 >                fprintf(stderr, "%s: cannot open\n", argv[i]);
81                  exit(1);
82          }
46        if ((fin = fopen(argv[i], "r")) == NULL) {
47                fprintf(stderr, "%s: cannot open\n", argv[1]);
48                exit(1);
49        }
83          if (i < argc-1 && freopen(argv[i+1], "w", stdout) == NULL) {
84                  fprintf(stderr, "%s: cannot open\n", argv[i+1]);
85                  exit(1);
86          }
87                                          /* transfer header */
88 <        copyheader(fin, stdout);
88 >        if ((rval = checkheader(fin, picfmt, stdout)) < 0) {
89 >                fprintf(stderr, "%s: input not a Radiance picture\n",
90 >                                progname);
91 >                exit(1);
92 >        }
93 >        if (rval)
94 >                fputformat(picfmt, stdout);
95                                          /* add new header info. */
96          printargs(i, argv, stdout);
97          putchar('\n');
98                                          /* get picture size */
99 <        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
99 >        if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
100                  fprintf(stderr, "%s: bad picture size\n", progname);
101                  exit(1);
102          }
103                                          /* write new picture size */
104 <        fputresolu(YMAJOR|YDECR, xres, yres, stdout);
104 >        fputresolu(neworder(), xres, yres, stdout);
105                                          /* goto end if vertical flip */
106          if (fvert)
107                  scanfile();
108          flip();                         /* flip the image */
109          exit(0);
110 + userr:
111 +        fprintf(stderr, "Usage: %s [-h][-v][-c] infile [outfile]\n",
112 +                        progname);
113 +        exit(1);
114   }
115  
116  
117 < memerr()
117 > static void
118 > memerr(void)
119   {
120          fprintf(stderr, "%s: out of memory\n", progname);
121          exit(1);
122   }
123  
124  
125 < scanfile()                              /* scan to the end of file */
125 > static void
126 > scanfile(void)                          /* scan to the end of file */
127   {
128          extern long     ftell();
129          COLR    *scanin;
# Line 88 | Line 133 | scanfile()                             /* scan to the end of file */
133                  memerr();
134          if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
135                  memerr();
136 <        for (y = yres-1; y >= 0; y--) {
136 >        for (y = yres-1; y > 0; y--) {
137                  scanpos[y] = ftell(fin);
138                  if (freadcolrs(scanin, xres, fin) < 0) {
139                          fprintf(stderr, "%s: read error\n", progname);
140                          exit(1);
141                  }
142          }
143 <        free((char *)scanin);
143 >        scanpos[0] = ftell(fin);
144 >        free((void *)scanin);
145   }
146  
147  
148 < flip()                                  /* flip the picture */
148 > static void
149 > flip(void)                                      /* flip the picture */
150   {
151          COLR    *scanin, *scanout;
152          int     y;
# Line 129 | Line 176 | flip()                                 /* flip the picture */
176                          exit(1);
177                  }
178          }
179 <        free((char *)scanin);
180 <        free((char *)scanout);
179 >        free((void *)scanin);
180 >        if (fhoriz)
181 >                free((void *)scanout);
182   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines