Ejemplo n.º 1
0
SEXP get_minc_separations(SEXP filename) {
  int                result;
  mihandle_t         hvol;
  const char *filepath = CHAR(asChar(filename));
  midimhandle_t dimensions[3];
  double* voxel_separations = malloc(3 * sizeof(double));
  SEXP output = PROTECT(allocVector(REALSXP, 3));
  
  result = miopen_volume(filepath,
                         MI2_OPEN_READ, &hvol);
  
  if (result != MI_NOERROR) {
    miclose_volume(hvol);
    error("Error opening input file: %s.\n", filepath);
  }
  
  result =
    miget_volume_dimensions(hvol, MI_DIMCLASS_SPATIAL, MI_DIMATTR_ALL,
                            MI_DIMORDER_FILE, 3, dimensions);
  
  result =
    miget_dimension_separations(dimensions, MI_ORDER_FILE, 
                                3, voxel_separations);
    
  
  miclose_volume(hvol);
  for(int i = 0; i < 3; ++i) REAL(output)[i] = voxel_separations[i];
  UNPROTECT(1);
  
  return(output);
}
Ejemplo n.º 2
0
int main(int argc, char *argv[]) 
{
 char history[STRINGLENGTH];
 int result,i;
 long n3,n2,n1;
 mihandle_t inputfile, outputfile;
 double step3, step2, step1;
 double start3, start2, start1;
 double starts[3], separations[3];
 history[0]='\0';
 if(argc!=3) 
 {
  fprintf(stderr, "Usage: input file and an output file!\n");
  return 1;
 }
 if (argc > 1)
 {
  for (i=0;i<argc;i++)
  {
  strcat(history, argv[i]);
  strcat(history," ");
  }
 }
 result = open_minc_file_and_read(argv[1], &inputfile);
 if(result) { return 1; }
   
 result = get_minc_spatial_dimensions_from_handle(inputfile, &n3, &n2, &n1);
 if(result) { return 1; }
 result = get_minc_spatial_separations_from_handle(inputfile, &step3,&step2,&step1); 
 if(result) { return 1; }
 result = get_minc_spatial_starts_from_handle(inputfile, &start3,&start2,&start1); 
 if(result) { return 1; }

 starts[0] = start3;
 starts[1] = start2;
 starts[2] = start1;
 separations[0] = step3;
 separations[1] = step2;
 separations[2] = step1;

 result = open_minc_file_and_write(argv[2], &outputfile, n3, n2, n1,  starts, separations,COMPLEX);
 if(result) { return 1; }
 result = miadd_history_attr(outputfile, strlen(history), history);
 if(result) {printf("could not history to minc header!!!\n");} 
 result = micopy_attr(inputfile,"/OPT",outputfile);
 if(result) {printf("could not copy attributes at path /OPT or does not exist\n");}
		
 result = threedifft(inputfile, outputfile, n3, n2, n1);
 if(result) { return 1;}
    
 result = miclose_volume(inputfile);
 if(result) { return 1;}

 result = miclose_volume(outputfile);
 if(result) { return 1;}

 return 0;

}
Ejemplo n.º 3
0
void get_volume_sizes(char **filename, unsigned int *sizes) {
  int result;
  mihandle_t  hvol;
  misize_t tmp_sizes[3];
  midimhandle_t dimensions[3];
   /* open the existing volume */
  result = miopen_volume(filename[0],
			 MI2_OPEN_READ, &hvol);
  if (result != MI_NOERROR) {
    error("Error opening input file: %s.\n", filename[0]);
  }

    /* get the file dimensions and their sizes */
  miget_volume_dimensions( hvol, MI_DIMCLASS_SPATIAL,
			   MI_DIMATTR_ALL, MI_DIMORDER_FILE,
			   3, dimensions);
  result = miget_dimension_sizes( dimensions, 3, tmp_sizes ); 
  //Rprintf("Sizes: %i %i %i\n", tmp_sizes[0], tmp_sizes[1], tmp_sizes[2]);
  sizes[0] = (unsigned int) tmp_sizes[0];
  sizes[1] = (unsigned int) tmp_sizes[1];
  sizes[2] = (unsigned int) tmp_sizes[2];

   // Close the volume, to free handle
   result = miclose_volume(hvol); 
   if (result != MI_NOERROR) {
    error("Error closing file: %s.\n", filename[0]);
  }
  return;
}
Ejemplo n.º 4
0
/* get a voxel from all files, world coordinates */
void get_world_voxel_from_files(char **filenames, int *num_files,
				double *v1, double *v2, double *v3, 
				double *voxel) {
  double location[3], voxel_coord_tmp[3];
  misize_t voxel_coord[3];
  mihandle_t hvol;
  int result;
  int i, j;

  location[0] = *v1;
  location[1] = *v2;
  location[2] = *v3;

  for(i=0; i < *num_files; i++) {
    /* open the volume */
    result = miopen_volume(filenames[i],
			   MI2_OPEN_READ, &hvol);
    if (result != MI_NOERROR) {
      error("Error opening input file: %s.\n", filenames[i]);
    }

    miconvert_world_to_voxel(hvol, location, voxel_coord_tmp);
    
    for (j=0; j < 3; j++) {
      voxel_coord[j] = (unsigned long) voxel_coord_tmp[j] + 0.5;
    }
    result = miget_real_value(hvol, voxel_coord, 3, &voxel[i]);

    if (result != MI_NOERROR) {
      error("Error getting voxel from: %s.\n", filenames[i]);
    }
    miclose_volume(hvol);
  }
}
Ejemplo n.º 5
0
/* get a voxel from all files, voxel coordinates */
void get_voxel_from_files(char **filenames, int *num_files,
			  int *v1, int *v2, int *v3, double *voxel) {
  misize_t location[3];
  mihandle_t hvol;
  int result;
  int i;

  location[0] = *v1;
  location[1] = *v2;
  location[2] = *v3;

  for(i=0; i < *num_files; i++) {
    /* open the volume */
    result = miopen_volume(filenames[i],
			   MI2_OPEN_READ, &hvol);
    if (result != MI_NOERROR) {
      error("Error opening input file: %s.\n", filenames[i]);
    }

    result = miget_real_value(hvol, location, 3, &voxel[i]);

    if (result != MI_NOERROR) {
      error("Error getting voxel from: %s.\n", filenames[i]);
    }
    miclose_volume(hvol);
  }
}
Ejemplo n.º 6
0
void free_minc_files(SEXP filenames, mihandle_t *hvol) {
  int num_files, i;
  
  num_files = LENGTH(filenames);
  for (i=0; i < num_files; i++) {
    miclose_volume(hvol[i]);
    //free
  }
}
Ejemplo n.º 7
0
void create_test_file(void)
{
    int r;
    double start_values[3]= {-6.96, -12.453,  -9.48};
    double separations[3]= {0.09,0.09,0.09};
    midimhandle_t hdim[NDIMS];
    mihandle_t hvol;
    unsigned short *buf = ( unsigned short *) malloc(CX * CY * CZ * sizeof(unsigned short));
    int i;
    long count[NDIMS];
    long start[NDIMS];
    miboolean_t flag=1;

    double min = -1.0;
    double max =  1.0;
    r = micreate_dimension("zspace", MI_DIMCLASS_SPATIAL,
                           MI_DIMATTR_REGULARLY_SAMPLED, CZ, &hdim[0]);

    r = micreate_dimension("yspace", MI_DIMCLASS_SPATIAL,
                           MI_DIMATTR_REGULARLY_SAMPLED, CY, &hdim[1]);

    r = micreate_dimension("xspace", MI_DIMCLASS_SPATIAL,
                           MI_DIMATTR_REGULARLY_SAMPLED, CX, &hdim[2]);

    r = miset_dimension_starts(hdim, NDIMS, start_values);
    r = miset_dimension_separations(hdim, NDIMS, separations);

    r = micreate_volume("hyperslab-test2.mnc", NDIMS, hdim, MI_TYPE_USHORT,
                        MI_CLASS_REAL, NULL, &hvol);
    /* set slice scaling flag to true */
    r = miset_slice_scaling_flag(hvol, flag);

    r = micreate_volume_image(hvol);

    for (i = 0; i < CZ*CY*CX; i++) {
        buf[i] = (unsigned short) i * 0.01;
    }

    start[0] = start[1] = start[2] = 0;
    count[0] = CZ;
    count[1] = CY;
    count[2] = CX;

    r = miset_voxel_value_hyperslab(hvol, MI_TYPE_USHORT, start, count, buf);
    /* Set random values to slice min and max for slice scaling*/
    start[0] =start[1]=start[2]=0;
    for (i=0; i < CZ; i++) {
        start[0] = i;
        min += 0.1;
        max += 0.1;
        r = miset_slice_range(hvol,start, 3, max, min);
    }

    r = miclose_volume(hvol);
}
Ejemplo n.º 8
0
SEXP get_minc_history(SEXP filename) {
  int                result;
  mihandle_t         hvol;
  int int_size   =   asInteger(minc_history_size(filename));
  char  *history = malloc(int_size);  
  const char *filepath = CHAR(asChar(filename));

  result = miopen_volume(filepath,
                         MI2_OPEN_READ, &hvol);

  if (result != MI_NOERROR) {
    miclose_volume(hvol);
    error("Error opening input file: %s.\n", filepath);
  }
  
  miget_attr_values(hvol, MI_TYPE_STRING, "", "history", int_size, history);
  miclose_volume(hvol);
  
  SEXP output = PROTECT(mkString(history));
  free(history);
  UNPROTECT(1);
  
  return(output);
}
Ejemplo n.º 9
0
int create_2D_image(void)
{
  int r,i;
  midimhandle_t hdim[NDIMS-1];
  mihandle_t hvol;
  short *buf = (short *)malloc(CX * CY * sizeof(short));
  double *offsets = (double *)malloc(CX * sizeof(double)); 
  double start_values[NDIMS-1]={-1.01, -2.02};
  miboolean_t flag=0;
  long count[NDIMS-1];
  long start[NDIMS-1];

  r = micreate_dimension("xspace", MI_DIMCLASS_SPATIAL, MI_DIMATTR_NOT_REGULARLY_SAMPLED, CX, &hdim[0]);

 
  
  r = micreate_dimension("yspace", MI_DIMCLASS_USER, MI_DIMATTR_REGULARLY_SAMPLED, CY, &hdim[1]);
  
   for(i=0; i < CX; i++)
    {
      offsets[i] = (i * i) + 0.1;
    } 
    r = miset_dimension_offsets(hdim[0], CX, 0, offsets);
  r = miset_dimension_separation(hdim[1], 0.06);
  r = miset_dimension_starts(hdim,  NDIMS-1, start_values);
  
  r = micreate_volume("2D_image.mnc", NDIMS-1 , hdim, MI_TYPE_SHORT, 
		      MI_CLASS_REAL, NULL, &hvol);

  /* set slice scaling flag to true */
  r = miset_slice_scaling_flag(hvol, flag);

  r = micreate_volume_image(hvol);

  for (i = 0; i < CX*CY; i++) {
    buf[i] = (short) i * 0.1;
  }
  start[0] = start[1] = 0;
  count[0] = CX; count[1] = CY;
    
  r = miset_voxel_value_hyperslab(hvol, MI_TYPE_SHORT, start, count, buf);

  r = miclose_volume(hvol);
  return r<0?1:0;
  
}
Ejemplo n.º 10
0
int
main(int argc, char **argv)
{
    miclass_t myclass = MI_CLASS_REAL;
    mitype_t mytype = MI_TYPE_UNKNOWN;
    misize_t mysize = 0;
    char *myname = NULL;
    mihandle_t volume = NULL;

    /* Turn off automatic error reporting.
     */
    H5Eset_auto2(H5E_DEFAULT, NULL, NULL);

    /* Check each file.
     */
    while (--argc > 0) {
	
	++argv;

	if (micreate_volume(*argv, 0, NULL, MI_TYPE_INT, 0, NULL, &volume) < 0) { /*type 0 is equivalent to H5T_INTEGER */
	    fprintf(stderr, "Error opening %s\n", *argv);
	}
	else {
	    int i;
	    /* Repeat many times to expose resource leakage problems, etc.
	     */
	    for (i = 0; i < 25000; i++) {
		miget_data_type(volume, &mytype);
		miget_data_type_size(volume, &mysize);
		miget_data_class(volume, &myclass);
		miget_space_name(volume, &myname);
	    
		mifree_name(myname);
	    }

	    miclose_volume(volume);

	    printf("file: %s type %d size %lld class %d\n", *argv,
		   mytype, mysize, myclass);
	}

    }
    return (0);
}
Ejemplo n.º 11
0
/* convert voxel coordinates to a vector of world coords */
void convert_voxel_to_world(char **filenames, double *v1, double *v2,
			    double *v3, double *world_coords) {
  int result;
  mihandle_t hvol;
  double voxel_coords[3];

  /* open the volume */
  result = miopen_volume(filenames[0], MI2_OPEN_READ, &hvol);
  if (result != MI_NOERROR) {
    error("Error opening input file: %s.\n", filenames[0]);
  }

  voxel_coords[0] = *v1;
  voxel_coords[1] = *v2;
  voxel_coords[2] = *v3;

  miconvert_voxel_to_world(hvol, voxel_coords, world_coords);
  miclose_volume(hvol);
}
Ejemplo n.º 12
0
SEXP get_vector_from_files(SEXP filenames,  SEXP num_files,  SEXP vec_length,
			  SEXP v1, SEXP v2, SEXP v3) {
  misize_t location[4];
  mihandle_t hvol;
  int result;
  int i, j, vector_length, number_files; 
  SEXP output;
  double *xoutput;

  vector_length = *INTEGER(vec_length);
  number_files = *INTEGER(num_files);

  location[1] = *INTEGER(v1);
  location[2] = *INTEGER(v2);
  location[3] = *INTEGER(v3);

  Rprintf("NFILES: %d NVEC: %d\n", number_files, vector_length);
  PROTECT(output=allocMatrix(REALSXP, number_files, vector_length));
  xoutput = REAL(output);


  for(i=0; i < number_files; i++) {
    /* open the volume */
    result = miopen_volume(CHAR(STRING_ELT(filenames,i)),
			   MI2_OPEN_READ, &hvol);
    if (result != MI_NOERROR) {
      error("Error opening input file: %s.\n", CHAR(STRING_ELT(filenames, i)));
    }

    for(j=0; j < vector_length; j++) {
      location[0] = j;
      result = miget_real_value(hvol, location, 4, &xoutput[i + number_files*j]);

      if (result != MI_NOERROR) {
	error("Error getting voxel from: %s.\n", CHAR(STRING_ELT(filenames, i)));
      }
    }
    miclose_volume(hvol);
  }
  UNPROTECT(1);
  return(output);
}
Ejemplo n.º 13
0
SEXP minc_history_size(SEXP filename){
  size_t hist_size;
  mihandle_t hvol;
  int result;
  const char *filepath = CHAR(asChar(filename));
  
  result = miopen_volume(filepath,
                         MI2_OPEN_READ, &hvol);
  
  if (result != MI_NOERROR) {
    error("Error opening input file: %s.\n", filepath);
  }
  
  miget_attr_length(hvol, "", "history", &hist_size);

  miclose_volume(hvol);
  
  SEXP output = ScalarInteger((int) hist_size);
  return(output);
}
Ejemplo n.º 14
0
int main ( int argc, char **argv )
{
  mihandle_t hvol;
  int r;
  misize_t coords[3];
  double min, max;
  int i;

  while ( --argc > 0 ) {
    const char *fname=*++argv;
    printf("Checking %s\n",fname);
    r = miopen_volume ( fname, MI2_OPEN_READ, &hvol );
    if ( r < 0 ) {
      fprintf ( stderr, "can't open %s, error %d\n", *argv, r );
      return 1;
    } else {
      for ( i = 0; i < CZ; i++ ) {
        coords[0] = i;         /*Z*/
        coords[1] = rand()%CX; /*X*/
        coords[2] = rand()%CY; /*Y*/

        r = miget_slice_min ( hvol, coords, 3, &min );
        if ( r < 0 ) {
          fprintf ( stderr, "error %d getting slice minimum at %d %d %d\n", r, (int)coords[0],(int)coords[1],(int)coords[2] );
          return 1;
        }

        r = miget_slice_max ( hvol, coords, 3, &max );
        if ( r < 0 ) {
          fprintf ( stderr, "error %d getting slice maximum at %d %d %d\n", r,(int)coords[0],(int)coords[1],(int)coords[2] );
          return 1;
        }
        /*printf ( "%d. min %f max %f\n", i, min, max );*/
        /*PASS*/
      }
      miclose_volume ( hvol );
    }
  }
  return ( 0 );
}
Ejemplo n.º 15
0
static void create_test_file ( void )
{
  midimhandle_t hdim[NDIMS];
  mihandle_t hvol;
  unsigned char *buf = malloc ( CZ * CX * CY * 3 );
  int i;
  misize_t count[NDIMS];
  misize_t start[NDIMS];

  micreate_dimension ( "zspace", MI_DIMCLASS_SPATIAL,
                           MI_DIMATTR_REGULARLY_SAMPLED, CZ, &hdim[0] );

  micreate_dimension ( "yspace", MI_DIMCLASS_SPATIAL,
                           MI_DIMATTR_REGULARLY_SAMPLED, CY, &hdim[1] );

  micreate_dimension ( "xspace", MI_DIMCLASS_SPATIAL,
                           MI_DIMATTR_REGULARLY_SAMPLED, CX, &hdim[2] );

  micreate_dimension ( "vector_dimension", MI_DIMCLASS_RECORD,
                           MI_DIMATTR_REGULARLY_SAMPLED, 3, &hdim[3] );

  micreate_volume ( "example_vector2.mnc", NDIMS, hdim, MI_TYPE_BYTE,
                    MI_CLASS_INT, NULL, &hvol );

  micreate_volume_image ( hvol );

  for ( i = 0; i < CZ * CY * CX * 3; i++ ) {
    buf[i] = ( unsigned char ) i;
  }

  start[0] = start[1] = start[2] = start[3] = 0;
  count[0] = CZ;
  count[1] = CY;
  count[2] = CX;
  count[3] = 3;
  miset_voxel_value_hyperslab ( hvol, MI_TYPE_BYTE, start, count, buf );

  miclose_volume ( hvol );
}
Ejemplo n.º 16
0
/* get a real value hyperslab from file */
void get_hyperslab(char **filename, int *start, int *count, double *slab) {
  int                result;
  mihandle_t         hvol;
  int                i;
  unsigned long      tmp_start[3];
  unsigned long      tmp_count[3];

  /* open the volume */
  result = miopen_volume(filename[0],
			 MI2_OPEN_READ, &hvol);
  if (result != MI_NOERROR) {
    error("Error opening input file: %s.\n", filename[0]);
  }

  for (i=0; i < 3; i++) {
    tmp_start[i] = (unsigned long) start[i];
    tmp_count[i] = (unsigned long) count[i];
  }

  /* get the hyperslab */
  //Rprintf("Start: %i %i %i\n", start[0], start[1], start[2]);
  //Rprintf("Count: %i %i %i\n", count[0], count[1], count[2]);
  if (miget_real_value_hyperslab(hvol, 
				 MI_TYPE_DOUBLE, 
				 (misize_t *) tmp_start, 
				 (misize_t *) tmp_count, 
				 slab)
      < 0) {
    error("Could not get hyperslab.\n");
  }
   // Close the volume, to free handle
   result = miclose_volume(hvol); 
   if (result != MI_NOERROR) {
    error("Error closing file: %s.\n", filename[0]);
  }

  return;
}
Ejemplo n.º 17
0
SEXP minc_overwrite_history(SEXP filename, SEXP history, SEXP hist_size){
  const char *history_line = CHAR(asChar(history));
  const char *filepath = CHAR(asChar(filename));
  int history_size = asInteger(hist_size);
  mihandle_t  hvol;
  int read_result;
  int hist_edit_result;
  
  read_result = miopen_volume(filepath,
                              MI2_OPEN_RDWR, &hvol);
  
  if (read_result != MI_NOERROR) {
    error("Error opening input file: %s.\n", filepath);
  }
  
  hist_edit_result = miadd_history_attr(hvol, history_size, history_line);
  if(hist_edit_result != MI_NOERROR){
    error("Error editing history for file: %s \n", filepath);
  }
  
  miclose_volume(hvol);
  
  return(R_NilValue);
}
Ejemplo n.º 18
0
vector<mihandle_t> open_minc2_volumes(CharacterVector filenames){
  
  vector<mihandle_t> volumes;
  mihandle_t current_handle;
  CharacterVector::iterator file_iterator;
  vector<mihandle_t>::iterator volume_iterator;
  
  for(file_iterator = filenames.begin();
      file_iterator != filenames.end();
      ++file_iterator){
    try {
      current_handle = open_minc2_volume(wrap(*file_iterator));
    } catch(...){
      for(volume_iterator = volumes.begin(); volume_iterator != volumes.end(); ++volume_iterator){
        miclose_volume(*volume_iterator);
      }
      throw;
    }
    
    volumes.push_back(current_handle);
  }
  
  return(volumes);
}
Ejemplo n.º 19
0
int main(void)
{
    mihandle_t vol;
    int r;
    midimhandle_t dim[NDIMS];
    int n;
    misize_t coords[NDIMS];
    misize_t count[NDIMS];
    int i,j,k;
    double offset;
    unsigned int voxel;

    /* Write data one voxel at a time. */
    for (i = 0; i < NDIMS; i++) {
        count[i] = 1;
    }

    r = micreate_dimension("time", MI_DIMCLASS_TIME,
                           MI_DIMATTR_NOT_REGULARLY_SAMPLED, CT, &dim[0]);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    for (i = 0; i < CT; i++) {
        offset = (i * i) + 100.0;
        r = miset_dimension_offsets(dim[0], 1, i, &offset);
        if (r < 0) {
            TESTRPT("failed", r);
        }
    }

    r = micreate_dimension("xspace",MI_DIMCLASS_SPATIAL,
                           MI_DIMATTR_REGULARLY_SAMPLED, CX, &dim[1]);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = miset_dimension_start(dim[1], XSTART);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = miset_dimension_separation(dim[1], XSTEP);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = micreate_dimension("yspace",MI_DIMCLASS_SPATIAL,
                           MI_DIMATTR_REGULARLY_SAMPLED, CY, &dim[2]);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = miset_dimension_start(dim[2], YSTART);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = miset_dimension_separation(dim[2], YSTEP);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = micreate_dimension("zspace",MI_DIMCLASS_SPATIAL,
                           MI_DIMATTR_REGULARLY_SAMPLED, CZ, &dim[3]);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = miset_dimension_start(dim[3], ZSTART);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = miset_dimension_separation(dim[3], ZSTEP);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = micreate_volume("tst-dim.mnc", NDIMS, dim, MI_TYPE_UINT,
                        MI_CLASS_REAL, NULL, &vol);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = micreate_volume_image(vol);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    check_dims(vol, dim);

    for (i = 0; i < CX; i++) {
        for (j = 0; j < CY; j++) {
            for (k = 0; k < CZ; k++) {
                coords[0] = 0;
                coords[1] = i;
                coords[2] = j;
                coords[3] = k;

                voxel = (i*10000)+(j*100)+k;
                r = miset_voxel_value_hyperslab(vol, 
                                                MI_TYPE_UINT,
                                                coords,
                                                count, 
                                                &voxel);
                if (r < 0) {
                    TESTRPT("Error writing voxel", r);
                }
            }
        }
    }


    r = miclose_volume(vol);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    /***** 03-Aug-2004: Added two tests for bugs reported by Leila */

    r = miopen_volume("tst-dim.mnc", MI2_OPEN_RDWR, &vol);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = miget_volume_dimension_count(vol, MI_DIMCLASS_ANY,
                                     MI_DIMATTR_REGULARLY_SAMPLED, &n);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    if (n != NDIMS - 1) {
        TESTRPT("wrong result", n);
    }

    r = miget_volume_dimension_count(vol, MI_DIMCLASS_ANY,
                                     MI_DIMATTR_NOT_REGULARLY_SAMPLED, &n);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    if (n != 1) {
        TESTRPT("wrong result", n);
    }

    r = miclose_volume(vol);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    /* Test #2 - verify that we don't print anything scary if a user
     * closes a volume prematurely.
     */
    r = micreate_dimension("xspace",MI_DIMCLASS_SPATIAL,
                           MI_DIMATTR_REGULARLY_SAMPLED, CX, &dim[0]);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    r = micreate_dimension("yspace",MI_DIMCLASS_SPATIAL,
                           MI_DIMATTR_REGULARLY_SAMPLED, CY, &dim[1]);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    r = micreate_dimension("zspace",MI_DIMCLASS_SPATIAL,
                           MI_DIMATTR_REGULARLY_SAMPLED, CZ, &dim[2]);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    r = micreate_volume("tst-vol.mnc", 3, dim, MI_TYPE_SHORT,
                        MI_CLASS_LABEL, NULL, &vol);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = miclose_volume(vol);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    /** End of tests added 03-Aug-2004 **/

    if (error_cnt != 0) {
        fprintf(stderr, "%d error%s reported\n", 
                error_cnt, (error_cnt == 1) ? "" : "s");
    }
    else {
        fprintf(stderr, "No errors\n");
    }
    return (error_cnt);
}
Ejemplo n.º 20
0
int main(int argc, char **argv)
{
    mihandle_t vol;
    int r;
    midimhandle_t dim[NDIMS];
    mivolumeprops_t props;
    int n;
    unsigned long coords[NDIMS];
    unsigned long count[NDIMS];
    int i,j,k;
    unsigned int voxel;

    printf("Creating volume...\n");

    /* Write data one voxel at a time. */
    for (i = 0; i < NDIMS; i++) {
        count[i] = 1;
    }

    r = minew_volume_props(&props);
    r = miset_props_compression_type(props, MI_COMPRESS_ZLIB);
    r = miset_props_zlib_compression(props, 3);
    r = miset_props_multi_resolution(props, 1, 3);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = micreate_dimension("xspace",MI_DIMCLASS_SPATIAL,MI_DIMATTR_REGULARLY_SAMPLED, CX,&dim[0]);
    if (r < 0) {
        TESTRPT("failed", r);
    }
  
    r = micreate_dimension("yspace",MI_DIMCLASS_SPATIAL,MI_DIMATTR_REGULARLY_SAMPLED, CY, &dim[1]);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    r = micreate_dimension("zspace",MI_DIMCLASS_SPATIAL,MI_DIMATTR_REGULARLY_SAMPLED, CZ,&dim[2]);
    if (r < 0) {
        TESTRPT("failed", r);
    }
 
    r = micreate_volume("tst-multi.mnc", NDIMS, dim, MI_TYPE_UINT, MI_CLASS_REAL,props,&vol);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = miset_volume_valid_range(vol, CX*10000.0 + CY*100 + CZ, 0.0);

    r = micreate_volume_image(vol);
    if (r < 0) {
        TESTRPT("failed", r);
    }
  
    r = miget_volume_dimension_count(vol, MI_DIMCLASS_SPATIAL, MI_DIMATTR_ALL, &n);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    printf("Writing data...\n");

    for (i = 0; i < CX; i++) {
        for (j = 0; j < CY; j++) {
            for (k = 0; k < CZ; k++) {
                coords[0] = i;
                coords[1] = j;
                coords[2] = k;

                voxel = i*10000 + j*100 + k;
                
                r = miset_voxel_value_hyperslab(vol, MI_TYPE_UINT,
                                                coords, count, &voxel);
                if (r < 0) {
                    TESTRPT("Error writing voxel", r);
                }
            }
        }
    }

    printf("Selecting half-size image\n");

    r = miselect_resolution(vol, 1);
    if (r < 0) {
        TESTRPT("miselect_resolution failed", r);
    }

    /* OK, now try to read the lower-resolution hyperslab */
    coords[0] = 0;
    coords[1] = 0;
    coords[2] = 0;
    count[0] = CX/2;
    count[1] = CY/2;
    count[2] = CZ/2;

    {
        unsigned int buffer[CX/2][CY/2][CZ/2];
    
        r = miget_voxel_value_hyperslab(vol, MI_TYPE_UINT,
                                        coords, count, buffer);
        if (r < 0) {
            TESTRPT("failed", r);
        }
    }

    printf("Selecting quarter-size image\n");

    r = miselect_resolution(vol, 2);
    if (r < 0) {
        TESTRPT("miselect_resolution failed", r);
    }

    /* OK, now try to read the lower-resolution hyperslab */
    coords[0] = 0;
    coords[1] = 0;
    coords[2] = 0;
    count[0] = CX/4;
    count[1] = CY/4;
    count[2] = CZ/4;

    {
        unsigned int buffer[CX/4][CY/4][CZ/4];
    
        r = miget_voxel_value_hyperslab(vol, MI_TYPE_UINT,
                                        coords, count, buffer);
        if (r < 0) {
            TESTRPT("failed", r);
        }
    }

    printf("Return to full resolution.\n");

    r = miselect_resolution(vol, 0); /* Back to full resolution */
    if (r < 0) {
        TESTRPT("miselect_resolution failed", r);
    }

    printf("Flush any remaining thumbnails.\n");

    r = miflush_from_resolution(vol, 3);
    if (r < 0) {
        TESTRPT("failed", r);
    }

    r = miclose_volume(vol);
    if (r < 0) {
        TESTRPT("failed", r);
    }
      
    if (error_cnt != 0) {
        fprintf(stderr, "%d error%s reported\n", 
                error_cnt, (error_cnt == 1) ? "" : "s");
    }
    else {
        fprintf(stderr, "No errors\n");
    }
    return (error_cnt);
}
Ejemplo n.º 21
0
int main(int argc, char **argv)
{
  mihandle_t vol;
  mivolumeprops_t  props;
  int r;
  micompression_t compression_type;
  miboolean_t enable_flag;
  int zlib_level;
  int depth;
  int edge_lengths[MI2_MAX_VAR_DIMS];
  int edge_count;
  int i;

  r = minew_volume_props(&props);

  if (r < 0) {
    TESTRPT("failed", r);
  }
  
  r = miset_props_multi_resolution(props, 1 , 2);

  if (r < 0) {
    TESTRPT("failed", r);
   }

  r = miget_props_multi_resolution(props, &enable_flag, &depth);
  if (r < 0) {
    TESTRPT("failed", r);
  }
  else {
    printf("Multiresolution enabled: %d depth: %d \n", enable_flag, depth);
  }

  r = miset_props_compression_type(props, MI_COMPRESS_NONE);
  if (r < 0) {
    TESTRPT("failed", r);
  }
  else {
      printf("Set compression type to %d\n", MI_COMPRESS_NONE);
  }

  r = miget_props_compression_type(props,&compression_type);
  if (r < 0 || compression_type != MI_COMPRESS_NONE) {
    TESTRPT("failed", r);
  }
  else {
    printf("Got compression type %d \n", compression_type);
 }  

  r = miset_props_zlib_compression(props,4);
  if (r < 0) {
    TESTRPT("failed", r);
  }
  else {
    printf("Set zlib level to %d\n", 4);
  }

  r = miget_props_zlib_compression(props,&zlib_level);
  if (r < 0 || zlib_level != 4) {
    TESTRPT("failed", r);
  }
  else {
    printf("Got zlib level %d \n", zlib_level);
  }

  mifree_volume_props(props);

  while (--argc > 0) {
      r = miopen_volume(*++argv, MI2_OPEN_RDWR, &vol);
      if (r < 0) {
	  TESTRPT("failed", r);
      }
      r = miget_volume_props(vol, &props);
      if (r < 0) {
	  TESTRPT("failed", r);
      }
      r = miget_props_blocking(props, &edge_count, edge_lengths, 
			       MI2_MAX_VAR_DIMS);
      if (r < 0) {
	  TESTRPT("failed", r);
      }
      printf("edge_count %d\n", edge_count);
      for (i = 0; i < edge_count; i++) {
	  printf("  %d", edge_lengths[i]);
      }
      printf("\n");
      mifree_volume_props(props);
      miclose_volume(vol);
  }

 if (error_cnt != 0) {
    fprintf(stderr, "%d error%s reported\n", 
	    error_cnt, (error_cnt == 1) ? "" : "s");
  }
  else {
    fprintf(stderr, "No errors\n");
  }
  return (error_cnt);
}
Ejemplo n.º 22
0
int
main(void)
{
    mihandle_t hvol;
    char *name;
    int result;
    midimhandle_t hdim[NDIMS];
    misize_t coords[NDIMS];
    misize_t count[NDIMS];
    int i,j,k;
    struct test {
        int r;
        int g;
        int b;
    } voxel;

    /* Write data one voxel at a time. */
    for (i = 0; i < NDIMS; i++) {
        count[i] = 1;
    }

    result = micreate_dimension("xspace", MI_DIMCLASS_SPATIAL, 
                           MI_DIMATTR_REGULARLY_SAMPLED, CX, &hdim[0]);

    result = micreate_dimension("yspace", MI_DIMCLASS_SPATIAL, 
                           MI_DIMATTR_REGULARLY_SAMPLED, CY, &hdim[1]);

    result = micreate_dimension("zspace", MI_DIMCLASS_SPATIAL, 
                           MI_DIMATTR_REGULARLY_SAMPLED, CZ, &hdim[2]);

    result = micreate_volume("tst-rec.mnc", NDIMS, hdim, MI_TYPE_UINT, 
                             MI_CLASS_UNIFORM_RECORD, NULL, &hvol);
    if (result < 0) {
	TESTRPT("Unable to create test file", result);
    }

    result = miset_record_field_name(hvol, 0, "Red");
    if (result < 0) {
        TESTRPT("miset_record_field_name", result);
    }
    miset_record_field_name(hvol, 1, "Green");
    miset_record_field_name(hvol, 2, "Blue");

    miget_record_field_name(hvol, 1, &name);
    if (strcmp(name, "Green") != 0) {
	TESTRPT("Unexpected label for value 1", 0);
    }
    mifree_name(name);

    miget_record_field_name(hvol, 0, &name);
    if (strcmp(name, "Red") != 0) {
	TESTRPT("Unexpected label for value 0", 0);
    }
    mifree_name(name);

    miget_record_field_name(hvol, 2, &name);
    if (strcmp(name, "Blue") != 0) {
	TESTRPT("Unexpected label for value 2", 0);
    }
    mifree_name(name);

    result = micreate_volume_image(hvol);
    if (result < 0) {
        TESTRPT("micreate_volume_image failed", result);
    }

    for (i = 0; i < CX; i++) {
        for (j = 0; j < CY; j++) {
            for (k = 0; k < CZ; k++) {
                coords[0] = i;
                coords[1] = j;
                coords[2] = k;

                voxel.r = i;
                voxel.g = j;
                voxel.b = k;
                
                result = miset_voxel_value_hyperslab(hvol, MI_TYPE_UNKNOWN,
                                                     coords, count, &voxel);
                if (result < 0) {
                    TESTRPT("Error writing voxel", result);
                }
            }
        }
    }

    for (i = 0; i < CX; i++) {
        for (j = 0; j < CY; j++) {
            for (k = 0; k < CZ; k++) {
                coords[0] = i;
                coords[1] = j;
                coords[2] = k;

                result = miget_voxel_value_hyperslab(hvol, MI_TYPE_UNKNOWN,
                                                     coords, count, &voxel);
                if (result < 0) {
                    TESTRPT("Error reading voxel", result);
                }
                if (voxel.r != i || voxel.g != j || voxel.b != k) {
                    TESTRPT("Data mismatch", 0);
                }
            }
        }
    }

    miclose_volume(hvol);

    if (error_cnt != 0) {
	fprintf(stderr, "%d error%s reported\n", 
		error_cnt, (error_cnt == 1) ? "" : "s");
    }
    else {
	fprintf(stderr, "No errors\n");
    }
    return (error_cnt);
}
Ejemplo n.º 23
0
SEXP minc2_apply(SEXP filenames, SEXP fn, SEXP have_mask, 
		 SEXP mask, SEXP mask_value, SEXP rho) {
  int                result;
  mihandle_t         *hvol, hmask;
  int                i, v0, v1, v2, output_index, buffer_index;
  unsigned long     start[3], count[3];
  //unsigned long      location[3];
  int                num_files;
  double             *xbuffer, *xoutput, **full_buffer;
  double             *xhave_mask, *xmask_value;
  double             *mask_buffer;
  midimhandle_t      dimensions[3];
  misize_t            sizes[3];
  SEXP               output, buffer;
  //SEXP               R_fcall;
  

  /* allocate memory for volume handles */
  num_files = LENGTH(filenames);
  hvol = malloc(num_files * sizeof(mihandle_t));

  Rprintf("Number of volumes: %i\n", num_files);

  /* open the mask - if so desired */
  xhave_mask = REAL(have_mask);
  if (xhave_mask[0] == 1) {
    result = miopen_volume(CHAR(STRING_ELT(mask, 0)),
			   MI2_OPEN_READ, &hmask);
    if (result != MI_NOERROR) {
      error("Error opening mask: %s.\n", CHAR(STRING_ELT(mask, 0)));
    }
  }
  
  /* get the value inside that mask */
  xmask_value = REAL(mask_value);

  /* open each volume */
  for(i=0; i < num_files; i++) {
    result = miopen_volume(CHAR(STRING_ELT(filenames, i)),
      MI2_OPEN_READ, &hvol[i]);
    if (result != MI_NOERROR) {
      error("Error opening input file: %s.\n", CHAR(STRING_ELT(filenames,i)));
    }
  }

  /* get the file dimensions and their sizes - assume they are the same*/
  miget_volume_dimensions( hvol[0], MI_DIMCLASS_SPATIAL,
			   MI_DIMATTR_ALL, MI_DIMORDER_FILE,
			   3, dimensions);
  result = miget_dimension_sizes( dimensions, 3, sizes );
  Rprintf("Volume sizes: %i %i %i\n", sizes[0], sizes[1], sizes[2]);

  /* allocate the output buffer */
  PROTECT(output=allocVector(REALSXP, (sizes[0] * sizes[1] * sizes[2])));
  xoutput = REAL(output);

  /* allocate the local buffer that will be passed to the function */
  PROTECT(buffer=allocVector(REALSXP, num_files));
  xbuffer = REAL(buffer); 

  //PROTECT(R_fcall = lang2(fn, R_NilValue));


  /* allocate first dimension of the buffer */
  full_buffer = malloc(num_files * sizeof(double));

  /* allocate second dimension of the buffer 
     - big enough to hold one slice per subject at a time */
  for (i=0; i < num_files; i++) {
    full_buffer[i] = malloc(sizes[1] * sizes[2] * sizeof(double));
  }
  
  /* allocate buffer for mask - if necessary */
  if (xhave_mask[0] == 1) {
    mask_buffer = malloc(sizes[1] * sizes[2] * sizeof(double));
  }
	
  /* set start and count. start[0] will change during the loop */
  start[0] = 0; start[1] = 0; start[2] = 0;
  count[0] = 1; count[1] = sizes[1]; count[2] = sizes[2];

  /* loop across all files and voxels */
  Rprintf("In slice \n");
  for (v0=0; v0 < sizes[0]; v0++) {
    start[0] = v0;
    for (i=0; i < num_files; i++) {
      if (miget_real_value_hyperslab(hvol[i], 
				     MI_TYPE_DOUBLE, 
				     (misize_t *) start, 
				     (misize_t *) count, 
				     full_buffer[i]) )
	error("Error opening buffer.\n");
    }
    /* get mask - if desired */
    if (xhave_mask[0] == 1) {
      if (miget_real_value_hyperslab(hmask, 
				     MI_TYPE_DOUBLE, 
				     (misize_t *) start, 
				     (misize_t *) count, 
				     mask_buffer) )
	error("Error opening mask buffer.\n");
    }

    Rprintf(" %d ", v0);
    for (v1=0; v1 < sizes[1]; v1++) {
      for (v2=0; v2 < sizes[2]; v2++) {
	output_index = v0*sizes[1]*sizes[2]+v1*sizes[2]+v2;
	buffer_index = sizes[2] * v1 + v2;

	/* only perform operation if not masked */
	if(xhave_mask[0] == 0 
	   || (xhave_mask[0] == 1 && 
	       mask_buffer[buffer_index] > xmask_value[0] -0.5 &&
	       mask_buffer[buffer_index] < xmask_value[0] + 0.5)) {
	
	  for (i=0; i < num_files; i++) {
// 	    location[0] = v0;
// 	    location[1] = v1;
// 	    location[2] = v2;
	    //SET_VECTOR_ELT(buffer, i, full_buffer[i][index]);
	    //result = miget_real_value(hvol[i], location, 3, &xbuffer[i]);
	    xbuffer[i] = full_buffer[i][buffer_index];
	    
	    //Rprintf("V%i: %f\n", i, full_buffer[i][index]);

	  }
	  /* install the variable "x" into environment */
	  defineVar(install("x"), buffer, rho);
	  //SETCADDR(R_fcall, buffer);
	  //SET_VECTOR_ELT(output, index, eval(R_fcall, rho));
	  //SET_VECTOR_ELT(output, index, test);
	  /* evaluate the function */
	  xoutput[output_index] = REAL(eval(fn, rho))[0]; 
	}
	else {
	  xoutput[output_index] = 0;
	}
      }
    }
  }
  Rprintf("\nDone\n");

  /* free memory */
  for (i=0; i<num_files; i++) {
    miclose_volume(hvol[i]);
    free(full_buffer[i]);
  }
  free(full_buffer);
  UNPROTECT(2);

  /* return the results */
  return(output);
}
Ejemplo n.º 24
0
int create_4D_image(void)
{
    int r;
    double start_values[NDIMS+1]={-6.96, -12.453,  -9.48, 20.002};
    double separations[NDIMS+1]={0.09,0.09,0.09, 1};
    midimhandle_t hdim[NDIMS+1];
    mihandle_t hvol;
    unsigned char *buf = (unsigned char *) malloc(CX * CU * CZ * CY * sizeof(unsigned char));
    int i,j;
    long count[NDIMS+1];
    long start[NDIMS+1];
    miboolean_t flag=1;
    
    double min = -1.0;
    double max =  1.0;
    r = micreate_dimension("xspace", MI_DIMCLASS_SPATIAL, 
                           MI_DIMATTR_REGULARLY_SAMPLED, CX, &hdim[0]);

    r = micreate_dimension("user", MI_DIMCLASS_USER, 
                                MI_DIMATTR_REGULARLY_SAMPLED, CU, &hdim[1]);
    
    r = micreate_dimension("zspace", MI_DIMCLASS_SPATIAL, 
                           MI_DIMATTR_REGULARLY_SAMPLED, CZ, &hdim[2]);

    r = micreate_dimension("yspace", MI_DIMCLASS_SPATIAL, 
                           MI_DIMATTR_REGULARLY_SAMPLED, CY, &hdim[3]);

    r = miset_dimension_starts(hdim, NDIMS+1, start_values);
    r = miset_dimension_separations(hdim, NDIMS+1, separations);

    r = micreate_volume("4D_image.mnc", NDIMS+1, hdim, MI_TYPE_UBYTE, 
                    MI_CLASS_REAL, NULL, &hvol);

    /* set slice scaling flag to true */
    r = miset_slice_scaling_flag(hvol, flag);

    r = micreate_volume_image(hvol);

    for (i = 0; i < CX*CU*CZ*CY; i++) {
        buf[i] = (unsigned char) i;
    }
    
    start[0] = start[1] = start[2] = start[3] = 0;
    count[0] = CX; count[1] = CU; count[2] = CZ; count[3] = CY;
    
    r = miset_voxel_value_hyperslab(hvol, MI_TYPE_UBYTE, start, count, buf);
    /* Set random values to slice min and max for slice scaling*/
    start[0] =start[1]=start[2]=start[3]=0;
    for (i=0; i < CX; i++) {
      
      start[0] = i;
      for ( j=0; j < CU; j++)
	{
	start[1] = j;
	min += -0.1;
	max += 0.1;
	r = miset_slice_range(hvol,start,NDIMS+1 , max, min);
	}
    }
    
  r = miclose_volume(hvol);
  
  return r<0?1:0;
}
Ejemplo n.º 25
0
/* writes a hyperslab to the output filename, creating the output voluem
   to be like the second filename passed in */
