runner.cc 6.22 KB
Newer Older
1
2
// Copyright (C) 2010, 2011 EPITA Research and Development Laboratory
// (LRDE)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena 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 Olena.  If not, see <http://www.gnu.org/licenses/>.

#include "runner.hh"

#include <mln/core/image/image2d.hh>
#include <mln/value/rgb8.hh>
#include <mln/io/magick/load.hh>

24
25
#include "demodir.hh"

26
27
28
29
30
31
#include "process.hh"
#include "preprocess.hh"
#include "config.hh"
#include "defs.hh"


32

33
34
35
using namespace mln;
using namespace scribo::toolchain::internal;

36
37
38
39
static
QString get_pathto(const QString& file,
		   const QString localdirsuffix = QString())
{
40
  // Not installed ?
41
42
43
44
  QFile f(SCRIBO_LOCAL_DEMODIR "/" + localdirsuffix + "/" + file);
  if (f.exists())
    return SCRIBO_LOCAL_DEMODIR "/" + localdirsuffix;

45
  // Installed in a specific path ?
46
  f.setFileName(SCRIBO_PREFIX_LIBEXECDIR "/" + file);
47
  if (f.exists())
48
    return SCRIBO_PREFIX_LIBEXECDIR;
49

50
51
52
53
54
55
56
  // Installed in a specific path but moved elsewhere ?
  QDir dir(QCoreApplication::applicationDirPath());
  dir.cdUp(); // move from prefix/bin to prefix/
  f.setFileName(dir.currentPath() + "/libexec/scribo" + file);
  if (f.exists())
    return dir.currentPath() + "/libexec/scribo";

57
58
59
60
61
62
  qDebug() << "FATAL ERROR: Can't locate file: " + file;

  return "";
}


63
64
65
66
67
68
69
runner::runner(QObject *parent)
  : QThread(parent)
{
  moveToThread(this);
}


70
void runner::run()
71
72
{

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  switch(mode_)
  {
    default:
    case Demat:
    {
      image2d<value::rgb8> ima;
      io::magick::load(ima, args_.at(0).toUtf8().constData());

      image2d<bool> bin_ima = preprocess(ima);
      process(ima, bin_ima);
    }
    break;

    case Export:
      export_as();
    break;
  }

  emit finished();
  qDebug() << "Done.";
93
94
95
}


96
void runner::stop()
97
{
98
99
  terminate();
}
100
101
102



103
104
105
106
// Demat related stuff

void runner::start_demat(const QString& filename)
{
107
  args_.clear();
108
109
110
111
  args_ << filename;
  mode_ = Demat;

  QThread::start();
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
}


image2d<bool>
runner::preprocess(const image2d<value::rgb8>& ima)
{
  emit new_step("Preprocessing");

  ::preprocess f;

  // Load config.
  config * const conf = config::get_instance();

  connect(&f, SIGNAL(progress()), this, SIGNAL(progress()));
  connect(&f, SIGNAL(new_progress_label(const QString&)),
	  this, SIGNAL(new_progress_label(const QString&)));

  f.enable_subsample = conf->preprocessing_subsample();
  f.enable_fg_extraction = conf->preprocessing_remove_bg();
  f.enable_deskew = conf->preprocessing_deskew();
  f.enable_denoising = conf->preprocessing_remove_noise();

  f.binarization_algo = static_cast<Binarization_Algo>(conf->preprocessing_bin_algo());

  emit new_progress_max_value(f.nsteps());

  // Perform preprocessing.
  f(ima);

  qDebug() << "Preprocess Done.";
  return f.output;
}


void runner::process(const image2d<value::rgb8>& original_ima,
		     const image2d<bool>& processed_ima)
{
  emit new_step("Page segmentation");

151
  ::process f(args_.at(0).toUtf8().constData());
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

  connect(&f, SIGNAL(progress()), this, SIGNAL(progress()));
  connect(&f, SIGNAL(new_progress_label(const QString&)),
	  this, SIGNAL(new_progress_label(const QString&)));
  connect(&f, SIGNAL(xml_saved(const QString&)),
	  this, SIGNAL(xml_saved(const QString&)));

  // Load config.
  config * const conf = config::get_instance();

  defs::FindSeparators
    find_seps = static_cast<defs::FindSeparators>(conf->segmentation_find_seps());
  f.enable_line_seps = (find_seps == defs::Lines
			|| find_seps == defs::LinesAndWhitespaces);
  f.enable_whitespace_seps = (find_seps == defs::Whitespaces
			      || find_seps == defs::LinesAndWhitespaces);
168
169
  f.enable_ocr = conf->ocr_enabled();
  f.ocr_language = conf->ocr_language().toAscii().data();
170
  f.xml_format = scribo::io::xml::PageExtended;
171
172
173


  f.save_doc_as_xml = true;
174
  QFileInfo file(args_.at(0));
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
  QString output_dir = QDir::tempPath();
  if (conf->general_save_xml_enabled())
  {
    if (conf->general_save_xml_same_dir())
      output_dir = file.absolutePath();
    else if (conf->general_save_xml_custom_dir())
      output_dir = conf->general_save_xml_custom_dir_path();
    else
      qDebug() << "runner::progress - Invalid xml saving option!";

    QDir dir(output_dir);
    if (!dir.exists() && !dir.mkpath(output_dir))
      output_dir = QDir::tempPath();
  }
  f.output_file = (output_dir  + "/" + file.baseName() + "_gui.xml").toUtf8().constData();

  emit new_progress_max_value(f.nsteps());

  // Perform text detection.
  f(original_ima, processed_ima);

  qDebug() << "Process Done.";
}


200
201
202
203
204

// Export related stuff

void runner::start_export(const QString& imgfile,
			  const QString& xmlfile, const QString& outfile)
205
{
206
  args_.clear();
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
  args_ << imgfile << xmlfile << outfile;
  mode_ = Export;

  QThread::start();
}


void runner::export_as()
{
  emit new_step("Exporting document...");
  emit new_progress_max_value(2);

  // Checking output format
  QFileInfo f(args_.at(2));

  QString pathto_xml2doc = get_pathto("scribo-xml2doc", "xml2doc");

  if (pathto_xml2doc.isEmpty())
  {
    QMessageBox::critical(0, "Fatal error", "Cannot export! Cannot find scribo-xml2doc program!");
    return;
  }

  emit progress();

  int rvalue = 0;
  if (f.suffix() == "pdf")
234
235
  {
    emit new_step("Exporting as PDF...");
236
237
238
    rvalue = system(QString("%1/scribo-xml2doc --pdf %2 %3 %4")
		    .arg(pathto_xml2doc).arg(args_.at(1)).arg(args_.at(0))
		    .arg(args_.at(2)).toAscii().constData());
239
  }
240
  else if (f.suffix() == "html" || f.suffix() == "htm")
241
242
  {
    emit new_step("Exporting as HTML...");
243
244
245
    rvalue = system(QString("%1/scribo-xml2doc --html %2 %3 %4")
		    .arg(pathto_xml2doc).arg(args_.at(1)).arg(args_.at(0))
		    .arg(args_.at(2)).toAscii().constData());
246
  }
247
248
249
250
251
  else
    QMessageBox::critical(0, "Fatal error", "Cannot export! Invalid output format!");

  if (rvalue != 0)
    QMessageBox::critical(0, "Fatal error", "Cannot export! Return value is not 0!");
252
}