GridSampler.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 Exception, RuntimeException;
  13. use chillerlan\QRCode\Decoder\BitMatrix;
  14. use function array_fill, count, 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 {@link #setGridSampler(GridSampler)}
  24. * with an instance of a class which implements this interface.
  25. *
  26. * @author Sean Owen
  27. */
  28. final class GridSampler{
  29. /**
  30. * <p>Checks a set of points that have been transformed to sample points on an image against
  31. * the image's dimensions to see if the point are even within the image.</p>
  32. *
  33. * <p>This method will actually "nudge" the endpoints back onto the image if they are found to be
  34. * barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder
  35. * patterns in an image where the QR Code runs all the way to the image border.</p>
  36. *
  37. * <p>For efficiency, the method will check points from either end of the line until one is found
  38. * to be within the image. Because the set of points are assumed to be linear, this is valid.</p>
  39. *
  40. * @param \chillerlan\QRCode\Decoder\BitMatrix $bitMatrix image into which the points should map
  41. * @param float[] $points actual points in x1,y1,...,xn,yn form
  42. *
  43. * @throws \RuntimeException if an endpoint is lies outside the image boundaries
  44. */
  45. private function checkAndNudgePoints(BitMatrix $bitMatrix, array $points):void{
  46. $dimension = $bitMatrix->getDimension();
  47. $nudged = true;
  48. $max = count($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)$points[$offset];
  52. $y = (int)$points[$offset + 1];
  53. if($x < -1 || $x > $dimension || $y < -1 || $y > $dimension){
  54. throw new RuntimeException(sprintf('checkAndNudgePoints 1, x: %s, y: %s, d: %s', $x, $y, $dimension));
  55. }
  56. $nudged = false;
  57. if($x === -1){
  58. $points[$offset] = 0.0;
  59. $nudged = true;
  60. }
  61. elseif($x === $dimension){
  62. $points[$offset] = $dimension - 1;
  63. $nudged = true;
  64. }
  65. if($y === -1){
  66. $points[$offset + 1] = 0.0;
  67. $nudged = true;
  68. }
  69. elseif($y === $dimension){
  70. $points[$offset + 1] = $dimension - 1;
  71. $nudged = true;
  72. }
  73. }
  74. // Check and nudge points from end:
  75. $nudged = true;
  76. for($offset = count($points) - 2; $offset >= 0 && $nudged; $offset -= 2){
  77. $x = (int)$points[$offset];
  78. $y = (int)$points[$offset + 1];
  79. if($x < -1 || $x > $dimension || $y < -1 || $y > $dimension){
  80. throw new RuntimeException(sprintf('checkAndNudgePoints 2, x: %s, y: %s, d: %s', $x, $y, $dimension));
  81. }
  82. $nudged = false;
  83. if($x === -1){
  84. $points[$offset] = 0.0;
  85. $nudged = true;
  86. }
  87. elseif($x === $dimension){
  88. $points[$offset] = $dimension - 1;
  89. $nudged = true;
  90. }
  91. if($y === -1){
  92. $points[$offset + 1] = 0.0;
  93. $nudged = true;
  94. }
  95. elseif($y === $dimension){
  96. $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 \RuntimeException 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 $image, int $dimension, PerspectiveTransform $transform):BitMatrix{
  112. if($dimension <= 0){
  113. throw new RuntimeException('invalid matrix size');
  114. }
  115. $bits = new BitMatrix($dimension);
  116. $points = array_fill(0, 2 * $dimension, 0.0);
  117. for($y = 0; $y < $dimension; $y++){
  118. $max = count($points);
  119. $iValue = $y + 0.5;
  120. for($x = 0; $x < $max; $x += 2){
  121. $points[$x] = ($x / 2) + 0.5;
  122. $points[$x + 1] = $iValue;
  123. }
  124. $transform->transformPoints($points);
  125. // Quick check to see if points transformed to something inside the image;
  126. // sufficient to check the endpoints
  127. $this->checkAndNudgePoints($image, $points);
  128. try{
  129. for($x = 0; $x < $max; $x += 2){
  130. if($image->get((int)$points[$x], (int)$points[$x + 1])){
  131. // Black(-ish) pixel
  132. $bits->set($x / 2, $y);
  133. }
  134. }
  135. }
  136. catch(Exception $aioobe){//ArrayIndexOutOfBoundsException
  137. // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
  138. // transform gets "twisted" such that it maps a straight line of points to a set of points
  139. // whose endpoints are in bounds, but others are not. There is probably some mathematical
  140. // way to detect this about the transformation that I don't know yet.
  141. // This results in an ugly runtime exception despite our clever checks above -- can't have
  142. // that. We could check each point's coordinates but that feels duplicative. We settle for
  143. // catching and wrapping ArrayIndexOutOfBoundsException.
  144. throw new RuntimeException('ArrayIndexOutOfBoundsException');
  145. }
  146. }
  147. return $bits;
  148. }
  149. }