Changeset View
Changeset View
Standalone View
Standalone View
plugins/ocr/ocrad/ocrocradengine.cpp
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | |||||
53 | 53 | | |||
54 | 54 | | |||
55 | static const char UndetectedChar = '_'; | 55 | static const char UndetectedChar = '_'; | ||
56 | 56 | | |||
57 | 57 | | |||
58 | OcrOcradEngine::OcrOcradEngine(QObject *pnt, const QVariantList &args) | 58 | OcrOcradEngine::OcrOcradEngine(QObject *pnt, const QVariantList &args) | ||
59 | : AbstractOcrEngine(pnt, "OcrOcradEngine") | 59 | : AbstractOcrEngine(pnt, "OcrOcradEngine") | ||
60 | { | 60 | { | ||
61 | m_ocrImagePBM = QString::null; | 61 | m_ocrImagePBM = QString(); | ||
62 | m_tempOrfName = QString::null; | 62 | m_tempOrfName = QString(); | ||
63 | ocradVersion = 0; | 63 | ocradVersion = 0; | ||
64 | } | 64 | } | ||
65 | 65 | | |||
66 | 66 | | |||
67 | AbstractOcrDialogue *OcrOcradEngine::createOcrDialogue(AbstractOcrEngine *plugin, QWidget *pnt) | 67 | AbstractOcrDialogue *OcrOcradEngine::createOcrDialogue(AbstractOcrEngine *plugin, QWidget *pnt) | ||
68 | { | 68 | { | ||
69 | return (new OcrOcradDialog(plugin, pnt)); | 69 | return (new OcrOcradDialog(plugin, pnt)); | ||
70 | } | 70 | } | ||
▲ Show 20 Lines • Show All 198 Lines • ▼ Show 20 Line(s) | 239 | { | |||
269 | int lineNo = 0; | 269 | int lineNo = 0; | ||
270 | int blockCnt = 0; | 270 | int blockCnt = 0; | ||
271 | QString line; | 271 | QString line; | ||
272 | QRect blockRect; | 272 | QRect blockRect; | ||
273 | 273 | | |||
274 | startResultDocument(); | 274 | startResultDocument(); | ||
275 | 275 | | |||
276 | while (!stream.atEnd()) { | 276 | while (!stream.atEnd()) { | ||
277 | line = stream.readLine().trimmed(); // line of text excluding '\n' | 277 | line = stream.readLine().trimmed(); // line of text excluding '\n' | ||
278 | 278 | | |||
279 | if (line.startsWith("#")) { | 279 | if (line.startsWith("#")) { | ||
280 | continue; // ignore comments | 280 | continue; // ignore comments | ||
281 | } | 281 | } | ||
282 | 282 | | |||
283 | if (verboseDebug()) { | 283 | if (verboseDebug()) { | ||
284 | //qDebug() << "# Line" << line; | 284 | //qDebug() << "# Line" << line; | ||
285 | } | 285 | } | ||
286 | if (line.startsWith("source file ")) { | 286 | if (line.startsWith("source file ")) { | ||
287 | continue; // source file name, ignore | 287 | continue; // source file name, ignore | ||
288 | } else if (line.startsWith("total blocks ")) { // total count of blocks, | 288 | } else if (line.startsWith("total blocks ")) { // total count of blocks, | ||
289 | // must be first line | 289 | // must be first line | ||
290 | blockCnt = line.mid(13).toInt(); | 290 | blockCnt = line.mid(13).toInt(); | ||
291 | qDebug() << "Block count (V<10)" << blockCnt; | 291 | qDebug() << "Block count (V<10)" << blockCnt; | ||
292 | } else if (line.startsWith("total text blocks ")) { | 292 | } else if (line.startsWith("total text blocks ")) { | ||
293 | blockCnt = line.mid(18).toInt(); | 293 | blockCnt = line.mid(18).toInt(); | ||
294 | qDebug() << "Block count (V>10)" << blockCnt; | 294 | qDebug() << "Block count (V>10)" << blockCnt; | ||
295 | } else if (line.startsWith("block ") || line.startsWith("text block ")) { | 295 | } else if (line.startsWith("block ") || line.startsWith("text block ")) { | ||
296 | // start of text block | 296 | // start of text block | ||
297 | // matching "block 1 0 0 560 792" | 297 | // matching "block 1 0 0 560 792" | ||
298 | if (rx1.indexIn(line) == -1) { | 298 | if (rx1.indexIn(line) == -1) { | ||
299 | //qDebug() << "Failed to match 'block' line" << line; | 299 | //qDebug() << "Failed to match 'block' line" << line; | ||
300 | continue; | 300 | continue; | ||
301 | } | 301 | } | ||
302 | 302 | | |||
303 | int currBlock = (rx1.cap(1).toInt()) - 1; | 303 | int currBlock = (rx1.cap(1).toInt()) - 1; | ||
304 | blockRect.setRect(rx1.cap(2).toInt(), rx1.cap(3).toInt(), | 304 | blockRect.setRect(rx1.cap(2).toInt(), rx1.cap(3).toInt(), | ||
305 | rx1.cap(4).toInt(), rx1.cap(5).toInt()); | 305 | rx1.cap(4).toInt(), rx1.cap(5).toInt()); | ||
306 | //qDebug() << "Current block" << currBlock << "rect" << blockRect; | 306 | //qDebug() << "Current block" << currBlock << "rect" << blockRect; | ||
307 | } else if (line.startsWith("lines ")) { // lines in this block | 307 | } else if (line.startsWith("lines ")) { // lines in this block | ||
308 | //qDebug() << "Block line count" << line.mid(6).toInt(); | 308 | //qDebug() << "Block line count" << line.mid(6).toInt(); | ||
309 | } else if (line.startsWith("line ")) { // start of text line | 309 | } else if (line.startsWith("line ")) { // start of text line | ||
310 | startLine(); | 310 | startLine(); | ||
311 | 311 | | |||
312 | if (rx2.indexIn(line) == -1) { | 312 | if (rx2.indexIn(line) == -1) { | ||
313 | //qDebug() << "Failed to match 'line' line" << line; | 313 | //qDebug() << "Failed to match 'line' line" << line; | ||
314 | continue; | 314 | continue; | ||
315 | } | 315 | } | ||
316 | 316 | | |||
317 | int charCount = rx2.cap(2).toInt(); | 317 | int charCount = rx2.cap(2).toInt(); | ||
Show All 22 Lines | 325 | for (int c = 0; c < charCount && !stream.atEnd(); ++c) { | |||
340 | 340 | | |||
341 | // find how many alternatives, matching " 1, 'r'0" | 341 | // find how many alternatives, matching " 1, 'r'0" | ||
342 | if (rx3.indexIn(resultStr) == -1) { | 342 | if (rx3.indexIn(resultStr) == -1) { | ||
343 | //qDebug() << "Failed to match" << resultStr << "in 'char' line" << charLine; | 343 | //qDebug() << "Failed to match" << resultStr << "in 'char' line" << charLine; | ||
344 | continue; | 344 | continue; | ||
345 | } | 345 | } | ||
346 | 346 | | |||
347 | int altCount = rx3.cap(1).toInt(); | 347 | int altCount = rx3.cap(1).toInt(); | ||
348 | if (altCount == 0) { // no alternatives, | 348 | if (altCount == 0) { // no alternatives, | ||
349 | // undecipherable character | 349 | // undecipherable character | ||
350 | if (verboseDebug()) { | 350 | if (verboseDebug()) { | ||
351 | //qDebug() << "Undecipherable character in 'char' line" << charLine; | 351 | //qDebug() << "Undecipherable character in 'char' line" << charLine; | ||
352 | } | 352 | } | ||
353 | } else { | 353 | } else { | ||
354 | int h = resultStr.indexOf(','); | 354 | int h = resultStr.indexOf(','); | ||
355 | if (h == -1) { | 355 | if (h == -1) { | ||
356 | //qDebug() << "No ',' in" << resultStr << "in 'char' line" << charLine; | 356 | //qDebug() << "No ',' in" << resultStr << "in 'char' line" << charLine; | ||
Show All 12 Lines | 365 | if (detectedChar != ' ') { | |||
369 | } | 369 | } | ||
370 | 370 | | |||
371 | QRect r(rx4.cap(1).toInt(), rx4.cap(2).toInt(), | 371 | QRect r(rx4.cap(1).toInt(), rx4.cap(2).toInt(), | ||
372 | rx4.cap(3).toInt(), rx4.cap(4).toInt()); | 372 | rx4.cap(3).toInt(), rx4.cap(4).toInt()); | ||
373 | wordRect |= r; | 373 | wordRect |= r; | ||
374 | } | 374 | } | ||
375 | } | 375 | } | ||
376 | 376 | | |||
377 | if (detectedChar == ' ') { // space terminates the word | 377 | if (detectedChar == ' ') { // space terminates the word | ||
378 | if (ocradVersion < 10) { // offset is relative to block | 378 | if (ocradVersion < 10) { // offset is relative to block | ||
379 | wordRect.translate(blockRect.x(), blockRect.y()); | 379 | wordRect.translate(blockRect.x(), blockRect.y()); | ||
380 | } | 380 | } | ||
381 | 381 | | |||
382 | OcrWordData wd; | 382 | OcrWordData wd; | ||
383 | wd.setProperty(OcrWordData::Rectangle, wordRect); | 383 | wd.setProperty(OcrWordData::Rectangle, wordRect); | ||
384 | addWord(word, wd); | 384 | addWord(word, wd); | ||
385 | 385 | | |||
386 | word = QString::null; // reset for next time | 386 | word = QString(); // reset for next time | ||
387 | wordRect = QRect(); | 387 | wordRect = QRect(); | ||
388 | } else { | 388 | } else { | ||
389 | word.append(detectedChar); // append char to word | 389 | word.append(detectedChar); // append char to word | ||
390 | } | 390 | } | ||
391 | } // end of text line loop | 391 | } // end of text line loop | ||
392 | ++lineNo; | 392 | ++lineNo; | ||
393 | 393 | | |||
394 | if (!word.isEmpty()) { // last word in line | 394 | if (!word.isEmpty()) { // last word in line | ||
395 | if (ocradVersion < 10) { // offset is relative to block | 395 | if (ocradVersion < 10) { // offset is relative to block | ||
396 | wordRect.translate(blockRect.x(), blockRect.y()); | 396 | wordRect.translate(blockRect.x(), blockRect.y()); | ||
397 | } | 397 | } | ||
398 | 398 | | |||
399 | OcrWordData wd; | 399 | OcrWordData wd; | ||
400 | wd.setProperty(OcrWordData::Rectangle, wordRect); | 400 | wd.setProperty(OcrWordData::Rectangle, wordRect); | ||
401 | addWord(word, wd); | 401 | addWord(word, wd); | ||
402 | 402 | | |||
403 | word = QString::null; // reset for next time | 403 | word = QString(); // reset for next time | ||
404 | wordRect = QRect(); | 404 | wordRect = QRect(); | ||
405 | } | 405 | } | ||
406 | 406 | | |||
407 | finishLine(); | 407 | finishLine(); | ||
408 | } else { | 408 | } else { | ||
409 | //qDebug() << "Unknown line format" << line; | 409 | //qDebug() << "Unknown line format" << line; | ||
410 | } | 410 | } | ||
411 | } | 411 | } | ||
412 | 412 | | |||
413 | file.close(); // finished with ORF file | 413 | file.close(); // finished with ORF file | ||
414 | finishResultDocument(); | 414 | finishResultDocument(); | ||
415 | qDebug() << "Finished analysing ORF"; | 415 | qDebug() << "Finished analysing ORF"; | ||
416 | 416 | | |||
417 | return (QString::null); // no error detected | 417 | return (QString()); // no error detected | ||
418 | } | 418 | } | ||
419 | 419 | | |||
420 | 420 | | |||
421 | void OcrOcradEngine::openAdvancedSettings() | 421 | void OcrOcradEngine::openAdvancedSettings() | ||
422 | { | 422 | { | ||
423 | ExecutablePathDialogue d(nullptr); | 423 | ExecutablePathDialogue d(nullptr); | ||
424 | 424 | | |||
425 | QString exec = KookaSettings::ocrOcradBinary(); | 425 | QString exec = KookaSettings::ocrOcradBinary(); | ||
Show All 13 Lines |