aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/chillerlan/php-qrcode/src/Common/IMagickLuminanceSource.php
blob: ade994a787a5036b08843c5fa9376e6a7690a72f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
 * Class IMagickLuminanceSource
 *
 * @created      17.01.2021
 * @author       Ashot Khanamiryan
 * @author       Smiley <smiley@chillerlan.net>
 * @copyright    2021 Smiley
 * @license      MIT
 *
 * @noinspection PhpComposerExtensionStubsInspection
 */

namespace chillerlan\QRCode\Common;

use chillerlan\Settings\SettingsContainerInterface;
use Imagick;
use function count;

/**
 * This class is used to help decode images from files which arrive as Imagick Resource
 * It does not support rotation.
 */
class IMagickLuminanceSource extends LuminanceSourceAbstract{

	protected Imagick $imagick;

	/**
	 * IMagickLuminanceSource constructor.
	 */
	public function __construct(Imagick $imagick, ?SettingsContainerInterface $options = null){
		parent::__construct($imagick->getImageWidth(), $imagick->getImageHeight(), $options);

		$this->imagick = $imagick;

		if($this->options->readerGrayscale){
			$this->imagick->setImageColorspace(Imagick::COLORSPACE_GRAY);
		}

		if($this->options->readerInvertColors){
			$this->imagick->negateImage($this->options->readerGrayscale);
		}

		if($this->options->readerIncreaseContrast){
			for($i = 0; $i < 10; $i++){
				$this->imagick->contrastImage(false); // misleading docs
			}
		}

		$this->setLuminancePixels();
	}

	/**
	 *
	 */
	protected function setLuminancePixels():void{
		$pixels = $this->imagick->exportImagePixels(1, 1, $this->width, $this->height, 'RGB', Imagick::PIXEL_CHAR);
		$count  = count($pixels);

		for($i = 0; $i < $count; $i += 3){
			$this->setLuminancePixel(($pixels[$i] & 0xff), ($pixels[($i + 1)] & 0xff), ($pixels[($i + 2)] & 0xff));
		}
	}

	/** @inheritDoc */
	public static function fromFile(string $path, ?SettingsContainerInterface $options = null):self{
		return new self(new Imagick(self::checkFile($path)), $options);
	}

	/** @inheritDoc */
	public static function fromBlob(string $blob, ?SettingsContainerInterface $options = null):self{
		$im = new Imagick;
		$im->readImageBlob($blob);

		return new self($im, $options);
	}

}