Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 10 additions & 46 deletions src/Encoder/MaskUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

namespace BaconQrCode\Encoder;

use BaconQrCode\Common\BitUtils;
use BaconQrCode\Exception\InvalidArgumentException;

/**
* Mask utility.
*/
Expand Down Expand Up @@ -181,52 +178,19 @@ public static function applyMaskPenaltyRule4(ByteMatrix $matrix) : int
* Returns the mask bit for "getMaskPattern" at "x" and "y".
*
* See 8.8 of JISX0510:2004 for mask pattern conditions.
*
* @throws InvalidArgumentException if an invalid mask pattern was supplied
*/
public static function getDataMaskBit(int $maskPattern, int $x, int $y) : bool
{
switch ($maskPattern) {
case 0:
$intermediate = ($y + $x) & 0x1;
break;

case 1:
$intermediate = $y & 0x1;
break;

case 2:
$intermediate = $x % 3;
break;

case 3:
$intermediate = ($y + $x) % 3;
break;

case 4:
$intermediate = (BitUtils::unsignedRightShift($y, 1) + (int) ($x / 3)) & 0x1;
break;

case 5:
$temp = $y * $x;
$intermediate = ($temp & 0x1) + ($temp % 3);
break;

case 6:
$temp = $y * $x;
$intermediate = (($temp & 0x1) + ($temp % 3)) & 0x1;
break;

case 7:
$temp = $y * $x;
$intermediate = (($temp % 3) + (($y + $x) & 0x1)) & 0x1;
break;

default:
throw new InvalidArgumentException('Invalid mask pattern: ' . $maskPattern);
}

return 0 == $intermediate;
return 0 === match ($maskPattern) {
0 => ($x + $y) % 2,
1 => $y % 2,
2 => $x % 3,
3 => ($x + $y) % 3,
Comment thread
DASPRiD marked this conversation as resolved.
Resolved
4 => (intdiv($y, 2) + intdiv($x, 3)) % 2,
5 => (($x * $y) % 2) + (($x * $y) % 3),
6 => ((($x * $y) % 2) + (($x * $y) % 3)) % 2,
7 => ((($x + $y) % 2) + (($x * $y) % 3)) % 2,
Comment thread
DASPRiD marked this conversation as resolved.
Resolved
};
}

/**
Expand Down
Loading