view tools/saitonap.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
/*
  saitonap.c            SAI to NAP format conversion.
 ------------

    Read a SAI header and a succession of frames.
    Write a NAP header, (created from the SAI header).
    Write each frame in NAP format.

*/


#include <stdio.h>
#include <math.h>
#include "header.h"
#include "options.h"
#include "units.h"
#include "strmatch.h"

char applic[]     = "SAI to NAP format conversion. " ;

static char *helpstr, *debugstr, *fstr, *headerstr ;

static Options option[] = {
    {   "help"      ,   "off"       ,  &helpstr     ,   "help"                          , DEBUG   },
    {   "debug"     ,   "off"       ,  &debugstr    ,   "debugging switch"              , DEBUG   },
    {   "frame"     ,   "min-max"   ,  &fstr        ,   "select frames inclusively"     , VAL     },
    {   "header"    ,   "on"        ,  &headerstr   ,   "nap header"                    , VAL     },
   ( char * ) 0 } ;


int     frameheight, framewidth ;       /* Sai parameters read from header */
int     frames ;

main(argc, argv)
int   argc ;
char *argv[] ;
{
    FILE   *fp ;
    int     i, framebytes, startbytes, frameshift;
    char   *header, *napheader, *SaiHeader();
    short  *frame  ;
    char   *val1, *val2 ;
    int     a,b ;

    fp = openopts( option,argc,argv ) ;
    if ( !isoff( helpstr ) )
	helpopts( helpstr, argv[0], applic, option ) ;

    if ( (header = ReadHeader(fp)) == (char *) 0 ) {
	fprintf(stderr,"saitonap: header not found\n");
	exit(1);
    }

    frameheight = HeaderInt( header, "frameheight" );
    framewidth =  HeaderInt( header, "framewidth"  );
    frames =      HeaderInt( header, "frames"      );

    framebytes = frameheight * framewidth * sizeof(short) ;


    /* Get limits on specified number of frames */

    if ( getvals( fstr, &val1, &val2, "-" ) == BADVAL ) {
	fprintf(stderr,"saitonap: bad frame selector [%s]\n", fstr ) ;
	exit( 1 ) ;
    }
    if      ( ismin( val1 ) ) a = 1 ;
    else if ( ismax( val1 ) ) a = frames ;
    else                      a = atoi( val1 ) ;

    if ( isempty( val2 ) )    b = a ;
    else if ( ismin( val2 ) ) b = 1 ;
    else if ( ismax( val2 ) ) b = frames ;
    else                      b = atoi( val2 ) ;

    if (a<=0 || b>frames || a>b) {
	fprintf(stderr,"saitonap: bad frame specifier \n" );
	exit(1);
    }


    /* Allocate space for framebytes of sai data */

    if ( ( frame = (short *)malloc( framebytes )) == NULL ) {
	fprintf(stderr,"saitonap: malloc out of space\n");
	exit(1);
    }

    if ( ison( headerstr ) )
	napheader = SaiHeader(header) ;

    /* Read and write framebytes of i/p sai data */

    for (i=1 ; i<a  &&  fread( frame,framebytes,1,fp ) ; i++)
	;

    if ( i < a ) {
	fprintf(stderr,"saitonap: insufficient data after header \n");
	exit(1);
    }

    if (b > 0)
	for (  ; i<=b && fread( frame,framebytes,1,fp ) ; i++)
	    writeframe( frame, napheader ) ;
    else
	while ( fread( frame,framebytes,1,fp ) )
	    writeframe( frame, napheader ) ;

    fclose(fp);
    fprintf(stderr,"saitonap done\n" ) ;

}



/* Write a frame in nap format */

writeframe( frame, header )
short  *frame ;
char   *header ;
{
    int    i, row, col;

    if ( ison( headerstr ) )
	WriteHeader( header, stdout ) ;
    for ( col=0 ; col < framewidth ; col++ )
	for ( row=0, i=col ; row < frameheight ; row++, i+=framewidth )
	    fwrite( &frame[i], sizeof(short), 1, stdout );
}



/*
   Copy the original sai header to a new nap header, changing in order:
     frames
     frameshift
     framewidth
     frameheight
     framebytes
   Then change applic name [gensai] to [gennap] in the Version string.
   Finally, update the new header_bytes, and return the new header.
*/

char *SaiHeader( SAIheader )
char *SAIheader ;
{
    char *NAPheader;
    char *p0, *p1, *p2, *s, str[64];

    NAPheader = (char *)malloc( strlen(SAIheader) + 64 ) ;

    p0 = NAPheader ;
    p1 = SAIheader ;


    /** copy up to frames **/

    p2 = HeaderString( SAIheader , "frames" ) ;
    while( p1 < p2 )
	*p0++ = *p1++ ;

    sprintf(str,"%d\n", framewidth);
    for (s = str ; *s != '\n' ; )
	*p0++ = *s++;
    *p0++ = *s;
    while (*p1 != '\n')
	*p1++;
    *p1++;


    /** copy up to frameshift **/

    p2 = HeaderString( SAIheader , "frameshift" ) ;
    while ( p1 < p2 )
	*p0++ = *p1++ ;

    sprintf(str,"%d\n", 1 );
    for (s = str ; *s != '\n' ; )
	*p0++ = *s++;
    *p0++ = *s;
    while (*p1 != '\n')
	*p1++;
    *p1++;


    /** copy up to framewidth **/

    p2 = HeaderString( SAIheader , "framewidth" ) ;
    while ( p1 < p2 )
	*p0++ = *p1++ ;

    sprintf(str,"%d\n", 1 );
    for (s = str ; *s != '\n' ; )
	*p0++ = *s++;
    *p0++ = *s;
    while (*p1 != '\n')
	*p1++;
    *p1++;


    /** copy up to frameheight **/

    p2 = HeaderString( SAIheader , "frameheight" ) ;
    while ( p1 < p2 )
	*p0++ = *p1++ ;

    sprintf(str,"%d\n", frameheight);
    for (s = str ; *s != '\n' ; )
	*p0++ = *s++;
    *p0++ = *s;
    while (*p1 != '\n')
	*p1++;
    *p1++;


    /** copy up to framebytes **/

    p2 = HeaderString( SAIheader , "framebytes" ) ;
    while ( p1 < p2 )
	*p0++ = *p1++ ;

    sprintf(str,"%d\n", frameheight * sizeof( short ) );
    for (s = str ; *s != '\n' ; )
	*p0++ = *s++;
    *p0++ = *s;
    while (*p1 != '\n')
	*p1++;
    *p1++;


    /** copy rest of header **/

    p2 = HeaderString( SAIheader , "Version" ) ;
    while ( p1 < p2 )
	*p0++ = *p1++ ;

    while (*p1 != ']')          /* change applic name [gensai] to [gennap] */
	*p0++ = *p1++ ;
    *(p0-3) = 'n' ;
    *(p0-2) = 'a' ;
    *(p0-1) = 'p' ;
    while (*p1 != '\n')
	*p0++ = *p1++ ;
    *p0++ = *p1++ ;


    /** update header_bytes **/

    sprintf(str, "%0*d", 7, p0-NAPheader);
    p0 = HeaderString( NAPheader , "header_bytes" ) ;
    s = str;
    while(*p0 != '\n')
	*p0++ = *s++ ;


    return NAPheader;
}