void write_minc2_volume(char **output, char **like_filename,
			int *start, int *count, double *max_range,
			double *min_range, double *slab) {
  mihandle_t hvol_like, hvol_new;
  midimhandle_t *dimensions_like, *dimensions_new;
  unsigned long tmp_count[3];
  unsigned long tmp_start[3];
  int i;

  /* allocate the dimension handles */
  dimensions_like = malloc(sizeof(midimhandle_t) * 3);
  dimensions_new = malloc(sizeof(midimhandle_t) * 3);

  /* read the like volume */
  if (miopen_volume(like_filename[0], MI2_OPEN_READ, &hvol_like) < 0 ) {
    error("Error opening volume: %s\n", like_filename[0]);
  }
  
  /* get dimensions */
  if (miget_volume_dimensions( hvol_like, MI_DIMCLASS_SPATIAL, MI_DIMATTR_ALL,
			       MI_DIMORDER_FILE, 3, dimensions_like ) < 0 ) {
    error("Error getting volume dimensions\n");
  }

  /* copy the dimensions to the new file */
  for (i=0; i < 3; i++) {
    if (micopy_dimension(dimensions_like[i], &dimensions_new[i]) < 0) {
      error ("Error copying dimension %d\n", i);
    }
  }

  /* create the new volume */
  if ( micreate_volume(output[0], 3, dimensions_new, MI_TYPE_USHORT,
		       MI_CLASS_REAL, NULL, &hvol_new) < 0 ) {
    error("Error creating volume %s\n", output[0]);
  }
  if (micreate_volume_image(hvol_new) < 0) {
    error("Error creating volume image\n");
  }
  
  /* set valid and real range */
  miset_volume_valid_range(hvol_new, 65535, 0);
  miset_volume_range(hvol_new, max_range[0], min_range[0]);

  Rprintf("Range: %f %f\n", max_range[0], min_range[0]);

  /* write the buffer */
  for (i=0; i < 3; i++) {
    tmp_start[i] = (unsigned long) start[i];
    tmp_count[i] = (unsigned long) count[i];
  }
  if (miset_real_value_hyperslab(hvol_new, MI_TYPE_DOUBLE, 
				 (misize_t *) tmp_start, 
				 (misize_t *) tmp_count,
				 slab) < 0) {
    error("Error writing buffer to volume\n");
  }
  if (miclose_volume(hvol_like) < 0) {
    error("Error closing like volume\n");
  }
  if (miclose_volume(hvol_new) < 0) {
    error("Error closing new volume\n");
  }

  free(dimensions_new);
  free(dimensions_like);
  return;
}
Ejemplo n.º 26
0
int 
main(int argc, char *argv[])
{   
    int r;
    
    const char **file_list = NULL;     /* List of file names */
    struct vff_attrs vffattrs;
    struct mnc_vars mnc2;
    char out_str[1024];         /* Big string for filename */
    midimhandle_t hdim[MAX_VFF_DIMS];
    mihandle_t hvol;
   
    double mnc_vrange[2];       /* MINC valid min/max */
    int num_file_args;          /* Number of files on command line */
    string_t out_dir;           /* Output directory */
    int length;
    struct stat st;
    int ifile;
    int num_files;              /* Total number of files */
    int is_file=0;
    int is_list=0;
    int ival;
    char *extension;

    mnc2.mnc_srange[0]= -1;
    mnc2.mnc_srange[1]= -1;

    G.pname = argv[0];          /* get program name */
    G.dirname = NULL;
    G.little_endian = 1; /*default is little endian unless otherwise*/
    G.minc_history = time_stamp(argc, argv); /* Create minc history string */

    if (ParseArgv(&argc, argv, argTable, 0) || argc < 2) {
        usage();
	exit(EXIT_FAILURE);
    }
     
    if (G.dirname != NULL) {
#if HAVE_DIRENT_H
      
      if (stat(G.dirname, &st) != 0 || !S_ISDIR(st.st_mode)) {
        fprintf(stderr,"Option -addattrs requires directory as argument!!!\n");
        exit(EXIT_FAILURE);
      }
#endif 
    }

    if (G.List) 
      {
      num_file_args = argc - 1; 
      }
    else 
      {
	strcpy(out_str, argv[1]);
        extension = strrchr(out_str, '.');
        if (extension != NULL )
	  {
	    extension++;
            if (strcmp(extension, "mnc") !=0)
	      {
		usage();
		exit(EXIT_FAILURE);
	      }
	  }
	if (argc == 3)
	  {
	  /* check if last argument is dir */
	  num_file_args = argc - 2; 
	  strcpy(out_dir, argv[argc - 1]); 
	  
	  /* make sure path ends with slash 
	   */
	  length = strlen(out_dir);
	  if (out_dir[length - 1] != '/') 
	    {
	    out_dir[length++] = '/';
	    out_dir[length++] = '\0';
	    }

	  if (stat(out_dir, &st) != 0 || !S_ISDIR(st.st_mode)) 
	    {/* assume filename */
	    is_file =1;
	    }
	  }
	else
	  { //list of 2d files must check!
	  num_file_args = argc - 2;
	  is_list = 1;
	  }
      }
	    
    if (!is_file || G.List)
      {
	
      /* Get space for file lists */
      /* Allocate the array of pointers used to implement the
       * list of filenames.
       */
      file_list = malloc(1 * sizeof(char *));
      CHKMEM(file_list);

      /* Go through the list of files, expanding directories where they
       * are encountered...
       */
      num_files = 0;
      for (ifile = 1 ; ifile <= num_file_args; ifile++) 
	{
#if HAVE_DIRENT_H    
	
	if (stat(argv[ifile + 1], &st) == 0 && S_ISDIR(st.st_mode)) 
	  {
	  DIR *dp;
	  struct dirent *np;
	  char *tmp_str;

	  length = strlen(argv[ifile + 1]);

	  dp = opendir(argv[ifile + 1]);
	  if (dp != NULL) 
	    {
	    while ((np = readdir(dp)) != NULL) 
	      {
	      /* Generate the full path to the file.
	       */
	      tmp_str = malloc(length + strlen(np->d_name) + 2);
	      strcpy(tmp_str, argv[ifile + 1]);
	      if (tmp_str[length-1] != '/') 
	       {
	       tmp_str[length] = '/';
	       tmp_str[length+1] = '\0';
	       }
	      strcat(&tmp_str[length], np->d_name);
	      if (stat(tmp_str, &st) == 0 && S_ISREG(st.st_mode)) 
		{
		file_list = realloc(file_list,
				    (num_files + 1) * sizeof(char *));
		file_list[num_files++] = tmp_str;
		}
	      else 
		{
		free(tmp_str);
		}
	      }
	    closedir(dp);
	  }
	  else 
	    {
	      fprintf(stderr, "Error opening directory '%s'\n", 
		      argv[ifile + 1]);
	    }
	  }
	else 
	  {
	  file_list = realloc(file_list, (num_files + 1) * sizeof(char *));
	  file_list[num_files++] = strdup(argv[ifile + 1]);
	  }
#else
	file_list = realloc(file_list, (num_files + 1) * sizeof(char *));
        file_list[num_files++] = strdup(argv[ifile + 1]);
#endif
	}
      }
 
    if (G.List)
      {
      exit(EXIT_SUCCESS);
      }

    if (is_file)
      {
      read_3Dvff_file_header(argv[2],&mnc2,&vffattrs);
      }
    else
      {
      read_2Dvff_files_header(file_list,num_files,&mnc2,&vffattrs);
      }

    /* ok starting to create minc2.0 file assuming 3D must take care of 2D*/

    r = micreate_dimension("zspace", MI_DIMCLASS_SPATIAL, 
                           MI_DIMATTR_REGULARLY_SAMPLED, mnc2.mnc_count[2], &hdim[0]);
    if (r != 0) {
      TESTRPT("failed create_dimension zspace", r);
      return (1);
    }
    r = micreate_dimension("yspace", MI_DIMCLASS_SPATIAL, 
                           MI_DIMATTR_REGULARLY_SAMPLED, mnc2.mnc_count[1], &hdim[1]);
    if (r != 0) {
     TESTRPT("failed create_dimension yspace", r);
      return (1);
    }
    r = micreate_dimension("xspace", MI_DIMCLASS_SPATIAL, 
                           MI_DIMATTR_REGULARLY_SAMPLED, mnc2.mnc_count[0], &hdim[2]);
    if (r != 0) {
      TESTRPT("failed create_dimension xspace", r);
      return (1);
    }
    
    r = miset_dimension_start(hdim[0], mnc2.mnc_starts[2]);
    if (r < 0) {
      TESTRPT("failed dimension start xspace", r);
      return (1);
    }
    r = miset_dimension_start(hdim[1], mnc2.mnc_starts[1]);
    if (r < 0) {
      TESTRPT("failed dimension start yspace", r);
      return (1);
    }
    /* create negative start for xspace to correct orientation */
    r = miset_dimension_start(hdim[2], mnc2.mnc_starts[0] * -1);
    if (r < 0) {
      TESTRPT("failed dimension start zspace", r);
      return (1);
    }
    /* create negative spacing for zspace to correct orientation */
    r = miset_dimension_separation(hdim[0], mnc2.mnc_steps[2] * -1);
    if (r < 0) {
      TESTRPT("failed dimension separation xspace", r);
      return (1);
    }
    /* create negative spacing for yspace to correct orientation */
    r = miset_dimension_separation(hdim[1], mnc2.mnc_steps[1] * -1);
    if (r < 0) {
      TESTRPT("failed dimension separation yspace", r);
      return (1);
    }
    
    r = miset_dimension_separation(hdim[2], mnc2.mnc_steps[0]);
    if (r < 0) {
      TESTRPT("failed dimension separation zspace", r);
      return (1);
    }
    
    r = micreate_volume(out_str,MAX_VFF_DIMS, hdim, mnc2.mnc_type, 
                        MI_CLASS_REAL, NULL, &hvol);
    if (r != 0) {
      TESTRPT("error creating volume", r);
      return (1);
    }

    r = micreate_volume_image(hvol);
    if (r != 0) {
      TESTRPT("error creating volume", r);
      return (1);
    }
    
    // read image slice by slice
    if (is_file)
      {
	read_3Dvff_file_image(hvol, argv[2], mnc2, vffattrs, mnc_vrange);
      }
    else 
      {
	read_2Dvff_files_image(hvol,file_list,num_files, mnc2, vffattrs, mnc_vrange);
      }
    miset_volume_valid_range(hvol,mnc_vrange[1], mnc_vrange[0]);

    if (mnc2.mnc_srange[0] == -1 || mnc2.mnc_srange[1] == -1) {
      /* min and max are not specified in the file
         voxel range is set same as real range */
      mnc2.mnc_srange[0] = mnc_vrange[0];
      mnc2.mnc_srange[1] = mnc_vrange[1];
    }
    miset_volume_range(hvol,mnc2.mnc_srange[1], mnc2.mnc_srange[0]);
    
    if (is_file) {
      /* create minc history 3D */
      strcat(vffattrs.cmd_line,G.minc_history);
      G.minc_history = vffattrs.cmd_line;
      /* attributes from vff file itself 3D case*/
      add_vff_attribute_to_file(hvol,&vffattrs);
    }

    if (G.dirname != NULL) {
      /* attributes from external files 3D case*/
      add_attributes_from_files(hvol);
    }
    else if (!is_file) {
      /* just afew attributes from 2D case */
      ival =  vffattrs.year;
      r = miset_attr_values(hvol, MI_TYPE_INT, "study",
			    MIstart_year,1 , &ival);
      if (r < 0) {
	TESTRPT("failed to add date:year attribute", r);
      }
      ival = vffattrs.month;
      r = miset_attr_values(hvol, MI_TYPE_INT, "study",
			    MIstart_month,1 , &ival);
      if (r < 0) {
	TESTRPT("failed to add date:month attribute", r);
      }
      ival = vffattrs.day;
      r = miset_attr_values(hvol, MI_TYPE_INT, "study",
			    MIstart_day,1 , &ival);
      if (r < 0) {
	TESTRPT("failed to add date:day attribute", r);
      }
    }

    /* add history attribute */
    r = miadd_history_attr(hvol,strlen(G.minc_history), G.minc_history);
    if (r < 0) {
	TESTRPT("error creating history", r);
	return (1);
      }
    
    if (file_list != NULL) {
      free_list(num_files, file_list);
      free(file_list);
    }
    miclose_volume(hvol);

    exit(EXIT_SUCCESS);
}
Ejemplo n.º 27
0
/*
   Given a minc filename, return a list containing:
   (1) the dimension names
   (2) the dimension sizes
   (3) and much, much more
   */
