ITK  4.9.0
Insight Segmentation and Registration Toolkit
itkGPUImage.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 
19 #ifndef itkGPUImage_h
20 #define itkGPUImage_h
21 
22 #include "itkImage.h"
23 #include "itkGPUImageDataManager.h"
24 #include "itkVersion.h"
25 #include "itkObjectFactoryBase.h"
26 
27 namespace itk
28 {
39 template <typename TPixel, unsigned int VImageDimension = 2>
40 class GPUImage : public Image<TPixel,VImageDimension>
41 {
42 public:
43  typedef GPUImage Self;
48 
49  itkNewMacro(Self);
50 
51  itkTypeMacro(GPUImage, Image);
52 
53  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
54 
55  typedef typename Superclass::PixelType PixelType;
56  typedef typename Superclass::ValueType ValueType;
62  typedef typename Superclass::SizeType SizeType;
63  typedef typename Superclass::IndexType IndexType;
69 
71 
73  //typedef typename Superclass::NeighborhoodAccessorFunctorType
74  // NeighborhoodAccessorFunctorType;
75 
76  //
77  // Allocate CPU and GPU memory space
78  //
79  virtual void Allocate(bool initialize=false) ITK_OVERRIDE;
80 
81  virtual void Initialize() ITK_OVERRIDE;
82 
83  void FillBuffer(const TPixel & value);
84 
85  void SetPixel(const IndexType & index, const TPixel & value);
86 
87  const TPixel & GetPixel(const IndexType & index) const;
88 
89  TPixel & GetPixel(const IndexType & index);
90 
91  const TPixel & operator[](const IndexType & index) const;
92 
93  TPixel & operator[](const IndexType & index);
94 
96  void UpdateBuffers();
97 
98  //
99  // Get CPU buffer pointer
100  //
101  TPixel* GetBufferPointer() ITK_OVERRIDE;
102 
103  const TPixel * GetBufferPointer() const ITK_OVERRIDE;
104 
107  {
108  m_DataManager->SetGPUBufferDirty();
110  }
112 
114  const AccessorType GetPixelAccessor(void) const
115  {
116  m_DataManager->UpdateCPUBuffer();
118  }
120 
123  {
124  m_DataManager->SetGPUBufferDirty();
125  //return Superclass::GetNeighborhoodAccessor();
127  }
129 
132  {
133  m_DataManager->UpdateCPUBuffer();
134  //return Superclass::GetNeighborhoodAccessor();
136  }
138 
139  void SetPixelContainer(PixelContainer *container);
140 
143  {
144  m_DataManager->SetGPUBufferDirty(); return Superclass::GetPixelContainer();
145  }
146 
148  {
149  m_DataManager->UpdateCPUBuffer();
151  }
152 
153  void SetCurrentCommandQueue( int queueid )
154  {
155  m_DataManager->SetCurrentCommandQueue( queueid );
156  }
157 
159  return m_DataManager->GetCurrentCommandQueueID();
160  }
161 
162  itkGetModifiableObjectMacro(DataManager, GPUImageDataManager< GPUImage >);
163 
165 
166  /* Override DataHasBeenGenerated() in DataObject class.
167  * We need this because CPU time stamp is always bigger
168  * than GPU's. That is because Modified() is called at
169  * the end of each filter in the pipeline so although we
170  * increment GPU's time stamp in GPUGenerateData() the
171  * CPU's time stamp will be increased after that.
172  */
173  void DataHasBeenGenerated() ITK_OVERRIDE
174  {
176  if( m_DataManager->IsCPUBufferDirty() )
177  {
178  m_DataManager->Modified();
179  }
180  }
181 
183  virtual void Graft(const DataObject *data) ITK_OVERRIDE;
184 
185 protected:
186  GPUImage();
187  virtual ~GPUImage();
188 
189 private:
190 
191  GPUImage(const Self&) ITK_DELETE_FUNCTION;
192  void operator=(const Self&) ITK_DELETE_FUNCTION;
193 
195 };
196 
198 {
199 public:
204 
206  virtual const char* GetITKSourceVersion() const ITK_OVERRIDE {
207  return ITK_SOURCE_VERSION;
208  }
209  const char* GetDescription() const ITK_OVERRIDE {
210  return "A Factory for GPUImage";
211  }
213 
215  itkFactorylessNewMacro(Self);
216 
219 
221  static void RegisterOneFactory(void)
222  {
224 
226  }
227 
228 private:
229  GPUImageFactory(const Self&) ITK_DELETE_FUNCTION;
230  void operator=(const Self&) ITK_DELETE_FUNCTION;
231 
232 #define OverrideImageTypeMacro(pt,dm) this->RegisterOverride( \
233  typeid(itk::Image<pt,dm>).name(), \
234  typeid(itk::GPUImage<pt,dm>).name(), \
235  "GPU Image Override", \
236  true, \
237  itk::CreateObjectFunction<GPUImage<pt,dm> >::New() )
238 
240  {
241  if( IsGPUAvailable() )
242  {
243  // 1/2/3D
244  OverrideImageTypeMacro(unsigned char, 1);
245  OverrideImageTypeMacro(signed char, 1);
246  OverrideImageTypeMacro(int, 1);
247  OverrideImageTypeMacro(unsigned int, 1);
248  OverrideImageTypeMacro(float, 1);
249  OverrideImageTypeMacro(double, 1);
250 
251  OverrideImageTypeMacro(unsigned char, 2);
252  OverrideImageTypeMacro(signed char, 2);
253  OverrideImageTypeMacro(int, 2);
254  OverrideImageTypeMacro(unsigned int, 2);
255  OverrideImageTypeMacro(float, 2);
256  OverrideImageTypeMacro(double, 2);
257 
258  OverrideImageTypeMacro(unsigned char, 3);
259  OverrideImageTypeMacro(signed char, 3);
260  OverrideImageTypeMacro(int, 3);
261  OverrideImageTypeMacro(unsigned int, 3);
262  OverrideImageTypeMacro(float, 3);
263  OverrideImageTypeMacro(double, 3);
264  }
265  }
266 
267 };
268 
269 template <typename T>
271 {
272 public:
273  typedef T Type;
274 };
275 
276 template <typename TPixelType, unsigned int NDimension>
277 class GPUTraits< Image< TPixelType, NDimension > >
278 {
279 public:
281 };
282 
283 } // end namespace itk
284 
285 #ifndef ITK_MANUAL_INSTANTIATION
286 #include "itkGPUImage.hxx"
287 #endif
288 
289 #endif
AccessorType GetPixelAccessor(void)
Definition: itkGPUImage.h:106
void DataHasBeenGenerated() override
Definition: itkGPUImage.h:173
void SetPixelContainer(PixelContainer *container)
Superclass::RegionType RegionType
Definition: itkImage.h:137
PixelContainer::ConstPointer PixelContainerConstPointer
Definition: itkGPUImage.h:67
virtual void Initialize() override
Superclass::OffsetType OffsetType
Definition: itkImage.h:123
void FillBuffer(const TPixel &value)
#define ITK_SOURCE_VERSION
Definition: itkVersion.h:40
void UpdateBuffers()
static Pointer New()
TPixel InternalPixelType
Definition: itkImage.h:102
SmartPointer< Self > Pointer
Definition: itkGPUImage.h:45
itk::SmartPointer< const Self > ConstPointer
Definition: itkGPUImage.h:203
Superclass::IOPixelType IOPixelType
Definition: itkGPUImage.h:58
const char * GetDescription() const override
Definition: itkGPUImage.h:209
Create instances of classes using an object factory.
Implements a weak reference to an object.
virtual void Graft(const DataObject *data) override
Superclass::SpacingType SpacingType
Definition: itkImage.h:141
TPixel ValueType
Definition: itkImage.h:96
GPUDataManager::Pointer GetGPUDataManager() const
DefaultPixelAccessorFunctor< Self > AccessorFunctorType
Definition: itkGPUImage.h:70
GPUImage Self
Definition: itkGPUImage.h:43
void SetCurrentCommandQueue(int queueid)
Definition: itkGPUImage.h:153
NeighborhoodAccessorFunctor< Self > NeighborhoodAccessorFunctorType
Definition: itkGPUImage.h:72
TPixel PixelType
Definition: itkImage.h:89
virtual ~GPUImage()
Superclass::InternalPixelType InternalPixelType
Definition: itkGPUImage.h:57
Superclass::PixelType PixelType
Definition: itkGPUImage.h:55
PixelType IOPixelType
Definition: itkImage.h:104
GPUImageFactory Self
Definition: itkGPUImage.h:200
PixelContainer * GetPixelContainer()
Definition: itkGPUImage.h:142
Superclass::DirectionType DirectionType
Definition: itkGPUImage.h:59
Templated n-dimensional image class for the GPU.
Definition: itkGPUImage.h:40
virtual void DataHasBeenGenerated()
Superclass::AccessorType AccessorType
Definition: itkGPUImage.h:68
WeakPointer< const Self > ConstWeakPointer
Definition: itkGPUImage.h:47
Provides accessor interfaces to Get pixels and is meant to be used on pointers contained within Neigh...
Superclass::PixelContainer PixelContainer
Definition: itkGPUImage.h:61
const PixelContainer * GetPixelContainer() const
Definition: itkGPUImage.h:147
Superclass::IndexType IndexType
Definition: itkImage.h:119
virtual const char * GetITKSourceVersion() const override
Definition: itkGPUImage.h:206
const NeighborhoodAccessorFunctorType GetNeighborhoodAccessor() const
Definition: itkGPUImage.h:131
Superclass::SizeType SizeType
Definition: itkImage.h:126
Image< TPixel, VImageDimension > Superclass
Definition: itkGPUImage.h:44
GPUImageDataManager< GPUImage >::Pointer m_DataManager
Definition: itkGPUImage.h:194
Superclass::DirectionType DirectionType
Definition: itkImage.h:133
Provides a common API for pixel accessors for Image and VectorImage.
ImportImageContainer< SizeValueType, PixelType > PixelContainer
Definition: itkImage.h:130
itk::ObjectFactoryBase Superclass
Definition: itkGPUImage.h:201
NeighborhoodAccessorFunctorType GetNeighborhoodAccessor()
Definition: itkGPUImage.h:122
Superclass::SpacingType SpacingType
Definition: itkGPUImage.h:60
const TPixel & GetPixel(const IndexType &index) const
#define OverrideImageTypeMacro(pt, dm)
Definition: itkGPUImage.h:232
SmartPointer< const Self > ConstPointer
Definition: itkGPUImage.h:46
Superclass::OffsetType OffsetType
Definition: itkGPUImage.h:64
Superclass::ValueType ValueType
Definition: itkGPUImage.h:56
Superclass::RegionType RegionType
Definition: itkGPUImage.h:65
GPUImage< TPixelType, NDimension > Type
Definition: itkGPUImage.h:280
AccessorType GetPixelAccessor(void)
Definition: itkImage.h:256
int GetCurrentCommandQueueID()
Definition: itkGPUImage.h:158
static bool RegisterFactory(ObjectFactoryBase *, InsertionPositionType where=INSERT_AT_BACK, vcl_size_t position=0)
DefaultPixelAccessor< PixelType > AccessorType
Definition: itkImage.h:108
virtual void Allocate(bool initialize=false) override
itk::SmartPointer< Self > Pointer
Definition: itkGPUImage.h:202
PixelContainer * GetPixelContainer()
Definition: itkImage.h:233
static void RegisterOneFactory(void)
Definition: itkGPUImage.h:221
bool IsGPUAvailable()
static const unsigned int ImageDimension
Definition: itkGPUImage.h:53
Give access to partial aspects a type.
Superclass::SizeType SizeType
Definition: itkGPUImage.h:62
TPixel * GetBufferPointer() override
Superclass::IndexType IndexType
Definition: itkGPUImage.h:63
void SetPixel(const IndexType &index, const TPixel &value)
Base class for all data objects in ITK.
Templated n-dimensional image class.
Definition: itkImage.h:75
const AccessorType GetPixelAccessor(void) const
Definition: itkGPUImage.h:114
Defines an itk::Image front-end to a standard C-array.
PixelContainer::Pointer PixelContainerPointer
Definition: itkGPUImage.h:66