Class ImageUtils

java.lang.Object
ucar.unidata.ui.ImageUtils

public class ImageUtils extends Object
Provides a set of image manipulation utilities
Author:
IDV development team
  • Field Details

    • debug

      public static boolean debug
      debug flag
  • Constructor Details

    • ImageUtils

      public ImageUtils()
  • Method Details

    • matte

      public static BufferedImage matte(BufferedImage image, int top, int bottom, int left, int right, Color bg)
      Add a matte border around the image
      Parameters:
      image - The image
      top - top space
      bottom - bottom space
      left - left space
      right - right space
      bg - Background color
      Returns:
      The matted image
    • clip

      public static BufferedImage clip(BufferedImage image, int[] ul, int[] lr)
      Clip the image
      Parameters:
      image - The image
      ul - upper left
      lr - lower right
      Returns:
      The clipped image
    • readImage

      public static Image readImage(String imagePath)
      Read and image
      Parameters:
      imagePath - the path to the image
      Returns:
      the Image
    • readImage

      public static Image readImage(String imagePath, boolean cache)
      Read and image
      Parameters:
      imagePath - the path to the image
      cache - Cache the image
      Returns:
      the Image
    • readImage

      public static Image readImage(String imagePath, boolean cache, boolean returnNullIfNotFound)
      Read and image
      Parameters:
      imagePath - the path to the image
      cache - Cache the image
      returnNullIfNotFound - if true, return null if the image does not exist
      Returns:
      the Image
    • waitOnImage

      public static Image waitOnImage(Image image)
      Wait until it is loaded in.
      Parameters:
      image - the image
      Returns:
      The image
    • makeColorTransparent

      public static BufferedImage makeColorTransparent(Image im, Color c)
      Make a color in the image transparent
      Parameters:
      im - image
      c - the color to make transparent
      Returns:
      a new image with the color transparent.
    • makeColorTransparent

      public static BufferedImage makeColorTransparent(Image im, int[] redRange, int[] greenRange, int[] blueRange)
      Set the colors taht are within the given red, green and blue ranges to be transparent.
      Parameters:
      im - The image
      redRange - red range
      greenRange - green range
      blueRange - blue range
      Returns:
      munged image
    • removeRedeye

      public static BufferedImage removeRedeye(Image im, int x1, int y1, int x2, int y2)
      Remove the brighter red from the image
      Parameters:
      im - image
      x1 - bounds
      y1 - bounds
      x2 - bounds
      y2 - bounds
      Returns:
      new image
    • toAlpha

      public static int toAlpha(double percent)
      Change the transparency percentage into an int alpha value
      Parameters:
      percent - the percent transparent 0-1.0
      Returns:
      the alpha value
    • setAlpha

      public static BufferedImage setAlpha(Image im, double percent)
      Set the alpha channel to the given transparency percent
      Parameters:
      im - image
      percent - Percent transparent 0-1.0
      Returns:
      munged image
    • toPoint

      public static Point toPoint(Point2D p)
      convenience to convert to a Point
      Parameters:
      p - point
      Returns:
      point
    • parsePoint

      public static Point parsePoint(String s, Rectangle r)
      Parse the string specification of a point with respect to the rectangle. The spec can look like: "rectpoint,offsetx,offsety" where rectpoint can be:
       ul    um    ur
       ml    mm    mr
       ll    lm    lr
      Where u=upper,m=middle,l=lower r=right,l=left
      Parameters:
      s - String specification
      r - Reference rectangle
      Returns:
      The point
    • toBufferedImage

      public static BufferedImage toBufferedImage(Image image)
      This method returns a buffered image with the contents of an image
      Parameters:
      image - the image
      Returns:
      a buffered image
    • toBufferedImage

      public static BufferedImage toBufferedImage(Image image, boolean force)
      This method returns a buffered image with the contents of an image
      Parameters:
      image - the image
      force - If false then just return the image argument if its a BufferedImage
      Returns:
      a buffered image
    • mergeImages

      public static Image mergeImages(List images, int space, Color bg)
      Merge images
      Parameters:
      images - list of images
      space - space between images
      bg - background color
      Returns:
      merged image
    • gridImages

      public static Image gridImages(List images, int space, Color bg, int columns)
      Merge images
      Parameters:
      images - list of images
      space - space between images
      bg - background color
      columns - number of columns
      Returns:
      merged image
    • gridImages2

      public static Image gridImages2(List<? extends Image> images, int space, Color bg, int columns)
      Merge Images. The heuristic is not the same as gridImages(List, int, Color, int) so the results can be different.
      Parameters:
      images - the images that will be gridded
      space - the space padding around each image.
      bg - the background color "behind" the image.
      columns - how many columns in the grid
      Returns:
      the image that has been gridded
    • padImage

      public static Image padImage(Image image, int space, Color color)
      Pad the image.
      Parameters:
      image - the image
      space - the space
      color - the color
      Returns:
      the image
    • mergeHorizontal

      public static Image mergeHorizontal(List<? extends Image> images)
      Merge the images horizontally.
      Parameters:
      images - the images
      Returns:
      the image
    • mergeVertical

      public static Image mergeVertical(List<? extends Image> images)
      Merge the images vertically.
      Parameters:
      images - the images
      Returns:
      the image
    • toBufferedImage

      public static BufferedImage toBufferedImage(Image image, int type)
      Create a BufferedImage from the given image
      Parameters:
      image - The image
      type - BufferedImage type
      Returns:
      The BufferedImage
    • hasAlpha

      public static boolean hasAlpha(Image image)
      Check to see if the image has alpha
      Parameters:
      image - the image
      Returns:
      true if has alpha
    • getImage

      public static Image getImage(Component component) throws Exception
      Get the screen image from the component
      Parameters:
      component - The component.
      Returns:
      Its image
      Throws:
      Exception
    • writeImageToFile

      public static void writeImageToFile(Image image, File saveFile) throws Exception
      Write a Buffered image to a file
      Parameters:
      image - image to write
      saveFile - file to write to
      Throws:
      Exception - problem writing file
    • writeImageToFile

      public static void writeImageToFile(Image image, String saveFile) throws Exception
      Write a Buffered image to a file
      Parameters:
      image - image to write
      saveFile - file to write to
      Throws:
      Exception - problem writing file
    • horizontalflip

      public static BufferedImage horizontalflip(BufferedImage img)
      Flip the image horizontally From: Josiah Hester - http://www.javalobby.org/articles/ultimate-image
      Parameters:
      img - image
      Returns:
      flipped image
    • verticalflip

      public static BufferedImage verticalflip(BufferedImage img)
      Flip the image vertically From: Josiah Hester - http://www.javalobby.org/articles/ultimate-image
      Parameters:
      img - image
      Returns:
      flipped image
    • rotate90

      public static BufferedImage rotate90(BufferedImage img, boolean left)
      Rotate the image 90 degrees
      Parameters:
      img - image
      left - rotate counter clockwise
      Returns:
      rotated image
    • convertImageTo

      public static String convertImageTo(String file, String newType)
      Convert an image to a new type
      Parameters:
      file - image file
      newType - new image type
      Returns:
      name of the new file
    • writeImageToFile

      public static void writeImageToFile(Image image, String saveFile, float quality) throws Exception
      Write a Buffered image to a file at a particular quality
      Parameters:
      image - image to write
      saveFile - file to write to
      quality - image quality (if supported)
      Throws:
      Exception - problem writing file
    • writeImageToFile

      public static void writeImageToFile(Image image, String saveFile, OutputStream os, float quality) throws Exception
      Write a Buffered image to a file at a particular quality
      Parameters:
      image - image to write
      saveFile - _more_
      os - output stream
      quality - image quality (if supported)
      Throws:
      Exception - problem writing file
    • isImage

      public static boolean isImage(String file)
      Is the file name an image
      Parameters:
      file - file
      Returns:
      is image
    • writeImage

      public static boolean writeImage(JDialog window, String file) throws Exception
      Make a screen capture of the window. Write it to the file.
      Parameters:
      window - The window
      file - The file
      Returns:
      Successful
      Throws:
      Exception
    • writeImage

      public static boolean writeImage(JFrame window, String file) throws Exception
      Make a screen capture of the window. Write it to the file.
      Parameters:
      window - The window
      file - The file
      Returns:
      Successful
      Throws:
      Exception
    • writeImageToFile

      public static void writeImageToFile(Component component, String saveFile) throws Exception
      Take a screen snapshot of the component. Write it to the file.
      Parameters:
      component - The component.
      saveFile - The file.
      Throws:
      Exception
    • writeAvi

      public static void writeAvi(List imageFiles, double frameRateInFPS, File outFile) throws IOException
      Write an AVI file
      Parameters:
      imageFiles - list of files
      frameRateInFPS - frame rate
      outFile - output file
      Throws:
      IOException - problem writing AVI
    • resize

      public static Image resize(Image image, int width, int height)
      Resize an image
      Parameters:
      image - the image
      width - new width
      height - new height
      Returns:
      resized image
    • getImage

      public static Image getImage(JEditorPane editor, Color transparentColor) throws Exception
      Get an image from the component
      Parameters:
      editor - component
      transparentColor - if non null then set this color to be transparent
      Returns:
      image
      Throws:
      Exception - on badness
    • renderHtml

      public static Image renderHtml(String html, int width, Color transparentColor, Font font) throws Exception
      Render the given html and return an image
      Parameters:
      html - html to render
      width - image width
      transparentColor - if non null set this color in the image to be transparent
      font - font to render with
      Returns:
      image
      Throws:
      Exception - on badness
    • getEditor

      public static JEditorPane getEditor(String html, int width, Color transparentColor, Font font) throws Exception
      Make a editor pane from the html
      Parameters:
      html - html
      width - width
      transparentColor - what color to set as transparent
      font - font
      Returns:
      editor pane
      Throws:
      Exception - on badness
    • getEditor

      public static JEditorPane getEditor(JEditorPane editor, String html, int width, Color transparentColor, Font font) throws Exception
      Make a editor pane from the html
      Parameters:
      editor - Initial editor
      html - html
      width - width
      transparentColor - what color to set as transparent
      font - font
      Returns:
      editor pane
      Throws:
      Exception - on badness
    • writePDF

      public static void writePDF(OutputStream out, JComponent comp) throws IOException
      test code
      Parameters:
      out - test
      comp - test
      Throws:
      IOException - test
    • main

      public static void main(String[] args) throws Exception
      Read in the image. Wait util it is loaded in.
      Parameters:
      args - args
      Throws:
      Exception - problem with this
    • getImageFile

      public static Image getImageFile(String file) throws Exception
      Read in the image from the given filename or url
      Parameters:
      file - File or url
      Returns:
      The image
      Throws:
      Exception - On badness
    • getColumnCountFromComps

      public static int getColumnCountFromComps(List<? extends Component> views)
      Gets the column count from the components.
      Parameters:
      views - the views
      Returns:
      the column from comp