SourceXtractorPlusPlus  0.10
Please provide a description of the project.
ImageChunk.h
Go to the documentation of this file.
1 
17 /*
18  * ImageChunk.h
19  *
20  * Created on: Aug 30, 2017
21  * Author: mschefer
22  */
23 
24 #ifndef _SEFRAMEWORK_IMAGE_IMAGECHUNK_H_
25 #define _SEFRAMEWORK_IMAGE_IMAGECHUNK_H_
26 
27 #include <cassert>
28 
30 
31 namespace SourceXtractor {
32 
33 template <typename T>
34 class ImageChunk : public Image<T>, public std::enable_shared_from_this<ImageChunk<T>> {
35 protected:
36  ImageChunk(const T* data, int width, int height, int stride, std::shared_ptr<const Image<T>> image = nullptr) :
37  m_data(data),
38  m_stride(stride),
39  m_width(width),
40  m_height(height),
41  m_image(image) {}
42 
43 public:
44  static std::shared_ptr<ImageChunk<T>> create(const T* data, int width, int height,
45  int stride, std::shared_ptr<const Image<T>> image = nullptr) {
46  return std::shared_ptr<ImageChunk<T>>(new ImageChunk<T>(data, width, height, stride, image));
47  }
48 
49  virtual ~ImageChunk() {
50  }
51 
52  std::string getRepr() const override {
53  return "ImageChunk<" + std::to_string(m_width) + "," + std::to_string(m_height) + ">(" + m_image->getRepr() + ")";
54  }
55 
57  T getValue(int x, int y) const final {
58  assert(x >= 0 && y >=0 && x < m_width && y < m_height);
59  return m_data[x + y * m_stride];
60  }
61 
63  int getWidth() const final {
64  return m_width;
65  }
66 
68  int getHeight() const final {
69  return m_height;
70  }
71 
72  virtual std::shared_ptr<ImageChunk<T>> getChunk(int x, int y, int width, int height) const override {
73  return create(&m_data[x + y * m_stride], width, height, m_stride, this->shared_from_this());
74  }
75 
76 protected:
77 
78  // We use this in cases when the ImageChunk subclass allocates a buffer to store the chunk after construction
79  // of the base ImageChunk
80  void setDataPtr(const T* data) {
81  m_data = data;
82  }
83 
84 private:
85  const T* m_data;
86  int m_stride;
88 
90 };
91 
92 template <typename T>
93 class UniversalImageChunk : public ImageChunk<T> {
94 
95 protected:
96  UniversalImageChunk(std::shared_ptr<const Image<T>> image, int x, int y, int width, int height) :
97  ImageChunk<T>(nullptr, width, height, width),
98  m_chunk_vector(width * height) {
99 
100  this->setDataPtr(&m_chunk_vector[0]);
101 
102  for (int cy=0; cy < height; cy++) {
103  for (int cx=0; cx < width; cx++) {
104  m_chunk_vector[cx + cy * width] = image->getValue(x + cx, y + cy);
105  }
106  }
107  }
108 
109  UniversalImageChunk(std::vector<T> &&data, int width, int height):
110  ImageChunk<T>(nullptr, width, height, width), m_chunk_vector(std::move(data))
111  {
112  assert(static_cast<int>(m_chunk_vector.size()) == width * height);
113  this->setDataPtr(&m_chunk_vector[0]);
114  }
115 
116 public:
117  template <typename... Args>
119  return std::shared_ptr<UniversalImageChunk<T>>(new UniversalImageChunk<T>(std::forward<Args>(args)...));
120  }
121 
123  }
124 
125 private:
127 
128 };
129 
130 
131 }
132 
133 #endif /* _SEFRAMEWORK_IMAGE_IMAGECHUNK_H_ */
SourceXtractor::UniversalImageChunk::UniversalImageChunk
UniversalImageChunk(std::vector< T > &&data, int width, int height)
Definition: ImageChunk.h:109
SourceXtractor::ImageChunk::~ImageChunk
virtual ~ImageChunk()
Definition: ImageChunk.h:49
std::string
STL class.
std::shared_ptr
STL class.
SourceXtractor::ImageChunk::getHeight
int getHeight() const final
Returns the height of the image chunk in pixels.
Definition: ImageChunk.h:68
std::move
T move(T... args)
SourceXtractor::ImageChunk
Definition: Image.h:34
SourceXtractor::UniversalImageChunk::m_chunk_vector
std::vector< T > m_chunk_vector
Definition: ImageChunk.h:126
std::vector
STL class.
SourceXtractor::ImageChunk::m_image
std::shared_ptr< const Image< T > > m_image
Definition: ImageChunk.h:89
SourceXtractor::ImageChunk::m_stride
int m_stride
Definition: ImageChunk.h:86
SourceXtractor::ImageChunk::getRepr
std::string getRepr() const override
Definition: ImageChunk.h:52
SourceXtractor::ImageChunk::getValue
T getValue(int x, int y) const final
Returns the value of the pixel with the coordinates (x,y)
Definition: ImageChunk.h:57
SourceXtractor::Image
Interface representing an image.
Definition: Image.h:43
SourceXtractor::ImageChunk::m_height
int m_height
Definition: ImageChunk.h:87
SourceXtractor
Definition: Aperture.h:30
SourceXtractor::UniversalImageChunk
Definition: ImageChunk.h:93
SourceXtractor::UniversalImageChunk::~UniversalImageChunk
virtual ~UniversalImageChunk()
Definition: ImageChunk.h:122
SourceXtractor::ImageChunk::ImageChunk
ImageChunk(const T *data, int width, int height, int stride, std::shared_ptr< const Image< T >> image=nullptr)
Definition: ImageChunk.h:36
std::to_string
T to_string(T... args)
std::enable_shared_from_this
Image.h
x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
Definition: MoffatModelFittingTask.cpp:93
SourceXtractor::ImageChunk::m_width
int m_width
Definition: ImageChunk.h:87
SourceXtractor::UniversalImageChunk::create
static std::shared_ptr< UniversalImageChunk< T > > create(Args &&... args)
Definition: ImageChunk.h:118
std
STL namespace.
SourceXtractor::ImageChunk::setDataPtr
void setDataPtr(const T *data)
Definition: ImageChunk.h:80
SourceXtractor::ImageChunk::getChunk
virtual std::shared_ptr< ImageChunk< T > > getChunk(int x, int y, int width, int height) const override
Definition: ImageChunk.h:72
SourceXtractor::ImageChunk::m_data
const T * m_data
Definition: ImageChunk.h:85
SourceXtractor::ImageChunk::getWidth
int getWidth() const final
Returns the width of the image chunk in pixels.
Definition: ImageChunk.h:63
y
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
Definition: MoffatModelFittingTask.cpp:93
SourceXtractor::UniversalImageChunk::UniversalImageChunk
UniversalImageChunk(std::shared_ptr< const Image< T >> image, int x, int y, int width, int height)
Definition: ImageChunk.h:96
SourceXtractor::ImageChunk::create
static std::shared_ptr< ImageChunk< T > > create(const T *data, int width, int height, int stride, std::shared_ptr< const Image< T >> image=nullptr)
Definition: ImageChunk.h:44