| 1 | 
greg | 
1.1 | 
#ifndef lint | 
| 2 | 
schorsch | 
2.9 | 
static const char       RCSid[] = "$Id: protate.c,v 2.8 2003/05/15 05:13:35 greg Exp $"; | 
| 3 | 
greg | 
1.1 | 
#endif | 
| 4 | 
  | 
  | 
/* | 
| 5 | 
  | 
  | 
 * prot.c - program to rotate picture file 90 degrees clockwise. | 
| 6 | 
  | 
  | 
 * | 
| 7 | 
  | 
  | 
 *      2/26/88 | 
| 8 | 
  | 
  | 
 */ | 
| 9 | 
  | 
  | 
 | 
| 10 | 
greg | 
1.7 | 
#include "standard.h" | 
| 11 | 
greg | 
1.1 | 
 | 
| 12 | 
  | 
  | 
#include "color.h" | 
| 13 | 
  | 
  | 
 | 
| 14 | 
greg | 
2.7 | 
#include <time.h> | 
| 15 | 
  | 
  | 
 | 
| 16 | 
greg | 
1.9 | 
#include "resolu.h" | 
| 17 | 
  | 
  | 
 | 
| 18 | 
  | 
  | 
int     order;                          /* input scanline order */ | 
| 19 | 
greg | 
1.1 | 
int     xres, yres;                     /* input resolution */ | 
| 20 | 
  | 
  | 
 | 
| 21 | 
greg | 
1.9 | 
int     correctorder = 0;               /* order correction? */ | 
| 22 | 
gwlarson | 
2.5 | 
int     ccw = 0;                        /* rotate CCW? */ | 
| 23 | 
greg | 
1.9 | 
 | 
| 24 | 
greg | 
2.8 | 
#ifdef SMLMEM | 
| 25 | 
  | 
  | 
char    buf[1<<20];                     /* output buffer */ | 
| 26 | 
  | 
  | 
#else | 
| 27 | 
greg | 
2.2 | 
char    buf[1<<22];                     /* output buffer */ | 
| 28 | 
  | 
  | 
#endif | 
| 29 | 
greg | 
1.1 | 
 | 
| 30 | 
  | 
  | 
int     nrows;                          /* number of rows output at once */ | 
| 31 | 
  | 
  | 
 | 
| 32 | 
  | 
  | 
#define scanbar         ((COLR *)buf) | 
| 33 | 
  | 
  | 
 | 
| 34 | 
  | 
  | 
char    *progname; | 
| 35 | 
  | 
  | 
 | 
| 36 | 
gwlarson | 
2.5 | 
short   ordertab[4][2] = { | 
| 37 | 
  | 
  | 
        {0,XDECR}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,0} | 
| 38 | 
  | 
  | 
}; | 
| 39 | 
greg | 
1.1 | 
 | 
| 40 | 
schorsch | 
2.9 | 
static int neworder(void); | 
| 41 | 
  | 
  | 
static void rotatecw(FILE *fp); | 
| 42 | 
  | 
  | 
static void rotateccw(FILE *fp); | 
| 43 | 
greg | 
1.8 | 
 | 
| 44 | 
schorsch | 
2.9 | 
 | 
| 45 | 
  | 
  | 
 | 
| 46 | 
  | 
  | 
static int | 
| 47 | 
  | 
  | 
