Class ImageUtil

java.lang.Object
megamek.common.util.ImageUtil

public final class ImageUtil extends Object
Generic utility methods for image data
  • Field Details

    • IMAGE_SCALE_BICUBIC

      public static final int IMAGE_SCALE_BICUBIC
      See Also:
    • IMAGE_SCALE_AVG_FILTER

      public static final int IMAGE_SCALE_AVG_FILTER
      See Also:
    • failStandardImage

      public static BufferedImage failStandardImage
      Holds a drawn "fail" image that can be used when image loading fails.
  • Method Details

    • createAcceleratedImage

      public static BufferedImage createAcceleratedImage(Image base)
      Returns:
      an image in a format best fitting for hardware acceleration, if possible, else just the image passed to it
    • createAcceleratedImage

      public static BufferedImage createAcceleratedImage(int w, int h)
      Returns:
      an image in a format best fitting for hardware acceleration, if possible
    • failStandardImage

      public static BufferedImage failStandardImage()
      Returns a standard size (84x72) "fail" image having a red on white cross. The image is drawn, not loaded and should therefore work in almost all cases.
    • getScaledImage

      public static BufferedImage getScaledImage(Image img, int newWidth, int newHeight)
      Parameters:
      img - Image Image to scale.
      newWidth - Width to scale to
      newHeight - Height to scale to.
      Returns:
      Get a scaled version of the input image.
    • convertToBufferedImage

      public static BufferedImage convertToBufferedImage(Image image)
      Converts the given image to a BufferedImage. If it already is a BufferedImage, the image is only returned with a type cast without doing any further conversion.
      Parameters:
      image - An Image of any type
      Returns:
      The image as a BufferedImage
    • fitImage

      public static BufferedImage fitImage(Image image, int maxWidth, int maxHeight, int scaleType)
      Returns a scaled version of the given image. Scaling is adjusted so that the image fits in the given maximum width and maximum height, keeping the aspect ratio of the image. Either the height or the width of the resulting image will be equal to the given max width or height, while the other value will be smaller than the given maximum. Uses the supplied scaling method.
      Parameters:
      image - The image to scale
      maxWidth - The maximum width of the resulting image
      maxHeight - The maximum height of the resulting image
      scaleType - The scale type, IMAGE_SCALE_BICUBIC or IMAGE_SCALE_AVG_FILTER
      Returns:
      A scaled image fitting in a rectangle of the size (maxWidth, maxHeight)
    • getScaledImage

      public static BufferedImage getScaledImage(Image img, int newWidth, int newHeight, int scaleType)
      Parameters:
      img - Image Image to scale.
      newWidth - Width to scale to
      newHeight - Height to scale to
      scaleType - Type of scaling to do.
      Returns:
      scaled version of the input image, using the supplied type to select which scaling method to use.
    • addImageLoader

      @Deprecated(since="0.51.0", forRemoval=true) public static void addImageLoader(ImageUtil.ImageLoader loader)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Add a new image loader to the first position of the list, if it isn't there already
    • loadImageFromFile

      public static Image loadImageFromFile(String fileName)
      Loads and returns the image of the given fileName. This method does not make sure the image is fully loaded, this must be done by the caller when necessary (the simplest way is to create a new ImageIcon with the image). If the image cannot be loaded for any reason, the method returns a placeholder image but not null.
      Parameters:
      fileName - The image filename
      Returns:
      The image if possible, a placeholder image otherwise
    • loadImageFromFile

      public static Image loadImageFromFile(MegaMekFile file)
      Loads and returns the image of the given fileName. This method does not make sure the image is fully loaded, this must be done by the caller when necessary (the simplest way is to create a new ImageIcon with the image). If the image cannot be loaded for any reason, the method returns a placeholder image but not null.
      Parameters:
      file - The image as a MegaMekFile
      Returns:
      The image if possible, a placeholder image otherwise
    • base64TextEncodeImage

      public static String base64TextEncodeImage(@Nullable Image image)
      takes an image and converts it to text in the Base64 encoding. When the given image is null, an empty String is returned.
    • createDoubleBlindHiddenImage

      public static void createDoubleBlindHiddenImage(Map<Integer,String> imgCache)
      creates a ? image, used when units are hidden in double-blind
    • getGaussKernel

      public static Kernel getGaussKernel(int width, float sigma)
      Returns a square normalized Kernel for a Gaussian Blur. The Kernel has width x width elements. Sigma gives its Gauss sharpness. A high sigma of e.g. 1000 returns a flat Kernel of equal elements (= 1 / width ^ 2); sigma should not be smaller than about 0.2 (will lead to an exception). Normal sigma values are in the area of 1 to 5.
      Parameters:
      width - The length and height of the Kernel
      sigma - The extent of the Gaussian
      Returns:
      A Kernel
      See Also: