RAII: TessBaseAPI::AnalyseLayout()

Co-authored-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Raf Schietekat 2017-05-14 04:17:34 +02:00 committed by Stefan Weil
parent 897e59613d
commit d71413f4aa
2 changed files with 6 additions and 14 deletions

View File

@ -701,9 +701,9 @@ Boxa *TessBaseAPI::GetConnectedComponents(Pixa **pixa) {
Boxa *TessBaseAPI::GetComponentImages(PageIteratorLevel level, bool text_only, bool raw_image,
const int raw_padding, Pixa **pixa, int **blockids,
int **paraids) {
PageIterator *page_it = GetIterator();
/*non-const*/ std::unique_ptr</*non-const*/ PageIterator> page_it(GetIterator());
if (page_it == nullptr) {
page_it = AnalyseLayout();
page_it.reset(AnalyseLayout());
}
if (page_it == nullptr) {
return nullptr; // Failed.
@ -783,7 +783,6 @@ Boxa *TessBaseAPI::GetComponentImages(PageIteratorLevel level, bool text_only, b
++component_index;
}
} while (page_it->Next(level));
delete page_it;
return boxa;
}
@ -1236,12 +1235,8 @@ bool TessBaseAPI::ProcessPage(Pix *pix, int page_index, const char *filename,
if (tesseract_->tessedit_pageseg_mode == PSM_AUTO_ONLY) {
// Disabled character recognition
PageIterator *it = AnalyseLayout();
if (it == nullptr) {
if (! std::unique_ptr<const PageIterator>(AnalyseLayout())) {
failed = true;
} else {
delete it;
}
} else if (tesseract_->tessedit_pageseg_mode == PSM_OSD_ONLY) {
failed = FindLines() != 0;
@ -1991,7 +1986,7 @@ bool TessBaseAPI::IsValidCharacter(const char *utf8_character) {
// TODO(rays) Obsolete this function and replace with a more aptly named
// function that returns image coordinates rather than tesseract coordinates.
bool TessBaseAPI::GetTextDirection(int *out_offset, float *out_slope) {
PageIterator *it = AnalyseLayout();
const std::unique_ptr<const PageIterator> it(AnalyseLayout());
if (it == nullptr) {
return false;
}
@ -2008,7 +2003,6 @@ bool TessBaseAPI::GetTextDirection(int *out_offset, float *out_slope) {
// textline's bounding box.
int left, top, right, bottom;
if (!it->BoundingBox(RIL_TEXTLINE, &left, &top, &right, &bottom)) {
delete it;
return false;
}
int left_y = IntCastRounded(*out_slope * left + *out_offset);
@ -2021,7 +2015,6 @@ bool TessBaseAPI::GetTextDirection(int *out_offset, float *out_slope) {
// the slope and height - offset for the offset.
*out_slope = -*out_slope;
*out_offset = rect_height_ - *out_offset;
delete it;
return true;
}

View File

@ -27,6 +27,7 @@
#endif
#include <cstdlib> // for std::getenv
#include <iostream>
#include <memory> // std::unique_ptr
#include <allheaders.h>
#include <tesseract/baseapi.h>
@ -706,7 +707,7 @@ int main(int argc, char **argv) {
tesseract::TextlineOrder order;
float deskew_angle;
const tesseract::PageIterator *it = api.AnalyseLayout();
const std::unique_ptr<const tesseract::PageIterator> it(api.AnalyseLayout());
if (it) {
// TODO: Implement output of page segmentation, see documentation
// ("Automatic page segmentation, but no OSD, or OCR").
@ -719,8 +720,6 @@ int main(int argc, char **argv) {
ret_val = EXIT_FAILURE;
}
delete it;
pixDestroy(&pixs);
return ret_val;
}