/* ----------------------------- MNI Header ----------------------------------- @NAME : save_volume_slice @INPUT : icvid - id of icv to write volume_info - volume information (minimum and maximum are ignored) slice_num - number of slice to write image - image to write slice_min - minimum real value for slice slice_max - maximum real value for slice @OUTPUT : (nothing) @RETURNS : (nothing) @DESCRIPTION: Routine to write out a slice. @METHOD : @GLOBALS : @CALLS : @CREATED : August 26, 1993 (Peter Neelin) @MODIFIED : ---------------------------------------------------------------------------- */ static void save_volume_slice(int icvid, Volume_Info *volume_info, int slice_num, unsigned char *image, double slice_min, double slice_max) { int mincid; long start[MAX_VAR_DIMS], count[MAX_VAR_DIMS]; /* Get the minc file id */ (void) miicv_inqint(icvid, MI_ICV_CDFID, &mincid); /* Set up the start and count variables for writinging the volume */ (void) miset_coords(3, 0, start); start[0] = slice_num; count[0] = 1; count[1] = volume_info->nrows; count[2] = volume_info->ncolumns; /* Write out the slice min and max */ (void) mivarput1(mincid, ncvarid(mincid, MIimagemin), start, NC_DOUBLE, NULL, &slice_min); (void) mivarput1(mincid, ncvarid(mincid, MIimagemax), start, NC_DOUBLE, NULL, &slice_max); /* Write out the volume */ (void) miicv_put(icvid, start, count, image); }
/* ----------------------------- MNI Header ----------------------------------- @NAME : read_volume_data @INPUT : icvid - id of icv to set up @OUTPUT : volume data @RETURNS : (nothing) @DESCRIPTION: Routine to read in the volume data. @METHOD : @GLOBALS : @CALLS : @CREATED : August 26, 1993 (Peter Neelin (mods B Pike Mar 30, 1994)) @MODIFIED : ---------------------------------------------------------------------------- */ void read_volume_data(int icvid, Volume_Info *volume_info, short *volume) { long start[MAX_VAR_DIMS], count[MAX_VAR_DIMS]; int idim, ndims; /* Get number of dimensions */ ndims = volume_info->number_of_dimensions; /* Set up the start and count variables for reading the volume */ (void) miset_coords(MAX_VAR_DIMS, 0, start); for(idim=0; idim<ndims; idim++){ count[idim] = volume_info->length[idim]; } /* if (ndims > 3){ for(idim=0; idim<ndims-3; idim++){ start[idim] = slider_image[idim]; count[idim] = 1; } } */ /* Read in the volume */ (void) miicv_get(icvid, start, count, volume); }
/* ----------------------------- MNI Header ----------------------------------- @NAME : get_volume_slice @INPUT : icvid - id of icv to read volume_info - info for volume slice_num - number of slice to read in (counting from zero) @OUTPUT : image - image that is read in @RETURNS : (nothing) @DESCRIPTION: Routine to read in an image. @METHOD : @GLOBALS : @CALLS : @CREATED : August 26, 1993 (Peter Neelin) @MODIFIED : ---------------------------------------------------------------------------- */ static void get_volume_slice(int icvid, Volume_Info *volume_info, int slice_num, unsigned char *image) { long start[MAX_VAR_DIMS], count[MAX_VAR_DIMS]; int offset, ndims; /* Get number of dimensions */ (void) miicv_inqint(icvid, MI_ICV_NUM_DIMS, &ndims); offset = ndims - NUMBER_OF_DIMENSIONS; /* Check slice_num */ if (slice_num >= volume_info->nslices) { (void) fprintf(stderr, "Slice %d is not in the file.\n", slice_num); exit(EXIT_FAILURE); } /* Set up the start and count variables for reading the volume */ (void) miset_coords(3, 0, start); if (offset >= 0) { start[offset] = slice_num; count[offset] = 1; } count[1+offset] = volume_info->nrows; count[2+offset] = volume_info->ncolumns; /* Read in the volume */ (void) miicv_get(icvid, start, count, image); }
/* ----------------------------- MNI Header ----------------------------------- @NAME : save_volume_slice @INPUT : icvid - id of icv to write volume_info - volume information (minimum and maximum are ignored) slice_num - number of slice to write image - image to write slice_min - minimum real value for slice slice_max - maximum real value for slice @OUTPUT : (nothing) @RETURNS : (nothing) @DESCRIPTION: Routine to write out a slice. @METHOD : @GLOBALS : @CALLS : @CREATED : August 26, 1993 (Peter Neelin) @MODIFIED : ---------------------------------------------------------------------------- */ void save_volume_slice(int icvid, Volume_Info *volume_info, int slice_num[], unsigned char *image, double slice_min, double slice_max) { int mincid; long start[MAX_VAR_DIMS], count[MAX_VAR_DIMS]; int idim, ndims; /* Get the minc file id */ (void) miicv_inqint(icvid, MI_ICV_CDFID, &mincid); ndims = volume_info->number_of_dimensions; /* Set up the start and count variables for writing the volume */ (void) miset_coords(MAX_VAR_DIMS, 0, start); for(idim=0; idim<ndims-2; idim++){ start[idim] = slice_num[idim]; count[idim] = 1; } count[ndims-2] = volume_info->length[ndims-2]; count[ndims-1] = volume_info->length[ndims-1]; /* Write out the slice min and max */ (void) mivarput1(mincid, ncvarid(mincid, MIimagemin), start, NC_DOUBLE, NULL, &slice_min); (void) mivarput1(mincid, ncvarid(mincid, MIimagemax), start, NC_DOUBLE, NULL, &slice_max); /* Write out the volume */ (void) miicv_put(icvid, start, count, image); }
/* ----------------------------- MNI Header ----------------------------------- @NAME : get_volume_slice @INPUT : icvid - id of icv to read volume_info - info for volume slice_num - number of slice to read in (counting from zero) @OUTPUT : image - image that is read in @RETURNS : (nothing) @DESCRIPTION: Routine to read in an image. @METHOD : @GLOBALS : @CALLS : @CREATED : August 26, 1993 (Peter Neelin) @MODIFIED : ---------------------------------------------------------------------------- */ void get_volume_slice(int icvid, Volume_Info *volume_info, int slice_num[], short *image) { long start[MAX_VAR_DIMS], count[MAX_VAR_DIMS]; int idim, ndims; /* Get number of dimensions */ ndims = volume_info->number_of_dimensions; /* Check slice_num */ for (idim=0; idim<ndims-2; idim++){ if (slice_num[idim] >= volume_info->length[idim]) { (void) fprintf(stderr, "%s %d is not in the file.\n", volume_info->dimension_names[idim], slice_num[idim]); exit_xdisp(EXIT_FAILURE); } } /* Set up the start and count variables for reading the volume */ (void) miset_coords(MAX_VAR_DIMS, 0, start); for (idim=0; idim<ndims-2; idim++){ start[idim] = slice_num[idim]; count[idim] = 1; } count[ndims-2] = volume_info->length[ndims-2]; count[ndims-1] = volume_info->length[ndims-1]; /* Read in the volume */ (void) miicv_get(icvid, start, count, image); }
int main() { int cdf, cdf2; int img; int dim[MAX_VAR_DIMS]; int dim2[MAX_VAR_DIMS]; long start[MAX_VAR_DIMS]; long count[MAX_VAR_DIMS]; double image[256*256]; int i, j, k, ioff; char filename[256]; char filename2[256]; set_ncopts(NC_VERBOSE|NC_FATAL); snprintf(filename, sizeof(filename), "test_minc-%d.mnc", getpid()); snprintf(filename2, sizeof(filename2), "test_minc2-%d.mnc", getpid()); cdf=micreate(filename, NC_CLOBBER); count[2]=5; count[1]=3; count[0]=7; dim[2]=ncdimdef(cdf, MIzspace, count[2]); dim[1]=ncdimdef(cdf, MIxspace, count[1]); dim[0]=ncdimdef(cdf, MIyspace, count[0]); dim2[0]=ncdimdef(cdf, MItime, NC_UNLIMITED); dim2[1]=dim[0]; dim2[2]=dim[1]; img=ncvardef(cdf, MIimage, NC_SHORT, 3, dim); (void) ncvardef(cdf, "testvar", NC_FLOAT, 2, dim2); (void) miattputstr(cdf, img, MIsigntype, MI_SIGNED); for (j=0; j<count[0]; j++) { for (i=0; i<count[1]; i++) { ioff=(j*count[1]+i)*count[2]; for (k=0; k<count[2]; k++) image[ioff+k]=ioff+k+10; } } cdf2=micreate(filename2,NC_CLOBBER); (void) ncdimdef(cdf2, "junkdim", NC_UNLIMITED); (void) micopy_all_var_defs(cdf, cdf2, 1, &img); (void) ncendef(cdf2); (void) ncendef(cdf); (void) miset_coords(3,0L,start); (void) mivarput(cdf, img, start, count, NC_DOUBLE, NULL, image); (void) micopy_all_var_values(cdf, cdf2, 1, &img); (void) miclose(cdf2); (void) miclose(cdf); unlink(filename); unlink(filename2); return(0); }
int main(int argc, char **argv) { int cdf; int img, img2; int dim[MAX_VAR_DIMS]; long start[MAX_VAR_DIMS]; long count[MAX_VAR_DIMS]; double image[256*256]; int i, itype, jtype; int cflag = 0; char filename[256]; #if MINC2 if (argc == 2 && !strcmp(argv[1], "-2")) { cflag = MI2_CREATE_V2; } #endif /* MINC2 */ snprintf(filename, sizeof(filename), "test_minc_types-%d.mnc", getpid()); ncopts=NC_VERBOSE|NC_FATAL; for (itype=0; itype<ntypes; itype++) { for (jtype=0; jtype<ntypes; jtype++) { cdf=micreate(filename, NC_CLOBBER | cflag); count[2]=256; count[1]=20; count[0]=7; dim[2]=ncdimdef(cdf, MIzspace, count[2]); dim[1]=ncdimdef(cdf, MIxspace, count[1]); dim[0]=ncdimdef(cdf, MIyspace, count[0]); img=ncvardef(cdf, MIimage, types[itype].type, 1, dim); (void) miattputstr(cdf, img, MIsigntype, types[itype].sign); img2=ncvardef(cdf, "image2", types[jtype].type, 1, dim); (void) miattputstr(cdf, img2, MIsigntype, types[jtype].sign); image[0]=10.0; image[1]=2.0*(-FLT_MAX); image[2]=2.0*FLT_MAX; image[3]=3.2; image[4]=3.7; image[5]=(-3.2); image[6]=(-3.7); #if 0 for (j=0; j<count[0]; j++) { for (i=0; i<count[1]; i++) { ioff=(j*count[1]+i)*count[2]; for (k=0; k<count[2]; k++) image[ioff+k]=ioff+k+10; } } #endif (void) ncendef(cdf); (void) miset_coords(3,0L,start); (void) mivarput(cdf, img, start, count, NC_DOUBLE, NULL, image); (void) mivarget(cdf, img, start, count, types[jtype].type, types[jtype].sign, image); (void) mivarput(cdf, img2, start, count, types[jtype].type, types[jtype].sign, image); (void) mivarget(cdf, img2, start, count, NC_DOUBLE, NULL, image); (void) printf("Image 1 : %s %s\n",types[itype].sign, types[itype].ctype); (void) printf("Image 2 : %s %s\n",types[jtype].sign, types[jtype].ctype); for (i=0;i<count[0];i++) (void) printf(" image[%d] = %g\n",(int) i, image[i]); (void) miclose(cdf); } } unlink(filename); return(0); }
/* ----------------------------- MNI Header ----------------------------------- @NAME : find_mincfile_range @INPUT : mincid - id of minc file @OUTPUT : minimum - minimum for file maximum - maximum for file @RETURNS : (nothing) @DESCRIPTION: Routine to find the min and max in a minc file @METHOD : @GLOBALS : @CALLS : @CREATED : April 25, 1995 (Peter Neelin) @MODIFIED : ---------------------------------------------------------------------------- */ static void find_mincfile_range(int mincid, double *minimum, double *maximum) { int varid; char *varname; double sign, value; double *extreme; long index[MAX_VAR_DIMS], count[MAX_VAR_DIMS]; int ndims, dim[MAX_VAR_DIMS]; int idim, imm; int old_ncopts; *minimum = 0.0; *maximum = 1.0; for (imm=0; imm < 2; imm++) { /* Set up for max or min */ if (imm == 0) { varname = MIimagemin; sign = -1.0; extreme = minimum; } else { varname = MIimagemax; sign = 1.0; extreme = maximum; } /* Get the variable id */ old_ncopts = ncopts; ncopts = 0; varid = ncvarid(mincid, varname); ncopts = old_ncopts; if (varid == MI_ERROR) continue; /* Get the dimension info */ (void) ncvarinq(mincid, varid, NULL, NULL, &ndims, dim, NULL); for (idim=0; idim < ndims; idim++) { (void) ncdiminq(mincid, dim[idim], NULL, &count[idim]); } if (ndims <= 0) { ndims = 1; count[0] = 1; } /* Loop through values, getting extrema */ (void) miset_coords(ndims, (long) 0, index); *extreme = sign * (-DBL_MAX); while (index[0] < count[0]) { (void) mivarget1(mincid, varid, index, NC_DOUBLE, NULL, &value); if ((value * sign) > (*extreme * sign)) { *extreme = value; } idim = ndims-1; index[idim]++; while ((index[idim] > count[idim]) && (idim > 0)) { idim--; index[idim]++; } } } }
int main(int argc, char *argv[]) { char *filename; int mincid, imgid, icvid, ndims, dims[MAX_VAR_DIMS]; nc_type datatype; int is_signed; long start[MAX_VAR_DIMS], count[MAX_VAR_DIMS], end[MAX_VAR_DIMS]; long size; int idim; void *data; double temp; /* Check arguments */ if (ParseArgv(&argc, argv, argTable, 0) || (argc != 2)) { (void) fprintf(stderr, "\nUsage: %s [<options>] <mincfile>\n", argv[0]); (void) fprintf(stderr, " %s -help\n\n", argv[0]); exit(EXIT_FAILURE); } filename = argv[1]; /* Check that a normalization option was specified */ if (normalize_output == VIO_BOOL_DEFAULT) { (void) fprintf(stderr, "Please specify either -normalize or -nonormalize\n"); (void) fprintf(stderr, "Usually -normalize is most appropriate\n"); exit(EXIT_FAILURE); } /* Open the file */ mincid = miopen(filename, NC_NOWRITE); /* Inquire about the image variable */ imgid = ncvarid(mincid, MIimage); (void) ncvarinq(mincid, imgid, NULL, NULL, &ndims, dims, NULL); (void)miget_datatype(mincid, imgid, &datatype, &is_signed); /* Check if arguments set */ /* Get output data type */ if (output_datatype == INT_MAX) output_datatype = datatype; /* Get output sign */ if (output_signed == INT_MAX) { if (output_datatype == datatype) output_signed = is_signed; else output_signed = (output_datatype != NC_BYTE); } /* Get output range */ if (valid_range[0] == DBL_MAX) { if ((output_datatype == datatype) && (output_signed == is_signed)) { (void) miget_valid_range(mincid, imgid, valid_range); } else { (void) miget_default_range(output_datatype, output_signed, valid_range); } } if (valid_range[0] > valid_range[1]) { temp = valid_range[0]; valid_range[0] = valid_range[1]; valid_range[1] = temp; } /* Set up image conversion */ icvid = miicv_create(); (void) miicv_setint(icvid, MI_ICV_TYPE, output_datatype); (void) miicv_setstr(icvid, MI_ICV_SIGN, (output_signed ? MI_SIGNED : MI_UNSIGNED)); (void) miicv_setdbl(icvid, MI_ICV_VALID_MIN, valid_range[0]); (void) miicv_setdbl(icvid, MI_ICV_VALID_MAX, valid_range[1]); if ((output_datatype == NC_FLOAT) || (output_datatype == NC_DOUBLE)) { (void) miicv_setint(icvid, MI_ICV_DO_NORM, TRUE); (void) miicv_setint(icvid, MI_ICV_USER_NORM, TRUE); } else if (normalize_output) { (void) miicv_setint(icvid, MI_ICV_DO_NORM, TRUE); } (void) miicv_attach(icvid, mincid, imgid); /* Set input file start, count and end vectors for reading a slice at a time */ for (idim=0; idim < ndims; idim++) { (void) ncdiminq(mincid, dims[idim], NULL, &end[idim]); } (void) miset_coords(ndims, (long) 0, start); (void) miset_coords(ndims, (long) 1, count); size = nctypelen(output_datatype); for (idim=ndims-2; idim < ndims; idim++) { count[idim] = end[idim]; size *= count[idim]; } /* Allocate space */ data = malloc(size); /* Loop over input slices */ while (start[0] < end[0]) { /* Read in the slice */ (void) miicv_get(icvid, start, count, data); /* Write out the slice */ if (fwrite(data, sizeof(char), (size_t) size, stdout) != size) { (void) fprintf(stderr, "Error writing data.\n"); exit(EXIT_FAILURE); } /* Increment start counter */ idim = ndims-1; start[idim] += count[idim]; while ( (idim>0) && (start[idim] >= end[idim])) { start[idim] = 0; idim--; start[idim] += count[idim]; } } /* End loop over slices */ /* Clean up */ (void) miclose(mincid); (void) miicv_free(icvid); free(data); exit(EXIT_SUCCESS); }