26 #include <itkImageIOBase.h>
27 #include <itkImageIOFactory.h>
31 this->SetNumberOfRequiredInputs(1);
42 if (fileName && (fileName == this->m_FileName))
48 this->m_FileName = fileName;
49 this->m_FileNameWithoutExtension = this->m_FileName;
50 this->m_Extension.clear();
51 std::size_t pos = this->m_FileName.find_last_of(
"/\\");
52 if (pos != std::string::npos)
54 std::size_t ppos = this->m_FileName.find_first_of(
'.', pos);
55 if (ppos != std::string::npos)
57 this->m_FileNameWithoutExtension = this->m_FileName.substr(0, ppos);
58 this->m_Extension = this->m_FileName.substr(ppos);
64 this->m_FileName.clear();
65 this->m_FileNameWithoutExtension.clear();
66 this->m_Extension.clear();
73 this->SetFileName(fileName.c_str());
78 if (extension && (extension == this->m_Extension))
84 this->m_Extension = extension;
85 this->m_FileName = this->m_FileNameWithoutExtension + this->m_Extension;
89 this->m_Extension.clear();
90 this->m_FileName = this->m_FileNameWithoutExtension;
97 this->SetFileName(extension.c_str());
102 this->m_Extension =
".mhd";
103 this->m_FileName = this->m_FileNameWithoutExtension + this->m_Extension;
107 #include <vtkConfigure.h>
108 #include <vtkImageData.h>
109 #include <vtkXMLImageDataWriter.h>
113 vtkwriter->SetFileName(filename);
119 #include <itkRGBAPixel.h>
123 MITK_INFO <<
"Writing image: " << fileName << std::endl;
126 if (fileName.find(
".png") != std::string::npos || fileName.find(
".tif") != std::string::npos ||
127 fileName.find(
".jpg") != std::string::npos || fileName.find(
".bmp") != std::string::npos)
144 catch (itk::ExceptionObject &e)
146 std::cerr <<
"Caught " << e.what() << std::endl;
148 catch (std::exception &e)
150 std::cerr <<
"Caught std::exception " << e.what() << std::endl;
166 itk::Vector<double, 4u> spacing4D;
167 spacing4D[0] = mitkSpacing[0];
168 spacing4D[1] = mitkSpacing[1];
169 spacing4D[2] = mitkSpacing[2];
172 itk::Vector<double, 4u> origin4D;
173 origin4D[0] = mitkOrigin[0];
174 origin4D[1] = mitkOrigin[1];
175 origin4D[2] = mitkOrigin[2];
179 itk::ImageIOFactory::CreateImageIO(fileName.c_str(), itk::ImageIOFactory::WriteMode);
181 if (imageIO.IsNull())
183 itkExceptionMacro(<<
"Error: Could not create itkImageIO via factory for file " << fileName);
187 imageIO->SetNumberOfDimensions(dimension);
190 static_cast<itk::ImageIOBase::IOComponentType
>(pixelType.
GetComponentType()) :
191 itk::ImageIOBase::UNKNOWNCOMPONENTTYPE);
194 itk::ImageIORegion ioRegion(dimension);
196 for (
unsigned int i = 0; i < dimension; i++)
198 imageIO->SetDimensions(i, dimensions[i]);
199 imageIO->SetSpacing(i, spacing4D[i]);
200 imageIO->SetOrigin(i, origin4D[i]);
203 mitkDirection.SetVnlVector(
205 itk::Vector<double, 4u> direction4D;
206 direction4D[0] = mitkDirection[0];
207 direction4D[1] = mitkDirection[1];
208 direction4D[2] = mitkDirection[2];
217 vnl_vector<double> axisDirection(dimension);
218 for (
unsigned int j = 0; j < dimension; j++)
220 axisDirection[j] = direction4D[j] / spacing4D[i];
222 imageIO->SetDirection(i, axisDirection);
229 imageIO->SetUseCompression(m_UseCompression);
231 imageIO->SetIORegion(ioRegion);
232 imageIO->SetFileName(fileName);
235 imageIO->Write(imageAccess.
GetData());
242 if (m_FileName ==
"")
244 itkWarningMacro(<<
"Sorry, filename has not been set!");
248 FILE *tempFile = fopen(m_FileName.c_str(),
"w");
249 if (tempFile ==
nullptr)
251 itkExceptionMacro(<<
"File location not writeable");
255 remove(m_FileName.c_str());
261 if (input->GetDimension() == 2)
263 if (!input->GetGeometry()->Is2DConvertable())
265 MITK_WARN <<
"Saving a 2D image with 3D geometry information. Geometry information will be lost! You might "
266 "consider using Convert2Dto3DImageFilter before saving.";
270 affTrans->SetIdentity();
273 input->GetGeometry()->SetIndexToWorldTransform(affTrans);
274 input->GetGeometry()->SetSpacing(spacing);
275 input->GetGeometry()->SetOrigin(origin);
279 bool vti = (m_Extension.find(
".vti") != std::string::npos);
282 if (m_Extension.find(
".pic") == std::string::npos && m_Extension.find(
".nrrd") == std::string::npos &&
283 m_Extension.find(
".nii") == std::string::npos && m_Extension.find(
".nii.gz") == std::string::npos)
285 if (input->GetDimension() > 3)
289 timesteps = input->GetDimension(3);
291 timeSelector->SetInput(input);
293 for (t = 0; t < timesteps; ++t)
296 timeSelector->SetTimeNr(t);
297 timeSelector->Update();
298 if (input->GetTimeGeometry()->IsValidTimeStep(t))
300 const mitk::TimeBounds &timebounds = input->GetTimeGeometry()->GetTimeBounds(t);
301 filename << m_FileNameWithoutExtension <<
"_S" << std::setprecision(0) << timebounds[0] <<
"_E"
302 << std::setprecision(0) << timebounds[1] <<
"_T" << t << m_Extension;
306 itkWarningMacro(<<
"Error on write: TimeGeometry invalid of image " << filename.str() <<
".");
307 filename << m_FileNameWithoutExtension <<
"_T" << t << m_Extension;
311 writeVti(filename.str().c_str(), input, t);
315 WriteByITK(image, filename.str());
321 writeVti(m_FileName.c_str(), input);
325 WriteByITK(input, m_FileName);
331 if (m_Extension.find(
".pic") != std::string::npos)
352 if (m_Extension.find(
".nrrd") != std::string::npos || m_Extension.find(
".nii") != std::string::npos ||
353 m_Extension.find(
".nii.gz") != std::string::npos)
355 WriteByITK(input, this->m_FileName);
358 m_MimeType =
"application/MITK.Pic";
365 return this->CanWriteBaseDataType(input->
GetData());
372 if (input && CanWriteDataType(input))
373 this->ProcessObject::SetNthInput(0, dynamic_cast<mitk::Image *>(input->
GetData()));
383 std::vector<std::string> possibleFileExtensions;
384 possibleFileExtensions.push_back(
".pic");
385 possibleFileExtensions.push_back(
".pic.gz");
386 possibleFileExtensions.push_back(
".bmp");
387 possibleFileExtensions.push_back(
".dcm");
388 possibleFileExtensions.push_back(
".DCM");
389 possibleFileExtensions.push_back(
".dicom");
390 possibleFileExtensions.push_back(
".DICOM");
391 possibleFileExtensions.push_back(
".gipl");
392 possibleFileExtensions.push_back(
".gipl.gz");
393 possibleFileExtensions.push_back(
".mha");
394 possibleFileExtensions.push_back(
".nii");
395 possibleFileExtensions.push_back(
".nii.gz");
396 possibleFileExtensions.push_back(
".nrrd");
397 possibleFileExtensions.push_back(
".nhdr");
398 possibleFileExtensions.push_back(
".png");
399 possibleFileExtensions.push_back(
".PNG");
400 possibleFileExtensions.push_back(
".spr");
401 possibleFileExtensions.push_back(
".mhd");
402 possibleFileExtensions.push_back(
".vtk");
403 possibleFileExtensions.push_back(
".vti");
404 possibleFileExtensions.push_back(
".hdr");
405 possibleFileExtensions.push_back(
".img");
406 possibleFileExtensions.push_back(
".img.gz");
407 possibleFileExtensions.push_back(
".png");
408 possibleFileExtensions.push_back(
".tif");
409 possibleFileExtensions.push_back(
".jpg");
410 return possibleFileExtensions;
425 this->ProcessObject::SetNthInput(0, image);
430 if (this->GetNumberOfInputs() < 1)
436 return static_cast<const mitk::Image *
>(this->ProcessObject::GetInput(0));
447 return "Nearly Raw Raster Data (*.nrrd);;"
448 "NIfTI format (*.nii *.nii.gz);;"
449 "VTK Image Data Files (*.vti);;"
450 "NRRD with detached header (*.nhdr);;"
451 "Analyze Format (*.hdr);;"
452 "MetaImage (*.mhd);;"
453 "Sets of 2D slices (*.png *.tiff *.jpg *.jpeg *.bmp);;"
454 "DICOM (*.dcm *.DCM *.dicom *.DICOM);;"
455 "UMDS GIPL Format Files (*.gipl *.gipl.gz)";
465 return dynamic_cast<mitk::Image *
>(data.GetPointer());
470 if (this->CanWriteBaseDataType(data))
472 this->SetInput(dynamic_cast<mitk::Image *>(data.GetPointer()));
479 m_UseCompression = useCompression;
const Point3D GetOrigin() const
Get the origin, e.g. the upper-left corner of the plane.
itk::SmartPointer< Self > Pointer
void SetDefaultExtension()
Set the extension to be added to the filename to the default.
itk::FixedArray< ScalarType, 2 > TimeBounds
Standard typedef for time-bounds.
#define MITK_ACCESSBYITK_PIXEL_TYPES_SEQ
const mitk::Image * GetInput()
static void Update(vtkPolyData *)
virtual std::string GetSupportedBaseData() const override
virtual void WriteByITK(mitk::Image *image, const std::string &fileName)
const void * GetData() const
Gives const access to the data.
const mitk::Vector3D GetSpacing() const
Get the spacing (size of a pixel).
virtual vtkImageData * GetVtkImageData(int t=0, int n=0)
Get a volume at a specific time t of channel n as a vtkImageData.
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
itk::ImageIOBase::IOPixelType GetPixelType() const
static void writeVti(const char *filename, mitk::Image *image, int t=0)
virtual std::vector< std::string > GetPossibleFileExtensions() override
Return the possible file extensions for the data type associated with the writer. ...
virtual std::string GetWritenMIMEType() override
Return the MimeType of the saved File.
#define AccessByItk_1(mitkImage, itkImageTypeFunction, arg1)
static const int PixelComponentUserType
virtual void SetFileName(const char *fileName) override
#define AccessFixedPixelTypeByItk_1(mitkImage, itkImageTypeFunction, pixelTypeSeq, arg1)
virtual void DoWrite(BaseData::Pointer data) override
static const char * GetStaticNameOfClass()
unsigned int * GetDimensions() const
Get the sizes of all dimensions as an integer-array.
Convenience class to temporarily change the current locale.
virtual bool CanWriteBaseDataType(BaseData::Pointer data) override
virtual bool CanWriteDataType(DataNode *) override
Check if the Writer can write the Content of the.
virtual const char * GetFileDialogPattern() override
static const std::string filename
virtual void GenerateData() override
Image class for storing images.
virtual const char * GetDefaultExtension() override
const RegionType & GetLargestPossibleRegion() const
int GetComponentType() const
Get the component type (the scalar (!) type). Each element may contain m_NumberOfComponents (more tha...
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
void SetInput(mitk::Image *input)
vcl_size_t GetNumberOfComponents() const
Get the number of components of which each element consists.
void _mitkItkPictureWriteComposite(itk::Image< TPixel, VImageDimension > *itkImage, const std::string &fileName)
ITK-Like method to be called for writing an image.
void SetUseCompression(bool useCompression)
void _mitkItkPictureWrite(itk::Image< TPixel, VImageDimension > *itkImage, const std::string &fileName)
ITK-Like method to be called for writing an single-component image using the AccessByItk Macros...
virtual void SetExtension(const char *extension)
Explicitly set the extension to be added to the filename.
unsigned int GetDimension() const
Get dimension of the image.
ImageReadAccessor class to get locked read access for a particular image part.
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
virtual const char * GetDefaultFilename() override
Class for nodes of the DataTree.
Class for defining the data type of pixels.
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.
#define MITK_ACCESSBYITK_COMPOSITE_PIXEL_TYPES_SEQ
virtual std::string GetFileExtension() override
Return the extension to be added to the filename.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.