PlanarYUVLuminanceSource.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. {
  30. private static $THUMBNAIL_SCALE_FACTOR = 2;
  31. private $yuvData;
  32. private $dataWidth;
  33. private $dataHeight;
  34. private $left;
  35. private $top;
  36. public function __construct($yuvData,
  37. $dataWidth,
  38. $dataHeight,
  39. $left,
  40. $top,
  41. $width,
  42. $height,
  43. $reverseHorizontal)
  44. {
  45. parent::__construct($width, $height);
  46. if ($left + $width > $dataWidth || $top + $height > $dataHeight) {
  47. throw new \InvalidArgumentException("Crop rectangle does not fit within image data.");
  48. }
  49. $this->yuvData = $yuvData;
  50. $this->dataWidth = $dataWidth;
  51. $this->dataHeight = $dataHeight;
  52. $this->left = $left;
  53. $this->top = $top;
  54. if ($reverseHorizontal) {
  55. $this->reverseHorizontal($width, $height);
  56. }
  57. }
  58. //@Override
  59. public function getRow($y, $row = null)
  60. {
  61. if ($y < 0 || $y >= getHeight()) {
  62. throw new \InvalidArgumentException("Requested row is outside the image: " + y);
  63. }
  64. $width = $this->getWidth();
  65. if ($row == null || count($row) < $width) {
  66. $row = [];//new byte[width];
  67. }
  68. $offset = ($y + $this->top) * $this->dataWidth + $this->left;
  69. $row = arraycopy($this->yuvData, $offset, $row, 0, $width);
  70. return $row;
  71. }
  72. //@Override
  73. public function getMatrix()
  74. {
  75. $width = $this->getWidth();
  76. $height = $this->getHeight();
  77. // If the caller asks for the entire underlying image, save the copy and give them the
  78. // original data. The docs specifically warn that result.length must be ignored.
  79. if ($width == $this->dataWidth && $height == $this->dataHeight) {
  80. return $this->yuvData;
  81. }
  82. $area = $width * $height;
  83. $matrix = [];//new byte[area];
  84. $inputOffset = $this->top * $this->dataWidth + $this->left;
  85. // If the width matches the full width of the underlying data, perform a single copy.
  86. if ($width == $this->dataWidth) {
  87. $matrix = arraycopy($this->yuvData, $inputOffset, $matrix, 0, $area);
  88. return $matrix;
  89. }
  90. // Otherwise copy one cropped row at a time.
  91. $yuv = $this->yuvData;
  92. for ($y = 0; $y < $height; $y++) {
  93. $outputOffset = $y * $width;
  94. $matrix = arraycopy($this->yuvData, $inputOffset, $matrix, $outputOffset, $width);
  95. $inputOffset += $this->dataWidth;
  96. }
  97. return $matrix;
  98. }
  99. // @Override
  100. public function isCropSupported()
  101. {
  102. return true;
  103. }
  104. // @Override
  105. public function crop($left, $top, $width, $height)
  106. {
  107. return new PlanarYUVLuminanceSource($this->yuvData,
  108. $this->dataWidth,
  109. $this->dataHeight,
  110. $this->left + $left,
  111. $this->top + $top,
  112. $width,
  113. $height,
  114. false);
  115. }
  116. public function renderThumbnail()
  117. {
  118. $width = (int)($this->getWidth() / self::$THUMBNAIL_SCALE_FACTOR);
  119. $height = (int)($this->getHeight() / self::$THUMBNAIL_SCALE_FACTOR);
  120. $pixels = [];//new int[width * height];
  121. $yuv = $this->yuvData;
  122. $inputOffset = $this->top * $this->dataWidth + $this->left;
  123. for ($y = 0; $y < $height; $y++) {
  124. $outputOffset = $y * $width;
  125. for ($x = 0; $x < $width; $x++) {
  126. $grey = ($yuv[$inputOffset + $x * self::$THUMBNAIL_SCALE_FACTOR] & 0xff);
  127. $pixels[$outputOffset + $x] = (0xFF000000 | ($grey * 0x00010101));
  128. }
  129. $inputOffset += $this->dataWidth * self::$THUMBNAIL_SCALE_FACTOR;
  130. }
  131. return $pixels;
  132. }
  133. /**
  134. * @return width of image from {@link #renderThumbnail()}
  135. */
  136. /*
  137. public int getThumbnailWidth() {
  138. return getWidth() / THUMBNAIL_SCALE_FACTOR;
  139. }*/
  140. /**
  141. * @return height of image from {@link #renderThumbnail()}
  142. */
  143. /*
  144. public int getThumbnailHeight() {
  145. return getHeight() / THUMBNAIL_SCALE_FACTOR;
  146. }
  147. private void reverseHorizontal(int width, int height) {
  148. byte[] yuvData = this.yuvData;
  149. for (int y = 0, rowStart = top * dataWidth + left; y < height; y++, rowStart += dataWidth) {
  150. int middle = rowStart + width / 2;
  151. for (int x1 = rowStart, x2 = rowStart + width - 1; x1 < middle; x1++, x2--) {
  152. byte temp = yuvData[x1];
  153. yuvData[x1] = yuvData[x2];
  154. yuvData[x2] = temp;
  155. }
  156. }
  157. }
  158. */
  159. }