def pixel_to_terrain_type(pixel):
    """
    Convert a RGBA pixel to a terrain type.

    The B channel composes the lower 8 bits, the A channel composes the upper 8 bits.
    Note that some images do not have an alpha channel.
    """
    if len(pixel) > 3:
        return pixel[2] & 0xff | ((pixel[3] & 0xff) << 8)
    else:
        return pixel[2] & 0xff