LuminanceSource.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /*
  3. * Copyright 2009 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;
  18. /**
  19. * The purpose of this class hierarchy is to abstract different bitmap implementations across
  20. * platforms into a standard interface for requesting greyscale luminance values. The interface
  21. * only provides immutable methods; therefore crop and rotation create copies. This is to ensure
  22. * that one Reader does not modify the original luminance source and leave it in an unknown state
  23. * for other Readers in the chain.
  24. *
  25. * @author dswitkin@google.com (Daniel Switkin)
  26. */
  27. abstract class LuminanceSource
  28. {
  29. private $width;
  30. private $height;
  31. public function __construct($width, $height)
  32. {
  33. $this->width = $width;
  34. $this->height = $height;
  35. }
  36. /**
  37. * Fetches luminance data for the underlying bitmap. Values should be fetched using:
  38. * {@code int luminance = array[y * width + x] & 0xff}
  39. *
  40. * @return A row-major 2D array of luminance values. Do not use result.length as it may be
  41. * larger than width * height bytes on some platforms. Do not modify the contents
  42. * of the result.
  43. */
  44. public abstract function getMatrix();
  45. /**
  46. * @return The width of the bitmap.
  47. */
  48. public final function getWidth()
  49. {
  50. return $this->width;
  51. }
  52. /**
  53. * @return The height of the bitmap.
  54. */
  55. public final function getHeight()
  56. {
  57. return $this->height;
  58. }
  59. /**
  60. * @return bool Whether this subclass supports cropping.
  61. */
  62. public function isCropSupported()
  63. {
  64. return false;
  65. }
  66. /**
  67. * Returns a new object with cropped image data. Implementations may keep a reference to the
  68. * original data rather than a copy. Only callable if isCropSupported() is true.
  69. *
  70. * @param left The left coordinate, which must be in [0,getWidth())
  71. * @param top The top coordinate, which must be in [0,getHeight())
  72. * @param width The width of the rectangle to crop.
  73. * @param height The height of the rectangle to crop.
  74. *
  75. * @return A cropped version of this object.
  76. */
  77. public function crop($left, $top, $width, $height)
  78. {
  79. throw new \Exception("This luminance source does not support cropping.");
  80. }
  81. /**
  82. * @return Whether this subclass supports counter-clockwise rotation.
  83. */
  84. public function isRotateSupported()
  85. {
  86. return false;
  87. }
  88. /**
  89. * @return a wrapper of this {@code LuminanceSource} which inverts the luminances it returns -- black becomes
  90. * white and vice versa, and each value becomes (255-value).
  91. */
  92. public function invert()
  93. {
  94. return new InvertedLuminanceSource($this);
  95. }
  96. /**
  97. * Returns a new object with rotated image data by 90 degrees counterclockwise.
  98. * Only callable if {@link #isRotateSupported()} is true.
  99. *
  100. * @return A rotated version of this object.
  101. */
  102. public function rotateCounterClockwise()
  103. {
  104. throw new \Exception("This luminance source does not support rotation by 90 degrees.");
  105. }
  106. /**
  107. * Returns a new object with rotated image data by 45 degrees counterclockwise.
  108. * Only callable if {@link #isRotateSupported()} is true.
  109. *
  110. * @return A rotated version of this object.
  111. */
  112. public function rotateCounterClockwise45()
  113. {
  114. throw new \Exception("This luminance source does not support rotation by 45 degrees.");
  115. }
  116. public final function toString()
  117. {
  118. $row = [];
  119. $result = '';
  120. for ($y = 0; $y < $this->height; $y++) {
  121. $row = $this->getRow($y, $row);
  122. for ($x = 0; $x < $this->width; $x++) {
  123. $luminance = $row[$x] & 0xFF;
  124. $c = '';
  125. if ($luminance < 0x40) {
  126. $c = '#';
  127. } else if ($luminance < 0x80) {
  128. $c = '+';
  129. } else if ($luminance < 0xC0) {
  130. $c = '.';
  131. } else {
  132. $c = ' ';
  133. }
  134. $result .= ($c);
  135. }
  136. $result .= ('\n');
  137. }
  138. return $result;
  139. }
  140. /**
  141. * Fetches one row of luminance data from the underlying platform's bitmap. Values range from
  142. * 0 (black) to 255 (white). Because Java does not have an unsigned byte type, callers will have
  143. * to bitwise and with 0xff for each value. It is preferable for implementations of this method
  144. * to only fetch this row rather than the whole image, since no 2D Readers may be installed and
  145. * getMatrix() may never be called.
  146. *
  147. * @param $y ; The row to fetch, which must be in [0,getHeight())
  148. * @param $row ; An optional preallocated array. If null or too small, it will be ignored.
  149. * Always use the returned object, and ignore the .length of the array.
  150. *
  151. * @return array
  152. * An array containing the luminance data.
  153. */
  154. public abstract function getRow($y, $row);
  155. }