Skip to content

Image Filter Functions

Yegor Pelykh edited this page May 26, 2024 · 3 revisions

Image Filter Functions

These functions modify images in place and return that image to make merging functions easier. Image filters can be applied using the static methods of the Filter abstract class.

adjustColor

Filter.adjustColor(opt: AdjustColorOptions): MemoryImage

Adjust the color of the options.src image using various color transformations.

Parameters:

  • image: the MemoryImage to modify.
  • blacks (optional): defines the black level of the image, as a color. Default is undefined.
  • whites (optional): defines the white level of the image, as a color. Default is undefined.
  • mids (optional): defines the mid level of hte image, as a color. Default is undefined.
  • contrast (optional): increases (>1) or decreases (<1) the contrast of the image by pushing colors away/toward neutral gray, where at 0 the image is entirely neutral gray (0 contrast), 1 - the image is not adjusted and >1 the image increases contrast. Default is undefined.
  • saturation (optional): increases (>1) or decreases (<1) the saturation of the image by pushing colors away/toward their grayscale value, where 0 is grayscale and 1 is the original image, and >1 the image becomes more saturated. Default is undefined.
  • brightness (optional): is a constant scalar of the image colors. At 0 the image is black, 1 unmodified, and >1 the image becomes brighter. Default is 1.
  • gamma (optional): an exponential scalar of the image colors. At <1 the image becomes brighter, and >1 the image becomes darker. A gamma of 1/2.2 will convert the image colors to linear color space. Default is 1.
  • exposure (optional): an exponential scalar of the image as rgb* pow(2, exposure). At 0, the image is unmodified; as the exposure increases, the image brightens. Default is 0.
  • hue (optional): shifts the hue component of the image colors in degrees. A hue of 0 will have no affect, and a hue of 45 will shift the hue of all colors by 45 degrees. Default is 0.
  • amount (optional): controls how much affect this filter has on the image, where 0 has no effect and 1 has full effect. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

billboard

Filter.billboard(opt: BillboardOptions): MemoryImage

Apply the billboard filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • grid (optional): specifies the size of the filter cell. Default is 10.
  • amount (optional): sets the strength of the effect. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

bleachBypass

Filter.bleachBypass(opt: BleachBypassOptions): MemoryImage

Apply the bleachBypass filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • amount (optional): sets the strength of the effect. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

bulgeDistortion

Filter.bulgeDistortion(opt: BulgeDistortionOptions): MemoryImage

Apply the bulgeDistortion filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • centerX (optional): sets the X coordinate of the center point of the effect. Default is image.width / 2.
  • centerY (optional): sets the Y coordinate of the center point of the effect. Default is image.height / 2.
  • radius (optional): sets the radius of the effect. Default is min(image.width, image.height) / 2.
  • scale (optional): controls the scaling effect. Default is 0.5.
  • interpolation (optional): the interpolation type that is used to obtain colors in non-integer coordinates. Default is Interpolation.nearest.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

bumpToNormal

Filter.bumpToNormal(opt: BumpToNormalOptions): MemoryImage

Generate a normal map from a height-field bump image. The red channel of the image is used as an input, 0 represents a low height and 1 a high value.

Parameters:

  • image: the MemoryImage to modify.
  • strength (optional): sets the strength of the normal image. Default is 2.

Returns the modified MemoryImage.

chromaticAberration

Filter.chromaticAberration(opt: ChromaticAberrationOptions): MemoryImage

Apply chromatic aberration filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • shift (optional): controls the amount of shifting from the start points. Default is 5.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

colorHalftone

Filter.colorHalftone(opt: ColorHalftone): MemoryImage

Apply color halftone filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • amount (optional): controls the strength of the effect. Default is 1.
  • centerX (optional): sets the X coordinate of the center point of the effect. Default is image.width / 2.
  • centerY (optional): sets the Y coordinate of the center point of the effect. Default is image.height / 2.
  • angle (optional): specifies the angle of the direction the effect spreads. Default is 1 (degree).
  • size (optional): specifies the size of the effect cell. Default is 5.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

colorOffset

Filter.colorOffset(opt: ColorOffsetOptions): MemoryImage

Add the red, green, blue and alpha values to the image image colors, a per-channel brightness.

