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.4 by greg, Mon Oct 16 11:40:22 1995 UTC vs.
Revision 2.11 by greg, Wed Feb 8 04:18:13 2012 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   *
7   *      2/26/88
8   */
9  
10 + #include "platform.h"
11   #include "standard.h"
12  
13   #include "color.h"
14  
15 + #include <time.h>
16 +
17   #include "resolu.h"
18  
19   int     order;                          /* input scanline order */
20   int     xres, yres;                     /* input resolution */
21  
22   int     correctorder = 0;               /* order correction? */
23 + int     ccw = 0;                        /* rotate CCW? */
24  
25 < #ifdef BIGMEM
25 < char    buf[1<<22];                     /* output buffer */
26 < #else
25 > #ifdef SMLMEM
26   char    buf[1<<20];                     /* output buffer */
27 + #else
28 + char    buf[1<<22];                     /* output buffer */
29   #endif
30  
31   int     nrows;                          /* number of rows output at once */
# Line 33 | Line 34 | int    nrows;                          /* number of rows output at once */
34  
35   char    *progname;
36  
37 < #define neworder()      (correctorder ? order : \
38 <                        (order^(order&YMAJOR?YDECR:XDECR)^YMAJOR))
37 > short   ordertab[4][2] = {
38 >        {0,0}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,XDECR}
39 > };
40  
41  
42 < main(argc, argv)
43 < int     argc;
44 < char    *argv[];
42 > static int neworder(void);
43 > static void rotatecw(FILE *fp);
44 > static void rotateccw(FILE *fp);
45 >
46 >
47 >
48 > static int
49 > neworder(void)          /* return corrected order */
50   {
51 +        static short    ordercw[8];
52 +        register int    i;
53 +
54 +        if (correctorder)
55 +                return(order);
56 +        if (!ordercw[0]) {
57 +                ordercw[YMAJOR|YDECR] = 0;
58 +                ordercw[0] = YMAJOR|XDECR;
59 +                ordercw[YMAJOR|XDECR] = XDECR|YDECR;
60 +                ordercw[XDECR|YDECR] = YMAJOR|YDECR;
61 +                ordercw[YMAJOR|XDECR|YDECR] = XDECR;
62 +                ordercw[XDECR] = YMAJOR;
63 +                ordercw[YMAJOR] = YDECR;
64 +                ordercw[YDECR] = YMAJOR|XDECR|YDECR;
65 +        }
66 +        if (!ccw)
67 +                return(ordercw[order]);
68 +        for (i = 8; i--; )
69 +                if (ordercw[i] == order)
70 +                        return(i);
71 +        fputs("Order botch!\n", stderr);
72 +        exit(2);
73 + }
74 +
75 + int
76 + main(
77 +        int     argc,
78 +        char    *argv[]
79 + )
80 + {
81          static char     picfmt[LPICFMT+1] = PICFMT;
82          int     rval;
83          FILE    *fin;
84  
85 +        SET_DEFAULT_BINARY();
86 +        SET_FILE_BINARY(stdout);
87 +
88          progname = argv[0];
89  
90 <        if (argc > 2 && !strcmp(argv[1], "-c")) {
91 <                correctorder++;
90 >        while (argc > 2 && argv[1][0] == '-') {
91 >                switch (argv[1][1]) {
92 >                case 'c':
93 >                        correctorder = 1;
94 >                        break;
95 >                case 'r':
96 >                        ccw = 1;
97 >                        break;
98 >                default:
99 >                        goto userr;
100 >                }
101                  argc--; argv++;
102          }
103 <        if (argc != 2 && argc != 3) {
104 <                fprintf(stderr, "Usage: %s [-c] infile [outfile]\n", progname);
56 <                exit(1);
57 <        }
103 >        if (argc != 2 && argc != 3)
104 >                goto userr;
105          if ((fin = fopen(argv[1], "r")) == NULL) {
106                  fprintf(stderr, "%s: cannot open\n", argv[1]);
107                  exit(1);
# Line 71 | Line 118 | char   *argv[];
118          if (rval)
119                  fputformat(picfmt, stdout);
120                                          /* add new header info. */
121 <        printf("%s%s\n\n", progname, correctorder?" -c":"");
121 >        fputs(progname, stdout);
122 >        if (ccw) fputs(" -r", stdout);
123 >        if (correctorder) fputs(" -c", stdout);
124 >        fputs("\n\n", stdout);
125                                          /* get picture size */
126          if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
127                  fprintf(stderr, "%s: bad picture size\n", progname);
# Line 81 | Line 131 | char   *argv[];
131          fputresolu(neworder(), yres, xres, stdout);
132                                          /* compute buffer capacity */
133          nrows = sizeof(buf)/sizeof(COLR)/yres;
134 <        rotate(fin);                    /* rotate the image */
134 >        if (ccw)                        /* rotate the image */
135 >                rotateccw(fin);
136 >        else
137 >                rotatecw(fin);
138          exit(0);
139 + userr:
140 +        fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname);
141 +        exit(1);
142   }
143  
144  
145 < rotate(fp)                      /* rotate picture */
146 < FILE    *fp;
145 > static void
146 > rotatecw(                       /* rotate picture clockwise */
147 >        FILE    *fp
148 > )
149   {
150          register COLR   *inln;
151          register int    xoff, inx, iny;
# Line 109 | Line 167 | FILE   *fp;
167                                  exit(1);
168                          }
169                          for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
170 <                                bcopy((char *)inln[xoff+inx],
171 <                                                (char *)scanbar[inx*yres+iny],
114 <                                                sizeof(COLR));
170 >                                copycolr(scanbar[inx*yres+iny],
171 >                                                inln[xoff+inx]);
172                  }
173                  for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
174                          if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
# Line 119 | Line 176 | FILE   *fp;
176                                  exit(1);
177                          }
178          }
179 <        free((char *)inln);
179 >        free((void *)inln);
180 > }
181 >
182 >
183 > static void
184 > rotateccw(                      /* rotate picture counter-clockwise */
185 >        FILE    *fp
186 > )
187 > {
188 >        register COLR   *inln;
189 >        register int    xoff, inx, iny;
190 >        long    start, ftell();
191 >
192 >        if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
193 >                fprintf(stderr, "%s: out of memory\n", progname);
194 >                exit(1);
195 >        }
196 >        start = ftell(fp);
197 >        for (xoff = xres-1; xoff >= 0; xoff -= nrows) {
198 >                if (fseek(fp, start, 0) < 0) {
199 >                        fprintf(stderr, "%s: seek error\n", progname);
200 >                        exit(1);
201 >                }
202 >                for (iny = 0; iny < yres; iny++) {
203 >                        if (freadcolrs(inln, xres, fp) < 0) {
204 >                                fprintf(stderr, "%s: read error\n", progname);
205 >                                exit(1);
206 >                        }
207 >                        for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
208 >                                copycolr(scanbar[inx*yres+iny],
209 >                                                inln[xoff-inx]);
210 >                }
211 >                for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
212 >                        if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
213 >                                fprintf(stderr, "%s: write error\n", progname);
214 >                                exit(1);
215 >                        }
216 >        }
217 >        free((void *)inln);
218   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines