def count_mask_pixels(b_mask, value, equal=True, bitwise_and=False):
    """ Count all mask pixels satisfying the match criteria:

        equal   bitwise_and   expression
        ------------------------------------------------
        False   False         mask[...] != 0
        True    False         mask[...] == value         [DEFAULT]
        False   True          mask[...] & value != 0
        True    True          mask[...] & value == value
    """
    if b_mask.shape[-1] < 1:
        raise ValueError("The mask has to have at least one band.")
    mask = b_mask.data[..., 0]
    if bitwise_and:
        mask &= value
    return ((mask == value) if equal else (mask != 0)).sum()