def convertToRowAndColumn(x, y, shape, xScale=None, yScale=None, safe=True):
    """
    Convert from plot coordinates to image row and column.
    """
    if xScale is None:
        c = x
    else:
        if x < xScale[0]:
            x = xScale[0]
        c = (x - xScale[0]) / xScale[1]
    if yScale is None:
        r = y
    else:
        if y < yScale[0]:
            y = yScale[0]
        r = (y - yScale[0]) / yScale[1]
    if safe:
        c = min(int(c), shape[1] - 1)
        r = min(int(r), shape[0] - 1)
    else:
        c = int(c)
        r = int(r)
    return r, c