SEXP get_volume_info(SEXP filename) {

    mihandle_t          minc_volume;
	midimhandle_t		*dimensions;
	miclass_t			volume_class;
	mitype_t			volume_type;
	
	int					result, i;
	int					n_dimensions;
	misize_t    n_dimensions_misize_t;
	int					n_protects, list_index;
	int					n_frames;
	
// variables to hold dim-related info
	misize_t		dim_sizes[MI2_MAX_VAR_DIMS];
	double				dim_starts[MI2_MAX_VAR_DIMS];
	double				dim_steps[MI2_MAX_VAR_DIMS];
	double				time_offsets[MAX_FRAMES];
	double				time_widths[MAX_FRAMES];
	char 				*dim_name;
	char 				*dim_units;
	char 				*space_type;
	Rboolean			time_dim_exists;
	static char *dimorder3d[] = { "zspace","yspace","xspace" };
	static char *dimorder4d[] = { "time", "zspace","yspace","xspace" };
	

	/* declare R datatypes  */
	SEXP rtnList, listNames;
	SEXP xDimSizes, xDimNames, xDimUnits, xDimStarts, xDimSteps, xTimeWidths, xTimeOffsets;



	// start ...
	if ( R_DEBUG_mincIO ) Rprintf("get_volume_info: start ...\n");


	/* do some initialization */
	for (i=0; i < MI2_MAX_VAR_DIMS; ++i){					// set dim info to zeros
		dim_sizes[i] = 0;
		dim_starts[i] = 0;
		dim_steps[i] = 0;
	}

	// frame-related init
	time_dim_exists = FALSE;
	for (i=0; i < MAX_FRAMES; ++i) {
		time_offsets[i]=999.9;
		time_widths[i]=999.9;
	}
	n_frames = 0;

	n_protects = 0;								// counter of protected R variables



	/* init the return list (include list names) */
	PROTECT(rtnList=allocVector(VECSXP, R_RTN_LIST_LEN));
	PROTECT(listNames=allocVector(STRSXP, R_RTN_LIST_LEN));
	n_protects = n_protects +2;


	/* open the existing volume */
	result = miopen_volume(CHAR(STRING_ELT(filename,0)), MI2_OPEN_READ, &minc_volume);
	/* error on open? */
	if (result != MI_NOERROR) {
		error("Error opening input file: %s.\n", CHAR(STRING_ELT(filename,0)));
	}

	/* set the apparent order to something conventional */
	//	... first need to get the number of dimensions
	if ( miget_volume_dimension_count(minc_volume, MI_DIMCLASS_ANY, MI_DIMATTR_ALL, &n_dimensions) != MI_NOERROR ){
		error("Error returned from miget_volume_dimension_count.\n");
	}
	n_dimensions_misize_t = (misize_t) n_dimensions;
	// ... now set the order
	if ( R_DEBUG_mincIO ) Rprintf("Setting the apparent order for %d dimensions ... ", n_dimensions);
	if ( n_dimensions == 3 ) {
		result = miset_apparent_dimension_order_by_name(minc_volume, 3, dimorder3d);
	} else if ( n_dimensions == 4 ) {
		result = miset_apparent_dimension_order_by_name(minc_volume, 4, dimorder4d);
	} else {
		error("Error file %s has %d dimensions and we can only deal with 3 or 4.\n", CHAR(STRING_ELT(filename,0)), n_dimensions);
	}
	if ( result != MI_NOERROR ) { 
		error("Error returned from miset_apparent_dimension_order_by_name while setting apparent order for %d dimensions.\n", n_dimensions); 
	}
	if ( R_DEBUG_mincIO ) Rprintf("Done.\n");

	/* get the volume data class (the intended "real" values) */ 
	if ( miget_data_class(minc_volume, &volume_class) != MI_NOERROR ){
		error("Error returned from miget_data_class.\n");
	}
	/* append to return list ... */
	list_index = 0;
	SET_VECTOR_ELT(rtnList, list_index, ScalarInteger(volume_class));
	SET_STRING_ELT(listNames, list_index, mkChar("volumeDataClass"));


	/* print the volume data type (as it is actually stored in the volume) */
	if ( miget_data_type(minc_volume, &volume_type) != MI_NOERROR ){
		error("Error returned from miget_data_type.\n");
	}
	/* append to return list ... */
	list_index++;
	SET_VECTOR_ELT(rtnList, list_index, ScalarInteger(volume_type));
	SET_STRING_ELT(listNames, list_index, mkChar("volumeDataType"));


	/* retrieve the volume space type (talairach, native, etc) */
	result = miget_space_name(minc_volume, &space_type);
	if ( result == MI_NOERROR ) { error("Error returned from miget_space_name.\n"); }
	/* append to return list ... */
	list_index++;
	SET_VECTOR_ELT(rtnList, list_index, mkString(space_type));
	SET_STRING_ELT(listNames, list_index, mkChar("spaceType"));


	/* retrieve the total number of dimensions in this volume */
	if ( miget_volume_dimension_count(minc_volume, MI_DIMCLASS_ANY, MI_DIMATTR_ALL, &n_dimensions) != MI_NOERROR ){
		error("Error returned from miget_volume_dimension_count.\n");
	}
	/* append to return list ... */
	list_index++;
	SET_VECTOR_ELT(rtnList, list_index, ScalarInteger(n_dimensions));
	SET_STRING_ELT(listNames, list_index, mkChar("nDimensions"));


	/* load up dimension-related information */
	//
	/* first allocate the R variables */
	PROTECT( xDimSizes=allocVector(INTSXP,n_dimensions) );
	PROTECT( xDimNames=allocVector(STRSXP,n_dimensions) );
	PROTECT( xDimUnits=allocVector(STRSXP,n_dimensions) );
	PROTECT( xDimStarts=allocVector(REALSXP,n_dimensions) );
	PROTECT( xDimSteps=allocVector(REALSXP,n_dimensions) );
	n_protects = n_protects +5;

	/* next, load up the midimension struct for all dimensions*/
	dimensions = (midimhandle_t *) malloc( sizeof( midimhandle_t ) * n_dimensions );
	result = miget_volume_dimensions(minc_volume, MI_DIMCLASS_ANY, MI_DIMATTR_ALL, MI_DIMORDER_APPARENT, n_dimensions, dimensions);
	// need to check against MI_ERROR, as "result" will contain nDimensions if OK
	if ( result == MI_ERROR ) { error("Error code(%d) returned from miget_volume_dimensions.\n", result); }


	/* get the dimension sizes for all dimensions */
	result = miget_dimension_sizes(dimensions, n_dimensions_misize_t, dim_sizes);
	if ( result != MI_NOERROR ) { error("Error returned from miget_dimension_sizes.\n"); }
	/* add to R vector ... */
	for (i=0; i<n_dimensions; ++i){
		INTEGER(xDimSizes)[i] = dim_sizes[i];
	}
	list_index++;
	SET_VECTOR_ELT(rtnList, list_index, xDimSizes);
	SET_STRING_ELT(listNames, list_index, mkChar("dimSizes"));


	/* get the dimension START values for all dimensions */
	result = miget_dimension_starts(dimensions, MI_ORDER_FILE, n_dimensions, dim_starts);
	if ( result == MI_ERROR ) { error("Error returned from miget_dimension_starts.\n"); }
	/* add to R vector ... */
	for (i=0; i<n_dimensions; ++i){
		REAL(xDimStarts)[i] = dim_starts[i];
	}
	list_index++;
	SET_VECTOR_ELT(rtnList, list_index, xDimStarts);
	SET_STRING_ELT(listNames, list_index, mkChar("dimStarts"));


	/* get the dimension STEP values for all dimensions */
	result = miget_dimension_separations(dimensions, MI_ORDER_FILE, n_dimensions, dim_steps);
	if ( result == MI_ERROR ) { error("Error returned from miget_dimension_separations.\n"); }
	/* add to R vector ... */
	for (i=0; i<n_dimensions; ++i){
		REAL(xDimSteps)[i] = dim_steps[i];
	}
	list_index++;
	SET_VECTOR_ELT(rtnList, list_index, xDimSteps);
	SET_STRING_ELT(listNames, list_index, mkChar("dimSteps"));


	/* Loop over the dimensions to grab the remaining info ... */
	for( i=0; i < n_dimensions; ++i ){
	//
	/* get (and print) the dimension names for all dimensions*
	... remember that since miget_dimension_name calls strdup which, in turn,
	... calls malloc to get memory for the new string -- we need to call "mifree" on
	... our pointer to release that memory.  */
		result = miget_dimension_name(dimensions[i], &dim_name);
		
		// do we have a time dimension?
		if ( !strcmp(dim_name, "time") ) { 
			time_dim_exists = TRUE;
			n_frames = ( time_dim_exists ) ? dim_sizes[0] : 0;
		}
		
		// store the dimension name and units
		SET_STRING_ELT(xDimNames, i, mkChar(dim_name));
		mifree_name(dim_name);
		
		result = miget_dimension_units(dimensions[i], &dim_units);
		SET_STRING_ELT(xDimUnits, i, mkChar(dim_units));
		mifree_name(dim_units);
		
	}
	/* add number of frames to return list */
	list_index++;
	SET_VECTOR_ELT(rtnList, list_index, ScalarInteger(n_frames));
	SET_STRING_ELT(listNames, list_index, mkChar("nFrames"));
	
	// add dim names to return list
	list_index++;
	SET_VECTOR_ELT(rtnList, list_index, xDimNames);
	SET_STRING_ELT(listNames, list_index, mkChar("dimNames"));
	// add dim units
	list_index++;
	SET_VECTOR_ELT(rtnList, list_index, xDimUnits);
	SET_STRING_ELT(listNames, list_index, mkChar("dimUnits"));


	/* get the dimension OFFSETS values for the TIME dimension */
	if ( time_dim_exists ) {

		PROTECT( xTimeOffsets=allocVector(REALSXP,n_frames) );
		n_protects++;
		result = miget_dimension_offsets(dimensions[0], n_frames, 0, time_offsets);
		if ( result == MI_ERROR ) { error("Error returned from miget_dimension_offsets.\n"); }
		/* add to R vector ... */
		for (i=0; i < n_frames; ++i) {
			REAL(xTimeOffsets)[i] = time_offsets[i];
//			if (R_DEBUG_mincIO) Rprintf("Time offset[%d] =  %g\n", i, time_offsets[i]);
		}
		list_index++;
		SET_VECTOR_ELT(rtnList, list_index, xTimeOffsets);
		SET_STRING_ELT(listNames, list_index, mkChar("timeOffsets"));

		/* get the dimension WIDTH values for the TIME dimension */
		PROTECT( xTimeWidths=allocVector(REALSXP,n_frames) );
		n_protects++;
	
		result = miget_dimension_widths(dimensions[0], MI_ORDER_FILE, n_frames, 0, time_widths);
		if ( result == MI_ERROR ) { error("Error returned from miget_dimension_widths.\n"); }
		/* add to R vector ... */
		for (i=0; i<n_frames; ++i) {
			REAL(xTimeWidths)[i] = time_widths[i];
//			if (R_DEBUG_mincIO) Rprintf("Time width[%d] =  %g\n", i, time_widths[i]);
		}
		list_index++;
		SET_VECTOR_ELT(rtnList, list_index, xTimeWidths);
		SET_STRING_ELT(listNames, list_index, mkChar("timeWidths"));
	}



	// free heap memory
	free(dimensions);


	/* close volume */
	miclose_volume(minc_volume);


	/* attach the list component names to the list */
	setAttrib(rtnList, R_NamesSymbol, listNames);


	/* remove gc collection protection */
	UNPROTECT(n_protects);

   /* return */
	if ( R_DEBUG_mincIO ) Rprintf("get_volume_info: returning ...\n");
   return(rtnList);
}
Ejemplo n.º 28
0
int main(int argc, char **argv)
{
    mihandle_t hvol;
    int r;
    mitype_t data_type;
    int length;
    static double tstarr[TESTARRAYSIZE] = { 
	1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10, 11.11
    };
    double dblarr[TESTARRAYSIZE];
    float fltarr[TESTARRAYSIZE];
    int intarr[TESTARRAYSIZE];
    char valstr[128];
    milisthandle_t hlist, h1list;
    char pathbuf[256];
    char namebuf[256];
    char pathbuf1[256];
    int count=0;

    r = micreate_volume("tst-grpa.mnc", 0, NULL, MI_TYPE_UINT, 
                        MI_CLASS_INT, NULL, &hvol);
    if (r < 0) {
	TESTRPT("Unable to create test file", r);
	return (-1);
    }

    r = micreate_group(hvol, "/", "test1");
    if (r < 0) {
        TESTRPT("micreate_group failed", r);
    }

    r = micreate_group(hvol, "/", "test2");
    if (r < 0) {
        TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/", "test3");
    if (r < 0) {
        TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/", "test4");
    if (r < 0) {
        TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test2", "stuff2");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test1", "stuff");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test1", "otherstuff");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test1", "theotherstuff");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test1/theotherstuff", "thisstuff");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test1/stuff", "hello");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test1/stuff", "helloleila");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    
    r = miset_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff/hello", 
			  "animal", 8, "fruitbat");
    if (r < 0) {
	TESTRPT("miset_attr_values failed", r);
    }

    r = miset_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff", 
			  "objtype", 10, "automobile");
    if (r < 0) {
	TESTRPT("miset_attr_values failed", r);
    }

    r = miset_attr_values(hvol, MI_TYPE_STRING, "/test3", 
			  "objtype", 10, "automobile");
    if (r < 0) {
	TESTRPT("miset_attr_values failed", r);
    }

    r = miset_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff", 
			  "objname", 10, "automobile");
    if (r < 0) {
	TESTRPT("miset_attr_values failed", r);
    }
    r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "/test2", 
			  "maxvals", TESTARRAYSIZE, tstarr);
    if (r < 0) {
	TESTRPT("miset_attr_values failed", r);
    }

    r = miget_attr_type(hvol, "/test1/stuff/hello", "animal", 
			&data_type);
    if (r < 0) {
	TESTRPT("miget_attr_type failed", r);
    }

    r = miget_attr_length(hvol, "/test1/stuff/hello", "animal", 
			  &length);
    if (r < 0) {
	TESTRPT("miget_attr_length failed", r);
    }

    if (data_type != MI_TYPE_STRING) {
	TESTRPT("miget_attr_type failed", data_type);
    }
    if (length != 8) {
	TESTRPT("miget_attr_length failed", length);
    }
    
    r = midelete_group(hvol, "/test1/stuff", "goodbye");
    if (r >= 0) {
	TESTRPT("midelete_group failed", r);
    }

    r = midelete_group(hvol, "/test1/stuff", "hello");
    /* This should succeed.
     */
    if (r < 0) {
	TESTRPT("midelete_group failed", r);
    }

    r = miget_attr_length(hvol, "/test1/stuff/hello", "animal", 
			  &length);
    /* This should fail since we deleted the group.
     */
    if (r >= 0) {
	TESTRPT("miget_attr_length failed", r);
    }

    r = miget_attr_values(hvol, MI_TYPE_DOUBLE, "/test2", "maxvals", 
			  TESTARRAYSIZE, dblarr);
    if (r < 0) {
	TESTRPT("miget_attr_values failed", r);
    }

    for (r = 0; r < TESTARRAYSIZE; r++) {
	if (dblarr[r] != tstarr[r]) {
	    TESTRPT("miget_attr_values mismatch", r);
	}
    }

    /* Get the values again in float rather than double format.
     */
    r = miget_attr_values(hvol, MI_TYPE_FLOAT, "/test2", "maxvals", 
			  TESTARRAYSIZE, fltarr);
    if (r < 0) {
	TESTRPT("miget_attr_values failed", r);
    }

    for (r = 0; r < TESTARRAYSIZE; r++) {
	if (fltarr[r] != (float) tstarr[r]) {
	    TESTRPT("miget_attr_values mismatch", r);
	    fprintf(stderr, "fltarr[%d] = %f, tstarr[%d] = %f\n",
		    r, fltarr[r], r, tstarr[r]);
	}
    }

    /* Get the values again in int rather than double format.
     */
    r = miget_attr_values(hvol, MI_TYPE_INT, "/test2", "maxvals", 
			  TESTARRAYSIZE, intarr);
    if (r < 0) {
	TESTRPT("miget_attr_values failed", r);
    }

    for (r = 0; r < TESTARRAYSIZE; r++) {
	if (intarr[r] != (int) tstarr[r]) {
	    TESTRPT("miget_attr_values mismatch", r);
	    fprintf(stderr, "intarr[%d] = %d, tstarr[%d] = %d\n",
		    r, intarr[r], r, (int) tstarr[r]);
	}
    }

    r = miget_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff", 
			  "objtype", 128, valstr);
    if (r < 0) {
	TESTRPT("miget_attr_values failed", r);
    }

    if (strcmp(valstr, "automobile") != 0) {
	TESTRPT("miget_attr_values failed", 0);
    }

    r = miset_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff",
			  "objtype", 8, "bicycle");

    if (r < 0) {
	TESTRPT("miset_attr_values failed on rewrite", r);
    }

    r = miget_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff", 
			  "objtype", 128, valstr);
    if (r < 0) {
	TESTRPT("miget_attr_values failed", r);
    }

    if (strcmp(valstr, "bicycle") != 0) {
	TESTRPT("miget_attr_values failed", 0);
    }

    
    r = milist_start(hvol, "/", 1, &hlist);
    if (r == MI_NOERROR) {
      count++;
      while (milist_attr_next(hvol, hlist, pathbuf, sizeof(pathbuf),
			      namebuf, sizeof(namebuf)) == MI_NOERROR) {
	printf(" %s %s\n", pathbuf, namebuf);
      }
    }
    milist_finish(hlist);
    
    printf("***************** \n");
    
    r = milist_start(hvol, "/", 1, &h1list);
    if (r == MI_NOERROR) {
      while( milist_grp_next(h1list, pathbuf1, sizeof(pathbuf1)) == MI_NOERROR) {
	printf("%s \n", pathbuf1);
      }
      
    }
    
    milist_finish(h1list);
	
    miclose_volume(hvol);

    if (error_cnt != 0) {
	fprintf(stderr, "%d error%s reported\n", 
		error_cnt, (error_cnt == 1) ? "" : "s");
    }
    else {
	fprintf(stderr, "No errors\n");
    }
    return (error_cnt);
}
Ejemplo n.º 29
0
int main(int argc, char **argv)
{
    mihandle_t vol;

    midimhandle_t dim[NDIMS];

    unsigned int sizes[NDIMS];
    unsigned long start[NDIMS];
    unsigned long count[NDIMS];
    unsigned long howfar[NDIMS];
    unsigned long location[NDIMS];
    double *buffer,value;
    int r = 0;

    static char *dimorder[] = {"xspace", "yspace", "zspace"};

    printf("Creating image with slice scaling!! \n");
    create_test_file();
    printf("Opening hyperslab-test2.mnc! \n");

    r = miopen_volume("hyperslab-test2.mnc", MI2_OPEN_READ, &vol);

    if (r < 0) {
        TESTRPT("failed to open image", r);
    }

#ifdef APPARENTORDER
    /* set the apparent dimension order to be xyz */
    r  = miset_apparent_dimension_order_by_name(vol, 3, dimorder);

    /* get the apparent dimensions and their sizes */
    r  = miget_volume_dimensions( vol, MI_DIMCLASS_SPATIAL,
                                  MI_DIMATTR_ALL, MI_DIMORDER_APPARENT,
                                  3, dim);
    r = miget_dimension_sizes( dim, 3, sizes );
#else
    /* get the apparent dimensions and their sizes */
    r = miget_volume_dimensions( vol, MI_DIMCLASS_SPATIAL,
                                 MI_DIMATTR_ALL, MI_DIMORDER_FILE,
                                 3, dim);
    r = miget_dimension_sizes( dim, 3, sizes );
#endif
    if (r == MI_NOERROR) {
        printf("Sizes: %d, %d, %d\n", sizes[0], sizes[1], sizes[2]);
    }
    else {
        fprintf(stderr, "Error getting dimension sizes\n");
    }
    /* try to play with hyperslab functions!! */
    start[0] = 4;
    start[1] = 3;
    start[2] = 5;

    howfar[0] = 120;
    howfar[1] = 180;
    howfar[2] = 110;

    count[0] = howfar[0] - start[0];
    count[1] = howfar[1] - start[1];
    count[2] = howfar[2] - start[2];

    /* Alocate memory for the hyperslab*/
    buffer = (double *)malloc(count[0] * count[1] * count[2] * sizeof(double));
    if (buffer == NULL) {
        fprintf(stderr, "Error allocation memory.\n");
        exit(-1);
    }

    /* Get real value hyperslab*/
    printf("\n");
    printf("Getting a real value hyperslab \n");
    printf("Starting at %d, %d, %d \n", start[0], start[1], start[2]);
    printf("Extending to %d, %d, %d \n", howfar[0], howfar[1], howfar[2]);
    printf("\n");
    if (miget_real_value_hyperslab(vol,MI_TYPE_DOUBLE, start, count, buffer)
            < 0) {
        fprintf(stderr, "Could not get hyperslab.\n");
        exit(-1);
    }
    /* set an arbitrary location to print values from */
    location[0] = 70;
    location[1] = 100;
    location[2] = 104;
    printf("Test arbitrary location %d, %d, %d \n",
           location[0], location[1], location[2]);
    miget_real_value(vol, location, 3, &value);
    printf("Test from hyperslab: %f \n",
           *( buffer + (location[0] - start[0])*count[1]*count[2] +
              (location[1]- start[1]) * count[2] + (location[2]- start[2])));
    printf("Test from voxel scaled: %f\n", value);
    miget_voxel_value(vol, location, 3, &value);
    printf("Test voxel value itself: %f\n", value);
    printf("\n");
    printf("HMMMMMMMMMM! let's try something else \n");
    printf("\n");
    /* set another arbitrary location to print values from */
    location[0] = 104;
    location[1] = 100;
    location[2] = 70;
    printf("Test arbitrary location %d, %d, %d \n",
           location[0], location[1], location[2]);
    miget_real_value(vol, location, 3, &value);
    printf("Test from hyperslab: %f \n",
           *( buffer + (location[0] - start[0])*count[1]*count[2] +
              (location[1]- start[1]) * count[2] + (location[2]- start[2])));
    printf("Test from voxel scaled: %f\n", value);
    miget_voxel_value(vol, location, 3, &value);
    printf("Test voxel value itself: %f\n", value);

    /* close volume*/
    miclose_volume(vol);

    if (error_cnt != 0) {
        fprintf(stderr, "%d error%s reported\n",
                error_cnt, (error_cnt == 1) ? "" : "s");
    }
    else {
        fprintf(stderr, "\n No errors\n");
    }

    return (error_cnt);
}
Ejemplo n.º 30
0
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Purpose: 
 *          
 *          
 *          
 *
 *          
