SourceXtractorPlusPlus 1.0.3
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
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
30namespace SourceXtractor {
31
32inline 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
38inline 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
55inline 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
71inline int WrapCoordinates(int N, int v) {
72 return (v % N + N) % N;
73}
74
75template<typename T, int CoordinateInterpolation(int, int) = nullptr>
76class PaddedImage : public Image<T> {
77protected:
78 PaddedImage(std::shared_ptr<const Image<T>> img, int width, int height)
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
87public:
88 template<typename... Args>
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
120private:
124};
125
126
127template<typename T>
128class PaddedImage<T, nullptr> : public Image<T> {
129protected:
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
142public:
143 template<typename... Args>
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
182private:
187};
188
189} // end SourceXtractor
190
191#endif // _SEFRAMEWORK_IMAGE_PADDEDIMAGE_H
int getHeight() const override
Returns the height of the image in pixels.
int getWidth() const override
Returns the width of the image in pixels.
Interface representing an image.
Definition Image.h:44
std::string getRepr() const override
Get a string identifying this image in a human readable manner.
PaddedImage(std::shared_ptr< const Image< T > > img, int width, int height)
std::shared_ptr< const Image< T > > m_img
PaddedImage(std::shared_ptr< const Image< T > > img, int width, int height, T default_value)
int getWidth() const override
Returns the width of the image in pixels.
static std::shared_ptr< PaddedImage< T, nullptr > > create(Args &&... args)
int getHeight() const override
Returns the height of the image in pixels.
std::shared_ptr< ImageChunk< T > > getChunk(int x, int y, int width, int height) const override
PaddedImage(std::shared_ptr< const Image< T > > img, int width, int height)
Definition PaddedImage.h:78
int getHeight() const override
Returns the height of the image in pixels.
std::shared_ptr< ImageChunk< T > > getChunk(int x, int y, int width, int height) const override
std::shared_ptr< const Image< DetectionImage::PixelType > > m_img
std::string getRepr() const override
Get a string identifying this image in a human readable manner.
Definition PaddedImage.h:93
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.
Definition PaddedImage.h:97
static std::shared_ptr< UniversalImageChunk< T > > create(Args &&... args)
Definition ImageChunk.h:144
T forward(T... args)
T max(T... args)
int ReflectCoordinates(int N, int v)
Definition PaddedImage.h:38
int ReplicateCoordinates(int N, int v)
Definition PaddedImage.h:32
int WrapCoordinates(int N, int v)
Definition PaddedImage.h:71
int Reflect101Coordinates(int N, int v)
Definition PaddedImage.h:55