question: public static int applyMaskPenaltyRule4 ( ByteMatrix matrix ) { int numDarkCells = 0 ; byte [ ] [ ] array = matrix . getArray ( ) ; int width = matrix . getWidth ( ) ; int height = matrix . getHeight ( ) ; for ( int y = 0 ; y < height ; ++ y ) { for ( int x = 0 ; x < width ; ++ x ) { if ( array [ y ] [ x ] == 1 ) { numDarkCells += 1 ; } } } int numTotalCells = matrix . getHeight ( ) * matrix . getWidth ( ) ; double darkRatio = ( double ) numDarkCells / numTotalCells ; return Math . abs ( ( int ) ( darkRatio * 100 - 50 ) ) / 5 * 10 ; }
target:  Apply mask penalty rule 4 and return the penalty. Calculate the ratio of dark cells and give penalty if the ratio is far from 50%. It gives 10 penalty for 5% distance. Examples: -   0% => 100 -  40% =>  20 -  45% =>  10 -  50% =>   0 -  55% =>  10 -  55% =>  20 - 100% => 100