Beispiel #1
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;
}
Beispiel #2
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();

   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;
}
Beispiel #3
0
/* *************************************************************** */
void reg_io_writeNRRDfile(Nrrd *image, const char *filename)
{
    // Set the encoding to gziped as it is compiled as part of the NiftyReg project
    NrrdIoState *nio=nrrdIoStateNew();
    if (nrrdEncodingGzip->available()){
        nrrdIoStateEncodingSet(nio, nrrdEncodingGzip);
        nrrdIoStateSet(nio, nrrdIoStateZlibLevel, 9);
    }
    else{
        fprintf(stderr, "[NiftyReg ERROR] Can not compress the file: \"%s\"\n", filename);
    }

    char *err;
    if (nrrdSave(filename, image, nio)){
        err = biffGetDone(NRRD);
        fprintf(stderr, "[NiftyReg ERROR] Can not write the file \"%s\":\n%s\n", filename, err);
        free(err);
        exit(1);
    }
    return;
}
void mexFunction(int nlhs, mxArray *plhs[],
  int nrhs, const mxArray *prhs[])
{
  char me[]="nrrdLoad", *filename, *errPtr, errBuff[AIR_STRLEN_MED];
  int filenameLen, sizeI[NRRD_DIM_MAX];
  mxClassID mtype;
  size_t sizeZ[NRRD_DIM_MAX];
  unsigned int axIdx;
  Nrrd *nrrd;
  NrrdIoState *nio;
  airArray *mop;

  if (!(1 == nrhs && mxIsChar(prhs[0]))) {
    sprintf(errBuff, "%s: requires one string argument (the name of the file)", me);
    mexErrMsgTxt(errBuff);
  }

  mop = airMopNew();
  filenameLen = mxGetM(prhs[0])*mxGetN(prhs[0])+1;
  filename = mxCalloc(filenameLen, sizeof(mxChar));  /* managed by Matlab */
  mxGetString(prhs[0], filename, filenameLen);

  nrrd = nrrdNew();
  airMopAdd(mop, nrrd, (airMopper)nrrdNix, airMopAlways);
  nio = nrrdIoStateNew();
  airMopAdd(mop, nio, (airMopper)nrrdIoStateNix, airMopAlways);
  nrrdIoStateSet(nio, nrrdIoStateSkipData, AIR_TRUE);

  /* read header, but no data */
  if (nrrdLoad(nrrd, filename, nio)) {
    errPtr = biffGetDone(NRRD);
    airMopAdd(mop, errPtr, airFree, airMopAlways);
    sprintf(errBuff, "%s: trouble reading NRRD header:\n%s", me, errPtr);
    airMopError(mop);
    mexErrMsgTxt(errBuff);
  }
  mtype = typeNtoM(nrrd->type);
  if (mxUNKNOWN_CLASS == mtype) {
    sprintf(errBuff, "%s: sorry, can't handle type %s (%d)", me,
            airEnumStr(nrrdType, nrrd->type), nrrd->type);
    airMopError(mop);
    mexErrMsgTxt(errBuff);
  }

  /* allocate matlab array based on nrrd struct */
  for (axIdx=0; axIdx<nrrd->dim; axIdx++) {
    sizeI[axIdx] = nrrd->axis[axIdx].size;
  }
  plhs[0]=mxCreateNumericArray(nrrd->dim,sizeI,mtype,mxREAL);

  /* copy data pointer */
  nrrd->data = mxGetPr(plhs[0]);

  /* read second time, now loading data */
  if (nrrdLoad(nrrd, filename, NULL)) {
    errPtr = biffGetDone(NRRD);
    airMopAdd(mop, errPtr, airFree, airMopAlways);
    sprintf(errBuff, "%s: trouble reading NRRD:\n%s", me, errPtr);
    airMopError(mop);
    mexErrMsgTxt(errBuff);
  }

  airMopOkay(mop);
  return;
}
Beispiel #5
0
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; 
}
Beispiel #6
0
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;
}