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.8 by greg, Thu Apr 18 14:42:06 1991 UTC vs.
Revision 2.8 by greg, Thu May 15 05:13:35 2003 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 13 | 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 */
17 double  inpaspect = 1.0;                /* input aspect ratio */
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  
23 int     wrongformat = 0;
24
32   #define scanbar         ((COLR *)buf)
33  
34   char    *progname;
35  
36 + short   ordertab[4][2] = {
37 +        {0,XDECR}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,0}
38 + };
39  
40 < headline(s)                             /* process line from header */
41 < char    *s;
40 >
41 > int
42 > neworder()              /* return corrected order */
43   {
44 <        char    fmt[32];
44 >        register int    i;
45  
46 <        fputs(s, stdout);
47 <        if (isaspect(s))
48 <                inpaspect *= aspectval(s);
49 <        else if (isformat(s)) {
50 <                formatval(fmt, s);
51 <                wrongformat = strcmp(fmt, COLRFMT);
52 <        }
46 >        if (correctorder)
47 >                return(order);
48 >        for (i = 4; i--; )
49 >                if ((order&~YMAJOR) == ordertab[i][ccw])
50 >                        return(ordertab[i][1-ccw] | ((order&YMAJOR)^YMAJOR));
51 >        fputs("Order botch!\n", stderr);
52 >        exit(2);
53   }
54  
55  
# Line 46 | Line 57 | main(argc, argv)
57   int     argc;
58   char    *argv[];
59   {
60 +        static char     picfmt[LPICFMT+1] = PICFMT;
61 +        int     rval;
62          FILE    *fin;
63  
64          progname = argv[0];
65  
66 <        if (argc != 2 && argc != 3) {
67 <                fprintf(stderr, "Usage: %s infile [outfile]\n", progname);
68 <                exit(1);
66 >        while (argc > 2 && argv[1][0] == '-') {
67 >                switch (argv[1][1]) {
68 >                case 'c':
69 >                        correctorder = 1;
70 >                        break;
71 >                case 'r':
72 >                        ccw = 1;
73 >                        break;
74 >                default:
75 >                        goto userr;
76 >                }
77 >                argc--; argv++;
78          }
79 +        if (argc != 2 && argc != 3)
80 +                goto userr;
81          if ((fin = fopen(argv[1], "r")) == NULL) {
82                  fprintf(stderr, "%s: cannot open\n", argv[1]);
83                  exit(1);
# Line 63 | Line 87 | char   *argv[];
87                  exit(1);
88          }
89                                          /* transfer header */
90 <        getheader(fin, headline, NULL);
91 <        if (wrongformat) {
68 <                fprintf(stderr, "%s: wrong picture format\n", progname);
90 >        if ((rval = checkheader(fin, picfmt, stdout)) < 0) {
91 >                fprintf(stderr, "%s: not a Radiance picture\n", progname);
92                  exit(1);
93          }
94 +        if (rval)
95 +                fputformat(picfmt, stdout);
96                                          /* add new header info. */
97 <        if (inpaspect < .99 || inpaspect > 1.01)
98 <                fputaspect(1./inpaspect/inpaspect, stdout);
99 <        printf("%s\n\n", progname);
97 >        fputs(progname, stdout);
98 >        if (ccw) fputs(" -r", stdout);
99 >        if (correctorder) fputs(" -c", stdout);
100 >        fputs("\n\n", stdout);
101                                          /* get picture size */
102 <        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
102 >        if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
103                  fprintf(stderr, "%s: bad picture size\n", progname);
104                  exit(1);
105          }
106                                          /* write new picture size */
107 <        fputresolu(YMAJOR|YDECR, yres, xres, stdout);
107 >        fputresolu(neworder(), yres, xres, stdout);
108                                          /* compute buffer capacity */
109          nrows = sizeof(buf)/sizeof(COLR)/yres;
110 <        rotate(fin);                    /* rotate the image */
110 >        if (ccw)                        /* rotate the image */
111 >                rotateccw(fin);
112 >        else
113 >                rotatecw(fin);
114          exit(0);
115 + userr:
116 +        fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname);
117 +        exit(1);
118   }
119  
120  
121 < rotate(fp)                      /* rotate picture */
121 > rotatecw(fp)                    /* rotate picture clockwise */
122   FILE    *fp;
123   {
124          register COLR   *inln;
# Line 109 | Line 141 | FILE   *fp;
141                                  exit(1);
142                          }
143                          for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
144 <                                bcopy((char *)inln[xoff+inx],
145 <                                                (char *)scanbar[inx*yres+iny],
114 <                                                sizeof(COLR));
144 >                                copycolr(scanbar[inx*yres+iny],
145 >                                                inln[xoff+inx]);
146                  }
147                  for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
148                          if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
# Line 119 | Line 150 | FILE   *fp;
150                                  exit(1);
151                          }
152          }
153 <        free((char *)inln);
153 >        free((void *)inln);
154 > }
155 >
156 >
157 > rotateccw(fp)                   /* rotate picture counter-clockwise */
158 > FILE    *fp;
159 > {
160 >        register COLR   *inln;
161 >        register int    xoff, inx, iny;
162 >        long    start, ftell();
163 >
164 >        if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
165 >                fprintf(stderr, "%s: out of memory\n", progname);
166 >                exit(1);
167 >        }
168 >        start = ftell(fp);
169 >        for (xoff = xres-1; xoff >= 0; xoff -= nrows) {
170 >                if (fseek(fp, start, 0) < 0) {
171 >                        fprintf(stderr, "%s: seek error\n", progname);
172 >                        exit(1);
173 >                }
174 >                for (iny = 0; iny < yres; iny++) {
175 >                        if (freadcolrs(inln, xres, fp) < 0) {
176 >                                fprintf(stderr, "%s: read error\n", progname);
177 >                                exit(1);
178 >                        }
179 >                        for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
180 >                                copycolr(scanbar[inx*yres+iny],
181 >                                                inln[xoff-inx]);
182 >                }
183 >                for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
184 >                        if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
185 >                                fprintf(stderr, "%s: write error\n", progname);
186 >                                exit(1);
187 >                        }
188 >        }
189 >        free((void *)inln);
190   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines