-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradient.cpp
More file actions
78 lines (61 loc) · 1.3 KB
/
gradient.cpp
File metadata and controls
78 lines (61 loc) · 1.3 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
#include "pch.h"
#include "gradient.h"
namespace ocr
{
gradient::gradient()
{}
gradient::gradient(std::string path)
{
if (!load(path))
journal("Gradient construction failed", log_error);
}
bool gradient::load(std::string path)
{
ifstream in(path.c_str());
if (!in.is_open())
{
journal("Unable to load gradient from " + path, log_error);
return false;
}
string buf;
size_t it = 0;
file_read(in, buf);
height = splstr<size_t>(buf, it);
width = splstr<size_t>(buf, it);
heatmap.resize(height, vector<float>(width));
for (vector<float>& row : heatmap)
{
file_read(in, buf);
it = 0;
for (float& cell : row)
cell = splstr<float>(buf, it, '\t');
}
in.close();
return true;
}
bool gradient::save(std::string path)
{
ofstream out(path.c_str());
if (!out.is_open())
{
journal("Unable to save gradient to " + path, log_error);
return false;
}
out << height << " " << width << "\n";
for (vector<float>& row : heatmap)
{
for (float& cell : row)
out << cell << " ";
out << "\n";
}
out.close();
return true;
}
float gradient::at(float y, float x)
{
// 1.0 causes overflow as it would be in the next interval
int iy = (int)(y * height) - (y == 1.0);
int ix = (int)(x * width) - (x == 1.0);
return heatmap[iy][ix];
}
}