int main(int argc, char *argv[]) { char *me, *fileS; FILE *file; unsigned int llen; NrrdIoState *io; me = argv[0]; if (2 != argc) { /* 0 1 (2) */ fprintf(stderr, "usage: %s <file>\n", me); exit(1); } fileS = argv[1]; if (!( file = myopen(fileS) )) { fprintf(stderr, "%s: couldn't open \"%s\" for reading\n", me, fileS); exit(1); } io = nrrdIoStateNew(); do { if (_nrrdOneLine(&llen, io, file)) { fprintf(stderr, "%s: trouble:\n%s", me, biffGet(NRRD)); exit(1); } if (llen) { printf("%2u |%s|\n", llen, io->line); } } while(llen > 0); nrrdIoStateNix(io); myclose(file); exit(0); }
int main(int argc, char **argv) { char *me, *err, *key="strong bad", *value; Nrrd *nrrd; NrrdIoState *io; me = argv[0]; if (2 != argc) usage(me); io = nrrdIoStateNew(); nrrdStateVerboseIO = 10; if (nrrdLoad(nrrd=nrrdNew(), argv[1], NULL)) { fprintf(stderr, "%s: trouble loading \"%s\":\n%s", me, argv[1], err = biffGetDone(NRRD)); free(err); exit(1); } if ((value = nrrdKeyValueGet(nrrd, key))) { fprintf(stderr, "%s: '%s':='%s' (%d)\n", me, key, value, (int)strlen(value)); } else { fprintf(stderr, "%s: value not found for key: %s\n", me, key); } nrrdIoStateNix(io); nrrdNuke(nrrd); exit(0); }
int main(int argc, char **argv) { char *me, *err; Nrrd *nrrd; NrrdIoState *nio; char hstr[] = "NRRD0001\n" "# Complete NRRD file format specification at:\n" "# http://teem.sourceforge.net/nrrd/format.html\n" "# one comment\n" "# two comment\n" "# three comment\n" "type: float\n" "dimension: 2\n" "sizes: 91 114\n" "centerings: node node\n" "endian: big\n" "encoding: raw\n" "bingo:=bob\n" "foo:=super duper fancy bar with corona\n" /* "data file: tmp.raw\n" */; char *wstr; AIR_UNUSED(argc); me = argv[0]; nrrdStateVerboseIO = 10; nio = nrrdIoStateNew(); nrrd = nrrdNew(); nio->path = airStrdup("."); if (nrrdStringRead(nrrd, hstr, nio)) { fprintf(stderr, "%s: trouble reading string:\n%s", me, err = biffGet(NRRD)); free(err); exit(1); } fprintf(stderr, "%s: nrrd->data = %p\n", me, nrrd->data); nrrdSave("out.nrrd", nrrd, NULL); if (nrrdStringWrite(&wstr, nrrd, NULL)) { fprintf(stderr, "%s: trouble writing string:\n%s", me, err = biffGet(NRRD)); free(err); exit(1); } fprintf(stderr, "%s: |%s|\n", me, wstr); free(wstr); nrrdIoStateNix(nio); nrrdNuke(nrrd); exit(0); }
Nrrd* LBLReader::Convert(int t, int c, bool get_max) { int64_t pos = m_path_name.find_last_of('.'); if (pos == -1) return 0; wstring str_name = m_path_name.substr(0, pos); wostringstream strs; strs << str_name /*<< "_t" << t << "_c" << c*/ << ".lbl"; str_name = strs.str(); FILE* lbl_file = 0; if (!WFOPEN(&lbl_file, str_name.c_str(), L"rb")) return 0; Nrrd *output = nrrdNew(); NrrdIoState *nio = nrrdIoStateNew(); nrrdIoStateSet(nio, nrrdIoStateSkipData, AIR_TRUE); if (nrrdRead(output, lbl_file, nio)) { fclose(lbl_file); return 0; } nio = nrrdIoStateNix(nio); rewind(lbl_file); if (output->dim != 3 || (output->type != nrrdTypeInt && output->type != nrrdTypeUInt)) { delete []output->data; nrrdNix(output); fclose(lbl_file); return 0; } int slice_num = int(output->axis[2].size); int x_size = int(output->axis[0].size); int y_size = int(output->axis[1].size); int data_size = slice_num * x_size * y_size; output->data = new unsigned int[data_size]; if (nrrdRead(output, lbl_file, NULL)) { delete []output->data; nrrdNix(output); fclose(lbl_file); return 0; } fclose(lbl_file); return output; }
Nrrd* LBLReader::Convert(int t, int c, bool get_max) { int64_t pos = m_path_name.find_last_of('.'); if (pos == -1) return 0; wstring str_name = m_path_name.substr(0, pos); wostringstream strs; strs << str_name /*<< "_t" << t << "_c" << c*/ << ".lbl"; str_name = strs.str(); Nrrd *output = nrrdNew(); NrrdIoState *nio = nrrdIoStateNew(); nrrdIoStateSet(nio, nrrdIoStateSkipData, AIR_TRUE); string str; str.assign(str_name.length(), 0); for (int i=0; i<(int)str_name.length(); i++) str[i] = (char)str_name[i]; if (nrrdLoad(output, str.c_str(), nio)) return 0; nio = nrrdIoStateNix(nio); if (output->dim != 3 || (output->type != nrrdTypeInt && output->type != nrrdTypeUInt)) { delete []output->data; nrrdNix(output); return 0; } int slice_num = int(output->axis[2].size); int x_size = int(output->axis[0].size); int y_size = int(output->axis[1].size); int data_size = slice_num * x_size * y_size; output->data = new unsigned int[data_size]; if (nrrdLoad(output, str.c_str(), NULL)) { delete []output->data; nrrdNix(output); return 0; } return output; }
int NrrdWriter(HxUniformScalarField3* field, const char* filename, int encoding) { // Identify data type int nrrdType = nrrdTypeUnknown; switch ( field->primType() ) { case McPrimType::mc_uint8: nrrdType = nrrdTypeUChar; break; case McPrimType::mc_int8: nrrdType = nrrdTypeChar; break; case McPrimType::mc_uint16: nrrdType = nrrdTypeUShort; break; case McPrimType::mc_int16: nrrdType = nrrdTypeShort; break; case McPrimType::mc_int32: nrrdType = nrrdTypeInt; break; case McPrimType::mc_float: nrrdType = nrrdTypeFloat; break; case McPrimType::mc_double: nrrdType = nrrdTypeDouble; break; default: break; } if(nrrdType == nrrdTypeUnknown) { theMsg->printf("ERROR: unsupported output type: %s for nrrd",field->primType().getName()); return 0; } void* data = field->lattice.dataPtr(); Nrrd *nrrd = nrrdNew(); NrrdIoState *nios = nrrdIoStateNew(); if ( encoding == nrrdEncodingTypeGzip) { if (nrrdEncodingGzip->available() ) { nrrdIoStateEncodingSet( nios, nrrdEncodingGzip ); nrrdIoStateSet( nios, nrrdIoStateZlibLevel, 9 ); } else theMsg->printf("WARNING: Nrrd library does not support Gzip compression encoding.\n Make sure Teem_ZLIB is on in CMAKE when building Nrrd library.\n"); } else if ( encoding == nrrdEncodingTypeBzip2) { if (nrrdEncodingBzip2->available() ) { nrrdIoStateEncodingSet( nios, nrrdEncodingBzip2 ); // nrrdIoStateSet( nios, nrrdIoStateBzip2BlockSize, 9 ); } else theMsg->printf("WARNING: Nrrd library does not support Bzip2 compression encoding.\n Make sure Teem_BZIP2 is on in CMAKE when building Nrrd library.\n"); } else if ( encoding == nrrdEncodingTypeAscii) { nrrdIoStateEncodingSet( nios, nrrdEncodingAscii ); } else { theMsg->printf("ERROR: Unimplemented nrrd encoding type: %d\n",encoding); return 0; } try { if ( nrrdWrap_va( nrrd, data, nrrdType, (size_t)3, (size_t)field->lattice.dimsInt()[0], (size_t)field->lattice.dimsInt()[1], (size_t)field->lattice.dimsInt()[2] ) ) { throw( biffGetDone(NRRD) ); } nrrdSpaceDimensionSet( nrrd, 3 ); // TODO: Would be nice to set space units. How does Amira store this? // if ( writeVolume->MetaKeyExists(CMTK_META_SPACE_UNITS_STRING) ) // { // nrrd->spaceUnits[0] = strdup( writeVolume->m_MetaInformation[CMTK_META_SPACE_UNITS_STRING].c_str() ); // nrrd->spaceUnits[1] = strdup( writeVolume->m_MetaInformation[CMTK_META_SPACE_UNITS_STRING].c_str() ); // nrrd->spaceUnits[2] = strdup( writeVolume->m_MetaInformation[CMTK_META_SPACE_UNITS_STRING].c_str() ); // } int kind[NRRD_DIM_MAX] = { nrrdKindDomain, nrrdKindDomain, nrrdKindDomain }; nrrdAxisInfoSet_nva( nrrd, nrrdAxisInfoKind, kind ); // TODO: Would be nice to write some kind of space if this exists // Fetch bounding box information and voxel size float* bbox = field->bbox(); McVec3f voxelSize = field->getVoxelSize(); // Just deal with space directions orthogonal to data axes // TODO: Fetch transformation and use that double spaceDir[NRRD_DIM_MAX][NRRD_SPACE_DIM_MAX]; for ( int i = 0; i < 3; ++i ) { for ( int j = 0; j < 3; ++j ) { if (i == j) spaceDir[i][j] = (double) voxelSize[i]; else spaceDir[i][j] = 0.0; // Can't assume that memory is zeroed } } nrrdAxisInfoSet_nva( nrrd, nrrdAxisInfoSpaceDirection, spaceDir ); double origin[NRRD_DIM_MAX] = { bbox[0], bbox[2], bbox[4] }; if ( nrrdSpaceOriginSet( nrrd, origin ) ) { throw( biffGetDone(NRRD) ); } nrrdAxisInfoSet_va( nrrd, nrrdAxisInfoLabel, "x", "y", "z" ); if ( nrrdSave( filename, nrrd, nios ) ) { throw( biffGetDone(NRRD) ); } } catch ( char* err ) { theMsg->printf("ERROR: hxNrrdIO library returned error '%s'\n", err); free( err ); return 0; } nrrdIoStateNix( nios ); nrrdNix(nrrd); return 1; }
Nrrd* NRRDReader::Convert(int t, int c, bool get_max) { if (t<0 || t>=m_time_num) return 0; int i; m_data_name = m_4d_seq[t].filename.substr(m_4d_seq[t].filename.find_last_of(GETSLASH())+1); Nrrd *output = nrrdNew(); NrrdIoState *nio = nrrdIoStateNew(); nrrdIoStateSet(nio, nrrdIoStateSkipData, AIR_TRUE); string str; str.assign(m_4d_seq[t].filename.length(), 0); for (i=0; i<(int)m_4d_seq[t].filename.length(); i++) str[i] = (char)m_4d_seq[t].filename[i]; if (nrrdLoad(output, str.c_str(), nio)) return 0; nio = nrrdIoStateNix(nio); if (output->dim != 3) { delete []output->data; nrrdNix(output); return 0; } m_slice_num = int(output->axis[2].size); m_x_size = int(output->axis[0].size); m_y_size = int(output->axis[1].size); m_xspc = output->axis[0].spacing; m_yspc = output->axis[1].spacing; m_zspc = output->axis[2].spacing; if (m_xspc>0.0 && m_xspc<100.0 && m_yspc>0.0 && m_yspc<100.0 && m_zspc>0.0 && m_zspc<100.0) m_valid_spc = true; else { m_valid_spc = false; m_xspc = 1.0; m_yspc = 1.0; m_zspc = 1.0; } int data_size = m_slice_num * m_x_size * m_y_size; if (output->type == nrrdTypeUShort || output->type == nrrdTypeShort) data_size *= 2; output->data = new unsigned char[data_size]; if (nrrdLoad(output, str.c_str(), NULL)) { delete []output->data; nrrdNix(output); return 0; } // turn signed into unsigned if (output->type == nrrdTypeChar) { for (i=0; i<m_slice_num*m_x_size*m_y_size; i++) { char val = ((char*)output->data)[i]; unsigned char n = val + 128; ((unsigned char*)output->data)[i] = n; } output->type = nrrdTypeUChar; } m_max_value = 0.0; // turn signed into unsigned unsigned short min_value = 32768, n; if (output->type == nrrdTypeShort || output->type == nrrdTypeUShort) { for (i=0; i<m_slice_num*m_x_size*m_y_size; i++) { if (output->type == nrrdTypeShort) { short val = ((short*)output->data)[i]; n = val + 32768; ((unsigned short*)output->data)[i] = n; min_value = (n < min_value)?n:min_value; } else { n = ((unsigned short*)output->data)[i]; } if (get_max) m_max_value = (n > m_max_value)?n:m_max_value; } output->type = nrrdTypeUShort; } //find max value if (output->type == nrrdTypeUChar) { //8 bit m_max_value = 255.0; m_scalar_scale = 1.0; } else if (output->type == nrrdTypeUShort) { m_max_value -= min_value; //16 bit for (i=0; i<m_slice_num*m_x_size*m_y_size; i++) { ((unsigned short*)output->data)[i] = ((unsigned short*)output->data)[i] - min_value; } if (m_max_value > 0.0) m_scalar_scale = 65535.0 / m_max_value; else m_scalar_scale = 1.0; } else { delete []output->data; nrrdNix(output); return 0; } m_cur_time = t; return output; }