SourceXtractorPlusPlus
1.0.3
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
SEFramework
SEFramework
Image
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
27
#include "
SEFramework/Image/Image.h
"
28
29
#include <cassert>
30
#include <vector>
31
32
33
namespace
SourceXtractor
{
34
35
template
<
typename
T>
36
class
ImageChunk
:
public
Image
<T> {
37
protected
:
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
44
public
:
45
static
std::shared_ptr<ImageChunk<T>
>
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
82
protected
:
83
std::shared_ptr<const std::vector<T>
>
m_data
;
84
int
m_offset
,
m_stride
;
85
int
m_width
,
m_height
;
86
};
87
88
template
<
typename
T>
89
class
UniversalImageChunk
:
public
ImageChunk
<T> {
90
91
protected
:
92
97
explicit
UniversalImageChunk
(
ImageChunk<T>
&& chunk) :
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
{
104
m_chunk_vector
=
std::make_shared<std::vector<T>
>(
m_width
*
m_height
);
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
}
111
m_data
=
m_chunk_vector
;
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
118
m_data
=
m_chunk_vector
;
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),
129
m_chunk_vector
(
std
::
make_shared
<
std
::
vector
<T>>(
std
::
move
(data)))
130
{
131
assert(
static_cast<
int
>
(
m_chunk_vector
->size()) == width * height);
132
m_data
=
m_chunk_vector
;
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);
139
m_data
=
m_chunk_vector
;
140
}
141
142
public
:
143
template
<
typename
... Args>
144
static
std::shared_ptr<UniversalImageChunk<T>
>
create
(Args&&... args) {
145
return
std::shared_ptr<UniversalImageChunk<T>
>(
new
UniversalImageChunk<T>
(
std::forward<Args>
(args)...));
146
}
147
148
virtual
~UniversalImageChunk
() {
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
159
private
:
160
std::shared_ptr<std::vector<T>
>
m_chunk_vector
;
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_ */
Image.h
std::string
SourceXtractor::ImageChunk::getValue
T getValue(int x, int y) const
Returns the value of the pixel with the coordinates (x,y).
Definition
ImageChunk.h:58
SourceXtractor::ImageChunk::ImageChunk
ImageChunk(std::shared_ptr< const std::vector< T > > data, int offset, int width, int height, int stride)
Definition
ImageChunk.h:38
SourceXtractor::ImageChunk::getValue
T getValue(const PixelCoordinate &coord) const
Definition
ImageChunk.h:63
SourceXtractor::ImageChunk::create
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
SourceXtractor::ImageChunk::m_data
std::shared_ptr< const std::vector< T > > m_data
Definition
ImageChunk.h:83
SourceXtractor::ImageChunk::getChunk
std::shared_ptr< ImageChunk< T > > getChunk(int x, int y, int width, int height) const override
Definition
ImageChunk.h:78
SourceXtractor::ImageChunk::~ImageChunk
virtual ~ImageChunk()
Definition
ImageChunk.h:50
SourceXtractor::ImageChunk::m_height
int m_height
Definition
ImageChunk.h:85
SourceXtractor::ImageChunk::getWidth
int getWidth() const final
Returns the width of the image chunk in pixels.
Definition
ImageChunk.h:69
SourceXtractor::ImageChunk::getHeight
int getHeight() const final
Returns the height of the image chunk in pixels.
Definition
ImageChunk.h:74
SourceXtractor::ImageChunk::m_stride
int m_stride
Definition
ImageChunk.h:84
SourceXtractor::ImageChunk::getRepr
std::string getRepr() const override
Get a string identifying this image in a human readable manner.
Definition
ImageChunk.h:53
SourceXtractor::ImageChunk::m_width
int m_width
Definition
ImageChunk.h:85
SourceXtractor::ImageChunk::m_offset
int m_offset
Definition
ImageChunk.h:84
SourceXtractor::Image
Interface representing an image.
Definition
Image.h:44
SourceXtractor::UniversalImageChunk::create
static std::shared_ptr< UniversalImageChunk< T > > create(Args &&... args)
Definition
ImageChunk.h:144
SourceXtractor::UniversalImageChunk::~UniversalImageChunk
virtual ~UniversalImageChunk()
Definition
ImageChunk.h:148
SourceXtractor::UniversalImageChunk::setValue
void setValue(int x, int y, T value)
Definition
ImageChunk.h:151
SourceXtractor::UniversalImageChunk::UniversalImageChunk
UniversalImageChunk(std::vector< T > &&data, int width, int height)
Definition
ImageChunk.h:127
SourceXtractor::UniversalImageChunk::m_chunk_vector
std::shared_ptr< std::vector< T > > m_chunk_vector
Definition
ImageChunk.h:160
SourceXtractor::UniversalImageChunk::at
T & at(int x, int y)
Definition
ImageChunk.h:155
SourceXtractor::UniversalImageChunk::UniversalImageChunk
UniversalImageChunk(int width, int height)
Definition
ImageChunk.h:135
SourceXtractor::UniversalImageChunk::UniversalImageChunk
UniversalImageChunk(ImageChunk< T > &&chunk)
Definition
ImageChunk.h:97
SourceXtractor::UniversalImageChunk::UniversalImageChunk
UniversalImageChunk(const Image< T > *image, int x, int y, int width, int height)
Definition
ImageChunk.h:114
std::forward
T forward(T... args)
std::make_shared
T make_shared(T... args)
std::move
T move(T... args)
SourceXtractor
Definition
Aperture.h:30
std
STL namespace.
std::shared_ptr
SourceXtractor::PixelCoordinate
A pixel coordinate made of two integers m_x and m_y.
Definition
PixelCoordinate.h:37
SourceXtractor::PixelCoordinate::m_y
int m_y
Definition
PixelCoordinate.h:38
SourceXtractor::PixelCoordinate::m_x
int m_x
Definition
PixelCoordinate.h:38
std::to_string
T to_string(T... args)
std::vector
Generated by
1.15.0