IMagickLuminanceSource::setLuminancePixels()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
nc 8
nop 0
dl 0
loc 17
rs 9.6111
c 1
b 0
f 0
1
<?php
2
/**
3
 * Class IMagickLuminanceSource
4
 *
5
 * @created      17.01.2021
6
 * @author       Ashot Khanamiryan
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2021 Smiley
9
 * @license      MIT
10
 *
11
 * @noinspection PhpComposerExtensionStubsInspection
12
 */
13
14
namespace chillerlan\QRCode\Decoder;
15
16
use chillerlan\Settings\SettingsContainerInterface;
17
use Imagick;
18
use function count;
19
20
/**
21
 * This class is used to help decode images from files which arrive as Imagick Resource
22
 * It does not support rotation.
23
 */
24
class IMagickLuminanceSource extends LuminanceSourceAbstract{
25
26
	protected Imagick $imagick;
27
28
	/**
29
	 * IMagickLuminanceSource constructor.
30
	 */
31
	public function __construct(Imagick $imagick, SettingsContainerInterface $options = null){
32
		parent::__construct($imagick->getImageWidth(), $imagick->getImageHeight(), $options);
33
34
		$this->imagick = $imagick;
35
36
		$this->setLuminancePixels();
37
	}
38
39
	/**
40
	 *
41
	 */
42
	protected function setLuminancePixels():void{
43
44
		if($this->options->readerGrayscale){
45
			$this->imagick->setImageColorspace(Imagick::COLORSPACE_GRAY);
46
		}
47
48
		if($this->options->readerIncreaseContrast){
49
			for($i = 0; $i < 10; $i++){
50
				$this->imagick->contrastImage(false); // misleading docs
51
			}
52
		}
53
54
		$pixels = $this->imagick->exportImagePixels(1, 1, $this->width, $this->height, 'RGB', Imagick::PIXEL_CHAR);
55
		$count  = count($pixels);
56
57
		for($i = 0; $i < $count; $i += 3){
58
			$this->setLuminancePixel(($pixels[$i] & 0xff), ($pixels[($i + 1)] & 0xff), ($pixels[($i + 2)] & 0xff));
59
		}
60
	}
61
62
	/** @inheritDoc */
63
	public static function fromFile(string $path, SettingsContainerInterface $options = null):self{
64
		return new self(new Imagick(self::checkFile($path)), $options);
65
	}
66
67
	/** @inheritDoc */
68
	public static function fromBlob(string $blob, SettingsContainerInterface $options = null):self{
69
		$im = new Imagick;
70
		$im->readImageBlob($blob);
71
72
		return new self($im, $options);
73
	}
74
75
}
76