private static int getMinGradientIndex(int width, int height, float[] gradient, int x, int y) {
        float topGradient = getGradient(width, height, gradient, x, y + 1);
        float bottomGradient = getGradient(width, height, gradient, x, y - 1);
        float leftGradient = getGradient(width, height, gradient, x - 1, y);
        float rightGradient = getGradient(width, height, gradient, x + 1, y);
        if (topGradient < bottomGradient) {
            if (leftGradient < rightGradient) {
                if (topGradient < leftGradient) {
                    return x + (y + 1) * width;
                } else {
                    return x - 1 + (y) * width;
                }
            } else {
                if (topGradient < rightGradient) {
                    return x + (y + 1) * width;
                } else {
                    return x + 1 + (y) * width;
                }
            }
        } else {
            if (leftGradient < rightGradient) {
                if (bottomGradient < leftGradient) {
                    return x + (y - 1) * width;
                } else {
                    return x - 1 + (y) * width;
                }
            } else {
                if (bottomGradient < rightGradient) {
                    return x + (y - 1) * width;
                } else {
                    return x + 1 + (y) * width;
                }
            }
        }
    }