SourceXtractorPlusPlus  0.15
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PaddedImage.h
Go to the documentation of this file.
1 
17 /*
18  * @file SEFramework/Image/PaddedImage.h
19  * @date 10/09/18
20  * @author aalvarez
21  */
22 
23 #ifndef _SEFRAMEWORK_IMAGE_PADDEDIMAGE_H
24 #define _SEFRAMEWORK_IMAGE_PADDEDIMAGE_H
25 
29 
30 namespace SourceXtractor {
31 
32 inline int ReplicateCoordinates(int N, int v) {
33  if (v < 0) return 0;
34  if (v > N - 1) return N - 1;
35  return v;
36 }
37 
38 inline int ReflectCoordinates(int N, int v) {
39  if (v >= 0 && v < N) {
40  return v;
41  }
42 
43  if (v < 0) ++v;
44  v = std::abs(v);
45 
46  int offset = v % N;
47  int ntimes = v / N;
48 
49  if (ntimes % 2 == 0) {
50  return offset;
51  }
52  return N - offset - 1;
53 }
54 
55 inline int Reflect101Coordinates(int N, int v) {
56  if (v >= 0 && v < N) {
57  return v;
58  }
59 
60  int max = N - 1;
61  v = std::abs(v);
62  int offset = v % max;
63  int ntimes = v / max;
64 
65  if (ntimes % 2 == 0) {
66  return offset;
67  }
68  return max - offset;
69 }
70 
71 inline int WrapCoordinates(int N, int v) {
72  return (v % N + N) % N;
73 }
74 
75 template<typename T, int CoordinateInterpolation(int, int) = nullptr>
76 class PaddedImage : public Image<T> {
77 protected:
79  : m_img{img}, m_width{width}, m_height{height} {
80  auto wdiff = m_width - img->getWidth();
81  auto hdiff = m_height - img->getHeight();
82 
83  m_lpad = wdiff / 2;
84  m_tpad = hdiff / 2;
85  }
86 
87 public:
88  template<typename... Args>
90  return std::shared_ptr<PaddedImage<T, CoordinateInterpolation>>(new PaddedImage{std::forward<Args>(args)...});
91  }
92 
93  std::string getRepr() const override {
94  return "PaddedImage(" + m_img->getRepr() + ")";
95  }
96 
97  int getWidth() const override {
98  return m_width;
99  }
100 
101  int getHeight() const override {
102  return m_height;
103  }
104 
105  std::shared_ptr<ImageChunk<T>> getChunk(int x, int y, int width, int height) const override{
106  ImageAccessor<T> accessor(m_img, ImageAccessor<T>::TOP_LEFT, width, height);
107  auto chunk = UniversalImageChunk<T>::create(width, height);
108  auto img_w = accessor.getWidth();
109  auto img_h = accessor.getHeight();
110  for (int iy = 0; iy < height; ++iy) {
111  for (int ix = 0; ix < width; ++ix) {
112  auto img_x = CoordinateInterpolation(img_w, ix + x - m_lpad);
113  auto img_y = CoordinateInterpolation(img_h, iy + y - m_tpad);
114  chunk->at(ix, iy) = accessor.getValue(img_x, img_y);
115  }
116  }
117  return chunk;
118  }
119 
120 private:
124 };
125 
126 
127 template<typename T>
128 class PaddedImage<T, nullptr> : public Image<T> {
129 protected:
130  PaddedImage(std::shared_ptr<const Image<T>> img, int width, int height, T default_value)
131  : m_img{img}, m_width{width}, m_height{height}, m_default{default_value} {
132  auto wdiff = m_width - img->getWidth();
133  auto hdiff = m_height - img->getHeight();
134 
135  m_lpad = wdiff / 2;
136  m_tpad = hdiff / 2;
137  }
138 
139  PaddedImage(std::shared_ptr<const Image<T>> img, int width, int height): PaddedImage(img, width, height, {}) {
140  }
141 
142 public:
143  template<typename... Args>
144  static std::shared_ptr<PaddedImage<T, nullptr>> create(Args &&... args) {
145  return std::shared_ptr<PaddedImage<T, nullptr>>(new PaddedImage{std::forward<Args>(args)...});
146  }
147 
148  std::string getRepr() const override {
149  return "PaddedImage(" + m_img->getRepr() + ")";
150  }
151 
152  int getWidth() const override {
153  return m_width;
154  }
155 
156  int getHeight() const override {
157  return m_height;
158  }
159 
160  std::shared_ptr<ImageChunk<T>> getChunk(int x, int y, int width, int height) const override{
161  ImageAccessor<T> accessor(m_img, ImageAccessor<T>::TOP_LEFT, width, height);
162 
163  auto chunk = UniversalImageChunk<T>::create(width, height);
164  auto img_w = accessor.getWidth();
165  auto img_h = accessor.getHeight();
166  for (int iy = 0; iy < height; ++iy) {
167  for (int ix = 0; ix < width; ++ix) {
168  auto img_x = x + ix;
169  auto img_y = y + iy;
170  if (img_x < m_lpad || img_y < m_tpad || img_x >= img_w + m_lpad ||
171  img_y >= img_h + m_tpad) {
172  chunk->at(ix, iy) = m_default;
173  }
174  else {
175  chunk->at(ix, iy) = accessor.getValue(img_x - m_lpad, img_y - m_tpad);
176  }
177  }
178  }
179  return chunk;
180  }
181 
182 private:
184  int m_width, m_height;
187 };
188 
189 } // end SourceXtractor
190 
191 #endif // _SEFRAMEWORK_IMAGE_PADDEDIMAGE_H
int ReplicateCoordinates(int N, int v)
Definition: PaddedImage.h:32
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
static std::shared_ptr< PaddedImage< T, CoordinateInterpolation > > create(Args &&...args)
Definition: PaddedImage.h:89
int getWidth() const override
Returns the width of the image in pixels.
int getWidth() const override
Returns the width of the image in pixels.
Definition: PaddedImage.h:97
PaddedImage(std::shared_ptr< const Image< T >> img, int width, int height, T default_value)
Definition: PaddedImage.h:130
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
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.
Definition: PaddedImage.h:93
static std::shared_ptr< UniversalImageChunk< T > > create(Args &&...args)
Definition: ImageChunk.h:142
int Reflect101Coordinates(int N, int v)
Definition: PaddedImage.h:55
PaddedImage(std::shared_ptr< const Image< T >> img, int width, int height)
Definition: PaddedImage.h:78
T max(T...args)
std::shared_ptr< const Image< T > > m_img
Definition: PaddedImage.h:121
int ReflectCoordinates(int N, int v)
Definition: PaddedImage.h:38
Interface representing an image.
Definition: Image.h:43
std::shared_ptr< ImageChunk< T > > getChunk(int x, int y, int width, int height) const override
Definition: PaddedImage.h:105
PaddedImage(std::shared_ptr< const Image< T >> img, int width, int height)
Definition: PaddedImage.h:139
int WrapCoordinates(int N, int v)
Definition: PaddedImage.h:71
int getHeight() const override
Returns the height of the image in pixels.
Definition: PaddedImage.h:101