public static function applyMaskPenaltyRule4(ByteMatrix $matrix) : int
    {
        $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 (1 === $arrayY[$x]) {
                    ++$numDarkCells;
                }
            }
        }

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

        return $fixedPercentVariances * self::N4;
    }
public static function applyMaskPenaltyRule4(ByteMatrix $matrix) : int
    {
        $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 (1 === $arrayY[$x]) {
                    ++$numDarkCells;
                }
            }
        }

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

        return $fixedPercentVariances * self::N4;
    }
public static function applyMaskPenaltyRule4 ( ByteMatrix $ matrix ) : int { $ 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 ( 1 === $ arrayY [ $ x ] ) { ++ $ numDarkCells ; } } } $ numTotalCells = $ height * $ width ; $ darkRatio = $ numDarkCells / $ numTotalCells ; $ fixedPercentVariances = ( int ) ( abs ( $ darkRatio - 0.5 ) * 20 ) ; return $ fixedPercentVariances * self :: N4 ; }
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.
Applies mask penalty rule 4 and returns the penalty .