Skip to content

Commit ae7de90

Browse files
committed
default-resolution: Add clamping. Update documentation. Add test
1 parent b1065c3 commit ae7de90

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

TesseractOCR/G8Tesseract.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@
9292

9393
/**
9494
* The resolution of the source image in pixels per inch so font size
95-
* information can be calculated in results.
95+
* information can be calculated in results. It should be from 70 to
96+
* 2400 or it will be clamped.
9697
*
9798
* @default Default value is 72
9899
*/

TesseractOCR/G8Tesseract.mm

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#import "genericvector.h"
2323
#import "strngs.h"
2424

25-
static int const kG8DefaultResolution = 72;
25+
static NSInteger const kG8DefaultResolution = 72;
26+
static NSInteger const kG8MinCredibleResolution = 70;
27+
static NSInteger const kG8MaxCredibleResolution = 2400;
2628

2729
namespace tesseract {
2830
class TessBaseAPI;
@@ -424,6 +426,15 @@ - (void)setRect:(CGRect)rect
424426
- (void)setSourceResolution:(NSInteger)sourceResolution
425427
{
426428
if (_sourceResolution != sourceResolution) {
429+
if (sourceResolution > kG8MaxCredibleResolution) {
430+
NSLog(@"Source resolution is too big: %ld > %ld", (long)sourceResolution, (long)kG8MaxCredibleResolution);
431+
sourceResolution = kG8MaxCredibleResolution;
432+
}
433+
else if (sourceResolution < kG8MinCredibleResolution) {
434+
NSLog(@"Source resolution is too small: %ld < %ld", (long)sourceResolution, (long)kG8MinCredibleResolution);
435+
sourceResolution = kG8MinCredibleResolution;
436+
}
437+
427438
_sourceResolution = sourceResolution;
428439

429440
_tesseract->SetSourceResolution((int)sourceResolution);

TestsProject/TestsProjectTests/G8RecognitionTestsHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef NS_ENUM(NSUInteger, G8CustomPreprocessing) {
3131
@property (nonatomic, assign) CGSize boundingSizeForResizing;
3232
@property (nonatomic, assign) G8CustomPreprocessing customPreprocessingType;
3333

34+
- (void)setupTesseract;
3435
- (void)recognizeImage;
3536
- (void)recognizeImageUsingOperation;
3637
- (UIImage *)thresholdedImageForImage:(UIImage *)sourceImage;

TestsProject/TestsProjectTests/G8RecognitionTestsHelper.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ - (void)waitTimeLmit:(NSTimeInterval)maximumWait whileTrue:(BOOL (^)())shouldKee
5050

5151
- (void)setupTesseract
5252
{
53+
if (self.tesseract == nil) {
54+
self.tesseract = [[G8Tesseract alloc] init];
55+
}
56+
5357
self.tesseract.delegate = self;
5458

5559
self.tesseract.language = kG8Languages;

TestsProject/TestsProjectTests/RecognitionTests.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@
125125
[[theValue(block.level) should] equal:theValue(G8PageIteratorLevelWord)];
126126
});
127127

128+
it(@"Should clamp source resolution", ^{
129+
[helper setupTesseract];
130+
131+
helper.tesseract.sourceResolution = 50;
132+
[[theValue(helper.tesseract.sourceResolution) should] beInTheIntervalFrom:theValue(70) to:theValue(2400)];
133+
134+
helper.tesseract.sourceResolution = 3000;
135+
[[theValue(helper.tesseract.sourceResolution) should] beInTheIntervalFrom:theValue(70) to:theValue(2400)];
136+
});
137+
128138
it(@"Should draw blocks on image", ^{
129139
[helper recognizeImage];
130140

0 commit comments

Comments
 (0)