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