123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- <?php
-
- namespace Zxing\Common;
-
- final class BitMatrix
- {
- private $width;
- private $height;
- private $rowSize;
- private $bits;
-
- public function __construct($width, $height = false, $rowSize = false, $bits = false)
- {
- if (!$height) {
- $height = $width;
- }
- if (!$rowSize) {
- $rowSize = (int)(($width + 31) / 32);
- }
- if (!$bits) {
- $bits = fill_array(0, $rowSize * $height, 0);
-
- }
- $this->width = $width;
- $this->height = $height;
- $this->rowSize = $rowSize;
- $this->bits = $bits;
- }
-
- public static function parse($stringRepresentation, $setString, $unsetString)
- {
- if (!$stringRepresentation) {
- throw new \InvalidArgumentException();
- }
- $bits = [];
- $bitsPos = 0;
- $rowStartPos = 0;
- $rowLength = -1;
- $nRows = 0;
- $pos = 0;
- while ($pos < strlen($stringRepresentation)) {
- if ($stringRepresentation[$pos] == '\n' ||
- $stringRepresentation->{$pos} == '\r') {
- if ($bitsPos > $rowStartPos) {
- if ($rowLength == -1) {
- $rowLength = $bitsPos - $rowStartPos;
- } else if ($bitsPos - $rowStartPos != $rowLength) {
- throw new \InvalidArgumentException("row lengths do not match");
- }
- $rowStartPos = $bitsPos;
- $nRows++;
- }
- $pos++;
- } else if (substr($stringRepresentation, $pos, strlen($setString)) == $setString) {
- $pos += strlen($setString);
- $bits[$bitsPos] = true;
- $bitsPos++;
- } else if (substr($stringRepresentation, $pos + strlen($unsetString)) == $unsetString) {
- $pos += strlen($unsetString);
- $bits[$bitsPos] = false;
- $bitsPos++;
- } else {
- throw new \InvalidArgumentException(
- "illegal character encountered: " . substr($stringRepresentation, $pos));
- }
- }
-
-
- if ($bitsPos > $rowStartPos) {
- if ($rowLength == -1) {
- $rowLength = $bitsPos - $rowStartPos;
- } else if ($bitsPos - $rowStartPos != $rowLength) {
- throw new \InvalidArgumentException("row lengths do not match");
- }
- $nRows++;
- }
-
- $matrix = new BitMatrix($rowLength, $nRows);
- for ($i = 0; $i < $bitsPos; $i++) {
- if ($bits[$i]) {
- $matrix->set($i % $rowLength, $i / $rowLength);
- }
- }
-
- return $matrix;
- }
-
-
-
- public function set($x, $y)
- {
- $offset = (int)($y * $this->rowSize + ($x / 32));
- if (!isset($this->bits[$offset])) {
- $this->bits[$offset] = 0;
- }
-
-
-
-
- $bob = $this->bits[$offset];
- $bob |= 1 << ($x & 0x1f);
- $this->bits[$offset] |= ($bob);
-
-
-
-
- }
-
- public function _unset($x, $y)
- {
- $offset = (int)($y * $this->rowSize + ($x / 32));
- $this->bits[$offset] &= ~(1 << ($x & 0x1f));
- }
-
-
-
- public function flip($x, $y)
- {
- $offset = $y * $this->rowSize + (int)($x / 32);
-
- $this->bits[$offset] = ($this->bits[$offset] ^ (1 << ($x & 0x1f)));
- }
-
-
-
- public function _xor($mask)
- {
- if ($this->width != $mask->getWidth() || $this->height != $mask->getHeight()
- || $this->rowSize != $mask->getRowSize()) {
- throw new \InvalidArgumentException("input matrix dimensions do not match");
- }
- $rowArray = new BitArray($this->width / 32 + 1);
- for ($y = 0; $y < $this->height; $y++) {
- $offset = $y * $this->rowSize;
- $row = $mask->getRow($y, $rowArray)->getBitArray();
- for ($x = 0; $x < $this->rowSize; $x++) {
- $this->bits[$offset + $x] ^= $row[$x];
- }
- }
- }
-
-
-
- public function clear()
- {
- $max = count($this->bits);
- for ($i = 0; $i < $max; $i++) {
- $this->bits[$i] = 0;
- }
- }
-
-
-
- public function setRegion($left, $top, $width, $height)
- {
- if ($top < 0 || $left < 0) {
- throw new \InvalidArgumentException("Left and top must be nonnegative");
- }
- if ($height < 1 || $width < 1) {
- throw new \InvalidArgumentException("Height and width must be at least 1");
- }
- $right = $left + $width;
- $bottom = $top + $height;
- if ($bottom > $this->height || $right > $this->width) {
- throw new \InvalidArgumentException("The region must fit inside the matrix");
- }
- for ($y = $top; $y < $bottom; $y++) {
- $offset = $y * $this->rowSize;
- for ($x = $left; $x < $right; $x++) {
- $this->bits[$offset + (int)($x / 32)] = ($this->bits[$offset + (int)($x / 32)] |= 1 << ($x & 0x1f));
- }
- }
- }
-
-
-
- public function rotate180()
- {
- $width = $this->getWidth();
- $height = $this->getHeight();
- $topRow = new BitArray($width);
- $bottomRow = new BitArray($width);
- for ($i = 0; $i < ($height + 1) / 2; $i++) {
- $topRow = $this->getRow($i, $topRow);
- $bottomRow = $this->getRow($height - 1 - $i, $bottomRow);
- $topRow->reverse();
- $bottomRow->reverse();
- $this->setRow($i, $bottomRow);
- $this->setRow($height - 1 - $i, $topRow);
- }
- }
-
-
-
- public function getWidth()
- {
- return $this->width;
- }
-
-
-
- public function getRow($y, $row)
- {
- if ($row == null || $row->getSize() < $this->width) {
- $row = new BitArray($this->width);
- } else {
- $row->clear();
- }
- $offset = $y * $this->rowSize;
- for ($x = 0; $x < $this->rowSize; $x++) {
- $row->setBulk($x * 32, $this->bits[$offset + $x]);
- }
-
- return $row;
- }
-
-
-
- public function setRow($y, $row)
- {
- $this->bits = arraycopy($row->getBitArray(), 0, $this->bits, $y * $this->rowSize, $this->rowSize);
- }
-
-
-
- public function getEnclosingRectangle()
- {
- $left = $this->width;
- $top = $this->height;
- $right = -1;
- $bottom = -1;
-
- for ($y = 0; $y < $this->height; $y++) {
- for ($x32 = 0; $x32 < $this->rowSize; $x32++) {
- $theBits = $this->bits[$y * $this->rowSize + $x32];
- if ($theBits != 0) {
- if ($y < $top) {
- $top = $y;
- }
- if ($y > $bottom) {
- $bottom = $y;
- }
- if ($x32 * 32 < $left) {
- $bit = 0;
- while (($theBits << (31 - $bit)) == 0) {
- $bit++;
- }
- if (($x32 * 32 + $bit) < $left) {
- $left = $x32 * 32 + $bit;
- }
- }
- if ($x32 * 32 + 31 > $right) {
- $bit = 31;
- while ((sdvig3($theBits, $bit)) == 0) {
- $bit--;
- }
- if (($x32 * 32 + $bit) > $right) {
- $right = $x32 * 32 + $bit;
- }
- }
- }
- }
- }
-
- $width = $right - $left;
- $height = $bottom - $top;
-
- if ($width < 0 || $height < 0) {
- return null;
- }
-
- return [$left, $top, $width, $height];
- }
-
-
-
- public function getTopLeftOnBit()
- {
- $bitsOffset = 0;
- while ($bitsOffset < count($this->bits) && $this->bits[$bitsOffset] == 0) {
- $bitsOffset++;
- }
- if ($bitsOffset == count($this->bits)) {
- return null;
- }
- $y = $bitsOffset / $this->rowSize;
- $x = ($bitsOffset % $this->rowSize) * 32;
-
- $theBits = $this->bits[$bitsOffset];
- $bit = 0;
- while (($theBits << (31 - $bit)) == 0) {
- $bit++;
- }
- $x += $bit;
-
- return [$x, $y];
- }
-
- public function getBottomRightOnBit()
- {
- $bitsOffset = count($this->bits) - 1;
- while ($bitsOffset >= 0 && $this->bits[$bitsOffset] == 0) {
- $bitsOffset--;
- }
- if ($bitsOffset < 0) {
- return null;
- }
-
- $y = $bitsOffset / $this->rowSize;
- $x = ($bitsOffset % $this->rowSize) * 32;
-
- $theBits = $this->bits[$bitsOffset];
- $bit = 31;
- while ((sdvig3($theBits, $bit)) == 0) {
- $bit--;
- }
- $x += $bit;
-
- return [$x, $y];
- }
-
-
-
- public function getHeight()
- {
- return $this->height;
- }
-
-
-
- public function getRowSize()
- {
- return $this->rowSize;
- }
-
- public function equals($o)
- {
- if (!($o instanceof BitMatrix)) {
- return false;
- }
- $other = $o;
-
- return $this->width == $other->width
- && $this->height == $other->height
- && $this->rowSize == $other->rowSize
- && $this->bits === $other->bits;
- }
-
-
-
- public function hashCode()
- {
- $hash = $this->width;
- $hash = 31 * $hash + $this->width;
- $hash = 31 * $hash + $this->height;
- $hash = 31 * $hash + $this->rowSize;
- $hash = 31 * $hash + hashCode($this->bits);
-
- return $hash;
- }
-
-
-
- public function toString($setString = '', $unsetString = '', $lineSeparator = '')
- {
- if (!$setString || !$unsetString) {
- return (string)'X ' . ' ';
- }
- if ($lineSeparator && $lineSeparator !== "\n") {
- return $this->toString_($setString, $unsetString, $lineSeparator);
- }
-
- return (string)($setString . $unsetString . "\n");
- }
-
- public function toString_($setString, $unsetString, $lineSeparator)
- {
-
- $result = '';
- for ($y = 0; $y < $this->height; $y++) {
- for ($x = 0; $x < $this->width; $x++) {
- $result .= ($this->get($x, $y) ? $setString : $unsetString);
- }
- $result .= ($lineSeparator);
- }
-
- return (string)$result;
- }
-
-
-
-
-
-
- public function get($x, $y)
- {
-
- $offset = (int)($y * $this->rowSize + ($x / 32));
- if (!isset($this->bits[$offset])) {
- $this->bits[$offset] = 0;
- }
-
-
- return (uRShift($this->bits[$offset], ($x & 0x1f)) & 1) != 0;
- }
-
-
-
- public function _clone()
- {
- return new BitMatrix($this->width, $this->height, $this->rowSize, $this->bits);
- }
- }
|