Skip to content

Commit fd4d208

Browse files
committed
:octocat: reduce calls to QRMatrix::getMatrix() and use QRMatrix::get() instead
1 parent db300ba commit fd4d208

File tree

9 files changed

+52
-38
lines changed

9 files changed

+52
-38
lines changed

examples/svgMeltedModules.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ protected function collectModules(Closure $transform):array{
3838
$paths = [];
3939

4040
// collect the modules for each type
41-
foreach($this->matrix->getMatrix() as $y => $row){
42-
foreach($row as $x => $M_TYPE){
41+
for($y = 0; $y < $this->moduleCount; $y++){
42+
for($x = 0; $x < $this->moduleCount; $x++){
43+
$M_TYPE = $this->matrix->get($x, $y);
4344
$M_TYPE_LAYER = $M_TYPE;
4445

4546
if($this->options->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)){

examples/svgRandomColoredDots.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ protected function collectModules(Closure $transform):array{
4141
$paths = [];
4242

4343
// collect the modules for each type
44-
foreach($this->matrix->getMatrix() as $y => $row){
45-
foreach($row as $x => $M_TYPE){
44+
for($y = 0; $y < $this->moduleCount; $y++){
45+
for($x = 0; $x < $this->moduleCount; $x++){
46+
$M_TYPE = $this->matrix->get($x, $y);
4647
$M_TYPE_LAYER = $M_TYPE;
4748

4849
if($this->options->connectPaths

examples/svgRoundQuietzone.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ protected function colorQuietzone(int $quietzoneSize, float $radius):void{
8787
// substract 1/2 stroke width and module radius from the circle radius to not cut off modules
8888
$r = ($radius - $this->options->circleRadius * 2);
8989

90-
foreach($this->matrix->getMatrix() as $y => $row){
91-
foreach($row as $x => $value){
90+
for($y = 0; $y < $this->moduleCount; $y++){
91+
for($x = 0; $x < $this->moduleCount; $x++){
9292

9393
// skip anything that's not quiet zone
94-
if($value !== QRMatrix::M_QUIETZONE){
94+
if(!$this->matrix->checkType($x, $y, QRMatrix::M_QUIETZONE)){
9595
continue;
9696
}
9797

@@ -175,8 +175,9 @@ protected function collectModules(Closure $transform):array{
175175
$paths = [];
176176

177177
// collect the modules for each type
178-
foreach($this->matrix->getMatrix() as $y => $row){
179-
foreach($row as $x => $M_TYPE){
178+
for($y = 0; $y < $this->moduleCount; $y++){
179+
for($x = 0; $x < $this->moduleCount; $x++){
180+
$M_TYPE = $this->matrix->get($x, $y);
180181
$M_TYPE_LAYER = $M_TYPE;
181182

182183
if(!$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)){

src/Output/QRFpdf.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,14 @@ public function dump(string $file = null){
115115

116116
$prevColor = null;
117117

118-
foreach($this->matrix->getMatrix() as $y => $row){
119-
120-
foreach($row as $x => $M_TYPE){
118+
for($y = 0; $y < $this->moduleCount; $y++){
119+
for($x = 0; $x < $this->moduleCount; $x++){
121120

122121
if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
123122
continue;
124123
}
125124

126-
$color = $this->getModuleValue($M_TYPE);
125+
$color = $this->getModuleValueAt($x, $y);
127126

128127
if($prevColor !== $color){
129128
/** @phan-suppress-next-line PhanParamTooFewUnpack */

src/Output/QRGdImage.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,38 +229,40 @@ protected function setTransparencyColor():void{
229229
* Creates the QR image
230230
*/
231231
protected function drawImage():void{
232-
foreach($this->matrix->getMatrix() as $y => $row){
233-
foreach($row as $x => $M_TYPE){
234-
$this->setPixel($x, $y, $M_TYPE);
232+
for($y = 0; $y < $this->moduleCount; $y++){
233+
for($x = 0; $x < $this->moduleCount; $x++){
234+
$this->setPixel($x, $y);
235235
}
236236
}
237237
}
238238

239239
/**
240240
* Creates a single QR pixel with the given settings
241241
*/
242-
protected function setPixel(int $x, int $y, int $M_TYPE):void{
242+
protected function setPixel(int $x, int $y):void{
243243

244244
if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
245245
return;
246246
}
247247

248+
$color = $this->getModuleValueAt($x, $y);
249+
248250
$this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)
249251
? imagefilledellipse(
250252
$this->image,
251253
(int)(($x * $this->scale) + ($this->scale / 2)),
252254
(int)(($y * $this->scale) + ($this->scale / 2)),
253255
(int)(2 * $this->options->circleRadius * $this->scale),
254256
(int)(2 * $this->options->circleRadius * $this->scale),
255-
$this->getModuleValue($M_TYPE)
257+
$color
256258
)
257259
: imagefilledrectangle(
258260
$this->image,
259261
($x * $this->scale),
260262
($y * $this->scale),
261263
(($x + 1) * $this->scale),
262264
(($y + 1) * $this->scale),
263-
$this->getModuleValue($M_TYPE)
265+
$color
264266
);
265267
}
266268

src/Output/QRImagick.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ protected function drawImage():void{
186186
$this->imagickDraw = new ImagickDraw;
187187
$this->imagickDraw->setStrokeWidth(0);
188188

189-
foreach($this->matrix->getMatrix() as $y => $row){
190-
foreach($row as $x => $M_TYPE){
191-
$this->setPixel($x, $y, $M_TYPE);
189+
for($y = 0; $y < $this->moduleCount; $y++){
190+
for($x = 0; $x < $this->moduleCount; $x++){
191+
$this->setPixel($x, $y);
192192
}
193193
}
194194

@@ -198,13 +198,13 @@ protected function drawImage():void{
198198
/**
199199
* draws a single pixel at the given position
200200
*/
201-
protected function setPixel(int $x, int $y, int $M_TYPE):void{
201+
protected function setPixel(int $x, int $y):void{
202202

203203
if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
204204
return;
205205
}
206206

207-
$this->imagickDraw->setFillColor($this->getModuleValue($M_TYPE));
207+
$this->imagickDraw->setFillColor($this->getModuleValueAt($x, $y));
208208

209209
$this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)
210210
? $this->imagickDraw->circle(

src/Output/QRMarkupHTML.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ protected function createMarkup(bool $saveToFile):string{
2727

2828
$html .= $this->options->eol;
2929

30-
foreach($this->matrix->getMatrix() as $row){
30+
for($y = 0; $y < $this->moduleCount; $y++){
3131
$html .= '<div>';
3232

33-
foreach($row as $M_TYPE){
34-
$html .= sprintf('<span style="background: %s;"></span>', $this->getModuleValue($M_TYPE));
33+
for($x = 0; $x < $this->moduleCount; $x++){
34+
$html .= sprintf('<span style="background: %s;"></span>', $this->getModuleValueAt($x, $y));
3535
}
3636

3737
$html .= '</div>'.$this->options->eol;

src/Output/QROutputAbstract.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,36 @@ protected function setModuleValues():void{
9292
}
9393

9494
/**
95-
* Returns the prepared value for the given $M_TYPE (return value depends on the output class)
95+
* Returns the prepared value for the given $M_TYPE
9696
*
97-
* @return mixed|null
97+
* @return mixed|null return value depends on the output class
9898
*/
9999
protected function getModuleValue(int $M_TYPE){
100100
return ($this->moduleValues[$M_TYPE] ?? null);
101101
}
102102

103103
/**
104-
* Prepares the value for the given input (return value depends on the output class)
104+
* Returns the prepared module value at the given coordinate [$x, $y] (convenience)
105+
*
106+
* @return mixed|null
107+
*/
108+
protected function getModuleValueAt(int $x, int $y){
109+
return $this->getModuleValue($this->matrix->get($x, $y));
110+
}
111+
112+
/**
113+
* Prepares the value for the given input ()
105114
*
106115
* @param mixed $value
107116
*
108-
* @return mixed
117+
* @return mixed|null return value depends on the output class
109118
*/
110119
abstract protected function prepareModuleValue($value);
111120

112121
/**
113-
* Returns a default value for either dark or light modules (return value depends on the output class)
122+
* Returns a default value for either dark or light modules
114123
*
115-
* @return mixed
124+
* @return mixed|null return value depends on the output class
116125
*/
117126
abstract protected function getDefaultModuleValue(bool $isDark);
118127

@@ -161,8 +170,9 @@ protected function collectModules(Closure $transform):array{
161170
$paths = [];
162171

163172
// collect the modules for each type
164-
foreach($this->matrix->getMatrix() as $y => $row){
165-
foreach($row as $x => $M_TYPE){
173+
for($y = 0; $y < $this->moduleCount; $y++){
174+
for($x = 0; $x < $this->moduleCount; $x++){
175+
$M_TYPE = $this->matrix->get($x, $y);
166176
$M_TYPE_LAYER = $M_TYPE;
167177

168178
if($this->options->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)){

src/Output/QRString.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ public function dump(string $file = null):string{
6666
protected function text():string{
6767
$str = [];
6868

69-
foreach($this->matrix->getMatrix() as $row){
69+
for($y = 0; $y < $this->moduleCount; $y++){
7070
$r = [];
7171

72-
foreach($row as $M_TYPE){
73-
$r[] = $this->getModuleValue($M_TYPE);
72+
for($x = 0; $x < $this->moduleCount; $x++){
73+
$r[] = $this->getModuleValueAt($x, $y);
7474
}
7575

7676
$str[] = implode('', $r);

0 commit comments

Comments
 (0)