SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NrrdToITK.h
Go to the documentation of this file.
1 //
2 // For more information, please see: http://software.sci.utah.edu
3 //
4 // The MIT License
5 //
6 // Copyright (c) 2009 Scientific Computing and Imaging Institute,
7 // University of Utah.
8 //
9 // License for the specific language governing rights and limitations under
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 // DEALINGS IN THE SOFTWARE.
27 
28 /*
29  *@file NrrdToITK.h
30  *@author McKay Davis
31  *@date Sun Oct 22 22:46:48 2006
32  */
33 
34 #ifndef SEG3D_NrrdToITK_H
35 #define SEG3D_NrrdToITK_H
36 
37 #include <sci_defs/insight_defs.h>
38 
39 #ifdef HAVE_INSIGHT
40 
41 #include <Core/Datatypes/NrrdData.h>
42 #include <Core/Datatypes/ITKDatatype.h>
43 #include <Core/Util/Assert.h>
44 #include <itkImageToImageFilter.h>
45 #include <itkImportImageFilter.h>
46 
47 #include <Core/Datatypes/share.h>
48 
49 namespace SCIRun {
50 
51 
52 // Note, this is a pretty specific funtion to convert a 3D volume of scalar
53 // nrrds with a stub axis of 1 dimension at axis 0
54 
56 nrrd_to_itk_image(NrrdDataHandle &nrrd);
57 
58 template <class PixType, int Dim>
59 itk::Object::Pointer
60 nrrd_to_itk_image(Nrrd *n)
61 {
62  ASSERT(n);
63  ASSERT(n->type == get_nrrd_type<PixType>());
64  ASSERT(n->dim == Dim+1);
65 
66  typedef itk::ImportImageFilter < PixType , Dim > ImportFilterType;
67  typename ImportFilterType::Pointer importFilter = ImportFilterType::New();
68  typename ImportFilterType::SizeType size;
69 
70  double origin[Dim];
71  double spacing[Dim];
72 
73  unsigned int count = 1;
74  for(unsigned int i=0; i < n->dim-1; i++) {
75  count *= n->axis[i+1].size;
76  size[i] = n->axis[i+1].size;
77 
78  if (!AIR_EXISTS(n->axis[i+1].min)) {
79  origin[i] = 0;
80  } else {
81  origin[i] = n->axis[i+1].min;
82  }
83 
84  if (!AIR_EXISTS(n->axis[i+1].spacing)) {
85  spacing[i] = 1.0;
86  } else {
87  spacing[i] = n->axis[i+1].spacing;
88  }
89  }
90  typename ImportFilterType::IndexType start;
91  typename ImportFilterType::RegionType region;
92  start.Fill(0);
93  region.SetIndex(start);
94  region.SetSize(size);
95  importFilter->SetRegion(region);
96  importFilter->SetOrigin(origin);
97  importFilter->SetSpacing(spacing);
98  importFilter->SetImportPointer((PixType *)n->data, count, false);
99  importFilter->Update();
100 
101  return importFilter->GetOutput();
102 }
103 
104 
105 template <class PixType, int Dim>
107 itk_image_to_nrrd(ITKDatatypeHandle &img_handle)
108 {
109  typedef itk::Image<PixType, Dim> ImageType;
110 
111  ImageType *img = dynamic_cast<ImageType *>(img_handle->data_.GetPointer());
112  if (img == 0) {
113  return 0;
114  }
115 
116  NrrdData *nrrd_data = new NrrdData(img_handle.get_rep());
117  Nrrd *nrrd = nrrd_data->nrrd_;
118 
119  size_t size[NRRD_DIM_MAX];
120  size[0] = 1;
121  size[1] = img->GetRequestedRegion().GetSize()[0];
122  size[2] = img->GetRequestedRegion().GetSize()[1];
123  size[3] = img->GetRequestedRegion().GetSize()[2];
124 
125  unsigned int centers[NRRD_DIM_MAX];
126  centers[0] = nrrdCenterNode;
127  centers[1] = nrrdCenterNode;
128  centers[2] = nrrdCenterNode;
129  centers[3] = nrrdCenterNode;
130 
131  nrrdWrap_nva(nrrd, (void *)img->GetBufferPointer(),
132  get_nrrd_type<PixType>(), 4, size);
133 
134  nrrdAxisInfoSet_nva(nrrd, nrrdAxisInfoCenter, centers);
135 
136  nrrd->axis[0].spacing = AIR_NAN;
137  nrrd->axis[0].min = 0;
138  nrrd->axis[0].max = 1;
139  nrrd->axis[0].kind = nrrdKindStub;
140 
141  for(unsigned int i = 0; i < Dim; i++) {
142  nrrd->axis[i+1].spacing = img->GetSpacing()[i];
143  nrrd->axis[i+1].min = img->GetOrigin()[i];
144  nrrd->axis[i+1].max = nrrd->axis[i+1].min +
145  (nrrd->axis[i+1].size-1) * nrrd->axis[i+1].spacing;
146  nrrd->axis[i+1].kind = nrrdKindDomain;
147  }
148  return nrrd_data;
149 }
150 
151 
152 }
153 #endif
154 #endif
#define SCISHARE
Definition: share.h:39
#define ASSERT(condition)
Definition: Assert.h:110
LockingHandle< NrrdData > NrrdDataHandle
Definition: NrrdData.h:108
LockingHandle< ITKDatatype > ITKDatatypeHandle
Definition: ITKDatatype.h:69
int count
Definition: readAllFields.py:25
int n
Definition: eab.py:9
int size
Definition: eabLatVolData.py:2