LuminanceSource.php 5.4 KB

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