Fixed DetectOS so it doesn't crash with a big image

This commit is contained in:
Ray Smith 2017-05-03 15:50:31 -07:00
parent 926a066d77
commit 6ac31dcbdd
4 changed files with 25 additions and 18 deletions

View File

@ -590,10 +590,11 @@ void TessBaseAPI::SetRectangle(int left, int top, int width, int height) {
* Get a copy of the internal thresholded image from Tesseract.
*/
Pix* TessBaseAPI::GetThresholdedImage() {
if (tesseract_ == NULL || thresholder_ == NULL)
return NULL;
if (tesseract_->pix_binary() == NULL)
Threshold(tesseract_->mutable_pix_binary());
if (tesseract_ == nullptr || thresholder_ == nullptr) return nullptr;
if (tesseract_->pix_binary() == nullptr &&
!Threshold(tesseract_->mutable_pix_binary())) {
return nullptr;
}
return pixClone(tesseract_->pix_binary());
}
@ -2189,7 +2190,7 @@ bool TessBaseAPI::InternalSetImage() {
* to an existing pixDestroyable Pix.
* The usual argument to Threshold is Tesseract::mutable_pix_binary().
*/
void TessBaseAPI::Threshold(Pix** pix) {
bool TessBaseAPI::Threshold(Pix** pix) {
ASSERT_HOST(pix != NULL);
if (*pix != NULL)
pixDestroy(pix);
@ -2205,7 +2206,7 @@ void TessBaseAPI::Threshold(Pix** pix) {
PageSegMode pageseg_mode =
static_cast<PageSegMode>(
static_cast<int>(tesseract_->tessedit_pageseg_mode));
thresholder_->ThresholdToPix(pageseg_mode, pix);
if (!thresholder_->ThresholdToPix(pageseg_mode, pix)) return false;
thresholder_->GetImageSizes(&rect_left_, &rect_top_,
&rect_width_, &rect_height_,
&image_width_, &image_height_);
@ -2229,6 +2230,7 @@ void TessBaseAPI::Threshold(Pix** pix) {
}
tesseract_->set_source_resolution(estimated_res);
SavePixForCrash(estimated_res, *pix);
return true;
}
/** Find lines from the image making the BLOCK_LIST. */
@ -2246,12 +2248,8 @@ int TessBaseAPI::FindLines() {
tesseract_ = new Tesseract;
tesseract_->InitAdaptiveClassifier(nullptr);
}
if (tesseract_->pix_binary() == NULL)
Threshold(tesseract_->mutable_pix_binary());
if (tesseract_->ImageWidth() > MAX_INT16 ||
tesseract_->ImageHeight() > MAX_INT16) {
tprintf("Image too large: (%d, %d)\n",
tesseract_->ImageWidth(), tesseract_->ImageHeight());
if (tesseract_->pix_binary() == NULL &&
!Threshold(tesseract_->mutable_pix_binary())) {
return -1;
}
@ -2359,11 +2357,13 @@ bool TessBaseAPI::DetectOS(OSResults* osr) {
if (tesseract_ == NULL)
return false;
ClearResults();
if (tesseract_->pix_binary() == NULL)
Threshold(tesseract_->mutable_pix_binary());
if (tesseract_->pix_binary() == NULL &&
!Threshold(tesseract_->mutable_pix_binary())) {
return false;
}
if (input_file_ == NULL)
input_file_ = new STRING(kInputFile);
return orientation_and_script_detection(*input_file_, osr, tesseract_);
return orientation_and_script_detection(*input_file_, osr, tesseract_) > 0;
}
void TessBaseAPI::set_min_orientation_margin(double margin) {

View File

@ -798,7 +798,7 @@ class TESS_API TessBaseAPI {
* Run the thresholder to make the thresholded image. If pix is not NULL,
* the source is thresholded to pix instead of the internal IMAGE.
*/
TESS_LOCAL virtual void Threshold(Pix** pix);
TESS_LOCAL virtual bool Threshold(Pix** pix);
/**
* Find lines from the image making the BLOCK_LIST.

View File

@ -179,7 +179,12 @@ void ImageThresholder::SetImage(const Pix* pix) {
// Threshold the source image as efficiently as possible to the output Pix.
// Creates a Pix and sets pix to point to the resulting pointer.
// Caller must use pixDestroy to free the created Pix.
void ImageThresholder::ThresholdToPix(PageSegMode pageseg_mode, Pix** pix) {
/// Returns false on error.
bool ImageThresholder::ThresholdToPix(PageSegMode pageseg_mode, Pix** pix) {
if (image_width_ > MAX_INT16 || image_height_ > MAX_INT16) {
tprintf("Image too large: (%d, %d)\n", image_width_, image_height_);
return false;
}
if (pix_channels_ == 0) {
// We have a binary image, but it still has to be copied, as this API
// allows the caller to modify the output.
@ -189,6 +194,7 @@ void ImageThresholder::ThresholdToPix(PageSegMode pageseg_mode, Pix** pix) {
} else {
OtsuThresholdRectToPix(pix_, pix);
}
return true;
}
// Gets a pix that contains an 8 bit threshold value at each pixel. The

View File

@ -117,7 +117,8 @@ class TESS_API ImageThresholder {
/// Threshold the source image as efficiently as possible to the output Pix.
/// Creates a Pix and sets pix to point to the resulting pointer.
/// Caller must use pixDestroy to free the created Pix.
virtual void ThresholdToPix(PageSegMode pageseg_mode, Pix** pix);
/// Returns false on error.
virtual bool ThresholdToPix(PageSegMode pageseg_mode, Pix** pix);
// Gets a pix that contains an 8 bit threshold value at each pixel. The
// returned pix may be an integer reduction of the binary image such that