view saitools/sairotate.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
line wrap: on
line source
/*
    Copyright (c) Applied Psychology Unit, Medical Research Council. 1993
    ===========================================================================

    Permission to use, copy, modify, and distribute this software without fee 
    is hereby granted for research purposes, provided that this copyright 
    notice appears in all copies and in all supporting documentation, and that 
    the software is not redistributed for any fee (except for a nominal 
    shipping charge). Anyone wanting to incorporate all or part of this 
    software in a commercial product must obtain a license from the Medical 
    Research Council.

    The MRC makes no representations about the suitability of this 
    software for any purpose.  It is provided "as is" without express or 
    implied warranty.
 
    THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
    ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL 
    THE A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES 
    OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 
    ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 
    SOFTWARE.
*/
/*    sairotate.c
*   ------------
*
* 'Rotates' a .sai frame by 90degrees clockwise, so the horizontal
* scale is frequency, vertical image-time.
*   
* If -R is specified, then put the trigger at the top, so ImageTime goes
*                     down. This is equiv to a 270degree rotation pluse
*                     a refelction about x=midpoint axis.
*
* If -T is specified, then start drawing from the "beginning" of the 
*                     TriggerPeak (defined as the right nextevent).
*
*
*  M Akeroyd.  June 1993. v1.00  Revised Spring 1994.
*/



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "tip.h"




/* Function declarations */
/*-------------------------------------------------------------------------*/

void parsecommandline (int argc, char *argv[], char inputfn[], char outputfn[]);
int readheader(FILE *inputfp);
void checkheader();
void writeheader(FILE *outputfp);
void changeheader(int no_frames, int new_pwidth, int new_nwidth, int framewidthsamples_output);
void changeheader_B(int new_mincf, int new_maxcf, int new_channels);

void writedata_output (FILE *outputfp, int samples_to_write);
FILE *open_file (char filefn[], FILE *dir_default, int streamtype);

int findpeaksreverse (); 
int find_trigger(int frame, int channel, int total_peaks);


/* Data Arrays */
/* ------------------------------------------------------------------------*/
short inputdata[MAX_DATA];
short inputbychannel[MAX_CHANNELS][MAX_DATA];
short outputfiguredata[MAX_DATA];
short outputgrounddata[MAX_DATA];


/* other variables */
/*-------------------------------------------------------------------------*/

struct Peak peak[MAX_PEAKS];
struct NextEvent nextevent[MAX_NEXTEVENTS];


/* variables read from header */
/*-------------------------------------------------------------------------*/
char header[MAX_LINES_HEADER][MAX_LINE_LENGTH];
int header_lines;

/* .sai parameters */
int no_frames;
int frameheight;             /* number of channels */
int framewidth_samples;      /* pwidth + nwidth * samplerate */
int frameshift_samples;      /* frstep_aid * samplerate */
int pwidth;                  /* in msecs */
int nwidth;                  /* in msecs: NEGATIVE */
int width_win;               /* pixels */
int height_win;              /* pixels */
long samplerate;             /* samples per sec */
int mincf;                   /* Hz */
int maxcf;                   /* Hz */

/* parameters read from command line*/
int framestart;
int frameend;
int minchannel;
int maxchannel;


/* misc */
/*-----------------------------------------------------------------------*/

char progname[MAX_STRING_LENGTH];
char outputfigurefn[MAX_STRING_LENGTH];

int verboseflag = OFF;               /* -v */
int trigger_bottomflag = ON;         /* -R ("lots of rotation" */
int triggerflag = OFF;               /* -T */
int greyscaleflag = OFF;             /* Not used: required for compilation */
int oppositearchflag = OFF;          /* -oparch : see saigraph.c for info */

/* ...................         Main         ................................*/
/* .........................................................................*/
/* .........................................................................*/



