PlanarYUVLuminanceSource.js 6.8 KB

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