neworder(void)          /* return corrected order */ | 
| 48 | 
gwlarson | 
2.5 | 
{ | 
| 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 | 
schorsch | 
2.9 | 
int | 
| 61 | 
  | 
  | 
main( | 
| 62 | 
  | 
  | 
        int     argc, | 
| 63 | 
  | 
  | 
        char    *argv[] | 
| 64 | 
  | 
  | 
) | 
| 65 | 
greg | 
1.1 | 
{ | 
| 66 | 
greg | 
2.4 | 
        static char     picfmt[LPICFMT+1] = PICFMT; | 
| 67 | 
  | 
  | 
        int     rval; | 
| 68 | 
greg | 
1.1 | 
        FILE    *fin; | 
| 69 | 
  | 
  | 
 | 
| 70 | 
  | 
  | 
        progname = argv[0]; | 
| 71 | 
  | 
  | 
 | 
| 72 | 
gwlarson | 
2.5 | 
        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 | 
greg | 
1.9 | 
                argc--; argv++; | 
| 84 | 
  | 
  | 
        } | 
| 85 | 
gwlarson | 
2.5 | 
        if (argc != 2 && argc != 3) | 
| 86 | 
  | 
  | 
                goto userr; | 
| 87 | 
greg | 
1.1 | 
        if ((fin = fopen(argv[1], "r")) == NULL) { | 
| 88 | 
  | 
  | 
                fprintf(stderr, "%s: cannot open\n", argv[1]); | 
| 89 | 
  | 
  | 
                exit(1); | 
| 90 | 
  | 
  | 
        } | 
| 91 | 
  | 
  | 
        if (argc == 3 && freopen(argv[2], "w", stdout) == NULL) { | 
| 92 | 
  | 
  | 
                fprintf(stderr, "%s: cannot open\n", argv[2]); | 
| 93 | 
  | 
  | 
                exit(1); | 
| 94 | 
  | 
  | 
        } | 
| 95 | 
greg | 
1.6 | 
                                        /* transfer header */ | 
| 96 | 
greg | 
2.4 | 
        if ((rval = checkheader(fin, picfmt, stdout)) < 0) { | 
| 97 | 
greg | 
1.9 | 
                fprintf(stderr, "%s: not a Radiance picture\n", progname); | 
| 98 | 
greg | 
1.8 | 
                exit(1); | 
| 99 | 
  | 
  | 
        } | 
| 100 | 
greg | 
2.4 | 
        if (rval) | 
| 101 | 
  | 
  | 
                fputformat(picfmt, stdout); | 
| 102 | 
greg | 
1.1 | 
                                        /* add new header info. */ | 
| 103 | 
gwlarson | 
2.5 | 
        fputs(progname, stdout); | 
| 104 | 
  | 
  | 
        if (ccw) fputs(" -r", stdout); | 
| 105 | 
  | 
  | 
        if (correctorder) fputs(" -c", stdout); | 
| 106 | 
  | 
  | 
        fputs("\n\n", stdout); | 
| 107 | 
greg | 
1.1 | 
                                        /* get picture size */ | 
| 108 | 
greg | 
1.9 | 
        if ((order = fgetresolu(&xres, &yres, fin)) < 0) { | 
| 109 | 
greg | 
1.1 | 
                fprintf(stderr, "%s: bad picture size\n", progname); | 
| 110 | 
  | 
  | 
                exit(1); | 
| 111 | 
  | 
  | 
        } | 
| 112 | 
  | 
  | 
                                        /* write new picture size */ | 
| 113 | 
greg | 
1.9 | 
        fputresolu(neworder(), yres, xres, stdout); | 
| 114 | 
greg | 
1.1 | 
                                        /* compute buffer capacity */ | 
| 115 | 
  | 
  | 
        nrows = sizeof(buf)/sizeof(COLR)/yres; | 
| 116 | 
gwlarson | 
2.5 | 
        if (ccw)                        /* rotate the image */ | 
| 117 | 
  | 
  | 
                rotateccw(fin); | 
| 118 | 
  | 
  | 
        else | 
| 119 | 
  | 
  | 
                rotatecw(fin); | 
| 120 | 
greg | 
1.1 | 
        exit(0); | 
| 121 | 
gwlarson | 
2.5 | 
userr: | 
| 122 | 
  | 
  | 
        fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname); | 
| 123 | 
  | 
  | 
        exit(1); | 
| 124 | 
greg | 
1.1 | 
} | 
| 125 | 
  | 
  | 
 | 
| 126 | 
  | 
  | 
 | 
| 127 | 
schorsch | 
2.9 | 
static void | 
| 128 | 
  | 
  | 
