LuminanceSource.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2009 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import StringBuilder from './util/StringBuilder';
  17. import UnsupportedOperationException from './UnsupportedOperationException';
  18. /*namespace com.google.zxing {*/
  19. /**
  20. * The purpose of this class hierarchy is to abstract different bitmap implementations across
  21. * platforms into a standard interface for requesting greyscale luminance values. The interface
  22. * only provides immutable methods; therefore crop and rotation create copies. This is to ensure
  23. * that one Reader does not modify the original luminance source and leave it in an unknown state
  24. * for other Readers in the chain.
  25. *
  26. * @author dswitkin@google.com (Daniel Switkin)
  27. */
  28. class LuminanceSource {
  29. constructor(width /*int*/, height /*int*/) {
  30. this.width = width;
  31. this.height = height;
  32. }
  33. /**
  34. * @return The width of the bitmap.
  35. */
  36. getWidth() {
  37. return this.width;
  38. }
  39. /**
  40. * @return The height of the bitmap.
  41. */
  42. getHeight() {
  43. return this.height;
  44. }
  45. /**
  46. * @return Whether this subclass supports cropping.
  47. */
  48. isCropSupported() {
  49. return false;
  50. }
  51. /**
  52. * Returns a new object with cropped image data. Implementations may keep a reference to the
  53. * original data rather than a copy. Only callable if isCropSupported() is true.
  54. *
  55. * @param left The left coordinate, which must be in [0,getWidth())
  56. * @param top The top coordinate, which must be in [0,getHeight())
  57. * @param width The width of the rectangle to crop.
  58. * @param height The height of the rectangle to crop.
  59. * @return A cropped version of this object.
  60. */
  61. crop(left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  62. throw new UnsupportedOperationException('This luminance source does not support cropping.');
  63. }
  64. /**
  65. * @return Whether this subclass supports counter-clockwise rotation.
  66. */
  67. isRotateSupported() {
  68. return false;
  69. }
  70. /**
  71. * Returns a new object with rotated image data by 90 degrees counterclockwise.
  72. * Only callable if {@link #isRotateSupported()} is true.
  73. *
  74. * @return A rotated version of this object.
  75. */
  76. rotateCounterClockwise() {
  77. throw new UnsupportedOperationException('This luminance source does not support rotation by 90 degrees.');
  78. }
  79. /**
  80. * Returns a new object with rotated image data by 45 degrees counterclockwise.
  81. * Only callable if {@link #isRotateSupported()} is true.
  82. *
  83. * @return A rotated version of this object.
  84. */
  85. rotateCounterClockwise45() {
  86. throw new UnsupportedOperationException('This luminance source does not support rotation by 45 degrees.');
  87. }
  88. /*@Override*/
  89. toString() {
  90. const row = new Uint8ClampedArray(this.width);
  91. let result = new StringBuilder();
  92. for (let y = 0; y < this.height; y++) {
  93. const sourceRow = this.getRow(y, row);
  94. for (let x = 0; x < this.width; x++) {
  95. const luminance = sourceRow[x] & 0xFF;
  96. let c;
  97. if (luminance < 0x40) {
  98. c = '#';
  99. }
  100. else if (luminance < 0x80) {
  101. c = '+';
  102. }
  103. else if (luminance < 0xC0) {
  104. c = '.';
  105. }
  106. else {
  107. c = ' ';
  108. }
  109. result.append(c);
  110. }
  111. result.append('\n');
  112. }
  113. return result.toString();
  114. }
  115. }
  116. export default LuminanceSource;