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
/*
******** nrrdNuke()
**
** blows away the nrrd and everything inside
**
** always returns NULL
*/
Nrrd *
nrrdNuke(Nrrd *nrrd) {
  
  if (nrrd) {
    nrrdEmpty(nrrd);
    nrrdNix(nrrd);
  }
  return NULL;
}
Beispiel #3
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 #4
0
void
_nrrdMeasureMode(void *ans, int ansType,
                 const void *_line, int lineType, size_t len, 
                 double axmin, double axmax) {
  Nrrd *nline, *nhist;
  void *line;

  AIR_UNUSED(axmin);
  AIR_UNUSED(axmax);
  line = calloc(len, nrrdTypeSize[lineType]);
  if (line) {
    memcpy(line, _line, len*nrrdTypeSize[lineType]);

    nline = nrrdNew();
    if (nrrdWrap_va(nline, line, lineType, 1, len)) {
      free(biffGetDone(NRRD));
      nrrdNix(nline);
      nrrdDStore[ansType](ans, AIR_NAN);
      return;
    }
    nhist = nrrdNew();
    if (nrrdHisto(nhist, nline, NULL, NULL,
                  nrrdStateMeasureModeBins, nrrdTypeInt)) {
      free(biffGetDone(NRRD));
      nrrdNuke(nhist);
      nrrdNix(nline);
      nrrdDStore[ansType](ans, AIR_NAN);
      return;
    }
    
    /* now we pass this histogram off to histo-mode */
    _nrrdMeasureHistoMode(ans, ansType,
                          nhist->data, nrrdTypeInt, nrrdStateMeasureModeBins,
                          nhist->axis[0].min, nhist->axis[0].max);
    nrrdNuke(nhist);
    nrrdNix(nline);
  } else {
    nrrdDStore[ansType](ans, 0);
  }
  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;
}
Beispiel #7
0
int
main(int argc, char **argv) {
  char *err, *me, *filename;
  Nrrd *nin;
  unsigned int axi;

  me = argv[0];
  if (2 != argc) {
    fprintf(stderr, "usage: %s <filename>\n", me);
    return 1;
  }

  filename = argv[1];

  /* create a nrrd; at this point this is just an empty container */
  nin = nrrdNew();

  /* read in the nrrd from file */
  if (nrrdLoad(nin, filename, NULL)) {
    err = biffGetDone(NRRD);
    fprintf(stderr, "%s: trouble reading \"%s\":\n%s", me, filename, err);
    free(err);
    return 1;
  }

  /* say something about the array */
  printf("%s: \"%s\" is a %d-dimensional nrrd of type %s (%d)\n",
         me, filename, nin->dim,
         airEnumStr(nrrdType, nin->type),
         nin->type);
  for (axi=0; axi<nin->dim; axi++) {
    printf(" axis[%d] size = %u\n", axi, (unsigned int)nin->axis[axi].size);
  }
  printf("%s: the array contains %d elements, each %d bytes in size\n",
         me, (int)nrrdElementNumber(nin), (int)nrrdElementSize(nin));

  /* blow away both the Nrrd struct *and* the memory at nin->data
     (nrrdNix() frees the struct but not the data,
     nrrdEmpty() frees the data but not the struct) */
  nrrdNuke(nin);

  {
    Nrrd *nout;
    unsigned int sx=640, sy=480, xi, yi, idx;
    unsigned char *odata, rr, gg, bb;
    odata = (unsigned char *)(malloc(sx*sy*3));
    for (yi=0; yi<sy; yi++) {
      rr = (unsigned char)(AIR_AFFINE(0, yi, sy, 0, 255));
      for (xi=0; xi<sx; xi++) {
        gg = (unsigned char)(AIR_AFFINE(0, xi, sx, 0, 255));
        bb = (unsigned char)(AIR_AFFINE(0, xi+yi, sx+sy, 0, 255));
        idx = xi + sx*yi;
        odata[0 + 3*idx] = rr;
        odata[1 + 3*idx] = gg;
        odata[2 + 3*idx] = bb;
      }
    }

    //Saves the contents of the buffer as a png image
    //Currently upsidedown
    nout = nrrdNew();
    if (nrrdWrap_va(nout, odata, nrrdTypeUChar, 3,
                    (size_t)3, (size_t)sx, (size_t)sy)
        || nrrdSave("out.png", nout, NULL)) {
      err = biffGetDone(NRRD);
      fprintf(stderr, "%s: trouble wrapping image:\n%s", me, err);
      free(err);
      return 1;
    }
    nrrdNix(nout);
    free(odata);
  }

  return 0;
}