Passed
Push — main ( 0a0d87...5e9cf0 )
by smiley
02:11
created

IMagickLuminanceSource::fromBlob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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