SourceXtractorPlusPlus  0.15
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  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),
74  m_read_height(h) {};
75 
76  ImageAccessor(const Image<T>& img, AccessHint hint = TOP_LEFT, int w = 64, int h = 64)
77  : m_image(&img), m_hint(hint), m_read_width(w), m_read_height(h) {};
78 
82  ImageAccessor(const ImageAccessor<T>&) = delete;
83 
87  ImageAccessor(ImageAccessor<T>&&) = default;
88 
93 
100  T getValue(int x, int y) {
102  x -= m_chunk_min.m_x;
103  y -= m_chunk_min.m_y;
104  return m_chunk->getValue(x, y);
105  }
106 
107  T getValue(const PixelCoordinate& coord) {
108  selectChunk(coord);
109  return m_chunk->getValue(coord - m_chunk_min);
110  }
111 
112  /*
113  * Forward these methods directly to the wrapped image
114  */
115 
116  std::string getRepr() const override {
117  return m_image->getRepr();
118  }
119 
120  int getWidth() const override {
121  return m_image->getWidth();
122  }
123 
124  int getHeight() const override {
125  return m_image->getHeight();
126  }
127 
128  std::shared_ptr<ImageChunk<T>> getChunk(int x, int y, int width, int height) const override {
129  return m_image->getChunk(x, y, width, height);
130  };
131 
132 private:
133  const Image<T>* m_image;
139 
144  void selectChunk(const PixelCoordinate& coord) {
145  if (m_chunk && coord >= m_chunk_min && coord <= m_chunk_max) {
146  return;
147  }
148  nextCoordinates(coord);
149  m_chunk = m_image->getChunk(m_chunk_min, m_chunk_max);
150  }
151 
155  void nextCoordinates(const PixelCoordinate& coord) {
156  if (!m_chunk) {
157  m_chunk_min = firstCoordinates(coord);
158  }
159  else {
160  switch (m_hint) {
161  case TOP_LEFT:
162  case CENTERED:
163  m_chunk_min.m_x = coord.m_x;
164  m_chunk_min.m_y = coord.m_y;
165  break;
166  case BOTTOM_RIGHT:
167  m_chunk_min.m_x = coord.m_x - m_read_width + 1;
168  m_chunk_min.m_y = coord.m_y - m_read_height + 1;
169  break;
170  }
171  }
172  // Make sure we don't leave the image
173  m_chunk_min.clip(m_image->getWidth(), m_image->getHeight());
174  // Max pixel
177  m_chunk_max.clip(m_image->getWidth(), m_image->getHeight());
178  }
179 
181  switch (m_hint) {
182  case CENTERED:
183  return PixelCoordinate(coord.m_x - m_read_width / 2, coord.m_y - m_read_height / 2);
184  case TOP_LEFT:
185  return coord;
186  case BOTTOM_RIGHT:
187  return PixelCoordinate(coord.m_x - m_read_width, coord.m_y - m_read_height);
188  }
189  return coord;
190  }
191 };
192 
193 } // end of namespace SourceXtractor
194 
195 #endif // _SEFRAMEWORK_IMAGE_IMAGEACCESSOR_H
void selectChunk(const PixelCoordinate &coord)
std::shared_ptr< ImageChunk< T > > getChunk(int x, int y, int width, int height) const override
ImageAccessor(const Image< T > &img, AccessHint hint=TOP_LEFT, int w=64, int h=64)
Definition: ImageAccessor.h:76
T getValue(const PixelCoordinate &coord)
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
int getWidth() const override
Returns the width of the image in pixels.
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
std::shared_ptr< const ImageChunk< T > > m_chunk
int getHeight() const override
Returns the height of the image in pixels.
STL class.
std::string getRepr() const override
Get a string identifying this image in a human readable manner.
A pixel coordinate made of two integers m_x and m_y.
T move(T...args)
PixelCoordinate firstCoordinates(const PixelCoordinate &coord)
ImageAccessor< T > & operator=(const ImageAccessor< T > &)=delete
std::shared_ptr< const Image< T > > m_keep_alive
void nextCoordinates(const PixelCoordinate &coord)
Interface representing an image.
Definition: Image.h:43
const Image< T > * m_image
ImageAccessor(std::shared_ptr< const Image< T >> img, AccessHint hint=TOP_LEFT, int w=64, int h=1)
Definition: ImageAccessor.h:72