RGBLuminanceSource.js 5.8 KB

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