Esempio n. 1
0
void		dim_loop(int fd, int dimensions_nb, t_vol *volume,
			 t_tissue_stack *t, char *id_percent,
			 int slice_resume, int dimension_resume)
{
  int		dim = 0;
  int		slice = 0;
  int		this_slice = 0;
  int		size;
  char		*hyperslab;
  int		i;
  unsigned long		*start;
  long unsigned int	*count;
  short			cancel = 0;

  if (dimension_resume > -1)
    dim = dimension_resume;
  start = malloc(volume->dim_nb * sizeof(*start));
  count = malloc(volume->dim_nb * sizeof(*count));
  start[0] = start[1] = start[2] = 0;
  i = 0;
  while (i < volume->dim_nb)
    {
      count[i] = volume->size[i];
      i++;
    }
  printf("Dimensions size: size[0] ==> %i | size[1] ==> %i | size[2] ==> %i\n\n", (int)volume->size[0], (int)volume->size[1], (int)volume->size[2]);
  while (dim < dimensions_nb && cancel == 0)
    {
      size = (dim == 0 ? (volume->size[2] * volume->size[1]) :
	      (dim == 1 ? (volume->size[0] * volume->size[2]) : (volume->size[0] * volume->size[1])));
      hyperslab = malloc(size * sizeof(*hyperslab));
      slice = volume->size[dim];
      if (slice_resume != -1)
	{
	  this_slice = slice_resume;
	  slice_resume = -1;
	}
      else
	this_slice = 0;
      count[dim] = 1;
      while (this_slice < slice && cancel == 0)
	{
	  start[dim] = this_slice;
	  memset(hyperslab, '\0', size);
	  miget_real_value_hyperslab(volume->minc_volume, MI_TYPE_UBYTE, (misize_t*)start, (misize_t*)count, hyperslab);
	  write(fd, hyperslab, size);
	  printf("Slice = %i / %i - dim = %i\r", this_slice, (int)volume->size[dim], dim);
	  fflush(stdout);
	  this_slice++;
	}
      start[dim] = 0;
      count[dim] = volume->size[dim];
      dim++;
      free(hyperslab);
      printf("                                                                                                                                          \r");
    }
}
Esempio n. 2
0
void		dim_loop(int fd, int dimensions_nb, t_vol *volume,
			 t_tissue_stack *t, char *id_percent,
			 int slice_resume, int dimension_resume)
{
  int		dim = 0;
  int		slice = 0;
  int		this_slice = 0;
  int		size;
  char		*hyperslab;
  int		i;
  unsigned long		*start;
  long unsigned int	*count;
  short			cancel = 0;

  if (dimension_resume > -1)
    dim = dimension_resume;
  start = malloc(volume->dim_nb * sizeof(*start));
  count = malloc(volume->dim_nb * sizeof(*count));
  start[0] = start[1] = start[2] = 0;
  i = 0;
  while (i < volume->dim_nb)
    {
      count[i] = volume->size[i];
      i++;
    }
  while (dim < dimensions_nb && cancel == 0)
    {
      size = (dim == 0 ? (volume->size[2] * volume->size[1]) :
	      (dim == 1 ? (volume->size[0] * volume->size[2]) : (volume->size[0] * volume->size[1])));
      hyperslab = malloc(size * sizeof(*hyperslab));
      slice = volume->size[dim];
      if (slice_resume != -1)
	{
	  this_slice = slice_resume;
	  slice_resume = -1;
	}
      else
	this_slice = 0;
      count[dim] = 1;
      while (this_slice < slice && cancel == 0)
	{
	  start[dim] = this_slice;
	  memset(hyperslab, '\0', size);
	  miget_real_value_hyperslab(volume->minc_volume, MI_TYPE_UBYTE, start, count, hyperslab);
	  write(fd, hyperslab, size);
	  DEBUG("Slice = %i - dim = %i", this_slice, dim);
	  this_slice++;
	  t->percent_add(1, id_percent, t);
	  cancel = t->is_percent_paused_cancel(id_percent, t);
	}
      start[dim] = 0;
      count[dim] = volume->size[dim];
      dim++;
      free(hyperslab);
    }
}
Esempio n. 3
0
void cautious_get_hyperslab(mihandle_t volume,
                            mitype_t buffer_data_type,
                            misize_t *voxel_offsets,
                            misize_t *sizes,
                            void *buffer,
                            string error_message){
  int res = miget_real_value_hyperslab(volume, buffer_data_type, voxel_offsets, sizes, buffer);
  if(res != MI_NOERROR){
    stop(error_message);
  }
}
Esempio n. 4
0
void get_mask_slice(mihandle_t hmask,
		    misize_t *sizes,
		    double *mask_buffer,
		    int slice_number) {
  int result;
  misize_t start[3];
  misize_t count[3];

  start[0] = slice_number; start[1] = 0; start[2] = 0;
  count[0] = 1; count[1] = sizes[1]; count[2] = sizes[2];

  result = miget_real_value_hyperslab(hmask, MI_TYPE_DOUBLE,
				      start, count, mask_buffer);
  if (result != MI_NOERROR) {
    error("Error getting mask slice %d.\n", slice_number);
  }
}
Esempio n. 5
0
/* get a real value hyperslab from file */
SEXP get_hyperslab2( SEXP filename,  SEXP start,  SEXP count, SEXP slab) {
  int                result;
  mihandle_t         hvol;
  int                i;
  misize_t      tmp_start[3];
  misize_t      tmp_count[3];

  /*
  char **c_filename;
  int *c_start;
  int *c_count;
  double *c_slab;
  */
  /* open the volume */
  
  Rprintf("Crap %s\n", CHAR(STRING_ELT(filename, 0)));
  result = miopen_volume(CHAR(STRING_ELT(filename,0)),
			 MI2_OPEN_READ, &hvol);
  if (result != MI_NOERROR) {
    error("Error opening input file: %s.\n", CHAR(STRING_ELT(filename,0)));
  }

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

  /* get the hyperslab */
  Rprintf("Start: %i %i %i\n", INTEGER(start)[0], INTEGER(start)[1], INTEGER(start)[2]);
  Rprintf("Count: %i %i %i\n", INTEGER(count)[0], INTEGER(count)[1], INTEGER(count)[2]);
  if (miget_real_value_hyperslab(hvol, 
				 MI_TYPE_DOUBLE, 
				 tmp_start, 
				 tmp_count, 
				 REAL(slab))
      < 0) {
    error("Could not get hyperslab.\n");
  }
  return(slab);
}
Esempio n. 6
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;
}
Esempio n. 7
0
void fill_slice_buffer(SEXP filenames, 
		       misize_t *sizes,
		       mihandle_t *hvol,
		       double **buffer,
		       int slice_number) {
  int num_files, result, i;
  misize_t start[3];
  misize_t count[3];

  //Rprintf("Sizes in fill_slice: %d %d %d\n",
  //sizes[0], sizes[1], sizes[2]);
  Rprintf("%d ", slice_number);

  start[0] = slice_number; start[1] = 0; start[2] = 0;
  count[0] = (misize_t) 1; 
  count[1] = sizes[1]; 
  count[2] = sizes[2];
  //count[1] = 1;
  //count[2] = 1;

  num_files = LENGTH(filenames);
  for (i=0; i < num_files; i++) {
    //Rprintf("buffer: %p\n", buffer[0]);
    //Rprintf("fill_slice_buffer: f %d, start %lu %lu %lu, count %lu %lu %lu\n",
    //i, start[0], start[1], start[2], 
    //count[0], count[1], count[2]);
    result = miget_real_value_hyperslab(hvol[i], 
					MI_TYPE_DOUBLE,
					start, 
					count, 
					buffer[i]);
    //Rprintf("hs results: %d\n", result);
    if (result != MI_NOERROR) {
      error("Error getting data from slice %d.\n", slice_number);
    }
  }
}
Esempio n. 8
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);
}
Esempio n. 9
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);
}