Skip to content

Commit 6ac31dc

Browse files
committed
Fixed DetectOS so it doesn't crash with a big image
1 parent 926a066 commit 6ac31dc

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

api/baseapi.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,11 @@ void TessBaseAPI::SetRectangle(int left, int top, int width, int height) {
590590
* Get a copy of the internal thresholded image from Tesseract.
591591
*/
592592
Pix* TessBaseAPI::GetThresholdedImage() {
593-
if (tesseract_ == NULL || thresholder_ == NULL)
594-
return NULL;
595-
if (tesseract_->pix_binary() == NULL)
596-
Threshold(tesseract_->mutable_pix_binary());
593+
if (tesseract_ == nullptr || thresholder_ == nullptr) return nullptr;
594+
if (tesseract_->pix_binary() == nullptr &&
595+
!Threshold(tesseract_->mutable_pix_binary())) {
596+
return nullptr;
597+
}
597598
return pixClone(tesseract_->pix_binary());
598599
}
599600

@@ -2189,7 +2190,7 @@ bool TessBaseAPI::InternalSetImage() {
21892190
* to an existing pixDestroyable Pix.
21902191
* The usual argument to Threshold is Tesseract::mutable_pix_binary().
21912192
*/
2192-
void TessBaseAPI::Threshold(Pix** pix) {
2193+
bool TessBaseAPI::Threshold(Pix** pix) {
21932194
ASSERT_HOST(pix != NULL);
21942195
if (*pix != NULL)
21952196
pixDestroy(pix);
@@ -2205,7 +2206,7 @@ void TessBaseAPI::Threshold(Pix** pix) {
22052206
PageSegMode pageseg_mode =
22062207
static_cast<PageSegMode>(
22072208
static_cast<int>(tesseract_->tessedit_pageseg_mode));
2208-
thresholder_->ThresholdToPix(pageseg_mode, pix);
2209+
if (!thresholder_->ThresholdToPix(pageseg_mode, pix)) return false;
22092210
thresholder_->GetImageSizes(&rect_left_, &rect_top_,
22102211
&rect_width_, &rect_height_,
22112212
&image_width_, &image_height_);
@@ -2229,6 +2230,7 @@ void TessBaseAPI::Threshold(Pix** pix) {
22292230
}
22302231
tesseract_->set_source_resolution(estimated_res);
22312232
SavePixForCrash(estimated_res, *pix);
2233+
return true;
22322234
}
22332235

22342236
/** Find lines from the image making the BLOCK_LIST. */
@@ -2246,12 +2248,8 @@ int TessBaseAPI::FindLines() {
22462248
tesseract_ = new Tesseract;
22472249
tesseract_->InitAdaptiveClassifier(nullptr);
22482250
}
2249-
if (tesseract_->pix_binary() == NULL)
2250-
Threshold(tesseract_->mutable_pix_binary());
2251-
if (tesseract_->ImageWidth() > MAX_INT16 ||
2252-
tesseract_->ImageHeight() > MAX_INT16) {
2253-
tprintf("Image too large: (%d, %d)\n",
2254-
tesseract_->ImageWidth(), tesseract_->ImageHeight());
2251+
if (tesseract_->pix_binary() == NULL &&
2252+
!Threshold(tesseract_->mutable_pix_binary())) {
22552253
return -1;
22562254
}
22572255

@@ -2359,11 +2357,13 @@ bool TessBaseAPI::DetectOS(OSResults* osr) {
23592357
if (tesseract_ == NULL)
23602358
return false;
23612359
ClearResults();
2362-
if (tesseract_->pix_binary() == NULL)
2363-
Threshold(tesseract_->mutable_pix_binary());
2360+
if (tesseract_->pix_binary() == NULL &&
2361+
!Threshold(tesseract_->mutable_pix_binary())) {
2362+
return false;
2363+
}
23642364
if (input_file_ == NULL)
23652365
input_file_ = new STRING(kInputFile);
2366-
return orientation_and_script_detection(*input_file_, osr, tesseract_);
2366+
return orientation_and_script_detection(*input_file_, osr, tesseract_) > 0;
23672367
}
23682368

23692369
void TessBaseAPI::set_min_orientation_margin(double margin) {

api/baseapi.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ class TESS_API TessBaseAPI {
798798
* Run the thresholder to make the thresholded image. If pix is not NULL,
799799
* the source is thresholded to pix instead of the internal IMAGE.
800800
*/
801-
TESS_LOCAL virtual void Threshold(Pix** pix);
801+
TESS_LOCAL virtual bool Threshold(Pix** pix);
802802

803803
/**
804804
* Find lines from the image making the BLOCK_LIST.

ccmain/thresholder.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,12 @@ void ImageThresholder::SetImage(const Pix* pix) {
179179
// Threshold the source image as efficiently as possible to the output Pix.
180180
// Creates a Pix and sets pix to point to the resulting pointer.
181181
// Caller must use pixDestroy to free the created Pix.
182-
void ImageThresholder::ThresholdToPix(PageSegMode pageseg_mode, Pix** pix) {
182+
/// Returns false on error.
183+
bool ImageThresholder::ThresholdToPix(PageSegMode pageseg_mode, Pix** pix) {
184+
if (image_width_ > MAX_INT16 || image_height_ > MAX_INT16) {
185+
tprintf("Image too large: (%d, %d)\n", image_width_, image_height_);
186+
return false;
187+
}
183188
if (pix_channels_ == 0) {
184189
// We have a binary image, but it still has to be copied, as this API
185190
// allows the caller to modify the output.
@@ -189,6 +194,7 @@ void ImageThresholder::ThresholdToPix(PageSegMode pageseg_mode, Pix** pix) {
189194
} else {
190195
OtsuThresholdRectToPix(pix_, pix);
191196
}
197+
return true;
192198
}
193199

194200
// Gets a pix that contains an 8 bit threshold value at each pixel. The

ccmain/thresholder.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ class TESS_API ImageThresholder {
117117
/// Threshold the source image as efficiently as possible to the output Pix.
118118
/// Creates a Pix and sets pix to point to the resulting pointer.
119119
/// Caller must use pixDestroy to free the created Pix.
120-
virtual void ThresholdToPix(PageSegMode pageseg_mode, Pix** pix);
120+
/// Returns false on error.
121+
virtual bool ThresholdToPix(PageSegMode pageseg_mode, Pix** pix);
121122

122123
// Gets a pix that contains an 8 bit threshold value at each pixel. The
123124
// returned pix may be an integer reduction of the binary image such that

0 commit comments

Comments
 (0)