Class MaskedImage

All Implemented Interfaces:
RenderedImage, Disposable

final class MaskedImage extends SourceAlignedImage
An image which is the result of clearing all pixels inside or outside a geometry. The geometry shall be expressed in pixel coordinates.
Since:
1.2
Version:
1.2
  • Field Details

    • clip

      private final Shape clip
      The clip in pixel coordinates.
    • maskInside

      private final boolean maskInside
      true for masking pixels inside the shape, or false for masking pixels outside the shape.
    • maskBounds

      private transient volatile Rectangle maskBounds
      Bounds of the clip in pixels coordinates and in tile coordinates. The latter provides a fast way to determine if a tile intersects the mask. The bounds are computed together when first needed.
      See Also:
    • maskTiles

      private transient volatile Rectangle maskTiles
      Bounds of the clip in pixels coordinates and in tile coordinates. The latter provides a fast way to determine if a tile intersects the mask. The bounds are computed together when first needed.
      See Also:
    • maskRef

      private transient SoftReference<ByteBuffer> maskRef
      The clip after rasterization. Each element contains 8 pixel values. Index of pixel value at coordinate (x,y) can be obtained as below:
      See Also:
    • maskScanlineStride

      private transient int maskScanlineStride
      Number of pixels (bits) to skip for reaching the same column of next line (scanline stride). This is the image width rounded to the next multiple of 8 (integer amount of bytes).
    • fillValues

      private final FillValues fillValues
      Values to assign to the pixels to mask.
    • emptyTiles

      private transient volatile TilePlaceholder emptyTiles
      A provider of empty tiles. All empty tiles will share the same data buffer for saving memory.
  • Constructor Details

    • MaskedImage

      MaskedImage(RenderedImage source, Shape clip, boolean maskInside, Number[] fillValues)
      Creates a new image with the given source image and clip.
      Parameters:
      source - the image from which to copy pixel values.
      clip - the clip or mask in pixel coordinates.
      maskInside - true for masking pixels inside the shape, or false for masking outside.
      fillValues - values to assign to pixels outside clip area, or null for default values.
  • Method Details

    • getProperty

      public Object getProperty(String key)
      Gets a property from this image.
      Specified by:
      getProperty in interface RenderedImage
      Overrides:
      getProperty in class PlanarImage
      Parameters:
      key - the name of the property to get.
      Returns:
      the property value, or Image.UndefinedProperty if none.
    • getPropertyNames

      public String[] getPropertyNames()
      Returns the names of all recognized properties.
      Specified by:
      getPropertyNames in interface RenderedImage
      Overrides:
      getPropertyNames in class PlanarImage
      Returns:
      names of all recognized properties, or null if none.
    • getMaskTiles

      private Rectangle getMaskTiles()
      Returns the bounds of the clip in tile coordinates. It provides a fast way to determine if a tile intersects the mask.
    • getMask

      private ByteBuffer getMask()
      Returns pixel values of the mask in a multi pixel packed array. After conversion to LongBuffer, index of pixel value at coordinate (x,y) can be obtained as below:

      Pre-conditions

      The getMaskTiles() method must have been invoked at least once before this method.
    • computeTile

      protected Raster computeTile(int tileX, int tileY, WritableRaster tile)
      Invoked when a tile need to be computed or updated. May be invoked concurrently in different threads.
      Specified by:
      computeTile in class ComputedImage
      Parameters:
      tileX - the column index of the tile to compute.
      tileY - the row index of the tile to compute.
      tile - if the tile already exists but needs to be updated, the tile to update. Otherwise null.
      Returns:
      computed tile for the given indices.
    • createEmptyTile

      private Raster createEmptyTile(int xmin, int ymin)
      Returns an empty tile starting at the given pixel coordinates. Each empty tile will share its data buffer with other empty tiles, including from other images using the same sample model.
    • needCreate

      private boolean needCreate(WritableRaster tile, Raster source)
      Returns true if the given tile should be discarded and a new tile created before to write pixel values.
      Parameters:
      tile - the tile to potentially reuse. Can be null.
      source - the corresponding tile from source image.
      Returns:
      true if given tile is null or should not be modified.
    • equals

      public boolean equals(Object object)
      Compares the given object with this image for equality. This method returns true if the given object is non-null, is an instance of the exact same class than this image, has equal sources, clip and fill values.
      Overrides:
      equals in class SourceAlignedImage
      Parameters:
      object - the object to compare with this image.
      Returns:
      true if the given object is an image performing the same clipping than this image.
    • hashCode

      public int hashCode()
      Returns a hash code value for this image.
      Overrides:
      hashCode in class SourceAlignedImage
      Returns:
      a hash code value based on a description of the operation performed by this image.