SourceXtractorPlusPlus 1.0.3
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
ImageChunk.h
Go to the documentation of this file.
1
17/*
18 * ImageChunk.h
19 *
20 * Created on: Aug 30, 2017
21 * Author: mschefer
22 */
23
24#ifndef _SEFRAMEWORK_IMAGE_IMAGECHUNK_H_
25#define _SEFRAMEWORK_IMAGE_IMAGECHUNK_H_
26
28
29#include <cassert>
30#include <vector>
31
32
33namespace SourceXtractor {
34
35template<typename T>
36class ImageChunk : public Image<T> {
37protected:
38 ImageChunk(std::shared_ptr<const std::vector<T>> data, int offset, int width, int height,
39 int stride)
40 : m_data(data),
41 m_offset(offset), m_stride(stride),
42 m_width(width), m_height(height) {}
43
44public:
46 create(std::shared_ptr<const std::vector<T>> data, int offset, int width, int height, int stride) {
47 return std::shared_ptr<ImageChunk<T>>(new ImageChunk<T>(data, offset, width, height, stride));
48 }
49
50 virtual ~ImageChunk() {
51 }
52
53 std::string getRepr() const override {
54 return "ImageChunk<" + std::to_string(m_width) + "," + std::to_string(m_height) + ">";
55 }
56
58 T getValue(int x, int y) const {
59 assert(x >= 0 && y >=0 && x < m_width && y < m_height);
60 return (*m_data)[m_offset + x + y * m_stride];
61 }
62
63 T getValue(const PixelCoordinate& coord) const {
64 assert(coord.m_x >= 0 && coord.m_y >=0 && coord.m_x < m_width && coord.m_y < m_height);
65 return (*m_data)[m_offset + coord.m_x + coord.m_y * m_stride];
66 }
67
69 int getWidth() const final {
70 return m_width;
71 }
72
74 int getHeight() const final {
75 return m_height;
76 }
77
78 std::shared_ptr<ImageChunk<T>> getChunk(int x, int y, int width, int height) const override {
79 return create(m_data, m_offset + x + y * m_stride, width, height, m_stride);
80 }
81
82protected:
86};
87
88template <typename T>
90
91protected:
92
98 ImageChunk<T>(nullptr, 0, chunk.getWidth(), chunk.getHeight(), chunk.getWidth()) {
99 UniversalImageChunk<T>* universal_ptr = dynamic_cast<UniversalImageChunk<T>*>(&chunk);
100 if (universal_ptr) {
101 m_chunk_vector = std::move(universal_ptr->m_chunk_vector);
102 }
103 else {
105 for (int cy = 0; cy < m_height; cy++) {
106 for (int cx = 0; cx < m_width; cx++) {
107 (*m_chunk_vector)[cx + cy * m_stride] = chunk.getValue(cx, cy);
108 }
109 }
110 }
112 }
113
114 UniversalImageChunk(const Image <T> *image, int x, int y, int width, int height)
115 : ImageChunk<T>(nullptr, 0, width, height, width),
116 m_chunk_vector(std::make_shared<std::vector<T>>(width * height)) {
117
119
120 for (int cy = 0; cy < height; cy++) {
121 for (int cx = 0; cx < width; cx++) {
122 (*m_chunk_vector)[cx + cy * width] = image->getValue(x + cx, y + cy);
123 }
124 }
125 }
126
127 UniversalImageChunk(std::vector<T> &&data, int width, int height):
128 ImageChunk<T>(nullptr, 0, width, height, width),
130 {
131 assert(static_cast<int>(m_chunk_vector->size()) == width * height);
133 }
134
135 UniversalImageChunk(int width, int height) :
136 ImageChunk<T>(nullptr, 0, width, height, width),
137 m_chunk_vector(std::make_shared<std::vector<T>>(width * height)) {
138 assert(static_cast<int>(m_chunk_vector->size()) == width * height);
140 }
141
142public:
143 template <typename... Args>
147
149 }
150
151 void setValue(int x, int y, T value) {
152 (*m_chunk_vector)[x + y * m_stride] = value;
153 }
154
155 T& at(int x, int y) {
156 return (*m_chunk_vector)[x + y * m_stride];
157 }
158
159private:
161 using ImageChunk<T>::m_width;
162 using ImageChunk<T>::m_height;
163 using ImageChunk<T>::m_stride;
164 using ImageChunk<T>::m_data;
165};
166
167
168}
169
170#endif /* _SEFRAMEWORK_IMAGE_IMAGECHUNK_H_ */
T getValue(int x, int y) const
Returns the value of the pixel with the coordinates (x,y)
Definition ImageChunk.h:58
ImageChunk(std::shared_ptr< const std::vector< T > > data, int offset, int width, int height, int stride)
Definition ImageChunk.h:38
T getValue(const PixelCoordinate &coord) const
Definition ImageChunk.h:63
static std::shared_ptr< ImageChunk< T > > create(std::shared_ptr< const std::vector< T > > data, int offset, int width, int height, int stride)
Definition ImageChunk.h:46
std::shared_ptr< const std::vector< T > > m_data
Definition ImageChunk.h:83
std::shared_ptr< ImageChunk< T > > getChunk(int x, int y, int width, int height) const override
Definition ImageChunk.h:78
int getWidth() const final
Returns the width of the image chunk in pixels.
Definition ImageChunk.h:69
int getHeight() const final
Returns the height of the image chunk in pixels.
Definition ImageChunk.h:74
std::string getRepr() const override
Get a string identifying this image in a human readable manner.
Definition ImageChunk.h:53
Interface representing an image.
Definition Image.h:44
static std::shared_ptr< UniversalImageChunk< T > > create(Args &&... args)
Definition ImageChunk.h:144
void setValue(int x, int y, T value)
Definition ImageChunk.h:151
UniversalImageChunk(std::vector< T > &&data, int width, int height)
Definition ImageChunk.h:127
std::shared_ptr< std::vector< T > > m_chunk_vector
Definition ImageChunk.h:160
UniversalImageChunk(int width, int height)
Definition ImageChunk.h:135
UniversalImageChunk(ImageChunk< T > &&chunk)
Definition ImageChunk.h:97
UniversalImageChunk(const Image< T > *image, int x, int y, int width, int height)
Definition ImageChunk.h:114
T forward(T... args)
T make_shared(T... args)
T move(T... args)
STL namespace.
A pixel coordinate made of two integers m_x and m_y.
T to_string(T... args)