Class BufferedImageHelper


  • public class BufferedImageHelper
    extends Object
    Helper class for BufferedImage objects.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Constructor Detail

      • BufferedImageHelper

        public BufferedImageHelper()
    • Method Detail

      • deepCopy

        public static BufferedImage deepCopy​(BufferedImage source)
        Returns a copy of a BufferedImage object.

        Taken from here (CC BY-SA 3.0). and from here (CC BY-SA 3.0).
        Parameters:
        source - the image to copy
      • getPixels

        public static int[] getPixels​(BufferedImage img)
        Returns all the pixels of the image as an int array (row-wise).
        Parameters:
        img - the image to get the pixels from
        Returns:
        the pixel array
        See Also:
        BufferedImage.getRGB(int, int)
      • split

        public static int[] split​(int pixel)
        Splits the RGBA value into R,G,B,A.
        Parameters:
        pixel - the RGB value to split
        Returns:
        the array with R,G,B,A
      • combine

        public static int combine​(int[] rgba)
        Combines the R,G,B,A values back into single integer.
        Parameters:
        rgba - the RGBA values
        Returns:
        the combined integer
      • combine

        public static int combine​(int r,
                                  int g,
                                  int b,
                                  int a)
        Combines the R,G,B,A values back into single integer.
        Parameters:
        r - the red value
        g - the green value
        b - the blue value
        a - the alpha value
        Returns:
        the combined integer
      • getRGBPixels

        public static int[][] getRGBPixels​(BufferedImage img)
        Returns all the pixels of the image as an int array (row-wise) with the RGB(A) components as second dimension.
        Parameters:
        img - the image to get the pixels from
        Returns:
        the pixel array
        See Also:
        BufferedImage.getRGB(int, int)
      • getPixelRaster

        public static int[][] getPixelRaster​(BufferedImage img)
        Returns the pixels of the image as an 2-D int array (row-wise).
        Parameters:
        img - the image to get the pixels from
        Returns:
        the pixel array
        See Also:
        BufferedImage.getRGB(int, int)
      • floodFill

        public static boolean floodFill​(BufferedImage img,
                                        int startX,
                                        int startY,
                                        Color targetColor,
                                        Color replacementColor)
        Performs flood fill on the image. Based on the 2nd alternative implementation.
        Parameters:
        img - the image to perform the floodfill on
        startX - the starting point, x coordinate
        startY - the starting point, y coordinate
        targetColor - the target color (what we want to fill)
        replacementColor - the replacement color (the color fill with)
        Returns:
        true if successfully filled
      • floodFill

        public static boolean floodFill​(BufferedImage img,
                                        int startX,
                                        int startY,
                                        int targetColor,
                                        int replacementColor)
        Performs flood fill on the image. Based on the 2nd alternative implementation.
        Parameters:
        img - the image to perform the floodfill on
        startX - the starting point, x coordinate
        startY - the starting point, y coordinate
        targetColor - the target color (what we want to fill)
        replacementColor - the replacement color (the color fill with)
        Returns:
        true if successfully filled
      • floodFill

        public static boolean floodFill​(BufferedImage img,
                                        int startX,
                                        int startY,
                                        Color targetColor,
                                        Color replacementColor,
                                        int[] extent)
        Performs flood fill on the image. Records the extent of the fill (bounding box). Based on the 2nd alternative implementation.
        Parameters:
        img - the image to perform the floodfill on
        startX - the starting point, x coordinate
        startY - the starting point, y coordinate
        targetColor - the target color (what we want to fill)
        replacementColor - the replacement color (the color fill with)
        extent - for recording the bounding box for the flood fill, all -1 if failed to fill
        Returns:
        true if successfully filled
      • floodFill

        public static boolean floodFill​(BufferedImage img,
                                        int startX,
                                        int startY,
                                        int targetColor,
                                        int replacementColor,
                                        int[] extent)
        Performs flood fill on the image. Records the extent of the fill (bounding box). Based on the 2nd alternative implementation.
        Parameters:
        img - the image to perform the floodfill on
        startX - the starting point, x coordinate
        startY - the starting point, y coordinate
        targetColor - the target color (what we want to fill)
        replacementColor - the replacement color (the color fill with)
        extent - for recording the bounding box for the flood fill, all -1 if failed to fill
        Returns:
        true if successfully filled
      • getReaderForFile

        public static ImageReader getReaderForFile​(File file)
        Returns the first image reader that handles the extension of the specified file.
        Parameters:
        file - the file to get the reader for
        Returns:
        the reader, null if none found
      • getReaderForExtension

        public static ImageReader getReaderForExtension​(String ext)
        Returns the first image reader that handles the specified extension.
        Parameters:
        ext - the extension to get the reader for
        Returns:
        the reader, null if none found
      • getWriterForFile

        public static ImageWriter getWriterForFile​(File file)
        Returns the first image writer that handles the extension of the specified file.
        Parameters:
        file - the file to get the writer for
        Returns:
        the writer, null if none found
      • getWriterForExtension

        public static ImageWriter getWriterForExtension​(String ext)
        Returns the first image writer that handles the extension of the specified file.
        Parameters:
        ext - the extension to get the writer for
        Returns:
        the writer, null if none found
      • read

        public static BufferedImageContainer read​(File file)
        Reads an image, also fills in meta-data.
        Parameters:
        file - the file to read
        Returns:
        the image container, null if failed to read
      • read

        public static BufferedImageContainer read​(File file,
                                                  boolean addMetaData)
        Reads an image, also fills in meta-data.
        Parameters:
        file - the file to read
        addMetaData - whether to add the meta-data
        Returns:
        the image container, null if failed to read
      • read

        public static BufferedImageContainer read​(InputStream stream,
                                                  boolean addMetaData)
        Reads an image, also fills in meta-data.
        Parameters:
        stream - the stream to read from
        addMetaData - whether to add the meta-data
        Returns:
        the image container, null if failed to read
      • removeAlphaChannel

        public static BufferedImage removeAlphaChannel​(BufferedImage img)
        Removes the alpha channel if present and turns it into RGB image.
        Parameters:
        img - the image to convert
        Returns:
        the (potentially) converted image
      • write

        public static String write​(BufferedImage img,
                                   File file)
        Writes the image to the specified file. If the output file points to a JPG file (.jpg or .jpeg), then images with alpha channel automatically get converted to RGB.
        Parameters:
        img - the image to save
        file - the file to write to
        Returns:
        null if successful, otherwise error message
      • write

        public static String write​(BufferedImage img,
                                   String format,
                                   File file)
        Writes the image to the specified file. If format is null, uses the file extension as format. If the format is JPG, then images with alpha channel automatically get converted to RGB.
        Parameters:
        img - the image to save
        format - the image format to use (eg jpg or png), if null or empty uses extension as format
        file - the file to write to
        Returns:
        null if successful, otherwise error message
      • write

        public static String write​(BufferedImage img,
                                   String format,
                                   OutputStream stream)
        Writes the image to the specified stream.
        Parameters:
        img - the image to save
        stream - the stream to write to
        Returns:
        null if successful, otherwise error message
      • toBytes

        public static byte[] toBytes​(BufferedImage img,
                                     String format,
                                     MessageCollection errors)
        Turns an image into a byte array.
        Parameters:
        img - the image to convert
        format - the format of the image, e.g., PNG or JPG
        Returns:
        the generated bytes, null if failed
      • fromBytes

        public static BufferedImage fromBytes​(byte[] bytes,
                                              MessageCollection errors)
        Turns the image bytes (eg JPG or PNG) into a BufferedImage.
        Parameters:
        bytes - the bytes to convert
        errors - for collecting errors
        Returns:
        the read image, null if failed to read
      • convert

        public static BufferedImage convert​(BufferedImage img,
                                            int type)
        Converts the image, if necessary to the specified type.
        Parameters:
        img - the image to convert
        type - the required type
        Returns:
        the (potentially) converted image
      • histogram

        public static int[][] histogram​(BufferedImage img,
                                        boolean gray)
        Generates a histogram for each of the R, G and B channels.
        Parameters:
        img - the image to analyze
        gray - whether to use (A)RGB or grayscale
        Returns:
        the histogram, if ARGB then 0 = R, 1 = G, 2 = B, 3 = A or 0 = gray in case of grayscale
      • getPixelDepth

        public static int getPixelDepth​(BufferedImage img)
        Returns the pixel depth in bits.
        Parameters:
        img - the image to analyze
        Returns:
        the number of bits, -1 if unknown type
      • toBufferedImage

        public static BufferedImage toBufferedImage​(JComponent comp,
                                                    Color background)
        Creates a BufferedImage from the component.
        Parameters:
        comp - the component to turn into an image
        background - the background
        Returns:
        the generated image
      • toBufferedImage

        public static BufferedImage toBufferedImage​(JComponent comp,
                                                    Color background,
                                                    int width,
                                                    int height)
        Creates a BufferedImage from the component.
        Parameters:
        comp - the component to turn into an image
        background - the background
        width - the width to use, -1 to use component width
        height - the height to use, -1 to use component height
        Returns:
        the generated image