Parameters:

  • image: the MemoryImage to modify.
  • red (optional): the amount of color shift in the red channel. Default is 0.
  • green (optional): the amount of color shift in the green channel. Default is 0.
  • blue (optional): the amount of color shift in the blue channel. Default is 0.
  • alpha (optional): the amount of color shift in the alpha channel. Default is 0.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

contrast

Filter.contrast(opt: ContrastOptions): MemoryImage

Set the contrast level for the image.

Parameters:

  • image: the MemoryImage to modify.
  • contrast: value below 100 will decrees the contrast of the image, and values above 100 will increase the contrast. A contrast of 100 will have no affect.
  • mode (optional): contrast mode of type ContrastMode: proportional or scurve. Default is ContrastMode.proportional.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

convolution

Filter.convolution(opt: ConvolutionOptions): MemoryImage

Apply a 3x3 convolution filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • filter: convolution filter coefficients. Should be an array of 9 numbers.
  • div (optional): the coefficient by which each value will be divided. Default is 1.
  • offset (optional): the offset that will be added to each result. Default is 0.
  • amount (optional): sets the strength of the effect. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

copyImageChannels

Filter.copyImageChannels(opt: CopyImageChannelsOptions): MemoryImage

Copy channels from the from image to the image. If scaled is true, then the from image will be scaled to the image resolution.

