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

Comparing ray/src/px/protate.c (file contents):
Revision 1.2 by greg, Tue Sep 12 13:04:27 1989 UTC vs.
Revision 2.9 by schorsch, Sun Mar 28 20:33:14 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1988 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
4   /*
5   * prot.c - program to rotate picture file 90 degrees clockwise.
# Line 9 | Line 7 | static char SCCSid[] = "$SunId$ LBL";
7   *      2/26/88
8   */
9  
10 < #include <stdio.h>
10 > #include "standard.h"
11  
12   #include "color.h"
13  
14 + #include <time.h>
15 +
16 + #include "resolu.h"
17 +
18 + int     order;                          /* input scanline order */
19   int     xres, yres;                     /* input resolution */
20  
21 + int     correctorder = 0;               /* order correction? */
22 + int     ccw = 0;                        /* rotate CCW? */
23 +
24 + #ifdef SMLMEM
25   char    buf[1<<20];                     /* output buffer */
26 + #else
27 + char    buf[1<<22];                     /* output buffer */
28 + #endif
29  
30   int     nrows;                          /* number of rows output at once */
31  
# Line 23 | Line 33 | int    nrows;                          /* number of rows output at once */
33  
34   char    *progname;
35  
36 + short   ordertab[4][2] = {
37 +        {0,XDECR}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,0}
38 + };
39  
40 < main(argc, argv)
41 < int     argc;
42 < char    *argv[];
40 > static int neworder(void);
41 > static void rotatecw(FILE *fp);
42 > static void rotateccw(FILE *fp);
43 >
44 >
45 >
46 > static int
47 > neworder(void)          /* return corrected order */
48   {
49 +        register int    i;
50 +
51 +        if (correctorder)
52 +                return(order);
53 +        for (i = 4; i--; )
54 +                if ((order&~YMAJOR) == ordertab[i][ccw])
55 +                        return(ordertab[i][1-ccw] | ((order&YMAJOR)^YMAJOR));
56 +        fputs("Order botch!\n", stderr);
57 +        exit(2);
58 + }
59 +
60 + int
61 + main(
62 +        int     argc,
63 +        char    *argv[]
64 + )
65 + {
66 +        static char     picfmt[LPICFMT+1] = PICFMT;
67 +        int     rval;
68          FILE    *fin;
69  
70          progname = argv[0];
71  
72 <        if (argc != 2 && argc != 3) {
73 <                fprintf(stderr, "Usage: %s infile [outfile]\n", progname);
74 <                exit(1);
72 >        while (argc > 2 && argv[1][0] == '-') {
73 >                switch (argv[1][1]) {
74 >                case 'c':
75 >                        correctorder = 1;
76 >                        break;
77 >                case 'r':
78 >                        ccw = 1;
79 >                        break;
80 >                default:
81 >                        goto userr;
82 >                }
83 >                argc--; argv++;
84          }
85 +        if (argc != 2 && argc != 3)
86 +                goto userr;
87          if ((fin = fopen(argv[1], "r")) == NULL) {
88                  fprintf(stderr, "%s: cannot open\n", argv[1]);
89                  exit(1);
# Line 44 | Line 92 | char   *argv[];
92                  fprintf(stderr, "%s: cannot open\n", argv[2]);
93                  exit(1);
94          }
95 <                                        /* copy header */
96 <        while (fgets(buf, sizeof(buf), fin) != NULL && buf[0] != '\n')
97 <                fputs(buf, stdout);
95 >                                        /* transfer header */
96 >        if ((rval = checkheader(fin, picfmt, stdout)) < 0) {
97 >                fprintf(stderr, "%s: not a Radiance picture\n", progname);
98 >                exit(1);
99 >        }
100 >        if (rval)
101 >                fputformat(picfmt, stdout);
102                                          /* add new header info. */
103 <        printf("%s\n\n", progname);
103 >        fputs(progname, stdout);
104 >        if (ccw) fputs(" -r", stdout);
105 >        if (correctorder) fputs(" -c", stdout);
106 >        fputs("\n\n", stdout);
107                                          /* get picture size */
108 <        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
108 >        if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
109                  fprintf(stderr, "%s: bad picture size\n", progname);
110                  exit(1);
111          }
112                                          /* write new picture size */
113 <        fputresolu(YMAJOR|YDECR, yres, xres, stdout);
113 >        fputresolu(neworder(), yres, xres, stdout);
114                                          /* compute buffer capacity */
115          nrows = sizeof(buf)/sizeof(COLR)/yres;
116 <        rotate(fin);                    /* rotate the image */
116 >        if (ccw)                        /* rotate the image */
117 >                rotateccw(fin);
118 >        else
119 >                rotatecw(fin);
120          exit(0);
121 + userr:
122 +        fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname);
123 +        exit(1);
124   }
125  
126  
127 < rotate(fp)                      /* rotate picture */
128 < FILE    *fp;
127 > static void
128 > rotatecw(                       /* rotate picture clockwise */
129 >        FILE    *fp
130 > )
131   {
132 <        register COLR   *inline;
132 >        register COLR   *inln;
133          register int    xoff, inx, iny;
134          long    start, ftell();
135  
136 <        if ((inline = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
136 >        if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
137                  fprintf(stderr, "%s: out of memory\n", progname);
138                  exit(1);
139          }
# Line 81 | Line 144 | FILE   *fp;
144                          exit(1);
145                  }
146                  for (iny = yres-1; iny >= 0; iny--) {
147 <                        if (freadcolrs(inline, xres, fp) < 0) {
147 >                        if (freadcolrs(inln, xres, fp) < 0) {
148                                  fprintf(stderr, "%s: read error\n", progname);
149                                  exit(1);
150                          }
151                          for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
152 <                                bcopy(inline[xoff+inx], scanbar[inx*yres+iny],
153 <                                                sizeof(COLR));
152 >                                copycolr(scanbar[inx*yres+iny],
153 >                                                inln[xoff+inx]);
154                  }
155                  for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
156                          if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
# Line 95 | Line 158 | FILE   *fp;
158                                  exit(1);
159                          }
160          }
161 <        free((char *)inline);
161 >        free((void *)inln);
162 > }
163 >
164 >
165 > static void
166 > rotateccw(                      /* rotate picture counter-clockwise */
167 >        FILE    *fp
168 > )
169 > {
170 >        register COLR   *inln;
171 >        register int    xoff, inx, iny;
172 >        long    start, ftell();
173 >
174 >        if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
175 >                fprintf(stderr, "%s: out of memory\n", progname);
176 >                exit(1);
177 >        }
178 >        start = ftell(fp);
179 >        for (xoff = xres-1; xoff >= 0; xoff -= nrows) {
180 >                if (fseek(fp, start, 0) < 0) {
181 >                        fprintf(stderr, "%s: seek error\n", progname);
182 >                        exit(1);
183 >                }
184 >                for (iny = 0; iny < yres; iny++) {
185 >                        if (freadcolrs(inln, xres, fp) < 0) {
186 >                                fprintf(stderr, "%s: read error\n", progname);
187 >                                exit(1);
188 >                        }
189 >                        for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
190 >                                copycolr(scanbar[inx*yres+iny],
191 >                                                inln[xoff-inx]);
192 >                }
193 >                for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
194 >                        if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
195 >                                fprintf(stderr, "%s: write error\n", progname);
196 >                                exit(1);
197 >                        }
198 >        }
199 >        free((void *)inln);
200   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines