Back to index

radiance  4R0+20100331
protate.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: protate.c,v 2.10 2004/04/30 20:29:24 greg Exp $";
00003 #endif
00004 /*
00005  * prot.c - program to rotate picture file 90 degrees clockwise.
00006  *
00007  *     2/26/88
00008  */
00009 
00010 #include "standard.h"
00011 
00012 #include "color.h"
00013 
00014 #include <time.h>
00015 
00016 #include "resolu.h"
00017 
00018 int    order;                      /* input scanline order */
00019 int    xres, yres;                 /* input resolution */
00020 
00021 int    correctorder = 0;           /* order correction? */
00022 int    ccw = 0;                    /* rotate CCW? */
00023 
00024 #ifdef SMLMEM
00025 char   buf[1<<20];                 /* output buffer */
00026 #else
00027 char   buf[1<<22];                 /* output buffer */
00028 #endif
00029 
00030 int    nrows;                      /* number of rows output at once */
00031 
00032 #define scanbar             ((COLR *)buf)
00033 
00034 char   *progname;
00035 
00036 short  ordertab[4][2] = {
00037        {0,0}, {XDECR,XDECR|YDECR}, {XDECR|YDECR,YDECR}, {YDECR,XDECR}
00038 };
00039 
00040 
00041 static int neworder(void);
00042 static void rotatecw(FILE *fp);
00043 static void rotateccw(FILE *fp);
00044 
00045 
00046 
00047 static int
00048 neworder(void)              /* return corrected order */
00049 {
00050        static short    ordercw[8];
00051        register int  i;
00052 
00053        if (correctorder)
00054               return(order);
00055        if (!ordercw[0]) {
00056               ordercw[YMAJOR|YDECR] = 0;
00057               ordercw[0] = YMAJOR|XDECR;
00058               ordercw[YMAJOR|XDECR] = XDECR|YDECR;
00059               ordercw[XDECR|YDECR] = YMAJOR|YDECR;
00060               ordercw[YMAJOR|XDECR|YDECR] = XDECR;
00061               ordercw[XDECR] = YMAJOR;
00062               ordercw[YMAJOR] = YDECR;
00063               ordercw[YDECR] = YMAJOR|XDECR|YDECR;
00064        }
00065        if (!ccw)
00066               return(ordercw[order]);
00067        for (i = 8; i--; )
00068               if (ordercw[i] == order)
00069                      return(i);
00070        fputs("Order botch!\n", stderr);
00071        exit(2);
00072 }
00073 
00074 int
00075 main(
00076        int    argc,
00077        char   *argv[]
00078 )
00079 {
00080        static char   picfmt[LPICFMT+1] = PICFMT;
00081        int    rval;
00082        FILE   *fin;
00083 
00084        progname = argv[0];
00085 
00086        while (argc > 2 && argv[1][0] == '-') {
00087               switch (argv[1][1]) {
00088               case 'c':
00089                      correctorder = 1;
00090                      break;
00091               case 'r':
00092                      ccw = 1;
00093                      break;
00094               default:
00095                      goto userr;
00096               }
00097               argc--; argv++;
00098        }
00099        if (argc != 2 && argc != 3)
00100               goto userr;
00101        if ((fin = fopen(argv[1], "r")) == NULL) {
00102               fprintf(stderr, "%s: cannot open\n", argv[1]);
00103               exit(1);
00104        }
00105        if (argc == 3 && freopen(argv[2], "w", stdout) == NULL) {
00106               fprintf(stderr, "%s: cannot open\n", argv[2]);
00107               exit(1);
00108        }
00109                                    /* transfer header */
00110        if ((rval = checkheader(fin, picfmt, stdout)) < 0) {
00111               fprintf(stderr, "%s: not a Radiance picture\n", progname);
00112               exit(1);
00113        }
00114        if (rval)
00115               fputformat(picfmt, stdout);
00116                                    /* add new header info. */
00117        fputs(progname, stdout);
00118        if (ccw) fputs(" -r", stdout);
00119        if (correctorder) fputs(" -c", stdout);
00120        fputs("\n\n", stdout);
00121                                    /* get picture size */
00122        if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
00123               fprintf(stderr, "%s: bad picture size\n", progname);
00124               exit(1);
00125        }
00126                                    /* write new picture size */
00127        fputresolu(neworder(), yres, xres, stdout);
00128                                    /* compute buffer capacity */
00129        nrows = sizeof(buf)/sizeof(COLR)/yres;
00130        if (ccw)                    /* rotate the image */
00131               rotateccw(fin);
00132        else
00133               rotatecw(fin);
00134        exit(0);
00135 userr:
00136        fprintf(stderr, "Usage: %s [-r][-c] infile [outfile]\n", progname);
00137        exit(1);
00138 }
00139 
00140 
00141 static void
00142 rotatecw(                   /* rotate picture clockwise */
00143        FILE   *fp
00144 )
00145 {
00146        register COLR *inln;
00147        register int  xoff, inx, iny;
00148        long   start, ftell();
00149 
00150        if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
00151               fprintf(stderr, "%s: out of memory\n", progname);
00152               exit(1);
00153        }
00154        start = ftell(fp);
00155        for (xoff = 0; xoff < xres; xoff += nrows) {
00156               if (fseek(fp, start, 0) < 0) {
00157                      fprintf(stderr, "%s: seek error\n", progname);
00158                      exit(1);
00159               }
00160               for (iny = yres-1; iny >= 0; iny--) {
00161                      if (freadcolrs(inln, xres, fp) < 0) {
00162                             fprintf(stderr, "%s: read error\n", progname);
00163                             exit(1);
00164                      }
00165                      for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
00166                             copycolr(scanbar[inx*yres+iny],
00167                                           inln[xoff+inx]);
00168               }
00169               for (inx = 0; inx < nrows && xoff+inx < xres; inx++)
00170                      if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
00171                             fprintf(stderr, "%s: write error\n", progname);
00172                             exit(1);
00173                      }
00174        }
00175        free((void *)inln);
00176 }
00177 
00178 
00179 static void
00180 rotateccw(                  /* rotate picture counter-clockwise */
00181        FILE   *fp
00182 )
00183 {
00184        register COLR *inln;
00185        register int  xoff, inx, iny;
00186        long   start, ftell();
00187 
00188        if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) {
00189               fprintf(stderr, "%s: out of memory\n", progname);
00190               exit(1);
00191        }
00192        start = ftell(fp);
00193        for (xoff = xres-1; xoff >= 0; xoff -= nrows) {
00194               if (fseek(fp, start, 0) < 0) {
00195                      fprintf(stderr, "%s: seek error\n", progname);
00196                      exit(1);
00197               }
00198               for (iny = 0; iny < yres; iny++) {
00199                      if (freadcolrs(inln, xres, fp) < 0) {
00200                             fprintf(stderr, "%s: read error\n", progname);
00201                             exit(1);
00202                      }
00203                      for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
00204                             copycolr(scanbar[inx*yres+iny],
00205                                           inln[xoff-inx]);
00206               }
00207               for (inx = 0; inx < nrows && xoff-inx >= 0; inx++)
00208                      if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) {
00209                             fprintf(stderr, "%s: write error\n", progname);
00210                             exit(1);
00211                      }
00212        }
00213        free((void *)inln);
00214 }