Exemple #1
0
main(int   argc,
     char  **argv)
{
    extern int	    optind;	/* for use of getopt() */
    extern char	    *optarg;	/* for use of getopt() */
    int		    ch;		/* command-line option letter */

    int		    outarch = NATIVE;

    char	    **field_names = NULL;
    int		    num_fields = 0;
    int		    alloc_fields = 0;

    int		    rflag = NO;	/* -r option specified? */
    char	    *rrange;	/* arguments of -r option */
    long	    start_rec;	/* starting record number */
    long	    end_rec;	/* ending record number */
    long	    num_recs;	/* number of records to read
				   (0 means all up to end of file) */
    long	    num_read;	/* number of records actually read */

    int		    Aflag = NO;	/* annotate Ascii output? */
    Annot	    *annotate = NULL;
    int		    annwidth = 70;

    char	    *iname;	/* input file name */
    FILE	    *ifile;	/* input stream */
    struct header   *ihd;	/* input file header */
    struct fea_data *irec;	/* input record */

    char	    *oname;	/* output file name */
    FILE	    *ofile;	/* output stream */
    FieldList	    list;	/* output field list */
    int		    outord = TYPE_ORDER;
    FieldSpec	    **ofields;	/* output fields in field or type order */

    double	    rec_freq;
    double	    start_time_offset;
    char	    *fdata;
    double	    *edata;
    int		    type;
    int		    len, i;
    long	    dim[1];
    FieldSpec	    *field;

    FieldList	    source;	/* field list of source file */

    while ((ch = getopt(argc, argv, "a:f:r:x:A:F")) != EOF)
	switch (ch)
	{
	case 'a':
	    outarch = ((!strcmp(optarg, "EDR1")) ? EDR1
		       : (!strcmp(optarg, "EDR2")) ? EDR2
		       : (!strcmp(optarg, "NATIVE")) ? NATIVE
		       : (!strcmp(optarg, "ASCII")) ? ASCII
		       : UNKNOWN);
	    break;
	case 'f':
	    if (num_fields >= alloc_fields)
	    {
		size_t	size;

		alloc_fields = num_fields + 1 + num_fields/2;
		size = (alloc_fields + 1) * sizeof(char *);
		field_names = (char **)
		    ((field_names == NULL)
		     ? malloc(size)
		     : realloc(field_names, size));
	    }
	    field_names[num_fields++] = optarg;
	    field_names[num_fields] = NULL;
	    break;
	case 'r':
	    rflag = YES;
	    rrange = optarg;
	    break;
	case 'x':
	    debug_level = atoi(optarg);
	    break;
	case 'A':
	    Aflag = YES;
	    annwidth = atoi(optarg);
	    break;
	case 'F':
	    outord = FIELD_ORDER;
	    break;
	default:
	    SYNTAX;
	    break;
	}

    if (argc - optind > 2)
    {
	fprintf(stderr,
		"%s: too many file names specified.\n", ProgName);
	SYNTAX;
    }
    if (argc - optind < 2)
    {
	fprintf(stderr,
		"%s: too few file names specified.\n", ProgName);
	SYNTAX;
    }

    iname = eopen(ProgName,
		  argv[optind++], "r", FT_FEA, NONE, &ihd, &ifile);

    oname = argv[optind++];

    start_rec = 1;
    end_rec = LONG_MAX;
    num_recs = 0;

    if (rflag)
    {
	lrange_switch(rrange, &start_rec, &end_rec, 0);
	if (end_rec != LONG_MAX)
	    num_recs = end_rec - start_rec + 1;
    }

    REQUIRE(start_rec >= 1, "can't start before beginning of file");
    REQUIRE(end_rec >= start_rec, "empty range of records specified");

    if (debug_level)
	fprintf(stderr,
		"start_rec: %ld.  end_rec: %ld.  num_recs: %ld.\n",
		start_rec, end_rec, num_recs);

    DebugMsgLevel = debug_level;
    DebugMsgFunc = DebugPrint;

    irec = allo_fea_rec(ihd);

    REQUIRE(irec != NULL, "can't allocate memory for input record");

    list = fea_to_FieldList(ihd, irec, field_names, FALSE);

    REQUIRE(list != NULL,
	    "failure converting input header to field list");

    switch (outord)
    {
    case TYPE_ORDER:
	if (debug_level)
	    fprintf(stderr, "making type-ordered field array.\n");
	ofields = TypeOrder(list);
	break;
    case FIELD_ORDER:
	if (debug_level)
	    fprintf(stderr, "making field-ordered field array.\n");
	ofields = FieldOrder(list);
	break;
    default:
	REQUIRE(0, "output order neither TYPE_ORDER nor FIELD_ORDER");
	break;
    }

    if (debug_level)
	fprintf(stderr, "setting field ordering.\n");

    REQUIRE(SetFieldOrdering(&list, outord),
	    "can't set field ordering of output");

    rec_freq = get_genhd_val("record_freq", ihd, 1.0);

    type = genhd_type("record_freq", &len, ihd);

    if (type != HD_UNDEF)
    {
	field = AddGlobalField(&list, "recordFreq", 0, NULL, EDOUBLE, NULL);
	*(double *) field->data = rec_freq;
    }

    type = genhd_type("start_time", &len, ihd);

    if (type != HD_UNDEF && rec_freq != 0)
    {
	start_time_offset = (start_rec - 1) / rec_freq;

	fdata = (char *) get_genhd("start_time", ihd);
	edata = (double *) type_convert((long) len, fdata, type,
					(char *) NULL, DOUBLE,
					(void (*)()) NULL);

	if (start_time_offset != 0)
	{
	    for (i = 0; i < len; i++)
		edata[i] += start_time_offset;
	}

	dim[0] = len;
	AddGlobalField(&list, "startTime", 1, dim, EDOUBLE, edata);
    }

    source = fea_to_FieldList(ihd, NULL, NULL, TRUE);
    (void) AddSource(&list, 0, iname, source);

    (void) AddCommandLine(&list, GetCommandLine(argc, argv));

    if (debug_level)
	fprintf(stderr, "annwidth %d.\n", annwidth);

    if (Aflag)
    {
	annotate = (Annot *) malloc(sizeof(Annot));
	annotate->position = 0;
	annotate->indent = 0;
	annotate->width = annwidth;
	annotate->recnum = 0;
    }

    if (debug_level)
	fprintf(stderr, "writing Esignal header.\n");

    REQUIRE(OpenOut(oname, list, outarch, &ofile, annotate),
	    "write header failed");
    if (ofile == stdout)
	oname = "<stdout>";

    num_read = start_rec - 1;

    if (debug_level)
	fprintf(stderr, "skipping %ld records.\n", num_read);

/*    skiprec(ifile, num_read, size_rec(ihd)); */
    fea_skiprec(ifile, num_read, ihd);

    while (num_read++ < end_rec && get_fea_rec(irec, ihd, ifile) != EOF)
    {
	if (debug_level > 2)
	    fprintf(stderr, "Record number %ld read.\n", num_read);

	WriteRecord(ofields, outarch, ofile, annotate);
    }

    if (--num_read < end_rec && num_recs != 0)
	fprintf(stderr, "fea2esig: only %ld records read.\n",
		num_read - (start_rec - 1));

    exit(0);
    /*NOTREACHED*/
}
Exemple #2
0
main(int   argc,
     char  **argv)
{
    extern int	    optind;	/* for use of getopt() */
    extern char	    *optarg;	/* for use of getopt() */
    int		    ch;		/* command-line option letter */

    static char	    *ProgName = "esig2fea";	/* name of this program */
    static char	    *Version = SCCS_VERSION;	/* program SCCS version */
    static char	    *Date = SCCS_DATE;		/* program SCCS date */

    char	    **field_names = NULL;
    int		    num_fields = 0;
    int		    alloc_fields = 0;

    int		    rflag = NO;	/* -r option specified? */
    char	    *rrange;	/* arguments of -r option */
    long	    start_rec;	/* starting record number */
    long	    end_rec;	/* ending record number */
    long	    num_recs;	/* number of records to read
				   (0 means all up to end of file) */
    long	    num_read;	/* number of records actually read */

    char	    *iname;	/* input file name */
    FILE	    *ifile;	/* input stream */
    FieldList	    list;	/* input field list */
    int		    inord;	/* input: field order or type order? */
    FieldSpec	    **ifields;	/* input fields in field or type order */

    char	    *subtype = NULL;		/* FEA subtype name */
    int		    subtype_code = NONE;	/* numeric subtype code */
    FieldSpec	    *fld;	/* spec of various special fields */

    char	    *oname;	/* output file name */
    FILE	    *ofile;	/* output stream */
    struct header   *ohd;	/* output file header */
    struct fea_data *orec;	/* output record */
    int		    outord = TYPE_ORDER;

    char	    *version;	/* version from input preamble */
    int		    arch;	/* architecture from input preamble */
    long	    pre_size;	/* preamble size */
    long	    hdr_size;	/* header size (bytes) from preamble */
    long	    rec_size;	/* record size from preamble */

    double	    rec_freq;
    double	    start_time_offset;
    double	    *data;
    long	    len, i;


    struct header   *source;	/* embedded source-file header */


    while ((ch = getopt(argc, argv, "f:r:x:FT:")) != EOF)
	switch (ch)
	{
	case 'f':
	    if (num_fields >= alloc_fields)
	    {
		size_t	size;

		alloc_fields = num_fields + 1 + num_fields/2;
		size = (alloc_fields + 1) * sizeof(char *);
		field_names = (char **)
		    ((field_names == NULL)
		     ? malloc(size)
		     : realloc(field_names, size));
	    }
	    field_names[num_fields++] = optarg;
	    field_names[num_fields] = NULL;
	    break;
	case 'r':
	    rflag = YES;
	    rrange = optarg;
	    break;
	case 'x':
	    debug_level = atoi(optarg);
	    break;
	case 'F':
	    outord = FIELD_ORDER;
	    break;
	case 'T':
	    subtype = optarg;
	    break;
	default:
	    SYNTAX;
	    break;
	}

    if (argc - optind > 2)
    {
	fprintf(stderr,
		"%s: too many file names specified.\n", ProgName);
	SYNTAX;
    }
    if (argc - optind < 2)
    {
	fprintf(stderr,
		"%s: too few file names specified.\n", ProgName);
	SYNTAX;
    }


    DebugMsgLevel = debug_level;
    DebugMsgFunc = DebugPrint;

    iname = argv[optind++];
    list = OpenIn(iname, &version,
		  &arch, &pre_size, &hdr_size, &rec_size, &ifile);
    REQUIRE(list != NULL, "read header failed");
    if (ifile == stdin)
	iname = "<stdin>";

    oname = argv[optind++];

    start_rec = 0;
    end_rec = LONG_MAX;
    num_recs = 0;	/* 0 means continue to end of file */

    if (rflag)
    {
	lrange_switch(rrange, &start_rec, &end_rec, 0);
	if (end_rec != LONG_MAX)
	    num_recs = end_rec - start_rec + 1;
    }

    REQUIRE(start_rec >= 0, "can't start before beginning of file");
    REQUIRE(end_rec >= start_rec, "empty range of records specified");

    if (debug_level)
	fprintf(stderr,
		"start_rec: %ld.  end_rec: %ld.  num_recs: %ld.\n",
		start_rec, end_rec, num_recs);

    REQUIRE(GetFieldOrdering(list, &inord),
	    "cant get field ordering of input");

    switch (inord)
    {
    case TYPE_ORDER:
	if (debug_level)
	    fprintf(stderr, "making type-ordered field array.\n");
	ifields = TypeOrder(list);
	break;
    case FIELD_ORDER:
	if (debug_level)
	    fprintf(stderr, "making field-ordered field array.\n");
	ifields = FieldOrder(list);
	break;
    default:
	REQUIRE(0, "input order neither TYPE_ORDER nor FIELD_ORDER");
	break;
    }

    ohd = FieldList_to_fea(list, &orec, field_names, FALSE);

    REQUIRE(ohd != NULL,
	    "failure converting input field list to header & record struct");

    if (subtype != NULL)
    {
	subtype_code = lin_search(fea_file_type, subtype);

	if (subtype_code == -1)
	    fprintf(stderr, "%s: unknown FEA file subtype \"%s\" ignored.\n",
		    ProgName, subtype);
	else
	    ohd->hd.fea->fea_type = subtype_code;
    }

    if (outord == FIELD_ORDER)
	ohd->hd.fea->field_order = YES;

    fld = FindField(list, "recordFreq");

    if (fld != NULL && fld->occurrence == GLOBAL && fld->data != NULL)
    {
	(void) type_convert(1L, (char *) fld->data, ElibTypeToEsps(fld->type),
			    (char *) &rec_freq, FDOUBLE, (void (*)()) NULL);

	*add_genhd_d("record_freq", NULL, 1, ohd) = rec_freq;
    }
    else
	rec_freq = 1.0;

    fld = FindField(list, "startTime");

    if (fld != NULL
	&& fld->occurrence == GLOBAL && fld->data != NULL && rec_freq != 0)
    {
	start_time_offset = start_rec / rec_freq;

	len = FieldLength(fld);
	data = (double *) type_convert(len, (char *) fld->data,
				       ElibTypeToEsps(fld->type),
				       (char *) NULL, FDOUBLE,
				       (void (*)()) NULL);

	if (start_time_offset != 0)
	{
	    for (i = 0; i < len; i++)
		data[i] += start_time_offset;
	}

	(void) add_genhd_d("start_time", data, len, ohd);
    }

    (void) strcpy(ohd->common.prog, ProgName);
    (void) strcpy(ohd->common.vers, Version);
    (void) strcpy(ohd->common.progdate, Date);

    source = FieldList_to_fea(list, NULL, NULL, TRUE);
    add_source_file(ohd, savestring(iname), source);

    add_comment(ohd, get_cmd_line(argc, argv));

    oname = eopen(ProgName,
		  oname, "w", NONE, NONE, NULL, &ofile);
    write_header(ohd, ofile);

    num_read = SkipRecs(ifile, start_rec,
			RecordSize(list, arch), ifields, arch);

    if (num_read != start_rec)
    {
	fprintf(stderr,
		"%s: couldn't reach starting record; only %ld skipped.\n",
		ProgName, num_read);
	exit(0);
    }

    for ( ;
	 num_read <= end_rec && ReadRecord(ifields, arch, ifile);
	 num_read++)
    {
	put_fea_rec(orec, ohd, ofile);
    }

    if (num_read <= end_rec && num_recs != 0)
	fprintf(stderr, "esig2fea: only %ld records read.\n",
		num_read - start_rec);

    exit(0);
    /*NOTREACHED*/
}
DoubleAdapter::DoubleAdapter(const ImagePtr image) :
	ConstImageAdapter<double>(image->size()), _image(image) {
	ConstImageAdapter<double>	*adapter = type_convert(image);
	doubleimage = std::shared_ptr<ConstImageAdapter<double> >(adapter);
}
Exemple #4
0
/**********************************    T2Fit     ************************************
*   This is where the work will be done
*************************************************************************************/
OSErr T2Fit( FILE *inFile, FILE *outFile )
{
	OSErr   error = noErr;
	long    theTimePt;
	int     i;
	float   *inData, *outData, *T2, *intercept, *chi2, *Smoother;
	long    VolSize, outVolBytes, inVolBytes;
	float	te[ u.inIm.dim.timePts ];
	unsigned char *theMask;
	short   rules;

	u.NumOutImages   = 0;
	VolSize          = u.inIm.dim.isoX * u.inIm.dim.isoY * u.inIm.dim.n_slices;
	inVolBytes       = VolSize * get_datasize( T_FLOAT );
	outVolBytes      = VolSize * get_datasize( u.outIm.data_type );
	T2               = (float *)ck_malloc( inVolBytes,  "Storage for R2" );
	intercept        = (float *)ck_malloc( inVolBytes,  "Storage for intercept" );
	chi2             = (float *)ck_malloc( inVolBytes,  "Storage for chi2"  );
	outData          = (float *)ck_malloc( outVolBytes, "Storage for output data" );
	inData           = (float *)ck_malloc( inVolBytes * u.inIm.dim.timePts,  "Storage for input data" );
	theMask          = (unsigned char*)ck_malloc( VolSize * sizeof( unsigned char ),  "Storage for theMask" );
	if( u.Smooth ) {
		Smoother = (float *)ck_malloc( inVolBytes * 2, "Storage for smoothing" );
	}

// load up te vector
	if( u.Verbose ) {
		printf( "TE: " );
	}
	for( i=0; i<u.inIm.dim.timePts; i++ ) {
		fscanf(u.TEFile ,"%f", &te[i]);
		if( u.Verbose ) {
			printf( "%0.3f ", te[i] );
		}
	}
	
// We will do everything in float
	SetImDatatype( &u.inIm, T_FLOAT );

//	Load all of the images into a single matrix
	for ( theTimePt = 0; theTimePt < u.inIm.dim.timePts; theTimePt++ ) {
		error = GetSelectedVolume( inFile, &u.inIm, inData+theTimePt*VolSize, theTimePt );
		RETURNONERROR;
		if( u.Smooth ) {
			vmov( inData+theTimePt*VolSize, 1, Smoother, 1, VolSize, T_FLOAT );
			error = gaussSmooth( &u.inIm, Smoother, inData+theTimePt*VolSize, &u.xSmooth, &u.ySmooth, &u.zSmooth );
			RETURNONERROR;

		}
		RETURNONERROR;
	}
	

// threshold on first images
	if( u.autoThresh ) {
		error = autoMask( inData, theMask, VolSize, &u.threshold, &u.nonNoise, T_FLOAT );
		RETURNONERROR;
	} else if( u.threshold != 0 ) {
		u.nonNoise = ThreshMask( inData,  theMask, VolSize, &u.threshold, T_FLOAT );
	} else {
		u.nonNoise = VolSize;
	}

	error = CalcT2( &u.inIm, inData, theMask, te, T2, intercept, chi2 ); RETURNONERROR;

	free( inData );

	error = type_convert( T2, T_FLOAT,
						  outData, u.outIm.data_type,
						  VolSize, &u.outIm.theMinf, &u.outIm.theMaxf, &rules );
	if( error && error != CONVERSION_ERROR ) return error;
	error = ck_fwrite( outData, 1, outVolBytes, outFile );
	u.NumOutImages++;
	RETURNONERROR;

	error = type_convert( intercept, T_FLOAT,
						  outData, u.outIm.data_type,
						  VolSize, &u.outIm.theMinf, &u.outIm.theMaxf, &rules );
	if( error && error != CONVERSION_ERROR ) return error;
	error = ck_fwrite( outData, 1, outVolBytes, outFile );
	u.NumOutImages++;
	RETURNONERROR;

	if( u.Smooth ) {
		free( Smoother );
	}

// Make R2 image
	for( i=0; i<VolSize; i++ ) {
		if( T2[i] !=0 ) {
			T2[i] = 1/T2[i];
		}
	}
	error = type_convert( T2, T_FLOAT,
						  outData, u.outIm.data_type,
						  VolSize, &u.outIm.theMinf, &u.outIm.theMaxf, &rules );
	if( error && error != CONVERSION_ERROR ) return error;
	error = ck_fwrite( outData, 1, outVolBytes, outFile );
	u.NumOutImages++;
	RETURNONERROR;
	error = type_convert( chi2, T_FLOAT,
						  outData, u.outIm.data_type,
						  VolSize, &u.outIm.theMinf, &u.outIm.theMaxf, &rules );
	if( error && error != CONVERSION_ERROR ) return error;
	error = ck_fwrite( outData, 1, outVolBytes, outFile );
	u.NumOutImages++;
	RETURNONERROR;

	fprintf( u.ProcFile, "Output file contains maps of T2, M0, R2 and chi2\n" );
	fprintf( u.ProcFile, "TE's used: " );
	for( i=0; i<u.inIm.dim.timePts; i++ ) {
		fprintf( u.ProcFile,  "%0.3f ", te[i] );
	}
	fprintf( u.ProcFile, "\n" );
	fprintf( u.ProcFile, "Input data were intensity thresholded at %0.3f\n", u.threshold ); 
	fprintf( u.ProcFile, "Non-Noise Points:\t%ld\n", u.nonNoise );
	if( u.Smooth ) {
		fprintf( u.ProcFile, "Input images were smoothed with a width of %0.3f mm in x, %0.3f mm in y and %0.3f mm in z\n", 
			u.xSmooth, u.ySmooth, u.zSmooth );
	}
	fprintf( stderr, "Output file contains maps of T2, M0, R2 and chi2\n" );
	fprintf( stderr, "TE's used: " );
	for( i=0; i<u.inIm.dim.timePts; i++ ) {
		fprintf( stderr,  "%0.3f ", te[i] );
	}
	fprintf( stderr, "\n" );
	fprintf( stderr, "Input data were intensity thresholded at %0.3f\n", u.threshold ); 
	fprintf( stderr, "Non-Noise Points:\t%ld\n\n", u.nonNoise );
	if( u.Smooth ) {
		fprintf( stderr, "Input images were smoothed with a width of %0.3f mm in x, %0.3f mm in y and %0.3f mm in z\n", 
			u.xSmooth, u.ySmooth, u.zSmooth );
	}
	free( theMask );
	free( outData );
	free( chi2 );
	free( intercept );
	free( T2 );

	return error;
}