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

« back to all changes in this revision

Viewing changes to Code/Common/src/sitkImage.hxx

  • 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
#ifndef sitkImage_hxx
 
19
#define sitkImage_hxx
 
20
 
 
21
#include "sitkImage.h"
 
22
 
 
23
#include "itkImage.h"
 
24
#include "itkVectorImage.h"
 
25
#include "itkLabelMap.h"
 
26
#include "itkLabelObject.h"
 
27
 
 
28
#include "sitkExceptionObject.h"
 
29
#include "sitkPimpleImageBase.hxx"
 
30
#include "sitkPixelIDTypeLists.h"
 
31
 
 
32
 
 
33
namespace itk
 
34
{
 
35
  namespace simple
 
36
  {
 
37
 
 
38
  // this is a little specialization just to get the
 
39
  // InternalInitialization method's PixelIDTpImageType lookup to get
 
40
  // a valid void type, so it'll dispatch to the a specialized
 
41
  // method. All this is just to instantiate something that will never
 
42
  // be actually used.
 
43
  template <unsigned int VImageDimension>
 
44
  struct PixelIDToImageType< typelist::NullType , VImageDimension >
 
45
  {
 
46
    typedef void ImageType;
 
47
  };
 
48
 
 
49
  // This method is explicitly instantiated, and in-turn implicitly
 
50
  // instantates the PipleImage for all used image types. This method
 
51
  // just dispatces to nother method, to aid in instantiating only the
 
52
  // images requested.
 
53
  template <int VPixelIDValue, unsigned int VImageDimension>
 
54
  void Image::InternalInitialization( typename PixelIDToImageType<typename typelist::TypeAt<InstantiatedPixelIDTypeList,
 
55
                                                                                            VPixelIDValue>::Result,
 
56
                                                                  VImageDimension>::ImageType *i )
 
57
  {
 
58
    this->ConditionalInternalInitialization<VPixelIDValue>( i );
 
59
  }
 
60
 
 
61
  template<int VPixelIDValue, typename TImageType>
 
62
  typename DisableIf<nsstd::is_same<TImageType, void>::value>::Type
 
63
  Image::ConditionalInternalInitialization( TImageType *image )
 
64
  {
 
65
    // no need to check if null
 
66
    delete this->m_PimpleImage;
 
67
    this->m_PimpleImage = NULL;
 
68
 
 
69
    this->m_PimpleImage = new PimpleImage<TImageType>( image );
 
70
  }
 
71
 
 
72
 
 
73
  template<class TImageType>
 
74
  typename EnableIf<IsBasic<TImageType>::Value>::Type
 
75
  Image::AllocateInternal ( unsigned int Width, unsigned int Height, unsigned int Depth, unsigned int dim4, unsigned int numberOfComponents )
 
76
  {
 
77
    if ( numberOfComponents != 1  && numberOfComponents != 0 )
 
78
      {
 
79
      sitkExceptionMacro( "Specified number of components as " << numberOfComponents
 
80
                          << " but did not specify pixelID as a vector type!" );
 
81
      }
 
82
 
 
83
    typename TImageType::IndexType  index;
 
84
    typename TImageType::SizeType   size;
 
85
    typename TImageType::RegionType region;
 
86
 
 
87
    index.Fill ( 0 );
 
88
    size.Fill(1);
 
89
    size[0] = Width;
 
90
    size[1] = Height;
 
91
 
 
92
    if ( TImageType::ImageDimension > 2 )
 
93
      {
 
94
      assert( Depth != 0 );
 
95
      size[2] = Depth;
 
96
      }
 
97
 
 
98
    if ( TImageType::ImageDimension > 3 )
 
99
      {
 
100
      assert(  dim4 != 0 );
 
101
      size[3] =  dim4;
 
102
      }
 
103
 
 
104
    region.SetSize ( size );
 
105
    region.SetIndex ( index );
 
106
 
 
107
    typename TImageType::Pointer image = TImageType::New();
 
108
    image->SetRegions ( region );
 
109
    image->Allocate();
 
110
    image->FillBuffer ( itk::NumericTraits<typename TImageType::PixelType>::Zero );
 
111
 
 
112
    delete this->m_PimpleImage;
 
113
    this->m_PimpleImage = NULL;
 
114
    m_PimpleImage =  new PimpleImage<TImageType>( image );
 
115
  }
 
116
 
 
117
  template<class TImageType>
 
118
  typename EnableIf<IsVector<TImageType>::Value>::Type
 
119
  Image::AllocateInternal ( unsigned int Width, unsigned int Height, unsigned int Depth, unsigned int dim4, unsigned int numberOfComponents )
 
120
  {
 
121
    if ( numberOfComponents == 0 )
 
122
      {
 
123
      numberOfComponents = TImageType::ImageDimension;
 
124
      }
 
125
 
 
126
    typename TImageType::IndexType  index;
 
127
    typename TImageType::SizeType   size;
 
128
    typename TImageType::RegionType region;
 
129
    typename TImageType::PixelType  zero;
 
130
 
 
131
    index.Fill ( 0 );
 
132
    size.Fill(1);
 
133
    size[0] = Width;
 
134
    size[1] = Height;
 
135
 
 
136
    if ( TImageType::ImageDimension > 2 )
 
137
      {
 
138
      assert( Depth != 0 );
 
139
      size[2] = Depth;
 
140
      }
 
141
 
 
142
    if ( TImageType::ImageDimension > 3 )
 
143
      {
 
144
      assert(  dim4 != 0 );
 
145
      size[3] =  dim4;
 
146
      }
 
147
 
 
148
    region.SetSize ( size );
 
149
    region.SetIndex ( index );
 
150
 
 
151
    zero.SetSize( numberOfComponents );
 
152
    zero.Fill ( itk::NumericTraits<typename TImageType::PixelType::ValueType>::Zero );
 
153
 
 
154
    typename TImageType::Pointer image = TImageType::New();
 
155
    image->SetRegions ( region );
 
156
    image->SetVectorLength( numberOfComponents );
 
157
    image->Allocate();
 
158
    image->FillBuffer ( zero );
 
159
 
 
160
    delete this->m_PimpleImage;
 
161
    this->m_PimpleImage = NULL;
 
162
 
 
163
    m_PimpleImage = new PimpleImage<TImageType>( image );
 
164
  }
 
165
 
 
166
  template<class TImageType>
 
167
  typename EnableIf<IsLabel<TImageType>::Value>::Type
 
168
  Image::AllocateInternal ( unsigned int Width, unsigned int Height, unsigned int Depth, unsigned int dim4, unsigned int numberOfComponents )
 
169
  {
 
170
    if ( numberOfComponents != 1 && numberOfComponents != 0 )
 
171
      {
 
172
      sitkExceptionMacro( "Specified number of components as " << numberOfComponents
 
173
                          << " but did not specify pixelID as a vector type!" );
 
174
      }
 
175
 
 
176
    typename TImageType::IndexType  index;
 
177
    typename TImageType::SizeType   size;
 
178
    typename TImageType::RegionType region;
 
179
 
 
180
    index.Fill ( 0 );
 
181
    size.Fill(1);
 
182
    size[0] = Width;
 
183
    size[1] = Height;
 
184
 
 
185
    if ( TImageType::ImageDimension > 2 )
 
186
      {
 
187
      assert( Depth != 0 );
 
188
      size[2] = Depth;
 
189
      }
 
190
 
 
191
    if ( TImageType::ImageDimension > 3 )
 
192
      {
 
193
      assert(  dim4 != 0 );
 
194
      size[3] =  dim4;
 
195
      }
 
196
 
 
197
    region.SetSize ( size );
 
198
    region.SetIndex ( index );
 
199
 
 
200
    typename TImageType::Pointer image = TImageType::New();
 
201
    image->SetRegions ( region );
 
202
    image->Allocate();
 
203
    image->SetBackgroundValue( 0 );
 
204
 
 
205
    delete this->m_PimpleImage;
 
206
    this->m_PimpleImage = NULL;
 
207
 
 
208
    m_PimpleImage = new PimpleImage<TImageType>( image );
 
209
  }
 
210
 
 
211
  }
 
212
}
 
213
 
 
214
#endif // sitkImage_h