Parameters:

  • image: the MemoryImage to modify.
  • from: the MemoryImage to copy from.
  • scaled (optional): if is true, then the from image will be scaled to the image resolution. Default is false.
  • red (optional): specifies the channel from the from image to be copied into the red channel of the image. Default is undefined (don't copy anything to the red channel).
  • green (optional): specifies the channel from the from image to be copied into the green channel of the image. Default is undefined (don't copy anything to the green channel).
  • blue (optional): specifies the channel from the from image to be copied into the blue channel of the image. Default is undefined (don't copy anything to the blue channel).
  • alpha (optional): specifies the channel from the from image to be copied into the alpha channel of the image. Default is undefined (don't copy anything to the alpha channel).
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

ditherImage

Filter.ditherImage(opt: DitherImageOptions): MemoryImage

Dither an image to reduce banding patterns when reducing the number of colors.

Parameters:

  • image: the MemoryImage to modify.
  • quantizer (optional): specifies the quantizer to be used for dithering. Default is the new NeuralQuantizer instance.
  • kernel (optional): the dithering kernel type to reduce banding patterns when reducing the number of colors. Default is DitherKernel.floydSteinberg.
  • serpentine (optional): specifies whether the "serpentine scanning" mode should be enabled (when the horizontal direction of scan alternates between lines). Default is false.

Returns the modified MemoryImage.

dotScreen

Filter.dotScreen(opt: DotScreenOptions): MemoryImage

Apply the dot screen filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • angle (optional): specifies the angle of the direction the effect spreads. Default is 180 (degrees).
  • size (optional): specifies the size of the effect cell. Default is 5.75.
  • centerX (optional): sets the X coordinate of the center point of the effect. Default is image.width / 2.
  • centerY (optional): sets the Y coordinate of the center point of the effect. Default is image.height / 2.
  • amount (optional): controls the strength of the effect. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

dropShadow

Filter.dropShadow(opt: DropShadowOptions): MemoryImage

Create a drop-shadow effect for the image.

Parameters:

  • image: the MemoryImage to modify.
  • hShadow: the horizontal shift of the shadow.
  • vShadow: the vertical shift of the shadow.
  • blur: the amount of shadow blur. Should be >= 0;
  • shadowColor (optional): the shadow color. Default is ColorRgba8(0, 0, 0, 128).

Returns the modified MemoryImage.

edgeGlow

Filter.edgeGlow(opt: EdgeGlowOptions): MemoryImage

Apply the edge glow filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • amount (optional): controls the strength of the effect. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

emboss

Filter.emboss(opt: EmbossOptions): MemoryImage

Apply an emboss convolution filter.

Parameters:

  • image: the MemoryImage to modify.
  • amount (optional): controls the strength of the effect. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

gamma

Filter.gamma(opt: GammaOptions): MemoryImage

Apply gamma scaling to the image.

Parameters:

  • image: the MemoryImage to modify.
  • gamma: the gamma scaling factor.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

gaussianBlur

Filter.gaussianBlur(opt: GaussianBlurOptions): MemoryImage

Apply gaussian blur to the image.

Parameters:

  • image: the MemoryImage to modify.
  • radius: determines how many pixels away from the current pixel should contribute to the blur, where 0 is no blur and the larger the radius, the stronger the blur.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

grayscale

Filter.grayscale(opt: GrayscaleOptions): MemoryImage

Convert the image to grayscale.

Parameters:

  • image: the MemoryImage to modify.
  • amount (optional): controls the strength of the effect. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

hdrToLdr

Filter.hdrToLdr(opt: HdrToLdrOptions): MemoryImage

Convert a high dynamic range image to a low dynamic range image, with optional exposure control.

Parameters:

  • image: the MemoryImage to modify.
  • exposure (optional): the exposure value. Default is undefined.

Returns the modified MemoryImage.

hexagonPixelate

Filter.hexagonPixelate(opt: HexagonPixelateOptions): MemoryImage

Apply the hexagon pixelation filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • centerX (optional): sets the X coordinate of the center point of the effect. Default is image.width / 2.
  • centerY (optional): sets the Y coordinate of the center point of the effect. Default is image.height / 2.
  • size (optional): specifies the size of the effect cell. Default is 5.
  • amount (optional): controls the strength of the effect. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

invert

Filter.invert(opt: InvertOptions): MemoryImage

Invert the colors of the image.

Parameters:

  • image: the MemoryImage to modify.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

luminanceThreshold

Filter.luminanceThreshold(opt: LuminanceThresholdOptions): MemoryImage

Invert the colors of the image.

Parameters:

  • image: the MemoryImage to modify.
  • threshold (optional): the threshold factor of the effect. Default is 0.5.
  • outputColor (optional): specifies whether to consider the current colors when applying the filter. Default is false.
  • amount (optional): controls the strength of the effect. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

monochrome

Filter.monochrome(opt: MonochromeOptions): MemoryImage

Apply the monochrome filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • color (optional): specifies the color for the monochrome colorization of the image. Default is undefined (using shades of gray).
  • amount (optional): controls the strength of the effect, in the range [0, 1]. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

noise

Filter.noise(opt: NoiseOptions): MemoryImage

Add random noise to pixel values.

Parameters:

  • image: the MemoryImage to modify.
  • sigma: how strong the effect should be.
  • type (optional): the noise type. Default is NoiseType.gaussian.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

normalize

Filter.normalize(opt: NormalizeOptions): MemoryImage

Linearly normalize the colors of the image. All color values will be mapped to the range min, max inclusive.

Parameters:

  • image: the MemoryImage to modify.
  • min: min value of the range of normalizing.
  • max: max value of the range of normalizing.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

pixelate

Filter.pixelate(opt: PixelateOptions): MemoryImage

Pixelate the image.

Parameters:

  • image: the MemoryImage to modify.
  • size: the size of the pixelated blocks.
  • mode (optional): if is PixelateMode.upperLeft then the upper-left corner of the block will be used for the block color, if mode is PixelateMode.average, the average of all the pixels in the block will be used for the block color. Default is PixelateMode.upperLeft.
  • amount (optional): controls the strength of the effect. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

quantize

Filter.quantize(opt: QuantizeOptions): MemoryImage

Quantize the number of colors in image to the given amount.

Parameters:

  • image: the MemoryImage to modify.
  • numberOfColors (optional): the number of colors. Default is 256.
  • method (optional): quantization method. Default is QuantizeMethod.neuralNet.
  • dither (optional): the dithering kernel type for quantization. Default is DitherKernel.none.
  • ditherSerpentine (optional): specifies whether the "serpentine scanning" mode should be enabled (when the horizontal direction of scan alternates between lines). Default is false.

Returns the modified MemoryImage.

reinhardToneMap

Filter.reinhardToneMap(opt: ReinhardToneMapOptions): MemoryImage

Applies Reinhard tone mapping to the hdr image.

Parameters:

  • image: the MemoryImage to modify.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

remapColors

Filter.remapColors(opt: RemapColorsOptions): MemoryImage

Remap the color channels of the image. red, green, blue and alpha should be set to one of the following: Channel.red, Channel.green, Channel.blue, Channel.alpha, or Channel.luminance.

Example for swapping the red and green channels of the image:

Filter.remapColors({
    image: image,
    red: Channel.green,
    green: Channel.red,
});

Example for setting the alpha channel to the luminance (grayscale) of the image:

Filter.remapColors({
    image: image,
    alpha: Channel.luminance,
});

Parameters:

  • image: the MemoryImage to modify.
  • red (optional): specifies the channel to be copied into the red channel of the image. Default is undefined (don't copy anything to the red channel).
  • green (optional): specifies the channel to be copied into the green channel of the image. Default is undefined (don't copy anything to the green channel).
  • blue (optional): specifies the channel to be copied into the blue channel of the image. Default is undefined (don't copy anything to the blue channel).
  • alpha (optional): specifies the channel to be copied into the alpha channel of the image. Default is undefined (don't copy anything to the alpha channel).

Returns the modified MemoryImage.

scaleRgba

Filter.scaleRgba(opt: ScaleRgbaOptions): MemoryImage

Scales the image channels according to the ratios specified in the scale color.

Parameters:

  • image: the MemoryImage to modify.
  • scale: a color whose each channel is a scaling factor to be applied to the corresponding channel ot the image.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

separableConvolution

Filter.separableConvolution(opt: SeparableConvolutionOptions): MemoryImage

Apply a generic separable convolution filter on the image, using the given kernel. ImageFilter.gaussianBlur() is an example of such a filter.

Parameters:

  • image: the MemoryImage to modify.
  • kernel: the separable kernel instance.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

sepia

Filter.sepia(opt: SepiaOptions): MemoryImage

Apply sepia tone to the image.

Parameters:

  • image: the MemoryImage to modify.
  • amount (optional): controls the strength of the effect, in the range [0, 1]. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

sketch

Filter.sketch(opt: SketchOptions): MemoryImage

Apply sketch filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • amount (optional): controls the strength of the effect, in the range [0, 1]. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

smooth

Filter.smooth(opt: SmoothOptions): MemoryImage

Apply a smoothing convolution filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • weight: the weight of the current pixel being filtered. If it's greater than 1, it will make the image sharper.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

sobel

Filter.sobel(opt: SobelOptions): MemoryImage

Apply Sobel edge detection filtering to the image.

Parameters:

  • image: the MemoryImage to modify.
  • amount (optional): controls the strength of the effect, in the range [0, 1]. Default is 1.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

solarize

Filter.solarize(opt: SolarizeOptions): MemoryImage

Solarize the colors of the image.

Parameters:

  • image: the MemoryImage to modify.
  • threshold: solarization threshold. Should be an integer value from 1 to 254.
  • mode (optional): sets solarization mode (SolarizeMode.highlights - bright objects become black, SolarizeMode.shadows - solarize shadows). Default is SolarizeMode.highlights.

Returns the modified MemoryImage.

stretchDistortion

Filter.stretchDistortion(opt: StretchDistortionOptions): MemoryImage

Apply stretch distortion filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • centerX (optional): sets the X coordinate of the center point of the effect. Default is image.width / 2.
  • centerY (optional): sets the Y coordinate of the center point of the effect. Default is image.height / 2.
  • interpolation (optional): the interpolation type that is used to obtain colors in non-integer coordinates. Default is Interpolation.nearest.
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.

vignette

Filter.vignette(opt: VignetteOptions): MemoryImage

Apply a vignette filter to the image.

Parameters:

  • image: the MemoryImage to modify.
  • start (optional): the inner radius from image center where fading starts. Default is 0.3.
  • end (optional): the outer radius of the vignette effect where the color is fully applied. Default is 0.85.
  • amount (optional): controls the blend of the effect with the original image. Default is 0.9.
  • color (optional): the color that the image fades to at the farthest point from the center. Default is ColorRgba8(0, 0, 0, 1) (black).
  • mask (optional): sets a mask of type MemoryImage that specifies which area the filter will apply to. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when applying the filter. Default is Channel.luminance.

Returns the modified MemoryImage.