BitMatrix.php 8.23 KB
Newer Older
Hamza Arfaoui's avatar
Hamza Arfaoui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
<?php
/**
 * BaconQrCode
 *
 * @link      http://github.com/Bacon/BaconQrCode For the canonical source repository
 * @copyright 2013 Ben 'DASPRiD' Scholzen
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
 */

namespace BaconQrCode\Common;

use SplFixedArray;

/**
 * Bit matrix.
 *
 * Represents a 2D matrix of bits. In function arguments below, and throughout
 * the common module, x is the column position, and y is the row position. The
 * ordering is always x, y. The origin is at the top-left.
 */
class BitMatrix
{
    /**
     * Width of the bit matrix.
     *
     * @var integer
     */
    protected $width;

    /**
     * Height of the bit matrix.
     *
     * @var integer
     */
    protected $height;

    /**
     * Size in bits of each individual row.
     *
     * @var integer
     */
    protected $rowSize;

    /**
     * Bits representation.
     *
     * @var SplFixedArray
     */
    protected $bits;

    /**
     * Creates a new bit matrix with given dimensions.
     *
     * @param  integer      $width
     * @param  integer|null $height
     * @throws Exception\InvalidArgumentException
     */
    public function __construct($width, $height = null)
    {
        if ($height === null) {
            $height = $width;
        }

        if ($width < 1 || $height < 1) {
            throw new Exception\InvalidArgumentException('Both dimensions must be greater than zero');
        }

        $this->width   = $width;
        $this->height  = $height;
        $this->rowSize = ($width + 31) >> 5;
        $this->bits    = new SplFixedArray($this->rowSize * $height);
    }

    /**
     * Gets the requested bit, where true means black.
     *
     * @param  integer $x
     * @param  integer $y
     * @return boolean
     */
    public function get($x, $y)
    {
        $offset = $y * $this->rowSize + ($x >> 5);
        return (BitUtils::unsignedRightShift($this->bits[$offset], ($x & 0x1f)) & 1) !== 0;
    }

    /**
     * Sets the given bit to true.
     *
     * @param  integer $x
     * @param  integer $y
     * @return void
     */
    public function set($x, $y)
    {
        $offset = $y * $this->rowSize + ($x >> 5);
        $this->bits[$offset] = $this->bits[$offset] | (1 << ($x & 0x1f));
    }

    /**
     * Flips the given bit.
     *
     * @param  integer $x
     * @param  integer $y
     * @return void
     */
    public function flip($x, $y)
    {
        $offset = $y * $this->rowSize + ($x >> 5);
        $this->bits[$offset] = $this->bits[$offset] ^ (1 << ($x & 0x1f));
    }

    /**
     * Clears all bits (set to false).
     *
     * @return void
     */
    public function clear()
    {
        $max = count($this->bits);

        for ($i = 0; $i < $max; $i++) {
            $this->bits[$i] = 0;
        }
    }

    /**
     * Sets a square region of the bit matrix to true.
     *
     * @param  integer $left
     * @param  integer $top
     * @param  integer $width
     * @param  integer $height
     * @return void
     */
    public function setRegion($left, $top, $width, $height)
    {
        if ($top < 0 || $left < 0) {
            throw new Exception\InvalidArgumentException('Left and top must be non-negative');
        }

        if ($height < 1 || $width < 1) {
            throw new Exception\InvalidArgumentException('Width and height must be at least 1');
        }

        $right  = $left + $width;
        $bottom = $top + $height;

        if ($bottom > $this->height || $right > $this->width) {
            throw new Exception\InvalidArgumentException('The region must fit inside the matrix');
        }

        for ($y = $top; $y < $bottom; $y++) {
            $offset = $y * $this->rowSize;

            for ($x = $left; $x < $right; $x++) {
                $index = $offset + ($x >> 5);
                $this->bits[$index] = $this->bits[$index] | (1 << ($x & 0x1f));
            }
        }
    }

    /**
     * A fast method to retrieve one row of data from the matrix as a BitArray.
     *
     * @param  integer  $y
     * @param  BitArray $row
     * @return BitArray
     */
    public function getRow($y, BitArray $row = null)
    {
        if ($row === null || $row->getSize() < $this->width) {
            $row = new BitArray($this->width);
        }

        $offset = $y * $this->rowSize;

        for ($x = 0; $x < $this->rowSize; $x++) {
            $row->setBulk($x << 5, $this->bits[$offset + $x]);
        }

        return $row;
    }

    /**
     * Sets a row of data from a BitArray.
     *
     * @param  integer  $y
     * @param  BitArray $row
     * @return void
     */
    public function setRow($y, BitArray $row)
    {
        $bits = $row->getBitArray();

        for ($i = 0; $i < $this->rowSize; $i++) {
            $this->bits[$y * $this->rowSize + $i] = $bits[$i];
        }
    }

    /**
     * This is useful in detecting the enclosing rectangle of a 'pure' barcode.
     *
     * @return SplFixedArray
     */
    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++) {
                $bits = $this->bits[$y * $this->rowSize + $x32];

                if ($bits !== 0) {
                    if ($y < $top) {
                        $top = $y;
                    }

                    if ($y > $bottom) {
                        $bottom = $y;
                    }

                    if ($x32 * 32 < $left) {
                        $bit = 0;

                        while (($bits << (31 - $bit)) === 0) {
                            $bit++;
                        }

                        if (($x32 * 32 + $bit) < $left) {
                            $left = $x32 * 32 + $bit;
                        }
                    }
                }

                if ($x32 * 32 + 31 > $right) {
                    $bit = 31;

                    while (BitUtils::unsignedRightShift($bits, $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 SplFixedArray::fromArray(array($left, $top, $width, $height), false);
    }

    /**
     * Gets the most top left set bit.
     *
     * This is useful in detecting a corner of a 'pure' barcode.
     *
     * @return SplFixedArray
     */
    public function getTopLeftOnBit()
    {
        $bitsOffset = 0;

        while ($bitsOffset < count($this->bits) && $this->bits[$bitsOffset] === 0) {
            $bitsOffset++;
        }

        if ($bitsOffset === count($this->bits)) {
            return null;
        }

        $x = intval($bitsOffset / $this->rowSize);
        $y = ($bitsOffset % $this->rowSize) << 5;

        $bits = $this->bits[$bitsOffset];
        $bit  = 0;

        while (($bits << (31 - $bit)) === 0) {
            $bit++;
        }

        $x += $bit;

        return SplFixedArray::fromArray(array($x, $y), false);
    }

    /**
     * Gets the most bottom right set bit.
     *
     * This is useful in detecting a corner of a 'pure' barcode.
     *
     * @return SplFixedArray
     */
    public function getBottomRightOnBit()
    {
        $bitsOffset = count($this->bits) - 1;

        while ($bitsOffset >= 0 && $this->bits[$bitsOffset] === 0) {
            $bitsOffset--;
        }

        if ($bitsOffset < 0) {
            return null;
        }

        $x = intval($bitsOffset / $this->rowSize);
        $y = ($bitsOffset % $this->rowSize) << 5;

        $bits = $this->bits[$bitsOffset];
        $bit  = 0;

        while (BitUtils::unsignedRightShift($bits, $bit) === 0) {
            $bit--;
        }

        $x += $bit;

        return SplFixedArray::fromArray(array($x, $y), false);
    }

    /**
     * Gets the width of the matrix,
     *
     * @return integer
     */
    public function getWidth()
    {
        return $this->width;
    }

    /**
     * Gets the height of the matrix.
     *
     * @return integer
     */
    public function getHeight()
    {
        return $this->height;
    }
}