Class ImageNormalizer
java.lang.Object
io.github.jspinak.brobot.util.image.ImageNormalizer
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final ColorDefault background color for transparent areas (dark gray) -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanareFormatsCompatible(BufferedImage img1, BufferedImage img2) Checks if two images have compatible formats for matching.static voiddiagnoseImage(BufferedImage image, String label) Diagnoses image format issues and logs detailed information.static StringgetDiagnosticReport(BufferedImage image) Creates a diagnostic report string for an image.static StringgetImageTypeName(int type) Gets a human-readable name for a BufferedImage type constant.static BufferedImageloadNormalizedImage(File file) Loads and normalizes an image to RGB format.static BufferedImagenormalizeToARGB(BufferedImage source) Normalizes an image to consistent ARGB format (32-bit) with alpha channel.static BufferedImagenormalizeToRGB(BufferedImage source) Normalizes an image to consistent RGB format (24-bit) without alpha channel.static BufferedImagenormalizeToRGB(BufferedImage source, Color backgroundColor) Normalizes an image to consistent RGB format (24-bit) without alpha channel, using a specified background color for transparent areas.static voidsaveNormalizedImage(BufferedImage image, File file) Saves an image with consistent RGB format to ensure matching compatibility.
-
Field Details
-
DEFAULT_BACKGROUND
Default background color for transparent areas (dark gray)
-
-
Constructor Details
-
ImageNormalizer
public ImageNormalizer()
-
-
Method Details
-
normalizeToRGB
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
Normalizes an image to consistent RGB format (24-bit) without alpha channel, using a specified background color for transparent areas.- Parameters:
source- The source imagebackgroundColor- The background color for transparent areas- Returns:
- A normalized RGB image, or null if source is null
-
normalizeToARGB
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
Saves an image with consistent RGB format to ensure matching compatibility.- Parameters:
image- The image to savefile- The file to save to- Throws:
IOException- If save fails
-
loadNormalizedImage
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
Checks if two images have compatible formats for matching.- Parameters:
img1- First imageimg2- Second image- Returns:
- true if formats are compatible, false otherwise
-
diagnoseImage
Diagnoses image format issues and logs detailed information.- Parameters:
image- The image to diagnoselabel- A label for the image in logs
-
getDiagnosticReport
Creates a diagnostic report string for an image.- Parameters:
image- The image to diagnose- Returns:
- A diagnostic report string
-
getImageTypeName
Gets a human-readable name for a BufferedImage type constant.- Parameters:
type- The BufferedImage type constant- Returns:
- A human-readable description of the type
-