Explorar o código

:octocat: +QRMatrix::checkNeighbours()

smiley %!s(int64=3) %!d(string=hai) anos
pai
achega
2a5214b070
Modificáronse 1 ficheiros con 47 adicións e 0 borrados
  1. 47 0
      src/Data/QRMatrix.php

+ 47 - 0
src/Data/QRMatrix.php

@@ -50,6 +50,24 @@ class QRMatrix{
 	/** @var int */
 	/** @var int */
 	public const IS_DARK      = 0b100000000000;
 	public const IS_DARK      = 0b100000000000;
 
 
+	/**
+	 * Map of flag => coord
+	 *
+	 * @see \chillerlan\QRCode\Data\QRMatrix::checkNeighbours()
+	 *
+	 * @var array
+	 */
+	protected const neighbours = [
+		0b00000001 => [-1, -1],
+		0b00000010 => [ 0, -1],
+		0b00000100 => [ 1, -1],
+		0b00001000 => [ 1,  0],
+		0b00010000 => [ 1,  1],
+		0b00100000 => [ 0,  1],
+		0b01000000 => [-1,  1],
+		0b10000000 => [-1,  0]
+	];
+
 	/**
 	/**
 	 * the used mask pattern, set via QRMatrix::mask()
 	 * the used mask pattern, set via QRMatrix::mask()
 	 */
 	 */
@@ -235,6 +253,35 @@ class QRMatrix{
 		return $this->checkType($x, $y, $this::IS_DARK);
 		return $this->checkType($x, $y, $this::IS_DARK);
 	}
 	}
 
 
+	/**
+	 * Checks the status neighbouring modules of the given module at ($x, $y) and returns a bitmask with the results.
+	 *
+	 * The 8 flags of the bitmask represent the status of each of the neighbouring fields,
+	 * starting with the lowest bit for top left, going clockwise:
+	 *
+	 *   1 2 3
+	 *   8 # 4
+	 *   7 6 5
+	 */
+	public function checkNeighbours(int $x, int $y, int $M_TYPE_VALUE = null):int{
+		$bits = 0;
+
+		foreach($this::neighbours as $bit => $coord){
+			[$ix, $iy] = $coord;
+
+			// check if the field is the same type
+			if($M_TYPE_VALUE !== null && ($this->get($x + $ix, $y + $iy) | $this::IS_DARK) !== ($M_TYPE_VALUE | $this::IS_DARK)){
+				continue;
+			}
+
+			if($this->checkType($x + $ix, $y + $iy, $this::IS_DARK)){
+				$bits |= $bit;
+			}
+		}
+
+		return $bits;
+	}
+
 	/**
 	/**
 	 * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder
 	 * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder
 	 *
 	 *