ImageMask

Inheritance: java.lang.Object

All Implemented Interfaces: com.aspose.imaging.magicwand.imagemasks.IImageMask

public abstract class ImageMask implements IImageMask

Describes a binary image mask.

Methods

MethodDescription
getSource()Gets the source image used to create this mask, if exists.
getWidth()Gets the width, in pixels, of this mask.
getHeight()Gets the height, in pixels, of this mask.
getBounds()Gets the bounds, in pixels, of this mask.
getSelectionBounds()Gets the bounds of the selected part of the mask, in pixels.
get_Item(int x, int y)Gets the opacity of the specified pixel.
inflate(int size)Inflates this mask by the specified amount.
crop(Size size)Crops mask with the specified size.
crop(int width, int height)Crops mask with the specified width and height.
crop(Rectangle rectangle)Crops mask with the specified rectangle.
isOpaque(int x, int y)Checks if the specified pixel is opaque.
isTransparent(int x, int y)Checks if the specified pixel is transparent.
getByteOpacity(int x, int y)Gets the opacity of the specified pixel with byte precision.
deepClone()Creates a new object that is a copy of the current instance.
getFeathered()Gets grayscale mask with the border feathered with the default settings.
getFeathered(FeatheringSettings settings)Gets grayscale mask with the border feathered with the specified settings.
apply()Applies current mask to the RasterImage source, if exists.
applyTo(RasterImage image)Applies current mask to the specified RasterImage.
invert()Gets the inversion of the current mask.
union(ImageMask mask)Gets the union of the current mask with provided.
union()Gets the union of the current mask with the result of magic wand selection applied to the source of the mask.
union(MagicWandSettings settings)Gets the union of the current mask with the result of magic wand selection applied to the source of the mask.
union(RasterImage image)Gets the union of the current mask with the result of magic wand selection applied to the provided image.
union(RasterImage image, MagicWandSettings settings)Gets the union of the current mask with the result of magic wand selection applied to the provided image.
subtract(ImageMask mask)Gets the subtraction of the provided mask from current.
subtract()Gets the result of magic wand selection applied to the source of the current mask subtracted from the mask.
subtract(MagicWandSettings settings)Gets the result of magic wand selection applied to the source of the current mask subtracted from the mask.
subtract(RasterImage image)Gets the result of magic wand selection applied to the provided image subtracted from the current mask.
subtract(RasterImage image, MagicWandSettings settings)Gets the result of magic wand selection applied to the provided image subtracted from the current mask.
intersect(ImageMask mask)Gets the intersection of current mask with provided.
intersect()Gets the intersection of the current mask with the result of magic wand selection applied to the source of the mask.
intersect(MagicWandSettings settings)Gets the intersection of the current mask with the result of magic wand selection applied to the source of the mask.
intersect(RasterImage image)Gets the intersection of the current mask with the result of magic wand selection applied to the provided image.
intersect(RasterImage image, MagicWandSettings settings)Gets the intersection of the current mask with the result of magic wand selection applied to the provided image.
exclusiveDisjunction(ImageMask mask)Gets the exclusive disjunction of current mask with provided.
exclusiveDisjunction()Gets the exclusive disjunction of the current mask with the result of magic wand selection applied to the source of the mask.
exclusiveDisjunction(MagicWandSettings settings)Gets the exclusive disjunction of the current mask with the result of magic wand selection applied to the source of the mask.
exclusiveDisjunction(RasterImage image)Gets the exclusive disjunction of the current mask with the result of magic wand selection applied to the provided image.
exclusiveDisjunction(RasterImage image, MagicWandSettings settings)Gets the exclusive disjunction of the current mask with the result of magic wand selection applied to the provided image.
to_ImageGrayscaleMask(ImageMask mask)Casting mask to a ImageGrayscaleMask.
op_LogicalNot(ImageMask a)Inverts mask.
op_Addition(ImageMask a, ImageMask b)Union of two masks.
op_Subtraction(ImageMask a, ImageMask b)Subtract second mask from first.
op_Multiply(ImageMask a, ImageMask b)Intersection of two masks.
op_ExclusiveOr(ImageMask a, ImageMask b)Exclusive disjunction of two masks.

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);
}

