SourceXtractorPlusPlus  0.12
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  const T* m_data;
78  int m_stride;
80 
82 };
83 
84 template <typename T>
85 class UniversalImageChunk : public ImageChunk<T> {
86 
87 protected:
88 
94  ImageChunk<T>(nullptr, chunk.getWidth(), chunk.getHeight(), chunk.getWidth()) {
95  UniversalImageChunk<T>* universal_ptr = dynamic_cast<UniversalImageChunk<T>*>(&chunk);
96  if (universal_ptr) {
97  m_chunk_vector = std::move(universal_ptr->m_chunk_vector);
98  }
99  else {
100  m_chunk_vector.resize(m_width * m_height);
101  for (int cy = 0; cy < m_height; cy++) {
102  for (int cx = 0; cx < m_width; cx++) {
103  m_chunk_vector[cx + cy * m_stride] = chunk.getValue(cx, cy);
104  }
105  }
106  }
107  m_data = &m_chunk_vector[0];
108  }
109 
110  UniversalImageChunk(std::shared_ptr<const Image<T>> image, int x, int y, int width, int height) :
111  ImageChunk<T>(nullptr, width, height, width),
112  m_chunk_vector(width * height) {
113 
114  m_data = &m_chunk_vector[0];
115 
116  for (int cy=0; cy < height; cy++) {
117  for (int cx=0; cx < width; cx++) {
118  m_chunk_vector[cx + cy * width] = image->getValue(x + cx, y + cy);
119  }
120  }
121  }
122 
124  ImageChunk<T>(nullptr, width, height, width), m_chunk_vector(std::move(data))
125  {
126  assert(static_cast<int>(m_chunk_vector.size()) == width * height);
127  m_data = &m_chunk_vector[0];
128  }
129 
130 public:
131  template <typename... Args>
133  return std::shared_ptr<UniversalImageChunk<T>>(new UniversalImageChunk<T>(std::forward<Args>(args)...));
134  }
135 
137  }
138 
139  void setValue(int x, int y, T value) {
140  m_chunk_vector[x + y * m_stride] = value;
141  }
142 
143 private:
148  using ImageChunk<T>::m_data;
149 };
150 
151 
152 }
153 
154 #endif /* _SEFRAMEWORK_IMAGE_IMAGECHUNK_H_ */
ImageChunk(const T *data, int width, int height, int stride, std::shared_ptr< const Image< T >> image=nullptr)
Definition: ImageChunk.h:36
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
UniversalImageChunk(std::vector< T > &&data, int width, int height)
Definition: ImageChunk.h:123
T to_string(T...args)
virtual std::shared_ptr< ImageChunk< T > > getChunk(int x, int y, int width, int height) const override
Definition: ImageChunk.h:72
void setValue(int x, int y, T value)
Definition: ImageChunk.h:139
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
int getWidth() const final
Returns the width of the image chunk in pixels.
Definition: ImageChunk.h:63
STL class.
int getHeight() const final
Returns the height of the image chunk in pixels.
Definition: ImageChunk.h:68
static std::shared_ptr< UniversalImageChunk< T > > create(Args &&...args)
Definition: ImageChunk.h:132
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
T move(T...args)
STL class.
std::string getRepr() const override
Get a string identifying this image in a human readable manner.
Definition: ImageChunk.h:52
Interface representing an image.
Definition: Image.h:43
std::shared_ptr< const Image< T > > m_image
Definition: ImageChunk.h:81
UniversalImageChunk(std::shared_ptr< const Image< T >> image, int x, int y, int width, int height)
Definition: ImageChunk.h:110
T getValue(int x, int y) const final
Returns the value of the pixel with the coordinates (x,y)
Definition: ImageChunk.h:57
UniversalImageChunk(ImageChunk< T > &&chunk)
Definition: ImageChunk.h:93