| 1 |
<?php |
| 2 |
declare(strict_types = 1); |
| 3 |
|
| 4 |
namespace BaconQrCode\Renderer\Module\EdgeIterator; |
| 5 |
|
| 6 |
use BaconQrCode\Encoder\ByteMatrix; |
| 7 |
use IteratorAggregate; |
| 8 |
use Traversable; |
| 9 |
|
| 10 |
/** |
| 11 |
* Edge iterator based on potrace. |
| 12 |
*/ |
| 13 |
final class EdgeIterator implements IteratorAggregate |
| 14 |
{ |
| 15 |
/** |
| 16 |
* @var int[] |
| 17 |
*/ |
| 18 |
private array $bytes = []; |
| 19 |
|
| 20 |
private ?int $size; |
| 21 |
|
| 22 |
private int $width; |
| 23 |
|
| 24 |
private int $height; |
| 25 |
|
| 26 |
public function __construct(ByteMatrix $matrix) |
| 27 |
{ |
| 28 |
$this->bytes = iterator_to_array($matrix->getBytes()); |
| 29 |
$this->size = count($this->bytes); |
| 30 |
$this->width = $matrix->getWidth(); |
| 31 |
$this->height = $matrix->getHeight(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @return Traversable<Edge> |
| 36 |
*/ |
| 37 |
public function getIterator() : Traversable |
| 38 |
{ |
| 39 |
$originalBytes = $this->bytes; |
| 40 |
$point = $this->findNext(0, 0); |
| 41 |
|
| 42 |
while (null !== $point) { |
| 43 |
$edge = $this->findEdge($point[0], $point[1]); |
| 44 |
$this->xorEdge($edge); |
| 45 |
|
| 46 |
yield $edge; |
| 47 |
|
| 48 |
$point = $this->findNext($point[0], $point[1]); |
| 49 |
} |
| 50 |
|
| 51 |
$this->bytes = $originalBytes; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @return int[]|null |
| 56 |
*/ |
| 57 |
private function findNext(int $x, int $y) : ?array |
| 58 |
{ |
| 59 |
$i = $this->width * $y + $x; |
| 60 |
|
| 61 |
while ($i < $this->size && 1 !== $this->bytes[$i]) { |
| 62 |
++$i; |
| 63 |
} |
| 64 |
|
| 65 |
if ($i < $this->size) { |
| 66 |
return $this->pointOf($i); |
| 67 |
} |
| 68 |
|
| 69 |
return null; |
| 70 |
} |
| 71 |
|
| 72 |
private function findEdge(int $x, int $y) : Edge |
| 73 |
{ |
| 74 |
$edge = new Edge($this->isSet($x, $y)); |
| 75 |
$startX = $x; |
| 76 |
$startY = $y; |
| 77 |
$dirX = 0; |
| 78 |
$dirY = 1; |
| 79 |
|
| 80 |
while (true) { |
| 81 |
$edge->addPoint($x, $y); |
| 82 |
$x += $dirX; |
| 83 |
$y += $dirY; |
| 84 |
|
| 85 |
if ($x === $startX && $y === $startY) { |
| 86 |
break; |
| 87 |
} |
| 88 |
|
| 89 |
$left = $this->isSet($x + ($dirX + $dirY - 1 ) / 2, $y + ($dirY - $dirX - 1) / 2); |
| 90 |
$right = $this->isSet($x + ($dirX - $dirY - 1) / 2, $y + ($dirY + $dirX - 1) / 2); |
| 91 |
|
| 92 |
if ($right && ! $left) { |
| 93 |
$tmp = $dirX; |
| 94 |
$dirX = -$dirY; |
| 95 |
$dirY = $tmp; |
| 96 |
} elseif ($right) { |
| 97 |
$tmp = $dirX; |
| 98 |
$dirX = -$dirY; |
| 99 |
$dirY = $tmp; |
| 100 |
} elseif (! $left) { |
| 101 |
$tmp = $dirX; |
| 102 |
$dirX = $dirY; |
| 103 |
$dirY = -$tmp; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return $edge; |
| 108 |
} |
| 109 |
|
| 110 |
private function xorEdge(Edge $path) : void |
| 111 |
{ |
| 112 |
$points = $path->getPoints(); |
| 113 |
$y1 = $points[0][1]; |
| 114 |
$length = count($points); |
| 115 |
$maxX = $path->getMaxX(); |
| 116 |
|
| 117 |
for ($i = 1; $i < $length; ++$i) { |
| 118 |
$y = $points[$i][1]; |
| 119 |
|
| 120 |
if ($y === $y1) { |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
$x = $points[$i][0]; |
| 125 |
$minY = min($y1, $y); |
| 126 |
|
| 127 |
for ($j = $x; $j < $maxX; ++$j) { |
| 128 |
$this->flip($j, $minY); |
| 129 |
} |
| 130 |
|
| 131 |
$y1 = $y; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
private function isSet(int $x, int $y) : bool |
| 136 |
{ |
| 137 |
return ( |
| 138 |
$x >= 0 |
| 139 |
&& $x < $this->width |
| 140 |
&& $y >= 0 |
| 141 |
&& $y < $this->height |
| 142 |
) && 1 === $this->bytes[$this->width * $y + $x]; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @return int[] |
| 147 |
*/ |
| 148 |
private function pointOf(int $i) : array |
| 149 |
{ |
| 150 |
$y = intdiv($i, $this->width); |
| 151 |
return [$i - $y * $this->width, $y]; |
| 152 |
} |
| 153 |
|
| 154 |
private function flip(int $x, int $y) : void |
| 155 |
{ |
| 156 |
$this->bytes[$this->width * $y + $x] = ( |
| 157 |
$this->isSet($x, $y) ? 0 : 1 |
| 158 |
); |
| 159 |
} |
| 160 |
} |