Commit ad5a1813 for tesseract

commit ad5a1813d4f31e86337a27786d4f06dba51ea9e2
Author: Stefan Weil <sw@weilnetz.de>
Date:   Fri Nov 13 22:31:46 2026 +0100

    thresholder: convert images with 24 bits per pixel to 32 bits

    ImageThresholder::SetImage(Image) stored a Pix with packed 24 bits
    per pixel as it was, although most functions of Leptonica (for
    example pixRotateOrth) only accept RGB images with 32 bits. For a
    page with vertical text Tesseract::GetRectImage() then got a null
    pointer from the rotation, and ImageData::SetPixInternal() used an
    uninitialized pointer and size, which aborted the process.

    Convert such an image when it is set, like the SetImage() function
    for raw image data does. Also return a null pointer from
    GetRectImage() if there is no image, and leave the image data empty
    in SetPixInternal() if Leptonica cannot encode the image.

    Fix proposed by the Anthropic OSS Scanner (finding ANT-2026-ZGZ9JR5R).

    Assisted-by: OpenCode / Claude Fable 5.2 (Anthropic)
    Signed-off-by: Stefan Weil <sw@weilnetz.de>

diff --git a/src/ccmain/linerec.cpp b/src/ccmain/linerec.cpp
index 8153413f..947acf4b 100644
--- a/src/ccmain/linerec.cpp
+++ b/src/ccmain/linerec.cpp
@@ -212,6 +212,9 @@ ImageData *Tesseract::GetRectImage(const TBOX &box, const BLOCK &block, int padd
     box_pix.destroy();
     box_pix = grey;
   }
+  if (box_pix == nullptr) {
+    return nullptr;
+  }
   bool vertical_text = false;
   if (num_rotations > 0) {
     // Rotated the clipped revised box back to internal coordinates.
diff --git a/src/ccmain/thresholder.cpp b/src/ccmain/thresholder.cpp
index 200486c5..a4248dd7 100644
--- a/src/ccmain/thresholder.cpp
+++ b/src/ccmain/thresholder.cpp
@@ -172,6 +172,10 @@ void ImageThresholder::SetImage(const Image pix) {
   // not just a clone of the input.
   if (depth > 1 && depth < 8) {
     pix_ = pixConvertTo8(src, false);
+  } else if (depth == 24) {
+    // Packed 24 bpp is not plain RGB: most Leptonica functions
+    // (e.g. pixRotateOrth) only accept RGB with 32 bpp.
+    pix_ = pixConvert24To32(src);
   } else {
     pix_ = src.copy();
   }
diff --git a/src/ccstruct/imagedata.cpp b/src/ccstruct/imagedata.cpp
index ad82041c..af4abb89 100644
--- a/src/ccstruct/imagedata.cpp
+++ b/src/ccstruct/imagedata.cpp
@@ -332,14 +332,20 @@ void ImageData::AddBoxes(const std::vector<TBOX> &boxes,
 // In case of missing PNG support in Leptonica use PNM format,
 // which requires more memory.
 void ImageData::SetPixInternal(Image pix, std::vector<char> *image_data) {
-  l_uint8 *data;
-  size_t size;
+  l_uint8 *data = nullptr;
+  size_t size = 0;
   l_int32 ret;
   ret = pixWriteMem(&data, &size, pix, IFF_PNG);
   if (ret) {
     ret = pixWriteMem(&data, &size, pix, IFF_PNM);
   }
   pix.destroy();
+  if (ret) {
+    // Leptonica could not encode the image (e.g. pix is null):
+    // leave image_data empty instead of copying garbage.
+    image_data->clear();
+    return;
+  }
   // TODO: optimize resize (no init).
   image_data->resize(size);
   memcpy(&(*image_data)[0], data, size);