Documentation
¶
Overview ¶
Package filter applies the filters of resvg that a caller can use alone: the Gaussian blur of feGaussianBlur, the four kinds of feColorMatrix and the five transfer functions of feComponentTransfer. They work on an okage.Mask, an okage.Pixmap and an image.NRGBA (D88).
The functions run the code that svg runs for these filter primitives, so they give the same bytes as resvg. svg and lottie call this package for their blurs and their colors.
No function returns an error. A value out of range is clamped, as in resvg.
Index ¶
- func ApplyColorMatrix(p *okage.Pixmap, m ColorMatrix, opts ...Option)
- func ApplyColorMatrixNRGBA(img *image.NRGBA, m ColorMatrix, opts ...Option)
- func ApplyComponentTransfer(p *okage.Pixmap, r, g, b, a TransferFunction, opts ...Option)
- func ApplyComponentTransferNRGBA(img *image.NRGBA, r, g, b, a TransferFunction, opts ...Option)
- func Blur(p *okage.Pixmap, sigmaX, sigmaY float64, opts ...Option)
- func BlurMask(m *okage.Mask, sigmaX, sigmaY float64, opts ...Option)
- type ColorMatrix
- type Option
- type TransferFunction
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyColorMatrix ¶
func ApplyColorMatrix(p *okage.Pixmap, m ColorMatrix, opts ...Option)
ApplyColorMatrix demultiplies the pixels of p, applies m, and multiplies the pixels again, in place, as resvg does. Each channel of the result is cut to 0 and 1, and truncated to 8 bits.
func ApplyColorMatrixNRGBA ¶
func ApplyColorMatrixNRGBA(img *image.NRGBA, m ColorMatrix, opts ...Option)
ApplyColorMatrixNRGBA applies m to the pixels of img, which are not premultiplied, in place. It gives the bytes of ApplyColorMatrix without its round trip through premultiplied pixels, which loses precision where the alpha is low.
Example ¶
This example makes a picture gray and then tints it with two colors, as a duotone does. Each row of a matrix gives a channel from the channels R, G, B and A, from 0 to 1, and a constant.
package main
import (
"fmt"
"image"
"image/color"
"github.com/xo/okage/filter"
)
func main() {
img := image.NewNRGBA(image.Rect(0, 0, 2, 1))
img.SetNRGBA(0, 0, color.NRGBA{R: 255, G: 128, B: 0, A: 255})
img.SetNRGBA(1, 0, color.NRGBA{R: 30, G: 60, B: 200, A: 128})
// The luminance with the weights of ivy, in each channel.
gray := filter.NewColorMatrix([20]float32{
0.299, 0.587, 0.114, 0, 0,
0.299, 0.587, 0.114, 0, 0,
0.299, 0.587, 0.114, 0, 0,
0, 0, 0, 1, 0,
})
filter.ApplyColorMatrixNRGBA(img, gray)
fmt.Printf("%v\n", img.Pix)
// A duotone from dark blue (0, 0, 0.4) to cream (1, 0.95, 0.8) by the
// gray in the channel R.
duotone := filter.NewColorMatrix([20]float32{
1, 0, 0, 0, 0,
0.95, 0, 0, 0, 0,
0.4, 0, 0, 0, 0.4,
0, 0, 0, 1, 0,
})
filter.ApplyColorMatrixNRGBA(img, duotone)
fmt.Printf("%v\n", img.Pix)
}
Output: [151 151 151 255 66 66 66 128] [151 143 162 255 66 62 128 128]
func ApplyComponentTransfer ¶
func ApplyComponentTransfer(p *okage.Pixmap, r, g, b, a TransferFunction, opts ...Option)
ApplyComponentTransfer demultiplies the pixels of p, applies the functions r, g, b and a to their channels, and multiplies the pixels again, in place, as resvg does. Each channel of the result is cut to 0 and 1, and truncated to 8 bits.
func ApplyComponentTransferNRGBA ¶
func ApplyComponentTransferNRGBA(img *image.NRGBA, r, g, b, a TransferFunction, opts ...Option)
ApplyComponentTransferNRGBA applies the functions r, g, b and a to the channels of the pixels of img, which are not premultiplied, in place.
Example ¶
This example changes the contrast and the brightness of a picture with a linear transfer function, and then makes it black and white. A discrete function of 256 values maps each byte of a channel to its own value, so it can put the threshold of the black and white at any byte.
package main
import (
"fmt"
"image"
"github.com/xo/okage/filter"
)
func main() {
img := image.NewNRGBA(image.Rect(0, 0, 3, 1))
copy(img.Pix, []byte{40, 40, 40, 255, 100, 100, 100, 255, 200, 200, 200, 255})
// A contrast factor of 1.5 around the middle gray, and a brightness of
// 0.1: 1.5 * (c - 0.5) + 0.5 + 0.1.
lum := filter.TransferLinear(1.5, -0.15)
filter.ApplyComponentTransferNRGBA(img, lum, lum, lum, filter.TransferFunction{})
fmt.Printf("%v\n", img.Pix)
// White from the byte 120 up.
values := make([]float32, 256)
for i := 120; i < 256; i++ {
values[i] = 1
}
biLevel := filter.TransferDiscrete(values)
filter.ApplyComponentTransferNRGBA(img, biLevel, biLevel, biLevel, filter.TransferFunction{})
fmt.Printf("%v\n", img.Pix)
}
Output: [21 21 21 255 111 111 111 255 255 255 255 255] [0 0 0 255 0 0 0 255 255 255 255 255]
func Blur ¶
Blur blurs the premultiplied pixels of p in place, as feGaussianBlur of resvg does after resolve_std_dev. sigmaX and sigmaY are the standard deviations in pixels, and the function rounds them to float32, as resvg holds them.
A sigma below 0.05, a negative sigma or a NaN does not blur on its axis. A sigma of 2 or more on either axis takes the box blur of resvg, which runs five box blurs on each axis. A smaller one takes the IIR blur of resvg, a recursive filter that runs along each row and each column. The box blur reads 0 outside the pixmap, and the IIR blur reflects the pixmap at its edges.
The box blur blurs only the smallest box that holds each pixel that is not 0, grown by the sizes of the boxes and cut to the pixmap. This gives the same bytes as a blur of the whole pixmap. The IIR blur blurs the whole pixmap.
With WithLinearRGB, Blur converts the colors to linearRGB before the blur and back to sRGB after it, as resvg does.
Example ¶
This example blurs a pixmap with a small standard deviation, which takes the IIR blur of resvg.
package main
import (
"fmt"
"github.com/xo/okage"
"github.com/xo/okage/filter"
)
func main() {
pixmap, _ := okage.NewPixmap(30, 30)
paint := okage.DefaultPaint()
paint.SetColorRGBA8(200, 50, 50, 255)
square, _ := okage.RectFromXYWH(10, 10, 10, 10)
pixmap.FillRect(square, &paint, okage.IdentityTransform(), nil)
filter.Blur(pixmap, 1.5, 1.5)
for _, x := range []uint32{8, 10, 15} {
c, _ := pixmap.Pixel(x, 15)
fmt.Println(c.Red(), c.Green(), c.Blue(), c.Alpha())
}
}
Output: 26 6 6 34 131 32 32 168 197 49 49 252
func BlurMask ¶
BlurMask blurs the values of m in place, as Blur blurs the alpha of a pixmap. Each value of the result is the alpha of the same pixel after Blur on a pixmap whose alpha is m, with the same sigmas and options. WithLinearRGB changes no alpha, so BlurMask ignores it.
BlurMask takes the bytes of the mask with DataMut, so the mask holds all its pixels after the blur. A mask of okage.NewRectMask turns into pixels in the same way.
Example ¶
This example draws a soft shadow: it fills the silhouette of a shape into a mask, blurs the mask, and fills a pixmap with a color through the mask.
package main
import (
"fmt"
"github.com/xo/okage"
"github.com/xo/okage/filter"
)
func main() {
rect, _ := okage.RectFromXYWH(20, 20, 40, 30)
shape := okage.PathFromRect(rect)
mask, _ := okage.NewMask(80, 70)
mask.FillPath(shape, okage.FillRuleWinding, true, okage.IdentityTransform())
// A standard deviation of 4 pixels takes the box blur of resvg.
filter.BlurMask(mask, 4, 4)
pixmap, _ := okage.NewPixmap(80, 70)
paint := okage.DefaultPaint()
paint.SetColorRGBA8(0, 0, 0, 128)
all, _ := okage.RectFromXYWH(0, 0, 80, 70)
pixmap.FillRect(all, &paint, okage.IdentityTransform(), mask)
row := mask.Data()[35*80:]
fmt.Println(row[10], row[16], row[20], row[24], row[40])
}
Output: 4 57 139 214 255
Types ¶
type ColorMatrix ¶
type ColorMatrix struct {
// contains filtered or unexported fields
}
ColorMatrix is one of the four kinds of feColorMatrix of resvg, the enum ColorMatrixKind of usvg. The zero value changes no color: ApplyColorMatrix then only demultiplies and multiplies the pixels again, as resvg does for each color filter.
func HueRotateMatrix ¶
func HueRotateMatrix(degrees float32) ColorMatrix
HueRotateMatrix returns the matrix of type="hueRotate", which turns the hue of the colors by degrees. The matrix keeps the alpha.
func LuminanceToAlphaMatrix ¶
func LuminanceToAlphaMatrix() ColorMatrix
LuminanceToAlphaMatrix returns the matrix of type="luminanceToAlpha", which sets the alpha to the luminance of the color and the color to black.
func NewColorMatrix ¶
func NewColorMatrix(m [20]float32) ColorMatrix
NewColorMatrix returns the matrix m of 4 rows of 5 numbers, as type="matrix" gives it, row by row. Each row gives a channel of R, G, B and A, from the channels R, G, B and A from 0 to 1, and a constant.
func SaturateMatrix ¶
func SaturateMatrix(v float32) ColorMatrix
SaturateMatrix returns the matrix of type="saturate" for the value v. 0 makes the colors gray, and 1 changes nothing. A value below 0 is 0. The matrix keeps the alpha.
type Option ¶
type Option func(*options)
Option configures one call of a function of this package (D20).
func WithBoxBlur ¶
func WithBoxBlur() Option
WithBoxBlur makes Blur and BlurMask take the box blur for each standard deviation, as lottie does for the blurs of ThorVG (D79). Without it, a standard deviation below 2 on both axes takes the IIR blur, as resvg does. resvg has no such field.
func WithLinearRGB ¶
func WithLinearRGB() Option
WithLinearRGB makes Blur and the color functions work in linearRGB, as the attribute color-interpolation-filters="linearRGB" of resvg does. The function converts the colors from sRGB to linearRGB, applies its filter, and converts the colors back to sRGB. Without it, the functions work in sRGB.
type TransferFunction ¶
type TransferFunction struct {
// contains filtered or unexported fields
}
TransferFunction is one of the five transfer functions of feComponentTransfer of resvg, the enum TransferFunction of usvg. It maps one channel of a color, from 0 to 1. The zero value is the identity.
func TransferDiscrete ¶
func TransferDiscrete(values []float32) TransferFunction
TransferDiscrete returns the function of type="discrete". It splits the range from 0 to 1 into as many equal steps as values, and maps each step to its value. No value gives the identity. The function holds a copy of values.
func TransferGamma ¶
func TransferGamma(amplitude, exponent, offset float32) TransferFunction
TransferGamma returns the function of type="gamma": amplitude * pow(c, exponent) + offset.
func TransferLinear ¶
func TransferLinear(slope, intercept float32) TransferFunction
TransferLinear returns the function of type="linear": slope * c + intercept.
func TransferTable ¶
func TransferTable(values []float32) TransferFunction
TransferTable returns the function of type="table". It maps the channel along the straight lines between the values, which split the range from 0 to 1 in equal parts. No value gives the identity. The function holds a copy of values.