|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Zxing; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final class RGBLuminanceSource extends LuminanceSource |
|
|
{ |
|
|
public $luminances; |
|
|
private $dataWidth; |
|
|
private $dataHeight; |
|
|
private $left; |
|
|
private $top; |
|
|
private $pixels; |
|
|
|
|
|
|
|
|
public function __construct( |
|
|
$pixels, |
|
|
$dataWidth, |
|
|
$dataHeight, |
|
|
$left = null, |
|
|
$top = null, |
|
|
$width = null, |
|
|
$height = null |
|
|
) { |
|
|
if (!$left && !$top && !$width && !$height) { |
|
|
$this->RGBLuminanceSource_($pixels, $dataWidth, $dataHeight); |
|
|
|
|
|
return; |
|
|
} |
|
|
parent::__construct($width, $height); |
|
|
if ($left + $width > $dataWidth || $top + $height > $dataHeight) { |
|
|
throw new \InvalidArgumentException("Crop rectangle does not fit within image data."); |
|
|
} |
|
|
$this->luminances = $pixels; |
|
|
$this->dataWidth = $dataWidth; |
|
|
$this->dataHeight = $dataHeight; |
|
|
$this->left = $left; |
|
|
$this->top = $top; |
|
|
} |
|
|
|
|
|
public function RGBLuminanceSource_($width, $height, $pixels) |
|
|
{ |
|
|
parent::__construct($width, $height); |
|
|
|
|
|
$this->dataWidth = $width; |
|
|
$this->dataHeight = $height; |
|
|
$this->left = 0; |
|
|
$this->top = 0; |
|
|
$this->pixels = $pixels; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->luminances = []; |
|
|
|
|
|
|
|
|
foreach ($pixels as $key => $pixel) { |
|
|
$r = $pixel['red']; |
|
|
$g = $pixel['green']; |
|
|
$b = $pixel['blue']; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($r == $g && $g == $b) { |
|
|
|
|
|
|
|
|
$this->luminances[$key] = $r; |
|
|
} else { |
|
|
|
|
|
$this->luminances[$key] = ($r + 2 * $g + $b) / 4; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
public function grayscale() |
|
|
{ |
|
|
$width = $this->dataWidth; |
|
|
$height = $this->dataHeight; |
|
|
|
|
|
$ret = fill_array(0, $width * $height, 0); |
|
|
for ($y = 0; $y < $height; $y++) { |
|
|
for ($x = 0; $x < $width; $x++) { |
|
|
$gray = $this->getPixel($x, $y, $width, $height); |
|
|
|
|
|
$ret[$x + $y * $width] = $gray; |
|
|
} |
|
|
} |
|
|
|
|
|
return $ret; |
|
|
} |
|
|
|
|
|
public function getPixel($x, $y, $width, $height) |
|
|
{ |
|
|
$image = $this->pixels; |
|
|
if ($width < $x) { |
|
|
die('error'); |
|
|
} |
|
|
if ($height < $y) { |
|
|
die('error'); |
|
|
} |
|
|
$point = ($x) + ($y * $width); |
|
|
|
|
|
$r = $image[$point]['red']; |
|
|
$g = $image[$point]['green']; |
|
|
$b = $image[$point]['blue']; |
|
|
|
|
|
$p = (int)(($r * 33 + $g * 34 + $b * 33) / 100); |
|
|
|
|
|
|
|
|
return $p; |
|
|
|
|
|
} |
|
|
|
|
|
public function grayScaleToBitmap($grayScale) |
|
|
{ |
|
|
$middle = $this->getMiddleBrightnessPerArea($grayScale); |
|
|
$sqrtNumArea = count($middle); |
|
|
$areaWidth = floor($this->dataWidth / $sqrtNumArea); |
|
|
$areaHeight = floor($this->dataHeight / $sqrtNumArea); |
|
|
$bitmap = fill_array(0, $this->dataWidth * $this->dataHeight, 0); |
|
|
|
|
|
for ($ay = 0; $ay < $sqrtNumArea; $ay++) { |
|
|
for ($ax = 0; $ax < $sqrtNumArea; $ax++) { |
|
|
for ($dy = 0; $dy < $areaHeight; $dy++) { |
|
|
for ($dx = 0; $dx < $areaWidth; $dx++) { |
|
|
$bitmap[(int)($areaWidth * $ax + $dx + ($areaHeight * $ay + $dy) * $this->dataWidth)] = ($grayScale[(int)($areaWidth * $ax + $dx + ($areaHeight * $ay + $dy) * $this->dataWidth)] < $middle[$ax][$ay]) ? 0 : 255; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return $bitmap; |
|
|
} |
|
|
|
|
|
public function getMiddleBrightnessPerArea($image) |
|
|
{ |
|
|
$numSqrtArea = 4; |
|
|
|
|
|
$areaWidth = floor($this->dataWidth / $numSqrtArea); |
|
|
$areaHeight = floor($this->dataHeight / $numSqrtArea); |
|
|
$minmax = fill_array(0, $numSqrtArea, 0); |
|
|
for ($i = 0; $i < $numSqrtArea; $i++) { |
|
|
$minmax[$i] = fill_array(0, $numSqrtArea, 0); |
|
|
for ($i2 = 0; $i2 < $numSqrtArea; $i2++) { |
|
|
$minmax[$i][$i2] = [0, 0]; |
|
|
} |
|
|
} |
|
|
for ($ay = 0; $ay < $numSqrtArea; $ay++) { |
|
|
for ($ax = 0; $ax < $numSqrtArea; $ax++) { |
|
|
$minmax[$ax][$ay][0] = 0xFF; |
|
|
for ($dy = 0; $dy < $areaHeight; $dy++) { |
|
|
for ($dx = 0; $dx < $areaWidth; $dx++) { |
|
|
$target = $image[(int)($areaWidth * $ax + $dx + ($areaHeight * $ay + $dy) * $this->dataWidth)]; |
|
|
if ($target < $minmax[$ax][$ay][0]) |
|
|
$minmax[$ax][$ay][0] = $target; |
|
|
if ($target > $minmax[$ax][$ay][1]) |
|
|
$minmax[$ax][$ay][1] = $target; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
$middle = []; |
|
|
for ($i3 = 0; $i3 < $numSqrtArea; $i3++) { |
|
|
$middle[$i3] = []; |
|
|
} |
|
|
for ($ay = 0; $ay < $numSqrtArea; $ay++) { |
|
|
for ($ax = 0; $ax < $numSqrtArea; $ax++) { |
|
|
$middle[$ax][$ay] = floor(($minmax[$ax][$ay][0] + $minmax[$ax][$ay][1]) / 2); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $middle; |
|
|
} |
|
|
|
|
|
|
|
|
public function getRow($y, $row = null) |
|
|
{ |
|
|
if ($y < 0 || $y >= $this->getHeight()) { |
|
|
throw new \InvalidArgumentException("Requested row is outside the image: " + y); |
|
|
} |
|
|
$width = $this->getWidth(); |
|
|
if ($row == null || count($row) < $width) { |
|
|
$row = []; |
|
|
} |
|
|
$offset = ($y + $this->top) * $this->dataWidth + $this->left; |
|
|
$row = arraycopy($this->luminances, $offset, $row, 0, $width); |
|
|
|
|
|
return $row; |
|
|
} |
|
|
|
|
|
|
|
|
public function getMatrix() |
|
|
{ |
|
|
$width = $this->getWidth(); |
|
|
$height = $this->getHeight(); |
|
|
|
|
|
|
|
|
|
|
|
if ($width == $this->dataWidth && $height == $this->dataHeight) { |
|
|
return $this->luminances; |
|
|
} |
|
|
|
|
|
$area = $width * $height; |
|
|
$matrix = []; |
|
|
$inputOffset = $this->top * $this->dataWidth + $this->left; |
|
|
|
|
|
|
|
|
if ($width == $this->dataWidth) { |
|
|
$matrix = arraycopy($this->luminances, $inputOffset, $matrix, 0, $area); |
|
|
|
|
|
return $matrix; |
|
|
} |
|
|
|
|
|
|
|
|
$rgb = $this->luminances; |
|
|
for ($y = 0; $y < $height; $y++) { |
|
|
$outputOffset = $y * $width; |
|
|
$matrix = arraycopy($rgb, $inputOffset, $matrix, $outputOffset, $width); |
|
|
$inputOffset += $this->dataWidth; |
|
|
} |
|
|
|
|
|
return $matrix; |
|
|
} |
|
|
|
|
|
|
|
|
public function isCropSupported() |
|
|
{ |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
public function crop($left, $top, $width, $height) |
|
|
{ |
|
|
return new RGBLuminanceSource($this->luminances, |
|
|
$this->dataWidth, |
|
|
$this->dataHeight, |
|
|
$this->left + $left, |
|
|
$this->top + $top, |
|
|
$width, |
|
|
$height); |
|
|
} |
|
|
} |
|
|
|