SimpleITK  1.2.4
sitkImageConvert.h
Go to the documentation of this file.
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 sitkImageConvert_h
19 #define sitkImageConvert_h
20 
21 
22 namespace itk
23 {
24 
25 template< typename T, unsigned int NVectorDimension > class Vector;
26 
27 namespace simple
28 {
29 
30 
34 template< typename TPixelType, unsigned int ImageDimension >
36 typename itk::Image< itk::Vector< TPixelType, ImageDimension >, ImageDimension>::Pointer
38 {
39  typedef itk::Image< itk::Vector< TPixelType, ImageDimension >, ImageDimension> ImageType;
40  typedef itk::VectorImage< TPixelType, ImageDimension > VectorImageType;
41 
42  // check number of element compatibility
43  if ( img->GetNumberOfComponentsPerPixel() != VectorImageType::ImageDimension )
44  {
45  sitkExceptionMacro("Expected number of elements in vector image to be the same as the dimension!");
46  }
47 
48  size_t numberOfElements = img->GetBufferedRegion().GetNumberOfPixels();
49  typename ImageType::PixelType* buffer = reinterpret_cast<typename ImageType::PixelType*>( img->GetPixelContainer()->GetBufferPointer() );
50 
51  if (!img->GetPixelContainer()->GetContainerManageMemory())
52  {
53  transferOwnership=false;
54  }
55 
56  typename ImageType::Pointer out = ImageType::New();
57 
58  out->CopyInformation( img );
59  out->SetRegions( img->GetBufferedRegion() );
60 
61  // Set the image's pixel container to import the pointer provided.
62  out->GetPixelContainer()->SetImportPointer(buffer, numberOfElements, transferOwnership );
63  img->GetPixelContainer()->SetContainerManageMemory(!transferOwnership);
64 
65  return out;
66 
67 }
68 
69 
70 template< class TPixelType, unsigned int NImageDimension, unsigned int NLength >
73 GetVectorImageFromImage( itk::Image< itk::Vector< TPixelType, NLength >, NImageDimension> *img, bool transferOwnership = false )
74 {
76 
77  size_t numberOfElements = img->GetBufferedRegion().GetNumberOfPixels();
78  typename VectorImageType::InternalPixelType* buffer = reinterpret_cast<typename VectorImageType::InternalPixelType*>( img->GetPixelContainer()->GetBufferPointer() );
79 
80  // Unlike an image of Vectors a VectorImage's container is a
81  // container of TPixelType, whos size is the image's number of
82  // pixels * number of pixels per component
83  numberOfElements *= NImageDimension;
84 
85 
86  if (!img->GetPixelContainer()->GetContainerManageMemory())
87  {
88  transferOwnership=false;
89  }
90 
91 
92  typename VectorImageType::Pointer out = VectorImageType::New();
93 
94  // Set the image's pixel container to import the pointer provided.
95  out->GetPixelContainer()->SetImportPointer(buffer, numberOfElements, transferOwnership );
96  img->GetPixelContainer()->SetContainerManageMemory(!transferOwnership);
97  out->CopyInformation( img );
98  out->SetRegions( img->GetBufferedRegion() );
99 
100  return out;
101 }
102 
103 
104 template< class TPixelType, unsigned int NImageDimension >
107 GetVectorImageFromImage( itk::Image< itk::Vector< TPixelType, NImageDimension >, NImageDimension> *img, bool transferOwnership = false )
108 {
109  return GetVectorImageFromImage<TPixelType,NImageDimension,NImageDimension>(img, transferOwnership);
110 }
111 
112 }
113 }
114 
115 #endif // sitkImageConvert_h
SizeValueType GetNumberOfPixels() const
unsigned int GetNumberOfComponentsPerPixel() const override
SITKCommon_HIDDEN itk::VectorImage< TPixelType, NImageDimension >::Pointer GetVectorImageFromImage(itk::Image< itk::Vector< TPixelType, NLength >, NImageDimension > *img, bool transferOwnership=false)
#define sitkExceptionMacro(x)
Definition: sitkMacro.h:88
virtual const RegionType & GetBufferedRegion() const
SITKCommon_HIDDEN itk::Image< itk::Vector< TPixelType, ImageDimension >, ImageDimension >::Pointer GetImageFromVectorImage(itk::VectorImage< TPixelType, ImageDimension > *img, bool transferOwnership=false)
A utility method to help convert between itk image types efficiently.
PixelContainer * GetPixelContainer()
#define SITKCommon_HIDDEN
Definition: sitkCommon.h:44