Skip to content

Commit 3852f4c

Browse files
committed
Optimize function ImageFind::FindImages
Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent 2b375fc commit 3852f4c

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/textord/imagefind.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,14 @@ static void ConnCompAndRectangularize(Image pix, DebugPixa *pixa_debug, Boxa **b
250250
// If not nullptr, it must be PixDestroyed by the caller.
251251
// If textord_tabfind_show_images, debug images are appended to pixa_debug.
252252
Image ImageFind::FindImages(Image pix, DebugPixa *pixa_debug) {
253+
auto width = pixGetWidth(pix);
254+
auto height = pixGetHeight(pix);
253255
// Not worth looking at small images.
254-
if (pixGetWidth(pix) < kMinImageFindSize || pixGetHeight(pix) < kMinImageFindSize) {
255-
return pixCreate(pixGetWidth(pix), pixGetHeight(pix), 1);
256+
// Leptonica will print an error message and return nullptr if we call
257+
// pixGenHalftoneMask(pixr, nullptr, ...) with width or height < 100
258+
// for the reduced image, so we want to bypass that, too.
259+
if (width / 2 < kMinImageFindSize || height / 2 < kMinImageFindSize) {
260+
return pixCreate(width, height, 1);
256261
}
257262

258263
// Reduce by factor 2.
@@ -262,15 +267,6 @@ Image ImageFind::FindImages(Image pix, DebugPixa *pixa_debug) {
262267
}
263268

264269
// Get the halftone mask directly from Leptonica.
265-
//
266-
// Leptonica will print an error message and return nullptr if we call
267-
// pixGenHalftoneMask(pixr, nullptr, ...) with too small image, so we
268-
// want to bypass that.
269-
if (pixGetWidth(pixr) < kMinImageFindSize || pixGetHeight(pixr) < kMinImageFindSize) {
270-
pixr.destroy();
271-
return pixCreate(pixGetWidth(pix), pixGetHeight(pix), 1);
272-
}
273-
// Get the halftone mask.
274270
l_int32 ht_found = 0;
275271
Pixa *pixadb = (textord_tabfind_show_images && pixa_debug != nullptr) ? pixaCreate(0) : nullptr;
276272
Image pixht2 = pixGenerateHalftoneMask(pixr, nullptr, &ht_found, pixadb);
@@ -287,7 +283,7 @@ Image ImageFind::FindImages(Image pix, DebugPixa *pixa_debug) {
287283
pixht2.destroy();
288284
}
289285
if (pixht2 == nullptr) {
290-
return pixCreate(pixGetWidth(pix), pixGetHeight(pix), 1);
286+
return pixCreate(width, height, 1);
291287
}
292288

293289
// Expand back up again.
@@ -334,7 +330,7 @@ Image ImageFind::FindImages(Image pix, DebugPixa *pixa_debug) {
334330
pixa_debug->AddPix(pixht, "FinalMask");
335331
}
336332
// Make the result image the same size as the input.
337-
Image result = pixCreate(pixGetWidth(pix), pixGetHeight(pix), 1);
333+
Image result = pixCreate(width, height, 1);
338334
result |= pixht;
339335
pixht.destroy();
340336
return result;

0 commit comments

Comments
 (0)