BufferedImage image = ImageIO.read(imageInputStream);
int width = Math.min(image.getWidth(), maxWidth);
int height = Math.min(image.getHeight(), maxHeight);
Mode mode = Mode.AUTOMATIC;
if (image.getHeight() > maxHeight) {
mode = Mode.FIT_TO_HEIGHT;
}
if (width != image.getWidth() || height != image.getHeight()) {