8000 Remove legacy `OneToFourBitPattern` function from Code93 decoder by wooyechan · Pull Request #972 · zxing-cpp/zxing-cpp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remove legacy OneToFourBitPattern function from Code93 decoder #972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ if (ZXING_READERS OR ZXING_WRITERS_OLD)
set (ONED_FILES
src/oned/ODUPCEANCommon.h
src/oned/ODUPCEANCommon.cpp
src/oned/ODCode93Patterns.h
src/oned/ODCode93Patterns.cpp
src/oned/ODCode128Patterns.h
src/oned/ODCode128Patterns.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion core/src/oned/ODCode128Patterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace ZXing::OneD::Code128 {

const std::array<std::array<int, 6>, 107> CODE_PATTERNS = { {
constexpr std::array<std::array<int, 6>, 107> CODE_PATTERNS = { {
{ 2, 1, 2, 2, 2, 2 }, // 0
{ 2, 2, 2, 1, 2, 2 },
{ 2, 2, 2, 2, 2, 1 },
Expand Down
62 changes: 62 additions & 0 deletions core/src/oned/ODCode93Patterns.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2016 Nu-book Inc.
* Copyright 2016 ZXing authors
*/
// SPDX-License-Identifier: Apache-2.0

#include "ODCode93Patterns.h"

namespace ZXing::OneD::Code93 {

constexpr std::array<std::array<int, 6>, 48> CODE_PATTERNS = { {
{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{ 3, 1, 1, 1, 2, 1 }, // 45
{ 1, 2, 2, 2, 1, 1 },
{ 1, 1, 1, 1, 4, 1 } // STOP_CODE
} };

} // namespace ZXing::OneD::Code93
15 changes: 15 additions & 0 deletions core/src/oned/ODCode93Patterns.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2016 Nu-book Inc.
* Copyright 2016 ZXing authors
*/
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <array>

namespace ZXing::OneD::Code93 {

extern const std::array<std::array<int, 6>, 48> CODE_PATTERNS;

} // namespace ZXing::OneD::Code93
48 changes: 24 additions & 24 deletions core/src/oned/ODCode93Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "ODCode93Reader.h"

#include "ODCode93Patterns.h"
#include "Barcode.h"
#include "ZXAlgorithms.h"

Expand All @@ -16,29 +17,12 @@
namespace ZXing::OneD {

// Note that 'abcd' are dummy characters in place of control characters.
// Control chars ($)==a, (%)==b, (/)==c, (+)==d
static constexpr char ALPHABET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%abcd*";

/**
* Each character consist of 3 bars and 3 spaces and is 9 modules wide in total.
* Each bar and space is from 1 to 4 modules wide.
* These represent the encodings of characters. Each module is assigned 1 bit.
* The 9 least-significant bits of each int correspond to the 9 modules in a symbol.
* Note: bit 9 (the first) is always 1, bit 1 (the last) is always 0.
*/
static constexpr int CHARACTER_ENCODINGS[] = {
0x114, 0x148, 0x144, 0x142, 0x128, 0x124, 0x122, 0x150, 0x112, 0x10A, // 0-9
0x1A8, 0x1A4, 0x1A2, 0x194, 0x192, 0x18A, 0x168, 0x164, 0x162, 0x134, // A-J
0x11A, 0x158, 0x14C, 0x146, 0x12C, 0x116, 0x1B4, 0x1B2, 0x1AC, 0x1A6, // K-T
0x196, 0x19A, 0x16C, 0x166, 0x136, 0x13A, // U-Z
0x12E, 0x1D4, 0x1D2, 0x1CA, 0x16E, 0x176, 0x1AE, // - - %
0x126, 0x1DA, 0x1D6, 0x132, 0x15E, // Control chars ($)==a, (%)==b, (/)==c, (+)==d, *
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving the comment regarding the control characters would be nice.

};

static_assert(Size(ALPHABET) == Size(CHARACTER_ENCODINGS), "table size mismatch");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This static_assert should also be preserved.


static const int ASTERISK_ENCODING = 0x15E;
static_assert(Size(ALPHABET) == Size(Code93::CODE_PATTERNS), "table size mismatch");

using CounterContainer = std::array<int, 6>;
static const int ASTERISK_ENCODING = 0x660; // E2E_PATTERNS[47]

static bool
CheckOneChecksum(const std::string& result, int checkPosition, int weightMax)
Expand All @@ -65,19 +49,34 @@ CheckChecksums(const std::string& result)
std::string DecodeCode39AndCode93FullASCII(std::string encoded, const char ctrl[4]);

constexpr int CHAR_LEN = 6;
constexpr int CHAR_SUM = 9;
constexpr int CHAR_MODS = 9;
// quiet zone is half the width of a character symbol
constexpr float QUIET_ZONE_SCALE = 0.5f;

//TODO: make this a constexpr variable initialization
static auto E2E_PATTERNS = [ ] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to implement this TODO (see also comment above)?

// This creates an array of ints for fast IndexOf lookup of the edge-2-edge patterns (ISO/IEC 15417:2007(E) Table 2)
// e.g. a code pattern of { 2, 1, 2, 2, 2, 2 } becomes the e2e pattern { 3, 3, 4, 4 } and the value 0bs100011110000.
std::array<int, 48> res;
for (int i = 0; i < Size(res); ++i) {
const auto& a = Code93::CODE_PATTERNS[i];
std::array<int, 4> e2e;
for (int j = 0; j < 4; j++)
e2e[j] = a[j] + a[j + 1];
res[i] = ToInt(e2e);
}
return res;
}();

static bool IsStartGuard(const PatternView& window, int spaceInPixel)
{
// The complete start pattern is FixedPattern<CHAR_LEN, CHAR_SUM>{1, 1, 1, 1, 4, 1}.
// The complete start pattern is FixedPattern<CHAR_LEN, CHAR_MODS>{1, 1, 1, 1, 4, 1}.
// Use only the first 4 elements which results in more than a 2x speedup. This is counter-intuitive since we save at
// most 1/3rd of the loop iterations in FindPattern. The reason might be a successful vectorization with the limited
// pattern size that is missed otherwise. We check for the remaining 2 slots for plausibility of the 4:1 ratio.
return IsPattern(window, FixedPattern<4, 4>{1, 1, 1, 1}, spaceInPixel, QUIET_ZONE_SCALE * 12) &&
window[4] > 3 * window[5] - 2 &&
RowReader::OneToFourBitPattern<CHAR_LEN, CHAR_SUM>(window) == ASTERISK_ENCODING;
ToInt(NormalizedE2EPattern<CHAR_LEN>(window, CHAR_MODS)) == ASTERISK_ENCODING;
}

Barcode Code93Reader::decodePattern(int rowNumber, PatternView& next, std::unique_ptr<DecodingState>&) const
Expand All @@ -99,7 +98,8 @@ Barcode Code93Reader::decodePattern(int rowNumber, PatternView& next, std::uniqu
if (!next.skipSymbol())
return {};

txt += LookupBitPattern(OneToFourBitPattern<CHAR_LEN, CHAR_SUM>(next), CHARACTER_ENCODINGS, ALPHABET);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LookupBitPattern helper use could very well stay.

txt += LookupBitPattern(ToInt(NormalizedE2EPattern<CHAR_LEN>(next, CHAR_MODS)), E2E_PATTERNS, ALPHABET);

if (txt.back() == 0)
return {};
} while (txt.back() != '*');
Expand Down
10 changes: 0 additions & 10 deletions core/src/oned/ODRowReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,6 @@ class RowReader
return pattern;
}

/**
* @brief each bar/space is 1-4 modules wide, we have N bars/spaces, they are SUM modules wide in total
*/
template <int LEN, int SUM>
static int OneToFourBitPattern(const PatternView& view)
{
// TODO: make sure none of the elements in the normalized pattern exceeds 4
return ToInt(NormalizedPattern<LEN, SUM>(view));
}

/**
* @brief Lookup the pattern in the table and return the character in alphabet at the same index.
* @returns 0 if pattern is not found. Used to be -1 but that fails on systems where char is unsigned.
Expand Down
0