forked from knossos-project/knossos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_io.cpp
263 lines (243 loc) · 11.8 KB
/
file_io.cpp
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* This file is a part of KNOSSOS.
*
* (C) Copyright 2007-2016
* Max-Planck-Gesellschaft zur Foerderung der Wissenschaften e.V.
*
* KNOSSOS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 of
* the License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* For further information, visit https://knossostool.org
* or contact knossos-team@mpimf-heidelberg.mpg.de
*/
#include "file_io.h"
#include <iostream>
#include "loader.h"
#include "segmentation/segmentation.h"
#include "skeleton/skeletonizer.h"
#include "viewer.h"
#include <quazipfile.h>
#include <QDir>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QRegularExpression>
#include <QStandardPaths>
#include <ctime>
QString annotationFileDefaultName() {
// Generate a default file name based on date and time.
auto currentTime = time(nullptr);
auto localTime = localtime(¤tTime);
return QString("annotation-%1%2%3T%4%5.000.k.zip")
.arg(1900 + localTime->tm_year)
//value, right aligned padded to width 2, base 10, filled with '0'
.arg(localTime->tm_mon + 1, 2, 10, QLatin1Char('0'))
.arg(localTime->tm_mday, 2, 10, QLatin1Char('0'))
.arg(localTime->tm_hour, 2, 10, QLatin1Char('0'))
.arg(localTime->tm_min, 2, 10, QLatin1Char('0'));
}
QString annotationFileDefaultPath() {
return QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/annotationFiles/" + annotationFileDefaultName();
}
void annotationFileLoad(const QString & filename, const QString & treeCmtOnMultiLoad) {
QRegularExpression cubeRegEx(R"regex(.*mag(?P<mag>[0-9]*)x(?P<x>[0-9]*)y(?P<y>[0-9]*)z(?P<z>[0-9]*)((\.seg\.sz)|(\.segmentation\.snappy)))regex");
QuaZip archive(filename);
if (archive.open(QuaZip::mdUnzip)) {
for (auto valid = archive.goToFirstFile(); valid; valid = archive.goToNextFile()) {
QuaZipFile file(&archive);
const auto & fileInside = archive.getCurrentFileName();
const auto match = cubeRegEx.match(fileInside);
if (match.hasMatch()) {
if (!Segmentation::enabled) {
Segmentation::enabled = true;
Loader::Controller::singleton().enableOverlay();
}
file.open(QIODevice::ReadOnly);
const auto cubeCoord = CoordOfCube(match.captured("x").toInt(), match.captured("y").toInt(), match.captured("z").toInt());
Loader::Controller::singleton().snappyCacheSupplySnappy(cubeCoord, match.captured("mag").toInt(), file.readAll().toStdString());
}
}
if (archive.setCurrentFile("microworker.txt")) {
QuaZipFile file(&archive);
Segmentation::singleton().jobLoad(file);
}
if (archive.setCurrentFile("mergelist.txt")) {
QuaZipFile file(&archive);
Segmentation::singleton().mergelistLoad(file);
}
//load skeleton after mergelist as it may depend on a loaded segmentation
std::unordered_map<decltype(treeListElement::treeID), std::reference_wrapper<treeListElement>> treeMap;
if (archive.setCurrentFile("annotation.xml")) {
QuaZipFile file(&archive);
treeMap = state->viewer->skeletonizer->loadXmlSkeleton(file, treeCmtOnMultiLoad);
}
if (archive.setCurrentFile("mergelist.txt")) {
// watkinspv - created a "load all" to avoid race conditions
Segmentation::singleton().loadMeshes();
}
//rutuja -original code commented by rutuja as we do not needto generate and depend on ply files
/*for (auto valid = archive.goToFirstFile(); valid; valid = archive.goToNextFile()) { // after annotation.xml, because loading .xml clears skeleton
const QRegularExpression meshRegEx(R"regex([0-9]*.ply)regex");
auto fileName = archive.getCurrentFileName();
const auto matchMesh = meshRegEx.match(fileName);
if (matchMesh .hasMatch()) {
QuaZipFile file(&archive);
auto nameWithoutExtension = fileName;
nameWithoutExtension.chop(4);
bool validId = false;
auto treeId = boost::make_optional<std::uint64_t>(nameWithoutExtension.toULongLong(&validId));
if (!validId) {
qDebug() << "Filename not of the form <tree id>.ply, so loading as new tree:" << fileName;
treeId = boost::none;
std::cout << "j" << std::endl;
} else if (state->skeletonState->mergeOnLoadFlag) {
const auto iter = treeMap.find(treeId.get());
if (iter != std::end(treeMap)) {
treeId = iter->second.get().treeID;
std::cout << "k" << std::endl;
} else {
qDebug() << "Tree not found for this mesh, loading as new tree:" << fileName;
treeId = boost::none;
std::cout << "l" << std::endl;
}
}
// Skeletonizer::singleton().loadMesh(file, treeId, fileName);
}
}*/
state->viewer->loader_notify();
} else {
throw std::runtime_error(QObject::tr("opening %1 for reading failed").arg(filename).toStdString());
}
}
void annotationFileSave(const QString & filename) {
QTime time;
time.start();
QuaZip archive_write(filename);
if (archive_write.open(QuaZip::mdCreate)) {
auto zipCreateFile = [](QuaZipFile & file_write, const QString & name, const int level){
auto fileinfo = QuaZipNewInfo(name);
//without permissions set, some archive utilities will not grant any on extract
fileinfo.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ReadGroup | QFileDevice::ReadOther);
return file_write.open(QIODevice::WriteOnly, fileinfo, nullptr, 0, Z_DEFLATED, level);
};
QuaZipFile file_write(&archive_write);
if (zipCreateFile(file_write, "annotation.xml", 1)) {
state->viewer->skeletonizer->saveXmlSkeleton(file_write);
} else {
throw std::runtime_error((filename + ": saving skeleton failed").toStdString());
}
if (Segmentation::singleton().hasObjects()) {
QuaZipFile file_write(&archive_write);
if (zipCreateFile(file_write, "mergelist.txt", 1)) {
Segmentation::singleton().mergelistSave(file_write);
} else {
throw std::runtime_error((filename + ": saving mergelist failed").toStdString());
}
}
if (Session::singleton().annotationMode.testFlag(AnnotationMode::Mode_MergeSimple)) {
QuaZipFile file_write(&archive_write);
if (zipCreateFile(file_write, "microworker.txt", 1)) {
Segmentation::singleton().jobSave(file_write);
} else {
throw std::runtime_error((filename + ": saving segmentation job failed").toStdString());
}
}
//rutuja -commented
/* for (const auto & tree : state->skeletonState->trees) {
if (tree.mesh != nullptr) {
QuaZipFile file_write(&archive_write);
const auto filename = QString::number(tree.treeID) + ".ply";
if (zipCreateFile(file_write, filename, 1)) {
Skeletonizer::singleton().saveMesh(file_write, tree);
} else {
throw std::runtime_error((filename + ": saving mesh failed").toStdString());
}
}
}*/
QTime cubeTime;
cubeTime.start();
const auto & cubes = Loader::Controller::singleton().getAllModifiedCubes();
for (std::size_t i = 0; i < cubes.size(); ++i) {
const auto magName = QString("%1_mag%2x%3y%4z%5.seg.sz").arg(state->name).arg(QString::number(std::pow(2, i)));
for (const auto & pair : cubes[i]) {
QuaZipFile file_write(&archive_write);
const auto cubeCoord = pair.first;
const auto name = magName.arg(cubeCoord.x).arg(cubeCoord.y).arg(cubeCoord.z);
if (zipCreateFile(file_write, name, 1)) {
file_write.write(pair.second.c_str(), pair.second.length());
} else {
throw std::runtime_error((filename + ": saving snappy cube failed").toStdString());
}
}
}
qDebug() << "save cubes" << cubeTime.restart();
} else {
throw std::runtime_error(QObject::tr("opening %1 for writing failed").arg(filename).toStdString());
}
Session::singleton().unsavedChanges = false;
qDebug() << "save" << time.restart();
}
void nmlExport(const QString & filename) {
QFile file(filename);
file.open(QIODevice::WriteOnly);
state->viewer->skeletonizer->saveXmlSkeleton(file);
file.close();
}
/** This is a replacement for the old updateFileName
It decides if a skeleton file has a revision(case 1) or not(case2).
if case1 the revision substring is extracted, incremented and will be replaced.
if case2 an initial revision will be inserted.
This method is actually only needed for the save or save as slots, if incrementFileName is selected
*/
QString updatedFileName(QString fileName) {
QRegExp versionRegEx = QRegExp("(\\.)([0-9]{3})(\\.)");
if (versionRegEx.indexIn(fileName) != -1) {
const auto versionIndex = versionRegEx.pos(2);//get second regex aka version without file extension
const auto incrementedVersion = fileName.midRef(versionIndex, 3).toInt() + 1;//3 chars following the dot
return fileName.replace(versionIndex, 3, QString("%1").arg(incrementedVersion, 3, 10, QChar('0')));//pad with zeroes
} else {
QFileInfo info(fileName);
return info.dir().absolutePath() + "/" + info.baseName() + ".001." + info.completeSuffix();
}
}
std::vector<std::tuple<uint8_t, uint8_t, uint8_t>> loadLookupTable(const QString & path) {
auto kill = [&path](){
const auto msg = QObject::tr("Failed to open LUT file: »%1«").arg(path);
qWarning() << msg;
throw std::runtime_error(msg.toUtf8());
};
const std::size_t expectedBinaryLutSize = 256 * 3;//RGB
std::vector<std::tuple<uint8_t, uint8_t, uint8_t>> table;
QFile overlayLutFile(path);
if (overlayLutFile.open(QIODevice::ReadOnly)) {
if (overlayLutFile.size() == expectedBinaryLutSize) {//imageJ binary LUT
const auto buffer = overlayLutFile.readAll();
table.resize(256);
for (int i = 0; i < 256; ++i) {
table[i] = std::make_tuple(static_cast<uint8_t>(buffer[i]), static_cast<uint8_t>(buffer[256 + i]), static_cast<uint8_t>(buffer[512 + i]));
}
} else {//json
QJsonDocument json_conf = QJsonDocument::fromJson(overlayLutFile.readAll());
auto jarray = json_conf.array();
if (!json_conf.isArray() || jarray.size() != 256) {//dataset adjustment currently requires 256 values
kill();
}
table.resize(jarray.size());
for (int i = 0; i < jarray.size(); ++i) {
table[i] = std::make_tuple(jarray[i].toArray()[0].toInt(), jarray[i].toArray()[1].toInt(), (jarray[i].toArray()[2].toInt()));
}
}
} else {
kill();
}
return table;
}