IMagickLuminanceSource.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Zxing;
  3. /**
  4. * This class is used to help decode images from files which arrive as GD Resource
  5. * It does not support rotation.
  6. */
  7. final class IMagickLuminanceSource extends LuminanceSource
  8. {
  9. public $luminances;
  10. private $dataWidth;
  11. private $dataHeight;
  12. private $left;
  13. private $top;
  14. private $image;
  15. public function __construct(
  16. \Imagick $image,
  17. $dataWidth,
  18. $dataHeight,
  19. $left = null,
  20. $top = null,
  21. $width = null,
  22. $height = null
  23. ) {
  24. if (!$left && !$top && !$width && !$height) {
  25. $this->_IMagickLuminanceSource($image, $dataWidth, $dataHeight);
  26. return;
  27. }
  28. parent::__construct($width, $height);
  29. if ($left + $width > $dataWidth || $top + $height > $dataHeight) {
  30. throw new \InvalidArgumentException("Crop rectangle does not fit within image data.");
  31. }
  32. $this->luminances = $image;
  33. $this->dataWidth = $dataWidth;
  34. $this->dataHeight = $dataHeight;
  35. $this->left = $left;
  36. $this->top = $top;
  37. }
  38. public function _IMagickLuminanceSource(\Imagick $image, $width, $height)
  39. {
  40. parent::__construct($width, $height);
  41. $this->dataWidth = $width;
  42. $this->dataHeight = $height;
  43. $this->left = 0;
  44. $this->top = 0;
  45. $this->image = $image;
  46. // In order to measure pure decoding speed, we convert the entire image to a greyscale array
  47. // up front, which is the same as the Y channel of the YUVLuminanceSource in the real app.
  48. $this->luminances = [];
  49. $image->setImageColorspace(\Imagick::COLORSPACE_GRAY);
  50. // $image->newPseudoImage(0, 0, "magick:rose");
  51. $pixels = $image->exportImagePixels(1, 1, $width, $height, "RGB", \Imagick::PIXEL_CHAR);
  52. $array = [];
  53. $rgb = [];
  54. $countPixels = count($pixels);
  55. for ($i = 0; $i < $countPixels; $i += 3) {
  56. $r = $pixels[$i] & 0xff;
  57. $g = $pixels[$i + 1] & 0xff;
  58. $b = $pixels[$i + 2] & 0xff;
  59. if ($r == $g && $g == $b) {
  60. // Image is already greyscale, so pick any channel.
  61. $this->luminances[] = $r;//(($r + 128) % 256) - 128;
  62. } else {
  63. // Calculate luminance cheaply, favoring green.
  64. $this->luminances[] = ($r + 2 * $g + $b) / 4;//(((($r + 2 * $g + $b) / 4) + 128) % 256) - 128;
  65. }
  66. }
  67. }
  68. //@Override
  69. public function getRow($y, $row = null)
  70. {
  71. if ($y < 0 || $y >= $this->getHeight()) {
  72. throw new \InvalidArgumentException('Requested row is outside the image: ' . $y);
  73. }
  74. $width = $this->getWidth();
  75. if ($row == null || count($row) < $width) {
  76. $row = [];
  77. }
  78. $offset = ($y + $this->top) * $this->dataWidth + $this->left;
  79. $row = arraycopy($this->luminances, $offset, $row, 0, $width);
  80. return $row;
  81. }
  82. //@Override
  83. public function getMatrix()
  84. {
  85. $width = $this->getWidth();
  86. $height = $this->getHeight();
  87. // If the caller asks for the entire underlying image, save the copy and give them the
  88. // original data. The docs specifically warn that result.length must be ignored.
  89. if ($width == $this->dataWidth && $height == $this->dataHeight) {
  90. return $this->luminances;
  91. }
  92. $area = $width * $height;
  93. $matrix = [];
  94. $inputOffset = $this->top * $this->dataWidth + $this->left;
  95. // If the width matches the full width of the underlying data, perform a single copy.
  96. if ($width == $this->dataWidth) {
  97. $matrix = arraycopy($this->luminances, $inputOffset, $matrix, 0, $area);
  98. return $matrix;
  99. }
  100. // Otherwise copy one cropped row at a time.
  101. $rgb = $this->luminances;
  102. for ($y = 0; $y < $height; $y++) {
  103. $outputOffset = $y * $width;
  104. $matrix = arraycopy($rgb, $inputOffset, $matrix, $outputOffset, $width);
  105. $inputOffset += $this->dataWidth;
  106. }
  107. return $matrix;
  108. }
  109. //@Override
  110. public function isCropSupported()
  111. {
  112. return true;
  113. }
  114. //@Override
  115. public function crop($left, $top, $width, $height)
  116. {
  117. return $this->luminances->cropImage($width, $height, $left, $top);
  118. return new GDLuminanceSource($this->luminances,
  119. $this->dataWidth,
  120. $this->dataHeight,
  121. $this->left + $left,
  122. $this->top + $top,
  123. $width,
  124. $height);
  125. }
  126. }