コード例 #1
0
ファイル: avwcreatehd.c プロジェクト: dungttbk/sips
int main(int argc, char **argv) 
{
  FSLIO* fslio;
  void *buffer=NULL;
  char *hdrxml, *filename;
  int fileread=1, filetype=-1, existingimage=0;
  size_t bufsize=0;
  short x,y,z,v, dt=-1;

  if (argc<2)
    usage();

  
  if ((argc==3) || (argc==4)) {
      /* use the XML form of header specification */
      filename = argv[2];
  } else {
      filename = argv[13];
  }

  /* check if file already exists and if so, read the image contents */
  /* also store the size of this buffer for later in case it is wrong */ 
  if (FslFileExists(filename)) {
    /* buffer = FslReadAllVolumes(fslio,filename); */
    existingimage = 1;
    fslio = FslOpen(filename,"rb");
    FslGetDim(fslio,&x,&y,&z,&v);
    filetype = FslGetFileType(fslio);
    bufsize = x * y * z * v * (FslGetDataType(fslio,&dt) / 8);
    buffer = (void *) calloc(bufsize,1);
    FslReadVolumes(fslio,buffer,v);
    FslClose(fslio);
  }
  
  if (existingimage) {
    fslio = FslXOpen(filename,"wb",filetype);
  } else {
    fslio = FslOpen(filename,"wb");
    filetype = FslGetFileType(fslio);
  }

  if (FslBaseFileType(FslGetFileType(fslio))==FSL_TYPE_MINC) {
    fprintf(stderr,"Minc file type is not supported yet\n");
    exit(EXIT_FAILURE);
  }

  if (argc>3) {
    /* set uninteresting defaults */
    if (existingimage) {
      FslSetDataType(fslio,dt);
    } else {
      FslSetDataType(fslio,atoi(argv[12]));
    }
    FslSetDim(fslio,atoi(argv[1]),atoi(argv[2]),atoi(argv[3]),atoi(argv[4])); 
    FslSetVoxDim(fslio,atof(argv[5]),atof(argv[6]),atof(argv[7]),atof(argv[8])); 
    
    {
      short short_array[100];
      short_array[0]=atoi(argv[9]);
      short_array[1]=atoi(argv[10]);
      short_array[2]=atoi(argv[11]);
      if ( (short_array[0]!=0) || (short_array[1]!=0) || (short_array[2]!=0) )
	{
           FslSetAnalyzeSform(fslio,short_array,
			 atof(argv[5]),atof(argv[6]),atof(argv[7]));
 	}
    }
    
  } else {
      /* read XML form */
      char *newstr, *oldfname, *oldiname;
      FILE *fileptr=NULL;
      if (strcmp(argv[1],"-")==0) {fileread=0;}
      newstr = (char *)calloc(10000,1);
      oldfname = (char *)calloc(10000,1);
      oldiname = (char *)calloc(10000,1);
      hdrxml = (char *)calloc(65534,1);  /* too long, to be safe */

      if (fileread) {
	  fileptr = fopen(argv[1],"rb");
	  if (fileptr==NULL) {
	      fprintf(stderr,"Cannot open file %s\n",argv[1]);
	      exit(EXIT_FAILURE);
	  }
      }

      do {
	  /* read string from file or stdin */
	  if (fileread) {
	      if (fgets(newstr,9999,fileptr)==NULL) break;
	  } else {
	      if (fgets(newstr,9999,stdin)==NULL) break;
	  }
	  strcat(hdrxml,newstr);
      }  while (strcmp(newstr + strlen(newstr) - 2,"/>")!=0);

      strcpy(oldfname,fslio->niftiptr->fname);
      strcpy(oldiname,fslio->niftiptr->iname);

      fslio->niftiptr = nifti_image_from_ascii(hdrxml);

      if (fslio->niftiptr == NULL) {
	  fprintf(stderr,"Incomplete or incorrect text: could not form header info\n");
	  exit(EXIT_FAILURE);
      }

      fslio->niftiptr->fname = oldfname;
      fslio->niftiptr->iname = oldiname;

      free(hdrxml);
      if (fileread) fclose(fileptr);
  }

  /* reset filetype and datatype in case it has been overwritten */
  
  FslSetFileType(fslio,filetype);
  if (existingimage) {
    /* dt is only set if an image was previously read */ 
    FslSetDataType(fslio,dt);
  }

  /* swap bytes */
  
  fslio->niftiptr->byteorder = short_order();
/*   if (strcmp(argv[argc-1],"-r")==0) {  */
/*       /\* swap *\/ */
/*       if (short_order()==MSB_FIRST) fslio->niftiptr->byteorder = LSB_FIRST; */
/*       else fslio->niftiptr->byteorder = MSB_FIRST; */
/*   } */
  
  /* write header */
  
  FslWriteHeader(fslio);

  /* if previously read buffer is wrong size then make a zero image here */
  FslGetDim(fslio,&x,&y,&z,&v);
  if ( bufsize != ( x * y * z * v * (FslGetDataType(fslio,&dt)/8)) ) {
      if (bufsize>0) free(buffer);  /* only if previously read */
      buffer = (void *) calloc(x * y * z * v,FslGetDataType(fslio,&dt)/8);
  }

  /* write the data out - either from previous read or zeros */
  FslWriteVolumes(fslio,buffer,fslio->niftiptr->dim[4]);
  
  FslClose(fslio);
  
  exit(0);
}
コード例 #2
0
ファイル: rsmask.c プロジェクト: janeisklar/RSTools
int main(int argc, char * argv[]) {
    
    FSLIO *fslio;
    FSLIO *fslioOutput;
    void *buffer;
    void *outputBuffer;
    size_t buffsize;
    
    char *inputpath = NULL;
    char *outputpath = NULL;
    char *maskpath = NULL;
    
    int x=-1, y=-1, z=-1, t=0;
    short xDim, yDim, zDim, vDim;
    size_t pixtype;
    short dt;
    float inter = 0.0, slope = 1.0;
        
    BOOL verbose = FALSE;
    int threads = 1;
    
    int ac;
    
    if( argc < 2 ) {
        rsMaskPrintHelp();
        return 1;
    }
    
    /* parse parameters */
    for( ac = 1; ac < argc; ac++ ) {
        if( ! strncmp(argv[ac], "-h", 2) ) {
            rsMaskPrintHelp();
            return 1;
        } else if ( ! strcmp(argv[ac], "-input") ) {
            if( ++ac >= argc ) {
                fprintf(stderr, "** missing argument for -input\n");
                return 1;
            }
            inputpath = argv[ac];  /* no string copy, just pointer assignment */
        } else if ( ! strncmp(argv[ac], "-m", 2) ) {
            if( ++ac >= argc ) {
                fprintf(stderr, "** missing argument for -m[ask]\n");
                return 1;
            }
            maskpath = argv[ac];  /* no string copy, just pointer assignment */
        } else if ( ! strncmp(argv[ac], "-v", 2) ) {
            verbose = TRUE;
        } else if ( ! strcmp(argv[ac], "-output") ) {
            if( ++ac >= argc ) {
                fprintf(stderr, "** missing argument for -output\n");
                return 1;
            }
            outputpath = argv[ac];  /* no string copy, just pointer assignment */
        } else if ( ! strcmp(argv[ac], "-threads") ) {
            if( ++ac >= argc ) {
                fprintf(stderr, "** missing argument for -threads\n");
                return 1;
            }
            threads = atoi(argv[ac]);  /* no string copy, just pointer assignment */
        } else {
            fprintf(stderr, "\nError, unrecognized command %s\n", argv[ac]);
        }
    }
    
    rsSetThreadsNum(threads);
    
    if ( inputpath == NULL ) {
        fprintf(stderr, "No input volume specified!\n");
        return 1;
    }
    
    if ( outputpath == NULL ) {
        fprintf(stderr, "No output volume specified!\n");
        return 1;
    }
    
    if ( maskpath == NULL ) {
        fprintf(stderr, "A binary mask must be specified!\n");
        return 1;
    }
    
    if ( verbose ) {
        fprintf(stdout, "Input file:  %s\n", inputpath);
        fprintf(stdout, "Mask file:   %s\n", maskpath);
        fprintf(stdout, "Output file: %s\n", outputpath);
    }
    
    // Load file
    
    /* open input file */
    fslio = FslOpen(inputpath, "rb");
    if (fslio == NULL) {
        fprintf(stderr, "\nError, could not read header info for %s.\n", inputpath);
        return 1;
    }
    
    /* determine dimensions */
    FslGetDim(fslio, &xDim, &yDim, &zDim, &vDim);
    
    if ( verbose ) {
        fprintf(stdout, "Dim: %d %d %d (%d Volumes)\n", xDim, yDim, zDim, vDim);
    }
    
    if (fslio->niftiptr->scl_slope != 0) {
        slope = fslio->niftiptr->scl_slope;
        inter = fslio->niftiptr->scl_inter;
    }
    
    /* determine datatype and initalize buffer */
    pixtype = FslGetDataType(fslio, &dt);
    
    /* prepare centrality file */    
    fslioOutput = FslOpen(outputpath, "wb");
    
    if (fslioOutput == NULL) {
        fprintf(stderr, "\nError, could not read header info for %s.\n", outputpath);
        return 1;
    }
    
    FslCloneHeader(fslioOutput, fslio);
    FslSetDim(fslioOutput, xDim, yDim, zDim, vDim);
    FslSetDimensionality(fslioOutput, 4);
    FslSetDataType(fslioOutput, dt);
    char *callString = rsMergeStringArray(argc, argv);
    rsWriteNiftiHeader(fslioOutput, callString);
    free(callString);
    
    /* load mask */
    unsigned long nPoints = 0L;
    double ***mask = d3matrix(zDim-1, yDim-1, xDim-1);
    Point3D *maskPoints = rsReadMask(maskpath, xDim, yDim, zDim, &nPoints, NULL, fslio, mask);
    if ( maskPoints == NULL) {
        fprintf(stderr, "\nError: Mask invalid.\n");
        FslClose(fslio);
        FslClose(fslioOutput);
        return 1;
    }
    
    // Prepare buffer
    buffsize       = rsGetBufferSize(xDim, yDim, zDim, vDim, dt);
    buffer         = malloc(buffsize);
    outputBuffer   = malloc(buffsize);
        
    if (buffer == NULL || outputBuffer == NULL) {
        fprintf(stdout, "Not enough free memory :-(\n");
        return 1;
    }
    
    FslReadVolumes(fslio, buffer, vDim);
    
    rsResetBufferToValue(dt, outputBuffer, slope, inter, xDim, yDim, zDim, vDim, log(-1)); 
    
    long p;
    
    #pragma omp parallel num_threads(rsGetThreadsNum()) private(p) shared(buffer,fslio)
    {
        #pragma omp for schedule(guided, 1)
        for (p = 0L; p<nPoints; p++) {

            const Point3D *point = &maskPoints[p];
            double *pointValues = (double*)malloc(sizeof(double)*vDim);
            
            rsExtractTimecourseFromBuffer(dt, pointValues, buffer, slope, inter, point, xDim, yDim, zDim, vDim);
            rsWriteTimecourseToBuffer(dt, pointValues, outputBuffer, slope, inter, point, xDim, yDim, zDim, vDim);
            
            free(pointValues);
        }
    }
    
    /* write output */
    free(buffer);
    FslWriteVolumes(fslioOutput, outputBuffer, vDim);
    FslClose(fslioOutput);
    free(outputBuffer);
    free(fslioOutput);
    free(maskPoints);
}