InvertedLuminanceSource.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 LuminanceSource from './LuminanceSource';
  17. /*namespace com.google.zxing {*/
  18. /**
  19. * A wrapper implementation of {@link LuminanceSource} which inverts the luminances it returns -- black becomes
  20. * white and vice versa, and each value becomes (255-value).
  21. *
  22. * @author Sean Owen
  23. */
  24. export default class InvertedLuminanceSource extends LuminanceSource {
  25. constructor(delegate) {
  26. super(delegate.getWidth(), delegate.getHeight());
  27. this.delegate = delegate;
  28. }
  29. /*@Override*/
  30. getRow(y /*int*/, row) {
  31. const sourceRow = this.delegate.getRow(y, row);
  32. const width = this.getWidth();
  33. for (let i = 0; i < width; i++) {
  34. sourceRow[i] = /*(byte)*/ (255 - (sourceRow[i] & 0xFF));
  35. }
  36. return sourceRow;
  37. }
  38. /*@Override*/
  39. getMatrix() {
  40. const matrix = this.delegate.getMatrix();
  41. const length = this.getWidth() * this.getHeight();
  42. const invertedMatrix = new Uint8ClampedArray(length);
  43. for (let i = 0; i < length; i++) {
  44. invertedMatrix[i] = /*(byte)*/ (255 - (matrix[i] & 0xFF));
  45. }
  46. return invertedMatrix;
  47. }
  48. /*@Override*/
  49. isCropSupported() {
  50. return this.delegate.isCropSupported();
  51. }
  52. /*@Override*/
  53. crop(left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  54. return new InvertedLuminanceSource(this.delegate.crop(left, top, width, height));
  55. }
  56. /*@Override*/
  57. isRotateSupported() {
  58. return this.delegate.isRotateSupported();
  59. }
  60. /**
  61. * @return original delegate {@link LuminanceSource} since invert undoes itself
  62. */
  63. /*@Override*/
  64. invert() {
  65. return this.delegate;
  66. }
  67. /*@Override*/
  68. rotateCounterClockwise() {
  69. return new InvertedLuminanceSource(this.delegate.rotateCounterClockwise());
  70. }
  71. /*@Override*/
  72. rotateCounterClockwise45() {
  73. return new InvertedLuminanceSource(this.delegate.rotateCounterClockwise45());
  74. }
  75. }