Passed
Push — main ( 3d99ee...53f6dc )
by smiley
10:29
created

LuminanceSourceAbstract::getHeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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