BitMatrix.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. namespace Zxing\Common;
  3. final class BitMatrix
  4. {
  5. private $width;
  6. private $height;
  7. private $rowSize;
  8. /**
  9. * @var mixed|int[]
  10. */
  11. private $bits;
  12. public function __construct($width, $height = false, $rowSize = false, $bits = false)
  13. {
  14. if (!$height) {
  15. $height = $width;
  16. }
  17. if (!$rowSize) {
  18. $rowSize = (int)(($width + 31) / 32);
  19. }
  20. if (!$bits) {
  21. $bits = fill_array(0, $rowSize * $height, 0);
  22. // [];//new int[rowSize * height];
  23. }
  24. $this->width = $width;
  25. $this->height = $height;
  26. $this->rowSize = $rowSize;
  27. $this->bits = $bits;
  28. }
  29. public static function parse($stringRepresentation, $setString, $unsetString)
  30. {
  31. if (!$stringRepresentation) {
  32. throw new \InvalidArgumentException();
  33. }
  34. $bits = [];
  35. $bitsPos = 0;
  36. $rowStartPos = 0;
  37. $rowLength = -1;
  38. $nRows = 0;
  39. $pos = 0;
  40. while ($pos < strlen((string) $stringRepresentation)) {
  41. if ($stringRepresentation[$pos] == '\n' ||
  42. $stringRepresentation->{$pos} == '\r') {
  43. if ($bitsPos > $rowStartPos) {
  44. if ($rowLength == -1) {
  45. $rowLength = $bitsPos - $rowStartPos;
  46. } elseif ($bitsPos - $rowStartPos != $rowLength) {
  47. throw new \InvalidArgumentException("row lengths do not match");
  48. }
  49. $rowStartPos = $bitsPos;
  50. $nRows++;
  51. }
  52. $pos++;
  53. } elseif (substr((string) $stringRepresentation, $pos, strlen((string) $setString)) == $setString) {
  54. $pos += strlen((string) $setString);
  55. $bits[$bitsPos] = true;
  56. $bitsPos++;
  57. } elseif (substr((string) $stringRepresentation, $pos + strlen((string) $unsetString)) == $unsetString) {
  58. $pos += strlen((string) $unsetString);
  59. $bits[$bitsPos] = false;
  60. $bitsPos++;
  61. } else {
  62. throw new \InvalidArgumentException(
  63. "illegal character encountered: " . substr((string) $stringRepresentation, $pos)
  64. );
  65. }
  66. }
  67. // no EOL at end?
  68. if ($bitsPos > $rowStartPos) {
  69. if ($rowLength == -1) {
  70. $rowLength = $bitsPos - $rowStartPos;
  71. } elseif ($bitsPos - $rowStartPos != $rowLength) {
  72. throw new \InvalidArgumentException("row lengths do not match");
  73. }
  74. $nRows++;
  75. }
  76. $matrix = new BitMatrix($rowLength, $nRows);
  77. for ($i = 0; $i < $bitsPos; $i++) {
  78. if ($bits[$i]) {
  79. $matrix->set($i % $rowLength, $i / $rowLength);
  80. }
  81. }
  82. return $matrix;
  83. }
  84. /**
  85. * <p>Sets the given bit to true.</p>
  86. *
  87. * @param $x ; The horizontal component (i.e. which column)
  88. * @param $y ; The vertical component (i.e. which row)
  89. */
  90. public function set($x, $y): void
  91. {
  92. $offset = (int)($y * $this->rowSize + ($x / 32));
  93. if (!isset($this->bits[$offset])) {
  94. $this->bits[$offset] = 0;
  95. }
  96. //$this->bits[$offset] = $this->bits[$offset];
  97. // if($this->bits[$offset]>200748364){
  98. //$this->bits= array(0,0,-16777216,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,-16777216,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,-16777216,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,-16777216,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,-16777216,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,-16777216,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,-16777216,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,-16777216,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,-16777216,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,-16777216,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,-16777216,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,-1090519040,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,1056964608,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,-1358954496,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,117440512,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,50331648,-1,-1,-1,-1,65535,0,0,0,0,0,0,0,33554432,-1,-1,536870911,-4096,65279,0,0,0,0,0,0,0,0,-1,-1,65535,-4096,65535,0,0,0,0,0,0,0,0,-193,536870911,0,-4096,65279,0,0,0,0,0,0,0,0,-254,32767,0,-4096,61951,0,0,0,0,0,0,0,0,20913920,0,0,-4096,50175,0,0,0,0,0,0,0,0,0,0,0,-4096,60159,0,0,0,0,0,0,0,0,0,0,0,-4096,64255,0,0,0,0,0,0,0,0,0,0,0,-8192,56319,0,0,0,0,0,0,0,0,0,0,0,-4096,16777215,0,0,0,0,0,0,0,0,0,0,0,-4096,16777215,0,0,0,0,0,0,0,0,0,0,0,-4096,16777215,0,0,0,0,0,0,0,0,0,0,0,-4096,16777215,0,0,0,0,0,0,0,0,0,0,0,-4096,16777215,0,0,0,0,0,0,0,0,0,0,0,-4096,16777215,0,0,0,0,0,0,0,0,0,0,0,-4096,16777215,0,0,0,0,0,0,0,0,0,0,0,-4096,16777215,0,0,0,0,0,0,0,251658240,0,0,0,-4096,-1,255,0,256,0,0,0,0,117440512,0,0,0,-4096,-1,255,0,512,0,0,0,0,117440512,0,0,0,-4096,-1,255,0,1024,0,0,0,0,117440512,0,0,0,-4096,-1,223,0,256,0,0,0,0,117440512,0,0,33030144,-4096,-1,191,0,256,0,0,0,0,117440512,0,0,33554428,-4096,-1,255,0,768,0,0,0,0,117440512,0,402849792,67108862,-8192,-1,255,0,768,0,0,0,0,117440512,0,470278396,63045630,-8192,-1,255,0,256,0,0,0,0,251658240,-8388608,470278399,58720286,-8192,-1,2686975,0,3842,0,0,0,0,251658240,-131072,1007149567,58720286,-8192,-1,2031615,0,3879,0,0,0,0,251658240,536739840,1007092192,58720286,-8192,-1,851967,0,3840,0,0,0,0,251658240,917504,1007092192,58720284,-8192,-1,2031615,0,3968,0,0,0,0,251658240,917504,1007092160,59244060,-8192,-1,65535,0,7936,0,0,0,0,251658240,917504,1009779136,59244060,-8192,-1,9371647,0,1792,0,0,0,0,251658240,917504,946921920,59244060,-8192,-1,8585215,0,1792,0,0,0,0,117440512,-15859712,477159875,59244060,-8192,-1,65535,0,12032,0,0,0,0,251658240,-15859712,52490691,59244060,-8192,-1,-1,0,65408,0,0,0,0,251658240,-15859712,58778051,59244060,-8192,-1,-1,0,65473,0,0,0,0,251658240,-15859712,125886915,59244060,-8192,-1,-1,0,65472,0,0,0,0,251658240,-15859712,58778051,59244060,-8192,-1,-1,0,65408,0,0,0,0,251658240,-15859712,8380867,59244060,-8192,-1,-1,0,65473,0,0,0,0,251658240,-15859712,8380867,59244060,-8192,-1,-1,0,131011,0,0,0,0,251658240,-15859712,8380867,58720284,-8192,-1,-1,0,130947,0,0,0,0,251658240,-15859712,2089411,58720284,-8192,-1,-1,0,130947,0,0,0,0,251658240,-32636928,449,58720284,-8192,-1,-1,33554431,131015,0,0,0,0,251658240,786432,448,62914588,-8192,-1,-1,16777215,131015,0,0,0,0,251658240,786432,448,67108860,-8192,-1,-1,553648127,131015,0,0,0,0,251658240,786432,946864576,67108860,-8192,-1,-1,32505855,131015,0,0,0,0,251658240,786432,946921976,8388604,-8192,-1,-1,8191999,131015,0,0,0,0,251658240,-262144,946921983,248,-8192,-1,-1,8126463,196551,0,0,0,0,251658240,-262144,7397887,0,-8192,-1,-1,16777215,262087,0,0,0,0,251658240,-262144,8257543,0,-8192,-1,-1,-2121269249,262095,0,0,0,0,520093696,0,8257536,0,-8192,-1,-1,-201326593,262095,0,0,0,0,520290304,0,8257536,117963776,-8192,-1,-1,-201326593,262095,0,0,0,0,520093696,0,-2140143616,118488579,-8192,-1,-1,-201326593,131023,0,0,0,0,520093696,0,-2131697280,118488579,-8192,-1,-1,-503316481,131023,0,0,0,0,520093696,2145386496,-2131631232,118484995,-16384,-1,-1,-469762049,262095,0,0,0,0,520093696,2147221504,552649600,118481344,-16384,-1,-1,-469762049,131023,0,0,0,0,520290304,2147221504,2029002240,118481344,-16384,-1,-1,-469762049,262031,0,0,0,0,520290304,-266600448,2029001791,125952960,-16384,-1,-1,-469762049,262031,0,0,0,0,1057423360,-266600448,2027953215,133177312,-16384,-1,-1,-134217729,262111,0,0,0,0,1058471936,-266600448,-119531393,133177343,-16384,-1,-1,-134217729,262111,0,0,0,0,1058471936,-2145648640,-253754369,66068479,-16384,-1,-1,-134217729,262111,0,0,0,0,1058471936,236716032,-253754369,15729663,-16384,-1,-1,-134217729,262095,0,0,0,0,1057947648,236716032,-253754369,6348807,-16384,-1,-1,-134217729,262095,0,0,0,0,524222464,236716032,-253690305,6348803,-16384,-1,-1,-134217729,262111,0,0,0,0,521076736,2115764224,-253625344,14737411,-16384,-1,-1,-134217729,262095,0,0,0,0,522125312,2115764224,-253625344,14743555,-16384,-1,-1,-134217729,262111,0,0,16772608,0,1073676288,-31719424,-2014283776,14810115,-16384,-1,-1,-1,262143,0,0,16776704,0,1065287680,-1642594304,-1879178880,14810115,-16384,-1,-1,-1,524287,0,0,16776192,0,2139029504,264241152,-2013396089,14809091,-16384,-1,-1,-1,262095,0,0,16776192,0,2139029504,264241152,-2080636025,14803335,-16384,-1,-1,-1,262087,0,0,16776192,0,2147418112,264241152,-2132803581,14803847,-16384,-1,-1,-402653185,524259,0,0,8386048,0,2147418112,0,-2132688896,123783,-16384,-1,-1,1207959551,262112,0,0,16775168,0,2147418112,0,14794752,1046535,-16384,-1,-1,268435455,262128,0,0,16775168,0,2147418112,0,14712832,1047615,-16384,-1,-1,536870911,524284,0,0,16776705,0,2147418112,0,14680832,1047615,-16384,-1,-1,-1,524287,0,0,16776704,0,2147418112,-1048576,14681087,1046591,-32768,-1,-1,-1,524287,0,0,16776704,0,2147418112,-524288,-2132802561,2080831,-32768,-1,-1,-1,524287,0,0,16776705,0,2147418112,-524288,-31718401,2080831,-32768,-1,-1,-1,1048575,0,0,16776193,0,2147418112,3670016,-31718528,2080831,-32768,-1,-1,-1,524287,0,0,16776195,0,2147418112,3670016,-31718528,134086719,-32768,-1,-1,-1,524287,0,0,16776195,0,2147418112,3670016,253494144,268173368,-32768,-1,-1,-1,524287,0,0,16775171,0,2147418112,3670016,268174208,268173368,-32768,-1,-1,-1,1048575,0,0,16771072,0,2147418112,-63438848,268174223,31457328,-32768,-1,-1,-1,1048575,0,0,10418176,0,-65536,-63438848,133957519,14807040,-32768,-1,-1,-1,2097151,0,0,15923200,0,2147418112,-63438848,1968015,14809095,-32768,-1,-1,-1,1048575,0,0,12808192,0,2147418112,-63438848,2082703,12711943,-32768,-1,-1,-1,2097151,0,0,6420480,0,2147418112,-63438848,2082703,14343,-32768,-1,-1,-1,2097151,0,0,15202304,0,-65536,-63438848,2082703,1849351,-32768,-1,-1,-1,2097151,0,0,15464448,0,-65536,-63438848,264472335,1849351,-32768,-1,-1,-1,4194303,0,0,16371712,0,-65536,-63438848,264472335,14343,-32768,-1,-1,-1,8388607,0,0,0,0,-65536,-63438848,532907791,235010048,-32768,-1,-1,-1,16777215,0,0,0,0,-65536,-63438848,-1603833,235010160,-32768,-1,-1,-1,16777215,0,0,0,0,-65536,3670016,-30976,67238000,-32768,-1,-1,-1,16777215,0,0,0,0,-65536,3670016,-30976,48,-32768,-1,-1,-1,16777215,0,0,0,0,-65536,3670016,-29391104,768,-32768,-1,-1,-1,16777215,0,0,0,0,-65536,3670016,-29391104,768,-32768,-1,-1,-1,16777215,0,0,0,0,-65536,-524287,-65042433,768,-32768,-1,-1,-1,16777215,0,0,0,0,-65536,-524287,2082441215,0,-65536,-1,-1,-1,16777215,0,0,0,0,-13697024,-524287,511,0,-65536,-1,-1,-1,16777215,0,0,0,0,-12648448,1,0,0,-65536,-1,-1,-1,14680063,0,0,0,0,-12648448,1,0,0,-65536,-1,-1,-1,16777215,0,0,0,0,-65536,1,0,0,-65536,-1,-1,-1,14680063,0,0,0,0,-8454144,1,0,0,-65536,-1,-1,-1,12582911,0,0,0,0,-12648448,1,0,0,-65536,-1,-1,-1,2097151,0,0,0,0,-12648448,1,0,0,-65536,-1,-1,-1,1048575,0,0,0,0,-14745600,1,0,0,-65536,-1,-1,-1,3145727,0,0,0,0,1056964608,1,0,0,-65536,-1,-1,-1,1048575,0,0,0,0,1056964608,1,0,0,-65536,-1,-1,-1,1048575,0,0,0,0,1056964608,1,0,0,-65536,-1,-1,-1,524287,0,0,0,0,2130706432,1,0,0,-65536,-1,-1,-1,1048575,0,0,0,0,1056964608,1,0,0,-65536,-1,-1,-1,524287,0,0,0,0,2130706432,1,0,0,-65536,-1,-1,-1,524287,0,0,0,0,2130706432,1,0,0,-65536,-1,-1,-1,1048575,0,0,0,0,2130706432,1,0,0,-65536,-1,-1,-1,1048575,0,0,0,0,50331648,1,0,0,-65536,-1,-1,-1,1048575,0,0,0,0,117440512,1,0,-268435456,-1,-1,-1,-1,524287,0,0,0,0,251658240,1,0,-320,-1,-1,-1,-1,262143,0,0,0,0,520093696,1,-2048,-1,-1,-1,-1,-1,262143,0,0,0,0,1056964608,-16777213,-1,-1,-1,-1,-1,-1,131071,0,0,0,0,-16777216,-121,-1,-1,-1,-1,-1,-1,131071,0,0,0,0,-16777216,-1,-1,-1,-1,-1,-1,-1,131071,0,0,0,0,-16777216,-1,-1,-1,-1,-1,-1,-1,131071,0,0,0,0,-16777216,-1,-1,-1,-1,-1,-1,-1,131071,0,0,0,0,-16777216,-1,-1,-1,-1,-1,-1,-1,65535,0,0,0,0,-16777216,-1,-1,-1,-1,-1,-1,-1,65535,0,0,0,0,-16777216,-1,-1,-1,-1,-1,-1,-1,131071,0,0,0,0,-16777216,-1,-1,-1,-1,-1,-1,-1,262143,0,0,0,0,-16777216,-1,-1,-1,-1,-1,-1,-1,524287,0,0,0,0,-16777216,-1,-1,-1,-1,-1,-1,-1,524287,0,0,0,0,-16777216,-1,-1,-1,-1,-1,-1,-1,589823,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,8179,0,0,0,0,50331648,-1,-1,-1,-1,-1,-1,-1,4080,0,0,0,0,117440512,-1,-1,-1,-1,-1,-1,-1,1016,0,0,0,0,251658240,-1,-1,-1,-1,-1,-1,1073741823,1020,0,0,0,0,50331648,-1,-1,-1,-1,-1,-1,536870911,254,0,0,0,0,50331648,-1,-1,-1,-1,-1,-1,536870911,255,0,0,0,0,50331648,-1,-1,-1,-1,-1,-1,-1879048193,127,0,0,0,0,50331648,-1,-1,-1,-1,-1,-1,-469762049,63,0,0,0,0,1191182336,-1,-1,-1,-1,1023999,0,-520093712,15,0,0,0,0,-218103808,-1,-1,-1,-1,0,-8454144,-260046849,7,0,0,0,0,0,-193,-1,-1,-1057947649,-2147483648,-1,-58720257,1,0,0,0,0,0,-251,-1,-1,-1057423361,-2074,-1,-1,0,0,0,0,0,0,-59648,-1,-1,-1,-1,-1,1073741823,0,0,0,0,0,0,-65536,-1,-1,-1,-1,-1,268435455,0,0,0,0,0,0,-65536,-1,-1,-1,-1,-1,67108863,0,0,0,0,0,0,-65536,-1,-1,-1,-1,-1,8388607,0,0,0,0,0,0,0,-403603456,-1,-1,-1,-1,262143,0,0,0,8388656,0,0,0,-1891434496,-1,-1,-1,-1,16383,0,0,0,8388608,0,0,0,-1612513280,-1,-1,-1,-1,63,0,0,0,0,0,0,0,-24320,-1,-1,-1,8388607,0,0,0,0,0,0,0,0,-256,-1,-1,1073741823,1,0,0,0,0,0,0,0,1610612736,-15,-1,-1,16383,0,0,0,0,0,0,0,0,-16646144,-1,-1,251658239,0,0,0,0,0,0,0,0,0,-51200,-1,-1,40959,0,0,0,0,0,0,268419584,103809024,-12713984,-1,-2147483137,4194303,0,0,0,0,0,0,0,402620416,-2144010240,-13631487,-32513,3,20480,0,0,0,0,0,0,0,419299328,0,-262144,-1,0,0,0,0,0,0,0,0,0,0,0,-5832704,268049407,0,0,0,0,0,0,0,0,0,0,0,0,33030144,0,0,0,0,0,0,0,0,0,0,0,0,3670016,0,0,0,0,0,0,0,0,0,0,0,0,1572864,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,458752,0,0,0,0,0,0,0,0,0,0,0,0,229376,0,0,0,0,0,0,0,0,0,0,0,0,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8192,0,0,0,0,0,0,0,0,0,0,0,0,8192,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,31744,0,0,0,0,0,0,0,0,0,0,0,0,31744,0,0,0,0,0,0,0,0,0,0,0,0,64512,0,0,0,0,0,0,0,0,0,0,0,0,15872,0,0,0,0,0,0,0,0,0,0,0,0,3584,0,0,0,0,0,0,0,0,0,0,0,0,7680,0,0,0,0,0,0,0,0,0,0,0,0,512,0,0,0,0,0,0,0,0,0,0,0,0,3968,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3840,0,0,0,0,0,0,0,0,0,0,0,0,1855,0,0,0,0,0,0,0,0,0,0,0,0,63,0,0,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,134217728,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,0,0,0,0,0,0,0,0,0,0,124,0,0,0,0,0,0,0,0,0,0,0,-260046848,63,0,0,0,0,0,0,0,0,0,0,0,-17301504,127,0,0,0,0,0,0,0,0,0,0,0,-524288,127,0,0,0,0,0,0,0,0,0,0,0,-262144,127,0,0,0,0,0,0,0,0,0,0,0,-262144,63,0,0,0,0,0,0,0,0,0,0,0,-262144,63,0,0,0,0,0,0,0,0,0,0,0,-262144,63,0,0,0,0,0,0,0,0,0,0,0,-262144,31,0,0,0,0,0,0,0,0,0,0,0,-262144,63,0,0,0,0,3,0,0,0,0,0,0,-262144,63,0,0,0,0,7,0,0,0,0,0,0,-262144,63,0,0,0,0,63,0,0,0,0,0,0,-262144,63,0,0,0,0,511,0,0,0,0,0,0,-524288,31,0,0,0,0,8191,0,0,0,0,0,0,-1048576,63,0,0,0,0,131071,0,0,0,0,0,0,-524288,63,0,0,0,0,262143,0,0,0,0,0,0,-524288,63,0,0,0,0,131071,0,0,0,0,0,0,-1048576,63,0,0,0,0,262143,0,0,0,0,0,0,-1048576,63,0,0,0,0,262143,0,0,0,0,0,0,-1048576,63,0,0,0,0,262143,0,0,0,0,0,0,-1048576,63,0,0,0,0,262143,0,0,0,0,0,0,-2097152,127,0,0,0,0,262143,0,0,0,0,0,0,-2097152,127,0,0,0,0,262143,0,0,0,0,0,0,-1048576,127,0,0,0,0,262143,0,0,0,0,0,0,-1048576,127,0,0,0,0,262143,0,0,0,0,0,0,-2097152,255,0,0,0,0,262143,0,0,0,0,0,0,-2097152,255,0,0,0,0,262142,0,0,0,0,0,0,-2097152,255,0,0,0,0,262142,0,0,0,0,0,0,-2097152,255,0,0,0,0,262142,0,0,0,0,0,0,-2097152,255,0,0,0,0,262140,0,0,0,0,0,0,-2097152,255,0,0,0,0,131068,0,0,0,0,0,0,-4194304,255,0,0,0,0,131068,0,0,0,0,0,0,-4194304,255,0,0,0,0,65528,0,0,0,0,0,0,-8388608,255,0,0,0,0,65528,0,0,0,0,0,0,-8388608,255,0,0,0,0,65528,0,0,0,0,0,0,-8388608,255,0,0,0,0,32760,0,0,0,0,0,0,-8388608,255,0,0,0,0,32760,0,0,0,0,0,0,-16777216,255,0,0,-2147483648,255,16368,0,0,0,0,0,0,-16777216,255,0,0,-536870912,1023,16368,0,0,0,0,0,0,-33554432,255,0,0,-16777216,4095,16352,0,0,0,0,0,0,-33554432,255,0,0,-8388608,262143,16352,0,0,0,0,0,0,-33554432,255,0,0,-1048576,2097151,16352,0,0,0,0,0,0,-67108864,255,0,0,-524288,8388607,16352,0,0,0,0,0,0,-67108864,255,0,0,-262144,16777215,16320,0,0,0,0,0,0,-67108864,255,0,0,-131072,16777215,100679648,0,0,0,0,0,0,-67108864,255,0,0,-16384,16776959,125861824,0,0,0,0,0,0,-134217728,255,0,0,-4096,16773121,62930880,0,0,0,0,0,0,-134217728,127,0,0,2147482624,16252928,32704,0,0,0,0,0,0,-134217728,127,0,0,268435200,14680064,16320,0,0,0,0,0,0,-134217728,127,0,0,134217600,0,32704,0,0,0,0,0,0,-33554432,127,0,1056964608,67108736,0,32704,0,0,0,0,0,0,-33554432,127,0,2130706432,33554368,0,65408,0,0,0,0,0,0,-33554432,127,0,-16777216,8388576,0,32640,0,0,0,0,0,0,-134217728,127,0,-16777216,2097136,0,32640,0,0,0,0,0,0,-134217728,63,0,-16776960,1048573,0,32640,0,0,0,0,0,0,-536870912,63,0,-16776448,1048575,0,32640,0,0,0,0,0,0,-536870912,63,0,-33553664,6291455,66752,32640,0,0,0,0,0,0,-536870912,63,0,2013266688,2097148,229376,32640,0,0,0,0,0,0,-536870912,63,0,256,4194300,229376,32640,0,0,0,0,0,0,-536870912,63,0,0,524280,196608,32512,0,0,8,0,0,0,-1073741824,63,0,0,-200,15,65280,0,0,24,0,0,0,-1073741824,63,0,0,-1867768,127,32512,0,0,56,0,0,0,-1073741824,63,0,0,-1056768,4095,32512,0,0,124,0,0,0,-1073741824,63,0,0,-1050624,8191,32512,0,0,508,0,0,0,-2147483648,31,0,0,-7866368,8191,32512,0,0,1020,0,0,0,-2147483648,31,0,0,-33030656,8095,32512,0,0,2046,0,0,0,-2147483648,63,0,0,-66586624,771,32512,0,0,4094,0,0,0,0,63,0,0,-134184960,1,32256,0,0,8190,0,0,0,0,63,0,0,1610612736,0,32256,0,0,16382,0,0,0,-2147483648,63,0,0,0,0,15872,0,0,32767,0,0,0,-2147483648,31,0,0,0,0,15872,0,-2147483648,65535,0,0,0,-2147483648,31,0,0,0,0,7680,0,0,65535,0,0,0,-2147483648,31,0,0,134217728,0,7680,0,-2147483648,65535,0,0,0,-2147483648,31,0,0,0,0,7680,0,-2147483648,65535,0,0,0,-2147483648,31,0,0,0,0,7680,0,-1073741824,65535,0,0,0,-2147483648,31,0,0,0,0,3072,0,-1073741824,65535,0,0,0,-2147483648,31,0,0,0,0,3072,0,-1073741824,65535,0,0,0,-2147483648,31,0,0,0,0,0,0,-2147483648,65535,0,0,0,-2147483648,31,0,0,0,0,0,0,-1073741824,65535,0,0,0,0,31,0,0,0,0,0,0,-1073741824,65535,0,0,0,0,31,0,0,0,0,0,0,-2147483648,65535,0,0,0,0,30,0,0,0,0,0,0,-1073741824,65535,0,0,0,0,30,0,0,0,0,0,0,-2147483648,65535,0,0,0,0,30,0,0,0,0,0,0,0,65535,0,0,0,0,28,0,0,0,0,0,0,0,65535,0,0,0,0,28,0,0,0,0,0,0,0,65535,0,0,0,0,28,0,0,0,0,0,0,0,65535,0,0,0,0,24,0,0,0,0,0,0,-2147483648,65535,0,0,0,0,0,0,0,0,0,0,0,-536870912,65535);//[$offset] |= intval32bits(1 << ($x & 0x1f));
  99. $bob = $this->bits[$offset];
  100. $bob |= 1 << ($x & 0x1f);
  101. $this->bits[$offset] |= ($bob);
  102. //$this->bits[$offset] = intval32bits($this->bits[$offset]);
  103. //}
  104. //16777216
  105. }
  106. public function _unset($x, $y): void
  107. {//было unset, php не позволяет использовать unset
  108. $offset = (int)($y * $this->rowSize + ($x / 32));
  109. $this->bits[$offset] &= ~(1 << ($x & 0x1f));
  110. }
  111. /**1 << (249 & 0x1f)
  112. * <p>Flips the given bit.</p>
  113. *
  114. * @param $x ; The horizontal component (i.e. which column)
  115. * @param $y ; The vertical component (i.e. which row)
  116. */
  117. public function flip($x, $y): void
  118. {
  119. $offset = $y * $this->rowSize + (int)($x / 32);
  120. $this->bits[$offset] = ($this->bits[$offset] ^ (1 << ($x & 0x1f)));
  121. }
  122. /**
  123. * Exclusive-or (XOR): Flip the bit in this {@code BitMatrix} if the corresponding
  124. * mask bit is set.
  125. *
  126. * @param $mask ; XOR mask
  127. */
  128. public function _xor($mask)
  129. {//было xor, php не позволяет использовать xor
  130. if ($this->width != $mask->getWidth() || $this->height != $mask->getHeight()
  131. || $this->rowSize != $mask->getRowSize()) {
  132. throw new \InvalidArgumentException("input matrix dimensions do not match");
  133. }
  134. $rowArray = new BitArray($this->width / 32 + 1);
  135. for ($y = 0; $y < $this->height; $y++) {
  136. $offset = $y * $this->rowSize;
  137. $row = $mask->getRow($y, $rowArray)->getBitArray();
  138. for ($x = 0; $x < $this->rowSize; $x++) {
  139. $this->bits[$offset + $x] ^= $row[$x];
  140. }
  141. }
  142. }
  143. /**
  144. * Clears all bits (sets to false).
  145. */
  146. public function clear(): void
  147. {
  148. $max = is_countable($this->bits) ? count($this->bits) : 0;
  149. for ($i = 0; $i < $max; $i++) {
  150. $this->bits[$i] = 0;
  151. }
  152. }
  153. /**
  154. * <p>Sets a square region of the bit matrix to true.</p>
  155. *
  156. * @param $left ; The horizontal position to begin at (inclusive)
  157. * @param $top ; The vertical position to begin at (inclusive)
  158. * @param $width ; The width of the region
  159. * @param $height ; The height of the region
  160. */
  161. public function setRegion($left, $top, $width, $height)
  162. {
  163. if ($top < 0 || $left < 0) {
  164. throw new \InvalidArgumentException("Left and top must be nonnegative");
  165. }
  166. if ($height < 1 || $width < 1) {
  167. throw new \InvalidArgumentException("Height and width must be at least 1");
  168. }
  169. $right = $left + $width;
  170. $bottom = $top + $height;
  171. if ($bottom > $this->height || $right > $this->width) { //> this.height || right > this.width
  172. throw new \InvalidArgumentException("The region must fit inside the matrix");
  173. }
  174. for ($y = $top; $y < $bottom; $y++) {
  175. $offset = $y * $this->rowSize;
  176. for ($x = $left; $x < $right; $x++) {
  177. $this->bits[$offset + (int)($x / 32)] = ($this->bits[$offset + (int)($x / 32)] |= 1 << ($x & 0x1f));
  178. }
  179. }
  180. }
  181. /**
  182. * Modifies this {@code BitMatrix} to represent the same but rotated 180 degrees
  183. */
  184. public function rotate180(): void
  185. {
  186. $width = $this->getWidth();
  187. $height = $this->getHeight();
  188. $topRow = new BitArray($width);
  189. $bottomRow = new BitArray($width);
  190. for ($i = 0; $i < ($height + 1) / 2; $i++) {
  191. $topRow = $this->getRow($i, $topRow);
  192. $bottomRow = $this->getRow($height - 1 - $i, $bottomRow);
  193. $topRow->reverse();
  194. $bottomRow->reverse();
  195. $this->setRow($i, $bottomRow);
  196. $this->setRow($height - 1 - $i, $topRow);
  197. }
  198. }
  199. /**
  200. * @return float The width of the matrix
  201. */
  202. public function getWidth()
  203. {
  204. return $this->width;
  205. }
  206. /**
  207. * A fast method to retrieve one row of data from the matrix as a BitArray.
  208. *
  209. * @param $y ; The row to retrieve
  210. * @param $row ; An optional caller-allocated BitArray, will be allocated if null or too small
  211. *
  212. * @return BitArray The resulting BitArray - this reference should always be used even when passing
  213. * your own row
  214. */
  215. public function getRow($y, $row)
  216. {
  217. if ($row == null || $row->getSize() < $this->width) {
  218. $row = new BitArray($this->width);
  219. } else {
  220. $row->clear();
  221. }
  222. $offset = $y * $this->rowSize;
  223. for ($x = 0; $x < $this->rowSize; $x++) {
  224. $row->setBulk($x * 32, $this->bits[$offset + $x]);
  225. }
  226. return $row;
  227. }
  228. /**
  229. * @param $y ; row to set
  230. * @param $row ; {@link BitArray} to copy from
  231. */
  232. public function setRow($y, $row): void
  233. {
  234. $this->bits = arraycopy($row->getBitArray(), 0, $this->bits, $y * $this->rowSize, $this->rowSize);
  235. }
  236. /**
  237. * This is useful in detecting the enclosing rectangle of a 'pure' barcode.
  238. *
  239. * @return {@code left,top,width,height} enclosing rectangle of all 1 bits, or null if it is all white
  240. */
  241. public function getEnclosingRectangle()
  242. {
  243. $left = $this->width;
  244. $top = $this->height;
  245. $right = -1;
  246. $bottom = -1;
  247. for ($y = 0; $y < $this->height; $y++) {
  248. for ($x32 = 0; $x32 < $this->rowSize; $x32++) {
  249. $theBits = $this->bits[$y * $this->rowSize + $x32];
  250. if ($theBits != 0) {
  251. if ($y < $top) {
  252. $top = $y;
  253. }
  254. if ($y > $bottom) {
  255. $bottom = $y;
  256. }
  257. if ($x32 * 32 < $left) {
  258. $bit = 0;
  259. while (($theBits << (31 - $bit)) == 0) {
  260. $bit++;
  261. }
  262. if (($x32 * 32 + $bit) < $left) {
  263. $left = $x32 * 32 + $bit;
  264. }
  265. }
  266. if ($x32 * 32 + 31 > $right) {
  267. $bit = 31;
  268. while ((sdvig3($theBits, $bit)) == 0) {//>>>
  269. $bit--;
  270. }
  271. if (($x32 * 32 + $bit) > $right) {
  272. $right = $x32 * 32 + $bit;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. $width = $right - $left;
  279. $height = $bottom - $top;
  280. if ($width < 0 || $height < 0) {
  281. return null;
  282. }
  283. return [$left, $top, $width, $height];
  284. }
  285. /**
  286. * This is useful in detecting a corner of a 'pure' barcode.
  287. *
  288. * @return {@code x,y} coordinate of top-left-most 1 bit, or null if it is all white
  289. */
  290. public function getTopLeftOnBit()
  291. {
  292. $bitsOffset = 0;
  293. while ($bitsOffset < (is_countable($this->bits) ? count($this->bits) : 0) && $this->bits[$bitsOffset] == 0) {
  294. $bitsOffset++;
  295. }
  296. if ($bitsOffset == (is_countable($this->bits) ? count($this->bits) : 0)) {
  297. return null;
  298. }
  299. $y = $bitsOffset / $this->rowSize;
  300. $x = ($bitsOffset % $this->rowSize) * 32;
  301. $theBits = $this->bits[$bitsOffset];
  302. $bit = 0;
  303. while (($theBits << (31 - $bit)) == 0) {
  304. $bit++;
  305. }
  306. $x += $bit;
  307. return [$x, $y];
  308. }
  309. public function getBottomRightOnBit()
  310. {
  311. $bitsOffset = (is_countable($this->bits) ? count($this->bits) : 0) - 1;
  312. while ($bitsOffset >= 0 && $this->bits[$bitsOffset] == 0) {
  313. $bitsOffset--;
  314. }
  315. if ($bitsOffset < 0) {
  316. return null;
  317. }
  318. $y = $bitsOffset / $this->rowSize;
  319. $x = ($bitsOffset % $this->rowSize) * 32;
  320. $theBits = $this->bits[$bitsOffset];
  321. $bit = 31;
  322. while ((sdvig3($theBits, $bit)) == 0) {//>>>
  323. $bit--;
  324. }
  325. $x += $bit;
  326. return [$x, $y];
  327. }
  328. /**
  329. * @return float The height of the matrix
  330. */
  331. public function getHeight()
  332. {
  333. return $this->height;
  334. }
  335. /**
  336. * @return int The row size of the matrix
  337. */
  338. public function getRowSize()
  339. {
  340. return $this->rowSize;
  341. }
  342. public function equals($o)
  343. {
  344. if (!($o instanceof BitMatrix)) {
  345. return false;
  346. }
  347. $other = $o;
  348. return $this->width == $other->width
  349. && $this->height == $other->height
  350. && $this->rowSize == $other->rowSize
  351. && $this->bits === $other->bits;
  352. }
  353. //@Override
  354. public function hashCode()
  355. {
  356. $hash = $this->width;
  357. $hash = 31 * $hash + $this->width;
  358. $hash = 31 * $hash + $this->height;
  359. $hash = 31 * $hash + $this->rowSize;
  360. $hash = 31 * $hash + hashCode($this->bits);
  361. return $hash;
  362. }
  363. //@Override
  364. public function toString($setString = '', $unsetString = '', $lineSeparator = '')
  365. {
  366. if (!$setString || !$unsetString) {
  367. return (string)'X ' . ' ';
  368. }
  369. if ($lineSeparator && $lineSeparator !== "\n") {
  370. return $this->toString_($setString, $unsetString, $lineSeparator);
  371. }
  372. return (string)($setString . $unsetString . "\n");
  373. }
  374. public function toString_($setString, $unsetString, $lineSeparator)
  375. {
  376. //$result = new StringBuilder(height * (width + 1));
  377. $result = '';
  378. for ($y = 0; $y < $this->height; $y++) {
  379. for ($x = 0; $x < $this->width; $x++) {
  380. $result .= ($this->get($x, $y) ? $setString : $unsetString);
  381. }
  382. $result .= ($lineSeparator);
  383. }
  384. return (string)$result;
  385. }
  386. /**
  387. * @deprecated call {@link #toString(String,String)} only, which uses \n line separator always
  388. */
  389. // @Deprecated
  390. /**
  391. * <p>Gets the requested bit, where true means black.</p>
  392. *
  393. * @param $x ; The horizontal component (i.e. which column)
  394. * @param $y ; The vertical component (i.e. which row)
  395. *
  396. * @return value of given bit in matrix
  397. */
  398. public function get($x, $y)
  399. {
  400. $offset = (int)($y * $this->rowSize + ($x / 32));
  401. if (!isset($this->bits[$offset])) {
  402. $this->bits[$offset] = 0;
  403. }
  404. // return (($this->bits[$offset] >> ($x & 0x1f)) & 1) != 0;
  405. return (uRShift($this->bits[$offset], ($x & 0x1f)) & 1) != 0;//было >>> вместо >>, не знаю как эмулировать беззнаковый сдвиг
  406. }
  407. // @Override
  408. public function _clone(): \Zxing\Common\BitMatrix
  409. {
  410. return new BitMatrix($this->width, $this->height, $this->rowSize, $this->bits);
  411. }
  412. }