def compute_sub_mask(outer_mask, outer_rect, inner_rect):
    """ Given a mask and sub-mask coordinates, extracts the sub-mask. No doubt numpy could do this in one line. :)
        outer_rect is the coords for the mask, and inner_rect is the sub-coordinates - contained entirely
        inside the outer_rect, nothing outside.
    """
    tl_x = inner_rect.tl_x - outer_rect.tl_x
    tl_y = inner_rect.tl_y - outer_rect.tl_y
    outer_width = outer_rect.width
    outer_height = outer_rect.height
    width = inner_rect.width
    height = inner_rect.height
    if width == outer_width and height == outer_height:
        return outer_mask
    inner_mask = []
    for y in range(height):
        for x in range(width):
            new_x = x + tl_x
            new_y = y + tl_y
            pixel_loc = new_x + new_y * outer_width
            new_pixel = outer_mask[pixel_loc]
            inner_mask.append(new_pixel)
    return inner_mask
