PerspectiveTransform.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /*
  3. * Copyright 2007 ZXing authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Zxing\Common;
  18. /**
  19. * <p>This class implements a perspective transform in two dimensions. Given four source and four
  20. * destination points, it will compute the transformation implied between them. The code is based
  21. * directly upon section 3.4.2 of George Wolberg's "Digital Image Warping"; see pages 54-56.</p>
  22. *
  23. * @author Sean Owen
  24. */
  25. final class PerspectiveTransform
  26. {
  27. private $a11;
  28. private $a12;
  29. private $a13;
  30. private $a21;
  31. private $a22;
  32. private $a23;
  33. private $a31;
  34. private $a32;
  35. private $a33;
  36. private function __construct(
  37. $a11, $a21, $a31,
  38. $a12, $a22, $a32,
  39. $a13, $a23, $a33
  40. ) {
  41. $this->a11 = $a11;
  42. $this->a12 = $a12;
  43. $this->a13 = $a13;
  44. $this->a21 = $a21;
  45. $this->a22 = $a22;
  46. $this->a23 = $a23;
  47. $this->a31 = $a31;
  48. $this->a32 = $a32;
  49. $this->a33 = $a33;
  50. }
  51. public static function quadrilateralToQuadrilateral(
  52. $x0, $y0,
  53. $x1, $y1,
  54. $x2, $y2,
  55. $x3, $y3,
  56. $x0p, $y0p,
  57. $x1p, $y1p,
  58. $x2p, $y2p,
  59. $x3p, $y3p
  60. ) {
  61. $qToS = self::quadrilateralToSquare($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3);
  62. $sToQ = self::squareToQuadrilateral($x0p, $y0p, $x1p, $y1p, $x2p, $y2p, $x3p, $y3p);
  63. return $sToQ->times($qToS);
  64. }
  65. public static function quadrilateralToSquare(
  66. $x0, $y0,
  67. $x1, $y1,
  68. $x2, $y2,
  69. $x3, $y3
  70. ) {
  71. // Here, the adjoint serves as the inverse:
  72. return self::squareToQuadrilateral($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3)->buildAdjoint();
  73. }
  74. public function buildAdjoint()
  75. {
  76. // Adjoint is the transpose of the cofactor matrix:
  77. return new PerspectiveTransform($this->a22 * $this->a33 - $this->a23 * $this->a32,
  78. $this->a23 * $this->a31 - $this->a21 * $this->a33,
  79. $this->a21 * $this->a32 - $this->a22 * $this->a31,
  80. $this->a13 * $this->a32 - $this->a12 * $this->a33,
  81. $this->a11 * $this->a33 - $this->a13 * $this->a31,
  82. $this->a12 * $this->a31 - $this->a11 * $this->a32,
  83. $this->a12 * $this->a23 - $this->a13 * $this->a22,
  84. $this->a13 * $this->a21 - $this->a11 * $this->a23,
  85. $this->a11 * $this->a22 - $this->a12 * $this->a21);
  86. }
  87. public static function squareToQuadrilateral(
  88. $x0, $y0,
  89. $x1, $y1,
  90. $x2, $y2,
  91. $x3, $y3
  92. ) {
  93. $dx3 = $x0 - $x1 + $x2 - $x3;
  94. $dy3 = $y0 - $y1 + $y2 - $y3;
  95. if ($dx3 == 0.0 && $dy3 == 0.0) {
  96. // Affine
  97. return new PerspectiveTransform($x1 - $x0, $x2 - $x1, $x0,
  98. $y1 - $y0, $y2 - $y1, $y0,
  99. 0.0, 0.0, 1.0);
  100. } else {
  101. $dx1 = $x1 - $x2;
  102. $dx2 = $x3 - $x2;
  103. $dy1 = $y1 - $y2;
  104. $dy2 = $y3 - $y2;
  105. $denominator = $dx1 * $dy2 - $dx2 * $dy1;
  106. $a13 = ($dx3 * $dy2 - $dx2 * $dy3) / $denominator;
  107. $a23 = ($dx1 * $dy3 - $dx3 * $dy1) / $denominator;
  108. return new PerspectiveTransform($x1 - $x0 + $a13 * $x1, $x3 - $x0 + $a23 * $x3, $x0,
  109. $y1 - $y0 + $a13 * $y1, $y3 - $y0 + $a23 * $y3, $y0,
  110. $a13, $a23, 1.0);
  111. }
  112. }
  113. public function times($other)
  114. {
  115. return new PerspectiveTransform($this->a11 * $other->a11 + $this->a21 * $other->a12 + $this->a31 * $other->a13,
  116. $this->a11 * $other->a21 + $this->a21 * $other->a22 + $this->a31 * $other->a23,
  117. $this->a11 * $other->a31 + $this->a21 * $other->a32 + $this->a31 * $other->a33,
  118. $this->a12 * $other->a11 + $this->a22 * $other->a12 + $this->a32 * $other->a13,
  119. $this->a12 * $other->a21 + $this->a22 * $other->a22 + $this->a32 * $other->a23,
  120. $this->a12 * $other->a31 + $this->a22 * $other->a32 + $this->a32 * $other->a33,
  121. $this->a13 * $other->a11 + $this->a23 * $other->a12 + $this->a33 * $other->a13,
  122. $this->a13 * $other->a21 + $this->a23 * $other->a22 + $this->a33 * $other->a23,
  123. $this->a13 * $other->a31 + $this->a23 * $other->a32 + $this->a33 * $other->a33);
  124. }
  125. public function transformPoints(&$points, &$yValues = 0)
  126. {
  127. if ($yValues) {
  128. $this->transformPoints_($points, $yValues);
  129. return;
  130. }
  131. $max = count($points);
  132. $a11 = $this->a11;
  133. $a12 = $this->a12;
  134. $a13 = $this->a13;
  135. $a21 = $this->a21;
  136. $a22 = $this->a22;
  137. $a23 = $this->a23;
  138. $a31 = $this->a31;
  139. $a32 = $this->a32;
  140. $a33 = $this->a33;
  141. for ($i = 0; $i < $max; $i += 2) {
  142. $x = $points[$i];
  143. $y = $points[$i + 1];
  144. $denominator = $a13 * $x + $a23 * $y + $a33;
  145. $points[$i] = ($a11 * $x + $a21 * $y + $a31) / $denominator;
  146. $points[$i + 1] = ($a12 * $x + $a22 * $y + $a32) / $denominator;
  147. }
  148. }
  149. public function transformPoints_(&$xValues, &$yValues)
  150. {
  151. $n = count($xValues);
  152. for ($i = 0; $i < $n; $i++) {
  153. $x = $xValues[$i];
  154. $y = $yValues[$i];
  155. $denominator = $this->a13 * $x + $this->a23 * $y + $this->a33;
  156. $xValues[$i] = ($this->a11 * $x + $this->a21 * $y + $this->a31) / $denominator;
  157. $yValues[$i] = ($this->a12 * $x + $this->a22 * $y + $this->a32) / $denominator;
  158. }
  159. }
  160. }