RGBLuminanceSource.js 5.7 KB

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