-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathimmdebug_internal.cpp
More file actions
229 lines (195 loc) · 7.74 KB
/
Copy pathimmdebug_internal.cpp
File metadata and controls
229 lines (195 loc) · 7.74 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "immdebug/immdebug_internal.h"
#include <ios>
#include <fstream>
#include <filesystem>
#include <random>
#include <iostream>
namespace ImmVision
{
namespace ImmDebug_Internal
{
template<typename T>
void WriteValue(std::ostream &os, T v)
{
os.write((char *) &v, sizeof(T));
}
template<typename T>
T ReadValue(std::istream &is)
{
T v;
is.read((char *) &v, sizeof(T));
return v;
}
void WriteImageBuffer(std::ostream &os, const ImageBuffer &img)
{
WriteValue<int>(os, img.width);
WriteValue<int>(os, img.height);
WriteValue<int>(os, img.channels);
WriteValue<int>(os, static_cast<int>(img.depth));
WriteValue<size_t>(os, img.step);
// Write pixel data row by row (source may have non-contiguous stride)
size_t row_bytes = (size_t)img.width * img.channels * ImageDepthSize(img.depth);
for (int y = 0; y < img.height; y++)
{
const uint8_t* row = static_cast<const uint8_t*>(img.data) + y * img.step;
os.write(reinterpret_cast<const char*>(row), (std::streamsize)row_bytes);
}
}
ImageBuffer ReadImageBuffer(std::istream &is)
{
int width = ReadValue<int>(is);
int height = ReadValue<int>(is);
int channels = ReadValue<int>(is);
ImageDepth depth = static_cast<ImageDepth>(ReadValue<int>(is));
size_t saved_step = ReadValue<size_t>(is);
(void)saved_step;
ImageBuffer img = ImageBuffer::Zeros(width, height, channels, depth);
size_t row_bytes = (size_t)width * channels * ImageDepthSize(depth);
for (int y = 0; y < height; y++)
{
uint8_t* row = static_cast<uint8_t*>(img.data) + y * img.step;
is.read(reinterpret_cast<char*>(row), (std::streamsize)row_bytes);
}
return img;
}
void WriteString(std::ostream &os, const std::string &v)
{
WriteValue(os, v.size());
os.write(v.data(), std::streamsize(sizeof(char) * v.size()));
}
std::string ReadString(std::istream &is)
{
std::size_t len = ReadValue<std::size_t>(is);
char *buffer = new char[len + 1];
is.read(buffer, std::streamsize(len));
buffer[len] = '\0';
std::string s(buffer);
delete[]buffer;
return s;
}
void WriteImagePayload(std::ostream &os, const ImagePayload &v)
{
WriteString(os, v.Legend);
WriteValue<Point2d>(os, v.ZoomCenter);
WriteValue<double>(os, v.ZoomRatio);
WriteString(os, v.ZoomKey);
WriteString(os, v.ColorAdjustmentsKey);
WriteValue<bool>(os, v.isColorOrderBGR);
WriteImageBuffer(os, v.Image);
}
ImagePayload ReadImagePayload(std::istream &is)
{
ImagePayload r;
r.Legend = ReadString(is);
r.ZoomCenter = ReadValue<Point2d>(is);
r.ZoomRatio = ReadValue<double>(is);
r.ZoomKey = ReadString(is);
r.ColorAdjustmentsKey = ReadString(is);
r.isColorOrderBGR = ReadValue<bool>(is);
r.Image = ReadImageBuffer(is);
return r;
}
std::string ImmDebugFolder(bool whileWriting)
{
static auto temp_path = std::filesystem::temp_directory_path();
std::string folder;
if (whileWriting)
folder = temp_path.string() + "/ImmDebug/Writing";
else
folder = temp_path.string() + "/ImmDebug";
if (! std::filesystem::is_directory(folder))
std::filesystem::create_directories(folder);
return folder;
}
std::string MakeUniqueFilename(bool whileWriting)
{
static long long id = []() {
std::default_random_engine generator;
generator.seed(static_cast<unsigned int>(std::chrono::system_clock::now().time_since_epoch().count()));
std::uniform_int_distribution<long long> distribution(100000000000, 1000000000000);
long long id_ = distribution(generator);
return id_;
}();
id++;
return ImmDebugFolder(whileWriting) + "/" + std::to_string(id) + ".bindata";
}
bool RemoveOneFile(const std::filesystem::path& path)
{
std::error_code ec;
bool successRemove = std::filesystem::remove(path, ec);
if (!successRemove)
{
std::cerr << "Error while removing " << path.string() << std::endl;
std::cerr << ec.message() << std::endl;
return false;
}
return true;
}
void RemoveOldImages(double maxAge_Seconds = 3600)
{
namespace fs = std::filesystem;
auto now = fs::file_time_type::clock::now();
std::string folder = ImmDebugFolder(false);
for(const fs::directory_entry& entry : fs::recursive_directory_iterator(folder))
{
try
{
const auto& path = entry.path();
if (! fs::is_regular_file(path))
continue;
std::chrono::duration<double> fileAge_DurationSeconds = now - fs::last_write_time(path);
double fileAge_Seconds = fileAge_DurationSeconds.count();
if (fileAge_Seconds > maxAge_Seconds)
RemoveOneFile(path);
}
catch (fs::filesystem_error & e)
{
std::cerr << e.what();
}
}
}
void SaveImagePayload(const ImagePayload & imagePayload)
{
RemoveOldImages();
std::string filename_whileWriting = MakeUniqueFilename(true);
std::ofstream ofs(filename_whileWriting, std::ios::binary);
WriteImagePayload(ofs, imagePayload);
ofs.close();
std::string filename_Done = MakeUniqueFilename(false);
rename(filename_whileWriting.c_str(), filename_Done.c_str());
}
std::optional<ImagePayload> ReadImagePayload()
{
RemoveOldImages();
std::optional<ImagePayload> imagePayload;
std::string folder = ImmDebugFolder(false);
namespace fs = std::filesystem;
std::vector<fs::path> imagePayloads;
for(const fs::directory_entry& entry : fs::directory_iterator(folder))
{
const auto& path = entry.path();
if (! fs::is_regular_file(path))
continue;
imagePayloads.push_back(path);
}
if (!imagePayloads.empty())
{
if (imagePayloads.size() > 1)
{
std::sort(imagePayloads.begin(), imagePayloads.end(),
[](const fs::path& f1, const fs::path& f2) {
return last_write_time(f1) < last_write_time(f2);
}
);
}
auto oldestPayload = imagePayloads.front();
std::ifstream is(oldestPayload, std::ios::binary);
imagePayload = ReadImagePayload(is);
is.close();
RemoveOneFile(oldestPayload);
return imagePayload;
}
return std::nullopt;
}
} // namespace ImmDebug_Internal
} // namespace ImmVision