PlanarYUVLuminanceSource.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. var System_1 = require("./util/System");
  33. var LuminanceSource_1 = require("./LuminanceSource");
  34. var InvertedLuminanceSource_1 = require("./InvertedLuminanceSource");
  35. var IllegalArgumentException_1 = require("./IllegalArgumentException");
  36. /**
  37. * This object extends LuminanceSource around an array of YUV data returned from the camera driver,
  38. * with the option to crop to a rectangle within the full data. This can be used to exclude
  39. * superfluous pixels around the perimeter and speed up decoding.
  40. *
  41. * It works for any pixel format where the Y channel is planar and appears first, including
  42. * YCbCr_420_SP and YCbCr_422_SP.
  43. *
  44. * @author dswitkin@google.com (Daniel Switkin)
  45. */
  46. var PlanarYUVLuminanceSource = /** @class */ (function (_super) {
  47. __extends(PlanarYUVLuminanceSource, _super);
  48. function PlanarYUVLuminanceSource(yuvData, dataWidth /*int*/, dataHeight /*int*/, left /*int*/, top /*int*/, width /*int*/, height /*int*/, reverseHorizontal) {
  49. var _this = _super.call(this, width, height) || this;
  50. _this.yuvData = yuvData;
  51. _this.dataWidth = dataWidth;
  52. _this.dataHeight = dataHeight;
  53. _this.left = left;
  54. _this.top = top;
  55. if (left + width > dataWidth || top + height > dataHeight) {
  56. throw new IllegalArgumentException_1.default('Crop rectangle does not fit within image data.');
  57. }
  58. if (reverseHorizontal) {
  59. _this.reverseHorizontal(width, height);
  60. }
  61. return _this;
  62. }
  63. /*@Override*/
  64. PlanarYUVLuminanceSource.prototype.getRow = function (y /*int*/, row) {
  65. if (y < 0 || y >= this.getHeight()) {
  66. throw new IllegalArgumentException_1.default('Requested row is outside the image: ' + y);
  67. }
  68. var width = this.getWidth();
  69. if (row === null || row === undefined || row.length < width) {
  70. row = new Uint8ClampedArray(width);
  71. }
  72. var offset = (y + this.top) * this.dataWidth + this.left;
  73. System_1.default.arraycopy(this.yuvData, offset, row, 0, width);
  74. return row;
  75. };
  76. /*@Override*/
  77. PlanarYUVLuminanceSource.prototype.getMatrix = function () {
  78. var width = this.getWidth();
  79. var height = this.getHeight();
  80. // If the caller asks for the entire underlying image, save the copy and give them the
  81. // original data. The docs specifically warn that result.length must be ignored.
  82. if (width === this.dataWidth && height === this.dataHeight) {
  83. return this.yuvData;
  84. }
  85. var area = width * height;
  86. var matrix = new Uint8ClampedArray(area);
  87. var inputOffset = this.top * this.dataWidth + this.left;
  88. // If the width matches the full width of the underlying data, perform a single copy.
  89. if (width === this.dataWidth) {
  90. System_1.default.arraycopy(this.yuvData, inputOffset, matrix, 0, area);
  91. return matrix;
  92. }
  93. // Otherwise copy one cropped row at a time.
  94. for (var y = 0; y < height; y++) {
  95. var outputOffset = y * width;
  96. System_1.default.arraycopy(this.yuvData, inputOffset, matrix, outputOffset, width);
  97. inputOffset += this.dataWidth;
  98. }
  99. return matrix;
  100. };
  101. /*@Override*/
  102. PlanarYUVLuminanceSource.prototype.isCropSupported = function () {
  103. return true;
  104. };
  105. /*@Override*/
  106. PlanarYUVLuminanceSource.prototype.crop = function (left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  107. return new PlanarYUVLuminanceSource(this.yuvData, this.dataWidth, this.dataHeight, this.left + left, this.top + top, width, height, false);
  108. };
  109. PlanarYUVLuminanceSource.prototype.renderThumbnail = function () {
  110. var width = this.getWidth() / PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
  111. var height = this.getHeight() / PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
  112. var pixels = new Int32Array(width * height);
  113. var yuv = this.yuvData;
  114. var inputOffset = this.top * this.dataWidth + this.left;
  115. for (var y = 0; y < height; y++) {
  116. var outputOffset = y * width;
  117. for (var x = 0; x < width; x++) {
  118. var grey = yuv[inputOffset + x * PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR] & 0xff;
  119. pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
  120. }
  121. inputOffset += this.dataWidth * PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
  122. }
  123. return pixels;
  124. };
  125. /**
  126. * @return width of image from {@link #renderThumbnail()}
  127. */
  128. PlanarYUVLuminanceSource.prototype.getThumbnailWidth = function () {
  129. return this.getWidth() / PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
  130. };
  131. /**
  132. * @return height of image from {@link #renderThumbnail()}
  133. */
  134. PlanarYUVLuminanceSource.prototype.getThumbnailHeight = function () {
  135. return this.getHeight() / PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
  136. };
  137. PlanarYUVLuminanceSource.prototype.reverseHorizontal = function (width /*int*/, height /*int*/) {
  138. var yuvData = this.yuvData;
  139. for (var y = 0, rowStart = this.top * this.dataWidth + this.left; y < height; y++, rowStart += this.dataWidth) {
  140. var middle = rowStart + width / 2;
  141. for (var x1 = rowStart, x2 = rowStart + width - 1; x1 < middle; x1++, x2--) {
  142. var temp = yuvData[x1];
  143. yuvData[x1] = yuvData[x2];
  144. yuvData[x2] = temp;
  145. }
  146. }
  147. };
  148. PlanarYUVLuminanceSource.prototype.invert = function () {
  149. return new InvertedLuminanceSource_1.default(this);
  150. };
  151. PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR = 2;
  152. return PlanarYUVLuminanceSource;
  153. }(LuminanceSource_1.default));
  154. exports.default = PlanarYUVLuminanceSource;