-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimage.cc
48 lines (43 loc) · 983 Bytes
/
image.cc
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
#include "image.hh"
#include "stb_image_resize.inl"
#include <utility> // for std::swap
namespace
{
auto clerp(float s, float smin, float smax, float dmin, float dmax)
{
if (s < smin) return dmin;
if (s > smax) return dmax;
return (s - smin) / (smax - smin) * (dmax - dmin) + dmin;
}
}
namespace tue
{
void image::padding(int n, int s, int w, int e)
{
auto nw = w_ + w + e;
auto nh = h_ + n + s;
image tmp{nw, nh};
each([&](auto& v, auto x, auto y) {
auto nx = w + x;
auto ny = s + y;
if (nx < 0) return;
if (ny < 0) return;
if (nx >= nw) return;
if (ny >= nh) return;
tmp.at(nx, ny) = v;
});
std::swap(*this, tmp);
}
void image::scale_to(int nw, int nh)
{
image tmp{nw, nh};
stbir_resize_float(data(), w_, h_, 0, tmp.data(), nw, nh, 0, 1);
std::swap(*this, tmp);
}
void image::clerp(float smin, float smax, float dmin, float dmax)
{
each([&](auto& v, auto, auto) {
v = ::clerp(v, smin, smax, dmin, dmax);
});
}
}