*/
SEXP write_volume(SEXP filename, SEXP nDimensions, 
								SEXP dimLengths, 
								SEXP dimStarts, 
								SEXP dimSteps, 
								SEXP volumeDataType, 
								SEXP volumeRange, 
								SEXP hSlab) {

	mihandle_t			minc_volume;
	midimhandle_t		dim[MI2_MAX_VAR_DIMS];
	mivolumeprops_t		volume_properties;
	
	int				result;
	int				ndx;
	int				no_dimensions;
	int				dim_lengths[MI2_MAX_VAR_DIMS];
	double			dim_starts[MI2_MAX_VAR_DIMS];
	double			dim_steps[MI2_MAX_VAR_DIMS];
	double			volume_range_min, volume_range_max;
	int				volume_data_type;
	
	// pointer to the volume data
	double			*hSlab_p;
	misize_t	hSlab_start[MI2_MAX_VAR_DIMS];
	misize_t	hSlab_count[MI2_MAX_VAR_DIMS];


	// start ...
	if ( R_DEBUG_mincIO ) Rprintf("write_volume: start ...\n");

	/* init number of dimensions and their respective sizes
	  ... yes, I could get this myself, but it's best set in the calling R code */
	no_dimensions = INTEGER(nDimensions)[0];
	volume_data_type = INTEGER(volumeDataType)[0];
	volume_range_min = REAL(volumeRange)[0];
	volume_range_max = REAL(volumeRange)[1];
	
	// init volume data pointer
	hSlab_p = REAL(hSlab);
	
	
	// init lenghts, steps, and starts
	for (ndx=0; ndx < no_dimensions; ++ndx) {
		dim_lengths[ndx]  = INTEGER(dimLengths)[ndx];
		hSlab_count[ndx]  = INTEGER(dimLengths)[ndx];
		dim_starts[ndx]  = REAL(dimStarts)[ndx];
		dim_steps[ndx]  = REAL(dimSteps)[ndx];
	}


	// set the properties for the new output volume
	// ... no compression, no chunking, no multi-resolution, ... nothin fancy
	result = minew_volume_props(&volume_properties);
	if (result != MI_NOERROR) {
		error("write_volume:minew_volume_props: Error setting output volume properties: %s.\n", CHAR(STRING_ELT(filename, 0)));
	}
	result = miset_props_compression_type(volume_properties, MI_COMPRESS_NONE);
	if (result != MI_NOERROR) {
		error("write_volume:miset_props_compression_type: Error setting output volume properties: %s.\n", CHAR(STRING_ELT(filename, 0)));
	}
	result = miset_props_multi_resolution(volume_properties, FALSE, 1);
	if (result != MI_NOERROR) {
		error("write_volume:miset_props_multi_resolution: Error setting output volume properties: %s.\n", CHAR(STRING_ELT(filename, 0)));
	}

	/*	create the 3 output dimensions in the order Z, Y, X, as the volume
		is stored in this order */
	// z-dim
	result = micreate_dimension("zspace", 
								MI_DIMCLASS_SPATIAL,
								MI_DIMATTR_REGULARLY_SAMPLED, 
								dim_lengths[0],
								&dim[0]);
	//
	if (result != MI_NOERROR) {
		error("write_volume: Error initializing the dimension struct for %s dimension.\n", "zspace");
	}
	
	// y-dim
	result = micreate_dimension("yspace", 
								MI_DIMCLASS_SPATIAL,
								MI_DIMATTR_REGULARLY_SAMPLED, 
								dim_lengths[1],
								&dim[1]);
	//
	if (result != MI_NOERROR) {
		error("write_volume: Error initializing the dimension struct for %s dimension.\n", "yspace");
	}
	
	// x-dim
	result = micreate_dimension("xspace", 
								MI_DIMCLASS_SPATIAL,
								MI_DIMATTR_REGULARLY_SAMPLED, 
								dim_lengths[2],
								&dim[2]);
	//
	if (result != MI_NOERROR) {
		error("write_volume: Error initializing the dimension struct for %s dimension.\n", "xspace");
	}


	// set the start values for each dimension
	result = miset_dimension_starts(dim, no_dimensions, dim_starts);
	if (result == MI_ERROR) {
		error("write_volume: Error setting dimension start values.\n");
	}

	// set the step values for each dimension
	result = miset_dimension_separations(dim, no_dimensions, dim_steps);
	if (result == MI_ERROR) {
		error("write_volume: Error setting dimension step values.\n");
	}


	// create the volume structure (no data yet, of course)
	result = micreate_volume(CHAR(STRING_ELT(filename, 0)),
								no_dimensions, 
								dim,
								volume_data_type,
								MI_CLASS_REAL,
								volume_properties,
								&minc_volume);
	//
	if (result != MI_NOERROR) {
		error("write_volume: Error creating output volume structure: %s.\n", CHAR(STRING_ELT(filename, 0)));
	}
		
	// create the path to the image data
	result = micreate_volume_image(minc_volume);
	//
	if (result != MI_NOERROR) {
		error("write_volume: Error writing data to volume %s.\n", CHAR(STRING_ELT(filename, 0)));
	}


	// set valid and real ranges 
	// ... 0xFFFF=65535=16-bits (unsigned)
	miset_volume_valid_range(minc_volume, 0x7FFF, 0);
	miset_volume_range(minc_volume, volume_range_max, volume_range_min);


	// write  hyperslab (entire volume)
	hSlab_start[0] = hSlab_start[1] = hSlab_start[2] = 0;
	if ( R_DEBUG_mincIO ) Rprintf("hSlab_count [0..2] = %d, %d, %d\n", 
		hSlab_count[0], hSlab_count[1], hSlab_count[2]);
			
	result = miset_real_value_hyperslab(minc_volume, MI_TYPE_DOUBLE, 
														hSlab_start,
														hSlab_count,
														hSlab_p);
	if ( result != MI_NOERROR ) {
		error("Error in miset_real_value_hyperslab: %s.\n", CHAR(STRING_ELT(filename, 0)));
	}




	// free resources
        //
        // in the current version of minc (libsrc2/volume.c), these volume properties
        // as well as the dimension handles are already freed by the miclose_volume function.
        //
	//mifree_volume_props(volume_properties);								// free the property list
	//for ( ndx=0; ndx<no_dimensions; ++ndx) {							// free the dimhandles
	//	mifree_dimension_handle(dim[ndx]);
	//}
	// close new volume
	result = miclose_volume(minc_volume);
	if (result != MI_NOERROR) {
		error("write_volume: Error closing newly created volume %s.\n", CHAR(STRING_ELT(filename, 0)));
	}

	// done, return NULL
	if ( R_DEBUG_mincIO ) Rprintf("write_volume: returning ...\n");
	return(R_NilValue);
}