getSource()

public final RasterImage getSource()

Gets the source image used to create this mask, if exists.

Returns: RasterImage - the source image used to create this mask, if exists.

getWidth()

public final int getWidth()

Gets the width, in pixels, of this mask.

Returns: int - the width, in pixels, of this mask.

getHeight()

public final int getHeight()

Gets the height, in pixels, of this mask.

Returns: int - the height, in pixels, of this mask.

getBounds()

public final Rectangle getBounds()

Gets the bounds, in pixels, of this mask.

Returns: Rectangle - the bounds, in pixels, of this mask.

getSelectionBounds()

public abstract Rectangle getSelectionBounds()

Gets the bounds of the selected part of the mask, in pixels.

Returns: Rectangle - the bounds of the selected part of the mask, in pixels.

get_Item(int x, int y)

public abstract boolean get_Item(int x, int y)

Gets the opacity of the specified pixel.

Parameters:

ParameterTypeDescription
xintThe x-coordinate of the pixel.
yintThe y-coordinate of the pixel.

Returns: boolean - true if the specified pixel is opaque; otherwise, false.

inflate(int size)

public abstract ImageMask inflate(int size)

Inflates this mask by the specified amount.

Parameters:

ParameterTypeDescription
sizeintThe amount to inflate this mask.

Returns: ImageMask - An ImageMask.

crop(Size size)

public final ImageMask crop(Size size)

Crops mask with the specified size.

Parameters:

ParameterTypeDescription
sizeSizeThe specified size.

Returns: ImageMask - An ImageMask.

crop(int width, int height)

public final ImageMask crop(int width, int height)

Crops mask with the specified width and height.

Parameters:

ParameterTypeDescription
widthintThe specified width.
heightintThe specified height.

Returns: ImageMask - An ImageMask.

crop(Rectangle rectangle)

public abstract ImageMask crop(Rectangle rectangle)

Crops mask with the specified rectangle.

Parameters:

ParameterTypeDescription
rectangleRectangleThe specified rectangle.

Returns: ImageMask - An ImageMask.

isOpaque(int x, int y)

public final boolean isOpaque(int x, int y)

Checks if the specified pixel is opaque.

Parameters:

ParameterTypeDescription
xintThe x-coordinate of the pixel.
yintThe y-coordinate of the pixel.

Returns: boolean - true if the specified pixel is opaque; otherwise, false.

isTransparent(int x, int y)

public final boolean isTransparent(int x, int y)

Checks if the specified pixel is transparent.

Parameters:

ParameterTypeDescription
xintThe x-coordinate of the pixel.
yintThe y-coordinate of the pixel.

Returns: boolean - true if the specified pixel is transparent; otherwise, false.

getByteOpacity(int x, int y)

public final byte getByteOpacity(int x, int y)

Gets the opacity of the specified pixel with byte precision.

Parameters:

ParameterTypeDescription
xintThe x-coordinate of the pixel.
yintThe y-coordinate of the pixel.

Returns: byte - Byte value, representing the opacity of the specified pixel.

deepClone()

public abstract Object deepClone()

Creates a new object that is a copy of the current instance.

Returns: java.lang.Object - A new object that is a copy of this instance.

getFeathered()

public final ImageGrayscaleMask getFeathered()

Gets grayscale mask with the border feathered with the default settings.

Returns: ImageGrayscaleMask - .to_ImageGrayscaleMask(ImageMask)} with feathered border.

getFeathered(FeatheringSettings settings)

public final ImageGrayscaleMask getFeathered(FeatheringSettings settings)

Gets grayscale mask with the border feathered with the specified settings.

Parameters:

ParameterTypeDescription
settingsFeatheringSettingsFeathering settings.

Returns: ImageGrayscaleMask - .to_ImageGrayscaleMask(ImageMask)} with feathered border.

apply()

public final void apply()

Applies current mask to the RasterImage source, if exists.

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);
    }});
}

applyTo(RasterImage image)

public final void applyTo(RasterImage image)

Applies current mask to the specified RasterImage.

Parameters:

