SourceXtractorPlusPlus  0.10
Please provide a description of the project.
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 
27 
28 namespace SourceXtractor {
29 
30 inline int ReplicateCoordinates(int N, int v) {
31  if (v < 0) return 0;
32  if (v > N - 1) return N - 1;
33  return v;
34 }
35 
36 inline int ReflectCoordinates(int N, int v) {
37  if (v >= 0 && v < N) {
38  return v;
39  }
40 
41  if (v < 0) ++v;
42  v = std::abs(v);
43 
44  int offset = v % N;
45  int ntimes = v / N;
46 
47  if (ntimes % 2 == 0) {
48  return offset;
49  }
50  return N - offset - 1;
51 }
52 
53 inline int Reflect101Coordinates(int N, int v) {
54  if (v >= 0 && v < N) {
55  return v;
56  }
57 
58  int max = N - 1;
59  v = std::abs(v);
60  int offset = v % max;
61  int ntimes = v / max;
62 
63  if (ntimes % 2 == 0) {
64  return offset;
65  }
66  return max - offset;
67 }
68 
69 inline int WrapCoordinates(int N, int v) {
70  return (v % N + N) % N;
71 }
72 
73 template<typename T, int CoordinateInterpolation(int, int) = nullptr>
74 class PaddedImage : public ImageBase<T> {
75 protected:
76  PaddedImage(std::shared_ptr<const Image<T>> img, int width, int height)
77  : m_img{img}, m_width{width}, m_height{height} {
78  auto wdiff = m_width - img->getWidth();
79  auto hdiff = m_height - img->getHeight();
80 
81  m_lpad = wdiff / 2;
82  m_tpad = hdiff / 2;
83  }
84 
85 public:
86  template<typename... Args>
88  return std::shared_ptr<PaddedImage<T, CoordinateInterpolation>>(new PaddedImage{std::forward<Args>(args)...});
89  }
90 
91  std::string getRepr() const override {
92  return "PaddedImage(" + m_img->getRepr() + ")";
93  }
94 
95  T getValue(int x, int y) const override {
96  int tx = CoordinateInterpolation(m_img->getWidth(), x - m_lpad);
97  int ty = CoordinateInterpolation(m_img->getHeight(), y - m_tpad);
98  return m_img->getValue(tx, ty);
99  }
100 
101  int getWidth() const override {
102  return m_width;
103  }
104 
105  int getHeight() const override {
106  return m_height;
107  }
108 
109 private:
113 };
114 
115 
116 template<typename T>
117 class PaddedImage<T, nullptr> : public ImageBase<T> {
118 protected:
119  PaddedImage(std::shared_ptr<const Image<T>> img, int width, int height, T default_value)
120  : m_img{img}, m_width{width}, m_height{height}, m_default{default_value} {
121  auto wdiff = m_width - img->getWidth();
122  auto hdiff = m_height - img->getHeight();
123 
124  m_lpad = wdiff / 2;
125  m_tpad = hdiff / 2;
126  }
127 
128  PaddedImage(std::shared_ptr<const Image<T>> img, int width, int height): PaddedImage(img, width, height, {}) {
129  }
130 
131 public:
132  template<typename... Args>
134  return std::shared_ptr<PaddedImage<T, nullptr>>(new PaddedImage{std::forward<Args>(args)...});
135  }
136 
137  std::string getRepr() const override {
138  return "PaddedImage(" + m_img->getRepr() + ")";
139  }
140 
141  T getValue(int x, int y) const override {
142  if (x < m_lpad || y < m_tpad || x >= m_img->getWidth() + m_lpad || y >= m_img->getHeight() + m_tpad) {
143  return m_default;
144  }
145  return m_img->getValue(x - m_lpad, y - m_tpad);
146  }
147 
148  int getWidth() const override {
149  return m_width;
150  }
151 
152  int getHeight() const override {
153  return m_height;
154  }
155 
156 private:
161 };
162 
163 } // end SourceXtractor
164 
165 #endif // _SEFRAMEWORK_IMAGE_PADDEDIMAGE_H
STL class.
int ReplicateCoordinates(int N, int v)
Definition: PaddedImage.h:30
static std::shared_ptr< PaddedImage< T, nullptr > > create(Args &&... args)
Definition: PaddedImage.h:133
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
T getValue(int x, int y) const override
Returns the value of the pixel with the coordinates (x,y)
Definition: PaddedImage.h:141
int getWidth() const override
Returns the width of the image in pixels.
Definition: PaddedImage.h:101
PaddedImage(std::shared_ptr< const Image< T >> img, int width, int height, T default_value)
Definition: PaddedImage.h:119
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
STL class.
std::string getRepr() const override
Get a string identifying this image in a human readable manner.
Definition: PaddedImage.h:91
int Reflect101Coordinates(int N, int v)
Definition: PaddedImage.h:53
std::shared_ptr< const Image< T > > m_img
Definition: PaddedImage.h:157
PaddedImage(std::shared_ptr< const Image< T >> img, int width, int height)
Definition: PaddedImage.h:76
T max(T... args)
T getValue(int x, int y) const override
Returns the value of the pixel with the coordinates (x,y)
Definition: PaddedImage.h:95
std::shared_ptr< const Image< T > > m_img
Definition: PaddedImage.h:110
int getWidth() const override
Returns the width of the image in pixels.
Definition: PaddedImage.h:148
int ReflectCoordinates(int N, int v)
Definition: PaddedImage.h:36
Interface representing an image.
Definition: Image.h:43
PaddedImage(std::shared_ptr< const Image< T >> img, int width, int height)
Definition: PaddedImage.h:128
int getHeight() const override
Returns the height of the image in pixels.
Definition: PaddedImage.h:152
int WrapCoordinates(int N, int v)
Definition: PaddedImage.h:69
int getHeight() const override
Returns the height of the image in pixels.
Definition: PaddedImage.h:105
static std::shared_ptr< PaddedImage< T, CoordinateInterpolation > > create(Args &&... args)
Definition: PaddedImage.h:87
std::string getRepr() const override
Get a string identifying this image in a human readable manner.
Definition: PaddedImage.h:137