public class ImageGrayscaleMask extends Object implements IImageMask
Describes a grayscale 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);
}
| Constructor and Description |
|---|
ImageGrayscaleMask(int width,
int height)
Initializes a new instance of the
ImageGrayscaleMask class with the specified width and height. |
ImageGrayscaleMask(RasterImage image)
Initializes a new instance of the
ImageGrayscaleMask class with the size of the specified existing RasterImage. |
| 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. |
ImageGrayscaleMask |
crop(int width,
int height)
Crops mask with the specified width and height.
|
ImageGrayscaleMask |
crop(Rectangle rectangle)
Crops mask with the specified rectangle.
|
ImageGrayscaleMask |
crop(Size size)
Crops mask with the specified size.
|
Object |
deepClone()
Creates a new object that is a copy of the current instance.
|
ImageGrayscaleMask |
exclusiveDisjunction(ImageGrayscaleMask mask)
Gets the exclusive disjunction of current mask with provided.
|
byte |
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.
|
int |
getHeight()
Gets the height, in pixels, of this mask.
|
Rectangle |
getSelectionBounds()
Gets the bounds of the selected part of the mask, in pixels.
|
RasterImage |
getSource()
Gets the source image used to create this mask, if exists.
|
int |
getWidth()
Gets the width, in pixels, of this mask.
|
ImageGrayscaleMask |
intersect(ImageGrayscaleMask mask)
Gets the intersection of current mask with provided.
|
ImageGrayscaleMask |
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 ImageGrayscaleMask |
op_Addition(ImageGrayscaleMask a,
ImageGrayscaleMask b)
Union of two masks.
|
static ImageGrayscaleMask |
op_ExclusiveOr(ImageGrayscaleMask a,
ImageGrayscaleMask b)
Exclusive disjunction of two masks.
|
static ImageGrayscaleMask |
op_LogicalNot(ImageGrayscaleMask a)
Inverts mask.
|
static ImageGrayscaleMask |
op_Multiply(ImageGrayscaleMask a,
ImageGrayscaleMask b)
Intersection of two masks.
|
static ImageGrayscaleMask |
op_Subtraction(ImageGrayscaleMask a,
ImageGrayscaleMask b)
Subtract second mask from first.
|
void |
set_Item(int x,
int y,
byte value)
Sets the opacity of the specified pixel.
|
ImageGrayscaleMask |
subtract(ImageGrayscaleMask mask)
Gets the subtraction of the provided mask from current.
|
ImageGrayscaleMask |
union(ImageGrayscaleMask mask)
Union of two masks.
|
public ImageGrayscaleMask(int width,
int height)
Initializes a new instance of the ImageGrayscaleMask class with the specified width and height.
width - Width of the mask.height - Height of the mask.public ImageGrayscaleMask(RasterImage image)
Initializes a new instance of the ImageGrayscaleMask class with the size of the specified existing RasterImage.
Specified RasterImage will be stored as source image.
image - Source image.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 final Rectangle getSelectionBounds()
Gets the bounds of the selected part of the mask, in pixels.
getSelectionBounds in interface IImageMaskpublic final byte 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.
Value:
Byte value; 0 if transparent; 255 if opaque.public final void set_Item(int x,
int y,
byte value)
Sets the opacity of the specified pixel.
x - The x-coordinate of the pixel.y - The y-coordinate of the pixel.
Value:
Byte value; 0 if transparent; 255 if opaque.value - the opacity of the specified pixel.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 final Object deepClone()
Creates a new object that is a copy of the current instance.
deepClone in interface com.aspose.ms.System.ICloneablepublic 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.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 ImageGrayscaleMask crop(Size size)
Crops mask with the specified size.
size - The specified size.ImageGrayscaleMask.public final ImageGrayscaleMask crop(int width, int height)
Crops mask with the specified width and height.
width - The specified width.height - The specified height.ImageGrayscaleMask.public final ImageGrayscaleMask crop(Rectangle rectangle)
Crops mask with the specified rectangle.
rectangle - The specified rectangle.ImageGrayscaleMask.public final ImageGrayscaleMask invert()
Gets the inversion of the current mask.
ImageGrayscaleMask.public final ImageGrayscaleMask union(ImageGrayscaleMask mask)
Union of two masks.
mask - Provided maskImageGrayscaleMask.public final ImageGrayscaleMask subtract(ImageGrayscaleMask mask)
Gets the subtraction of the provided mask from current.
mask - Provided maskImageGrayscaleMask.public final ImageGrayscaleMask intersect(ImageGrayscaleMask mask)
Gets the intersection of current mask with provided.
mask - Provided maskImageGrayscaleMask.public final ImageGrayscaleMask exclusiveDisjunction(ImageGrayscaleMask mask)
Gets the exclusive disjunction of current mask with provided.
mask - Provided maskImageGrayscaleMask.public static ImageGrayscaleMask op_LogicalNot(ImageGrayscaleMask a)
Inverts mask.
a - The mask to be inverted.ImageGrayscaleMask.public static ImageGrayscaleMask op_Addition(ImageGrayscaleMask a, ImageGrayscaleMask b)
Union of two masks.
a - The first mask.b - The second mask.ImageGrayscaleMask.public static ImageGrayscaleMask op_Subtraction(ImageGrayscaleMask a, ImageGrayscaleMask b)
Subtract second mask from first.
a - The first mask.b - The second mask.ImageGrayscaleMask.public static ImageGrayscaleMask op_Multiply(ImageGrayscaleMask a, ImageGrayscaleMask b)
Intersection of two masks.
a - The first mask.b - The second mask.ImageGrayscaleMask.public static ImageGrayscaleMask op_ExclusiveOr(ImageGrayscaleMask a, ImageGrayscaleMask b)
Exclusive disjunction of two masks.
a - The first mask.b - The second mask.ImageGrayscaleMask.Copyright (c) 2008-2025 Aspose Pty Ltd. All Rights Reserved.