int main(int argc, char **argv) { miclass_t myclass = MI_CLASS_REAL; mitype_t mytype = MI_TYPE_UNKNOWN; misize_t mysize = 0; char *myname = NULL; mihandle_t volume = NULL; /* Turn off automatic error reporting. */ H5Eset_auto2(H5E_DEFAULT, NULL, NULL); /* Check each file. */ while (--argc > 0) { ++argv; if (micreate_volume(*argv, 0, NULL, MI_TYPE_INT, 0, NULL, &volume) < 0) { /*type 0 is equivalent to H5T_INTEGER */ fprintf(stderr, "Error opening %s\n", *argv); } else { int i; /* Repeat many times to expose resource leakage problems, etc. */ for (i = 0; i < 25000; i++) { miget_data_type(volume, &mytype); miget_data_type_size(volume, &mysize); miget_data_class(volume, &myclass); miget_space_name(volume, &myname); mifree_name(myname); } miclose_volume(volume); printf("file: %s type %d size %lld class %d\n", *argv, mytype, mysize, myclass); } } return (0); }
/* 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); }
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); }
void MINCResource::Load() { if (loaded) return; int result = miopen_volume(file.c_str(), MI2_OPEN_READ, &handle); if (result != MI_NOERROR) { logger.warning << "Error opening the MINC input file: " << file << logger.end; return; } // char** names = new char*[3]; // names[0] = "x_space"; // names[1] = "y_space"; // names[2] = "z_space"; // miset_apparent_dimension_order_by_name(handle, 3, names); int count; miget_volume_voxel_count(handle, &count); logger.info << "voxel count: " << count << logger.end; // atomic type of the resulting texture (float, int) miclass_t klass; miget_data_class(handle, &klass); logger.info << "data class: " << klass << logger.end; // data type of a voxel (float, uchar, ...) // convert this type into class type by scaling. mitype_t type; miget_data_type(handle, &type); logger.info << "voxel type: " << type << logger.end; misize_t vsize; miget_data_type_size(handle, &vsize); logger.info << "voxel size in bytes: " << vsize << logger.end; midimhandle_t dims[3]; result = miget_volume_dimensions(handle, MI_DIMCLASS_SPATIAL, MI_DIMATTR_ALL, MI_DIMORDER_FILE, 3, dims); if (result != 3) { logger.warning << "Only three spatial dimensions supported. Volume has " << result << logger.end; return; } w = h = d = 0; // hack: in our files dimensions are given in z,y,x order, so we reverse it. miget_dimension_size(dims[0], &w); miget_dimension_size(dims[1], &h); miget_dimension_size(dims[2], &d); // midimhandle_t* dims_app = new midimhandle_t[3]; // dims_app[0] = dims[2]; // dims_app[1] = dims[1]; // dims_app[2] = dims[0]; // miset_apparent_dimension_order (handle, 3, dims_app); logger.info << "dimensions: " << w << " x " << h << " x " << d << logger.end; // count records int recs = 0; miget_record_length(handle, &recs); logger.info << "record length: " << recs << logger.end; // count labels int lbls = 0; miget_number_of_defined_labels(handle, &lbls); logger.info << "# of labels: " << lbls << logger.end; // count attributes milisthandle_t lhandle; milist_start(handle, "", 0, &lhandle); char* path = new char[255]; char* name = new char[255]; while (milist_attr_next(handle, lhandle, path, 255, name, 255) == MI_NOERROR) { logger.info << "path: " << string(path) << " name: " << string(name) << logger.end; } milist_finish(lhandle); delete path; delete name; // char* nm; // miget_dimension_name(dims[2], &nm); // string hest(nm); // mifree_name(nm); // logger.info << "dimension name: " << hest << logger.end; char* space_name; miget_space_name(handle, &space_name); logger.info << "space name: " << string(space_name) << logger.end; mifree_name(space_name); loaded = true; }