SourceXtractorPlusPlus  0.15
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> {
35 protected:
36  ImageChunk(std::shared_ptr<const std::vector<T>> data, int offset, int width, int height,
37  int stride)
38  : m_data(data),
39  m_offset(offset), m_stride(stride),
40  m_width(width), m_height(height) {}
41 
42 public:
44  create(std::shared_ptr<const std::vector<T>> data, int offset, int width, int height, int stride) {
45  return std::shared_ptr<ImageChunk<T>>(new ImageChunk<T>(data, offset, width, height, stride));
46  }
47 
48  virtual ~ImageChunk() {
49  }
50 
51  std::string getRepr() const override {
52  return "ImageChunk<" + std::to_string(m_width) + "," + std::to_string(m_height) + ">";
53  }
54 
56  T getValue(int x, int y) const {
57  assert(x >= 0 && y >=0 && x < m_width && y < m_height);
58  return (*m_data)[m_offset + x + y * m_stride];
59  }
60 
61  T getValue(const PixelCoordinate& coord) const {
62  assert(coord.m_x >= 0 && coord.m_y >=0 && coord.m_x < m_width && coord.m_y < m_height);
63  return (*m_data)[m_offset + coord.m_x + coord.m_y * m_stride];
64  }
65 
67  int getWidth() const final {
68  return m_width;
69  }
70 
72  int getHeight() const final {
73  return m_height;
74  }
75 
76  virtual std::shared_ptr<ImageChunk<T>> getChunk(int x, int y, int width, int height) const override {
77  return create(m_data, m_offset + x + y * m_stride, width, height, m_stride);
78  }
79 
80 protected:
84 };
85 
86 template <typename T>
87 class UniversalImageChunk : public ImageChunk<T> {
88 
89 protected:
90 
96  ImageChunk<T>(nullptr, 0, chunk.getWidth(), chunk.getHeight(), chunk.getWidth()) {
97  UniversalImageChunk<T>* universal_ptr = dynamic_cast<UniversalImageChunk<T>*>(&chunk);
98  if (universal_ptr) {
99  m_chunk_vector = std::move(universal_ptr->m_chunk_vector);
100  }
101  else {
102  m_chunk_vector = std::make_shared<std::vector<T>>(m_width * m_height);
103  for (int cy = 0; cy < m_height; cy++) {
104  for (int cx = 0; cx < m_width; cx++) {
105  (*m_chunk_vector)[cx + cy * m_stride] = chunk.getValue(cx, cy);
106  }
107  }
108  }
110  }
111 
112  UniversalImageChunk(const Image <T> *image, int x, int y, int width, int height)
113  : ImageChunk<T>(nullptr, 0, width, height, width),
114  m_chunk_vector(std::make_shared<std::vector<T>>(width * height)) {
115 
117 
118  for (int cy = 0; cy < height; cy++) {
119  for (int cx = 0; cx < width; cx++) {
120  (*m_chunk_vector)[cx + cy * width] = image->getValue(x + cx, y + cy);
121  }
122  }
123  }
124 
125  UniversalImageChunk(std::vector<T> &&data, int width, int height):
126  ImageChunk<T>(nullptr, 0, width, height, width),
128  {
129  assert(static_cast<int>(m_chunk_vector->size()) == width * height);
131  }
132 
133  UniversalImageChunk(int width, int height) :
134  ImageChunk<T>(nullptr, 0, width, height, width),
135  m_chunk_vector(std::make_shared<std::vector<T>>(width * height)) {
136  assert(static_cast<int>(m_chunk_vector->size()) == width * height);
138  }
139 
140 public:
141  template <typename... Args>
143  return std::shared_ptr<UniversalImageChunk<T>>(new UniversalImageChunk<T>(std::forward<Args>(args)...));
144  }
145 
147  }
148 
149  void setValue(int x, int y, T value) {
150  (*m_chunk_vector)[x + y * m_stride] = value;
151  }
152 
153  T& at(int x, int y) {
154  return (*m_chunk_vector)[x + y * m_stride];
155  }
156 
157 private:
162  using ImageChunk<T>::m_data;
163 };
164 
165 
166 }
167 
168 #endif /* _SEFRAMEWORK_IMAGE_IMAGECHUNK_H_ */
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
T getValue(int x, int y) const
Returns the value of the pixel with the coordinates (x,y)
Definition: ImageChunk.h:56
static std::shared_ptr< ImageChunk< T > > create(std::shared_ptr< const std::vector< T >> data, int offset, int width, int height, int stride)
Definition: ImageChunk.h:44
T getValue(const PixelCoordinate &coord) const
Definition: ImageChunk.h:61
std::shared_ptr< const std::vector< T > > m_data
Definition: ImageChunk.h:81
ImageChunk(std::shared_ptr< const std::vector< T >> data, int offset, int width, int height, int stride)
Definition: ImageChunk.h:36
virtual std::shared_ptr< ImageChunk< T > > getChunk(int x, int y, int width, int height) const override
Definition: ImageChunk.h:76
int getWidth() const final
Returns the width of the image chunk in pixels.
Definition: ImageChunk.h:67
int getHeight() const final
Returns the height of the image chunk in pixels.
Definition: ImageChunk.h:72
std::string getRepr() const override
Get a string identifying this image in a human readable manner.
Definition: ImageChunk.h:51
Interface representing an image.
Definition: Image.h:43
void setValue(int x, int y, T value)
Definition: ImageChunk.h:149
static std::shared_ptr< UniversalImageChunk< T > > create(Args &&... args)
Definition: ImageChunk.h:142
UniversalImageChunk(std::vector< T > &&data, int width, int height)
Definition: ImageChunk.h:125
std::shared_ptr< std::vector< T > > m_chunk_vector
Definition: ImageChunk.h:158
UniversalImageChunk(int width, int height)
Definition: ImageChunk.h:133
UniversalImageChunk(ImageChunk< T > &&chunk)
Definition: ImageChunk.h:95
UniversalImageChunk(const Image< T > *image, int x, int y, int width, int height)
Definition: ImageChunk.h:112
T make_shared(T... args)
T move(T... args)
STL namespace.
A pixel coordinate made of two integers m_x and m_y.
T to_string(T... args)