SourceXtractorPlusPlus  0.16
Please provide a description of the project.
ImageAccessor.h
Go to the documentation of this file.
1 
18 #ifndef _SEFRAMEWORK_IMAGE_IMAGEACCESSOR_H
19 #define _SEFRAMEWORK_IMAGE_IMAGEACCESSOR_H
20 
23 
24 namespace SourceXtractor {
25 
40 template<typename T>
41 class ImageAccessor: public Image<T> {
42 public:
43 
48  enum AccessHint {
49  TOP_LEFT, //< The first coordinate is likely the top left corner of what is going to be needed
50  CENTERED, //< The first coordinate is likely the center of a region
51  BOTTOM_RIGHT, //< The first coordinate is likely the bottom right corner
52  };
53 
55  ~ImageAccessor() = default;
56 
72  explicit ImageAccessor(std::shared_ptr<const Image<T>> img, AccessHint hint = TOP_LEFT, int w = 64, int h = 1)
73  : m_image(img.get()), m_keep_alive(std::move(img)), m_hint(hint), m_read_width(w), m_read_height(h){};
74 
75  explicit ImageAccessor(const Image<T>& img, AccessHint hint = TOP_LEFT, int w = 64, int h = 64)
76  : m_image(&img), m_hint(hint), m_read_width(w), m_read_height(h){};
77 
81  ImageAccessor(const ImageAccessor<T>&) = delete;
82 
87 
92 
99  T getValue(int x, int y) {
101  x -= m_chunk_min.m_x;
102  y -= m_chunk_min.m_y;
103  return m_chunk->getValue(x, y);
104  }
105 
106  T getValue(const PixelCoordinate& coord) {
107  selectChunk(coord);
108  return m_chunk->getValue(coord - m_chunk_min);
109  }
110 
111  /*
112  * Forward these methods directly to the wrapped image
113  */
114 
115  std::string getRepr() const override {
116  return m_image->getRepr();
117  }
118 
119  int getWidth() const override {
120  return m_image->getWidth();
121  }
122 
123  int getHeight() const override {
124  return m_image->getHeight();
125  }
126 
127  std::shared_ptr<ImageChunk<T>> getChunk(int x, int y, int width, int height) const override {
128  return m_image->getChunk(x, y, width, height);
129  };
130 
131 private:
138 
143  void selectChunk(const PixelCoordinate& coord) {
144  if (m_chunk && coord >= m_chunk_min && coord <= m_chunk_max) {
145  return;
146  }
147  nextCoordinates(coord);
148  m_chunk = m_image->getChunk(m_chunk_min, m_chunk_max);
149  }
150 
154  void nextCoordinates(const PixelCoordinate& coord) {
155  if (!m_chunk) {
156  m_chunk_min = firstCoordinates(coord);
157  }
158  else {
159  switch (m_hint) {
160  case TOP_LEFT:
161  case CENTERED:
162  m_chunk_min.m_x = coord.m_x;
163  m_chunk_min.m_y = coord.m_y;
164  break;
165  case BOTTOM_RIGHT:
166  m_chunk_min.m_x = coord.m_x - m_read_width + 1;
167  m_chunk_min.m_y = coord.m_y - m_read_height + 1;
168  break;
169  }
170  }
171  // Make sure we don't leave the image
172  m_chunk_min.clip(m_image->getWidth(), m_image->getHeight());
173  // Max pixel
176  m_chunk_max.clip(m_image->getWidth(), m_image->getHeight());
177  }
178 
180  switch (m_hint) {
181  case CENTERED:
182  return PixelCoordinate(coord.m_x - m_read_width / 2, coord.m_y - m_read_height / 2);
183  case TOP_LEFT:
184  return coord;
185  case BOTTOM_RIGHT:
186  return PixelCoordinate(coord.m_x - m_read_width, coord.m_y - m_read_height);
187  }
188  return coord;
189  }
190 };
191 
192 } // end of namespace SourceXtractor
193 
194 #endif // _SEFRAMEWORK_IMAGE_IMAGEACCESSOR_H
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
std::shared_ptr< ImageChunk< T > > getChunk(int x, int y, int width, int height) const override
ImageAccessor(ImageAccessor< T > &&)=default
const Image< T > * m_image
ImageAccessor< T > & operator=(const ImageAccessor< T > &)=delete
std::shared_ptr< const Image< T > > m_keep_alive
std::string getRepr() const override
Get a string identifying this image in a human readable manner.
ImageAccessor(const Image< T > &img, AccessHint hint=TOP_LEFT, int w=64, int h=64)
Definition: ImageAccessor.h:75
ImageAccessor(const ImageAccessor< T > &)=delete
T getValue(const PixelCoordinate &coord)
ImageAccessor(std::shared_ptr< const Image< T >> img, AccessHint hint=TOP_LEFT, int w=64, int h=1)
Definition: ImageAccessor.h:72
std::shared_ptr< const ImageChunk< T > > m_chunk
int getHeight() const override
Returns the height of the image in pixels.
PixelCoordinate firstCoordinates(const PixelCoordinate &coord)
void nextCoordinates(const PixelCoordinate &coord)
void selectChunk(const PixelCoordinate &coord)
int getWidth() const override
Returns the width of the image in pixels.
Interface representing an image.
Definition: Image.h:43
T move(T... args)
STL namespace.
A pixel coordinate made of two integers m_x and m_y.