Skip to content

Commit

Permalink
ITF: support shorter ITF symbols (2, 3 or 4 depending on heuristic)
Browse files Browse the repository at this point in the history
If we are looking only for ITF symbols, the minimum length is now 4
instead of 6. If the symbol covers the whole image, this gets halved, so
2 or 3 respectively. This fixes #853 without introducing any false
positive for our sample data-set.
  • Loading branch information
axxel committed Oct 22, 2024
1 parent fc42827 commit 8fb2f81
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
13 changes: 8 additions & 5 deletions core/src/oned/ODITFReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace ZXing::OneD {

Barcode ITFReader::decodePattern(int rowNumber, PatternView& next, std::unique_ptr<DecodingState>&) const
{
const int minCharCount = 6;
const int minCharCount = _opts.formats().count() == 1 ? 4 : 6; // if we are only looking for ITF, we accept shorter symbols
const int minQuietZone = 6; // spec requires 10

next = FindLeftGuard(next, 4 + minCharCount/2 + 3, FixedPattern<4, 4>{1, 1, 1, 1}, minQuietZone);
Expand All @@ -33,6 +33,8 @@ Barcode ITFReader::decodePattern(int rowNumber, PatternView& next, std::unique_p

constexpr int weights[] = {1, 2, 4, 7, 0};
int xStart = next.pixelsInFront();
bool startsAtFirstBar = next.isAtFirstBar();

next = next.subView(4, 10);

std::string txt;
Expand Down Expand Up @@ -62,17 +64,18 @@ Barcode ITFReader::decodePattern(int rowNumber, PatternView& next, std::unique_p

next = next.subView(0, 3);

if (Size(txt) < minCharCount || !next.isValid())
return {};

// Check quiet zone size
if (!(next.isAtLastBar() || next[3] > minQuietZone * (threshold.bar + threshold.space) / 3))
if (!next.isValid() || !(next.isAtLastBar() || next[3] > minQuietZone * (threshold.bar + threshold.space) / 3))
return {};

// Check stop pattern
if (next[0] < threshold[0] || next[1] > threshold[1] || next[2] > threshold[2])
return {};

// Check min length depending on whether the code covers the complete image or not
if (Size(txt) < (startsAtFirstBar && next.isAtLastBar() ? (minCharCount / 2) : minCharCount))
return {};

Error error = _opts.validateITFCheckSum() && !GTIN::IsCheckDigitValid(txt) ? ChecksumError() : Error();

// Symbology identifier ISO/IEC 16390:2007 Annex C Table C.1
Expand Down
6 changes: 3 additions & 3 deletions test/blackbox/BlackboxTestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,9 @@ int runBlackBoxTests(const fs::path& testPathPrefix, const std::set<std::string>
{ 3, 5, 180 },
}, ReaderOptions().setEanAddOnSymbol(EanAddOnSymbol::Require));

runTests("itf-1", "ITF", 11, {
{ 10, 11, 0 },
{ 10, 11, 180 },
runTests("itf-1", "ITF", 12, {
{ 11, 12, 0 },
{ 11, 12, 180 },
});

runTests("itf-2", "ITF", 6, {
Expand Down
Binary file added test/samples/itf-1/#853.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/samples/itf-1/#853.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1515

0 comments on commit 8fb2f81

Please sign in to comment.