class ImageHandler {

    public static BufferedImage rescaleImage(BufferedImage bufferedImage, int boxWidth, int boxHeight, boolean toFit) {
        if (bufferedImage == null) {
            LOGGER.warn("given image was NULL");
            return null;
        }

        int iWidth = bufferedImage.getWidth();
        int iHeight = bufferedImage.getHeight();

        double scaleX = (double)boxWidth / (double)iWidth;
        double scaleY = (double)boxHeight / (double)iHeight;
        double scale = Math.min(scaleX, scaleY);

        if (toFit) {
            scaleX = scale;
            scaleY = scale;
        }

        BufferedImage rescaledImage;

        if (scale >= 1.0) {
            rescaledImage = scaleUp(bufferedImage, scaleX, scaleY);
        } else {
            rescaledImage = scaleDown(bufferedImage, scaleX, scaleY);
        }

        return rescaledImage;
    }

}