RGBLuminanceSource.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 class is used to help decode images from files which arrive as RGB data from
  20. * an ARGB pixel array. It does not support rotation.
  21. *
  22. * @author dswitkin@google.com (Daniel Switkin)
  23. * @author Betaminos
  24. */
  25. final class RGBLuminanceSource extends LuminanceSource
  26. {
  27. public $luminances;
  28. private $dataWidth;
  29. private $dataHeight;
  30. private $left;
  31. private $top;
  32. private $pixels;
  33. public function __construct(
  34. $pixels,
  35. $dataWidth,
  36. $dataHeight,
  37. $left = null,
  38. $top = null,
  39. $width = null,
  40. $height = null
  41. ) {
  42. if (!$left && !$top && !$width && !$height) {
  43. $this->RGBLuminanceSource_($pixels, $dataWidth, $dataHeight);
  44. return;
  45. }
  46. parent::__construct($width, $height);
  47. if ($left + $width > $dataWidth || $top + $height > $dataHeight) {
  48. throw new \InvalidArgumentException("Crop rectangle does not fit within image data.");
  49. }
  50. $this->luminances = $pixels;
  51. $this->dataWidth = $dataWidth;
  52. $this->dataHeight = $dataHeight;
  53. $this->left = $left;
  54. $this->top = $top;
  55. }
  56. public function RGBLuminanceSource_($width, $height, $pixels)
  57. {
  58. parent::__construct($width, $height);
  59. $this->dataWidth = $width;
  60. $this->dataHeight = $height;
  61. $this->left = 0;
  62. $this->top = 0;
  63. $this->pixels = $pixels;
  64. // In order to measure pure decoding speed, we convert the entire image to a greyscale array
  65. // up front, which is the same as the Y channel of the YUVLuminanceSource in the real app.
  66. $this->luminances = [];
  67. //$this->luminances = $this->grayScaleToBitmap($this->grayscale());
  68. foreach ($pixels as $key => $pixel) {
  69. $r = $pixel['red'];
  70. $g = $pixel['green'];
  71. $b = $pixel['blue'];
  72. /* if (($pixel & 0xFF000000) == 0) {
  73. $pixel = 0xFFFFFFFF; // = white
  74. }
  75. // .229R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC)
  76. $this->luminances[$key] =
  77. (306 * (($pixel >> 16) & 0xFF) +
  78. 601 * (($pixel >> 8) & 0xFF) +
  79. 117 * ($pixel & 0xFF) +
  80. 0x200) >> 10;
  81. */
  82. //$r = ($pixel >> 16) & 0xff;
  83. //$g = ($pixel >> 8) & 0xff;
  84. //$b = $pixel & 0xff;
  85. if ($r == $g && $g == $b) {
  86. // Image is already greyscale, so pick any channel.
  87. $this->luminances[$key] = $r;//(($r + 128) % 256) - 128;
  88. } else {
  89. // Calculate luminance cheaply, favoring green.
  90. $this->luminances[$key] = ($r + 2 * $g + $b) / 4;//(((($r + 2 * $g + $b) / 4) + 128) % 256) - 128;
  91. }
  92. }
  93. /*
  94. for ($y = 0; $y < $height; $y++) {
  95. $offset = $y * $width;
  96. for ($x = 0; $x < $width; $x++) {
  97. $pixel = $pixels[$offset + $x];
  98. $r = ($pixel >> 16) & 0xff;
  99. $g = ($pixel >> 8) & 0xff;
  100. $b = $pixel & 0xff;
  101. if ($r == $g && $g == $b) {
  102. // Image is already greyscale, so pick any channel.
  103. $this->luminances[(int)($offset + $x)] = (($r+128) % 256) - 128;
  104. } else {
  105. // Calculate luminance cheaply, favoring green.
  106. $this->luminances[(int)($offset + $x)] = (((($r + 2 * $g + $b) / 4)+128)%256) - 128;
  107. }
  108. }
  109. */
  110. //}
  111. // $this->luminances = $this->grayScaleToBitmap($this->luminances);
  112. }
  113. public function grayscale()
  114. {
  115. $width = $this->dataWidth;
  116. $height = $this->dataHeight;
  117. $ret = fill_array(0, $width * $height, 0);
  118. for ($y = 0; $y < $height; $y++) {
  119. for ($x = 0; $x < $width; $x++) {
  120. $gray = $this->getPixel($x, $y, $width, $height);
  121. $ret[$x + $y * $width] = $gray;
  122. }
  123. }
  124. return $ret;
  125. }
  126. public function getPixel($x, $y, $width, $height)
  127. {
  128. $image = $this->pixels;
  129. if ($width < $x) {
  130. die('error');
  131. }
  132. if ($height < $y) {
  133. die('error');
  134. }
  135. $point = ($x) + ($y * $width);
  136. $r = $image[$point]['red'];//($image[$point] >> 16) & 0xff;
  137. $g = $image[$point]['green'];//($image[$point] >> 8) & 0xff;
  138. $b = $image[$point]['blue'];//$image[$point] & 0xff;
  139. $p = (int)(($r * 33 + $g * 34 + $b * 33) / 100);
  140. return $p;
  141. }
  142. public function grayScaleToBitmap($grayScale)
  143. {
  144. $middle = $this->getMiddleBrightnessPerArea($grayScale);
  145. $sqrtNumArea = count($middle);
  146. $areaWidth = floor($this->dataWidth / $sqrtNumArea);
  147. $areaHeight = floor($this->dataHeight / $sqrtNumArea);
  148. $bitmap = fill_array(0, $this->dataWidth * $this->dataHeight, 0);
  149. for ($ay = 0; $ay < $sqrtNumArea; $ay++) {
  150. for ($ax = 0; $ax < $sqrtNumArea; $ax++) {
  151. for ($dy = 0; $dy < $areaHeight; $dy++) {
  152. for ($dx = 0; $dx < $areaWidth; $dx++) {
  153. $bitmap[(int)($areaWidth * $ax + $dx + ($areaHeight * $ay + $dy) * $this->dataWidth)] = ($grayScale[(int)($areaWidth * $ax + $dx + ($areaHeight * $ay + $dy) * $this->dataWidth)] < $middle[$ax][$ay]) ? 0 : 255;
  154. }
  155. }
  156. }
  157. }
  158. return $bitmap;
  159. }
  160. public function getMiddleBrightnessPerArea($image)
  161. {
  162. $numSqrtArea = 4;
  163. //obtain middle brightness((min + max) / 2) per area
  164. $areaWidth = floor($this->dataWidth / $numSqrtArea);
  165. $areaHeight = floor($this->dataHeight / $numSqrtArea);
  166. $minmax = fill_array(0, $numSqrtArea, 0);
  167. for ($i = 0; $i < $numSqrtArea; $i++) {
  168. $minmax[$i] = fill_array(0, $numSqrtArea, 0);
  169. for ($i2 = 0; $i2 < $numSqrtArea; $i2++) {
  170. $minmax[$i][$i2] = [0, 0];
  171. }
  172. }
  173. for ($ay = 0; $ay < $numSqrtArea; $ay++) {
  174. for ($ax = 0; $ax < $numSqrtArea; $ax++) {
  175. $minmax[$ax][$ay][0] = 0xFF;
  176. for ($dy = 0; $dy < $areaHeight; $dy++) {
  177. for ($dx = 0; $dx < $areaWidth; $dx++) {
  178. $target = $image[(int)($areaWidth * $ax + $dx + ($areaHeight * $ay + $dy) * $this->dataWidth)];
  179. if ($target < $minmax[$ax][$ay][0])
  180. $minmax[$ax][$ay][0] = $target;
  181. if ($target > $minmax[$ax][$ay][1])
  182. $minmax[$ax][$ay][1] = $target;
  183. }
  184. }
  185. //minmax[ax][ay][0] = (minmax[ax][ay][0] + minmax[ax][ay][1]) / 2;
  186. }
  187. }
  188. $middle = [];
  189. for ($i3 = 0; $i3 < $numSqrtArea; $i3++) {
  190. $middle[$i3] = [];
  191. }
  192. for ($ay = 0; $ay < $numSqrtArea; $ay++) {
  193. for ($ax = 0; $ax < $numSqrtArea; $ax++) {
  194. $middle[$ax][$ay] = floor(($minmax[$ax][$ay][0] + $minmax[$ax][$ay][1]) / 2);
  195. //Console.out.print(middle[ax][ay] + ",");
  196. }
  197. //Console.out.println("");
  198. }
  199. //Console.out.println("");
  200. return $middle;
  201. }
  202. //@Override
  203. public function getRow($y, $row = null)
  204. {
  205. if ($y < 0 || $y >= $this->getHeight()) {
  206. throw new \InvalidArgumentException("Requested row is outside the image: " + y);
  207. }
  208. $width = $this->getWidth();
  209. if ($row == null || count($row) < $width) {
  210. $row = [];
  211. }
  212. $offset = ($y + $this->top) * $this->dataWidth + $this->left;
  213. $row = arraycopy($this->luminances, $offset, $row, 0, $width);
  214. return $row;
  215. }
  216. //@Override
  217. public function getMatrix()
  218. {
  219. $width = $this->getWidth();
  220. $height = $this->getHeight();
  221. // If the caller asks for the entire underlying image, save the copy and give them the
  222. // original data. The docs specifically warn that result.length must be ignored.
  223. if ($width == $this->dataWidth && $height == $this->dataHeight) {
  224. return $this->luminances;
  225. }
  226. $area = $width * $height;
  227. $matrix = [];
  228. $inputOffset = $this->top * $this->dataWidth + $this->left;
  229. // If the width matches the full width of the underlying data, perform a single copy.
  230. if ($width == $this->dataWidth) {
  231. $matrix = arraycopy($this->luminances, $inputOffset, $matrix, 0, $area);
  232. return $matrix;
  233. }
  234. // Otherwise copy one cropped row at a time.
  235. $rgb = $this->luminances;
  236. for ($y = 0; $y < $height; $y++) {
  237. $outputOffset = $y * $width;
  238. $matrix = arraycopy($rgb, $inputOffset, $matrix, $outputOffset, $width);
  239. $inputOffset += $this->dataWidth;
  240. }
  241. return $matrix;
  242. }
  243. //@Override
  244. public function isCropSupported()
  245. {
  246. return true;
  247. }
  248. //@Override
  249. public function crop($left, $top, $width, $height)
  250. {
  251. return new RGBLuminanceSource($this->luminances,
  252. $this->dataWidth,
  253. $this->dataHeight,
  254. $this->left + $left,
  255. $this->top + $top,
  256. $width,
  257. $height);
  258. }
  259. }