Class ImageUtil


  • public class ImageUtil
    extends Object
    Various Image/BufferedImage utilities.
    • Constructor Detail

      • ImageUtil

        public ImageUtil()
    • Method Detail

      • bufferedImageFromImage

        public static BufferedImage bufferedImageFromImage​(Image image)
        Creates a BufferedImage from an Image.
        Parameters:
        image - An Image to be converted to a BufferedImage.
        Returns:
        A BufferedImage instance of the same given image.
      • recolorImage

        public static BufferedImage recolorImage​(Image image,
                                                 Color rgb)
        Creates a new image with the same alpha channel, but a constant RGB channel
      • luminanceOffset

        public static BufferedImage luminanceOffset​(Image rawImg,
                                                    int offset)
        Offsets an image's luminance by a given value.
        Parameters:
        rawImg - The image to be darkened or brightened.
        offset - A signed 8-bit integer value to brighten or darken the image with. Values above 0 will brighten, and values below 0 will darken.
        Returns:
        The given image with its brightness adjusted by the given offset.
      • luminanceScale

        public static BufferedImage luminanceScale​(Image rawImg,
                                                   float percentage)
        Changes an images luminance by a scaling factor
        Parameters:
        rawImg - The image to be darkened or brightened.
        percentage - The ratio to darken or brighten the given image. Values above 1 will brighten, and values below 1 will darken.
        Returns:
        The given image with its brightness scaled by the given percentage.
      • alphaOffset

        public static BufferedImage alphaOffset​(Image rawImg,
                                                int offset)
        Offsets an image's alpha component by a given offset.
        Parameters:
        rawImg - The image to be made more or less transparent.
        offset - A signed 8-bit integer value to modify the image's alpha component with. Values above 0 will increase transparency, and values below 0 will decrease transparency.
        Returns:
        The given image with its alpha component adjusted by the given offset.
      • alphaOffset

        public static BufferedImage alphaOffset​(Image rawImg,
                                                float percentage)
        Offsets an image's alpha component by a given percentage.
        Parameters:
        rawImg - The image to be made more or less transparent.
        percentage - The ratio to modify the image's alpha component with. Values above 1 will increase transparency, and values below 1 will decrease transparency.
        Returns:
        The given image with its alpha component scaled by the given percentage.
      • grayscaleImage

        public static BufferedImage grayscaleImage​(BufferedImage image)
        Creates a grayscale image from the given image.
        Parameters:
        image - The source image to be converted.
        Returns:
        A copy of the given imnage, with colors converted to grayscale.
      • resizeImage

        public static BufferedImage resizeImage​(BufferedImage image,
                                                int newWidth,
                                                int newHeight)
        Re-size a BufferedImage to the given dimensions.
        Parameters:
        image - the BufferedImage.
        newWidth - The width to set the BufferedImage to.
        newHeight - The height to set the BufferedImage to.
        Returns:
        The BufferedImage with the specified dimensions
      • resizeImage

        public static BufferedImage resizeImage​(BufferedImage image,
                                                int newWidth,
                                                int newHeight,
                                                boolean preserveAspectRatio)
        Re-size a BufferedImage to the given dimensions.
        Parameters:
        image - the BufferedImage.
        newWidth - The width to set the BufferedImage to.
        newHeight - The height to set the BufferedImage to.
        preserveAspectRatio - Whether to preserve the original image's aspect ratio. When true, the image will be scaled to have a maximum of newWidth width and newHeight height.
        Returns:
        The BufferedImage with the specified dimensions
      • resizeCanvas

        public static BufferedImage resizeCanvas​(BufferedImage image,
                                                 int newWidth,
                                                 int newHeight)
        Re-size a BufferedImage's canvas to the given dimensions.
        Parameters:
        image - The image whose canvas should be re-sized.
        newWidth - The width to set the BufferedImage to.
        newHeight - The height to set the BufferedImage to.
        Returns:
        The BufferedImage centered within canvas of given dimensions.
      • rotateImage

        public static BufferedImage rotateImage​(BufferedImage image,
                                                double theta)
        Rotates an image around its center by a given number of radians.
        Parameters:
        image - The image to be rotated.
        theta - The number of radians to rotate the image.
        Returns:
        The given image, rotated by the given theta.
      • flipImage

        public static BufferedImage flipImage​(BufferedImage image,
                                              boolean horizontal,
                                              boolean vertical)
        Flips an image horizontally and/or vertically.
        Parameters:
        image - The image to be flipped.
        horizontal - Whether the image should be flipped horizontally.
        vertical - Whether the image should be flipped vertically.
        Returns:
        The given image, flipped horizontally and/or vertically.
      • outlineImage

        public static BufferedImage outlineImage​(BufferedImage image,
                                                 Color color)
        Outlines non-transparent pixels of a BufferedImage with the given color.
        Parameters:
        image - The image to be outlined.
        color - The color to use for the outline.
        Returns:
        The BufferedImage with its edges outlined with the given color.
      • outlineImage

        public static BufferedImage outlineImage​(BufferedImage image,
                                                 Color color,
                                                 Boolean outlineCorners)
        Outlines non-transparent pixels of a BufferedImage with the given color. Optionally outlines corners in addition to edges.
        Parameters:
        image - The image to be outlined.
        color - The color to use for the outline.
        outlineCorners - Whether to draw an outline around corners, or only around edges.
        Returns:
        The BufferedImage with its edges--and optionally, corners--outlined with the given color.
      • loadImageResource

        public static BufferedImage loadImageResource​(Class<?> c,
                                                      String path)
        Reads an image resource from a given path relative to a given class. This method is primarily shorthand for the synchronization and error handling required for loading image resources from the classpath.
        Parameters:
        c - The class to be referenced for the package path.
        path - The path, relative to the given class.
        Returns:
        A BufferedImage of the loaded image resource from the given path.
      • fillImage

        public static BufferedImage fillImage​(BufferedImage image,
                                              Color color)
        Fills all non-transparent pixels of the given image with the given color.
        Parameters:
        image - The image which should have its non-transparent pixels filled.
        color - The color with which to fill pixels.
        Returns:
        The given image with all non-transparent pixels set to the given color.
      • getImageSpritePixels

        public static SpritePixels getImageSpritePixels​(BufferedImage image,
                                                        Client client)
        Converts the buffered image into a sprite image and returns it
        Parameters:
        image - The image to be converted
        client - Current client instance
        Returns:
        The buffered image as a sprite image
      • getImageIndexedSprite

        public static IndexedSprite getImageIndexedSprite​(BufferedImage image,
                                                          Client client)
        Converts an image into an IndexedSprite instance. The passed in image can only have at max 255 different colors.
        Parameters:
        image - The image to be converted
        client - Current client instance
        Returns:
        The image as an IndexedSprite