| <?php |
|
|
|
|
| namespace Zxing; |
|
|
| |
| |
| |
| |
| |
| |
| |
| final class GDLuminanceSource extends LuminanceSource |
| { |
| public $luminances; |
| private $dataWidth; |
| private $dataHeight; |
| private $left; |
| private $top; |
| private $gdImage; |
|
|
| public function __construct( |
| $gdImage, |
| $dataWidth, |
| $dataHeight, |
| $left = null, |
| $top = null, |
| $width = null, |
| $height = null |
| ) { |
| if (!$left && !$top && !$width && !$height) { |
| $this->GDLuminanceSource($gdImage, $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 = $gdImage; |
| $this->dataWidth = $dataWidth; |
| $this->dataHeight = $dataHeight; |
| $this->left = $left; |
| $this->top = $top; |
| } |
|
|
| public function GDLuminanceSource($gdImage, $width, $height) |
| { |
| parent::__construct($width, $height); |
|
|
| $this->dataWidth = $width; |
| $this->dataHeight = $height; |
| $this->left = 0; |
| $this->top = 0; |
| $this->gdImage = $gdImage; |
|
|
|
|
| |
| |
| $this->luminances = []; |
| |
|
|
| $array = []; |
| $rgb = []; |
|
|
| for ($j = 0; $j < $height; $j++) { |
| for ($i = 0; $i < $width; $i++) { |
| $argb = imagecolorat($this->gdImage, $i, $j); |
| $pixel = imagecolorsforindex($this->gdImage, $argb); |
| $r = $pixel['red']; |
| $g = $pixel['green']; |
| $b = $pixel['blue']; |
| if ($r == $g && $g == $b) { |
| |
|
|
| $this->luminances[] = $r; |
| } else { |
| |
| $this->luminances[] = ($r + 2 * $g + $b) / 4; |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
|
|
| |
| 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 GDLuminanceSource($this->luminances, |
| $this->dataWidth, |
| $this->dataHeight, |
| $this->left + $left, |
| $this->top + $top, |
| $width, |
| $height); |
| } |
| } |
|
|