def check_bitdepth_rescale(
        palette, bitdepth, transparent, alpha, greyscale):
    """
    Returns (bitdepth, rescale) pair.
    """

    if palette:
        if len(bitdepth) != 1:
            raise ProtocolError(
                "with palette, only a single bitdepth may be used")
        (bitdepth, ) = bitdepth
        if bitdepth not in (1, 2, 4, 8):
            raise ProtocolError(
                "with palette, bitdepth must be 1, 2, 4, or 8")
        if transparent is not None:
            raise ProtocolError("transparent and palette not compatible")
        if alpha:
            raise ProtocolError("alpha and palette not compatible")
        if greyscale:
            raise ProtocolError("greyscale and palette not compatible")
        return bitdepth, None

    # No palette, check for sBIT chunk generation.

    if greyscale and not alpha:
        # Single channel, L.
        (bitdepth,) = bitdepth
        if bitdepth in (1, 2, 4, 8, 16):
            return bitdepth, None
        if bitdepth > 8:
            targetbitdepth = 16
        elif bitdepth == 3:
            targetbitdepth = 4
        else:
            assert bitdepth in (5, 6, 7)
            targetbitdepth = 8
        return targetbitdepth, [(bitdepth, targetbitdepth)]

    assert alpha or not greyscale

    depth_set = tuple(set(bitdepth))
    if depth_set in [(8,), (16,)]:
        # No sBIT required.
        (bitdepth, ) = depth_set
        return bitdepth, None

    targetbitdepth = (8, 16)[max(bitdepth) > 8]
    return targetbitdepth, [(b, targetbitdepth) for b in bitdepth]
def check_bitdepth_rescale ( palette , bitdepth , transparent , alpha , greyscale ) : if palette : if len ( bitdepth ) != 1 : raise ProtocolError ( "with palette, only a single bitdepth may be used" ) ( bitdepth , ) = bitdepth if bitdepth not in ( 1 , 2 , 4 , 8 ) : raise ProtocolError ( "with palette, bitdepth must be 1, 2, 4, or 8" ) if transparent is not None : raise ProtocolError ( "transparent and palette not compatible" ) if alpha : raise ProtocolError ( "alpha and palette not compatible" ) if greyscale : raise ProtocolError ( "greyscale and palette not compatible" ) return bitdepth , None if greyscale and not alpha : ( bitdepth , ) = bitdepth if bitdepth in ( 1 , 2 , 4 , 8 , 16 ) : return bitdepth , None if bitdepth > 8 : targetbitdepth = 16 elif bitdepth == 3 : targetbitdepth = 4 else : assert bitdepth in ( 5 , 6 , 7 ) targetbitdepth = 8 return targetbitdepth , [ ( bitdepth , targetbitdepth ) ] assert alpha or not greyscale depth_set = tuple ( set ( bitdepth ) ) if depth_set in [ ( 8 , ) , ( 16 , ) ] : ( bitdepth , ) = depth_set return bitdepth , None targetbitdepth = ( 8 , 16 ) [ max ( bitdepth ) > 8 ] return targetbitdepth , [ ( b , targetbitdepth ) for b in bitdepth ]
Returns (bitdepth, rescale) pair.