Class AbstractImageFilter

  • All Implemented Interfaces:
    java.awt.image.BufferedImageOp
    Direct Known Subclasses:
    AbstractImagewiseFilter, AbstractPixelwiseFilter, AbstractRowwiseFilter, BlurFilter, GlowFilter, MarginFilter, PixelArtUpscalingFilter, StrokeFilter, TrimFilter, TurnAndFlipFilter

    public abstract class AbstractImageFilter
    extends java.lang.Object
    implements java.awt.image.BufferedImageOp
    An abstract base class for filtering images. While BufferedImageOps are generally designed to operate on images that use any storage format or colour model, AbstractImageFilter is optimized to work with 32-bit ARGB image data (BufferedImage.TYPE_INT_ARGB). Images stored in other formats will typically be converted to and from ARGB data as necessary. This greatly simplifies the task of writing an image filter, though at the cost of conversion (when required).

    This base class provides default implementations for many methods of BufferedImageOp. These default implementations are suitable for any operation that does not change the shape or size of the image. Most concrete filters will not subclass this base class directly, but will instead subclass one of AbstractPixelwiseFilter, AbstractRowwiseFilter, or AbstractImagewiseFilter.

    Performance note: As a side effect of automatic conversion, image types other than TYPE_INT_ARGB and TYPE_INT_RGB will become unmanaged images as a result of the filtering process. Unmanaged images cannot be hardware accelerated, and thus are typically much slower to draw. The best way to maximize performance is to ensure that all images are in one of the optimal formats. If you are unsure about a particular image, use ImageUtilities#ensureIntRGBFormat(java.awt.image.BufferedImage).

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static int clamp​(float pixelByte)
      Clamps an input pixel value to a maximum of 255, converting it from float to int in the process.
      static int clamp​(int pixelByte)
      Clamps an input pixel value to a maximum of 255.
      static int clamp​(int value, int low, int high)
      Clamps an integer value to the range low..high inclusive.
      static int clampBoth​(int pixelByte)
      Clamps an input pixel value to the range 0..255.
      java.awt.image.BufferedImage createCompatibleDestImage​(java.awt.image.BufferedImage source, java.awt.image.ColorModel destinationColorModel)
      static float fA​(int argb)
      Returns the alpha component of a packed ARGB pixel as a float value between 0 and 1.
      static float fB​(int argb)
      Returns the blue component of a packed ARGB pixel as a float value between 0 and 1.
      static float fG​(int argb)
      Returns the green component of a packed ARGB pixel as a float value between 0 and 1.
      static float fR​(int argb)
      Returns the red component of a packed ARGB pixel as a float value between 0 and 1.
      static int[] getARGB​(java.awt.image.BufferedImage image, int[] pixels)
      Stores the ARGB pixel data for an entire image in an array.
      static int[] getARGB​(java.awt.image.BufferedImage image, int x, int y, int width, int height, int[] pixels)
      Fetches pixel data from an image in ARGB format.
      protected static int[] getARGBSynch​(java.awt.image.BufferedImage image, int x, int y, int width, int height, int[] pixels)
      Fetches pixel data from an image in ARGB format.
      java.awt.geom.Rectangle2D getBounds2D​(java.awt.image.BufferedImage source)
      java.awt.geom.Point2D getPoint2D​(java.awt.geom.Point2D sourcePoint, java.awt.geom.Point2D destPoint)
      java.awt.RenderingHints getRenderingHints()
      static int iA​(float a)
      Converts a floating point alpha value between 0 and 1 to a packed ARGB alpha value.
      static int iB​(float b)
      Converts a floating point blue value between 0 and 1 to a packed ARGB blue value.
      static int iG​(float g)
      Converts a floating point green value between 0 and 1 to a packed ARGB green value.
      static int iR​(float r)
      Converts a floating point red value between 0 and 1 to a packed ARGB red value.
      static void setARGB​(java.awt.image.BufferedImage image, int[] pixels)
      Replaces the data in an image with pixel values from an array.
      static void setARGB​(java.awt.image.BufferedImage image, int x, int y, int width, int height, int[] pixels)
      Stores ARGB pixel data in an image.
      protected static void setARGBSynch​(java.awt.image.BufferedImage image, int x, int y, int width, int height, int[] pixels)
      Stores ARGB pixel data in an image.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.awt.image.BufferedImageOp

        filter
    • Constructor Detail

      • AbstractImageFilter

        public AbstractImageFilter()
    • Method Detail

      • createCompatibleDestImage

        public java.awt.image.BufferedImage createCompatibleDestImage​(java.awt.image.BufferedImage source,
                                                                      java.awt.image.ColorModel destinationColorModel)
        Specified by:
        createCompatibleDestImage in interface java.awt.image.BufferedImageOp
      • getBounds2D

        public java.awt.geom.Rectangle2D getBounds2D​(java.awt.image.BufferedImage source)

        The base implementation returns the bounds of the image in image space; that is, the rectangle defined by 0, 0, source.getWidth(), source.getHeight().

        Specified by:
        getBounds2D in interface java.awt.image.BufferedImageOp
      • getPoint2D

        public java.awt.geom.Point2D getPoint2D​(java.awt.geom.Point2D sourcePoint,
                                                java.awt.geom.Point2D destPoint)

        The base implementation returns a copy of the original point.

        Specified by:
        getPoint2D in interface java.awt.image.BufferedImageOp
      • getRenderingHints

        public java.awt.RenderingHints getRenderingHints()

        The base implementation returns null.

        Specified by:
        getRenderingHints in interface java.awt.image.BufferedImageOp
      • clampBoth

        public static int clampBoth​(int pixelByte)
        Clamps an input pixel value to the range 0..255.
        Parameters:
        pixelByte - the pixel value
        Returns:
        max(0,min(pixelByte,255))
      • clamp

        public static int clamp​(int pixelByte)
        Clamps an input pixel value to a maximum of 255.
        Parameters:
        pixelByte - the pixel value
        Returns:
        min(pixelByte,255)
      • clamp

        public static int clamp​(int value,
                                int low,
                                int high)
        Clamps an integer value to the range low..high inclusive.
        Parameters:
        value - the value to clamp
        low - the minimum value
        high - the maximum value
        Returns:
        the clamped value
      • clamp

        public static int clamp​(float pixelByte)
        Clamps an input pixel value to a maximum of 255, converting it from float to int in the process.
        Parameters:
        pixelByte - the pixel value
        Returns:
        min( (int) (pixelByte+0.5f),255)
      • fA

        public static float fA​(int argb)
        Returns the alpha component of a packed ARGB pixel as a float value between 0 and 1. This value is equivalent to the following calculation, (though possibly more efficient):
        (float) ((argb >> 24) & 0xff) / 255f
        Parameters:
        argb - the 32-bit pixel value
        Returns:
        the alpha component as a unit value
      • fR

        public static float fR​(int argb)
        Returns the red component of a packed ARGB pixel as a float value between 0 and 1. This value is equivalent to the following calculation, (though possibly more efficient):
        (float) ((argb >> 16) & 0xff) / 255f
        Parameters:
        argb - the 32-bit pixel value
        Returns:
        the red component as a unit value
      • fG

        public static float fG​(int argb)
        Returns the green component of a packed ARGB pixel as a float value between 0 and 1. This value is equivalent to the following calculation, (though possibly more efficient):
        (float) ((argb >> 8) & 0xff) / 255f
        Parameters:
        argb - the 32-bit pixel value
        Returns:
        the green component as a unit value
      • fB

        public static float fB​(int argb)
        Returns the blue component of a packed ARGB pixel as a float value between 0 and 1. This value is equivalent to the following calculation, (though possibly more efficient):
        (float) (argb & 0xff) / 255f
        Parameters:
        argb - the 32-bit pixel value
        Returns:
        the blue component as a unit value
      • iA

        public static int iA​(float a)
        Converts a floating point alpha value between 0 and 1 to a packed ARGB alpha value. (The value is clamped to the range 0..255 and shifted left 24 bits.)
        Parameters:
        a - the alpha component from 0 to 1
        Returns:
        a packed ARGB alpha value
      • iR

        public static int iR​(float r)
        Converts a floating point red value between 0 and 1 to a packed ARGB red value. (The value is clamped to the range 0..255 and shifted left 16 bits.)
        Parameters:
        r - the red component from 0 to 1
        Returns:
        a packed ARGB red value
      • iG

        public static int iG​(float g)
        Converts a floating point green value between 0 and 1 to a packed ARGB green value. (The value is clamped to the range 0..255 and shifted left 8 bits.)
        Parameters:
        g - the green component from 0 to 1
        Returns:
        a packed ARGB green value
      • iB

        public static int iB​(float b)
        Converts a floating point blue value between 0 and 1 to a packed ARGB blue value. (The value is clamped to the range 0..255.)
        Parameters:
        b - the blue component from 0 to 1
        Returns:
        a packed ARGB blue value
      • getARGB

        public static int[] getARGB​(java.awt.image.BufferedImage image,
                                    int x,
                                    int y,
                                    int width,
                                    int height,
                                    int[] pixels)
        Fetches pixel data from an image in ARGB format. The data are converted from the source format, if required.
        Parameters:
        image - the image to get pixels from
        x - the x-offset of the subimage
        y - the y-offset of the subimage
        width - the width of the subimage
        height - the height of the subimage
        pixels - an array to use; may be null
        Returns:
        an array of pixel data for the subimage
      • getARGBSynch

        protected static int[] getARGBSynch​(java.awt.image.BufferedImage image,
                                            int x,
                                            int y,
                                            int width,
                                            int height,
                                            int[] pixels)
        Fetches pixel data from an image in ARGB format. The data are converted from the source format, if required. This method does not perform parallel processing.
        Parameters:
        image - the image to get pixels from
        x - the x-offset of the subimage
        y - the y-offset of the subimage
        width - the width of the subimage
        height - the height of the subimage
        pixels - an array to use; may be null
        Returns:
        an array of pixel data for the subimage
      • setARGB

        public static void setARGB​(java.awt.image.BufferedImage image,
                                   int x,
                                   int y,
                                   int width,
                                   int height,
                                   int[] pixels)
        Stores ARGB pixel data in an image. The data are converted into the destination format, if required. This method may perform parallel processing.
        Parameters:
        image - the image to set pixels on
        x - the x-offset of the subimage
        y - the y-offset of the subimage
        width - the width of the subimage
        height - the height of the subimage
        pixels - an array to use
      • setARGBSynch

        protected static void setARGBSynch​(java.awt.image.BufferedImage image,
                                           int x,
                                           int y,
                                           int width,
                                           int height,
                                           int[] pixels)
        Stores ARGB pixel data in an image. The data are converted into the destination format, if required. This method does not perform parallel processing.
        Parameters:
        image - the image to set pixels on
        x - the x-offset of the subimage
        y - the y-offset of the subimage
        width - the width of the subimage
        height - the height of the subimage
        pixels - an array to use
      • getARGB

        public static int[] getARGB​(java.awt.image.BufferedImage image,
                                    int[] pixels)
        Stores the ARGB pixel data for an entire image in an array. The data are converted from the source format, if required. This method may perform the copying in parallel depending on image size.
        Parameters:
        image - the image to obtain a pixel array for
        pixels - the array to store the data in (may be null)
      • setARGB

        public static void setARGB​(java.awt.image.BufferedImage image,
                                   int[] pixels)
        Replaces the data in an image with pixel values from an array. The data are converted into the destination format, if required. This method may perform the copying in parallel depending on image size.
        Parameters:
        image - the image to set pixels on
        pixels - an array of source pixels