PlanarYUVLuminanceSource.js 5.6 KB

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