|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Zxing\Common; |
|
|
|
|
|
use Zxing\Binarizer; |
|
|
use Zxing\LuminanceSource; |
|
|
use Zxing\NotFoundException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final class HybridBinarizer extends GlobalHistogramBinarizer |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
private static $BLOCK_SIZE_POWER = 3; |
|
|
private static $BLOCK_SIZE = 8; |
|
|
private static $BLOCK_SIZE_MASK = 7; |
|
|
private static $MINIMUM_DIMENSION = 40; |
|
|
private static $MIN_DYNAMIC_RANGE = 24; |
|
|
|
|
|
private $matrix; |
|
|
|
|
|
public function __construct($source) |
|
|
{ |
|
|
parent::__construct($source); |
|
|
self::$BLOCK_SIZE_POWER = 3; |
|
|
self::$BLOCK_SIZE = 1 << self::$BLOCK_SIZE_POWER; |
|
|
self::$BLOCK_SIZE_MASK = self::$BLOCK_SIZE - 1; |
|
|
self::$MINIMUM_DIMENSION = self::$BLOCK_SIZE * 5; |
|
|
self::$MIN_DYNAMIC_RANGE = 24; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getBlackMatrix() |
|
|
{ |
|
|
if ($this->matrix !== null) { |
|
|
return $this->matrix; |
|
|
} |
|
|
$source = $this->getLuminanceSource(); |
|
|
$width = $source->getWidth(); |
|
|
$height = $source->getHeight(); |
|
|
if ($width >= self::$MINIMUM_DIMENSION && $height >= self::$MINIMUM_DIMENSION) { |
|
|
$luminances = $source->getMatrix(); |
|
|
$subWidth = $width >> self::$BLOCK_SIZE_POWER; |
|
|
if (($width & self::$BLOCK_SIZE_MASK) != 0) { |
|
|
$subWidth++; |
|
|
} |
|
|
$subHeight = $height >> self::$BLOCK_SIZE_POWER; |
|
|
if (($height & self::$BLOCK_SIZE_MASK) != 0) { |
|
|
$subHeight++; |
|
|
} |
|
|
$blackPoints = self::calculateBlackPoints($luminances, $subWidth, $subHeight, $width, $height); |
|
|
|
|
|
$newMatrix = new BitMatrix($width, $height); |
|
|
self::calculateThresholdForBlock($luminances, $subWidth, $subHeight, $width, $height, $blackPoints, $newMatrix); |
|
|
$this->matrix = $newMatrix; |
|
|
} else { |
|
|
|
|
|
$this->matrix = parent::getBlackMatrix(); |
|
|
} |
|
|
|
|
|
return $this->matrix; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static function calculateBlackPoints( |
|
|
$luminances, |
|
|
$subWidth, |
|
|
$subHeight, |
|
|
$width, |
|
|
$height |
|
|
) { |
|
|
$blackPoints = fill_array(0, $subHeight, 0); |
|
|
foreach ($blackPoints as $key => $point) { |
|
|
$blackPoints[$key] = fill_array(0, $subWidth, 0); |
|
|
} |
|
|
for ($y = 0; $y < $subHeight; $y++) { |
|
|
$yoffset = ($y << self::$BLOCK_SIZE_POWER); |
|
|
$maxYOffset = $height - self::$BLOCK_SIZE; |
|
|
if ($yoffset > $maxYOffset) { |
|
|
$yoffset = $maxYOffset; |
|
|
} |
|
|
for ($x = 0; $x < $subWidth; $x++) { |
|
|
$xoffset = ($x << self::$BLOCK_SIZE_POWER); |
|
|
$maxXOffset = $width - self::$BLOCK_SIZE; |
|
|
if ($xoffset > $maxXOffset) { |
|
|
$xoffset = $maxXOffset; |
|
|
} |
|
|
$sum = 0; |
|
|
$min = 0xFF; |
|
|
$max = 0; |
|
|
for ($yy = 0, $offset = $yoffset * $width + $xoffset; $yy < self::$BLOCK_SIZE; $yy++, $offset += $width) { |
|
|
for ($xx = 0; $xx < self::$BLOCK_SIZE; $xx++) { |
|
|
$pixel = ((int)($luminances[(int)($offset + $xx)]) & 0xFF); |
|
|
$sum += $pixel; |
|
|
|
|
|
if ($pixel < $min) { |
|
|
$min = $pixel; |
|
|
} |
|
|
if ($pixel > $max) { |
|
|
$max = $pixel; |
|
|
} |
|
|
} |
|
|
|
|
|
if ($max - $min > self::$MIN_DYNAMIC_RANGE) { |
|
|
|
|
|
for ($yy++, $offset += $width; $yy < self::$BLOCK_SIZE; $yy++, $offset += $width) { |
|
|
for ($xx = 0; $xx < self::$BLOCK_SIZE; $xx++) { |
|
|
$sum += ($luminances[$offset + $xx] & 0xFF); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$average = ($sum >> (self::$BLOCK_SIZE_POWER * 2)); |
|
|
if ($max - $min <= self::$MIN_DYNAMIC_RANGE) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$average = (int)($min / 2); |
|
|
|
|
|
if ($y > 0 && $x > 0) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$averageNeighborBlackPoint = |
|
|
(int)(($blackPoints[$y - 1][$x] + (2 * $blackPoints[$y][$x - 1]) + $blackPoints[$y - 1][$x - 1]) / 4); |
|
|
if ($min < $averageNeighborBlackPoint) { |
|
|
$average = $averageNeighborBlackPoint; |
|
|
} |
|
|
} |
|
|
} |
|
|
$blackPoints[$y][$x] = (int)($average); |
|
|
} |
|
|
} |
|
|
|
|
|
return $blackPoints; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static function calculateThresholdForBlock( |
|
|
$luminances, |
|
|
$subWidth, |
|
|
$subHeight, |
|
|
$width, |
|
|
$height, |
|
|
$blackPoints, |
|
|
$matrix |
|
|
) { |
|
|
for ($y = 0; $y < $subHeight; $y++) { |
|
|
$yoffset = ($y << self::$BLOCK_SIZE_POWER); |
|
|
$maxYOffset = $height - self::$BLOCK_SIZE; |
|
|
if ($yoffset > $maxYOffset) { |
|
|
$yoffset = $maxYOffset; |
|
|
} |
|
|
for ($x = 0; $x < $subWidth; $x++) { |
|
|
$xoffset = ($x << self::$BLOCK_SIZE_POWER); |
|
|
$maxXOffset = $width - self::$BLOCK_SIZE; |
|
|
if ($xoffset > $maxXOffset) { |
|
|
$xoffset = $maxXOffset; |
|
|
} |
|
|
$left = self::cap($x, 2, $subWidth - 3); |
|
|
$top = self::cap($y, 2, $subHeight - 3); |
|
|
$sum = 0; |
|
|
for ($z = -2; $z <= 2; $z++) { |
|
|
$blackRow = $blackPoints[$top + $z]; |
|
|
$sum += $blackRow[$left - 2] + $blackRow[$left - 1] + $blackRow[$left] + $blackRow[$left + 1] + $blackRow[$left + 2]; |
|
|
} |
|
|
$average = (int)($sum / 25); |
|
|
|
|
|
self::thresholdBlock($luminances, $xoffset, $yoffset, $average, $width, $matrix); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
private static function cap($value, $min, $max) |
|
|
{ |
|
|
if ($value < $min) { |
|
|
return $min; |
|
|
} elseif ($value > $max) { |
|
|
return $max; |
|
|
} else { |
|
|
return $value; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static function thresholdBlock( |
|
|
$luminances, |
|
|
$xoffset, |
|
|
$yoffset, |
|
|
$threshold, |
|
|
$stride, |
|
|
$matrix |
|
|
) { |
|
|
|
|
|
for ($y = 0, $offset = $yoffset * $stride + $xoffset; $y < self::$BLOCK_SIZE; $y++, $offset += $stride) { |
|
|
for ($x = 0; $x < self::$BLOCK_SIZE; $x++) { |
|
|
|
|
|
if (($luminances[$offset + $x] & 0xFF) <= $threshold) { |
|
|
$matrix->set($xoffset + $x, $yoffset + $y); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
public function createBinarizer($source) |
|
|
{ |
|
|
return new HybridBinarizer($source); |
|
|
} |
|
|
} |
|
|
|