SourceXtractorPlusPlus
0.21
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
SEFramework
src
lib
Image
BufferedImage.cpp
Go to the documentation of this file.
1
18
#include "
SEFramework/Image/BufferedImage.h
"
19
#include "
SEFramework/Image/ImageSource.h
"
20
#include "
SEFramework/Image/ImageTile.h
"
21
#include "
SEFramework/Image/TileManager.h
"
22
23
namespace
SourceXtractor
{
24
25
template
<
typename
T>
26
BufferedImage<T>::BufferedImage
(
std::shared_ptr<const ImageSource>
source
,
27
std::shared_ptr<TileManager>
tile_manager
)
28
: m_source(
source
), m_tile_manager(
tile_manager
) {}
29
30
31
template
<
typename
T>
32
std::shared_ptr<BufferedImage<T>
>
BufferedImage<T>::create
(
std::shared_ptr<const ImageSource>
source
,
33
std::shared_ptr<TileManager>
tile_manager
) {
34
return
std::shared_ptr<BufferedImage<T>
>(
new
BufferedImage<T>
(
source
,
tile_manager
));
35
}
36
37
38
template
<
typename
T>
39
std::string
BufferedImage<T>::getRepr
()
const
{
40
return
"BufferedImage("
+ m_source->
getRepr
() +
")"
;
41
}
42
43
44
template
<
typename
T>
45
int
BufferedImage<T>::getWidth
()
const
{
46
return
m_source->
getWidth
();
47
}
48
49
50
template
<
typename
T>
51
int
BufferedImage<T>::getHeight
()
const
{
52
return
m_source->
getHeight
();
53
}
54
55
56
template
<
typename
T>
57
std::shared_ptr<ImageChunk<T>
>
BufferedImage<T>::getChunk
(
int
x
,
int
y
,
int
width,
int
height)
const
{
58
int
tile_width
= m_tile_manager->getTileWidth();
59
int
tile_height
= m_tile_manager->getTileHeight();
60
int
tile_offset_x
=
x
%
tile_width
;
61
int
tile_offset_y
=
y
%
tile_height
;
62
63
// When the chunk does *not* cross boundaries, we can just use the memory hold by the single tile
64
if
(
tile_offset_x
+ width <=
tile_width
&&
tile_offset_y
+ height <=
tile_height
) {
65
// the tile image is going to be kept in memory as long as the chunk exists, but it could be unloaded
66
// from TileManager and even reloaded again, wasting memory,
67
// however image chunks are normally short lived so it's probably OK
68
auto
tile
=
std::dynamic_pointer_cast<ImageTileWithType<T>
>(m_tile_manager->getTileForPixel(
x
,
y
, m_source));
69
assert
(
tile
!=
nullptr
);
70
71
// The tile may be smaller than tile_width x tile_height if the image is smaller, or does not divide neatly!
72
auto
image
=
tile
->getImage();
73
return
image
->getChunk(
tile_offset_x
,
tile_offset_y
, width, height);
74
}
75
else
{
76
// If the chunk cross boundaries, we can't just use the memory from within a tile, so we need to copy
77
// To avoid the overhead of calling getValue() - which uses a thread local - we do the full thing here
78
// Also, instead of iterating on the pixel coordinates, to avoid asking several times for the same tile,
79
// iterate over the tiles
80
std::vector<T>
data(width * height);
81
int
tile_w
= m_tile_manager->getTileWidth();
82
int
tile_h
= m_tile_manager->getTileHeight();
83
84
int
tile_start_x
=
x
/
tile_w
*
tile_w
;
85
int
tile_start_y
=
y
/
tile_h
*
tile_h
;
86
int
tile_end_x
= (
x
+ width - 1) /
tile_w
*
tile_w
;
87
int
tile_end_y
= (
y
+ height - 1) /
tile_h
*
tile_h
;
88
89
for
(
int
iy
=
tile_start_y
;
iy
<=
tile_end_y
;
iy
+=
tile_h
) {
90
for
(
int
ix
=
tile_start_x
;
ix
<=
tile_end_x
;
ix
+=
tile_w
) {
91
auto
tile
=
std::dynamic_pointer_cast<ImageTileWithType<T>
>(m_tile_manager->getTileForPixel(
ix
,
iy
, m_source));
92
copyOverlappingPixels(*
tile
, data,
x
,
y
, width, height,
tile_w
,
tile_h
);
93
}
94
}
95
96
return
UniversalImageChunk<T>::create
(
std::move
(data), width, height);
97
}
98
}
99
100
101
template
<
typename
T>
102
void
BufferedImage<T>::copyOverlappingPixels
(
const
ImageTileWithType<T>
&
tile
,
std::vector<T>
&
output
,
103
int
x
,
int
y
,
int
w
,
int
h
,
104
int
tile_w
,
int
tile_h
)
const
{
105
int
start_x
=
std::max
(
tile
.getPosX(),
x
);
106
int
start_y
=
std::max
(
tile
.getPosY(),
y
);
107
int
end_x
=
std::min
(
tile
.getPosX() +
tile_w
,
x
+
w
);
108
int
end_y
=
std::min
(
tile
.getPosY() +
tile_h
,
y
+
h
);
109
int
off_x
=
start_x
-
x
;
110
int
off_y
=
start_y
-
y
;
111
112
for
(
int
data_y
=
off_y
,
img_y
=
start_y
;
img_y
<
end_y
; ++
data_y
, ++
img_y
) {
113
for
(
int
data_x
=
off_x
,
img_x
=
start_x
;
img_x
<
end_x
; ++
data_x
, ++
img_x
) {
114
tile
.getValue(
img_x
,
img_y
,
output
[
data_x
+
data_y
*
w
]);
115
}
116
}
117
}
118
119
120
template
class
BufferedImage<MeasurementImage::PixelType>
;
121
template
class
BufferedImage<FlagImage::PixelType>
;
122
template
class
BufferedImage<unsigned int>
;
123
template
class
BufferedImage<int>
;
124
template
class
BufferedImage<double>
;
125
126
127
}
// end namespace SourceXtractor
BufferedImage.h
ImageSource.h
ImageTile.h
x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
Definition
MoffatModelFittingTask.cpp:94
y
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
Definition
MoffatModelFittingTask.cpp:94
TileManager.h
std::string
SourceXtractor::BufferedImage
Definition
BufferedImage.h:39
SourceXtractor::BufferedImage::create
static std::shared_ptr< BufferedImage< T > > create(std::shared_ptr< const ImageSource > source, std::shared_ptr< TileManager > tile_manager=TileManager::getInstance())
Definition
BufferedImage.cpp:32
SourceXtractor::BufferedImage::getRepr
std::string getRepr() const override
Get a string identifying this image in a human readable manner.
Definition
BufferedImage.cpp:39
SourceXtractor::BufferedImage::getWidth
int getWidth() const override
Returns the width of the image in pixels.
Definition
BufferedImage.cpp:45
SourceXtractor::BufferedImage::getHeight
int getHeight() const override
Returns the height of the image in pixels.
Definition
BufferedImage.cpp:51
SourceXtractor::BufferedImage::BufferedImage
BufferedImage(std::shared_ptr< const ImageSource > source, std::shared_ptr< TileManager > tile_manager)
Definition
BufferedImage.cpp:26
SourceXtractor::UniversalImageChunk
Definition
ImageChunk.h:89
std::function
std::max
T max(T... args)
std::min
T min(T... args)
std::move
T move(T... args)
SourceXtractor
Definition
Aperture.h:30
Generated by
1.10.0