MaskUtilTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. declare(strict_types = 1);
  3. namespace BaconQrCodeTest\Encoder;
  4. use BaconQrCode\Encoder\ByteMatrix;
  5. use BaconQrCode\Encoder\MaskUtil;
  6. use PHPUnit\Framework\TestCase;
  7. class MaskUtilTest extends TestCase
  8. {
  9. public function dataMaskBits() : array
  10. {
  11. return [
  12. [0, [
  13. [1, 0, 1, 0, 1, 0],
  14. [0, 1, 0, 1, 0, 1],
  15. [1, 0, 1, 0, 1, 0],
  16. [0, 1, 0, 1, 0, 1],
  17. [1, 0, 1, 0, 1, 0],
  18. [0, 1, 0, 1, 0, 1],
  19. ]],
  20. [1, [
  21. [1, 1, 1, 1, 1, 1],
  22. [0, 0, 0, 0, 0, 0],
  23. [1, 1, 1, 1, 1, 1],
  24. [0, 0, 0, 0, 0, 0],
  25. [1, 1, 1, 1, 1, 1],
  26. [0, 0, 0, 0, 0, 0],
  27. ]],
  28. [2, [
  29. [1, 0, 0, 1, 0, 0],
  30. [1, 0, 0, 1, 0, 0],
  31. [1, 0, 0, 1, 0, 0],
  32. [1, 0, 0, 1, 0, 0],
  33. [1, 0, 0, 1, 0, 0],
  34. [1, 0, 0, 1, 0, 0],
  35. ]],
  36. [3, [
  37. [1, 0, 0, 1, 0, 0],
  38. [0, 0, 1, 0, 0, 1],
  39. [0, 1, 0, 0, 1, 0],
  40. [1, 0, 0, 1, 0, 0],
  41. [0, 0, 1, 0, 0, 1],
  42. [0, 1, 0, 0, 1, 0],
  43. ]],
  44. [4, [
  45. [1, 1, 1, 0, 0, 0],
  46. [1, 1, 1, 0, 0, 0],
  47. [0, 0, 0, 1, 1, 1],
  48. [0, 0, 0, 1, 1, 1],
  49. [1, 1, 1, 0, 0, 0],
  50. [1, 1, 1, 0, 0, 0],
  51. ]],
  52. [5, [
  53. [1, 1, 1, 1, 1, 1],
  54. [1, 0, 0, 0, 0, 0],
  55. [1, 0, 0, 1, 0, 0],
  56. [1, 0, 1, 0, 1, 0],
  57. [1, 0, 0, 1, 0, 0],
  58. [1, 0, 0, 0, 0, 0],
  59. ]],
  60. [6, [
  61. [1, 1, 1, 1, 1, 1],
  62. [1, 1, 1, 0, 0, 0],
  63. [1, 1, 0, 1, 1, 0],
  64. [1, 0, 1, 0, 1, 0],
  65. [1, 0, 1, 1, 0, 1],
  66. [1, 0, 0, 0, 1, 1],
  67. ]],
  68. [7, [
  69. [1, 0, 1, 0, 1, 0],
  70. [0, 0, 0, 1, 1, 1],
  71. [1, 0, 0, 0, 1, 1],
  72. [0, 1, 0, 1, 0, 1],
  73. [1, 1, 1, 0, 0, 0],
  74. [0, 1, 1, 1, 0, 0],
  75. ]],
  76. ];
  77. }
  78. /**
  79. * @dataProvider dataMaskBits
  80. */
  81. public function testGetDatMaskBit(int $maskPattern, array $expected) : void
  82. {
  83. for ($x = 0; $x < 6; ++$x) {
  84. for ($y = 0; $y < 6; ++$y) {
  85. $this->assertSame(
  86. 1 === $expected[$y][$x],
  87. MaskUtil::getDataMaskBit($maskPattern, $x, $y)
  88. );
  89. }
  90. }
  91. }
  92. public function testApplyMaskPenaltyRule1() : void
  93. {
  94. $matrix = new ByteMatrix(4, 1);
  95. $matrix->set(0, 0, 0);
  96. $matrix->set(1, 0, 0);
  97. $matrix->set(2, 0, 0);
  98. $matrix->set(3, 0, 0);
  99. $this->assertSame(0, MaskUtil::applyMaskPenaltyRule1($matrix));
  100. // Horizontal
  101. $matrix = new ByteMatrix(6, 1);
  102. $matrix->set(0, 0, 0);
  103. $matrix->set(1, 0, 0);
  104. $matrix->set(2, 0, 0);
  105. $matrix->set(3, 0, 0);
  106. $matrix->set(4, 0, 0);
  107. $matrix->set(5, 0, 1);
  108. $this->assertSame(3, MaskUtil::applyMaskPenaltyRule1($matrix));
  109. $matrix->set(5, 0, 0);
  110. $this->assertSame(4, MaskUtil::applyMaskPenaltyRule1($matrix));
  111. // Vertical
  112. $matrix = new ByteMatrix(1, 6);
  113. $matrix->set(0, 0, 0);
  114. $matrix->set(0, 1, 0);
  115. $matrix->set(0, 2, 0);
  116. $matrix->set(0, 3, 0);
  117. $matrix->set(0, 4, 0);
  118. $matrix->set(0, 5, 1);
  119. $this->assertSame(3, MaskUtil::applyMaskPenaltyRule1($matrix));
  120. $matrix->set(0, 5, 0);
  121. $this->assertSame(4, MaskUtil::applyMaskPenaltyRule1($matrix));
  122. }
  123. public function testApplyMaskPenaltyRule2() : void
  124. {
  125. $matrix = new ByteMatrix(1, 1);
  126. $matrix->set(0, 0, 0);
  127. $this->assertSame(0, MaskUtil::applyMaskPenaltyRule2($matrix));
  128. $matrix = new ByteMatrix(2, 2);
  129. $matrix->set(0, 0, 0);
  130. $matrix->set(1, 0, 0);
  131. $matrix->set(0, 1, 0);
  132. $matrix->set(1, 1, 1);
  133. $this->assertSame(0, MaskUtil::applyMaskPenaltyRule2($matrix));
  134. $matrix = new ByteMatrix(2, 2);
  135. $matrix->set(0, 0, 0);
  136. $matrix->set(1, 0, 0);
  137. $matrix->set(0, 1, 0);
  138. $matrix->set(1, 1, 0);
  139. $this->assertSame(3, MaskUtil::applyMaskPenaltyRule2($matrix));
  140. $matrix = new ByteMatrix(3, 3);
  141. $matrix->set(0, 0, 0);
  142. $matrix->set(1, 0, 0);
  143. $matrix->set(2, 0, 0);
  144. $matrix->set(0, 1, 0);
  145. $matrix->set(1, 1, 0);
  146. $matrix->set(2, 1, 0);
  147. $matrix->set(0, 2, 0);
  148. $matrix->set(1, 2, 0);
  149. $matrix->set(2, 2, 0);
  150. $this->assertSame(3 * 4, MaskUtil::applyMaskPenaltyRule2($matrix));
  151. }
  152. public function testApplyMaskPenalty3() : void
  153. {
  154. // Horizontal 00001011101
  155. $matrix = new ByteMatrix(11, 1);
  156. $matrix->set(0, 0, 0);
  157. $matrix->set(1, 0, 0);
  158. $matrix->set(2, 0, 0);
  159. $matrix->set(3, 0, 0);
  160. $matrix->set(4, 0, 1);
  161. $matrix->set(5, 0, 0);
  162. $matrix->set(6, 0, 1);
  163. $matrix->set(7, 0, 1);
  164. $matrix->set(8, 0, 1);
  165. $matrix->set(9, 0, 0);
  166. $matrix->set(10, 0, 1);
  167. $this->assertSame(40, MaskUtil::applyMaskPenaltyRule3($matrix));
  168. // Horizontal 10111010000
  169. $matrix = new ByteMatrix(11, 1);
  170. $matrix->set(0, 0, 1);
  171. $matrix->set(1, 0, 0);
  172. $matrix->set(2, 0, 1);
  173. $matrix->set(3, 0, 1);
  174. $matrix->set(4, 0, 1);
  175. $matrix->set(5, 0, 0);
  176. $matrix->set(6, 0, 1);
  177. $matrix->set(7, 0, 0);
  178. $matrix->set(8, 0, 0);
  179. $matrix->set(9, 0, 0);
  180. $matrix->set(10, 0, 0);
  181. $this->assertSame(40, MaskUtil::applyMaskPenaltyRule3($matrix));
  182. // Vertical 00001011101
  183. $matrix = new ByteMatrix(1, 11);
  184. $matrix->set(0, 0, 0);
  185. $matrix->set(0, 1, 0);
  186. $matrix->set(0, 2, 0);
  187. $matrix->set(0, 3, 0);
  188. $matrix->set(0, 4, 1);
  189. $matrix->set(0, 5, 0);
  190. $matrix->set(0, 6, 1);
  191. $matrix->set(0, 7, 1);
  192. $matrix->set(0, 8, 1);
  193. $matrix->set(0, 9, 0);
  194. $matrix->set(0, 10, 1);
  195. $this->assertSame(40, MaskUtil::applyMaskPenaltyRule3($matrix));
  196. // Vertical 10111010000
  197. $matrix = new ByteMatrix(1, 11);
  198. $matrix->set(0, 0, 1);
  199. $matrix->set(0, 1, 0);
  200. $matrix->set(0, 2, 1);
  201. $matrix->set(0, 3, 1);
  202. $matrix->set(0, 4, 1);
  203. $matrix->set(0, 5, 0);
  204. $matrix->set(0, 6, 1);
  205. $matrix->set(0, 7, 0);
  206. $matrix->set(0, 8, 0);
  207. $matrix->set(0, 9, 0);
  208. $matrix->set(0, 10, 0);
  209. $this->assertSame(40, MaskUtil::applyMaskPenaltyRule3($matrix));
  210. }
  211. public function testApplyMaskPenaltyRule4() : void
  212. {
  213. // Dark cell ratio = 0%
  214. $matrix = new ByteMatrix(1, 1);
  215. $matrix->set(0, 0, 0);
  216. $this->assertSame(100, MaskUtil::applyMaskPenaltyRule4($matrix));
  217. // Dark cell ratio = 5%
  218. $matrix = new ByteMatrix(2, 1);
  219. $matrix->set(0, 0, 0);
  220. $matrix->set(0, 0, 1);
  221. $this->assertSame(0, MaskUtil::applyMaskPenaltyRule4($matrix));
  222. // Dark cell ratio = 66.67%
  223. $matrix = new ByteMatrix(6, 1);
  224. $matrix->set(0, 0, 0);
  225. $matrix->set(1, 0, 1);
  226. $matrix->set(2, 0, 1);
  227. $matrix->set(3, 0, 1);
  228. $matrix->set(4, 0, 1);
  229. $matrix->set(5, 0, 0);
  230. $this->assertSame(30, MaskUtil::applyMaskPenaltyRule4($matrix));
  231. }
  232. }