~ubuntu-branches/debian/sid/simpleitk/sid

« back to all changes in this revision

Viewing changes to Code/IO/src/sitkImageSeriesWriter.cxx

  • Committer: Package Import Robot
  • Author(s): Ghislain Antony Vaillant
  • Date: 2017-11-02 08:49:18 UTC
  • Revision ID: package-import@ubuntu.com-20171102084918-7hs09ih668xq87ej
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=========================================================================
 
2
*
 
3
*  Copyright Insight Software Consortium
 
4
*
 
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
 
8
*
 
9
*         http://www.apache.org/licenses/LICENSE-2.0.txt
 
10
*
 
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.
 
16
*
 
17
*=========================================================================*/
 
18
 
 
19
// suppress warning about using sprintf
 
20
#ifdef _MSC_VER
 
21
  #define _CRT_SECURE_NO_WARNINGS
 
22
#endif
 
23
 
 
24
#include "sitkImageSeriesWriter.h"
 
25
 
 
26
#include <itkImageIOBase.h>
 
27
#include <itkImageSeriesWriter.h>
 
28
 
 
29
#include <cctype>
 
30
 
 
31
namespace itk {
 
32
  namespace simple {
 
33
 
 
34
  void WriteImage ( const Image& inImage, const std::vector<std::string> &filenames, bool inUseCompression )
 
35
  {
 
36
    ImageSeriesWriter writer;
 
37
    writer.Execute( inImage, filenames, inUseCompression );
 
38
  }
 
39
 
 
40
  ImageSeriesWriter::ImageSeriesWriter()
 
41
  {
 
42
 
 
43
    this->m_UseCompression = false;
 
44
 
 
45
    // list of pixel types supported
 
46
    typedef NonLabelPixelIDTypeList PixelIDTypeList;
 
47
 
 
48
    this->m_MemberFactory.reset( new detail::MemberFunctionFactory<MemberFunctionType>( this ) );
 
49
 
 
50
    this->m_MemberFactory->RegisterMemberFunctions< PixelIDTypeList, 3 > ();
 
51
    //this->m_MemberFactory->RegisterMemberFunctions< PixelIDTypeList, 2 > ();
 
52
  }
 
53
 
 
54
  std::string ImageSeriesWriter::ToString() const
 
55
  {
 
56
 
 
57
    std::ostringstream out;
 
58
    out << "itk::simple::ImageSeriesWriter";
 
59
    out << std::endl;
 
60
 
 
61
    out << "  UseCompression: ";
 
62
    this->ToStringHelper(out, this->m_UseCompression);
 
63
    out << std::endl;
 
64
 
 
65
    out << "  FileNames:" << std::endl;
 
66
    std::vector<std::string>::const_iterator iter  = m_FileNames.begin();
 
67
    while( iter != m_FileNames.end() )
 
68
      {
 
69
      std::cout << "    \"" << *iter << "\"" << std::endl;
 
70
      ++iter;
 
71
      }
 
72
 
 
73
    return out.str();
 
74
  }
 
75
 
 
76
 
 
77
  ImageSeriesWriter::Self&
 
78
  ImageSeriesWriter::SetUseCompression( bool UseCompression )
 
79
  {
 
80
    this->m_UseCompression = UseCompression;
 
81
    return *this;
 
82
  }
 
83
 
 
84
  bool ImageSeriesWriter::GetUseCompression( void ) const
 
85
  {
 
86
    return this->m_UseCompression;
 
87
  }
 
88
 
 
89
 
 
90
  ImageSeriesWriter& ImageSeriesWriter::SetFileNames ( const std::vector<std::string> &filenames )
 
91
  {
 
92
    this->m_FileNames = filenames;
 
93
    return *this;
 
94
  }
 
95
 
 
96
  const std::vector<std::string> &ImageSeriesWriter::GetFileNames() const
 
97
  {
 
98
    return this->m_FileNames;
 
99
  }
 
100
 
 
101
 
 
102
  ImageSeriesWriter& ImageSeriesWriter::Execute ( const Image& image, const std::vector<std::string> &inFileNames, bool inUseCompression )
 
103
  {
 
104
    this->SetFileNames( inFileNames );
 
105
    this->SetUseCompression( inUseCompression );
 
106
    return this->Execute( image );
 
107
  }
 
108
 
 
109
  ImageSeriesWriter &ImageSeriesWriter::Execute ( const Image &image )
 
110
  {
 
111
 
 
112
    // check that the number of file names match the slice size
 
113
    PixelIDValueType type = image.GetPixelIDValue();
 
114
    unsigned int dimension = image.GetDimension();
 
115
 
 
116
    return this->m_MemberFactory->GetMemberFunction( type, dimension )( image );
 
117
  }
 
118
 
 
119
  template <class TImageType>
 
120
  ImageSeriesWriter &
 
121
  ImageSeriesWriter::ExecuteInternal( const Image& inImage )
 
122
  {
 
123
    // Define the input and output image types
 
124
    typedef TImageType     InputImageType;
 
125
 
 
126
    // Verify input file name are provided
 
127
    if( this->m_FileNames.empty() )
 
128
      {
 
129
      sitkExceptionMacro("The parameter \"FileNames\" is empty!");
 
130
      }
 
131
 
 
132
    // Verify the input file name are not DICOM
 
133
    for(unsigned int i = 0; i < this->m_FileNames.size(); ++i)
 
134
      {
 
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);
 
138
 
 
139
      if (ext == "dcm" || ext == "dicom")
 
140
        {
 
141
        sitkExceptionMacro(<<this->GetName()<<" does not support writing a DICOM series!")
 
142
        }
 
143
      }
 
144
 
 
145
 
 
146
    typename InputImageType::ConstPointer image = this->CastImageToITK<InputImageType>( inImage );
 
147
 
 
148
    typedef itk::ImageSeriesWriter<InputImageType,
 
149
                                   typename InputImageType::template Rebind<typename InputImageType::PixelType, InputImageType::ImageDimension-1>::Type> Writer;
 
150
 
 
151
    typename Writer::Pointer writer = Writer::New();
 
152
    writer->SetUseCompression( this->m_UseCompression );
 
153
    writer->SetFileNames( this->m_FileNames );
 
154
    writer->SetInput( image );
 
155
 
 
156
    this->PreUpdate( writer.GetPointer() );
 
157
 
 
158
    writer->Update();
 
159
 
 
160
    return *this;
 
161
  }
 
162
 
 
163
  }
 
164
}