1
/*=========================================================================
3
* Copyright Insight Software Consortium
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0.txt
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
*=========================================================================*/
19
// suppress warning about using sprintf
21
#define _CRT_SECURE_NO_WARNINGS
24
#include "sitkImageSeriesWriter.h"
26
#include <itkImageIOBase.h>
27
#include <itkImageSeriesWriter.h>
34
void WriteImage ( const Image& inImage, const std::vector<std::string> &filenames, bool inUseCompression )
36
ImageSeriesWriter writer;
37
writer.Execute( inImage, filenames, inUseCompression );
40
ImageSeriesWriter::ImageSeriesWriter()
43
this->m_UseCompression = false;
45
// list of pixel types supported
46
typedef NonLabelPixelIDTypeList PixelIDTypeList;
48
this->m_MemberFactory.reset( new detail::MemberFunctionFactory<MemberFunctionType>( this ) );
50
this->m_MemberFactory->RegisterMemberFunctions< PixelIDTypeList, 3 > ();
51
//this->m_MemberFactory->RegisterMemberFunctions< PixelIDTypeList, 2 > ();
54
std::string ImageSeriesWriter::ToString() const
57
std::ostringstream out;
58
out << "itk::simple::ImageSeriesWriter";
61
out << " UseCompression: ";
62
this->ToStringHelper(out, this->m_UseCompression);
65
out << " FileNames:" << std::endl;
66
std::vector<std::string>::const_iterator iter = m_FileNames.begin();
67
while( iter != m_FileNames.end() )
69
std::cout << " \"" << *iter << "\"" << std::endl;
77
ImageSeriesWriter::Self&
78
ImageSeriesWriter::SetUseCompression( bool UseCompression )
80
this->m_UseCompression = UseCompression;
84
bool ImageSeriesWriter::GetUseCompression( void ) const
86
return this->m_UseCompression;
90
ImageSeriesWriter& ImageSeriesWriter::SetFileNames ( const std::vector<std::string> &filenames )
92
this->m_FileNames = filenames;
96
const std::vector<std::string> &ImageSeriesWriter::GetFileNames() const
98
return this->m_FileNames;
102
ImageSeriesWriter& ImageSeriesWriter::Execute ( const Image& image, const std::vector<std::string> &inFileNames, bool inUseCompression )
104
this->SetFileNames( inFileNames );
105
this->SetUseCompression( inUseCompression );
106
return this->Execute( image );
109
ImageSeriesWriter &ImageSeriesWriter::Execute ( const Image &image )
112
// check that the number of file names match the slice size
113
PixelIDValueType type = image.GetPixelIDValue();
114
unsigned int dimension = image.GetDimension();
116
return this->m_MemberFactory->GetMemberFunction( type, dimension )( image );
119
template <class TImageType>
121
ImageSeriesWriter::ExecuteInternal( const Image& inImage )
123
// Define the input and output image types
124
typedef TImageType InputImageType;
126
// Verify input file name are provided
127
if( this->m_FileNames.empty() )
129
sitkExceptionMacro("The parameter \"FileNames\" is empty!");
132
// Verify the input file name are not DICOM
133
for(unsigned int i = 0; i < this->m_FileNames.size(); ++i)
135
const std::string & fn = this->m_FileNames[i];
136
std::string ext = fn.substr(fn.find_last_of(".")+1);
137
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
139
if (ext == "dcm" || ext == "dicom")
141
sitkExceptionMacro(<<this->GetName()<<" does not support writing a DICOM series!")
146
typename InputImageType::ConstPointer image = this->CastImageToITK<InputImageType>( inImage );
148
typedef itk::ImageSeriesWriter<InputImageType,
149
typename InputImageType::template Rebind<typename InputImageType::PixelType, InputImageType::ImageDimension-1>::Type> Writer;
151
typename Writer::Pointer writer = Writer::New();
152
writer->SetUseCompression( this->m_UseCompression );
153
writer->SetFileNames( this->m_FileNames );
154
writer->SetInput( image );
156
this->PreUpdate( writer.GetPointer() );