ParameterTypeDescription
imageRasterImageImage to apply mask to.

invert()

public final ImageBitMask invert()

Gets the inversion of the current mask.

Returns: ImageBitMask - New ImageBitMask.

union(ImageMask mask)

public final ImageBitMask union(ImageMask mask)

Gets the union of the current mask with provided.

Parameters:

ParameterTypeDescription
maskImageMaskProvided mask

Returns: ImageBitMask - New ImageBitMask.

union()

public final ImageBitMask union()

Gets the union of the current mask with the result of magic wand selection applied to the source of the mask.

Returns: ImageBitMask - New ImageBitMask.

union(MagicWandSettings settings)

public final ImageBitMask union(MagicWandSettings settings)

Gets the union of the current mask with the result of magic wand selection applied to the source of the mask.

Parameters:

ParameterTypeDescription
settingsMagicWandSettingsMagic wand settings.

Returns: ImageBitMask - New ImageBitMask.

union(RasterImage image)

public final ImageBitMask union(RasterImage image)

Gets the union of the current mask with the result of magic wand selection applied to the provided image.

Parameters:

ParameterTypeDescription
imageRasterImageImage for magic wand.

Returns: ImageBitMask - New ImageBitMask.

union(RasterImage image, MagicWandSettings settings)

public final ImageBitMask union(RasterImage image, MagicWandSettings settings)

Gets the union of the current mask with the result of magic wand selection applied to the provided image.

Parameters:

ParameterTypeDescription
imageRasterImageImage for magic wand.
settingsMagicWandSettingsMagic wand settings.

Returns: ImageBitMask - New ImageBitMask.

subtract(ImageMask mask)

public final ImageBitMask subtract(ImageMask mask)

Gets the subtraction of the provided mask from current.

Parameters:

ParameterTypeDescription
maskImageMaskProvided mask

Returns: ImageBitMask - New ImageBitMask.

subtract()

public final ImageBitMask subtract()

Gets the result of magic wand selection applied to the source of the current mask subtracted from the mask.

Returns: ImageBitMask - New ImageBitMask.

subtract(MagicWandSettings settings)

public final ImageBitMask subtract(MagicWandSettings settings)

Gets the result of magic wand selection applied to the source of the current mask subtracted from the mask.

Parameters:

ParameterTypeDescription
settingsMagicWandSettingsMagic wand settings.

Returns: ImageBitMask - New ImageBitMask.

subtract(RasterImage image)

public final ImageBitMask subtract(RasterImage image)

Gets the result of magic wand selection applied to the provided image subtracted from the current mask.

Parameters:

ParameterTypeDescription
imageRasterImageImage for magic wand.

Returns: ImageBitMask - New ImageBitMask.

subtract(RasterImage image, MagicWandSettings settings)

public final ImageBitMask subtract(RasterImage image, MagicWandSettings settings)

Gets the result of magic wand selection applied to the provided image subtracted from the current mask.

Parameters:

ParameterTypeDescription
imageRasterImageImage for magic wand.
settingsMagicWandSettingsMagic wand settings.

Returns: ImageBitMask - New ImageBitMask.

intersect(ImageMask mask)

public final ImageBitMask intersect(ImageMask mask)

Gets the intersection of current mask with provided.

Parameters:

ParameterTypeDescription
maskImageMaskProvided mask

Returns: ImageBitMask - New ImageBitMask.

intersect()

public final ImageBitMask intersect()

Gets the intersection of the current mask with the result of magic wand selection applied to the source of the mask.

Returns: ImageBitMask - New ImageBitMask.

intersect(MagicWandSettings settings)

public final ImageBitMask intersect(MagicWandSettings settings)

Gets the intersection of the current mask with the result of magic wand selection applied to the source of the mask.

Parameters:

ParameterTypeDescription
settingsMagicWandSettingsMagic wand settings.

Returns: ImageBitMask - New ImageBitMask.

intersect(RasterImage image)

public final ImageBitMask intersect(RasterImage image)

Gets the intersection of the current mask with the result of magic wand selection applied to the provided image.

Parameters:

ParameterTypeDescription
imageRasterImageImage for magic wand.

