ThresholdBinarizerTest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2022 gitlost
  3. */
  4. // SPDX-License-Identifier: Apache-2.0
  5. #include "ThresholdBinarizer.h"
  6. #include "oned/ODReader.h"
  7. #include "gtest/gtest.h"
  8. using namespace ZXing;
  9. // Helper to parse a 0/1 string into a BitMatrix
  10. static BitMatrix ParseBitMatrix(const std::string& str, const int width)
  11. {
  12. const int height = narrow_cast<int>(str.length() / width);
  13. BitMatrix mat(width, height);
  14. for (int y = 0; y < height; ++y) {
  15. size_t offset = y * width;
  16. for (int x = 0; x < width; ++x, offset++) {
  17. if (str[offset] == '1') {
  18. mat.set(x, y);
  19. }
  20. }
  21. }
  22. return mat;
  23. }
  24. // Helper to convert a BitMatrix into a black/white ImageView
  25. static ImageView getImageView(std::vector<uint8_t> &buf, const BitMatrix &bits)
  26. {
  27. buf.resize(bits.width() * bits.height());
  28. for (int r = 0; r < bits.height(); r++) {
  29. const int k = r * bits.width();
  30. for (int c = 0; c < bits.width(); c++) {
  31. buf[k + c] = bits.get(c, r) ? 0x00 : 0xFF;
  32. }
  33. }
  34. return ImageView(buf.data(), bits.width(), bits.height(), ImageFormat::Lum);
  35. }
  36. TEST(ThresholdBinarizerTest, PatternRowClear)
  37. {
  38. std::string bitstream;
  39. BitMatrix bits;
  40. ReaderOptions opts;
  41. std::vector<uint8_t> buf;
  42. // Test that ThresholdBinarizer::getPatternRow() clears row first (same as GlobalHistogramBinarizer)
  43. // Following was failing due to OneD::DoDecode() accumulating bars in loop when using ThresholdBinarizer
  44. // DataBarExpanded Stacked with 11 rows (11 + 10 * 3 separators), 1 column (2 data segments) wide
  45. bitstream = "01010101111011111110111111110000101100100000010100010"
  46. "00001010000100000001000000001010010011011111101010000"
  47. "00000101010101010101010101010101010101010101010100000"
  48. "00001000001110100010100001010101001110000101100110000"
  49. "10100111110001011101011110000000010001111010011000101"
  50. "00001000001110100010100001010101001110000101100110000"
  51. "00000101010101010101010101010101010101010101010100000"
  52. "00000001110100100001010000001010010101100111000110000"
  53. "01011110001011011110001111110000101010011000111000010"
  54. "00000001110100100001010000001010010101100111000110000"
  55. "00000101010101010101010101010101010101010101010100000"
  56. "00000110001111101110100001010100001110001110111010000"
  57. "10101001110000010001011110000001110001110001000100101"
  58. "00000110001111101110100001010100001110001110111010000"
  59. "00000101010101010101010101010101010101010101010100000"
  60. "00001000011011000101010000101010010011100010001100000"
  61. "01000111100100111010001111000000101100011101110010010"
  62. "00001000011011000101010000101010010011100010001100000"
  63. "00000101010101010101010101010101010101010101010100000"
  64. "00001000110001110110100000000100001011110100001000000"
  65. "10100111001110001001011111111001110100001011110111101"
  66. "00001000110001110110100000000100001011110100001000000"
  67. "00000101010101010101010101010101010101010101010100000"
  68. "00000010011101111101010010101010010100110000100000000"
  69. "01011101100010000010001100000000101011001111011110010"
  70. "00000010011101111101010010101010010100110000100000000"
  71. "00000101010101010101010101010101010101010101010100000"
  72. "00000100010111101110100000101010001100000110011010000"
  73. "10111011101000010001011111000000110011111001100101101"
  74. "00000100010111101110100000101010001100000110011010000"
  75. "00000101010101010101010101010101010101010101010100000"
  76. "00000011000110001001000000010101010000011110011010000"
  77. "01011100111001110110011111100000101111100001100101010"
  78. "00000011000110001001000000010101010000011110011010000"
  79. "00000101010101010101010101010101010101010101010100000"
  80. "00001011100010001110100000000010001011111010001100000"
  81. "10100100011101110001011111111100110100000101110011101"
  82. "00001011100010001110100000000010001011111010001100000"
  83. "00000101010101010101010101010101010101010101010100000"
  84. "00001000111010000101000101010101010100001011000110000"
  85. "01000111000101111010011000000000101011110100111000010";
  86. bits = ParseBitMatrix(bitstream, 53 /*width*/);
  87. opts.setFormats(BarcodeFormat::DataBarExpanded);
  88. opts.setMinLineCount(1);
  89. OneD::Reader reader(opts);
  90. auto barcode = reader.decode(ThresholdBinarizer(getImageView(buf, bits), 0x7F));
  91. EXPECT_TRUE(barcode.isValid());
  92. EXPECT_EQ(barcode.text(TextMode::HRI), "(91)12345678901234567890123456789012345678901234567890123456789012345678");
  93. }