123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?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);
- }
- }
|