Beispiel #1
0
 public function testGetNextSet5()
 {
     mt_srand(hexdec('deadbeef'));
     for ($i = 0; $i < 10; $i++) {
         $array = new BitArray(mt_rand(1, 100));
         $numSet = mt_rand(0, 19);
         for ($j = 0; $j < $numSet; $j++) {
             $array->set(mt_rand(0, $array->getSize() - 1));
         }
         $numQueries = mt_rand(0, 19);
         for ($j = 0; $j < $numQueries; $j++) {
             $query = mt_rand(0, $array->getSize() - 1);
             $expected = $query;
             while ($expected < $array->getSize() && !$array->get($expected)) {
                 $expected++;
             }
             $actual = $array->getNextSet($query);
             if ($actual !== $expected) {
                 $array->getNextSet($query);
             }
             $this->assertEquals($expected, $actual);
         }
     }
 }
Beispiel #2
0
 /**
  * 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;
 }
Beispiel #3
0
 /**
  * Terminates the bits in a bit array.
  *
  * @param  integer  $numDataBytes
  * @param  BitArray $bits
  * @throws Exception\WriterException
  */
 protected static function terminateBits($numDataBytes, BitArray $bits)
 {
     $capacity = $numDataBytes << 3;
     if ($bits->getSize() > $capacity) {
         throw new Exception\WriterException('Data bits cannot fit in the QR code');
     }
     for ($i = 0; $i < 4 && $bits->getSize() < $capacity; $i++) {
         $bits->appendBit(false);
     }
     $numBitsInLastByte = $bits->getSize() & 0x7;
     if ($numBitsInLastByte > 0) {
         for ($i = $numBitsInLastByte; $i < 8; $i++) {
             $bits->appendBit(false);
         }
     }
     $numPaddingBytes = $numDataBytes - $bits->getSizeInBytes();
     for ($i = 0; $i < $numPaddingBytes; $i++) {
         $bits->appendBits(($i & 0x1) === 0 ? 0xec : 0x11, 8);
     }
     if ($bits->getSize() !== $capacity) {
         throw new Exception\WriterException('Bits size does not equal capacity');
     }
 }
Beispiel #4
0
 /**
  * Appends another bit array to this array.
  *
  * @param  BitArray $other
  * @return void
  */
 public function appendBitArray(self $other)
 {
     $otherSize = $other->getSize();
     $this->ensureCapacity($this->size + $other->getSize());
     for ($i = 0; $i < $otherSize; $i++) {
         $this->appendBit($other->get($i));
     }
 }
Beispiel #5
0
 /**
  * Embeds "dataBits" using "getMaskPattern".
  *
  *  For debugging purposes, it skips masking process if "getMaskPattern" is
  * -1. See 8.7 of JISX0510:2004 (p.38) for how to embed data bits.
  *
  * @param  BitArray   $dataBits
  * @param  integer    $maskPattern
  * @param  ByteMatrix $matrix
  * @return void
  * @throws Exception\WriterException
  */
 protected static function embedDataBits(BitArray $dataBits, $maskPattern, ByteMatrix $matrix)
 {
     $bitIndex = 0;
     $direction = -1;
     // Start from the right bottom cell.
     $x = $matrix->getWidth() - 1;
     $y = $matrix->getHeight() - 1;
     while ($x > 0) {
         // Skip vertical timing pattern.
         if ($x === 6) {
             $x--;
         }
         while ($y >= 0 && $y < $matrix->getHeight()) {
             for ($i = 0; $i < 2; $i++) {
                 $xx = $x - $i;
                 // Skip the cell if it's not empty.
                 if ($matrix->get($xx, $y) !== -1) {
                     continue;
                 }
                 if ($bitIndex < $dataBits->getSize()) {
                     $bit = $dataBits->get($bitIndex);
                     $bitIndex++;
                 } else {
                     // Padding bit. If there is no bit left, we'll fill the
                     // left cells with 0, as described in 8.4.9 of
                     // JISX0510:2004 (p. 24).
                     $bit = false;
                 }
                 // Skip masking if maskPattern is -1.
                 if ($maskPattern !== -1 && MaskUtil::getDataMaskBit($maskPattern, $xx, $y)) {
                     $bit = !$bit;
                 }
                 $matrix->set($xx, $y, $bit);
             }
             $y += $direction;
         }
         $direction = -$direction;
         $y += $direction;
         $x -= 2;
     }
     // All bits should be consumed
     if ($bitIndex !== $dataBits->getSize()) {
         throw new Exception\WriterException('Not all bits consumed (' . $bitIndex . ' out of ' . $dataBits->getSize() . ')');
     }
 }