-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlut.cpp
More file actions
87 lines (66 loc) · 1.98 KB
/
lut.cpp
File metadata and controls
87 lines (66 loc) · 1.98 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
#include "lut.h"
#include "trim.h"
#include <iostream>
#include <fstream>
void lut::read(const std::string& fileName)
{
std::ifstream fin(fileName);
if (!fin)
throw std::runtime_error("Couldn't open file: " + fileName);
std::string line;
while (std::getline(fin, line))
{
std::string trimmed = trim(line);
if (!trimmed.empty() && trimmed[0] != ';')
{
const size_t seperator = trimmed.find('|');
const float first = std::stof(trimmed.substr(0, seperator));
const float second = std::stof(trimmed.substr(seperator + 1));
m_entries.push_back(std::make_pair(first, second));
}
}
fin.close();
}
void lut::dump(std::ostream& stream) const
{
for (const auto& entry : m_entries)
{
stream << entry.first << " | " << entry.second << std::endl;
}
}
bool lut::dump(const std::string& fileName) const
{
std::ofstream of(fileName);
if (of)
{
dump(of);
of.close();
return true;
}
return false;
}
float lut::lookup(float first) const
{
if (m_entries.empty())
throw std::runtime_error("empty lut");
if (first < m_entries.front().first)
return m_entries.front().second;
if (first > m_entries.back().first)
return m_entries.back().second;
for (size_t i = 0; i < m_entries.size() - 1; i++)
{
if (first == m_entries[i].first)
return m_entries[i].second;
if (first >= m_entries[i].first && first <= m_entries[i + 1].first)
{
const float second_low = m_entries[i].second;
const float first_low = m_entries[i].first;
const float first_delta = m_entries[i + 1].first - first_low;
const float second_delta = m_entries[i + 1].second - second_low;
if (second_delta == 0)
return second_low;
return second_low + ((first - first_low) * second_delta) / first_delta;
}
}
return 0.0f;
}