Package jexer.bits

Class ImageUtils


  • public class ImageUtils
    extends java.lang.Object
    ImageUtils contains methods to: - Check if an image is fully transparent. - Scale an image and preserve aspect ratio. - Break an image up into cells and optionally convert to Unicode glyphs. - Open an animated image as an Animation. - Compute the distance between two colors in RGB space. - Compute the partial movement between two colors in RGB space.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ImageUtils.DisplayMode
      Selections for approximating an image as text cells.
      static class  ImageUtils.Scale
      Selections for fitting an image to the text cells.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean canDrawUnicodeBlockDrawingChar​(int ch)
      Check if a Unicode block-drawing character is drawable by drawUnicodeBlockDrawingChar().
      static java.awt.image.BufferedImage createImage​(java.awt.image.BufferedImage image, int width, int height)
      Create a BufferedImage using the same color model as another image.
      static void drawUnicodeBlockDrawingChar​(int ch, java.awt.Color foreColor, java.awt.Color backColor, java.awt.image.BufferedImage image)
      Draw a Unicode block-drawing character to an image.
      static Animation getAnimation​(java.io.File file)
      Open an image as an Animation.
      static Animation getAnimation​(java.io.InputStream inputStream)
      Open an image as an Animation.
      static Animation getAnimation​(java.lang.String filename)
      Open an image as an Animation.
      static Animation getAnimation​(java.net.URL url)
      Open an image as an Animation.
      static Cell[][] imageToCells​(Backend backend, java.awt.image.BufferedImage image, int textWidth, int textHeight, ImageUtils.DisplayMode displayMode, java.awt.Color backColor, boolean bleedThrough)
      Convert an image to a 2D array of cells.
      static boolean isFullyOpaque​(java.awt.image.BufferedImage image)
      Check if any pixels in an image have not-100% alpha value.
      static boolean isFullyTransparent​(java.awt.image.BufferedImage image)
      Check if any pixels in an image have not-0% alpha value.
      static int rgbAverage​(java.awt.image.BufferedImage image)
      Compute the average RGB value of an entire image, including pixels that may be partially or fully transparent.
      static int rgbAverage​(java.awt.image.BufferedImage image, boolean onlyOpaque)
      Compute the average RGB value of an entire image.
      static int rgbDistance​(int first, int second)
      Report the absolute distance in RGB space between two RGB colors.
      static int rgbMove​(int start, int end, double fraction)
      Move from one point in RGB space to another, by a certain fraction.
      static double rgbStdDev​(java.awt.image.BufferedImage image, java.awt.image.BufferedImage averageImage)
      Compute the standard deviation of RGB values of an entire image.
      static java.awt.image.BufferedImage scaleImage​(java.awt.image.BufferedImage image, int width, int height, ImageUtils.Scale scale, java.awt.Color backColor)
      Scale an image to be scaleFactor size and/or stretch it to fit a target box.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isFullyTransparent

        public static boolean isFullyTransparent​(java.awt.image.BufferedImage image)
        Check if any pixels in an image have not-0% alpha value.
        Parameters:
        image - the image to check
        Returns:
        true if every pixel is fully transparent
      • isFullyOpaque

        public static boolean isFullyOpaque​(java.awt.image.BufferedImage image)
        Check if any pixels in an image have not-100% alpha value.
        Parameters:
        image - the image to check
        Returns:
        true if every pixel is fully transparent
      • scaleImage

        public static java.awt.image.BufferedImage scaleImage​(java.awt.image.BufferedImage image,
                                                              int width,
                                                              int height,
                                                              ImageUtils.Scale scale,
                                                              java.awt.Color backColor)
        Scale an image to be scaleFactor size and/or stretch it to fit a target box.
        Parameters:
        image - the image to scale
        width - the width in pixels for the destination image
        height - the height in pixels for the destination image
        scale - the scaling type
        backColor - the background color to use for Scale.SCALE
        Returns:
        the scaled image
      • imageToCells

        public static Cell[][] imageToCells​(Backend backend,
                                            java.awt.image.BufferedImage image,
                                            int textWidth,
                                            int textHeight,
                                            ImageUtils.DisplayMode displayMode,
                                            java.awt.Color backColor,
                                            boolean bleedThrough)
        Convert an image to a 2D array of cells.
        Parameters:
        image - the image to convert
        backend - the backend that can obtain the correct background color for a text cell
        textWidth - the width of a cell in pixels
        textHeight - the height of a cell in pixels
        displayMode - option to convert image to Unicode glyphs
        backColor - the color to use for the right and bottom padding to the text cell boundary
        bleedThrough - if true, then image cells that are fully covered by a single color will be replaced by empty text cells of that color, permitting text "behind" that cell to bleed through if translucence is enabled
        Returns:
        a two-dimensional array of cells (cells[x][y])
      • getAnimation

        public static Animation getAnimation​(java.lang.String filename)
        Open an image as an Animation.
        Parameters:
        filename - the name of the file that contains an animation
        Returns:
        the animation, or null on error
      • getAnimation

        public static Animation getAnimation​(java.io.File file)
        Open an image as an Animation.
        Parameters:
        file - the file that contains an animation
        Returns:
        the animation, or null on error
      • getAnimation

        public static Animation getAnimation​(java.net.URL url)
        Open an image as an Animation.
        Parameters:
        url - the URK that contains an animation
        Returns:
        the animation, or null on error
      • getAnimation

        public static Animation getAnimation​(java.io.InputStream inputStream)
        Open an image as an Animation.
        Parameters:
        inputStream - the inputStream that contains an animation
        Returns:
        the animation, or null on error
      • rgbDistance

        public static int rgbDistance​(int first,
                                      int second)
        Report the absolute distance in RGB space between two RGB colors.
        Parameters:
        first - the first color
        second - the second color
        Returns:
        the distance
      • rgbMove

        public static int rgbMove​(int start,
                                  int end,
                                  double fraction)
        Move from one point in RGB space to another, by a certain fraction.
        Parameters:
        start - the starting point color
        end - the ending point color
        fraction - the amount of movement between start and end, between 0.0 (start) and 1.0 (end).
        Returns:
        the final color
      • rgbAverage

        public static int rgbAverage​(java.awt.image.BufferedImage image)
        Compute the average RGB value of an entire image, including pixels that may be partially or fully transparent.
        Parameters:
        image - the image to check
        Returns:
        the average color
      • rgbAverage

        public static int rgbAverage​(java.awt.image.BufferedImage image,
                                     boolean onlyOpaque)
        Compute the average RGB value of an entire image.
        Parameters:
        image - the image to check
        onlyOpaque - if true, only count pixels that are fully opaque
        Returns:
        the average color
      • rgbStdDev

        public static double rgbStdDev​(java.awt.image.BufferedImage image,
                                       java.awt.image.BufferedImage averageImage)
        Compute the standard deviation of RGB values of an entire image.
        Parameters:
        image - the image to check
        averageImage - the image's "average" pixel values
        Returns:
        the average color
        Throws:
        java.lang.IllegalArgumentException - if the two images are of different dimensions
      • createImage

        public static java.awt.image.BufferedImage createImage​(java.awt.image.BufferedImage image,
                                                               int width,
                                                               int height)
        Create a BufferedImage using the same color model as another image.
        Parameters:
        image - the original image
        width - the width of the new image
        height - the height of the new image
        Returns:
        the new image
      • canDrawUnicodeBlockDrawingChar

        public static boolean canDrawUnicodeBlockDrawingChar​(int ch)
        Check if a Unicode block-drawing character is drawable by drawUnicodeBlockDrawingChar().
        Parameters:
        ch - the character to draw
        Returns:
        true if drawUnicodeBlockDrawingChar() can draw that character
      • drawUnicodeBlockDrawingChar

        public static void drawUnicodeBlockDrawingChar​(int ch,
                                                       java.awt.Color foreColor,
                                                       java.awt.Color backColor,
                                                       java.awt.image.BufferedImage image)
        Draw a Unicode block-drawing character to an image.
        Parameters:
        ch - the character to draw
        foreColor - the foreground color
        backColor - the background color
        image - the image to draw onto