forked from HumanBrainProject/nutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nutilapplication.cpp
364 lines (295 loc) · 11.5 KB
/
nutilapplication.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "nutilapplication.h"
#include <stdio.h>
#include <iostream>
#include <QImage>
#include <QFile>
#include <QImageReader>
#include <QImageWriter>
#include <QDebug>
#include <iostream>
#include <cstdlib>
#include "source/util/util.h"
#ifndef TARGET_OS_MAC
#include <omp.h>
#else
#include <omp.h>
//#include "/usr/local/opt/libomp/include/omp.h"
#endif
#include "source/util/counter.h"
#include "source/nauto.h"
//#include <tiffio.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <stdlib.h>
NutilApplication::NutilApplication(int argc, char *argv[])
{
m_argc = argc;
for (int i=0;i<argc;i++)
m_argv.append(QString(argv[i]));
}
bool Execute(QString fileName, int numThreads) {
try {
Data::data.isConsole = true;
NutilTemplate nt;
nt.Load(fileName);
Nauto nauto;
nauto.m_data = &nt;
nauto.m_numThreads = numThreads;
nauto.m_filename = fileName;
QElapsedTimer t;
t.start();
nauto.Execute();
// qDebug() << "Operation complete in "<<Util::;
}
catch (QString e) {
return false;
}
return true;
}
void NutilApplication::exec()
{
bool ok = false;
if (m_argc==1) {
PrintUsage();
exit(1);
}
if (m_argv[1].toLower() == "validate") {
/* if (m_argv.count()>=2) {
std::cout << "Nutil Self-Validtor requires a specifically set-up directory as parameter 2";
exit(1);
}*/
if (!Validator())
exit(1);
}
QString file = m_argv[1];
int numThreads = omp_get_max_threads();
if (m_argv.count()>2)
numThreads = m_argv[2].toInt();
ok = Execute(file, numThreads);
if (!ok) {
PrintUsage();
}
}
bool NutilApplication::RotateTiff()
{
/* QCoreApplication::addLibraryPath("imageformats");
if (m_argc!=11) {
return false;
}
QString inFile = m_argv[2];
QString outFile = m_argv[3];
QString compression = m_argv[4];
float angle = m_argv[5].toFloat();
float scale = m_argv[6].toFloat();
QColor background = QColor(m_argv[7].toFloat(), m_argv[8].toFloat(), m_argv[9].toFloat(),m_argv[10].toFloat());
// qDebug() << "Loading tiled tiff file:" << inFile;
LTiff tif;
if (!tif.Open(inFile)) {
qDebug() << "Could not open tiff file! Please check the filename and try again.";
exit(1);
}
tif.PrintImageInfo();
if (!TIFFIsTiled(tif.m_tif)) {
qDebug() << "Error: this tiff file is not tiled. Please use the other transformation method for non-tiled tiffs.";
exit(1);
}
if (!(tif.m_samplesPerPixel==3 && tif.m_bitsPerSample==8)) {
qDebug() << "Error! For now, lutil only supports big tiffs with RGB format";
exit(1);
}
tif.SetupBuffers();
int writeCompression = 0;
if (compression.toLower()=="jpeg")
writeCompression = 7;
if (compression.toLower()=="lzw")
writeCompression = 5;
if (compression.toLower()=="none")
writeCompression = 1;
if (writeCompression==0) {
qDebug() << "Unknown compression format. Must be either jpeg, lzw or none.";
exit(1);
}
LTiff otif;
otif.New(outFile);
otif.CreateFromMeta(tif, writeCompression, angle, background, true);
qDebug() << "Transforming to file " << outFile << " with compression " << tif.m_compressionTypes[writeCompression];
// otif.Transform(tif, angle, scale, 0,0, background, m_);
tif.Close();
otif.Close();
*/
return true;
}
bool NutilApplication::Transform()
{
/* QCoreApplication::addLibraryPath("imageformats");
if (m_argc!=10) {
return false;
}
QString inFile = m_argv[2];
QString outFile = m_argv[3];
float angle = m_argv[4].toFloat();
float scale = m_argv[5].toFloat();
QColor background = QColor(m_argv[6].toFloat(), m_argv[7].toFloat(), m_argv[8].toFloat(),m_argv[9].toFloat());
//std::cout << "infile: " << inFile.toStdString() << std::endl;
//std::cout << "angle: " << angle << std::endl;
// qDebug() << "LOADING " << inFile;
QImage image(inFile);
if (image.isNull()) {
qDebug() << "COULD NOT LOAD FILE";
exit(1);
}
QImage copy(image.width(), image.height(),image.format());
float centerx = (image.width()/2);
float centery = (image.height()/2);
//QRgb background = qRgb(0,0,255);
Q_TIMER_START();
Counter counter = Counter(image.width()*image.height(),"Rotating ", false);
#pragma omp parallel for
for (int i=0;i<image.width();i++) {
// std::cout<<"threads="<<omp_get_num_threads()<<std::endl;
for (int j=0;j<image.height();j++) {
float x = (i - centerx)*scale;
float y = (j - centery)*scale;
float xr = x*cos(angle)-y*sin(angle) + centerx;
float yr = y*cos(angle)+x*sin(angle) + centery;
QRgb color = background.rgba();
if (xr>=0 && xr<copy.width() && yr>=0 && yr<copy.height()) {
color = image.pixel((int)xr,(int)yr);
}
copy.setPixel(i,j,color);
counter.Tick();
}
}
std::cout << std::endl;
Q_TIMER_ELAPSED("Rotation");
//QFile file(outFile);
QImageWriter imageWriter( outFile );
// imageWriter.setOptimize( true );
// imageWriter.setProgressive( true );
if (outFile.contains("tif"))
imageWriter.setCompression(1);
qDebug() << "Saving...";
Q_TIMER_START();
if (imageWriter.write( copy )) {
qDebug() << "Image empty, cannot save ";
}
// imageWriter.close();
Q_TIMER_ELAPSED("Saving");
*/
return true;
}
void NutilApplication::PrintUsage()
{
std::cout << "Welcome to Nutil" << std::endl << std::endl;
std::cout << "Supported file formats: ";
qDebug() << QImageReader::supportedImageFormats();
std::cout << "Usage: "<< std::endl;
std::cout << " Nutil <file>" << std::endl;
std::cout << " " << std::endl;
// std::cout << " batch [ excel file ] [ int sheet no ]" << std::endl;
// std::cout << " transform [img input] [img output] [angle in rad] [scale] [bg col r] [bg col g] [bg col b] [bg col a]"<<std::endl;
// std::cout << " transform_tiff [img input] [img output] [compression = none, jpeg, lzw] [angle in rad] [scale] [bg col b] [bg col g] [bg col r] [bg col a]" << std::endl;
std::cout << " " << std::endl;
std::cout << "If anything crashes or acts naughy, don't hesitate contacting [email protected]" << std::endl;
std::cout << "" << std::endl;
std::cout << "" << std::endl;
}
bool NutilApplication::Batch()
{
if (m_argc!=4) {
return false;
}
QString inFile = m_argv[2];
int sheetIndex = m_argv[3].toInt();
// Nauto n(inFile, sheetIndex);
// n.Execute();
return true;
}
void NutilApplication::PrintFailure() {
/* std::cout<<" ▓█████ ██▓███ ██▓ ▄████▄ █████▒▄▄▄ ██▓ ██▓ "<<std::endl;
std::cout<<" ▓█ ▀ ▓██░ ██▒▓██▒▒██▀ ▀█ ▓██ ▒▒████▄ ▓██▒▓██▒ "<<std::endl;
std::cout<<" ▒███ ▓██░ ██▓▒▒██▒▒▓█ ▄ ▒████ ░▒██ ▀█▄ ▒██▒▒██░ "<<std::endl;
std::cout<<" ▒▓█ ▄ ▒██▄█▓▒ ▒░██░▒▓▓▄ ▄██▒ ░▓█▒ ░░██▄▄▄▄██ ░██░▒██░ "<<std::endl;
std::cout<<" ░▒████▒▒██▒ ░ ░░██░▒ ▓███▀ ░ ░▒█░ ▓█ ▓██▒░██░░██████▒"<<std::endl;
std::cout<<" ░░ ▒░ ░▒▓▒░ ░ ░░▓ ░ ░▒ ▒ ░ ▒ ░ ▒▒ ▓▒█░░▓ ░ ▒░▓ ░"<<std::endl;
std::cout<<" ░ ░ ░░▒ ░ ▒ ░ ░ ▒ ░ ▒ ▒▒ ░ ▒ ░░ ░ ▒ ░"<<std::endl;
std::cout<<" ░ ░░ ▒ ░░ ░ ░ ░ ▒ ▒ ░ ░ ░ "<<std::endl;
std::cout<<" ░ ░ ░ ░ ░ ░ ░ ░ ░ ░"<<std::endl;
std::cout << std::endl;*/
std::cout << "******* FAILURE!" <<std::endl;
std::cout << "Nutil self-validator failed. Please check the file comparisons for errors!"<<std::endl<<std::endl;;
}
void NutilApplication::PrintSuccess()
{
/* std::cout<<" .▄▄ · ▄• ▄▌ ▄▄· ▄▄· ▄▄▄ ..▄▄ · .▄▄ · ▄▄ "<<std::endl;
std::cout<<" ▐█ ▀. █▪██▌▐█ ▌▪▐█ ▌▪▀▄.▀·▐█ ▀. ▐█ ▀. ██▌"<<std::endl;
std::cout<<" ▄▀▀▀█▄█▌▐█▌██ ▄▄██ ▄▄▐▀▀▪▄▄▀▀▀█▄▄▀▀▀█▄▐█·"<<std::endl;
std::cout<<" ▐█▄▪▐█▐█▄█▌▐███▌▐███▌▐█▄▄▌▐█▄▪▐█▐█▄▪▐█.▀ "<<std::endl;
std::cout<<" ▀▀▀▀ ▀▀▀ ·▀▀▀ ·▀▀▀ ▀▀▀ ▀▀▀▀ ▀▀▀▀ ▀ "<<std::endl;*/
std::cout << "Success!" <<std::endl;
std::cout << std::endl;
}
bool NutilApplication::Validator()
{
std::cout << "Welcome to the Nutil Self-validator!" <<std::endl;
// First, got through quantifier
QDirIterator it(".", QDir::Dirs | QDir::NoDotAndDotDot);
while (it.hasNext()) {
Data::data.quiet = true;
QString dir = it.next();
qDebug() << "Executing : "<<dir;
if (!ValidateRun(dir,QStringList() << "png" << "tif"<<"tiff" << "csv" <<"html")) {
PrintFailure();
return false;
}
}
PrintSuccess();
return true;
}
bool NutilApplication::ValidateRun(QString dir, QStringList checkFiles)
{
Execute(dir+"/batch.nut",8);
for (QString s : checkFiles) {
qDebug().noquote() << "Validating results for : " << s;
if (!CompareFiles(dir+"/correct",s)) {
return false;
}
}
qDebug().noquote() << "All files from job '"+dir+"' are OK.";
return true;
}
bool NutilApplication::CompareFiles(QString directory, QString extension)
{
QStringList lst;
Util::findFilesInSubDirectories(&lst, directory, extension, true);
for (QString f : lst) {
QString comp = f;
comp = comp.replace("correct","out");
QString f1 = comp;
QString f2 = f;
if (!QFile::exists(comp)) {
std::cout << "************ ERROR : Could not find output file: "<<comp.toStdString() << std::endl;
return false;
}
bool ret = Util::CompareIdenticalFiles(comp,f);
QString r = ret?"true":"false";
// qDebug() << "Comparing '" << comp << "' and '"<<f<<"' : " <<r ;
// std::cout << r.toStdString() <<std::endl;
if (ret == false) {
std::cout << "Comparing '" << f1.remove(QDir().currentPath()).toStdString() << "' and '"<<f2.remove(QDir().currentPath()).toStdString()<<"' : ";
std::cout << "************ ERROR : Files are not identical! Aborting... " << std::endl;
/* if (comp.toLower().endsWith("csv")) {
std::cout << Util::loadTextFile(comp).toStdString();
std::cout << Util::loadTextFile(f).toStdString();
}*/
/* qDebug() << comp << f;
qDebug() << Util::loadTextFile(comp);
qDebug() << Util::loadTextFile(f);*/
return false;
}
}
return true;
}