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 2.1 by greg, Tue Nov 12 16:04:09 1991 UTC vs.
Revision 2.10 by greg, Fri Apr 30 20:29:24 2004 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   * prot.c - program to rotate picture file 90 degrees clockwise.
6   *
# Line 14 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
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 29 | Line 33 | int    nrows;                          /* number of rows output at once */
33  
34   char    *progname;
35  
36 < #define neworder()      (correctorder ? order : \
37 <                        (order^(order&YMAJOR?YDECR:XDECR)^YMAJOR))
36 > short   ordertab[4][2] = {
37 >        {0,0}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,XDECR}
38 > };
39  
40  
41 < main(argc, argv)
42 < int     argc;
43 < char    *argv[];
41 > static int neworder(void);
42 > static void rotatecw(FILE *fp);
43 > static void rotateccw(FILE *fp);
44 >
45 >
46 >
47 > static int
48 > neworder(void)          /* return corrected order */
49   {
50 +        static short    ordercw[8];
51 +        register int    i;
52 +
53 +        if (correctorder)
54 +                return(order);
55 +        if (!ordercw[0]) {
56 +                ordercw[YMAJOR|YDECR] = 0;
57 +                ordercw[0] = YMAJOR|XDECR;
58 +                ordercw[YMAJOR|XDECR] = XDECR|YDECR;
59 +                ordercw[XDECR|YDECR] = YMAJOR|YDECR;
60 +                ordercw[YMAJOR|XDECR|YDECR] = XDECR;
61 +                ordercw[XDECR] = YMAJOR;
62 +                ordercw[YMAJOR] = YDECR;
63 +                ordercw[YDECR] = YMAJOR|XDECR|YDECR;
64 +        }
65 +        if (!ccw)
66 +                return(ordercw[order]);
67 +        for (i = 8; i--; )
68 +                if (ordercw[i] == order)
69 +                        return(i);
70 +        fputs("Order botch!\n", stderr);
71 +        exit(2);
72 + }
73 +
74 + int
75 + main(
76 +        int     argc,
77 +        char    *argv[]
78 + )
79 + {
80 +        static char     picfmt[LPICFMT+1] = PICFMT;
81 +        int     rval;
82          FILE    *fin;
83  
84          progname = argv[0];
85  
86 <        if (argc > 2 && !strcmp(argv[1], "-c")) {
87 <                correctorder++;
86 >        while (argc > 2 && argv[1][0] == '-') {
87 >                switch (argv[1][1]) {
88 >                case 'c':
89 >                        correctorder = 1;
90 >                        break;
91 >                case 'r':
92 >                        ccw = 1;
93 >                        break;
94 >                default:
95 >                        goto userr;
96 >                }
97                  argc--; argv++;
98          }
99 <        if (argc != 2 && argc != 3) {
100 <                fprintf(stderr, "Usage: %s [-c] infile [outfile]\n", progname);
50 <                exit(1);
51 <        }
99 >        if (argc != 2 && argc != 3)
100 >                goto userr;
101          if ((fin = fopen(argv[1], "r")) == NULL) {
102                  fprintf(stderr, "%s: cannot open\n", argv[1]);
103                  exit(1);
# Line 58 | Line 107 | char   *argv[];
107                  exit(1);
108          }
109                                          /* transfer header */
110 <        if (checkheader(fin, COLRFMT, stdout) < 0) {
110 >        if ((rval = checkheader(fin, picfmt, stdout)) < 0) {
111                  fprintf(stderr, "%s: not a Radiance picture\n", progname);
112                  exit(1);
113          }
114 +        if (rval)
115 +                fputformat(picfmt, stdout);
116                                          /* add new header info. */
117 <        printf("%s\n\n", progname);
117 >        fputs(progname, stdout);
118 >        if (ccw) fputs(" -r", stdout);
119 >        if (correctorder) fputs(" -c", stdout);
120 >        fputs("\n\n", stdout);
121                                          /* get picture size */
122          if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
123                  fprintf(stderr, "%s: bad picture size\n", progname);
# Line 73 | Line 127 | char   *argv[];
127          fputresolu(neworder(), yres, xres, stdout);
128                                          /* compute buffer capacity */
129          nrows = sizeof(buf)/sizeof(COLR)/yres;
130 <        rotate(fin);                    /* rotate the image */
130 >        if (ccw)                        /* rotate the image */
131 >                rotateccw(fin);
132 >        else
133 >                rotatecw(fin);
134          exit(0);
135 + userr:
136 +        fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname);
137 +        exit(1);
138   }
139  
140  
141 < rotate(fp)                      /* rotate picture */
142 < FILE    *fp;
141 > static void
142 > rotatecw(                       /* rotate picture clockwise */
143 >        FILE    *fp
144 > )
145   {
146          register COLR   *inln;
147          register int    xoff, inx, iny;
# Line 101 | Line 163 | FILE   *fp;
163                                  exit(1);
164                          }
165                          for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
166 <                                bcopy((char *)inln[xoff+inx],
167 <                                                (char *)scanbar[inx*yres+iny],
106 <                                                sizeof(COLR));
166 >                                copycolr(scanbar[inx*yres+iny],
167 >                                                inln[xoff+inx]);
168                  }
169                  for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
170                          if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
# Line 111 | Line 172 | FILE   *fp;
172                                  exit(1);
173                          }
174          }
175 <        free((char *)inln);
175 >        free((void *)inln);
176 > }
177 >
178 >
179 > static void
180 > rotateccw(                      /* rotate picture counter-clockwise */
181 >        FILE    *fp
182 > )
183 > {
184 >        register COLR   *inln;
185 >        register int    xoff, inx, iny;
186 >        long    start, ftell();
187 >
188 >        if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
189 >                fprintf(stderr, "%s: out of memory\n", progname);
190 >                exit(1);
191 >        }
192 >        start = ftell(fp);
193 >        for (xoff = xres-1; xoff >= 0; xoff -= nrows) {
194 >                if (fseek(fp, start, 0) < 0) {
195 >                        fprintf(stderr, "%s: seek error\n", progname);
196 >                        exit(1);
197 >                }
198 >                for (iny = 0; iny < yres; iny++) {
199 >                        if (freadcolrs(inln, xres, fp) < 0) {
200 >                                fprintf(stderr, "%s: read error\n", progname);
201 >                                exit(1);
202 >                        }
203 >                        for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
204 >                                copycolr(scanbar[inx*yres+iny],
205 >                                                inln[xoff-inx]);
206 >                }
207 >                for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
208 >                        if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
209 >                                fprintf(stderr, "%s: write error\n", progname);
210 >                                exit(1);
211 >                        }
212 >        }
213 >        free((void *)inln);
214   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines