Package adams.data.image
Class BufferedImageHelper
- java.lang.Object
-
- adams.data.image.BufferedImageHelper
-
public class BufferedImageHelper extends Object
Helper class for BufferedImage objects.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
-
-
Constructor Summary
Constructors Constructor Description BufferedImageHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intcombine(int[] rgba)Combines the R,G,B,A values back into single integer.static intcombine(int r, int g, int b, int a)Combines the R,G,B,A values back into single integer.static BufferedImageconvert(BufferedImage img, int type)Converts the image, if necessary to the specified type.static BufferedImagedeepCopy(BufferedImage source)Returns a copy of a BufferedImage object.static booleanfloodFill(BufferedImage img, int startX, int startY, int targetColor, int replacementColor)Performs flood fill on the image.static booleanfloodFill(BufferedImage img, int startX, int startY, int targetColor, int replacementColor, int[] extent)Performs flood fill on the image.static booleanfloodFill(BufferedImage img, int startX, int startY, Color targetColor, Color replacementColor)Performs flood fill on the image.static booleanfloodFill(BufferedImage img, int startX, int startY, Color targetColor, Color replacementColor, int[] extent)Performs flood fill on the image.static BufferedImagefromBytes(byte[] bytes, MessageCollection errors)Turns the image bytes (eg JPG or PNG) into a BufferedImage.static intgetPixelDepth(BufferedImage img)Returns the pixel depth in bits.static IntArrayMatrixViewgetPixelMatrix(BufferedImage img)Returns all the pixels of the image as an int array (row-wise).static int[][]getPixelRaster(BufferedImage img)Returns the pixels of the image as an 2-D int array (row-wise).static int[]getPixels(BufferedImage img)Returns all the pixels of the image as an int array (row-wise).static ImageReadergetReaderForExtension(String ext)Returns the first image reader that handles the specified extension.static ImageReadergetReaderForFile(File file)Returns the first image reader that handles the extension of the specified file.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.static ImageWritergetWriterForExtension(String ext)Returns the first image writer that handles the extension of the specified file.static ImageWritergetWriterForFile(File file)Returns the first image writer that handles the extension of the specified file.static int[][]histogram(BufferedImage img, boolean gray)Generates a histogram for each of the R, G and B channels.static BufferedImageContainerread(File file)Reads an image, also fills in meta-data.static BufferedImageContainerread(File file, boolean addMetaData)Reads an image, also fills in meta-data.static BufferedImageContainerread(InputStream stream, boolean addMetaData)Reads an image, also fills in meta-data.static BufferedImageremoveAlphaChannel(BufferedImage img)Removes the alpha channel if present and turns it into RGB image.static BufferedImagerotate(BufferedImage img, int degrees, Color background)Rotates the images by the specified number of degrees.static int[]split(int pixel)Splits the RGBA value into R,G,B,A.static BufferedImagetoBufferedImage(JComponent comp, Color background)Creates a BufferedImage from the component.static BufferedImagetoBufferedImage(JComponent comp, Color background, int width, int height)Creates a BufferedImage from the component.static BufferedImageContainertoBufferedImageContainer(AbstractImageContainer cont)Creates aBufferedImageContainercontainer if necessary, otherwise it just casts the object.static byte[]toBytes(BufferedImage img, String format, MessageCollection errors)Turns an image into a byte array.static Stringwrite(BufferedImage img, File file)Writes the image to the specified file.static Stringwrite(BufferedImage img, String format, File file)Writes the image to the specified file.static Stringwrite(BufferedImage img, String format, OutputStream stream)Writes the image to the specified stream.
-
-
-
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)
-
getPixelMatrix
public static IntArrayMatrixView getPixelMatrix(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 valueg- the green valueb- the blue valuea- 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 onstartX- the starting point, x coordinatestartY- the starting point, y coordinatetargetColor- 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 onstartX- the starting point, x coordinatestartY- the starting point, y coordinatetargetColor- 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 onstartX- the starting point, x coordinatestartY- the starting point, y coordinatetargetColor- 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 onstartX- the starting point, x coordinatestartY- the starting point, y coordinatetargetColor- 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 readaddMetaData- 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 fromaddMetaData- 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 savefile- 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 saveformat- the image format to use (eg jpg or png), if null or empty uses extension as formatfile- 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 savestream- 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 convertformat- 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 converterrors- 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 converttype- 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 analyzegray- 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
-
toBufferedImageContainer
public static BufferedImageContainer toBufferedImageContainer(AbstractImageContainer cont)
Creates aBufferedImageContainercontainer if necessary, otherwise it just casts the object.- Parameters:
cont- the cont to cast/convert- Returns:
- the casted/converted container
-
toBufferedImage
public static BufferedImage toBufferedImage(JComponent comp, Color background)
Creates a BufferedImage from the component.- Parameters:
comp- the component to turn into an imagebackground- 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 imagebackground- the backgroundwidth- the width to use, -1 to use component widthheight- the height to use, -1 to use component height- Returns:
- the generated image
-
rotate
public static BufferedImage rotate(BufferedImage img, int degrees, Color background)
Rotates the images by the specified number of degrees.- Parameters:
img- the image to rotatedegrees- the rotationbackground- the background color to use- Returns:
- the rotated image
-
-