Package com.jhlabs.image
Class ImageMath
- java.lang.Object
-
- com.jhlabs.image.ImageMath
-
public class ImageMath extends java.lang.Object
A class containing static math methods useful for image processing.
-
-
Field Summary
Fields Modifier and Type Field Description static float
HALF_PI
The value of half pi as a float.static float
PI
The value of pi as a float.static float
QUARTER_PI
The value of quarter pi as a float.static float
TWO_PI
The value of two pi as a float.
-
Constructor Summary
Constructors Constructor Description ImageMath()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static float
bias(float a, float b)
Apply a bias to a number in the unit interval, moving numbers towards 0 or 1 according to the bias parameter.static int
bilinearInterpolate(float x, float y, int nw, int ne, int sw, int se)
Bilinear interpolation of ARGB values.static int
brightnessNTSC(int rgb)
Return the NTSC gray level of an RGB value.static float
circleDown(float x)
A "circle down" function.static float
circleUp(float x)
A "circle up" function.static float
clamp(float x, float a, float b)
Clamp a value to an interval.static int
clamp(int x, int a, int b)
Clamp a value to an interval.static int
colorSpline(float x, int numKnots, int[] knots)
Compute a Catmull-Rom spline for RGB values.static int
colorSpline(int x, int numKnots, int[] xknots, int[] yknots)
Compute a Catmull-Rom spline for RGB values, but with variable knot spacing.static float
gain(float a, float b)
A variant of the gamma function.static float
lerp(float t, float a, float b)
Linear interpolation.static int
lerp(float t, int a, int b)
Linear interpolation.static int
mixColors(float t, int rgb1, int rgb2)
Linear interpolation of ARGB values.static double
mod(double a, double b)
Return a mod b.static float
mod(float a, float b)
Return a mod b.static int
mod(int a, int b)
Return a mod b.static void
premultiply(int[] p, int offset, int length)
Premultiply a block of pixels.static float
pulse(float a, float b, float x)
The pulse function.static void
resample(int[] source, int[] dest, int length, int offset, int stride, float[] out)
An implementation of Fant's resampling algorithm.static float
smoothPulse(float a1, float a2, float b1, float b2, float x)
A smoothed pulse function.static float
smoothStep(float a, float b, float x)
A smoothed step function.static float
spline(float x, int numKnots, float[] knots)
Compute a Catmull-Rom spline.static float
spline(float x, int numKnots, int[] xknots, int[] yknots)
Compute a Catmull-Rom spline, but with variable knot spacing.static float
step(float a, float x)
The step function.static float
triangle(float x)
The triangle function.static void
unpremultiply(int[] p, int offset, int length)
Unpremultiply a block of pixels
-
-
-
Field Detail
-
PI
public static final float PI
The value of pi as a float.- See Also:
- Constant Field Values
-
HALF_PI
public static final float HALF_PI
The value of half pi as a float.- See Also:
- Constant Field Values
-
QUARTER_PI
public static final float QUARTER_PI
The value of quarter pi as a float.- See Also:
- Constant Field Values
-
TWO_PI
public static final float TWO_PI
The value of two pi as a float.- See Also:
- Constant Field Values
-
-
Method Detail
-
bias
public static float bias(float a, float b)
Apply a bias to a number in the unit interval, moving numbers towards 0 or 1 according to the bias parameter.- Parameters:
a
- the number to biasb
- the bias parameter. 0.5 means no change, smaller values bias towards 0, larger towards 1.- Returns:
- the output value
-
gain
public static float gain(float a, float b)
A variant of the gamma function.- Parameters:
a
- the number to apply gain tob
- the gain parameter. 0.5 means no change, smaller values reduce gain, larger values increase gain.- Returns:
- the output value
-
step
public static float step(float a, float x)
The step function. Returns 0 below a threshold, 1 above.- Parameters:
a
- the threshold positionx
- the input parameter- Returns:
- the output value - 0 or 1
-
pulse
public static float pulse(float a, float b, float x)
The pulse function. Returns 1 between two thresholds, 0 outside.- Parameters:
a
- the lower threshold positionb
- the upper threshold positionx
- the input parameter- Returns:
- the output value - 0 or 1
-
smoothPulse
public static float smoothPulse(float a1, float a2, float b1, float b2, float x)
A smoothed pulse function. A cubic function is used to smooth the step between two thresholds.- Parameters:
a1
- the lower threshold position for the start of the pulsea2
- the upper threshold position for the start of the pulseb1
- the lower threshold position for the end of the pulseb2
- the upper threshold position for the end of the pulsex
- the input parameter- Returns:
- the output value
-
smoothStep
public static float smoothStep(float a, float b, float x)
A smoothed step function. A cubic function is used to smooth the step between two thresholds.- Parameters:
a
- the lower threshold positionb
- the upper threshold positionx
- the input parameter- Returns:
- the output value
-
circleUp
public static float circleUp(float x)
A "circle up" function. Returns y on a unit circle given 1-x. Useful for forming bevels.- Parameters:
x
- the input parameter in the range 0..1- Returns:
- the output value
-
circleDown
public static float circleDown(float x)
A "circle down" function. Returns 1-y on a unit circle given x. Useful for forming bevels.- Parameters:
x
- the input parameter in the range 0..1- Returns:
- the output value
-
clamp
public static float clamp(float x, float a, float b)
Clamp a value to an interval.- Parameters:
a
- the lower clamp thresholdb
- the upper clamp thresholdx
- the input parameter- Returns:
- the clamped value
-
clamp
public static int clamp(int x, int a, int b)
Clamp a value to an interval.- Parameters:
a
- the lower clamp thresholdb
- the upper clamp thresholdx
- the input parameter- Returns:
- the clamped value
-
mod
public static double mod(double a, double b)
Return a mod b. This differs from the % operator with respect to negative numbers.- Parameters:
a
- the dividendb
- the divisor- Returns:
- a mod b
-
mod
public static float mod(float a, float b)
Return a mod b. This differs from the % operator with respect to negative numbers.- Parameters:
a
- the dividendb
- the divisor- Returns:
- a mod b
-
mod
public static int mod(int a, int b)
Return a mod b. This differs from the % operator with respect to negative numbers.- Parameters:
a
- the dividendb
- the divisor- Returns:
- a mod b
-
triangle
public static float triangle(float x)
The triangle function. Returns a repeating triangle shape in the range 0..1 with wavelength 1.0- Parameters:
x
- the input parameter- Returns:
- the output value
-
lerp
public static float lerp(float t, float a, float b)
Linear interpolation.- Parameters:
t
- the interpolation parametera
- the lower interpolation rangeb
- the upper interpolation range- Returns:
- the interpolated value
-
lerp
public static int lerp(float t, int a, int b)
Linear interpolation.- Parameters:
t
- the interpolation parametera
- the lower interpolation rangeb
- the upper interpolation range- Returns:
- the interpolated value
-
mixColors
public static int mixColors(float t, int rgb1, int rgb2)
Linear interpolation of ARGB values.- Parameters:
t
- the interpolation parameterrgb1
- the lower interpolation rangergb2
- the upper interpolation range- Returns:
- the interpolated value
-
bilinearInterpolate
public static int bilinearInterpolate(float x, float y, int nw, int ne, int sw, int se)
Bilinear interpolation of ARGB values.- Parameters:
x
- the X interpolation parameter 0..1y
- the y interpolation parameter 0..1nw
- The ARGB northwest valuene
- The ARGB northeast valuesw
- The ARGB southwest valuese
- The ARGB southeast value- Returns:
- the interpolated value
-
brightnessNTSC
public static int brightnessNTSC(int rgb)
Return the NTSC gray level of an RGB value.- Parameters:
rgb
- the input pixel- Returns:
- the gray level (0-255)
-
spline
public static float spline(float x, int numKnots, float[] knots)
Compute a Catmull-Rom spline.- Parameters:
x
- the input parameternumKnots
- the number of knots in the splineknots
- the array of knots- Returns:
- the spline value
-
spline
public static float spline(float x, int numKnots, int[] xknots, int[] yknots)
Compute a Catmull-Rom spline, but with variable knot spacing.- Parameters:
x
- the input parameternumKnots
- the number of knots in the splinexknots
- the array of knot x valuesyknots
- the array of knot y values- Returns:
- the spline value
-
colorSpline
public static int colorSpline(float x, int numKnots, int[] knots)
Compute a Catmull-Rom spline for RGB values.- Parameters:
x
- the input parameternumKnots
- the number of knots in the splineknots
- the array of knots- Returns:
- the spline value
-
colorSpline
public static int colorSpline(int x, int numKnots, int[] xknots, int[] yknots)
Compute a Catmull-Rom spline for RGB values, but with variable knot spacing.- Parameters:
x
- the input parameternumKnots
- the number of knots in the splinexknots
- the array of knot x valuesyknots
- the array of knot y values- Returns:
- the spline value
-
resample
public static void resample(int[] source, int[] dest, int length, int offset, int stride, float[] out)
An implementation of Fant's resampling algorithm.- Parameters:
source
- the source pixelsdest
- the destination pixelslength
- the length of the scanline to resampleoffset
- the start offset into the arraysstride
- the offset between pixels in consecutive rowsout
- an array of output positions for each pixel
-
premultiply
public static void premultiply(int[] p, int offset, int length)
Premultiply a block of pixels.- Parameters:
p
- The block of pixels.offset
- The starting pixel.length
- The number of pixels to premultiply.
-
unpremultiply
public static void unpremultiply(int[] p, int offset, int length)
Unpremultiply a block of pixels- Parameters:
p
- The block of pixels.offset
- The starting pixel.length
- The number of pixels to premultiply.
-
-