#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <pbm.h>
#include <pgm.h>
#include <ppm.h>
#include <assert.h>


/* rotate a .pgm picture from horizontal to vertical */

int main(argc, argv)
int argc;  char *argv[];
{   int cols, rows, i, j;   gray maxval;
    FILE *fp;
    gray **picin;  	/* incoming picture */
    gray **picout;	/* 90 deg rotated outgoing picture */

    pgm_init(&argc, argv);

    if (argc < 2 || strcmp(argv[1],"-")==0) fp = stdin; 
    else fp = fopen(argv[1],"r");
    if (fp == NULL) {printf("No %s file!!\n",argv[1]); exit(0); };
    picin = pgm_readpgm(fp, &cols, &rows, &maxval );
    fclose(fp);
    
    picout = pgm_allocarray(rows,cols);  /* rotated picture */

    for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) /* rotate */
      picout[j][i] = picin[rows-1-i][j];

    if (maxval==0) maxval=255;  /* some programs (ppmtopgm) neglect maxval,
				   this tries to fix it up for programs
				   (pgmtops) that need it */

    fp = stdout;
    pgm_writepgm(fp,picout,rows,cols,maxval,0);       /* and write it out */
    fclose(fp);

    exit(0);
};
