* chore(deps): bump github.com/kovidgoyal/imaging from 1.8.7 to 1.8.19 Bumps [github.com/kovidgoyal/imaging](https://github.com/kovidgoyal/imaging) from 1.8.7 to 1.8.19. - [Release notes](https://github.com/kovidgoyal/imaging/releases) - [Commits](https://github.com/kovidgoyal/imaging/compare/v1.8.7...v1.8.19) --- updated-dependencies: - dependency-name: github.com/kovidgoyal/imaging dependency-version: 1.8.19 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps): bump golang.org/x/net from 0.48.0 to 0.49.0 Bumps [golang.org/x/net](https://github.com/golang/net) from 0.48.0 to 0.49.0. - [Commits](https://github.com/golang/net/compare/v0.48.0...v0.49.0) --- updated-dependencies: - dependency-name: golang.org/x/net dependency-version: 0.49.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> # Conflicts: # go.mod * chore(deps-dev): bump @babel/runtime in /services/idp Bumps [@babel/runtime](https://github.com/babel/babel/tree/HEAD/packages/babel-runtime) from 7.28.2 to 7.28.6. - [Release notes](https://github.com/babel/babel/releases) - [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md) - [Commits](https://github.com/babel/babel/commits/v7.28.6/packages/babel-runtime) --- updated-dependencies: - dependency-name: "@babel/runtime" dependency-version: 7.28.6 dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
151 lines
2.9 KiB
Go
151 lines
2.9 KiB
Go
package imaging
|
|
|
|
import (
|
|
"image"
|
|
)
|
|
|
|
// ConvolveOptions are convolution parameters.
|
|
type ConvolveOptions struct {
|
|
// If Normalize is true the kernel is normalized before convolution.
|
|
Normalize bool
|
|
|
|
// If Abs is true the absolute value of each color channel is taken after convolution.
|
|
Abs bool
|
|
|
|
// Bias is added to each color channel value after convolution.
|
|
Bias int
|
|
}
|
|
|
|
// Convolve3x3 convolves the image with the specified 3x3 convolution kernel.
|
|
// Default parameters are used if a nil *ConvolveOptions is passed.
|
|
func Convolve3x3(img image.Image, kernel [9]float64, options *ConvolveOptions) *image.NRGBA {
|
|
return convolve(img, kernel[:], options)
|
|
}
|
|
|
|
// Convolve5x5 convolves the image with the specified 5x5 convolution kernel.
|
|
// Default parameters are used if a nil *ConvolveOptions is passed.
|
|
func Convolve5x5(img image.Image, kernel [25]float64, options *ConvolveOptions) *image.NRGBA {
|
|
return convolve(img, kernel[:], options)
|
|
}
|
|
|
|
func convolve(img image.Image, kernel []float64, options *ConvolveOptions) *image.NRGBA {
|
|
src := toNRGBA(img)
|
|
w := src.Bounds().Max.X
|
|
h := src.Bounds().Max.Y
|
|
dst := image.NewNRGBA(image.Rect(0, 0, w, h))
|
|
|
|
if w < 1 || h < 1 {
|
|
return dst
|
|
}
|
|
|
|
if options == nil {
|
|
options = &ConvolveOptions{}
|
|
}
|
|
|
|
if options.Normalize {
|
|
normalizeKernel(kernel)
|
|
}
|
|
|
|
type coef struct {
|
|
x, y int
|
|
k float64
|
|
}
|
|
var coefs []coef
|
|
var m int
|
|
|
|
switch len(kernel) {
|
|
case 9:
|
|
m = 1
|
|
case 25:
|
|
m = 2
|
|
}
|
|
|
|
i := 0
|
|
for y := -m; y <= m; y++ {
|
|
for x := -m; x <= m; x++ {
|
|
if kernel[i] != 0 {
|
|
coefs = append(coefs, coef{x: x, y: y, k: kernel[i]})
|
|
}
|
|
i++
|
|
}
|
|
}
|
|
|
|
if err := run_in_parallel_over_range(0, func(start, limit int) {
|
|
for y := start; y < limit; y++ {
|
|
for x := range w {
|
|
var r, g, b float64
|
|
for _, c := range coefs {
|
|
ix := x + c.x
|
|
if ix < 0 {
|
|
ix = 0
|
|
} else if ix >= w {
|
|
ix = w - 1
|
|
}
|
|
|
|
iy := y + c.y
|
|
if iy < 0 {
|
|
iy = 0
|
|
} else if iy >= h {
|
|
iy = h - 1
|
|
}
|
|
|
|
off := iy*src.Stride + ix*4
|
|
s := src.Pix[off : off+3 : off+3]
|
|
r += float64(s[0]) * c.k
|
|
g += float64(s[1]) * c.k
|
|
b += float64(s[2]) * c.k
|
|
}
|
|
|
|
if options.Abs {
|
|
if r < 0 {
|
|
r = -r
|
|
}
|
|
if g < 0 {
|
|
g = -g
|
|
}
|
|
if b < 0 {
|
|
b = -b
|
|
}
|
|
}
|
|
|
|
if options.Bias != 0 {
|
|
r += float64(options.Bias)
|
|
g += float64(options.Bias)
|
|
b += float64(options.Bias)
|
|
}
|
|
|
|
srcOff := y*src.Stride + x*4
|
|
dstOff := y*dst.Stride + x*4
|
|
d := dst.Pix[dstOff : dstOff+4 : dstOff+4]
|
|
d[0] = clamp(r)
|
|
d[1] = clamp(g)
|
|
d[2] = clamp(b)
|
|
d[3] = src.Pix[srcOff+3]
|
|
}
|
|
}
|
|
}, 0, h); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return dst
|
|
}
|
|
|
|
func normalizeKernel(kernel []float64) {
|
|
var sum, sumpos float64
|
|
for i := range kernel {
|
|
sum += kernel[i]
|
|
if kernel[i] > 0 {
|
|
sumpos += kernel[i]
|
|
}
|
|
}
|
|
if sum != 0 {
|
|
for i := range kernel {
|
|
kernel[i] /= sum
|
|
}
|
|
} else if sumpos != 0 {
|
|
for i := range kernel {
|
|
kernel[i] /= sumpos
|
|
}
|
|
}
|
|
}
|