Exemplo n.º 1
0
OSErr writeHeader( IMAGE *im, char *basename )
/* creates an analyze style header based on the struct im */
{
	FILE  *f;
	char  buff[256];
	dsr   theDSR;
	OSErr error = noErr;
	
	sprintf( buff, "%s.hdr", basename );
	f = fopen( buff, "wb" );
	if( !f ) {
		return WRITE_ERROR;
	}
	error = EmptyAnaHdr( &theDSR, "From Signa", im->dim.scanspacing, im->dim.x,
				             im->dim.y, im->dim.n_slices, im->dim.timePts, axial );
		if( error ) ILError( error, "Creating analyze header" );

	if( pcByteOrder() ) {
		error = swapHdrBytes( &theDSR );
			if( error ) ILError( error, "swapping header bytes" );
		printf( "\nWARNING: Computer and data differ in byte order\n" );
	}
	
	error = ck_fwrite( &theDSR, sizeof( dsr ), 1, f );
		if( error ) ILError( error, buff );
		
	ck_fclose( f );
	return error;
}
Exemplo n.º 2
0
/******************************    DoSwap     ***************************
*   This is where the work will be done
*************************************************************************************/
OSErr DoSwap( FILE *inFile, FILE *outFile )
{
	OSErr   error = noErr;
	long    theTimePt;
	void    *inData, *outData;
	long    bufSize;
	int     elemSize;
	void    *buf;
	int     count;

	elemSize = sizeof( float );
	bufSize = u.id.xs * u.id.ys * elemSize;
	buf = ck_malloc( bufSize, "swap buffer" );

	for( count=0; count<u.id.zs; count++ ) {
		if( u.Verbose ) {
			printf( "." );
		}
		error = ck_fread( buf, sizeof( char ), bufSize, inFile );
		ILError( error, "DoSwap" );

		error = vbyteswap( buf, u.id.xs * u.id.ys, T_FLOAT );
		ILError( error, "DoSwap" );

		error = ck_fwrite( buf, sizeof( char ), bufSize, outFile );
		ILError( error, "DoSwap" );
	}

	return error;
}
Exemplo n.º 3
0
Arquivo: sdiff.c Projeto: 0mp/freebsd
/* Advance LINES on LF's infile, copying lines to OUTFILE */
static void
lf_copy (struct line_filter *lf, lin lines, FILE *outfile)
{
  char *start = lf->bufpos;

  while (lines)
    {
      lf->bufpos = (char *) memchr (lf->bufpos, '\n', lf->buflim - lf->bufpos);
      if (! lf->bufpos)
	{
	  ck_fwrite (start, lf->buflim - start, outfile);
	  if (! lf_refill (lf))
	    return;
	  start = lf->bufpos;
	}
      else
	{
	  --lines;
	  ++lf->bufpos;
	}
    }

  ck_fwrite (start, lf->bufpos - start, outfile);
}
Exemplo n.º 4
0
int main( int argc, char **argv )
{
	IMAGE im;
	dsr   myDSR;
	short tmp;
	FILE  *fp;
	char  inName[256], outName[256];
	OSErr error = noErr;

	printf( "%s - Treat times points in an analyze header as slices\n", argv[0] );
	printf( "WARNING -- this tool will overwrite the original header\n" );

	if( argc == 1 ) {
		printf( "Enter the name of an analyze image file ( xxx.img ): " );
		scanf( "%s", inName );
	} else {
		strcpy( inName, argv[1] );
	}

	error = UC_Readheader( inName, &im );
	ILError( error, "reading input header" );

	tmp = im.dim.n_slices;
	im.dim.n_slices = im.dim.timePts;
	im.dim.timePts = tmp;

	error = dsrFromIMAGE( &im, &myDSR );
	ILError( error, "Creating analyze header" );

	strcpy( outName, inName );
	sprintf( strrchr( outName, '.' ), ".hdr" );

	printf( "Header will be overwritten to %s\n", outName );

	error = ck_fopen( &fp, outName, "wb" );
	ILError( error, "opening output header" );

	error = ck_fwrite( &myDSR, sizeof( dsr ), 1, fp );
	ILError( error, "writing analyze header" );

	error = ck_fclose( fp );
	ILError( error, "closing header file" );

	return 0;
}
Exemplo n.º 5
0
Arquivo: sdiff.c Projeto: 0mp/freebsd
/* interpret an edit command */
static bool
edit (struct line_filter *left, char const *lname, lin lline, lin llen,
      struct line_filter *right, char const *rname, lin rline, lin rlen,
      FILE *outfile)
{
  for (;;)
    {
      int cmd0, cmd1;
      bool gotcmd = false;

      cmd1 = 0; /* Pacify `gcc -W'.  */

      while (! gotcmd)
	{
	  if (putchar ('%') != '%')
	    perror_fatal (_("write failed"));
	  ck_fflush (stdout);

	  cmd0 = skip_white ();
	  switch (cmd0)
	    {
	    case '1': case '2': case 'l': case 'r':
	    case 's': case 'v': case 'q':
	      if (skip_white () != '\n')
		{
		  give_help ();
		  flush_line ();
		  continue;
		}
	      gotcmd = true;
	      break;

	    case 'e':
	      cmd1 = skip_white ();
	      switch (cmd1)
		{
		case '1': case '2': case 'b': case 'd': case 'l': case 'r':
		  if (skip_white () != '\n')
		    {
		      give_help ();
		      flush_line ();
		      continue;
		    }
		  gotcmd = true;
		  break;
		case '\n':
		  gotcmd = true;
		  break;
		default:
		  give_help ();
		  flush_line ();
		  continue;
		}
	      break;

	    case EOF:
	      if (feof (stdin))
		{
		  gotcmd = true;
		  cmd0 = 'q';
		  break;
		}
	      /* Fall through.  */
	    default:
	      flush_line ();
	      /* Fall through.  */
	    case '\n':
	      give_help ();
	      continue;
	    }
	}

      switch (cmd0)
	{
	case '1': case 'l':
	  lf_copy (left, llen, outfile);
	  lf_skip (right, rlen);
	  return true;
	case '2': case 'r':
	  lf_copy (right, rlen, outfile);
	  lf_skip (left, llen);
	  return true;
	case 's':
	  suppress_common_lines = true;
	  break;
	case 'v':
	  suppress_common_lines = false;
	  break;
	case 'q':
	  return false;
	case 'e':
	  {
	    int fd;

	    if (tmpname)
	      tmp = fopen (tmpname, "w");
	    else
	      {
		if ((fd = temporary_file ()) < 0)
		  perror_fatal ("mkstemp");
		tmp = fdopen (fd, "w");
	      }

	    if (! tmp)
	      perror_fatal (tmpname);

	    switch (cmd1)
	      {
	      case 'd':
		if (llen)
		  {
		    if (llen == 1)
		      fprintf (tmp, "--- %s %ld\n", lname, (long int) lline);
		    else
		      fprintf (tmp, "--- %s %ld,%ld\n", lname,
			       (long int) lline,
			       (long int) (lline + llen - 1));
		  }
		/* Fall through.  */
	      case '1': case 'b': case 'l':
		lf_copy (left, llen, tmp);
		break;

	      default:
		lf_skip (left, llen);
		break;
	      }

	    switch (cmd1)
	      {
	      case 'd':
		if (rlen)
		  {
		    if (rlen == 1)
		      fprintf (tmp, "+++ %s %ld\n", rname, (long int) rline);
		    else
		      fprintf (tmp, "+++ %s %ld,%ld\n", rname,
			       (long int) rline,
			       (long int) (rline + rlen - 1));
		  }
		/* Fall through.  */
	      case '2': case 'b': case 'r':
		lf_copy (right, rlen, tmp);
		break;

	      default:
		lf_skip (right, rlen);
		break;
	      }

	    ck_fclose (tmp);

	    {
	      int wstatus;
	      int werrno = 0;
	      ignore_SIGINT = true;
	      checksigs ();

	      {
#if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK)
		char *command =
		  xmalloc (quote_system_arg (0, editor_program)
			   + 1 + strlen (tmpname) + 1);
		sprintf (command + quote_system_arg (command, editor_program),
			 " %s", tmpname);
		wstatus = system (command);
		if (wstatus == -1)
		  werrno = errno;
		free (command);
#else
		pid_t pid;

		pid = vfork ();
		if (pid == 0)
		  {
		    char const *argv[3];
		    int i = 0;

		    argv[i++] = editor_program;
		    argv[i++] = tmpname;
		    argv[i] = 0;

		    execvp (editor_program, (char **) argv);
		    _exit (errno == ENOENT ? 127 : 126);
		  }

		if (pid < 0)
		  perror_fatal ("fork");

		while (waitpid (pid, &wstatus, 0) < 0)
		  if (errno == EINTR)
		    checksigs ();
		  else
		    perror_fatal ("waitpid");
#endif
	      }

	      ignore_SIGINT = false;
	      check_child_status (werrno, wstatus, EXIT_SUCCESS,
				  editor_program);
	    }

	    {
	      char buf[SDIFF_BUFSIZE];
	      size_t size;
	      tmp = ck_fopen (tmpname, "r");
	      while ((size = ck_fread (buf, SDIFF_BUFSIZE, tmp)) != 0)
		{
		  checksigs ();
		  ck_fwrite (buf, size, outfile);
		}
	      ck_fclose (tmp);
	    }
	    return true;
	  }
	default:
	  give_help ();
	  break;
	}
    }
}
Exemplo n.º 6
0
int G2A (int argc, char *argv[] )
{
	char origname[128];	// original name
	char newname [128];	// renumbered name
	char command [255];	// command buffer
	char *TempName;
	char *numstring;
	char ImageName[128];
	int  counter = 0;
	int  number;
	char *p;
	char nameFileName[255];
	char analyzeFileName[255];
	char headerFileName[255];
	char baseName[255];
	short verbose = true;
	FILE *nameFile;
	FILE *TempFile;
	FILE *dataFile;
	FILE *procFile;
	FILE *headerFile;
	FILE *inFile;
	int  i;
	IMAGE im;
	short count;
	short *dataBuff;
	Boolean DebugMode = false;
	
	static char id[] = "$Revision: 1.21 $$Date: 2006/06/09 18:40:34 $";
	OSErr error = noErr;

	if( argc < 2 ) {
		print_usage( argv[0] );
	}

// setup an output file name, based on the last input file name
	sprintf( baseName, "%s", argv[argc-1] );	// output file name

	p = strrchr( baseName, 'I' );
	*p = '\0';	// null terminate
	
	error = OpenProcFile( argc, argv, baseName, id, &procFile );
	ILError( error, "Opening Proc file" );
	
	sprintf( nameFileName, "%s.NAME_FILE", baseName );
	fprintf( procFile, "Associated name file ... %s\n", nameFileName );
	sprintf( headerFileName, "%s.GenesisHdrs", baseName );
	fprintf( procFile, "Associated headers file ... %s\n", headerFileName );

// corrected names will be stored temporarily in nameFile	
	error = CreateTempFile( &TempFile, &TempName );
	ILError( error, TempName );
	
// use UC_ReadHeader to determine image dimensions, etc...
	error = UC_Readheader( argv[argc-1], &im );
	if( error ) ILError( error, "Read Image" );

// rename the files and create name list into the file called TempName
   for( i=argc-1; i>0; i-- )
   {
		strcpy( origname, argv[i] );
		strcpy( newname, argv[i] );

		numstring = strrchr( newname, 'I' ) + 1;
		number = atoi( numstring );


		sprintf( numstring, "%0.3d.MR", number );

		fprintf( TempFile, "%s\n", newname );

		if( strcmp( origname, newname ) || DebugMode ) {
			if( verbose ) {
			    printf( "i:%d\t%s --> %s\n", i, origname, newname );
			}
		    sprintf( command, "mv %s %s\n", origname, newname );
		    fprintf( procFile, "\tRenamed %s to %s\n", origname, newname );
		    system( command );
		}
	}
	error = ck_fclose( TempFile );
	ILError( error, TempName );

// Sort the contents of the temporary nameFile and place it into the permanent version
	sprintf( command, "sort %s > %s", TempName, nameFileName );
	system( command );

	if( false ) {
		DBG( command );
		printf( "\nContents of name file\n" );
		sprintf( command, "cat %s", nameFileName );
		DBG( command );
		system( command );
	}
	
// now, place the name of the header file at the beginning of a file */
	error = CreateTempFile( &TempFile, &TempName );
	ILError ( error, TempName );

	fprintf( TempFile, "%s\n", headerFileName );
	error = ck_fclose( TempFile );
	ILError( error, TempName );
	
// add the file names to the end
	sprintf( command, "cat %s >> %s\n", nameFileName, TempName );
	system( command );
	
// and rename this file
	sprintf( command, "mv %s %s\n", TempName, nameFileName );
	system( command );
	
	if( false ) {
		printf( "new Contents of name file" );
		sprintf( command, "cat %s", nameFileName );
		DBG( command );
		system( command );
	}

	im.dim.n_slices = argc - 1;

	error = CreateHeader( ANALYZE, &im, baseName );
	if( error ) ILError( error, "Writing Header" );

	sprintf( analyzeFileName, "%s.img", baseName );
	error = ck_fopen( &dataFile, analyzeFileName, "wb" );
	ILError( error, "data file" );
	error = ck_fopen( &nameFile, nameFileName, "rb" );
	ILError( error, "name file" );
	error = ck_fopen( &headerFile, headerFileName, "wb" );
	ILError( error, "header file" );
	
	if( im.dim.x * im.dim.y * sizeof(short) < SIGHDRSIZE ) {
		dataBuff = (short *)ck_malloc( SIGHDRSIZE, "data buffer" );
	} else {
		dataBuff = (short *)ck_malloc( im.dim.x * im.dim.y * sizeof(short), "data buffer" );
	}
	
	count = fscanf( nameFile, "%s", headerFileName );	// just read past this.
	count = fscanf( nameFile, "%s", ImageName );

	if( false ) { DBG( ImageName ); }

	printf( "\n\tPatience for a moment\n" );

	while( count>0 ) {
		printf( "%d of %d complete\n", counter++, argc - 1 );
		error = ck_fopen( &inFile, ImageName, "rb" );
			ILError( error, "Opening image" );
		error = ck_fread ( dataBuff, sizeof( char ), SIGHDRSIZE, inFile );
			ILError( error, "Reading header" );
		error = ck_fwrite( dataBuff, sizeof( char ), SIGHDRSIZE, headerFile );
			ILError( error, "Writing image header" );
		error = ck_fread ( dataBuff, sizeof( short ), im.dim.x * im.dim.y, inFile );
			ILError( error, "Reading image data" );
		error = ck_fwrite( dataBuff, sizeof( short ), im.dim.x * im.dim.y, dataFile );
			ILError( error, "Writing image data" );
		ck_fclose( inFile );
		count = fscanf( nameFile, "%s", ImageName );
	}

	printf( "\n\nCreated Analyze (4D) file: %s\n", analyzeFileName );
	printf( "\tSave these files!: %s and %s\n\n", nameFileName, headerFileName );
	printf( "To create corresponding genesis images from an analyze file named XXX.img, use:\n" );
	printf( "\tAnalyze2Genesis -n %s -i XXX.img\n", nameFileName );

	free( dataBuff );	
	error = ck_fclose( headerFile );
		if( error ) ILError( error, headerFileName );
	error = ck_fclose( nameFile );
		if( error ) ILError( error, nameFileName );
	error = ck_fclose( dataFile );
		if( error ) ILError( error, analyzeFileName );
	error = ck_fclose( procFile );
		if( error ) ILError( error, "proc file" );

	return error;
}
Exemplo n.º 7
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;
}