PerspectiveTransform.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. private $a11;
  27. private $a12;
  28. private $a13;
  29. private $a21;
  30. private $a22;
  31. private $a23;
  32. private $a31;
  33. private $a32;
  34. private $a33;
  35. private function __construct($a11, $a21, $a31,
  36. $a12, $a22, $a32,
  37. $a13, $a23, $a33) {
  38. $this->a11 = $a11;
  39. $this->a12 = $a12;
  40. $this->a13 = $a13;
  41. $this->a21 = $a21;
  42. $this->a22 = $a22;
  43. $this->a23 = $a23;
  44. $this->a31 = $a31;
  45. $this->a32 = $a32;
  46. $this->a33 = $a33;
  47. }
  48. public static function quadrilateralToQuadrilateral($x0, $y0,
  49. $x1, $y1,
  50. $x2, $y2,
  51. $x3, $y3,
  52. $x0p, $y0p,
  53. $x1p, $y1p,
  54. $x2p, $y2p,
  55. $x3p, $y3p) {
  56. $qToS = self::quadrilateralToSquare($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3);
  57. $sToQ = self::squareToQuadrilateral($x0p, $y0p, $x1p, $y1p, $x2p, $y2p, $x3p, $y3p);
  58. return $sToQ->times($qToS);
  59. }
  60. public function transformPoints(&$points,&$yValues=0) {
  61. if($yValues) {
  62. $this->transformPoints_($points,$yValues);
  63. return;
  64. }
  65. $max =count($points);
  66. $a11 = $this->a11;
  67. $a12 = $this->a12;
  68. $a13 = $this->a13;
  69. $a21 = $this->a21;
  70. $a22 = $this->a22;
  71. $a23 = $this->a23;
  72. $a31 = $this->a31;
  73. $a32 = $this->a32;
  74. $a33 = $this->a33;
  75. for ($i = 0; $i < $max; $i += 2) {
  76. $x = $points[$i];
  77. $y = $points[$i + 1];
  78. $denominator = $a13 * $x + $a23 * $y + $a33;
  79. $points[$i] = ($a11 * $x + $a21 * $y + $a31) / $denominator;
  80. $points[$i + 1] = ($a12 * $x + $a22 * $y +$a32) / $denominator;
  81. }
  82. }
  83. public function transformPoints_(&$xValues, &$yValues) {
  84. $n = count($xValues);
  85. for ($i = 0; $i < $n; $i ++) {
  86. $x = $xValues[$i];
  87. $y = $yValues[$i];
  88. $denominator = $this->a13 * $x + $this->a23 * $y + $this->a33;
  89. $xValues[$i] = ($this->a11 * $x + $this->a21 * $y + $this->a31) / $denominator;
  90. $yValues[$i] = ($this->a12 * $x + $this->a22 *$y + $this->a32) / $denominator;
  91. }
  92. }
  93. public static function squareToQuadrilateral($x0, $y0,
  94. $x1, $y1,
  95. $x2, $y2,
  96. $x3, $y3) {
  97. $dx3 = $x0 - $x1 + $x2 - $x3;
  98. $dy3 = $y0 - $y1 + $y2 - $y3;
  99. if ($dx3 == 0.0 && $dy3 == 0.0) {
  100. // Affine
  101. return new PerspectiveTransform($x1 - $x0, $x2 - $x1, $x0,
  102. $y1 - $y0, $y2 - $y1, $y0,
  103. 0.0, 0.0, 1.0);
  104. } else {
  105. $dx1 = $x1 - $x2;
  106. $dx2 = $x3 - $x2;
  107. $dy1 = $y1 - $y2;
  108. $dy2 = $y3 - $y2;
  109. $denominator = $dx1 * $dy2 - $dx2 * $dy1;
  110. $a13 = ($dx3 * $dy2 - $dx2 * $dy3) / $denominator;
  111. $a23 = ($dx1 * $dy3 - $dx3 * $dy1) / $denominator;
  112. return new PerspectiveTransform($x1 - $x0 + $a13 * $x1, $x3 - $x0 + $a23 * $x3, $x0,
  113. $y1 - $y0 + $a13 * $y1, $y3 - $y0 + $a23 * $y3, $y0,
  114. $a13, $a23, 1.0);
  115. }
  116. }
  117. public static function quadrilateralToSquare($x0, $y0,
  118. $x1, $y1,
  119. $x2, $y2,
  120. $x3, $y3) {
  121. // Here, the adjoint serves as the inverse:
  122. return self::squareToQuadrilateral($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3)->buildAdjoint();
  123. }
  124. function buildAdjoint() {
  125. // Adjoint is the transpose of the cofactor matrix:
  126. return new PerspectiveTransform($this->a22 * $this->a33 - $this->a23 * $this->a32,
  127. $this->a23 * $this->a31 - $this->a21 * $this->a33,
  128. $this->a21 * $this->a32 - $this->a22 * $this->a31,
  129. $this->a13 * $this->a32 - $this->a12 * $this->a33,
  130. $this->a11 * $this->a33 - $this->a13 * $this->a31,
  131. $this->a12 * $this->a31 - $this->a11 * $this->a32,
  132. $this->a12 * $this->a23 - $this->a13 * $this->a22,
  133. $this->a13 * $this->a21 - $this->a11 * $this->a23,
  134. $this->a11 * $this->a22 - $this->a12 * $this->a21);
  135. }
  136. function times($other) {
  137. return new PerspectiveTransform($this->a11 * $other->a11 + $this->a21 * $other->a12 + $this->a31 * $other->a13,
  138. $this->a11 * $other->a21 + $this->a21 * $other->a22 + $this->a31 * $other->a23,
  139. $this->a11 * $other->a31 + $this->a21 * $other->a32 + $this->a31 * $other->a33,
  140. $this->a12 * $other->a11 + $this->a22 * $other->a12 + $this->a32 * $other->a13,
  141. $this->a12 * $other->a21 + $this->a22 * $other->a22 + $this->a32 * $other->a23,
  142. $this->a12 * $other->a31 + $this->a22 * $other->a32 + $this->a32 * $other->a33,
  143. $this->a13 * $other->a11 + $this->a23 * $other->a12 + $this->a33 * $other->a13,
  144. $this->a13 * $other->a21 + $this->a23 * $other->a22 + $this->a33 * $other->a23,
  145. $this->a13 * $other->a31 + $this->a23 * $other->a32 + $this->a33 * $other->a33);
  146. }
  147. }