rotatecw(                       /* rotate picture clockwise */ | 
| 129 | 
  | 
  | 
        FILE    *fp | 
| 130 | 
  | 
  | 
) | 
| 131 | 
greg | 
1.1 | 
{ | 
| 132 | 
greg | 
1.5 | 
        register COLR   *inln; | 
| 133 | 
greg | 
1.1 | 
        register int    xoff, inx, iny; | 
| 134 | 
  | 
  | 
        long    start, ftell(); | 
| 135 | 
  | 
  | 
 | 
| 136 | 
greg | 
1.5 | 
        if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) { | 
| 137 | 
greg | 
1.1 | 
                fprintf(stderr, "%s: out of memory\n", progname); | 
| 138 | 
  | 
  | 
                exit(1); | 
| 139 | 
  | 
  | 
        } | 
| 140 | 
  | 
  | 
        start = ftell(fp); | 
| 141 | 
  | 
  | 
        for (xoff = 0; xoff < xres; xoff += nrows) { | 
| 142 | 
  | 
  | 
                if (fseek(fp, start, 0) < 0) { | 
| 143 | 
  | 
  | 
                        fprintf(stderr, "%s: seek error\n", progname); | 
| 144 | 
  | 
  | 
                        exit(1); | 
| 145 | 
  | 
  | 
                } | 
| 146 | 
  | 
  | 
                for (iny = yres-1; iny >= 0; iny--) { | 
| 147 | 
greg | 
1.5 | 
                        if (freadcolrs(inln, xres, fp) < 0) { | 
| 148 | 
greg | 
1.1 | 
                                fprintf(stderr, "%s: read error\n", progname); | 
| 149 | 
  | 
  | 
                                exit(1); | 
| 150 | 
  | 
  | 
                        } | 
| 151 | 
  | 
  | 
                        for (inx = 0; inx < nrows && xoff+inx < xres; inx++) | 
| 152 | 
gwlarson | 
2.6 | 
                                copycolr(scanbar[inx*yres+iny], | 
| 153 | 
  | 
  | 
                                                inln[xoff+inx]); | 
| 154 | 
greg | 
1.1 | 
                } | 
| 155 | 
  | 
  | 
                for (inx = 0; inx < nrows && xoff+inx < xres; inx++) | 
| 156 | 
gwlarson | 
2.5 | 
                        if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { | 
| 157 | 
  | 
  | 
                                fprintf(stderr, "%s: write error\n", progname); | 
| 158 | 
  | 
  | 
                                exit(1); | 
| 159 | 
  | 
  | 
                        } | 
| 160 | 
  | 
  | 
        } | 
| 161 | 
greg | 
2.7 | 
        free((void *)inln); | 
| 162 | 
gwlarson | 
2.5 | 
} | 
| 163 | 
  | 
  | 
 | 
| 164 | 
  | 
  | 
 | 
| 165 | 
schorsch | 
2.9 | 
static void | 
| 166 | 
  | 
  | 
rotateccw(                      /* rotate picture counter-clockwise */ | 
| 167 | 
  | 
  | 
        FILE    *fp | 
| 168 | 
  | 
  | 
) | 
| 169 | 
gwlarson | 
2.5 | 
{ | 
| 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 | 
gwlarson | 
2.6 | 
                                copycolr(scanbar[inx*yres+iny], | 
| 191 | 
  | 
  | 
                                                inln[xoff-inx]); | 
| 192 | 
gwlarson | 
2.5 | 
                } | 
| 193 | 
  | 
  | 
                for (inx = 0; inx < nrows && xoff-inx >= 0; inx++) | 
| 194 | 
greg | 
1.1 | 
                        if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { | 
| 195 | 
  | 
  | 
                                fprintf(stderr, "%s: write error\n", progname); | 
| 196 | 
  | 
  | 
                                exit(1); | 
| 197 | 
  | 
  | 
                        } | 
| 198 | 
  | 
  | 
        } | 
| 199 | 
greg | 
2.7 | 
        free((void *)inln); | 
| 200 | 
greg | 
1.1 | 
} |