Commit 11404e79 for tesseract

commit 11404e7930fbc75ccabb64270dd9856198821f49
Author: Eyüp Can Akman <eyupcanakman@gmail.com>
Date:   Wed Mar 18 18:39:45 2026 +0300

    Fix inconsistent recognition_done_ state on empty pages

    Recognize() returned early for a page without blocks and left
    recognition_done_ false, so GetUTF8Text() and the other functions
    which check that flag ran Recognize() again. Set the flag before
    the check for an empty page.

    Fixes #4112

diff --git a/src/api/baseapi.cpp b/src/api/baseapi.cpp
index 772d02f5..5673f513 100644
--- a/src/api/baseapi.cpp
+++ b/src/api/baseapi.cpp
@@ -783,13 +783,13 @@ int TessBaseAPI::Recognize(ETEXT_DESC *monitor) {
     return -1;
   }
   delete page_res_;
+  recognition_done_ = true;
   if (block_list_->empty()) {
     page_res_ = new PAGE_RES(false, block_list_, &tesseract_->prev_word_best_choice_);
     return 0; // Empty page.
   }

   tesseract_->SetBlackAndWhitelist();
-  recognition_done_ = true;
 #ifndef DISABLED_LEGACY_ENGINE
   if (tesseract_->tessedit_resegment_from_line_boxes) {
     page_res_ = tesseract_->ApplyBoxes(input_file_.c_str(), true, block_list_);
diff --git a/unittest/baseapi_test.cc b/unittest/baseapi_test.cc
index d70e4a24..34079bff 100644
--- a/unittest/baseapi_test.cc
+++ b/unittest/baseapi_test.cc
@@ -55,6 +55,9 @@ public:
   Tesseract *osd_tesseract() const {
     return osd_tesseract_;
   }
+  bool recognition_done() const {
+    return recognition_done_;
+  }
 };

 std::string GetCleanedTextResult(tesseract::TessBaseAPI *tess, Image pix) {
@@ -246,6 +249,39 @@ TEST_F(TesseractTest, HOCRContainsBaseline) {
   src_pix.destroy();
 }

+// Tests that all output formats return valid results on an empty page.
+// Regression test for https://github.com/tesseract-ocr/tesseract/issues/4112
+TEST_F(TesseractTest, EmptyPageOutputConsistency) {
+  FriendlyTessBaseAPI api;
+  REQUIRE_TRAINEDDATA(TESSDATA_DIR, "eng");
+  if (api.Init(TessdataPath().c_str(), "eng", tesseract::OEM_LSTM_ONLY) == -1) {
+    // eng.traineddata not found.
+    GTEST_SKIP();
+  }
+  // Create a blank white image (no text to detect).
+  Image blank_pix = pixCreate(200, 200, 8);
+  CHECK(blank_pix);
+  pixSetAll(blank_pix);
+  api.SetImage(blank_pix);
+  EXPECT_EQ(api.Recognize(nullptr), 0);
+  EXPECT_TRUE(api.recognition_done());
+
+  // All output formats should return non-null, even on an empty page.
+  char *hocr = api.GetHOCRText(0);
+  EXPECT_TRUE(hocr != nullptr);
+  delete[] hocr;
+
+  char *utf8 = api.GetUTF8Text();
+  EXPECT_TRUE(utf8 != nullptr);
+  delete[] utf8;
+
+  char *tsv = api.GetTSVText(0);
+  EXPECT_TRUE(tsv != nullptr);
+  delete[] tsv;
+
+  blank_pix.destroy();
+}
+
 // Tests that Tesseract gets exactly the right answer on some page numbers.
 TEST_F(TesseractTest, AdaptToWordStrTest) {
 #ifdef DISABLED_LEGACY_ENGINE