MagicWandSettings

Inheritance: java.lang.Object

public class MagicWandSettings

A magic wand selection settings class.

Constructors

ConstructorDescription
MagicWandSettings(Point point)Initializes a new instance of the MagicWandSettings class.
MagicWandSettings(int x, int y)Initializes a new instance of the MagicWandSettings class.

Methods

MethodDescription
getAreaOfInterest()Gets the bounds of the area for algorithm work.
setAreaOfInterest(Rectangle value)Sets the bounds of the area for algorithm work.
getPoint()Gets the reference point for algorithm work.
getThreshold()Gets the tolerance level for pixels color comparison.
setThreshold(int value)Sets the tolerance level for pixels color comparison.
getContiguousMode()Gets a value indicating whether magic wand will define only contiguous pixels.
setContiguousMode(boolean value)Sets a value indicating whether magic wand will define only contiguous pixels.
getDirectionalMode()Gets the mode of flood fill search algorithm: four of eight direction search.
setDirectionalMode(int value)Sets the mode of flood fill search algorithm: four of eight direction search.
getColorCompareMode()Gets the algorithm how colors are compared.
setColorCompareMode(int value)Sets the algorithm how colors are compared.
getColorComparisonDelegate()Gets the custom color comparison algorithm if ColorCompareMode(.getColorCompareMode/.setColorCompareMode(int)) is set to ColorComparisonMode.Custom.
setColorComparisonDelegate(MagicWandSettings.ColorComparison value)Sets the custom color comparison algorithm if ColorCompareMode(.getColorCompareMode/.setColorCompareMode(int)) is set to ColorComparisonMode.Custom.

Example: The example shows how to select a simple area of an image based on tone and color of any pixel using Magic Wand tool.

String imageFilePath = "input.png";
String outputFilePath = "masked.png";
try (RasterImage image = (RasterImage)Image.load(imageFilePath))
{
    // Create a new mask using magic wand tool based on tone and color of pixel (120, 100) with custom threshold equal to 150
    MagicWandTool
            .select(image, new MagicWandSettings(120, 100) {{ setThreshold(150); }})
            // Apply mask to the image
            .apply();

    // Save image with forced transparency color type option
    image.save(outputFilePath, new PngOptions()
    {{
        setColorType(PngColorType.TruecolorWithAlpha);
    }});
}

Example: The example shows how to select a complicated area of an image using Magic Wand tool and the ability to interact with masks (invert, union, subtract).

String imageFilePath = "input.png";
String outputFilePath = "masked-complex.png";
try (RasterImage image = (RasterImage)Image.load(imageFilePath))
{
    // Create a new mask using magic wand tool based on tone and color of pixel (845, 128)
    MagicWandTool.select(image, new MagicWandSettings(845, 128))
            // Union the existing mask with the specified one created by magic wand tool
            .union(new MagicWandSettings(416, 387))
            // Invert the existing mask
            .invert()
            // Subtract the specified mask created by magic wand tool with specified threshold from the existing one
            .subtract(new MagicWandSettings(1482, 346) {{ setThreshold(69); }})
            // Subtract four specified rectangle masks from the existing mask one by one
            .subtract(new RectangleMask(0, 0, 800, 150))
            .subtract(new RectangleMask(0, 380, 600, 220))
            .subtract(new RectangleMask(930, 520, 110, 40))
            .subtract(new RectangleMask(1370, 400, 120, 200))
            // Feather mask with specified settings
            .getFeathered(new FeatheringSettings() {{ setSize(3); }})
            // Apply mask to the image
            .apply();

    // Save image
    image.save(outputFilePath);
}

MagicWandSettings(Point point)

public MagicWandSettings(Point point)

Initializes a new instance of the MagicWandSettings class.

Parameters:

ParameterTypeDescription
pointPointThe reference point.

MagicWandSettings(int x, int y)

public MagicWandSettings(int x, int y)