Returns: ImageBitMask - New ImageBitMask.

intersect(RasterImage image, MagicWandSettings settings)

public final ImageBitMask intersect(RasterImage image, MagicWandSettings settings)

Gets the intersection of the current mask with the result of magic wand selection applied to the provided image.

Parameters:

ParameterTypeDescription
imageRasterImageImage for magic wand.
settingsMagicWandSettingsMagic wand settings.

Returns: ImageBitMask - New ImageBitMask.

exclusiveDisjunction(ImageMask mask)

public final ImageBitMask exclusiveDisjunction(ImageMask mask)

Gets the exclusive disjunction of current mask with provided.

Parameters:

ParameterTypeDescription
maskImageMaskProvided mask

Returns: ImageBitMask - New ImageBitMask.

exclusiveDisjunction()

public final ImageBitMask exclusiveDisjunction()

Gets the exclusive disjunction of the current mask with the result of magic wand selection applied to the source of the mask.

Returns: ImageBitMask - New ImageBitMask.

exclusiveDisjunction(MagicWandSettings settings)

public final ImageBitMask exclusiveDisjunction(MagicWandSettings settings)

Gets the exclusive disjunction of the current mask with the result of magic wand selection applied to the source of the mask.

Parameters:

ParameterTypeDescription
settingsMagicWandSettingsMagic wand settings.

Returns: ImageBitMask - New ImageBitMask.

exclusiveDisjunction(RasterImage image)

public final ImageBitMask exclusiveDisjunction(RasterImage image)

Gets the exclusive disjunction of the current mask with the result of magic wand selection applied to the provided image.

Parameters:

ParameterTypeDescription
imageRasterImageImage for magic wand.

Returns: ImageBitMask - New ImageBitMask.

exclusiveDisjunction(RasterImage image, MagicWandSettings settings)

public final ImageBitMask exclusiveDisjunction(RasterImage image, MagicWandSettings settings)

Gets the exclusive disjunction of the current mask with the result of magic wand selection applied to the provided image.

Parameters:

ParameterTypeDescription
imageRasterImageImage for magic wand.
settingsMagicWandSettingsMagic wand settings.

Returns: ImageBitMask - New ImageBitMask.

to_ImageGrayscaleMask(ImageMask mask)

public static ImageGrayscaleMask to_ImageGrayscaleMask(ImageMask mask)

Casting mask to a ImageGrayscaleMask.

Parameters:

ParameterTypeDescription
maskImageMaskThe mask value.

Returns: ImageGrayscaleMask - The new ImageGrayscaleMask base on mask.

op_LogicalNot(ImageMask a)

public static ImageBitMask op_LogicalNot(ImageMask a)

Inverts mask.

Parameters:

ParameterTypeDescription
aImageMaskThe mask to be inverted.

Returns: ImageBitMask - New ImageBitMask.

op_Addition(ImageMask a, ImageMask b)

public static ImageBitMask op_Addition(ImageMask a, ImageMask b)

Union of two masks.

Parameters:

ParameterTypeDescription
aImageMaskThe first mask.
bImageMaskThe second mask.

Returns: ImageBitMask - New ImageBitMask.

op_Subtraction(ImageMask a, ImageMask b)

public static ImageBitMask op_Subtraction(ImageMask a, ImageMask b)

Subtract second mask from first.

Parameters:

ParameterTypeDescription
aImageMaskThe first mask.
bImageMaskThe second mask.

Returns: ImageBitMask - New ImageBitMask.

op_Multiply(ImageMask a, ImageMask b)

public static ImageBitMask op_Multiply(ImageMask a, ImageMask b)

Intersection of two masks.

Parameters:

ParameterTypeDescription
aImageMaskThe first mask.
bImageMaskThe second mask.

Returns: ImageBitMask - New ImageBitMask.

op_ExclusiveOr(ImageMask a, ImageMask b)

public static ImageBitMask op_ExclusiveOr(ImageMask a, ImageMask b)

Exclusive disjunction of two masks.

Parameters:

ParameterTypeDescription
aImageMaskThe first mask.
bImageMaskThe second mask.

Returns: ImageBitMask - New ImageBitMask.