MaskUtil.php 8.73 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
<?php
/**
 * BaconQrCode
 *
 * @link      http://github.com/Bacon/BaconQrCode For the canonical source repository
 * @copyright 2013 Ben 'DASPRiD' Scholzen
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
 */

namespace BaconQrCode\Encoder;

use BaconQrCode\Common\BitUtils;

/**
 * Mask utility.
 */
class MaskUtil
{
    /**#@+
     * Penalty weights from section 6.8.2.1
     */
    const N1 = 3;
    const N2 = 3;
    const N3 = 40;
    const N4 = 10;
    /**#@-*/

    /**
     * Applies mask penalty rule 1 and returns the penalty.
     *
     * Finds repetitive cells with the same color and gives penalty to them.
     * Example: 00000 or 11111.
     *
     * @param  ByteMatrix $matrix
     * @return integer
     */
    public static function applyMaskPenaltyRule1(ByteMatrix $matrix)
    {
        return (
            self::applyMaskPenaltyRule1Internal($matrix, true)
            + self::applyMaskPenaltyRule1Internal($matrix, false)
        );
    }

    /**
     * Applies mask penalty rule 2 and returns the penalty.
     *
     * Finds 2x2 blocks with the same color and gives penalty to them. This is
     * actually equivalent to the spec's rule, which is to find MxN blocks and
     * give a penalty proportional to (M-1)x(N-1), because this is the number of
     * 2x2 blocks inside such a block.
     *
     * @param  ByteMatrix $matrix
     * @return integer
     */
    public static function applyMaskPenaltyRule2(ByteMatrix $matrix)
    {
        $penalty = 0;
        $array   = $matrix->getArray();
        $width   = $matrix->getWidth();
        $height  = $matrix->getHeight();

        for ($y = 0; $y < $height - 1; $y++) {
            for ($x = 0; $x < $width - 1; $x++) {
                $value = $array[$y][$x];

                if ($value === $array[$y][$x + 1] && $value === $array[$y + 1][$x] && $value === $array[$y + 1][$x + 1]) {
                    $penalty++;
                }
            }
        }

        return self::N2 * $penalty;
    }

    /**
     * Applies mask penalty rule 3 and returns the penalty.
     *
     * Finds consecutive cells of 00001011101 or 10111010000, and gives penalty
     * to them. If we find patterns like 000010111010000, we give penalties
     * twice (i.e. 40 * 2).
     *
     * @param  ByteMatrix $matrix
     * @return integer
     */
    public static function applyMaskPenaltyRule3(ByteMatrix $matrix)
    {
        $penalty = 0;
        $array   = $matrix->getArray();
        $width   = $matrix->getWidth();
        $height  = $matrix->getHeight();

        for ($y = 0; $y < $height; $y++) {
            for ($x = 0; $x < $width; $x++) {
                if (
                    $x + 6 < $width
                    && $array[$y][$x] === 1
                    && $array[$y][$x + 1] === 0
                    && $array[$y][$x + 2] === 1
                    && $array[$y][$x + 3] === 1
                    && $array[$y][$x + 4] === 1
                    && $array[$y][$x + 5] === 0
                    && $array[$y][$x + 6] === 1
                    && (
                        (
                            $x + 10 < $width
                            && $array[$y][$x + 7] === 0
                            && $array[$y][$x + 8] === 0
                            && $array[$y][$x + 9] === 0
                            && $array[$y][$x + 10] === 0
                        )
                        || (
                            $x - 4 >= 0
                            && $array[$y][$x - 1] === 0
                            && $array[$y][$x - 2] === 0
                            && $array[$y][$x - 3] === 0
                            && $array[$y][$x - 4] === 0
                        )
                    )
                ) {
                    $penalty += self::N3;
                }

                if (
                    $y + 6 < $height
                    && $array[$y][$x] === 1
                    && $array[$y + 1][$x] === 0
                    && $array[$y + 2][$x] === 1
                    && $array[$y + 3][$x] === 1
                    && $array[$y + 4][$x] === 1
                    && $array[$y + 5][$x] === 0
                    && $array[$y + 6][$x] === 1
                    && (
                        (
                            $y + 10 < $height
                            && $array[$y + 7][$x] === 0
                            && $array[$y + 8][$x] === 0
                            && $array[$y + 9][$x] === 0
                            && $array[$y + 10][$x] === 0
                        )
                        || (
                            $y - 4 >= 0
                            && $array[$y - 1][$x] === 0
                            && $array[$y - 2][$x] === 0
                            && $array[$y - 3][$x] === 0
                            && $array[$y - 4][$x] === 0
                        )
                    )
                ) {
                    $penalty += self::N3;
                }
            }
        }

        return $penalty;
    }

    /**
     * Applies mask penalty rule 4 and returns the penalty.
     *
     * Calculates the ratio of dark cells and gives penalty if the ratio is far
     * from 50%. It gives 10 penalty for 5% distance.
     *
     * @param  ByteMatrix $matrix
     * @return integer
     */
    public static function applyMaskPenaltyRule4(ByteMatrix $matrix)
    {
        $numDarkCells = 0;

        $array  = $matrix->getArray();
        $width  = $matrix->getWidth();
        $height = $matrix->getHeight();

        for ($y = 0; $y < $height; $y++) {
            $arrayY = $array[$y];

            for ($x = 0; $x < $width; $x++) {
                if ($arrayY[$x] === 1) {
                    $numDarkCells++;
                }
            }
        }

        $numTotalCells         = $height * $width;
        $darkRatio             = $numDarkCells / $numTotalCells;
        $fixedPercentVariances = (int) (abs($darkRatio - 0.5) * 20);

        return $fixedPercentVariances * self::N4;
    }

    /**
     * Returns the mask bit for "getMaskPattern" at "x" and "y".
     *
     * See 8.8 of JISX0510:2004 for mask pattern conditions.
     *
     * @param  integer $maskPattern
     * @param  integer $x
     * @param  integer $y
     * @return integer
     * @throws Exception\InvalidArgumentException
     */
    public static function getDataMaskBit($maskPattern, $x, $y)
    {
        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) + ($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 Exception\InvalidArgumentException('Invalid mask pattern: ' . $maskPattern);
        }

        return $intermediate === 0;
    }

    /**
     * Helper function for applyMaskPenaltyRule1.
     *
     * We need this for doing this calculation in both vertical and horizontal
     * orders respectively.
     *
     * @param  ByteMatrix $matrix
     * @param  boolean    $isHorizontal
     * @return integer
     */
    protected static function applyMaskPenaltyRule1Internal(ByteMatrix $matrix, $isHorizontal)
    {
        $penalty = 0;
        $iLimit  = $isHorizontal ? $matrix->getHeight() : $matrix->getWidth();
        $jLimit  = $isHorizontal ? $matrix->getWidth() : $matrix->getHeight();
        $array   = $matrix->getArray();

        for ($i = 0; $i < $iLimit; $i++) {
            $numSameBitCells = 0;
            $prevBit         = -1;

            for ($j = 0; $j < $jLimit; $j++) {
                $bit = $isHorizontal ? $array[$i][$j] : $array[$j][$i];

                if ($bit === $prevBit) {
                    $numSameBitCells++;
                } else {
                    if ($numSameBitCells >= 5) {
                        $penalty += self::N1 + ($numSameBitCells - 5);
                    }

                    $numSameBitCells = 1;
                    $prevBit         = $bit;
                }
            }

            if ($numSameBitCells >= 5) {
                $penalty += self::N1 + ($numSameBitCells - 5);
            }
        }

        return $penalty;
    }
}