PlanarYUVLuminanceSource.php 5.1 KB

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