FAST  3.2.0
Framework for Heterogeneous Medical Image Computing and Visualization
Image.hpp
Go to the documentation of this file.
1 #pragma once
2 
9 #include <FAST/DeviceManager.hpp>
10 #include <unordered_map>
11 
12 namespace fast {
13 
14 #ifndef SWIG
15 using pixel_deleter_t = std::function<void(void *)>;
16 using unique_pixel_ptr = std::unique_ptr<void, pixel_deleter_t>;
17 template<typename T>
18 auto pixel_deleter(void const * data) -> void
19 {
20  T const * p = static_cast<T const*>(data);
21  //std::cout << "[" << (uint64_t)p << "] is being deleted." << std::endl;
22  delete[] p;
23 }
24 
25 template<typename T>
27  return unique_pixel_ptr(ptr, &pixel_deleter<T>);
28 }
29 unique_pixel_ptr allocatePixelArray(std::size_t size, DataType type);
30 #endif
31 
32 class FAST_EXPORT Image : public SpatialDataObject {
34  public:
41  void createFromImage(Image::pointer image);
49  void create(VectorXui size, DataType type, uint nrOfChannels);
58  void create(uint width, uint height, DataType type, uint nrOfChannels);
68  void create(uint width, uint height, uint depth, DataType type, uint nrOfChannels);
78  void create(VectorXui size, DataType type, uint nrOfChannels, ExecutionDevice::pointer device,
79  const void *const data);
90  void create(uint width, uint height, DataType type, uint nrOfChannels, ExecutionDevice::pointer device, const void * const data);
102  void create(uint width, uint height, uint depth, DataType type, uint nrOfChannels, ExecutionDevice::pointer device, const void * const data);
111  void create(VectorXui size, DataType type, uint nrOfChannels, const void* const data);
121  void create(uint width, uint height, DataType type, uint nrOfChannels, const void* const data);
132  void create(uint width, uint height, uint depth, DataType type, uint nrOfChannels, const void* const data);
133 
144  template <class T>
145  void create(uint width, uint height, DataType type, uint nrOfChannels, ExecutionDevice::pointer device, std::unique_ptr<T> data);
157  template <class T>
158  void create(uint width, uint height, uint depth, DataType type, uint nrOfChannels, ExecutionDevice::pointer device, std::unique_ptr<T> data);
159 
169  template <class T>
170  void create(VectorXui, DataType type, uint nrOfChannels, ExecutionDevice::pointer device, std::unique_ptr<T> ptr);
171 
181  template <class T>
182  void create(uint width, uint height, DataType type, uint nrOfChannels, std::unique_ptr<T> ptr);
193  template <class T>
194  void create(uint width, uint height, uint depth, DataType type, uint nrOfChannels, std::unique_ptr<T> ptr);
195 
204  template <class T>
205  void create(VectorXui, DataType type, uint nrOfChannels, std::unique_ptr<T> ptr);
206 
207  OpenCLImageAccess::pointer getOpenCLImageAccess(accessType type, OpenCLDevice::pointer);
208  OpenCLBufferAccess::pointer getOpenCLBufferAccess(accessType type, OpenCLDevice::pointer);
209  ImageAccess::pointer getImageAccess(accessType type);
210 
211  ~Image();
212 
213  int getWidth() const;
214  int getHeight() const;
215  int getDepth() const;
219  int getNrOfVoxels() const;
220  Vector3ui getSize() const;
221  uchar getDimensions() const;
222  DataType getDataType() const;
223  int getNrOfChannels() const;
224  Vector3f getSpacing() const;
225  void setSpacing(Vector3f spacing);
226  void setSpacing(float x, float y, float z);
227 
228  float calculateMaximumIntensity();
229  float calculateMinimumIntensity();
230  float calculateAverageIntensity();
231 
236 
240  Image::pointer crop(VectorXi offset, VectorXi size, bool allowOutOfBoundsCropping = false);
241 
246  void fill(float value);
247 
248  // Override
249  DataBoundingBox getTransformedBoundingBox() const override;
250  DataBoundingBox getBoundingBox() const override;
251 
252  void free(ExecutionDevice::pointer device) override;
253  void freeAll() override;
254  protected:
255  Image();
256 
263  void setData(ExecutionDevice::pointer device, void* data);
270  void copyData(ExecutionDevice::pointer device, const void* const data);
271 
272  void findDeviceWithUptodateData(ExecutionDevice::pointer& device, bool& isOpenCLImage);
273 
274  // OpenCL Images
275  std::unordered_map<OpenCLDevice::pointer, cl::Image*> mCLImages;
276  std::unordered_map<OpenCLDevice::pointer, bool> mCLImagesIsUpToDate;
277 
278  // OpenCL Buffers
279  std::unordered_map<OpenCLDevice::pointer, cl::Buffer*> mCLBuffers;
280  std::unordered_map<OpenCLDevice::pointer, bool> mCLBuffersIsUpToDate;
281 
282  // Host data
286 
287  void setAllDataToOutOfDate();
288  bool isInitialized() const;
289 
290  void updateOpenCLImageData(OpenCLDevice::pointer device);
291  void transferCLImageFromHost(OpenCLDevice::pointer device);
292  void transferCLImageToHost(OpenCLDevice::pointer device);
293 
294  void updateOpenCLBufferData(OpenCLDevice::pointer device);
295  void transferCLBufferFromHost(OpenCLDevice::pointer device);
296  void transferCLBufferToHost(OpenCLDevice::pointer device);
297 
298  void updateHostData();
299 
300  bool hasAnyData();
301 
302  uint getBufferSize() const;
303 
304  uint mWidth, mHeight, mDepth;
309 
310  Vector3f mSpacing;
311 
312  float mMaximumIntensity, mMinimumIntensity, mAverageIntensity;
313  unsigned long mMaxMinTimestamp, mAverageIntensityTimestamp;
314  bool mMaxMinInitialized, mAverageInitialized;
315  void calculateMaxAndMinIntensity();
316 
317  // Declare as friends so they can get access to the accessFinished methods
318  friend class ImageAccess;
319  friend class OpenCLBufferAccess;
320  friend class OpenCLImageAccess;
321 };
322 
323 template <class T>
324 void Image::create(uint width, uint height, DataType type, uint nrOfChannels, ExecutionDevice::pointer device, std::unique_ptr<T> ptr) {
325  create(width, height, type, nrOfChannels);
326 
327  setData(device, ptr.release());
328 }
329 
330 template <class T>
331 void Image::create(uint width, uint height, uint depth, DataType type, uint nrOfChannels, ExecutionDevice::pointer device, std::unique_ptr<T> ptr) {
332  create(width, height, depth, type, nrOfChannels);
333 
334  setData(device, ptr.release());
335 }
336 
337 template <class T>
338 void Image::create(uint width, uint height, DataType type, uint nrOfChannels, std::unique_ptr<T> ptr) {
339  create(width, height, type, nrOfChannels, DeviceManager::getInstance()->getDefaultDevice(), std::move(ptr));
340 }
341 
342 template <class T>
343 void Image::create(uint width, uint height, uint depth, DataType type, uint nrOfChannels, std::unique_ptr<T> ptr) {
344  create(width, height, depth, type, nrOfChannels, DeviceManager::getInstance()->getDefaultDevice(), std::move(ptr));
345 }
346 
347 template <class T>
348 void Image::create(VectorXui size, DataType type, uint nrOfChannels, std::unique_ptr<T> ptr) {
349  if(size.size() == 3) {
350  create(size.x(), size.y(), size.z(), type, nrOfChannels, DeviceManager::getInstance()->getDefaultDevice(),
351  std::move(ptr));
352  } else if(size.size() == 2) {
353  create(size.x(), size.y(), type, nrOfChannels, DeviceManager::getInstance()->getDefaultDevice(),
354  std::move(ptr));
355  } else {
356  throw Exception("Incorrect size");
357  }
358 }
359 
360 template <class T>
361 void Image::create(VectorXui size, DataType type, uint nrOfChannels, ExecutionDevice::pointer device, std::unique_ptr<T> ptr) {
362  if(size.size() == 3) {
363  create(size.x(), size.y(), size.z(), type, nrOfChannels, device,
364  std::move(ptr));
365  } else if(size.size() == 2) {
366  create(size.x(), size.y(), type, nrOfChannels, device,
367  std::move(ptr));
368  } else {
369  throw Exception("Incorrect size");
370  }
371 }
372 
373 } // end namespace fast
374 
fast::ExecutionDevice::pointer
std::shared_ptr< ExecutionDevice > pointer
Definition: ExecutionDevice.hpp:11
fast::Image::mHostData
unique_pixel_ptr mHostData
Definition: Image.hpp:283
fast::Image::setData
void setData(ExecutionDevice::pointer device, void *data)
fast::pixel_deleter
auto pixel_deleter(void const *data) -> void
Definition: Image.hpp:18
fast::DeviceManager::getDefaultDevice
ExecutionDevice::pointer getDefaultDevice()
fast::DataType
DataType
Definition: DataTypes.hpp:38
fast::Exception
Definition: Exception.hpp:15
fast::Image::mWidth
uint mWidth
Definition: Image.hpp:304
fast::pixel_deleter_t
std::function< void(void *)> pixel_deleter_t
Definition: Image.hpp:15
fast
Definition: AffineTransformation.hpp:7
fast::OpenCLBufferAccess
Definition: OpenCLBufferAccess.hpp:13
fast::Image::mHostDataIsUpToDate
bool mHostDataIsUpToDate
Definition: Image.hpp:285
fast::allocatePixelArray
unique_pixel_ptr allocatePixelArray(std::size_t size, DataType type)
fast::OpenCLImageAccess
Definition: OpenCLImageAccess.hpp:14
fast::Image
Definition: Image.hpp:32
fast::VectorXui
Eigen::Matrix< uint, Eigen::Dynamic, 1 > VectorXui
Definition: DataTypes.hpp:33
ExecutionDevice.hpp
ImageAccess.hpp
fast::Image::mIsInitialized
bool mIsInitialized
Definition: Image.hpp:308
fast::OpenCLImageAccess::pointer
std::unique_ptr< OpenCLImageAccess > pointer
Definition: OpenCLImageAccess.hpp:23
fast::Image::mHostHasData
bool mHostHasData
Definition: Image.hpp:284
FAST_OBJECT
#define FAST_OBJECT(className)
Definition: Object.hpp:9
SpatialDataObject.hpp
OpenCLImageAccess.hpp
fast::Image::mSpacing
Vector3f mSpacing
Definition: Image.hpp:310
fast::Image::mMaxMinInitialized
bool mMaxMinInitialized
Definition: Image.hpp:314
fast::Image::mType
DataType mType
Definition: Image.hpp:306
accessType
accessType
Definition: Access.hpp:5
fast::Image::mCLImagesIsUpToDate
std::unordered_map< OpenCLDevice::pointer, bool > mCLImagesIsUpToDate
Definition: Image.hpp:276
fast::DeviceManager::getInstance
static DeviceManager * getInstance()
DataTypes.hpp
fast::ImageAccess
Definition: ImageAccess.hpp:8
fast::Image::mMaxMinTimestamp
unsigned long mMaxMinTimestamp
Definition: Image.hpp:313
fast::Image::mCLBuffersIsUpToDate
std::unordered_map< OpenCLDevice::pointer, bool > mCLBuffersIsUpToDate
Definition: Image.hpp:280
fast::Image::mChannels
uint mChannels
Definition: Image.hpp:307
fast::DataObject::pointer
std::shared_ptr< DataObject > pointer
Definition: DataObject.hpp:16
fast::Image::mDimensions
uchar mDimensions
Definition: Image.hpp:305
uint
unsigned int uint
Definition: DataTypes.hpp:16
fast::Image::mCLBuffers
std::unordered_map< OpenCLDevice::pointer, cl::Buffer * > mCLBuffers
Definition: Image.hpp:279
fast::make_unique_pixel
auto make_unique_pixel(T *ptr) -> unique_pixel_ptr
Definition: Image.hpp:26
uchar
unsigned char uchar
Definition: DataTypes.hpp:14
DeviceManager.hpp
fast::Image::mMinimumIntensity
float mMinimumIntensity
Definition: Image.hpp:312
fast::Image::create
void create(VectorXui size, DataType type, uint nrOfChannels)
fast::ImageAccess::pointer
std::unique_ptr< ImageAccess > pointer
Definition: ImageAccess.hpp:37
fast::SpatialDataObject
Definition: SpatialDataObject.hpp:10
fast::OpenCLBufferAccess::pointer
std::unique_ptr< OpenCLBufferAccess > pointer
Definition: OpenCLBufferAccess.hpp:19
fast::unique_pixel_ptr
std::unique_ptr< void, pixel_deleter_t > unique_pixel_ptr
Definition: Image.hpp:16
OpenCLBufferAccess.hpp
fast::Image::mCLImages
std::unordered_map< OpenCLDevice::pointer, cl::Image * > mCLImages
Definition: Image.hpp:275
fast::DataBoundingBox
Definition: DataBoundingBox.hpp:13
fast::Vector3ui
Eigen::Matrix< uint, 3, 1 > Vector3ui
Definition: DataTypes.hpp:35