public function testApplyMaskPenaltyRule4() { // Dark cell ratio = 0% $matrix = new ByteMatrix(1, 1); $matrix->set(0, 0, 0); $this->assertEquals(100, MaskUtil::applyMaskPenaltyRule4($matrix)); // Dark cell ratio = 5% $matrix = new ByteMatrix(2, 1); $matrix->set(0, 0, 0); $matrix->set(0, 0, 1); $this->assertEquals(0, MaskUtil::applyMaskPenaltyRule4($matrix)); // Dark cell ratio = 66.67% $matrix = new ByteMatrix(6, 1); $matrix->set(0, 0, 0); $matrix->set(1, 0, 1); $matrix->set(2, 0, 1); $matrix->set(3, 0, 1); $matrix->set(4, 0, 1); $matrix->set(5, 0, 0); $this->assertEquals(30, MaskUtil::applyMaskPenaltyRule4($matrix)); }
/** * Calculates the mask penalty for a matrix. * * @param ByteMatrix $matrix * @return integer */ protected static function calculateMaskPenalty(ByteMatrix $matrix) { return MaskUtil::applyMaskPenaltyRule1($matrix) + MaskUtil::applyMaskPenaltyRule2($matrix) + MaskUtil::applyMaskPenaltyRule3($matrix) + MaskUtil::applyMaskPenaltyRule4($matrix); }
/** * 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() . ')'); } }