Exemplo n.º 1
0
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *  Purpose:
 *
 *    Convert world coordinates to a vector of voxel coords
 *
 * NOTE: 
 *    (1) 0-relative voxel coordinates are being returned
 *    (2) output voxel coordinates are in volume order, which we are forcing to be (t)zyx
 *    (3) input world coordinates are in xyz order
 *    (4) if the volume has 4D, then 4 voxel coords are returned, else 3 of 'em
 *
 * * * * * * * * * * * * * */
SEXP convert_world_to_voxel_mincIO(SEXP filename, SEXP worldCoords) {

	int			result, ndx;
	int			n_dimensions;
	mihandle_t	minc_volume;
	double		voxel_coords[MI2_MAX_VAR_DIMS];
	double		world_coords[MI2_3D];
	static char *dimorder3d[] = { "zspace","yspace","xspace" };
	static char *dimorder4d[] = { "time", "zspace","yspace","xspace" };
	
	SEXP		output;



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

	// init the world coordinates
	for ( ndx=0; ndx < MI2_3D; ++ndx ) {
		world_coords[ndx] = REAL(worldCoords)[ndx];
	}

	/* open the volume */
	result = miopen_volume(CHAR(STRING_ELT(filename,0)), MI2_OPEN_READ, &minc_volume);
	//
	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");
	}
	/* ... now set the order */
	if ( R_DEBUG_mincIO ) Rprintf("convert_world_to_voxel: 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");


	// do the call
	miconvert_world_to_voxel(minc_volume,  world_coords, voxel_coords);

	// allocate output vector and then copy the results into it
	PROTECT(output=allocVector(REALSXP, n_dimensions));
	for ( ndx=0; ndx < n_dimensions; ++ndx ) {
		REAL(output)[ndx] = voxel_coords[ndx];
	}

	// remove protection and return vector
	UNPROTECT(1);
	if ( R_DEBUG_mincIO ) Rprintf("convert_world_to_voxel: returning ...\n");
	return(output);
}
Exemplo n.º 2
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);
}
Exemplo n.º 3
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);
}
Exemplo n.º 4
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);
}
Exemplo n.º 5
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);
}
Exemplo n.º 6
0
t_vol		*init_get_volume_from_minc_file(char *path)
{
  t_vol		*volume;
  int		result;

  volume = malloc(sizeof(*volume));
  volume->path = path;
  volume->dim_nb = 3;
  if (volume->path == NULL)
    return (NULL);
  // open the minc file
  if ((result = miopen_volume(volume->path, MI2_OPEN_READ, &volume->minc_volume)) != MI_NOERROR)
    {
      ERROR("Error opening input file: %d.", result);
      return (NULL);
    }

  if ((result = miget_volume_dimension_count(volume->minc_volume, 0, 0, &volume->dim_nb)) != MI_NOERROR)
    {
      ERROR("Error getting number of dimensions: %d.", result);
      return (NULL);
    }

  volume->dimensions = malloc(volume->dim_nb * sizeof(*volume->dimensions));
  volume->starts = malloc(volume->dim_nb * sizeof(*volume->starts));
  volume->steps = malloc(volume->dim_nb * sizeof(*volume->steps));
  volume->size = malloc(volume->dim_nb * sizeof(*volume->size));
  volume->dim_name = malloc(volume->dim_nb * sizeof(*volume->dim_name));

  // get the volume dimensions
  if ((result = miget_volume_dimensions(volume->minc_volume, MI_DIMCLASS_SPATIAL, MI_DIMATTR_ALL,
					MI_DIMORDER_FILE, volume->dim_nb, volume->dimensions)) == MI_ERROR)
    {
      ERROR("Error getting dimensions: %d.", result);
      return (NULL);
    }
  // get the size of each dimensions
  if ((result = miget_dimension_sizes(volume->dimensions, volume->dim_nb, volume->size)) != MI_NOERROR)
    {
      ERROR("Error getting dimensions size: %d.", result);
      return (NULL);
    }
  if ((result = miget_dimension_starts(volume->dimensions, 0, volume->dim_nb, volume->starts)) != MI_NOERROR)
    {
      ERROR("Error getting dimensions start: %d.", result);
      return (NULL);
    }
  if ((result = miget_dimension_separations(volume->dimensions, 0, volume->dim_nb, volume->steps)) != MI_NOERROR)
    {
      ERROR("Error getting dimensions steps: %d.", result);
      return (NULL);
    }
  if (miget_dimension_name (volume->dimensions[0], &volume->dim_name[0]) != MI_NOERROR ||
      miget_dimension_name (volume->dimensions[1], &volume->dim_name[1]) != MI_NOERROR ||
      miget_dimension_name (volume->dimensions[2], &volume->dim_name[2]))
    {
      ERROR("Error getting dimensions name.");
      return (NULL);
    }
  // get slices_max
  volume->slices_max = get_slices_max(volume);
  volume->next = NULL;
  return (volume);
}
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 );
}