LuminanceSourceAbstract.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Class LuminanceSourceAbstract
  4. *
  5. * @created 24.01.2021
  6. * @author ZXing Authors
  7. * @author Smiley <smiley@chillerlan.net>
  8. * @copyright 2021 Smiley
  9. * @license Apache-2.0
  10. */
  11. namespace chillerlan\QRCode\Decoder;
  12. use chillerlan\QRCode\QROptions;
  13. use chillerlan\Settings\SettingsContainerInterface;
  14. use function array_slice, array_splice, file_exists, is_file, is_readable, realpath;
  15. /**
  16. * The purpose of this class hierarchy is to abstract different bitmap implementations across
  17. * platforms into a standard interface for requesting greyscale luminance values. The interface
  18. * only provides immutable methods; therefore crop and rotation create copies. This is to ensure
  19. * that one Reader does not modify the original luminance source and leave it in an unknown state
  20. * for other Readers in the chain.
  21. *
  22. * @author dswitkin@google.com (Daniel Switkin)
  23. */
  24. abstract class LuminanceSourceAbstract implements LuminanceSourceInterface{
  25. /** @var \chillerlan\QRCode\QROptions|\chillerlan\Settings\SettingsContainerInterface */
  26. protected SettingsContainerInterface $options;
  27. protected array $luminances;
  28. protected int $width;
  29. protected int $height;
  30. /**
  31. *
  32. */
  33. public function __construct(int $width, int $height, SettingsContainerInterface $options = null){
  34. $this->width = $width;
  35. $this->height = $height;
  36. $this->options = $options ?? new QROptions;
  37. $this->luminances = [];
  38. }
  39. /** @inheritDoc */
  40. public function getLuminances():array{
  41. return $this->luminances;
  42. }
  43. /** @inheritDoc */
  44. public function getWidth():int{
  45. return $this->width;
  46. }
  47. /** @inheritDoc */
  48. public function getHeight():int{
  49. return $this->height;
  50. }
  51. /** @inheritDoc */
  52. public function getRow(int $y):array{
  53. if($y < 0 || $y >= $this->getHeight()){
  54. throw new QRCodeDecoderException('Requested row is outside the image: '.$y);
  55. }
  56. $arr = [];
  57. array_splice($arr, 0, $this->width, array_slice($this->luminances, $y * $this->width, $this->width));
  58. return $arr;
  59. }
  60. /**
  61. *
  62. */
  63. protected function setLuminancePixel(int $r, int $g, int $b):void{
  64. $this->luminances[] = $r === $g && $g === $b
  65. // Image is already greyscale, so pick any channel.
  66. ? $r // (($r + 128) % 256) - 128;
  67. // Calculate luminance cheaply, favoring green.
  68. : ($r + 2 * $g + $b) / 4; // (((($r + 2 * $g + $b) / 4) + 128) % 256) - 128;
  69. }
  70. /**
  71. * @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException
  72. */
  73. protected static function checkFile(string $path):string{
  74. $path = trim($path);
  75. if(!file_exists($path) || !is_file($path) || !is_readable($path)){
  76. throw new QRCodeDecoderException('invalid file: '.$path);
  77. }
  78. $realpath = realpath($path);
  79. if($realpath === false){
  80. throw new QRCodeDecoderException('unable to resolve path: '.$path);
  81. }
  82. return $realpath;
  83. }
  84. }