Debian Codegraph @

vendor/chillerlan/php-qrcode/src/Common/LuminanceSourceAbstract.php

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