LuminanceSourceAbstract::setLuminancePixel()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 4
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
13
namespace chillerlan\QRCode\Decoder;
14
15
use chillerlan\QRCode\QROptions;
16
use chillerlan\Settings\SettingsContainerInterface;
17
use function array_slice, array_splice, file_exists, is_file, is_readable, realpath;
18
19
/**
20
 * The purpose of this class hierarchy is to abstract different bitmap implementations across
21
 * platforms into a standard interface for requesting greyscale luminance values.
22
 *
23
 * @author [email protected] (Daniel Switkin)
24
 */
25
abstract class LuminanceSourceAbstract implements LuminanceSourceInterface{
26
27
	/** @var \chillerlan\QRCode\QROptions|\chillerlan\Settings\SettingsContainerInterface */
28
	protected SettingsContainerInterface $options;
29
	protected array $luminances;
30
	protected int   $width;
31
	protected int   $height;
32
33
	/**
34
	 *
35
	 */
36
	public function __construct(int $width, int $height, SettingsContainerInterface $options = null){
37
		$this->width   = $width;
38
		$this->height  = $height;
39
		$this->options = ($options ?? new QROptions);
40
41
		$this->luminances = [];
42
	}
43
44
	/** @inheritDoc */
45
	public function getLuminances():array{
46
		return $this->luminances;
47
	}
48
49
	/** @inheritDoc */
50
	public function getWidth():int{
51
		return $this->width;
52
	}
53
54
	/** @inheritDoc */
55
	public function getHeight():int{
56
		return $this->height;
57
	}
58
59
	/** @inheritDoc */
60
	public function getRow(int $y):array{
61
62
		if($y < 0 || $y >= $this->getHeight()){
63
			throw new QRCodeDecoderException('Requested row is outside the image: '.$y);
64
		}
65
66
		$arr = [];
67
68
		array_splice($arr, 0, $this->width, array_slice($this->luminances, ($y * $this->width), $this->width));
69
70
		return $arr;
71
	}
72
73
	/**
74
	 *
75
	 */
76
	protected function setLuminancePixel(int $r, int $g, int $b):void{
77
		$this->luminances[] = ($r === $g && $g === $b)
78
			// Image is already greyscale, so pick any channel.
79
			? $r // (($r + 128) % 256) - 128;
80
			// Calculate luminance cheaply, favoring green.
81
			: (($r + 2 * $g + $b) / 4); // (((($r + 2 * $g + $b) / 4) + 128) % 256) - 128;
82
	}
83
84
	/**
85
	 * @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException
86
	 */
87
	protected static function checkFile(string $path):string{
88
		$path = trim($path);
89
90
		if(!file_exists($path) || !is_file($path) || !is_readable($path)){
91
			throw new QRCodeDecoderException('invalid file: '.$path);
92
		}
93
94
		$realpath = realpath($path);
95
96
		if($realpath === false){
97
			throw new QRCodeDecoderException('unable to resolve path: '.$path);
98
		}
99
100
		return $realpath;
101
	}
102
103
}
104