iTextSharp.text.pdf.qrcode.MaskUtil.ApplyMaskPenaltyRule1Internal C# (CSharp) Method

ApplyMaskPenaltyRule1Internal() private static method

private static ApplyMaskPenaltyRule1Internal ( ByteMatrix matrix, bool isHorizontal ) : int
matrix ByteMatrix
isHorizontal bool
return int
        private static int ApplyMaskPenaltyRule1Internal(ByteMatrix matrix, bool isHorizontal) {
            int penalty = 0;
            int numSameBitCells = 0;
            int prevBit = -1;
            // Horizontal mode:
            //   for (int i = 0; i < matrix.Height(); ++i) {
            //     for (int j = 0; j < matrix.Width(); ++j) {
            //       int bit = matrix.Get(i, j);
            // Vertical mode:
            //   for (int i = 0; i < matrix.Width(); ++i) {
            //     for (int j = 0; j < matrix.Height(); ++j) {
            //       int bit = matrix.Get(j, i);
            int iLimit = isHorizontal ? matrix.GetHeight() : matrix.GetWidth();
            int jLimit = isHorizontal ? matrix.GetWidth() : matrix.GetHeight();
            sbyte[][] array = matrix.GetArray();
            for (int i = 0; i < iLimit; ++i) {
                for (int j = 0; j < jLimit; ++j) {
                    int bit = isHorizontal ? array[i][j] : array[j][i];
                    if (bit == prevBit) {
                        numSameBitCells += 1;
                        // Found five repetitive cells with the same color (bit).
                        // We'll give penalty of 3.
                        if (numSameBitCells == 5) {
                            penalty += 3;
                        }
                        else if (numSameBitCells > 5) {
                            // After five repetitive cells, we'll add the penalty one
                            // by one.
                            penalty += 1;
                        }
                    }
                    else {
                        numSameBitCells = 1;  // Include the cell itself.
                        prevBit = bit;
                    }
                }
                numSameBitCells = 0;  // Clear at each row/column.
            }
            return penalty;
        }
    }