hvstack.hpp
3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// // Copyright 2021 Olzhas Zhumabek <anonymous.from.applecity@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#include "boost/gil/image_view_factory.hpp"
#include <boost/gil/image.hpp>
#include <boost/gil/image_view.hpp>
#include <cmath>
#include <numeric>
#include <stdexcept>
#include <vector>
namespace boost { namespace gil {
template <typename View>
void hstack(const std::vector<View>& views, const View& output_view)
{
if (views.size() == 0)
{
throw std::invalid_argument("empty views vector is passed - cannot create stacked image");
}
auto height = views.front().height();
for (const auto& view : views)
{
if (view.height() != height)
{
throw std::invalid_argument("one or many views are not of the same height");
}
}
std::ptrdiff_t full_width =
std::accumulate(views.begin(), views.end(), 0,
[](std::ptrdiff_t old, const View& view) { return old + view.width(); });
if (output_view.width() != full_width || output_view.height() != height)
{
throw std::invalid_argument("the destination view is not of the right dimensions");
}
std::ptrdiff_t current_x = 0;
for (std::size_t i = 0; i < views.size(); ++i)
{
auto subview =
subimage_view(output_view, current_x, 0, views[i].width(), views[i].height());
copy_pixels(views[i], subview);
current_x += views[i].width();
}
}
template <typename View>
image<typename View::value_type> hstack(const std::vector<View>& views)
{
if (views.size() == 0)
{
throw std::invalid_argument("empty views vector is passed - cannot create stacked image");
}
std::ptrdiff_t full_width =
std::accumulate(views.begin(), views.end(), 0,
[](std::ptrdiff_t old, const View& view) { return old + view.width(); });
std::ptrdiff_t height = views.front().height();
image<typename View::value_type> result_image(full_width, height);
hstack(views, view(result_image));
return result_image;
}
template <typename View>
void vstack(const std::vector<View>& views, const View& output_view)
{
if (views.size() == 0)
{
throw std::invalid_argument("empty views vector is passed - cannot create stacked image");
}
auto full_height =
std::accumulate(views.begin(), views.end(), 0,
[](std::ptrdiff_t old, const View& view) { return old + view.height(); });
std::ptrdiff_t width = views.front().height();
for (const auto& view : views)
{
if (view.width() != width)
{
throw std::invalid_argument("one or many views are not of the same width");
}
}
if (output_view != full_height || output_view.width() != width)
{
throw std::invalid_argument("the destination view is not of the right dimensions");
}
std::ptrdiff_t current_y = 0;
for (std::size_t i = 0; i < views.size(); ++i)
{
auto subview =
subimage_view(output_view, 0, current_y, views[i].width(), views[i].height());
copy_pixels(views[i], subview);
current_y += views[i].height();
}
}
template <typename View>
image<typename View::value_type> vstack(const std::vector<View>& views)
{
if (views.size() == 0)
{
throw std::invalid_argument("empty views vector is passed - cannot create stacked image");
}
auto full_height =
std::accumulate(views.begin(), views.end(), 0,
[](std::ptrdiff_t old, const View& view) { return old + view.height(); });
std::ptrdiff_t width = views.front().height();
image<typename View::value_type> result_image(width, full_height);
hstack(views, view(result_image));
return result_image;
}
}} // namespace boost::gil