GridSampler.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Class GridSampler
  4. *
  5. * @created 17.01.2021
  6. * @author ZXing Authors
  7. * @author Smiley <smiley@chillerlan.net>
  8. * @copyright 2021 Smiley
  9. * @license Apache-2.0
  10. */
  11. namespace chillerlan\QRCode\Detector;
  12. use chillerlan\QRCode\Data\QRMatrix;
  13. use chillerlan\QRCode\Decoder\BitMatrix;
  14. use function array_fill, count, intdiv, sprintf;
  15. /**
  16. * Implementations of this class can, given locations of finder patterns for a QR code in an
  17. * image, sample the right points in the image to reconstruct the QR code, accounting for
  18. * perspective distortion. It is abstracted since it is relatively expensive and should be allowed
  19. * to take advantage of platform-specific optimized implementations, like Sun's Java Advanced
  20. * Imaging library, but which may not be available in other environments such as J2ME, and vice
  21. * versa.
  22. *
  23. * The implementation used can be controlled by calling #setGridSampler(GridSampler)
  24. * with an instance of a class which implements this interface.
  25. *
  26. * @author Sean Owen
  27. */
  28. final class GridSampler{
  29. /** @var float[] */
  30. private array $points;
  31. /**
  32. * Checks a set of points that have been transformed to sample points on an image against
  33. * the image's dimensions to see if the point are even within the image.
  34. *
  35. * This method will actually "nudge" the endpoints back onto the image if they are found to be
  36. * barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder
  37. * patterns in an image where the QR Code runs all the way to the image border.
  38. *
  39. * For efficiency, the method will check points from either end of the line until one is found
  40. * to be within the image. Because the set of points are assumed to be linear, this is valid.
  41. *
  42. * @param int $dimension matrix width/height
  43. *
  44. * @throws \chillerlan\QRCode\Detector\QRCodeDetectorException if an endpoint is lies outside the image boundaries
  45. */
  46. private function checkAndNudgePoints(int $dimension):void{
  47. $nudged = true;
  48. $max = count($this->points);
  49. // Check and nudge points from start until we see some that are OK:
  50. for($offset = 0; $offset < $max && $nudged; $offset += 2){
  51. $x = (int)$this->points[$offset];
  52. $y = (int)$this->points[($offset + 1)];
  53. if($x < -1 || $x > $dimension || $y < -1 || $y > $dimension){
  54. throw new QRCodeDetectorException(sprintf('checkAndNudgePoints 1, x: %s, y: %s, d: %s', $x, $y, $dimension));
  55. }
  56. $nudged = false;
  57. if($x === -1){
  58. $this->points[$offset] = 0.0;
  59. $nudged = true;
  60. }
  61. elseif($x === $dimension){
  62. $this->points[$offset] = ($dimension - 1);
  63. $nudged = true;
  64. }
  65. if($y === -1){
  66. $this->points[($offset + 1)] = 0.0;
  67. $nudged = true;
  68. }
  69. elseif($y === $dimension){
  70. $this->points[($offset + 1)] = ($dimension - 1);
  71. $nudged = true;
  72. }
  73. }
  74. // Check and nudge points from end:
  75. $nudged = true;
  76. for($offset = ($max - 2); $offset >= 0 && $nudged; $offset -= 2){
  77. $x = (int)$this->points[$offset];
  78. $y = (int)$this->points[($offset + 1)];
  79. if($x < -1 || $x > $dimension || $y < -1 || $y > $dimension){
  80. throw new QRCodeDetectorException(sprintf('checkAndNudgePoints 2, x: %s, y: %s, d: %s', $x, $y, $dimension));
  81. }
  82. $nudged = false;
  83. if($x === -1){
  84. $this->points[$offset] = 0.0;
  85. $nudged = true;
  86. }
  87. elseif($x === $dimension){
  88. $this->points[$offset] = ($dimension - 1);
  89. $nudged = true;
  90. }
  91. if($y === -1){
  92. $this->points[($offset + 1)] = 0.0;
  93. $nudged = true;
  94. }
  95. elseif($y === $dimension){
  96. $this->points[($offset + 1)] = ($dimension - 1);
  97. $nudged = true;
  98. }
  99. }
  100. }
  101. /**
  102. * Samples an image for a rectangular matrix of bits of the given dimension. The sampling
  103. * transformation is determined by the coordinates of 4 points, in the original and transformed
  104. * image space.
  105. *
  106. * @return \chillerlan\QRCode\Decoder\BitMatrix representing a grid of points sampled from the image within a region
  107. * defined by the "from" parameters
  108. * @throws \chillerlan\QRCode\Detector\QRCodeDetectorException if image can't be sampled, for example, if the transformation defined
  109. * by the given points is invalid or results in sampling outside the image boundaries
  110. */
  111. public function sampleGrid(BitMatrix $matrix, int $dimension, PerspectiveTransform $transform):BitMatrix{
  112. if($dimension <= 0){
  113. throw new QRCodeDetectorException('invalid matrix size');
  114. }
  115. $bits = new BitMatrix($dimension);
  116. $this->points = array_fill(0, (2 * $dimension), 0.0);
  117. for($y = 0; $y < $dimension; $y++){
  118. $max = count($this->points);
  119. $iValue = ($y + 0.5);
  120. for($x = 0; $x < $max; $x += 2){
  121. $this->points[$x] = (($x / 2) + 0.5);
  122. $this->points[($x + 1)] = $iValue;
  123. }
  124. // phpcs:ignore
  125. [$this->points, ] = $transform->transformPoints($this->points);
  126. // Quick check to see if points transformed to something inside the image;
  127. // sufficient to check the endpoints
  128. $this->checkAndNudgePoints($matrix->getSize());
  129. // no need to try/catch as QRMatrix::set() will silently discard out of bounds values
  130. # try{
  131. for($x = 0; $x < $max; $x += 2){
  132. // Black(-ish) pixel
  133. $bits->set(
  134. intdiv($x, 2),
  135. $y,
  136. $matrix->check((int)$this->points[$x], (int)$this->points[($x + 1)]),
  137. QRMatrix::M_DATA
  138. );
  139. }
  140. # }
  141. # catch(\Throwable $aioobe){//ArrayIndexOutOfBoundsException
  142. // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
  143. // transform gets "twisted" such that it maps a straight line of points to a set of points
  144. // whose endpoints are in bounds, but others are not. There is probably some mathematical
  145. // way to detect this about the transformation that I don't know yet.
  146. // This results in an ugly runtime exception despite our clever checks above -- can't have
  147. // that. We could check each point's coordinates but that feels duplicative. We settle for
  148. // catching and wrapping ArrayIndexOutOfBoundsException.
  149. # throw new QRCodeDetectorException('ArrayIndexOutOfBoundsException');
  150. # }
  151. }
  152. return $bits;
  153. }
  154. }