
/**************************************************************************
 *                                                                        *
 * Copyright (c) 1996 Michael Richmond and Richard Treffers               *
 *                                                                        *
 *                    This software may be copied and distributed for     *
 *                    educational, research and not for profit services   *
 *                    provided that this copyright and statement are      *
 *                    included in all such copies.                        *
 *                                                                        *
 **************************************************************************/


/*	
	flips the image about either rows or columns
	
		FLIP file [ROWS] [COLS]

	9/22/92 - usage, exit(0) -rrt
	10/13/93 - add outfile option
*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include "pcvista.h"
#include "fits.h"

#ifdef PROTO
int main(int, char **);
void fliprow(void);
void flipcol(void);
#else
void fliprow();
void flipcol();
#endif

static int ncol;
static int nrow;
static FITS_HANDLE fin, fout;
static int16 data1[NMAX], data2[NMAX];

#define MODE_NONE 0
#define MODE_ROWS 1
#define MODE_COLS 2
#define USAGE "usage: flip file_name  rows | cols [outfile=] [help]"

int
main(argc,argv)
int argc;
char *argv[];
{
	int i;
	int mode=MODE_NONE;
	char *gotit,outfile[NBUF];
	int new_file=0;

	if (argc < 2) {
		error(-1, USAGE);
	}
	if (strcmp(argv[1], "help") == 0) {
		printf("%s\n", USAGE);
		exit(0);
	}

	fin = fits_open(argv[1], "r", &nrow, &ncol);
	strcpy(outfile,argv[1]);

	for(i=2; i<argc; i++){
		if(keyword("help",argv[i])) {
			printf("%s\n",USAGE);
			exit(0);
		}
		if ((gotit = find("outfile", argv[i])) != NULL) {
			strcpy(outfile,gotit);
			new_file=1;
			continue;
		}
		if(keyword("rows",argv[i])){
			mode=MODE_ROWS;
			continue;
		}
		if(keyword("cols",argv[i])){
			mode=MODE_COLS;
			continue;
		}
		fprintf(stderr,"flip: unknown option '%s'\n",argv[i]);
		exit(1);
	}

	fout = fits_open(outfile, new_file?"w":"x", &nrow, &ncol);
		 
	if(new_file)
		fits_copyheader(fin,fout, FITS_CHECK);

	switch(mode){
		case MODE_ROWS:
			fliprow();
			break;
		case MODE_COLS:
			flipcol();
			break;
		default:
			fprintf(stderr,"flip:  either rows or cols must be specified\n");
			exit(1);
	}
	
	fits_close(fout);
	exit(0);
}

void
fliprow()
{
	int row1, row2;

	for(row1 = 0, row2 = nrow - 1; row1 < row2; row1++, row2--) {
		fits_get_data(fin, row1, 0, data1, ncol);
		fits_get_data(fin, row2, 0, data2, ncol);
		fits_put_data(fout, row1, 0, data2, ncol);
		fits_put_data(fout, row2, 0, data1, ncol);
	}
}

void
flipcol()
{
	int i, j, row, temp;

	for (row = 0; row < nrow; row++) {
		fits_get_data(fin, row, 0, data1, ncol);
		for (i = 0, j = ncol - 1; i < j; i++, j--) {
			temp = data1[i];
			data1[i] = data1[j];
			data1[j] = temp;
		}
		fits_put_data(fout, row, 0, data1, ncol);
	}
}
