Skip to content

Commit efb829c

Browse files
committed
:octocat: fully support reflectance reversal
1 parent 5a280c8 commit efb829c

File tree

2 files changed

+119
-70
lines changed

2 files changed

+119
-70
lines changed

src/Data/QRMatrix.php

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,77 @@
2121
*/
2222
class QRMatrix{
2323

24+
/*
25+
* special values
26+
*/
27+
2428
/** @var int */
25-
public const IS_DARK = 0b100000000000;
29+
public const IS_DARK = 0b100000000000;
2630
/** @var int */
27-
public const M_NULL = 0b000000000000;
31+
public const M_NULL = 0b000000000000;
2832
/** @var int */
29-
public const M_DARKMODULE = 0b100000000001;
33+
public const M_LOGO = 0b001000000000;
3034
/** @var int */
31-
public const M_DATA = 0b000000000010;
35+
public const M_LOGO_DARK = 0b101000000000;
3236
/** @var int */
33-
public const M_DATA_DARK = 0b100000000010;
37+
public const M_TEST = 0b011111111111;
3438
/** @var int */
35-
public const M_FINDER = 0b000000000100;
39+
public const M_TEST_DARK = 0b111111111111;
40+
41+
/*
42+
* light values
43+
*/
44+
3645
/** @var int */
37-
public const M_FINDER_DARK = 0b100000000100;
46+
public const M_DATA = 0b000000000010;
3847
/** @var int */
39-
public const M_SEPARATOR = 0b000000001000;
48+
public const M_FINDER = 0b000000000100;
4049
/** @var int */
41-
public const M_SEPARATOR_DARK = 0b100000001000;
50+
public const M_SEPARATOR = 0b000000001000;
4251
/** @var int */
43-
public const M_ALIGNMENT = 0b000000010000;
52+
public const M_ALIGNMENT = 0b000000010000;
4453
/** @var int */
45-
public const M_ALIGNMENT_DARK = 0b100000010000;
54+
public const M_TIMING = 0b000000100000;
4655
/** @var int */
47-
public const M_TIMING = 0b000000100000;
56+
public const M_FORMAT = 0b000001000000;
4857
/** @var int */
49-
public const M_TIMING_DARK = 0b100000100000;
58+
public const M_VERSION = 0b000010000000;
5059
/** @var int */
51-
public const M_FORMAT = 0b000001000000;
60+
public const M_QUIETZONE = 0b000100000000;
61+
62+
/*
63+
* dark values
64+
*/
65+
5266
/** @var int */
53-
public const M_FORMAT_DARK = 0b100001000000;
67+
public const M_DARKMODULE = 0b100000000001;
5468
/** @var int */
55-
public const M_VERSION = 0b000010000000;
69+
public const M_DATA_DARK = 0b100000000010;
5670
/** @var int */
57-
public const M_VERSION_DARK = 0b100010000000;
71+
public const M_FINDER_DARK = 0b100000000100;
5872
/** @var int */
59-
public const M_QUIETZONE = 0b000100000000;
73+
public const M_ALIGNMENT_DARK = 0b100000010000;
6074
/** @var int */
61-
public const M_QUIETZONE_DARK = 0b100100000000;
75+
public const M_TIMING_DARK = 0b100000100000;
6276
/** @var int */
63-
public const M_LOGO = 0b001000000000;
77+
public const M_FORMAT_DARK = 0b100001000000;
6478
/** @var int */
65-
public const M_LOGO_DARK = 0b101000000000;
79+
public const M_VERSION_DARK = 0b100010000000;
6680
/** @var int */
67-
public const M_FINDER_DOT = 0b110000000000;
81+
public const M_FINDER_DOT = 0b110000000000;
82+
83+
/*
84+
* values used for reversed reflectance
85+
*/
86+
87+
/** @var int */
88+
public const M_DARKMODULE_LIGHT = 0b000000000001;
6889
/** @var int */
69-
public const M_TEST = 0b011111111111;
90+
public const M_FINDER_DOT_LIGHT = 0b010000000000;
7091
/** @var int */
71-
public const M_TEST_DARK = 0b111111111111;
92+
public const M_SEPARATOR_DARK = 0b100000001000;
93+
/** @var int */
94+
public const M_QUIETZONE_DARK = 0b100100000000;
7295

7396
/**
7497
* Map of flag => coord
@@ -579,6 +602,28 @@ public function rotate90():self{
579602
return $this;
580603
}
581604

605+
/**
606+
* Inverts the values of the whole matrix
607+
*
608+
* ISO/IEC 18004:2015 Section 6.2 - Reflectance reversal
609+
*/
610+
public function invert():self{
611+
612+
foreach($this->matrix as $y => $row){
613+
foreach($row as $x => $val){
614+
615+
// skip null fields
616+
if($val === $this::M_NULL){
617+
continue;
618+
}
619+
620+
$this->set($x, $y, ($val & $this::IS_DARK) !== $this::IS_DARK, $val);
621+
}
622+
}
623+
624+
return $this;
625+
}
626+
582627
/**
583628
* Clears a space of $width * $height in order to add a logo or text.
584629
* If no $height is given, the space will be assumed a square of $width.

src/Output/QROutputInterface.php

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,32 @@ interface QROutputInterface{
6565
*/
6666
public const DEFAULT_MODULE_VALUES = [
6767
// light
68-
QRMatrix::M_NULL => false,
69-
QRMatrix::M_DATA => false,
70-
QRMatrix::M_FINDER => false,
71-
QRMatrix::M_SEPARATOR => false,
72-
QRMatrix::M_ALIGNMENT => false,
73-
QRMatrix::M_TIMING => false,
74-
QRMatrix::M_FORMAT => false,
75-
QRMatrix::M_VERSION => false,
76-
QRMatrix::M_QUIETZONE => false,
77-
QRMatrix::M_LOGO => false,
78-
QRMatrix::M_TEST => false,
68+
QRMatrix::M_NULL => false,
69+
QRMatrix::M_DARKMODULE_LIGHT => false,
70+
QRMatrix::M_DATA => false,
71+
QRMatrix::M_FINDER => false,
72+
QRMatrix::M_SEPARATOR => false,
73+
QRMatrix::M_ALIGNMENT => false,
74+
QRMatrix::M_TIMING => false,
75+
QRMatrix::M_FORMAT => false,
76+
QRMatrix::M_VERSION => false,
77+
QRMatrix::M_QUIETZONE => false,
78+
QRMatrix::M_LOGO => false,
79+
QRMatrix::M_FINDER_DOT_LIGHT => false,
80+
QRMatrix::M_TEST => false,
7981
// dark
80-
QRMatrix::M_DARKMODULE => true,
81-
QRMatrix::M_DATA_DARK => true,
82-
QRMatrix::M_FINDER_DARK => true,
83-
QRMatrix::M_SEPARATOR_DARK => true,
84-
QRMatrix::M_ALIGNMENT_DARK => true,
85-
QRMatrix::M_TIMING_DARK => true,
86-
QRMatrix::M_FORMAT_DARK => true,
87-
QRMatrix::M_VERSION_DARK => true,
88-
QRMatrix::M_QUIETZONE_DARK => true,
89-
QRMatrix::M_LOGO_DARK => true,
90-
QRMatrix::M_FINDER_DOT => true,
91-
QRMatrix::M_TEST_DARK => true,
82+
QRMatrix::M_DARKMODULE => true,
83+
QRMatrix::M_DATA_DARK => true,
84+
QRMatrix::M_FINDER_DARK => true,
85+
QRMatrix::M_SEPARATOR_DARK => true,
86+
QRMatrix::M_ALIGNMENT_DARK => true,
87+
QRMatrix::M_TIMING_DARK => true,
88+
QRMatrix::M_FORMAT_DARK => true,
89+
QRMatrix::M_VERSION_DARK => true,
90+
QRMatrix::M_QUIETZONE_DARK => true,
91+
QRMatrix::M_LOGO_DARK => true,
92+
QRMatrix::M_FINDER_DOT => true,
93+
QRMatrix::M_TEST_DARK => true,
9294
];
9395

9496
/**
@@ -98,30 +100,32 @@ interface QROutputInterface{
98100
*/
99101
public const LAYERNAMES = [
100102
// light
101-
QRMatrix::M_NULL => 'null',
102-
QRMatrix::M_DATA => 'data',
103-
QRMatrix::M_FINDER => 'finder',
104-
QRMatrix::M_SEPARATOR => 'separator',
105-
QRMatrix::M_ALIGNMENT => 'alignment',
106-
QRMatrix::M_TIMING => 'timing',
107-
QRMatrix::M_FORMAT => 'format',
108-
QRMatrix::M_VERSION => 'version',
109-
QRMatrix::M_QUIETZONE => 'quietzone',
110-
QRMatrix::M_LOGO => 'logo',
111-
QRMatrix::M_TEST => 'test',
103+
QRMatrix::M_NULL => 'null',
104+
QRMatrix::M_DARKMODULE_LIGHT => 'darkmodule-light',
105+
QRMatrix::M_DATA => 'data',
106+
QRMatrix::M_FINDER => 'finder',
107+
QRMatrix::M_SEPARATOR => 'separator',
108+
QRMatrix::M_ALIGNMENT => 'alignment',
109+
QRMatrix::M_TIMING => 'timing',
110+
QRMatrix::M_FORMAT => 'format',
111+
QRMatrix::M_VERSION => 'version',
112+
QRMatrix::M_QUIETZONE => 'quietzone',
113+
QRMatrix::M_LOGO => 'logo',
114+
QRMatrix::M_FINDER_DOT_LIGHT => 'finder-dot-light',
115+
QRMatrix::M_TEST => 'test',
112116
// dark
113-
QRMatrix::M_DARKMODULE => 'darkmodule',
114-
QRMatrix::M_DATA_DARK => 'data-dark',
115-
QRMatrix::M_FINDER_DARK => 'finder-dark',
116-
QRMatrix::M_SEPARATOR_DARK => 'separator-dark',
117-
QRMatrix::M_ALIGNMENT_DARK => 'alignment-dark',
118-
QRMatrix::M_TIMING_DARK => 'timing-dark',
119-
QRMatrix::M_FORMAT_DARK => 'format-dark',
120-
QRMatrix::M_VERSION_DARK => 'version-dark',
121-
QRMatrix::M_QUIETZONE_DARK => 'quietzone-dark',
122-
QRMatrix::M_LOGO_DARK => 'logo-dark',
123-
QRMatrix::M_FINDER_DOT => 'finder-dot',
124-
QRMatrix::M_TEST_DARK => 'test-dark',
117+
QRMatrix::M_DARKMODULE => 'darkmodule',
118+
QRMatrix::M_DATA_DARK => 'data-dark',
119+
QRMatrix::M_FINDER_DARK => 'finder-dark',
120+
QRMatrix::M_SEPARATOR_DARK => 'separator-dark',
121+
QRMatrix::M_ALIGNMENT_DARK => 'alignment-dark',
122+
QRMatrix::M_TIMING_DARK => 'timing-dark',
123+
QRMatrix::M_FORMAT_DARK => 'format-dark',
124+
QRMatrix::M_VERSION_DARK => 'version-dark',
125+
QRMatrix::M_QUIETZONE_DARK => 'quietzone-dark',
126+
QRMatrix::M_LOGO_DARK => 'logo-dark',
127+
QRMatrix::M_FINDER_DOT => 'finder-dot',
128+
QRMatrix::M_TEST_DARK => 'test-dark',
125129
];
126130

127131
/**

0 commit comments

Comments
 (0)