RGBLuminanceSource.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /*namespace com.google.zxing {*/
  17. import './InvertedLuminanceSource'; // required because of circular dependencies between LuminanceSource and InvertedLuminanceSource
  18. import InvertedLuminanceSource from './InvertedLuminanceSource';
  19. import LuminanceSource from './LuminanceSource';
  20. import System from './util/System';
  21. import IllegalArgumentException from './IllegalArgumentException';
  22. /**
  23. * This class is used to help decode images from files which arrive as RGB data from
  24. * an ARGB pixel array. It does not support rotation.
  25. *
  26. * @author dswitkin@google.com (Daniel Switkin)
  27. * @author Betaminos
  28. */
  29. export default class RGBLuminanceSource extends LuminanceSource {
  30. constructor(luminances, width /*int*/, height /*int*/, dataWidth /*int*/, dataHeight /*int*/, left /*int*/, top /*int*/) {
  31. super(width, height);
  32. this.dataWidth = dataWidth;
  33. this.dataHeight = dataHeight;
  34. this.left = left;
  35. this.top = top;
  36. if (luminances.BYTES_PER_ELEMENT === 4) { // Int32Array
  37. const size = width * height;
  38. const luminancesUint8Array = new Uint8ClampedArray(size);
  39. for (let offset = 0; offset < size; offset++) {
  40. const pixel = luminances[offset];
  41. const r = (pixel >> 16) & 0xff; // red
  42. const g2 = (pixel >> 7) & 0x1fe; // 2 * green
  43. const b = pixel & 0xff; // blue
  44. // Calculate green-favouring average cheaply
  45. luminancesUint8Array[offset] = /*(byte) */ ((r + g2 + b) / 4) & 0xFF;
  46. }
  47. this.luminances = luminancesUint8Array;
  48. }
  49. else {
  50. this.luminances = luminances;
  51. }
  52. if (undefined === dataWidth) {
  53. this.dataWidth = width;
  54. }
  55. if (undefined === dataHeight) {
  56. this.dataHeight = height;
  57. }
  58. if (undefined === left) {
  59. this.left = 0;
  60. }
  61. if (undefined === top) {
  62. this.top = 0;
  63. }
  64. if (this.left + width > this.dataWidth || this.top + height > this.dataHeight) {
  65. throw new IllegalArgumentException('Crop rectangle does not fit within image data.');
  66. }
  67. }
  68. /*@Override*/
  69. getRow(y /*int*/, row) {
  70. if (y < 0 || y >= this.getHeight()) {
  71. throw new IllegalArgumentException('Requested row is outside the image: ' + y);
  72. }
  73. const width = this.getWidth();
  74. if (row === null || row === undefined || row.length < width) {
  75. row = new Uint8ClampedArray(width);
  76. }
  77. const offset = (y + this.top) * this.dataWidth + this.left;
  78. System.arraycopy(this.luminances, offset, row, 0, width);
  79. return row;
  80. }
  81. /*@Override*/
  82. getMatrix() {
  83. const width = this.getWidth();
  84. const height = this.getHeight();
  85. // If the caller asks for the entire underlying image, save the copy and give them the
  86. // original data. The docs specifically warn that result.length must be ignored.
  87. if (width === this.dataWidth && height === this.dataHeight) {
  88. return this.luminances;
  89. }
  90. const area = width * height;
  91. const matrix = new Uint8ClampedArray(area);
  92. let inputOffset = this.top * this.dataWidth + this.left;
  93. // If the width matches the full width of the underlying data, perform a single copy.
  94. if (width === this.dataWidth) {
  95. System.arraycopy(this.luminances, inputOffset, matrix, 0, area);
  96. return matrix;
  97. }
  98. // Otherwise copy one cropped row at a time.
  99. for (let y = 0; y < height; y++) {
  100. const outputOffset = y * width;
  101. System.arraycopy(this.luminances, inputOffset, matrix, outputOffset, width);
  102. inputOffset += this.dataWidth;
  103. }
  104. return matrix;
  105. }
  106. /*@Override*/
  107. isCropSupported() {
  108. return true;
  109. }
  110. /*@Override*/
  111. crop(left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  112. return new RGBLuminanceSource(this.luminances, width, height, this.dataWidth, this.dataHeight, this.left + left, this.top + top);
  113. }
  114. invert() {
  115. return new InvertedLuminanceSource(this);
  116. }
  117. }