void main (int argc, char *argv[])
{
  int n, sample;
  int frame, channel;
  int trigger_peak = 0;
  int header_bytes = 0;
  int cut_framestart, cut_frameend, cut_no_frames;
  int cut_minchannel, cut_maxchannel, cut_frameheight;
  char inputfn[MAX_STRING_LENGTH],  outputbasefn[MAX_STRING_LENGTH];
  FILE *inputfp, *outputfigurefp;
  int total_peaks;


/*-------------------------------------*/
  strcpy(progname, argv[0]);
  strcpy(inputfn, "");
  strcpy(outputbasefn, "");
  strcpy(outputfigurefn, "");

  parsecommandline(argc, argv, inputfn, outputbasefn);

  if (strcmp(outputbasefn, "") == 0) 
    strcpy(outputfigurefn, "");
  else {
    strcpy(outputfigurefn, outputbasefn);
/*    strcat(outputfigurefn, OUTPUT_EXT);*/
  }

/*--------------------------------------*/
  /* open files, read and check header */
  inputfp = open_file(inputfn, stdin, READ);
  outputfigurefp = open_file(outputfigurefn, stdout, WRITE);
  header_bytes = readheader(inputfp);
  checkheader();

/*--------------------------------------*/

  /* reset the frame/channel counters 
   * framewidth -> frameheight 
   * framehieght -> framewidth
   * (it seems that onlr framewidth has an affect on 'gensai -use':
   * pwidth/nwidth, nor mincf/maxcf don't do anything)
   */

  changeheader(no_frames, pwidth, nwidth, frameheight); 
  changeheader_B(mincf, maxcf, framewidth_samples);

  writeheader(outputfigurefp);

/*--------------------------------------*/

  if  (verboseflag==ON) { 
    fprintf(stderr, " frames "); 
    fflush(stderr);}

/*------------------------------------------------------------------------*/

  for (frame=1; frame<=no_frames; frame++) {
    if  (verboseflag==ON) {
	fprintf(stderr, " %i ", frame); fflush(stderr);}

    /*--------------------------------------*/

    for (channel=1; channel <=frameheight; channel ++) {
      
      for(n=0; n < MAX_PEAKS; n++) { 
        peak[n].tent = UNSET;
        nextevent[n].type = UNSET;}

      for(sample=0; sample<framewidth_samples; sample++) 
	inputdata[sample] = 0;

      fread (inputdata, 2, framewidth_samples, inputfp);

      /* input check */
      for(sample=0; sample<framewidth_samples; sample++)
	if (inputdata[sample] < 0 )  {
	  fprintf(stderr, "%s: something's gone wrong: the data is negative.\n", progname);
	  exit(-1); }

      if (triggerflag == ON) {
	/* find peaks */
	total_peaks = findpeaksreverse();
	/* find the trigger peak */
	trigger_peak = 0;
	trigger_peak = find_trigger(frame, channel, total_peaks);
	/* from its rightwards nextevent onwards, set everything to zero.*/
	for (sample = nextevent[trigger_peak-1].sample; sample<framewidth_samples; sample++)
	  inputdata[sample] = 0;
      }
	
      for(sample=0; sample<framewidth_samples; sample++) 
	inputbychannel[channel][sample] = inputdata[sample];
     
    } /* (loading of) channel */

    /*--------------------------------------*/

    /* Write output */
    if (trigger_bottomflag == ON ) {
      /* write this frame, rotated through 90 degrees 
       * Trigger goes at top */
      for(sample= framewidth_samples-1; sample >= 0; sample--) {
	for(channel = 1; channel <= frameheight; channel++) 
	  outputfiguredata[(channel-1)] = inputbychannel[channel][sample];
	writedata_output(outputfigurefp, frameheight); } }
    else {
      /* write this frame, rotated through 270 degrees (but with a mirror on 
       * centre freq as well) 
       * Trigger goes at bottom */
      for(sample=0; sample<framewidth_samples; sample++) {
	for(channel = 1; channel <= frameheight; channel++) 
	  outputfiguredata[(channel-1)] = inputbychannel[channel][sample];
	writedata_output(outputfigurefp, frameheight); }
    }


    /*--------------------------------------*/

  } /* frame */

/*----------------------------------------------------------------------*/

  /* Tidy up and exit */
  fprintf(stderr, "\n");
  fclose(inputfp);   fclose(outputfigurefp); 

  if ( ferror(inputfp) != 0) {
    fprintf(stderr, " %s : error closing input file.\n", progname);
    exit(-1);}

  if ( ferror(outputfigurefp) != 0) {
    fprintf(stderr, " %s : error closing figure file.\n", progname);
    exit(-1);}

  exit(0);

} /* Main */




/*......................................................................*/
/*......................................................................*/





void parsecommandline(int argc, char *argv[], char inputfn[], char outputbasefn[])
{
  int x=1, helpflag = OFF;

  while (x < argc){
    if      (!strcmp(argv[x], "-o")) {strcpy(outputbasefn, argv[x+1]); x+=2;}
    else if (!strcmp(argv[x], "-output")) {strcpy(outputbasefn, argv[x+1]); x+=2;}
    else if (!strcmp(argv[x], "-i")) {strcpy(inputfn, argv[x+1]); x+=2;}
    else if (!strcmp(argv[x], "-input")) {strcpy(inputfn, argv[x+1]); x+=2;}
    else if (!strcmp(argv[x], "-help")) {helpflag = ON; x+=1;}
    else if (!strcmp(argv[x], "-h")) {helpflag = ON; x+=1;}
    else if (!strcmp(argv[x], "-verbose")) {verboseflag = ON; x+=1;}
    else if (!strcmp(argv[x], "-v")) {verboseflag = ON; x+=1;}
    else if (!strcmp(argv[x], "-T")) {triggerflag = ON; x+=1;}
    else if (!strcmp(argv[x], "-oparch")) {oppositearchflag = ON; x+=1;}
    else if (!strcmp(argv[x], "-R")) {trigger_bottomflag = OFF; x+=1;}
    else {fprintf(stderr, "%s: unknown option %s\n", progname, argv[x]);
	  exit(-1);}
  }
  
  if (helpflag == ON)
    {
      fprintf(stderr, "\n");
      fprintf(stderr, "                         %s\n", progname);
      fprintf(stderr, " -----------------------------------------------------------\n");
      fprintf(stderr, " Rotates / reflects  a .sai. Default is start at nwidth=max,\n");
      fprintf(stderr, " and put the trigger at the bottom (90 deg rotation)\n");
      fprintf(stderr, "\n");
      fprintf(stderr, " The -oparch option is REQUIRED if reading a Sun .sai files \n");
      fprintf(stderr, " on a DEC cpu or vice-versa.\n");
      fprintf(stderr, " Add .sai to both input and output filenames.\n");
      fprintf(stderr, " -----------------------------------------------------------\n");
      fprintf(stderr, " -i <.sai file>      input file             default = stdin\n");
      fprintf(stderr, " -o <.sai file>      output file            default = stdout\n");
      fprintf(stderr, " -oparch          assume input was generated on an opposing architecture cpu\n");
      fprintf(stderr, "\n");
      fprintf(stderr, " -T                  start at the TriggerPeak\n");
      fprintf(stderr, " -R                  put Trigger at top (270 deg)\n");
      fprintf(stderr, "\n");
      fprintf(stderr, " -v                  verbose output (to stderr)\n");
      fprintf(stderr, "\n\n" );
      exit(-1);}

}