BitMatrix.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. namespace Zxing\Common;
  3. final class BitMatrix {
  4. private $width;
  5. private $height;
  6. private $rowSize;
  7. private $bits;
  8. public function __construct($width, $height=false, $rowSize=false, $bits=false) {
  9. if(!$height){
  10. $height = $width;
  11. }
  12. if(!$rowSize){
  13. $rowSize = intval(($width + 31) / 32);
  14. }
  15. if(!$bits){
  16. $bits = fill_array(0,$rowSize*$height,0);array();//new int[rowSize * height];
  17. }
  18. $this->width = $width;
  19. $this->height = $height;
  20. $this->rowSize = $rowSize;
  21. $this->bits = $bits;
  22. }
  23. public static function parse($stringRepresentation, $setString, $unsetString){
  24. if (!$stringRepresentation) {
  25. throw new \InvalidArgumentException();
  26. }
  27. $bits = array();
  28. $bitsPos = 0;
  29. $rowStartPos = 0;
  30. $rowLength = -1;
  31. $nRows = 0;
  32. $pos = 0;
  33. while ($pos < strlen($stringRepresentation)) {
  34. if ($stringRepresentation{$pos} == '\n' ||
  35. $stringRepresentation->{$pos} == '\r') {
  36. if ($bitsPos > $rowStartPos) {
  37. if($rowLength == -1) {
  38. $rowLength = $bitsPos - $rowStartPos;
  39. }
  40. else if ($bitsPos - $rowStartPos != $rowLength) {
  41. throw new \InvalidArgumentException("row lengths do not match");
  42. }
  43. $rowStartPos = $bitsPos;
  44. $nRows++;
  45. }
  46. $pos++;
  47. }
  48. else if (substr($stringRepresentation,$pos, strlen($setString))==$setString) {
  49. $pos += strlen($setString);
  50. $bits[$bitsPos] = true;
  51. $bitsPos++;
  52. }
  53. else if (substr($stringRepresentation, $pos + strlen($unsetString))==$unsetString) {
  54. $pos += strlen($unsetString);
  55. $bits[$bitsPos] = false;
  56. $bitsPos++;
  57. } else {
  58. throw new \InvalidArgumentException(
  59. "illegal character encountered: " . substr($stringRepresentation,$pos));
  60. }
  61. }
  62. // no EOL at end?
  63. if ($bitsPos > $rowStartPos) {
  64. if($rowLength == -1) {
  65. $rowLength = $bitsPos - $rowStartPos;
  66. } else if ($bitsPos - $rowStartPos != $rowLength) {
  67. throw new \InvalidArgumentException("row lengths do not match");
  68. }
  69. $nRows++;
  70. }
  71. $matrix = new BitMatrix($rowLength, $nRows);
  72. for ($i = 0; $i < $bitsPos; $i++) {
  73. if ($bits[$i]) {
  74. $matrix->set($i % $rowLength, $i / $rowLength);
  75. }
  76. }
  77. return $matrix;
  78. }
  79. /**
  80. * <p>Gets the requested bit, where true means black.</p>
  81. *
  82. * @param $x; The horizontal component (i.e. which column)
  83. * @param $y; The vertical component (i.e. which row)
  84. * @return value of given bit in matrix
  85. */
  86. public function get($x, $y) {
  87. $offset = intval($y * $this->rowSize + ($x / 32));
  88. if(!isset($this->bits[$offset])){
  89. $this->bits[$offset] = 0;
  90. }
  91. // return (($this->bits[$offset] >> ($x & 0x1f)) & 1) != 0;
  92. return (uRShift($this->bits[$offset],($x & 0x1f)) & 1) != 0;//было >>> вместо >>, не знаю как эмулировать беззнаковый сдвиг
  93. }
  94. /**
  95. * <p>Sets the given bit to true.</p>
  96. *
  97. * @param $x; The horizontal component (i.e. which column)
  98. * @param $y; The vertical component (i.e. which row)
  99. */
  100. public function set($x, $y) {
  101. $offset = intval($y * $this->rowSize + ($x / 32));
  102. if(!isset($this->bits[$offset])){
  103. $this->bits[$offset] = 0;
  104. }
  105. //$this->bits[$offset] = $this->bits[$offset];
  106. // if($this->bits[$offset]>200748364){
  107. //$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));
  108. $bob = $this->bits[$offset];
  109. $bob |= 1 << ($x & 0x1f);
  110. $this->bits[$offset] |= overflow32($bob);
  111. //$this->bits[$offset] = intval32bits($this->bits[$offset]);
  112. //}
  113. //16777216
  114. }
  115. public function _unset($x, $y) {//было unset, php не позволяет использовать unset
  116. $offset = intval($y * $this->rowSize + ($x / 32));
  117. $this->bits[$offset] &= ~(1 << ($x & 0x1f));
  118. }
  119. /**1 << (249 & 0x1f)
  120. * <p>Flips the given bit.</p>
  121. *
  122. * @param $x; The horizontal component (i.e. which column)
  123. * @param $y; The vertical component (i.e. which row)
  124. */
  125. public function flip($x, $y) {
  126. $offset = $y * $this->rowSize + intval($x / 32);
  127. $this->bits[$offset] = overflow32($this->bits[$offset]^(1 << ($x & 0x1f)));
  128. }
  129. /**
  130. * Exclusive-or (XOR): Flip the bit in this {@code BitMatrix} if the corresponding
  131. * mask bit is set.
  132. *
  133. * @param $mask; XOR mask
  134. */
  135. public function _xor($mask) {//было xor, php не позволяет использовать xor
  136. if ($this->width != $mask->getWidth() || $this->height != $mask->getHeight()
  137. || $this->rowSize != $mask->getRowSize()) {
  138. throw new \InvalidArgumentException("input matrix dimensions do not match");
  139. }
  140. $rowArray = new BitArray($this->width / 32 + 1);
  141. for ($y = 0; $y < $this->height; $y++) {
  142. $offset = $y * $this->rowSize;
  143. $row = $mask->getRow($y, $rowArray)->getBitArray();
  144. for ($x = 0; $x < $this->rowSize; $x++) {
  145. $this->bits[$offset + $x] ^= $row[$x];
  146. }
  147. }
  148. }
  149. /**
  150. * Clears all bits (sets to false).
  151. */
  152. public function clear() {
  153. $max = count($this->bits);
  154. for ($i = 0; $i < $max; $i++) {
  155. $this->bits[$i] = 0;
  156. }
  157. }
  158. /**
  159. * <p>Sets a square region of the bit matrix to true.</p>
  160. *
  161. * @param $left; The horizontal position to begin at (inclusive)
  162. * @param $top; The vertical position to begin at (inclusive)
  163. * @param $width; The width of the region
  164. * @param $height; The height of the region
  165. */
  166. public function setRegion($left, $top, $width, $height) {
  167. if ($top < 0 || $left < 0) {
  168. throw new \InvalidArgumentException("Left and top must be nonnegative");
  169. }
  170. if ($height < 1 || $width < 1) {
  171. throw new \InvalidArgumentException("Height and width must be at least 1");
  172. }
  173. $right = $left + $width;
  174. $bottom = $top + $height;
  175. if ($bottom > $this->height || $right > $this->width) { //> this.height || right > this.width
  176. throw new \InvalidArgumentException("The region must fit inside the matrix");
  177. }
  178. for ($y = $top; $y < $bottom; $y++) {
  179. $offset = $y * $this->rowSize;
  180. for ($x = $left; $x < $right; $x++) {
  181. $this->bits[$offset + intval($x / 32)] = overflow32($this->bits[$offset + intval($x / 32)]|= 1 << ($x & 0x1f));
  182. }
  183. }
  184. }
  185. /**
  186. * A fast method to retrieve one row of data from the matrix as a BitArray.
  187. *
  188. * @param $y; The row to retrieve
  189. * @param $row; An optional caller-allocated BitArray, will be allocated if null or too small
  190. * @return The resulting BitArray - this reference should always be used even when passing
  191. * your own row
  192. */
  193. public function getRow($y, $row) {
  194. if ($row == null || $row->getSize() < $this->width) {
  195. $row = new BitArray($this->width);
  196. } else {
  197. $row->clear();
  198. }
  199. $offset = $y * $this->rowSize;
  200. for ($x = 0; $x < $this->rowSize; $x++) {
  201. $row->setBulk($x * 32, $this->bits[$offset + $x]);
  202. }
  203. return $row;
  204. }
  205. /**
  206. * @param $y; row to set
  207. * @param $row; {@link BitArray} to copy from
  208. */
  209. public function setRow($y, $row) {
  210. $this->bits = arraycopy($row->getBitArray(), 0, $this->bits, $y * $this->rowSize, $this->rowSize);
  211. }
  212. /**
  213. * Modifies this {@code BitMatrix} to represent the same but rotated 180 degrees
  214. */
  215. public function rotate180() {
  216. $width = $this->getWidth();
  217. $height = $this-getHeight();
  218. $topRow = new BitArray($width);
  219. $bottomRow = new BitArray($width);
  220. for ($i = 0; $i < ($height+1) / 2; $i++) {
  221. $topRow = $this->getRow($i, $topRow);
  222. $bottomRow = $this->getRow($height - 1 - $i, $bottomRow);
  223. $topRow->reverse();
  224. $bottomRow->reverse();
  225. $this->setRow($i, $bottomRow);
  226. $this->setRow($height - 1 - $i, $topRow);
  227. }
  228. }
  229. /**
  230. * This is useful in detecting the enclosing rectangle of a 'pure' barcode.
  231. *
  232. * @return {@code left,top,width,height} enclosing rectangle of all 1 bits, or null if it is all white
  233. */
  234. public function getEnclosingRectangle() {
  235. $left = $this->width;
  236. $top = $this->height;
  237. $right = -1;
  238. $bottom = -1;
  239. for ($y = 0; $y < $this->height; $y++) {
  240. for ($x32 = 0; $x32 < $this->rowSize; $x32++) {
  241. $theBits = $this->bits[$y * $this->rowSize + $x32];
  242. if ($theBits != 0) {
  243. if ($y < $top) {
  244. $top = $y;
  245. }
  246. if ($y > $bottom) {
  247. $bottom = $y;
  248. }
  249. if ($x32 * 32 < $left) {
  250. $bit = 0;
  251. while (($theBits << (31 - $bit)) == 0) {
  252. $bit++;
  253. }
  254. if (($x32 * 32 + $bit) < $left) {
  255. $left = $x32 * 32 + $bit;
  256. }
  257. }
  258. if ($x32 * 32 + 31 > $right) {
  259. $bit = 31;
  260. while ((sdvig3($theBits, $bit)) == 0) {//>>>
  261. $bit--;
  262. }
  263. if (($x32 * 32 + $bit) > $right) {
  264. $right = $x32 * 32 + $bit;
  265. }
  266. }
  267. }
  268. }
  269. }
  270. $width = $right - $left;
  271. $height = $bottom - $top;
  272. if ($width < 0 || $height < 0) {
  273. return null;
  274. }
  275. return array($left, $top, $width, $height);
  276. }
  277. /**
  278. * This is useful in detecting a corner of a 'pure' barcode.
  279. *
  280. * @return {@code x,y} coordinate of top-left-most 1 bit, or null if it is all white
  281. */
  282. public function getTopLeftOnBit() {
  283. $bitsOffset = 0;
  284. while ($bitsOffset < count($this->bits) && $this->bits[$bitsOffset] == 0) {
  285. $bitsOffset++;
  286. }
  287. if ($bitsOffset == count($this->bits)) {
  288. return null;
  289. }
  290. $y = $bitsOffset / $this->rowSize;
  291. $x = ($bitsOffset % $this->rowSize) * 32;
  292. $theBits = $this->bits[$bitsOffset];
  293. $bit = 0;
  294. while (($theBits << (31-$bit)) == 0) {
  295. $bit++;
  296. }
  297. $x += $bit;
  298. return array($x, $y);
  299. }
  300. public function getBottomRightOnBit() {
  301. $bitsOffset = count($this->bits) - 1;
  302. while ($bitsOffset >= 0 && $this->bits[$bitsOffset] == 0) {
  303. $bitsOffset--;
  304. }
  305. if ($bitsOffset < 0) {
  306. return null;
  307. }
  308. $y = $bitsOffset / $this->rowSize;
  309. $x = ($bitsOffset % $this->rowSize) * 32;
  310. $theBits = $this->bits[$bitsOffset];
  311. $bit = 31;
  312. while ((sdvig3($theBits, $bit)) == 0) {//>>>
  313. $bit--;
  314. }
  315. $x += $bit;
  316. return array($x, $y);
  317. }
  318. /**
  319. * @return The width of the matrix
  320. */
  321. public function getWidth() {
  322. return $this->width;
  323. }
  324. /**
  325. * @return The height of the matrix
  326. */
  327. public function getHeight() {
  328. return $this->height;
  329. }
  330. /**
  331. * @return The row size of the matrix
  332. */
  333. public function getRowSize() {
  334. return $this->rowSize;
  335. }
  336. //@Override
  337. public function equals($o) {
  338. if (!($o instanceof BitMatrix)) {
  339. return false;
  340. }
  341. $other = $o;
  342. return $this->width == $other->width && $this->height == $other->height && $this->rowSize == $other->rowSize &&
  343. $this->bits===$other->bits;
  344. }
  345. //@Override
  346. public function hashCode() {
  347. $hash = $this->width;
  348. $hash = 31 * $hash + $this->width;
  349. $hash = 31 * $hash + $this->height;
  350. $hash = 31 * $hash + $this->rowSize;
  351. $hash = 31 * $hash + hashCode($this->bits);
  352. return $hash;
  353. }
  354. //@Override
  355. public function toString($setString='', $unsetString='',$lineSeparator='') {
  356. if(!$setString||!$unsetString){
  357. return (string)"X "." ";
  358. }
  359. if($lineSeparator&&$lineSeparator!=="\n"){
  360. return $this->toString_($setString, $unsetString, $lineSeparator);
  361. }
  362. return (string)($setString. $unsetString. "\n");
  363. }
  364. /**
  365. * @deprecated call {@link #toString(String,String)} only, which uses \n line separator always
  366. */
  367. // @Deprecated
  368. public function toString_($setString, $unsetString, $lineSeparator) {
  369. //$result = new StringBuilder(height * (width + 1));
  370. $result = '';
  371. for ($y = 0; $y < $this->height; $y++) {
  372. for ($x = 0; $x < $this->width; $x++) {
  373. $result .= ($this->get($x, $y) ? $setString : $unsetString);
  374. }
  375. $result .= ($lineSeparator);
  376. }
  377. return (string)$result;
  378. }
  379. // @Override
  380. public function _clone() {//clone()
  381. return new BitMatrix($this->width, $this->height, $this->rowSize, $this->bits);
  382. }
  383. }