Initializes a new instance of the MagicWandSettings class.

Parameters:

ParameterTypeDescription
xintThe x-coordinate of the reference point.
yintThe y-coordinate of the reference point.

getAreaOfInterest()

public final Rectangle getAreaOfInterest()

Gets the bounds of the area for algorithm work.

Value: The rectangle representing the bounds of the area of interest.

Returns: Rectangle - the bounds of the area for algorithm work.

setAreaOfInterest(Rectangle value)

public final void setAreaOfInterest(Rectangle value)

Sets the bounds of the area for algorithm work.

Value: The rectangle representing the bounds of the area of interest.

Parameters:

ParameterTypeDescription
valueRectanglethe bounds of the area for algorithm work.

getPoint()

public final Point getPoint()

Gets the reference point for algorithm work.

Value: The Point value.

Returns: Point - the reference point for algorithm work.

getThreshold()

public final int getThreshold()

Gets the tolerance level for pixels color comparison.

Value: The threshold for color comparing.

Returns: int - the tolerance level for pixels color comparison.

setThreshold(int value)

public final void setThreshold(int value)

Sets the tolerance level for pixels color comparison.

Value: The threshold for color comparing.

Parameters:

ParameterTypeDescription
valueintthe tolerance level for pixels color comparison.

getContiguousMode()

public final boolean getContiguousMode()

Gets a value indicating whether magic wand will define only contiguous pixels.

Value: true if the element is enabled; otherwise, false. The default value is true.

Returns: boolean - a value indicating whether magic wand will define only contiguous pixels.

setContiguousMode(boolean value)

public final void setContiguousMode(boolean value)

Sets a value indicating whether magic wand will define only contiguous pixels.

Value: true if the element is enabled; otherwise, false. The default value is true.

Parameters:

ParameterTypeDescription
valuebooleana value indicating whether magic wand will define only contiguous pixels.

getDirectionalMode()

public final int getDirectionalMode()

Gets the mode of flood fill search algorithm: four of eight direction search.

Value: The mode of flood fill search algorithm.

Returns: int - the mode of flood fill search algorithm: four of eight direction search.

setDirectionalMode(int value)

public final void setDirectionalMode(int value)

Sets the mode of flood fill search algorithm: four of eight direction search.

Value: The mode of flood fill search algorithm.

Parameters:

ParameterTypeDescription
valueintthe mode of flood fill search algorithm: four of eight direction search.

getColorCompareMode()

public final int getColorCompareMode()

Gets the algorithm how colors are compared.

Value: The color compare mode.

Returns: int - the algorithm how colors are compared.

setColorCompareMode(int value)

public final void setColorCompareMode(int value)

Sets the algorithm how colors are compared.

Value: The color compare mode.

Parameters:

ParameterTypeDescription
valueintthe algorithm how colors are compared.

getColorComparisonDelegate()

public final MagicWandSettings.ColorComparison getColorComparisonDelegate()

Gets the custom color comparison algorithm if ColorCompareMode(.getColorCompareMode/.setColorCompareMode(int)) is set to ColorComparisonMode.Custom.

Value: The color compare delegate.

Returns: ColorComparison - the custom color comparison algorithm if ColorCompareMode(.getColorCompareMode/.setColorCompareMode(int)) is set to ColorComparisonMode.Custom.

setColorComparisonDelegate(MagicWandSettings.ColorComparison value)

public final void setColorComparisonDelegate(MagicWandSettings.ColorComparison value)

Sets the custom color comparison algorithm if ColorCompareMode(.getColorCompareMode/.setColorCompareMode(int)) is set to ColorComparisonMode.Custom.

Value: The color compare delegate.

Parameters:

ParameterTypeDescription
valueColorComparisonthe custom color comparison algorithm if ColorCompareMode(.getColorCompareMode/.setColorCompareMode(int)) is set to ColorComparisonMode.Custom.