Class ImageUtils


  • public class ImageUtils
    extends Object
    Helper class for image related operations.
    Author:
    FracPete (fracpete at waikato dot ac dot nz)
    • Constructor Detail

      • ImageUtils

        public ImageUtils()
    • Method Detail

      • combineImages

        public static void combineImages​(BufferedImage source,
                                         BufferedImage target)
        Transfers all the non-black pixels from the source image into the target one.
        Parameters:
        source - the source image
        target - the target image
      • replaceColor

        public static boolean replaceColor​(int[] pixels,
                                           Color oldColor,
                                           Color newColor)
        Replaces the old color with the new one.
        Parameters:
        pixels - the pixels to process
        oldColor - the old color to replace
        newColor - the replacement color
        Returns:
        true if modified
      • replaceColor

        public static boolean replaceColor​(BufferedImage image,
                                           Color oldColor,
                                           Color newColor)
        Replaces the old color with the new one.
        Parameters:
        image - the image to process
        oldColor - the old color to replace
        newColor - the replacement color
        Returns:
        true if modified
      • replaceColor

        public static boolean replaceColor​(int[] pixels,
                                           int width,
                                           int height,
                                           Color oldColor,
                                           Color newColor,
                                           int[] rangeX,
                                           int[] rangeY)
        Replaces the old color with the new one.
        Parameters:
        pixels - the pixels to process
        width - the width of the image
        height - the height of the image
        oldColor - the old color to replace
        newColor - the replacement color
        rangeX - the sub-range of x coordinates to work on (both incl)
        rangeY - the sub-range of y coordinates to work on (both incl)
        Returns:
        true if modified
      • replaceColor

        public static boolean replaceColor​(BufferedImage image,
                                           Color oldColor,
                                           Color newColor,
                                           int[] rangeX,
                                           int[] rangeY)
        Replaces the old color with the new one.
        Parameters:
        image - the image to process
        oldColor - the old color to replace
        newColor - the replacement color
        rangeX - the sub-range of x coordinates to work on (both incl)
        rangeY - the sub-range of y coordinates to work on (both incl)
        Returns:
        true if modified
      • initImage

        public static void initImage​(BufferedImage image,
                                     Color color)
        Replaces any non-black pixels with the specified color.
        Parameters:
        image - the image to process
        color - the new color for non-black pixels
      • newImage

        public static BufferedImage newImage​(int width,
                                             int height)
        Creates a new image with just black pixels.
        Parameters:
        width - the width of the image
        height - the height of the image
      • requiresReplacement

        protected static boolean requiresReplacement​(int pixel,
                                                     Integer target,
                                                     int replacement)
        Checks whether the pixel requires replacement. If target is not null, then the pixel must match that RGB value. If target is null, then the pixel must be difference from the replacement RGB value.
        Parameters:
        pixel - the pixel to check
        target - the target RGB to replace, can be null
        replacement - the replacement RGB to use
        Returns:
        true if pixel requires replacing
      • fill

        public static void fill​(BufferedImage image,
                                Point n,
                                Color targetColor,
                                Color replacementColor)
        Performs flood-fill on the provided image. Based on pseudo code from here: https://en.wikipedia.org/wiki/Flood_fill#Alternative_implementations
        Parameters:
        image - the image to update
        n - the starting point
        targetColor - the color to replace, can be null to apply to any color
        replacementColor - the replacement color