20 #include "itksys/SystemTools.hxx"
21 #include "itkNrrdImageIO.h"
25 ToFNrrdImageWriter::ToFNrrdImageWriter(): ToFImageWriter(),
26 m_DistanceOutfile(), m_AmplitudeOutfile(), m_IntensityOutfile()
31 ToFNrrdImageWriter::~ToFNrrdImageWriter()
87 void ToFNrrdImageWriter::Add(
float* distanceFloatData,
float* amplitudeFloatData,
float* intensityFloatData,
unsigned char* rgbData)
108 void ToFNrrdImageWriter::OpenStreamFile( std::ofstream &outfile, std::string outfileName )
110 outfile.open(outfileName.c_str(), std::ofstream::binary);
111 if(!outfile.is_open())
113 MITK_ERROR <<
"Error opening outfile: " << outfileName;
114 throw std::logic_error(
"Error opening outfile.");
119 void ToFNrrdImageWriter::CloseStreamFile( std::ofstream &outfile, std::string fileName )
124 throw std::logic_error(
"File is empty.");
130 this->ConvertStreamToNrrdFormat( fileName );
134 void ToFNrrdImageWriter::ConvertStreamToNrrdFormat( std::string fileName )
136 int CaptureWidth = 0;
137 int CaptureHeight = 0;
139 int ImageSizeInBytes = 0;
155 unsigned int* dimensions;
159 dimensions =
new unsigned int[dimension];
160 dimensions[0] = CaptureWidth;
161 dimensions[1] = CaptureHeight;
168 dimensions =
new unsigned int[dimension];
169 dimensions[0] = CaptureWidth;
170 dimensions[1] = CaptureHeight;
175 throw std::logic_error(
"No image type set, please choose between 2D+t and 3D!");
178 unsigned char* rgbData;
181 rgbData =
new unsigned char[PixelNumber*3];
182 for(
int i=0; i<PixelNumber*3; i++)
184 rgbData[i] = i + 0.0;
186 mitk::PixelType RGBType = MakePixelType<unsigned char, itk::RGBPixel<unsigned char>, 3>();
187 imageTemplate->Initialize( RGBType,dimension, dimensions, 1);
188 imageTemplate->SetSlice(rgbData, 0, 0, 0);
192 floatData =
new float[PixelNumber];
193 for(
int i=0; i<PixelNumber; i++)
195 floatData[i] = i + 0.0;
198 imageTemplate->Initialize( FloatType,dimension, dimensions, 1);
199 imageTemplate->SetSlice(floatData, 0, 0, 0);
203 nrrdWriter->SetNumberOfDimensions(dimension);
204 nrrdWriter->SetPixelType( imageTemplate->GetPixelType().GetPixelType());
205 nrrdWriter->SetComponentType( (itk::ImageIOBase::IOComponentType) imageTemplate->GetPixelType().GetComponentType());
206 if(imageTemplate->GetPixelType().GetNumberOfComponents() > 1)
208 nrrdWriter->SetNumberOfComponents(imageTemplate->GetPixelType().GetNumberOfComponents());
211 itk::ImageIORegion ioRegion( dimension );
212 mitk::Vector3D spacing = imageTemplate->GetGeometry()->GetSpacing();
213 mitk::Point3D origin = imageTemplate->GetGeometry()->GetOrigin();
215 for(
unsigned int i = 0; i < dimension; i++)
217 nrrdWriter->SetDimensions(i,dimensions[i]);
218 nrrdWriter->SetSpacing(i,spacing[i]);
219 nrrdWriter->SetOrigin(i,origin[i]);
222 direction.Set_vnl_vector(imageTemplate->GetGeometry()->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix().get_column(i));
223 vnl_vector< double > axisDirection(dimension);
225 for(
unsigned int j = 0; j < dimension; j++)
227 axisDirection[j] = direction[j]/spacing[i];
229 nrrdWriter->SetDirection( i, axisDirection );
231 ioRegion.SetSize(i, imageTemplate->GetLargestPossibleRegion().GetSize(i) );
232 ioRegion.SetIndex(i, imageTemplate->GetLargestPossibleRegion().GetIndex(i) );
235 nrrdWriter->SetIORegion(ioRegion);
236 nrrdWriter->SetFileName(fileName);
237 nrrdWriter->SetUseStreamedWriting(
true);
239 std::ifstream stream(fileName.c_str(), std::ifstream::binary);
243 unsigned int sizeInBytes = size *
sizeof(
unsigned char);
244 unsigned char* data =
new unsigned char[size];
245 stream.read((
char*)data, sizeInBytes);
246 nrrdWriter->Write(data);
253 unsigned int sizeInBytes = size *
sizeof(float);
254 float* data =
new float[size];
255 stream.read((
char*)data, sizeInBytes);
258 nrrdWriter->Write(data);
260 catch (itk::ExceptionObject* e)
itk::SmartPointer< Self > Pointer
std::ofstream m_RGBOutfile
file for intensity image
int m_RGBImageSizeInBytes
size of the image to save in bytes
bool m_AmplitudeImageSelected
flag indicating if amplitude image should be recorded
int m_NumOfFrames
number of frames written to the image. Used for pic header.
bool m_IntensityImageSelected
flag indicating if intensity image should be recorded
void CheckForFileExtension(std::string &fileName)
Checks file name if file extension exists. If not an error message is returned.
bool m_DistanceImageSelected
flag indicating if distance image should be recorded
itk::SmartPointer< Self > Pointer
DataCollection - Class to facilitate loading/accessing structured data.
void Add(float *distanceFloatData, float *amplitudeFloatData, float *intensityFloatData, unsigned char *rgbData=0) override
Add new data to file.
int m_RGBPixelNumber
number of pixels (widht*height) of the images to record
int m_ToFCaptureWidth
width (x-dimension) of the images to record.
int m_ToFCaptureHeight
height (y-dimension) of the images to record.
int m_RGBCaptureHeight
height (y-dimension) of the images to record.
std::string m_IntensityImageFileName
file name for saving the intensity image
bool m_RGBImageSelected
flag indicating if RGB image should be recorded
std::string m_AmplitudeImageFileName
file name for saving the amplitude image
int m_ToFPixelNumber
number of pixels (widht*height) of the images to record
std::string m_Extension
file extension used for saving images
std::string m_DistanceImageFileName
file name for saving the distance image
std::ofstream m_DistanceOutfile
file for distance image
std::ofstream m_IntensityOutfile
file for intensity image
std::ofstream m_AmplitudeOutfile
file for amplitude image
int m_ToFImageSizeInBytes
size of the image to save in bytes
std::string m_RGBImageFileName
file name for saving the RGB image
ToFImageWriter::ToFImageType m_ToFImageType
type of image to be recorded: ToFImageType3D (0) or ToFImageType2DPlusT (1)
int m_RGBCaptureWidth
width (x-dimension) of the images to record.
void Close() override
Close file(s) add .pic header and write.
Class for defining the data type of pixels.
void Open() override
Open file(s) for writing.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.