Skip to content

Commit 48a0350

Browse files
committed
:octocat: optional color negation in the reader
1 parent b4780d7 commit 48a0350

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

src/Decoder/GDLuminanceSource.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use chillerlan\Settings\SettingsContainerInterface;
1717
use function file_get_contents, get_resource_type, imagecolorat, imagecolorsforindex,
1818
imagecreatefromstring, imagefilter, imagesx, imagesy, is_resource;
19-
use const IMG_FILTER_BRIGHTNESS, IMG_FILTER_CONTRAST, IMG_FILTER_GRAYSCALE, PHP_MAJOR_VERSION;
19+
use const IMG_FILTER_BRIGHTNESS, IMG_FILTER_CONTRAST, IMG_FILTER_GRAYSCALE, IMG_FILTER_NEGATE, PHP_MAJOR_VERSION;
2020

2121
/**
2222
* This class is used to help decode images from files which arrive as GD Resource
@@ -41,7 +41,7 @@ public function __construct($gdImage, SettingsContainerInterface $options = null
4141

4242
/** @noinspection PhpFullyQualifiedNameUsageInspection */
4343
if(
44-
(PHP_MAJOR_VERSION >= 8 && !$gdImage instanceof \GdImage)
44+
(PHP_MAJOR_VERSION >= 8 && !$gdImage instanceof \GdImage) // @todo: remove version check in v6
4545
|| (PHP_MAJOR_VERSION < 8 && (!is_resource($gdImage) || get_resource_type($gdImage) !== 'gd'))
4646
){
4747
throw new QRCodeDecoderException('Invalid GD image source.'); // @codeCoverageIgnore
@@ -51,23 +51,27 @@ public function __construct($gdImage, SettingsContainerInterface $options = null
5151

5252
$this->gdImage = $gdImage;
5353

54-
$this->setLuminancePixels();
55-
}
56-
57-
/**
58-
*
59-
*/
60-
protected function setLuminancePixels():void{
61-
6254
if($this->options->readerGrayscale){
6355
imagefilter($this->gdImage, IMG_FILTER_GRAYSCALE);
6456
}
6557

58+
if($this->options->readerInvertColors){
59+
imagefilter($this->gdImage, IMG_FILTER_NEGATE);
60+
}
61+
6662
if($this->options->readerIncreaseContrast){
6763
imagefilter($this->gdImage, IMG_FILTER_BRIGHTNESS, -100);
6864
imagefilter($this->gdImage, IMG_FILTER_CONTRAST, -100);
6965
}
7066

67+
$this->setLuminancePixels();
68+
}
69+
70+
/**
71+
*
72+
*/
73+
protected function setLuminancePixels():void{
74+
7175
for($j = 0; $j < $this->height; $j++){
7276
for($i = 0; $i < $this->width; $i++){
7377
$argb = imagecolorat($this->gdImage, $i, $j);

src/Decoder/IMagickLuminanceSource.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,27 @@ public function __construct(Imagick $imagick, SettingsContainerInterface $option
3333

3434
$this->imagick = $imagick;
3535

36-
$this->setLuminancePixels();
37-
}
38-
39-
/**
40-
*
41-
*/
42-
protected function setLuminancePixels():void{
43-
4436
if($this->options->readerGrayscale){
4537
$this->imagick->setImageColorspace(Imagick::COLORSPACE_GRAY);
4638
}
4739

40+
if($this->options->readerInvertColors){
41+
$this->imagick->negateImage($this->options->readerGrayscale);
42+
}
43+
4844
if($this->options->readerIncreaseContrast){
4945
for($i = 0; $i < 10; $i++){
5046
$this->imagick->contrastImage(false); // misleading docs
5147
}
5248
}
5349

50+
$this->setLuminancePixels();
51+
}
52+
53+
/**
54+
*
55+
*/
56+
protected function setLuminancePixels():void{
5457
$pixels = $this->imagick->exportImagePixels(1, 1, $this->width, $this->height, 'RGB', Imagick::PIXEL_CHAR);
5558
$count = count($pixels);
5659

src/QROptionsTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ trait QROptionsTrait{
466466
*/
467467
protected bool $readerGrayscale = false;
468468

469+
/**
470+
* Invert the colors of the image
471+
*/
472+
protected bool $readerInvertColors = false;
473+
469474
/**
470475
* Increase the contrast before reading
471476
*

0 commit comments

Comments
 (0)