/usr/include/InsightToolkit/SpatialObject/itkMetaImageConverter.txx is in libinsighttoolkit3-dev 3.20.1+git20120521-6build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | /*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkMetaImageConverter.txx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkMetaImageConverter_txx
#define __itkMetaImageConverter_txx
#include "itkMetaImageConverter.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionIteratorWithIndex.h"
namespace itk
{
/** Constructor */
template <unsigned int NDimensions, class PixelType>
MetaImageConverter<NDimensions,PixelType>
::MetaImageConverter()
{
}
/** Convert a metaImage into an ImageMaskSpatialObject */
template <unsigned int NDimensions, class PixelType>
typename MetaImageConverter<NDimensions,PixelType>::MaskSpatialObjectPointer
MetaImageConverter<NDimensions,PixelType>
::MetaImageToImageMaskSpatialObject(MetaImage * image)
{
MaskSpatialObjectPointer spatialObject = MaskSpatialObjectType::New();
typedef itk::Image<unsigned char,NDimensions> ImageType;
typedef typename ImageType::Pointer ImagePointer;
typedef typename ImageType::SizeType SizeType;
typedef typename ImageType::RegionType RegionType;
ImagePointer myImage = ImageType::New();
SizeType size;
double spacing[NDimensions];
for(unsigned int i=0;i<NDimensions;i++)
{
size[i] = image->DimSize()[i];
spacing[i] = image->ElementSpacing()[i];
if(spacing[i] == 0) {spacing[i] = 1;}
}
RegionType region;
region.SetSize(size);
itk::Index<NDimensions> zeroIndex;
zeroIndex.Fill(0);
region.SetIndex( zeroIndex );
myImage->SetLargestPossibleRegion(region);
myImage->SetBufferedRegion(region);
myImage->SetRequestedRegion(region);
myImage->SetSpacing(spacing);
myImage->Allocate();
itk::ImageRegionIteratorWithIndex< ImageType > it(myImage, region);
for(unsigned int i = 0; !it.IsAtEnd(); i++, ++it)
{
it.Set(
static_cast< typename ImageType::PixelType >( image->ElementData(i) ));
}
spatialObject->SetImage(myImage);
spatialObject->SetId(image->ID());
spatialObject->SetParentId(image->ParentID());
spatialObject->GetProperty()->SetName(image->Name());
return spatialObject;
}
/** Convert a metaImage into an Image SpatialObject */
template <unsigned int NDimensions, class PixelType>
typename MetaImageConverter<NDimensions,PixelType>::SpatialObjectPointer
MetaImageConverter<NDimensions,PixelType>
::MetaImageToImageSpatialObject(MetaImage * image)
{
SpatialObjectPointer spatialObject = SpatialObjectType::New();
typedef itk::Image<PixelType,NDimensions> ImageType;
typedef typename ImageType::Pointer ImagePointer;
typedef typename ImageType::SizeType SizeType;
typedef typename ImageType::RegionType RegionType;
ImagePointer myImage = ImageType::New();
SizeType size;
double spacing[NDimensions];
for(unsigned int i=0;i<NDimensions;i++)
{
size[i] = image->DimSize()[i];
spacing[i] = image->ElementSpacing()[i];
if(spacing[i] == 0) {spacing[i] = 1;}
}
RegionType region;
region.SetSize(size);
itk::Index<NDimensions> zeroIndex;
zeroIndex.Fill(0);
region.SetIndex( zeroIndex );
myImage->SetLargestPossibleRegion(region);
myImage->SetBufferedRegion(region);
myImage->SetRequestedRegion(region);
myImage->SetSpacing(spacing);
myImage->Allocate();
itk::ImageRegionIteratorWithIndex< ImageType > it(myImage, region);
for(unsigned int i = 0; !it.IsAtEnd(); i++, ++it)
{
it.Set(
static_cast< typename ImageType::PixelType >( image->ElementData(i) ));
}
spatialObject->SetImage(myImage);
spatialObject->SetId(image->ID());
spatialObject->SetParentId(image->ParentID());
spatialObject->GetProperty()->SetName(image->Name());
return spatialObject;
}
/** Convert an Image SpatialObject into a metaImage */
template <unsigned int NDimensions, class PixelType>
MetaImage*
MetaImageConverter<NDimensions,PixelType>
::ImageSpatialObjectToMetaImage(SpatialObjectType * spatialObject)
{
typedef itk::Image<PixelType,NDimensions> ImageType;
typedef typename ImageType::ConstPointer ImageConstPointer;
typedef typename ImageType::SizeType SizeType;
typedef typename ImageType::RegionType RegionType;
ImageConstPointer SOImage = spatialObject->GetImage();
float spacing[NDimensions];
int size[NDimensions];
for(unsigned int i=0;i<NDimensions;i++)
{
size[i] = SOImage->GetLargestPossibleRegion().GetSize()[i];
spacing[i] = SOImage->GetSpacing()[i];
}
MetaImage* Image = new MetaImage(NDimensions,size,
spacing,MET_GetPixelType(typeid(PixelType)));
itk::ImageRegionConstIterator< ImageType > it(SOImage,
SOImage->GetLargestPossibleRegion());
for(unsigned int i = 0; !it.IsAtEnd(); i++, ++it)
{
Image->ElementData(i,it.Get());
}
Image->ID(spatialObject->GetId());
if(spatialObject->GetParent())
{
Image->ParentID(spatialObject->GetParent()->GetId());
}
return Image;
}
/** Read a meta file give the type */
template <unsigned int NDimensions, class PixelType>
typename MetaImageConverter<NDimensions,PixelType>::SpatialObjectPointer
MetaImageConverter<NDimensions,PixelType>
::ReadMeta(const char* name)
{
SpatialObjectPointer spatialObject;
MetaImage* Image = new MetaImage();
Image->Read(name);
Image->PrintInfo();
spatialObject = MetaImageToImageSpatialObject(Image);
return spatialObject;
}
/** Write a meta Image file */
template <unsigned int NDimensions, class PixelType>
bool
MetaImageConverter<NDimensions,PixelType>
::WriteMeta(SpatialObjectType* spatialObject,const char* name)
{
MetaImage* Image = ImageSpatialObjectToMetaImage(spatialObject);
Image->Write(name);
return true;
}
} // end namespace itk
#endif
|