コード例 #1
0
ファイル: vff2mnc.c プロジェクト: Angel-Fernandez/minc-tools
/* ----------------------------- MNI Header -----------------------------------
@NAME       : add_vff_attributes_to_file
@INPUT      : hvol volume handle
	      vattrs vff attributes to be added to minc2.0
@OUTPUT     : (nothing)
@RETURNS    :  
@DESCRIPTION: adds various attributes to minc file
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : Jul 2006 (Leila Baghdadi)
@MODIFIED   : 
---------------------------------------------------------------------------- */
int
add_vff_attribute_to_file(mihandle_t hvol,const struct vff_attrs *vattrs)
{
  int ival;
  double dval;
  int r;
  ival = vattrs->y_bin;
  r = miset_attr_values(hvol, MI_TYPE_INT, "acquisition",
			"ybin",1 , &ival);
  if (r < 0) {
    TESTRPT("failed to add y_bin attribute", r);
  }
  ival = vattrs->z_bin;
  r = miset_attr_values(hvol, MI_TYPE_INT, "acquisition",
			"z_bin",1 , &ival);
  if (r < 0) {
    TESTRPT("failed to add z_bin attribute", r);
  }
  ival = vattrs->bands;
  r = miset_attr_values(hvol, MI_TYPE_INT, "acquisition",
			"bands",1 , &ival);
  if (r < 0) {
   TESTRPT("failed to add bands attribute", r);
  }
  dval = vattrs->center_of_rotation;
  r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "acquisition",
			"center_of_rotation",1 , &dval);
  if (r < 0) {
    TESTRPT("failed to add center_of_rotation attribute", r);
  }
  dval = vattrs->central_slice;
  r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "acquisition",
			"central_slice",1 , &dval);
  if (r < 0) {
    TESTRPT("failed to add central_slice attribute", r);
  }
  dval = vattrs->rfan_y;
  r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "acquisition",
			"rfan_y",1 , &dval);
  if (r < 0) {
    TESTRPT("failed to add rfan_y attribute", r);
  }
  dval = vattrs->rfan_z;
  r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "acquisition",
			"rfan_z",1 , &dval);
  if (r < 0) {
    TESTRPT("failed to add rfan_z attribute", r);
  }
  dval = vattrs->angle_increment;
  r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "acquisition",
			"angle_increment",1 , &dval);
  if (r < 0) {
    TESTRPT("failed to add angle_increment attribute", r);
  }
  ival = vattrs->reverse_order;
  r = miset_attr_values(hvol, MI_TYPE_INT, "acquisition",
			"reverse_order",1 , &ival);
  if (r < 0) {
    TESTRPT("failed to add reverse_order attribute", r);
  }
  ival =  vattrs->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 = vattrs->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 = vattrs->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);
  }

  return(0);
}
コード例 #2
0
ファイル: hyper-test-2.c プロジェクト: BIC-MNI/minc
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);
}
コード例 #3
0
ファイル: vff2mnc.c プロジェクト: Angel-Fernandez/minc-tools
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);
}
コード例 #4
0
ファイル: vff2mnc.c プロジェクト: Angel-Fernandez/minc-tools
/* ----------------------------- MNI Header -----------------------------------
@NAME       : read_3Dvff_file_image
@INPUT      : hvol minc volume handle
              filename (vff filename)
	      m2 minc2 variables (struct)
	      vattrs vff attributes to be added to minc2.0
	      range (data min and max range)
	     
@OUTPUT     : (nothing)
@RETURNS    :  
@DESCRIPTION: Function to read the 3D vff file image
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : Jan 2007 (Leila Baghdadi)
@MODIFIED   : 
---------------------------------------------------------------------------- */
void
read_3Dvff_file_image(mihandle_t hvol, char *filename, 
		      struct mnc_vars m2, struct vff_attrs vattrs, 
		      double range[2])
{
  FILE *fp ;
  int i,counts,r;
  void *buffer;
  int number_of_bits = vattrs.bits/8;
  unsigned long start[MAX_VFF_DIMS]; /* MINC data starts */
  unsigned long count[MAX_VFF_DIMS];
  double valid_range[2];
 
  range[0] = DBL_MAX;
  range[1] = -DBL_MAX;

  start[1] = 0;
  start[2] = 0;

  count[1] = m2.mnc_count[1];
  count[2] = m2.mnc_count[0];
  
  counts = m2.mnc_count[0]*m2.mnc_count[1];

  /* open file */
  fp = fopen(filename  , "rb" );
  if( fp == NULL ) {
    exit(EXIT_FAILURE);  /* bad open? */
  }
    
  // Set file position indicator to beginning of image
  r = fseek(fp,-counts * m2.mnc_count[2] * number_of_bits,SEEK_END);
  if ( r != 0) {
    printf(" fseek is reporting a problem!!\n");
    exit(EXIT_FAILURE);
  }
     
  // allocate memory for one slice only
  buffer = malloc( counts * number_of_bits);
  CHKMEM(buffer);
  
  for (i = 0; i < m2.mnc_count[2]; i++)
    {
    // set start to the current slice
    start[0] = i;  
    count[0] = 1;
    
    // read data one slice at a time
    r = fread(buffer,sizeof(char),counts * number_of_bits,fp);
    if ( r == 0) {
      printf(" fread is reporting a problem leila.\n");
      exit(EXIT_FAILURE);
    }
    
    if (G.little_endian && vattrs.bits==16) {
     
      swab(buffer, buffer, counts * number_of_bits);
    }
    // write the slice
    r = miset_voxel_value_hyperslab(hvol, m2.mnc_type,
				    start, count, buffer);
    if (r != 0) {
      TESTRPT("can not write data with hperslab function",r);
      exit(EXIT_FAILURE);
    }
 
    //calculate min and max of slice
    
    computeScalarRange(m2.mnc_type,valid_range,counts,buffer);
   
    if (valid_range[0] < range[0]) {
      range[0] = valid_range[0];
    }
    if (valid_range[1] > range[1]) {
      range[1] = valid_range[1];
      }
    
    } 
  free(buffer);

  fclose(fp);
} 
コード例 #5
0
static int
check_dims(mihandle_t vol, midimhandle_t dim[])
{
    int i;
    int r;
    int n;
    mihandle_t vol_tmp;
    midimhandle_t dim_tmp[NDIMS];
    double offsets[100];

    for (i = 0; i < CT; i++) {
        double tmp = -1;

        r = miget_dimension_offsets(dim[0], 1, i, &tmp);
        if (r < 0) {
            TESTRPT("failed", r);
        }
        if ((i * i) + 100.0 != tmp) {
            TESTRPT("bad value", i);
        }
    }

    r = miget_dimension_offsets(dim[1], CX, 0, offsets);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    for (i = 0; i < CX; i++) {
        if (offsets[i] != XSTART + (i * XSTEP)) {
            TESTRPT("bad value", i);
        }
    }

    r = miget_dimension_offsets(dim[2], CY, 0, offsets);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    for (i = 0; i < CY; i++) {
        if (offsets[i] != YSTART + (i * YSTEP)) {
            TESTRPT("bad value", i);
        }
    }

    r = miget_dimension_offsets(dim[3], CZ, 0, offsets);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    for (i = 0; i < CZ; i++) {
        if (offsets[i] != ZSTART + (i * ZSTEP)) {
            TESTRPT("bad value", i);
        }
    }


    r = miget_volume_dimension_count(vol, MI_DIMCLASS_SPATIAL, MI_DIMATTR_ALL,
                                     &n);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    if (n != NDIMS-1) {
        TESTRPT("wrong number of spatial dimensions", n);
    }

    r = miget_volume_dimension_count(vol, MI_DIMCLASS_ANY, MI_DIMATTR_ALL,
                                     &n);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    if (n != NDIMS) {
        TESTRPT("wrong number of dimensions", n);
    }

    r = miget_volume_dimension_count(vol, MI_DIMCLASS_TIME, MI_DIMATTR_ALL, 
                                     &n);
    if (r < 0) {
        TESTRPT("failed", r);
    }
    if (n != 1) {
        TESTRPT("wrong number of time dimensions", n);
    }

    for (i = 0; i < NDIMS; i++) {
        miboolean_t flag_value;

        r = miget_dimension_sampling_flag(dim[i], &flag_value);
        if (r < 0) {
            TESTRPT("error getting sampling flag", r);
        }
        else if (flag_value != (i == 0)) {
            TESTRPT("wrong value for sampling flag", i);
        }
    }

    r = miget_volume_dimensions(vol, MI_DIMCLASS_ANY, MI_DIMATTR_ALL,
                                MI_DIMORDER_FILE, NDIMS, dim_tmp);
    if (r < 0) {
        TESTRPT("failed to get dimensions", r);
    }

    for (i = 0; i < NDIMS; i++) {
        vol_tmp = NULL;
        r = miget_volume_from_dimension(dim_tmp[i], &vol_tmp);
        if (r < 0) {
            TESTRPT("failed to get volume from dimension", r);
        }
        else if (vol_tmp != vol) {
            TESTRPT("wrong volume returned", i);
        }
    }
    return (error_cnt);
}
コード例 #6
0
ファイル: vff2mnc.c プロジェクト: Angel-Fernandez/minc-tools
/* ----------------------------- MNI Header -----------------------------------
@NAME       : add_attributes_from_files
@INPUT      : hvol volume handle
	      
@OUTPUT     : (nothing)
@RETURNS    :  
@DESCRIPTION: adds a few files generated with vff file to minc file
@METHOD     : This method assumes the txt files are in the top directory
@GLOBALS    : 
@CALLS      : 
@CREATED    : Jul 2006 (Leila Baghdadi)
@MODIFIED   : This code is a bit ugly thanks for the wodnerful work of windows.
---------------------------------------------------------------------------- */
int
add_attributes_from_files(mihandle_t hvol)
{
  FILE *inf;
  char *strbuf;
  char *buffer;
  string_t fullpath_pro;
  string_t fullpath_des;
  string_t fullpath_par;
  char *p;
  int r,i;
  char **str;
  int found_protocol=0;
  int found_description=0;
  int found_parameters=0;

  
  /* first find files names --> different platforms */
  find_filenames_first(fullpath_pro,fullpath_des,fullpath_par,
		       &found_protocol,&found_description, &found_parameters); 

  if (!found_protocol) {
    printf("could not find file with extension protocol\n");
  }
  else {
    /* add *.protocol to "acquisition" protocol attribute */
    inf = fopen(fullpath_pro,"r");
    if (inf == NULL) {
      printf("Could not open file %s \n", fullpath_pro);
      exit(EXIT_FAILURE);
    }
    else {
      strbuf = malloc(MAX_BUF_LINE + 1);
      CHKMEM(strbuf);
      strbuf[0]='\0';
      buffer = malloc(MAX_BUF_TEXT + 1);
      CHKMEM(buffer);
      buffer[0]='\0';
      while (fgets(strbuf, MAX_BUF_LINE, inf) != NULL) {
	strcat(buffer,strbuf);
      }
    }
    fclose(inf);
    
    r = miset_attr_values(hvol, MI_TYPE_STRING, "acquisition",
			  "protocol", strlen(buffer) ,buffer);
    free(buffer);
    free(strbuf);
    if (r < 0) {
      TESTRPT("failed to add protocol attribute", r);
    }
  }


  if (!found_description) {
    printf("Could not find file Description.txt\n");
  }
  else {
    /* add Description.txt to patient name and study id */
    inf = fopen(fullpath_des,"r");		       
    if (inf == NULL) {
      printf("Could not open file %s \n", fullpath_des);
      exit(EXIT_FAILURE);
    }
    else {
      /* just need the first two lines of this file */
      str = malloc(MAX_DESCRIPTION);
      CHKMEM(str);
      for (i=0; i < MAX_DESCRIPTION; i++) {
	str[i] = malloc(MAX_BUF_LINE + 1);
	CHKMEM(str[i]);
	if(fgets(str[i], MAX_BUF_LINE, inf) == NULL){
      fprintf(stderr, "Error with fgets on line %d of %s\n", 
         __LINE__, __FILE__);
      }
	for (p = str[i]; *p != '\0'; p++) {
	  if (*p == '\r') {
	    *p = '\0';
	  }
	}
      }
      r = miset_attr_values(hvol, MI_TYPE_STRING, "patient",
			    "full_name",strlen(str[0]) , str[0]);
      if (r < 0) {
	TESTRPT("failed to add full_name attribute", r);
      }
      
      r = miset_attr_values(hvol, MI_TYPE_STRING, "study",
			    "study_id",strlen(str[1]) , str[1]);
      if (r < 0) {
	TESTRPT("failed to add study_id attribute", r);
      }
      fclose(inf);
      for (i=0; i < MAX_DESCRIPTION; i++) {
	free(str[i]);
      }
      free(str);
    }
  }

  if (!found_parameters) {
    printf("Could not find file Parameters.txt\n");
  }
  else {
    /* add Parameters.txt to "acquisition" scan_parameters */
    inf = fopen(fullpath_par,"r");
    if (inf == NULL) {
      printf("Could not open file %s \n", fullpath_par);
      exit(EXIT_FAILURE);
    }
   else {
     strbuf = malloc(MAX_BUF_LINE + 1);
     CHKMEM(strbuf);
     strbuf[0]='\0';
     buffer = malloc(MAX_BUF_TEXT + 1);
     CHKMEM(buffer);
     buffer[0]='\0';
     while (fgets(strbuf, MAX_BUF_LINE, inf) != NULL) {
       strcat(buffer,strbuf);
     }
   }
    fclose(inf);
    
    r = miset_attr_values(hvol, MI_TYPE_STRING, "acquisition",
			  "scan_parameters",strlen(buffer) ,buffer);
    free(buffer);
    free(strbuf);
    if (r < 0) {
      TESTRPT("failed to add scan_parameters attribute", r);
    }
  }

  return(0);
}
コード例 #7
0
ファイル: multires-test.c プロジェクト: BIC-MNI/minc
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);
}
コード例 #8
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);
}
コード例 #9
0
ファイル: volprops-test.c プロジェクト: BIC-MNI/libminc
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);
}
コード例 #10
0
ファイル: minc2-record-test.c プロジェクト: BIC-MNI/libminc
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);
}
コード例 #11
0
ファイル: grpattr-test.c プロジェクト: jcupitt/minc
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);
}
コード例 #12
0
int main ( void )
{
  mihandle_t vol;
  int r = 0;
  midimhandle_t dim[NDIMS];
  misize_t lengths[NDIMS];
  midimhandle_t copy_dim[NDIMS];
  misize_t coords[NDIMS];
  misize_t count[NDIMS];
  int i, j;
  unsigned char * Atmp;
  midimclass_t dimension_class;
  int ndims;
  Atmp = ( unsigned char * ) malloc ( CX * CY * CZ * sizeof ( unsigned char ) );

  create_test_file();

  printf ( " \n" );
  printf ( "Opening vector-dimension file!\n" );
  printf ( " \n" );

  r = miopen_volume ( "example_vector2.mnc", MI2_OPEN_READ, &vol );

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

  r = miget_volume_dimension_count ( vol, MI_DIMCLASS_ANY, MI_DIMATTR_REGULARLY_SAMPLED, &ndims );
  if ( r < 0 ) {
    TESTRPT ( "failed to get number of dimensions", r );

  }

  printf ( "Total number of dimensions : %d \n", ndims );

  r = miget_volume_dimensions ( vol, MI_DIMCLASS_ANY, MI_DIMATTR_REGULARLY_SAMPLED, MI_DIMORDER_FILE, NDIMS, dim );

  if ( r < 0 ) {
    TESTRPT ( "Could not get dimension handles from volume", r );

  }

  r = miget_dimension_sizes ( dim, NDIMS, lengths );
  if ( r < 0 ) {
    TESTRPT ( " more trouble", r );

  }
  printf ( "Dimension Size in file order : " );
  for ( i = 0; i < NDIMS; i++ ) {
    printf ( " %lld ", lengths[i] );
  }
  printf ( " \n" );

  for ( i = 0; i < NDIMS; i++ ) {
    r = miget_dimension_class ( dim[i], &dimension_class );
    if ( r < 0 ) {
      TESTRPT ( "failed to get dimension class", r );
    }
    if ( dimension_class == MI_DIMCLASS_RECORD ) {
      printf ( "Dim class RECORD present check dim name for *vector_dimension*\n" );
    }
  }

  printf ( "Let's get the first 10 data values of each vector component (file order) \n" );

  coords[0] = coords[1] = coords[2] = 0;

  count[0] = CZ;
  count[1] = CY;
  count[2] = CX;
  count[3] = 1;
  printf ( " FILE ORDER --> zspace, yspace, xspace, vector_dimension \n" );
  for ( i = 0; i < 3; i++ ) {
    printf ( "Vector Componenet %d \n", i + 1 );
    coords[3] = i;
    r = miget_voxel_value_hyperslab ( vol, MI_TYPE_UBYTE, coords, count, Atmp );
    if ( r < 0 ) {
      TESTRPT ( "Failed to operate hyperslab function", r );
    }

    for ( j = 0; j < 10; j++ ) {
      printf ( " %u ", Atmp[j] );
    }
    printf ( " \n" );
  }
  printf ( "APPARENT ORDER --> vector_dimension, zspace, yspace, xspace\n" );

  // Set the apparent dimension order

  copy_dim[0] = dim[3];
  copy_dim[1] = dim[0];
  copy_dim[2] = dim[1];
  copy_dim[3] = dim[2];

  r = miset_apparent_dimension_order ( vol, NDIMS, copy_dim );
  if ( r < 0 ) {
    TESTRPT ( "failed to set apparent order", r );
  }

  coords[1] = coords[2] = coords[3] = 0;

  count[0] = 1; //must always be one
  count[1] = CZ;
  count[2] = CY;
  count[3] = CZ;

  printf ( "APPARENT ORDER SET \n" );
  for ( i = 0; i < 3; i++ ) {
    printf ( "Vector Componenet %d \n", i + 1 );
    coords[0] = i;
    r = miget_voxel_value_hyperslab ( vol, MI_TYPE_UBYTE, coords, count, Atmp );
    if ( r < 0 ) {
      TESTRPT ( "Failed to operate hyperslab function", r );
    }

    for ( j = 0; j < 10; j++ ) {
      printf ( " %u ", Atmp[j] );
    }
    printf ( " \n" );
  }

  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 );
}
コード例 #13
0
ファイル: minc2-grpattr-test.c プロジェクト: BIC-MNI/libminc
int main(void)
{
  mihandle_t hvol;
  mihandle_t hvol1;
  int r;
  mitype_t data_type;
  size_t 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]="";
  float val1=12.5;
  float val2=34.5;
  milisthandle_t hlist, h1list;
  char pathbuf[256]="";
  char namebuf[256]="";
  char pathbuf1[1024]="";
  int count=0;
  
  r = micreate_volume("tst-grpa.mnc", 0, NULL, MI_TYPE_UINT,
                      MI_CLASS_REAL, NULL, &hvol);
  if (r < 0) {
    TESTRPT("Unable to create test file", r);
    return (-1);
  }
  r = micreate_volume("tst-grpb.mnc", 0, NULL, MI_TYPE_UINT,
                      MI_CLASS_REAL, NULL, &hvol1);
  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);
  }
  if (data_type != MI_TYPE_STRING) {
    TESTRPT("miget_attr_type failed", data_type);
  }
  
  r = miget_attr_length(hvol, "/test1/stuff/hello", "animal",
                        &length);
  if (r < 0) {
    TESTRPT("miget_attr_length failed", r);
  }
  
  if (length != 8) {
    TESTRPT("miget_attr_length failed", (int)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 not 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);
    fprintf(stderr,"Expected :\"%s\" read \"%s\"\n","automobile",valstr);
  }

  /* Get the values again but this time with only enough space
     for the result with null termination.
   */
  memset(valstr, 0x55, sizeof(valstr));
  r = miget_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff",
                        "objtype", 11, valstr);
  if (r < 0) {
    TESTRPT("miget_attr_values failed", r);
  }
  if (strcmp(valstr, "automobile") != 0) {
    TESTRPT("miget_attr_values failed", 0);
  }

  /* Get the values again but this time with only enough space
     for the result and without null termination.
   */
  memset(valstr, 0x55, sizeof(valstr));
  r = miget_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff",
                        "objtype", 10, valstr);
  if (r < 0) {
    TESTRPT("miget_attr_values failed", r);
  }
  if (valstr[9] != 'e') {
    TESTRPT("miget_attr_values failed", 0);
  }
  if (valstr[10] != 0x55) {
    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 = miset_attr_values(hvol, MI_TYPE_FLOAT, "/OPT",
                        "zoom",1, &val1);
  
  if (r < 0) {
    TESTRPT("miset_attr_values failed", r);
  }
  
  r = miset_attr_values(hvol, MI_TYPE_FLOAT, "/OPT",
                        "binning",1, &val2);
  if (r < 0) {
    TESTRPT("miset_attr_values failed", r);
  }
  
  r = miset_attr_values(hvol, MI_TYPE_FLOAT, "/OPT",
                        "gain",1, &val1);
  if (r < 0) {
    TESTRPT("miset_attr_values failed", r);
  }

  r = milist_start(hvol, "/", 0, &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);
    }
  } else {
    TESTRPT("milist_start failed", r);
  }
  milist_finish(hlist);
  
  printf("copy all attributes in the provided path in the new volume\n");
  if((r = micopy_attr(hvol,"/OPT",hvol1))<0) 
    TESTRPT("micopy_attr failed", r);
  printf("***************** \n");

  r = milist_start(hvol1, "/", 1, &h1list);
  if (r == MI_NOERROR) {
    while( milist_grp_next(h1list, pathbuf1, sizeof(pathbuf1)-1) == MI_NOERROR) {
      printf("%s \n", pathbuf1);
    }
  } else {
    TESTRPT("milist_start failed", r);  
  }
  
  r = milist_finish(h1list);
  if(r<0)
  {
    TESTRPT("milist_finish failed", r);  
  }

  r = miclose_volume(hvol1);
  if(r<0)
  {
    TESTRPT("miclose_volume failed", r);  
  }
  
  r = miclose_volume(hvol);
  if(r<0)
  {
    TESTRPT("miclose_volume 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);
}