| 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. | 
| 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 |  |  | 
| 33 |  |  | 
| 34 |  | char    *progname; | 
| 35 |  |  | 
| 36 | + | short   ordertab[4][2] = { | 
| 37 | + | {0,0}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,XDECR} | 
| 38 | + | }; | 
| 39 |  |  | 
| 40 | < | main(argc, argv) | 
| 41 | < | int     argc; | 
| 42 | < | char    *argv[]; | 
| 40 | > |  | 
| 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 && argc != 3) { | 
| 87 | < | fprintf(stderr, "Usage: %s infile [outfile]\n", progname); | 
| 88 | < | exit(1); | 
| 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 | + | goto userr; | 
| 101 |  | if ((fin = fopen(argv[1], "r")) == NULL) { | 
| 102 |  | fprintf(stderr, "%s: cannot open\n", argv[1]); | 
| 103 |  | exit(1); | 
| 106 |  | fprintf(stderr, "%s: cannot open\n", argv[2]); | 
| 107 |  | exit(1); | 
| 108 |  | } | 
| 109 | < | /* copy header */ | 
| 110 | < | while (fgets(buf, sizeof(buf), fin) != NULL && buf[0] != '\n') | 
| 111 | < | fputs(buf, stdout); | 
| 109 | > | /* transfer header */ | 
| 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 (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { | 
| 122 | > | if ((order = fgetresolu(&xres, &yres, fin)) < 0) { | 
| 123 |  | fprintf(stderr, "%s: bad picture size\n", progname); | 
| 124 |  | exit(1); | 
| 125 |  | } | 
| 126 |  | /* write new picture size */ | 
| 127 | < | fputresolu(YMAJOR|YDECR, yres, xres, stdout); | 
| 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   *inline; | 
| 146 | > | register COLR   *inln; | 
| 147 |  | register int    xoff, inx, iny; | 
| 148 |  | long    start, ftell(); | 
| 149 |  |  | 
| 150 | < | if ((inline = (COLR *)malloc(xres*sizeof(COLR))) == NULL) { | 
| 150 | > | if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) { | 
| 151 |  | fprintf(stderr, "%s: out of memory\n", progname); | 
| 152 |  | exit(1); | 
| 153 |  | } | 
| 158 |  | exit(1); | 
| 159 |  | } | 
| 160 |  | for (iny = yres-1; iny >= 0; iny--) { | 
| 161 | < | if (freadcolrs(inline, xres, fp) < 0) { | 
| 161 | > | if (freadcolrs(inln, xres, fp) < 0) { | 
| 162 |  | fprintf(stderr, "%s: read error\n", progname); | 
| 163 |  | exit(1); | 
| 164 |  | } | 
| 165 |  | for (inx = 0; inx < nrows && xoff+inx < xres; inx++) | 
| 166 | < | bcopy(inline[xoff+inx], scanbar[inx*yres+iny], | 
| 167 | < | 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) { | 
| 172 |  | exit(1); | 
| 173 |  | } | 
| 174 |  | } | 
| 175 | < | free((char *)inline); | 
| 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 |  | } |