1 Star 0 Fork 0

register / graphics-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
blur.go 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
zhaoce 提交于 2018-03-23 11:19 . add google graphics code commit
// Copyright 2011 The Graphics-Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package graphics
import (
"code.google.com/p/graphics-go/graphics/convolve"
"errors"
"image"
"image/draw"
"math"
)
// DefaultStdDev is the default blurring parameter.
var DefaultStdDev = 0.5
// BlurOptions are the blurring parameters.
// StdDev is the standard deviation of the normal, higher is blurrier.
// Size is the size of the kernel. If zero, it is set to Ceil(6 * StdDev).
type BlurOptions struct {
StdDev float64
Size int
}
// Blur produces a blurred version of the image, using a Gaussian blur.
func Blur(dst draw.Image, src image.Image, opt *BlurOptions) error {
if dst == nil {
return errors.New("graphics: dst is nil")
}
if src == nil {
return errors.New("graphics: src is nil")
}
sd := DefaultStdDev
size := 0
if opt != nil {
sd = opt.StdDev
size = opt.Size
}
if size < 1 {
size = int(math.Ceil(sd * 6))
}
kernel := make([]float64, 2*size+1)
for i := 0; i <= size; i++ {
x := float64(i) / sd
x = math.Pow(1/math.SqrtE, x*x)
kernel[size-i] = x
kernel[size+i] = x
}
// Normalize the weights to sum to 1.0.
kSum := 0.0
for _, k := range kernel {
kSum += k
}
for i, k := range kernel {
kernel[i] = k / kSum
}
return convolve.Convolve(dst, src, &convolve.SeparableKernel{
X: kernel,
Y: kernel,
})
}
Go
1
https://gitee.com/register2019/graphics-go.git
git@gitee.com:register2019/graphics-go.git
register2019
graphics-go
graphics-go
6e9c5fa68587

搜索帮助