Class ImageNormalizer

java.lang.Object
io.github.jspinak.brobot.util.image.ImageNormalizer

public class ImageNormalizer extends Object
Normalizes images to ensure consistent bit depth and format for pattern matching.

This utility addresses common issues in GUI automation where images may have:

  • Different bit depths (24-bit RGB vs 32-bit ARGB)
  • Inconsistent alpha channel handling
  • Format variations between captured and saved images
  • Color space differences affecting pattern matching

The normalizer provides methods to:

  • Convert images to standard RGB (24-bit) or ARGB (32-bit) formats
  • Handle transparent areas consistently
  • Diagnose format compatibility issues
  • Save and load images with consistent formatting

This is particularly useful when working with:

  • Screenshots from different capture methods
  • Pattern images created in different tools
  • Cross-platform pattern matching
  • DPI-scaled environments
Since:
1.0
  • Field Details

    • DEFAULT_BACKGROUND

      public static final Color DEFAULT_BACKGROUND
      Default background color for transparent areas (dark gray)
  • Constructor Details

    • ImageNormalizer

      public ImageNormalizer()
  • Method Details

    • normalizeToRGB

      public static BufferedImage normalizeToRGB(BufferedImage source)
      Normalizes an image to consistent RGB format (24-bit) without alpha channel. This ensures saved and loaded images have the same bit depth.
      Parameters:
      source - The source image
      Returns:
      A normalized RGB image, or null if source is null
    • normalizeToRGB

      public static BufferedImage normalizeToRGB(BufferedImage source, Color backgroundColor)
      Normalizes an image to consistent RGB format (24-bit) without alpha channel, using a specified background color for transparent areas.
      Parameters:
      source - The source image
      backgroundColor - The background color for transparent areas
      Returns:
      A normalized RGB image, or null if source is null
    • normalizeToARGB

      public static BufferedImage normalizeToARGB(BufferedImage source)
      Normalizes an image to consistent ARGB format (32-bit) with alpha channel.
      Parameters:
      source - The source image
      Returns:
      A normalized ARGB image, or null if source is null
    • saveNormalizedImage

      public static void saveNormalizedImage(BufferedImage image, File file) throws IOException
      Saves an image with consistent RGB format to ensure matching compatibility.
      Parameters:
      image - The image to save
      file - The file to save to
      Throws:
      IOException - If save fails
    • loadNormalizedImage

      public static BufferedImage loadNormalizedImage(File file) throws IOException
      Loads and normalizes an image to RGB format.
      Parameters:
      file - The file to load from
      Returns:
      The normalized image
      Throws:
      IOException - If load fails
    • areFormatsCompatible

      public static boolean areFormatsCompatible(BufferedImage img1, BufferedImage img2)
      Checks if two images have compatible formats for matching.
      Parameters:
      img1 - First image
      img2 - Second image
      Returns:
      true if formats are compatible, false otherwise
    • diagnoseImage

      public static void diagnoseImage(BufferedImage image, String label)
      Diagnoses image format issues and logs detailed information.
      Parameters:
      image - The image to diagnose
      label - A label for the image in logs
    • getDiagnosticReport

      public static String getDiagnosticReport(BufferedImage image)
      Creates a diagnostic report string for an image.
      Parameters:
      image - The image to diagnose
      Returns:
      A diagnostic report string
    • getImageTypeName

      public static String getImageTypeName(int type)
      Gets a human-readable name for a BufferedImage type constant.
      Parameters:
      type - The BufferedImage type constant
      Returns:
      A human-readable description of the type