public abstract class ImageMask extends Object implements IImageMask
Describes a binary image mask.
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);
}
| Modifier and Type | Method and Description |
|---|---|
void |
apply()
Applies current mask to the
RasterImage source, if exists. |
void |
applyTo(RasterImage image)
Applies current mask to the specified
RasterImage. |
ImageMask |
crop(int width,
int height)
Crops mask with the specified width and height.
|
abstract ImageMask |
crop(Rectangle rectangle)
Crops mask with the specified rectangle.
|
ImageMask |
crop(Size size)
Crops mask with the specified size.
|
abstract Object |
deepClone()
Creates a new object that is a copy of the current instance.
|
ImageBitMask |
exclusiveDisjunction()
Gets the exclusive disjunction of the current mask with the result of magic wand selection applied to the source of the mask.
|
ImageBitMask |
exclusiveDisjunction(ImageMask mask)
Gets the exclusive disjunction of current mask with provided.
|
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.
|
ImageBitMask |
exclusiveDisjunction(RasterImage image)
Gets the exclusive disjunction of the current mask with the result of magic wand selection applied to the provided image.
|
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.
|
abstract boolean |
get_Item(int x,
int y)
Gets the opacity of the specified pixel.
|
Rectangle |
getBounds()
Gets the bounds, in pixels, of this mask.
|
byte |
getByteOpacity(int x,
int y)
Gets the opacity of the specified pixel with byte precision.
|
ImageGrayscaleMask |
getFeathered()
Gets grayscale mask with the border feathered with the default settings.
|
ImageGrayscaleMask |
getFeathered(FeatheringSettings settings)
Gets grayscale mask with the border feathered with the specified settings.
|
int |
getHeight()
Gets the height, in pixels, of this mask.
|
RasterImage |
getSource()
Gets the source image used to create this mask, if exists.
|
int |
getWidth()
Gets the width, in pixels, of this mask.
|
abstract ImageMask |
inflate(int size)
Inflates this mask by the specified amount.
|
ImageBitMask |
intersect()
Gets the intersection of the current mask with the result of magic wand selection applied to the source of the mask.
|
ImageBitMask |
intersect(ImageMask mask)
Gets the intersection of current mask with provided.
|
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.
|
ImageBitMask |
intersect(RasterImage image)
Gets the intersection of the current mask with the result of magic wand selection applied to the provided image.
|
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.
|
ImageBitMask |
invert()
Gets the inversion of the current mask.
|
boolean |
isOpaque(int x,
int y)
Checks if the specified pixel is opaque.
|
boolean |
isTransparent(int x,
int y)
Checks if the specified pixel is transparent.
|
static ImageBitMask |
op_Addition(ImageMask a,
ImageMask b)
Union of two masks.
|
static ImageBitMask |
op_ExclusiveOr(ImageMask a,
ImageMask b)
Exclusive disjunction of two masks.
|
static ImageBitMask |
op_LogicalNot(ImageMask a)
Inverts mask.
|
static ImageBitMask |
op_Multiply(ImageMask a,
ImageMask b)
Intersection of two masks.
|
static ImageBitMask |
op_Subtraction(ImageMask a,
ImageMask b)
Subtract second mask from first.
|
ImageBitMask |
subtract()
Gets the result of magic wand selection applied to the source of the current mask subtracted from the mask.
|
ImageBitMask |
subtract(ImageMask mask)
Gets the subtraction of the provided mask from current.
|
ImageBitMask |
subtract(MagicWandSettings settings)
Gets the result of magic wand selection applied to the source of the current mask subtracted from the mask.
|
ImageBitMask |
subtract(RasterImage image)
Gets the result of magic wand selection applied to the provided image subtracted from the current mask.
|
ImageBitMask |
subtract(RasterImage image,
MagicWandSettings settings)
Gets the result of magic wand selection applied to the provided image subtracted from the current mask.
|
static ImageGrayscaleMask |
to_ImageGrayscaleMask(ImageMask mask)
Casting
mask to a ImageGrayscaleMask. |
ImageBitMask |
union()
Gets the union of the current mask with the result of magic wand selection applied to the source of the mask.
|
ImageBitMask |
union(ImageMask mask)
Gets the union of the current mask with provided.
|
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.
|
ImageBitMask |
union(RasterImage image)
Gets the union of the current mask with the result of magic wand selection applied to the provided image.
|
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.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetSelectionBoundspublic static ImageGrayscaleMask to_ImageGrayscaleMask(ImageMask mask)
Casting mask to a ImageGrayscaleMask.
mask - The mask value.ImageGrayscaleMask base on mask.public static ImageBitMask op_LogicalNot(ImageMask a)
Inverts mask.
a - The mask to be inverted.ImageBitMask.public static ImageBitMask op_Addition(ImageMask a, ImageMask b)
Union of two masks.
a - The first mask.b - The second mask.ImageBitMask.public static ImageBitMask op_Subtraction(ImageMask a, ImageMask b)
Subtract second mask from first.
a - The first mask.b - The second mask.ImageBitMask.public static ImageBitMask op_Multiply(ImageMask a, ImageMask b)
Intersection of two masks.
a - The first mask.b - The second mask.ImageBitMask.public static ImageBitMask op_ExclusiveOr(ImageMask a, ImageMask b)
Exclusive disjunction of two masks.
a - The first mask.b - The second mask.ImageBitMask.public final RasterImage getSource()
Gets the source image used to create this mask, if exists.
getSource in interface IImageMaskpublic final int getWidth()
Gets the width, in pixels, of this mask.
getWidth in interface IImageMaskpublic final int getHeight()
Gets the height, in pixels, of this mask.
getHeight in interface IImageMaskpublic final Rectangle getBounds()
Gets the bounds, in pixels, of this mask.
getBounds in interface IImageMaskpublic abstract boolean get_Item(int x,
int y)
Gets the opacity of the specified pixel.
x - The x-coordinate of the pixel.y - The y-coordinate of the pixel.public abstract ImageMask inflate(int size)
Inflates this mask by the specified amount.
size - The amount to inflate this mask.public final ImageMask crop(Size size)
Crops mask with the specified size.
size - The specified size.public final ImageMask crop(int width, int height)
Crops mask with the specified width and height.
width - The specified width.height - The specified height.public abstract ImageMask crop(Rectangle rectangle)
Crops mask with the specified rectangle.
rectangle - The specified rectangle.public final boolean isOpaque(int x,
int y)
Checks if the specified pixel is opaque.
isOpaque in interface IImageMaskx - The x-coordinate of the pixel.y - The y-coordinate of the pixel.public final boolean isTransparent(int x,
int y)
Checks if the specified pixel is transparent.
isTransparent in interface IImageMaskx - The x-coordinate of the pixel.y - The y-coordinate of the pixel.public final byte getByteOpacity(int x,
int y)
Gets the opacity of the specified pixel with byte precision.
getByteOpacity in interface IImageMaskx - The x-coordinate of the pixel.y - The y-coordinate of the pixel.public abstract Object deepClone()
Creates a new object that is a copy of the current instance.
deepClone in interface com.aspose.ms.System.ICloneablepublic final ImageGrayscaleMask getFeathered()
Gets grayscale mask with the border feathered with the default settings.
ImageMask.to_ImageGrayscaleMask(ImageMask)} with feathered border.public final ImageGrayscaleMask getFeathered(FeatheringSettings settings)
Gets grayscale mask with the border feathered with the specified settings.
settings - Feathering settings.ImageMask.to_ImageGrayscaleMask(ImageMask)} with feathered border.public final void apply()
Applies current mask to the RasterImage source, if exists.
com.aspose.ms.System.NullReferenceException - Thrown when the source image is not defined.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);
}});
}
public final void applyTo(RasterImage image)
Applies current mask to the specified RasterImage.
image - Image to apply mask to.com.aspose.ms.System.ArgumentNullException - Thrown when the image is not defined.public final ImageBitMask invert()
Gets the inversion of the current mask.
ImageBitMask.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);
}
public final ImageBitMask union(ImageMask mask)
Gets the union of the current mask with provided.
mask - Provided maskImageBitMask.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.
ImageBitMask.com.aspose.ms.System.ArgumentNullException - Thrown when the source image is not defined in mask.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.
settings - Magic wand settings.ImageBitMask.com.aspose.ms.System.ArgumentNullException - Thrown when the source image is not defined in mask.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.
image - Image for magic wand.ImageBitMask.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.
image - Image for magic wand.settings - Magic wand settings.ImageBitMask.public final ImageBitMask subtract(ImageMask mask)
Gets the subtraction of the provided mask from current.
mask - Provided maskImageBitMask.public final ImageBitMask subtract()
Gets the result of magic wand selection applied to the source of the current mask subtracted from the mask.
ImageBitMask.com.aspose.ms.System.ArgumentNullException - Thrown when the source image is not defined in mask.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.
settings - Magic wand settings.ImageBitMask.com.aspose.ms.System.ArgumentNullException - Thrown when the source image is not defined in mask.public final ImageBitMask subtract(RasterImage image)
Gets the result of magic wand selection applied to the provided image subtracted from the current mask.
image - Image for magic wand.ImageBitMask.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.
image - Image for magic wand.settings - Magic wand settings.ImageBitMask.public final ImageBitMask intersect(ImageMask mask)
Gets the intersection of current mask with provided.
mask - Provided maskImageBitMask.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.
ImageBitMask.com.aspose.ms.System.ArgumentNullException - Thrown when the source image is not defined in mask.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.
settings - Magic wand settings.ImageBitMask.com.aspose.ms.System.ArgumentNullException - Thrown when the source image is not defined in mask.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.
image - Image for magic wand.ImageBitMask.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.
image - Image for magic wand.settings - Magic wand settings.ImageBitMask.public final ImageBitMask exclusiveDisjunction(ImageMask mask)
Gets the exclusive disjunction of current mask with provided.
mask - Provided maskImageBitMask.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.
ImageBitMask.com.aspose.ms.System.ArgumentNullException - Thrown when the source image is not defined in mask.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.
settings - Magic wand settings.ImageBitMask.com.aspose.ms.System.ArgumentNullException - Thrown when the source image is not defined in mask.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.
image - Image for magic wand.ImageBitMask.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.
image - Image for magic wand.settings - Magic wand settings.ImageBitMask.Copyright (c) 2008-2025 Aspose Pty Ltd. All Rights Reserved.