All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squeakysand.commons.graphics.ImageUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.squeakysand.commons.graphics;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Provides functions for manipulating images.
 */
public final class ImageUtils {

    private static final Logger LOG = LoggerFactory.getLogger(ImageUtils.class);

    /**
     * Utility class, so we enforce singleton pattern by hiding default constructor.
     */
    private ImageUtils() {
        super();
    }

    /**
     * Crops an image based on a provided aspect ratio.
     *
     * @param   image        The original image.
     * @param   aspectRatio  The height:width aspect ratio.
     * @return  A new image that is the largest possible centered sub-image with the specified aspect ratio.
     * @throws  java.lang.IllegalArgumentException  if image is null or aspectRatio <=
     *                                    0.0
     */
    public static BufferedImage crop(BufferedImage image, float aspectRatio) {
        if (image == null) {
            throw new IllegalArgumentException("image must not be null");
        } else if (aspectRatio <= 0F) {
            throw new IllegalArgumentException("aspectRatio must be greater than 0");
        }
        int srcHeight = image.getHeight();
        int srcWidth = image.getWidth();
        float srcAspectRatio = (float) srcHeight / (float) srcWidth;
        BufferedImage result = null;
        if (srcAspectRatio == aspectRatio) {
            result = image;
            LOG.debug("aspect ratios match");
        } else {
            int newHeight = srcHeight;
            int newWidth = srcWidth;
            int xOffset = 0;
            int yOffset = 0;
            if (aspectRatio < 1) {
                if (srcAspectRatio > aspectRatio) {
                    newHeight = (int) (srcWidth * aspectRatio);
                    yOffset = (int) (((float) srcHeight - (float) newHeight) / 2f);
                } else if (srcAspectRatio < aspectRatio) {
                    newWidth = (int) (srcHeight / aspectRatio);
                    xOffset = (int) (((float) srcWidth - (float) newWidth) / 2f);
                }
            } else if (aspectRatio > 1) {
                if (srcAspectRatio < aspectRatio) {
                    newWidth = (int) (srcHeight / aspectRatio);
                    xOffset = (int) (((float) srcWidth - (float) newWidth) / 2f);
                } else if (srcAspectRatio > aspectRatio) {
                    newHeight = (int) (srcWidth * aspectRatio);
                    yOffset = (int) (((float) srcHeight - (float) newHeight) / 2f);
                }
            } else {
                // aspectRatio == 1
                if (srcHeight > srcWidth) {
                    newHeight = srcWidth;
                    yOffset = (int) (((float) srcHeight - (float) newHeight) / 2f);
                } else {
                    newWidth = srcHeight;
                    xOffset = (int) (((float) srcWidth - (float) newWidth) / 2f);
                }
                // no need to test for srcHeight and srcWidth being the same here
                // as that condition would have been caught earlier when we check for
                // matching aspectRatios.
            }
            result = image.getSubimage(xOffset, yOffset, newWidth, newHeight);
        }
        return result;
    }

    /**
     * Resizes the supplied image to the new dimensions.
     *
     * @param   image      The image to be resized.
     * @param   height     The new height.
     * @param   width      The new width.
     * @param   imageType  The underlying type of the image being resized. Refer to imageType param for
     *                     {@link java.awt.image.BufferedImage#BufferedImage(int, int, int)}.
     * @return  A new image with the specified dimensions.
     */
    public static BufferedImage resize(final BufferedImage image, final int height, final int width, final int imageType) {
        BufferedImage result = new BufferedImage(width, height, imageType);
        Graphics g = result.getGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy