Multiply instead of dividing

This commit is contained in:
jst 2014-01-17 23:14:37 +01:00
parent 314cea6612
commit 45368ad0cd

View File

@ -42,7 +42,7 @@ type filterModel struct {
// instead of blurring an image before downscaling to avoid aliasing, // instead of blurring an image before downscaling to avoid aliasing,
// the filter is scaled by a factor which leads to a similar effect // the filter is scaled by a factor which leads to a similar effect
factor float32 factorInv float32
// for optimized access to image points // for optimized access to image points
converter converter
@ -51,12 +51,12 @@ type filterModel struct {
tempRow []colorArray tempRow []colorArray
} }
func (f *filterModel) convolution1d(x float32, p []colorArray, factor float32) (c colorArray) { func (f *filterModel) convolution1d(x float32, p []colorArray) (c colorArray) {
var k float32 var k float32
var sum float32 = 0 var sum float32 = 0
for j := range p { for j := range p {
k = f.kernel((x - float32(j)) / factor) k = f.kernel((x - float32(j)) * f.factorInv)
sum += k sum += k
for i := range c { for i := range c {
c[i] += p[j][i] * k c[i] += p[j][i] * k
@ -79,7 +79,7 @@ func (f *filterModel) Interpolate(u float32, y int) color.RGBA64 {
f.at(uf+i, y, &f.tempRow[i]) f.at(uf+i, y, &f.tempRow[i])
} }
c := f.convolution1d(u, f.tempRow, f.factor) c := f.convolution1d(u, f.tempRow)
return color.RGBA64{ return color.RGBA64{
clampToUint16(c[0]), clampToUint16(c[0]),
clampToUint16(c[1]), clampToUint16(c[1]),
@ -96,37 +96,37 @@ func createFilter(img image.Image, factor float32, size int, kernel func(float32
switch img.(type) { switch img.(type) {
default: default:
f = &filterModel{ f = &filterModel{
kernel, factor, kernel, 1. / factor,
&genericConverter{img}, &genericConverter{img},
make([]colorArray, sizeX), make([]colorArray, sizeX),
} }
case *image.RGBA: case *image.RGBA:
f = &filterModel{ f = &filterModel{
kernel, factor, kernel, 1. / factor,
&rgbaConverter{img.(*image.RGBA)}, &rgbaConverter{img.(*image.RGBA)},
make([]colorArray, sizeX), make([]colorArray, sizeX),
} }
case *image.RGBA64: case *image.RGBA64:
f = &filterModel{ f = &filterModel{
kernel, factor, kernel, 1. / factor,
&rgba64Converter{img.(*image.RGBA64)}, &rgba64Converter{img.(*image.RGBA64)},
make([]colorArray, sizeX), make([]colorArray, sizeX),
} }
case *image.Gray: case *image.Gray:
f = &filterModel{ f = &filterModel{
kernel, factor, kernel, 1. / factor,
&grayConverter{img.(*image.Gray)}, &grayConverter{img.(*image.Gray)},
make([]colorArray, sizeX), make([]colorArray, sizeX),
} }
case *image.Gray16: case *image.Gray16:
f = &filterModel{ f = &filterModel{
kernel, factor, kernel, 1. / factor,
&gray16Converter{img.(*image.Gray16)}, &gray16Converter{img.(*image.Gray16)},
make([]colorArray, sizeX), make([]colorArray, sizeX),
} }
case *image.YCbCr: case *image.YCbCr:
f = &filterModel{ f = &filterModel{
kernel, factor, kernel, 1. / factor,
&ycbcrConverter{img.(*image.YCbCr)}, &ycbcrConverter{img.(*image.YCbCr)},
make([]colorArray, sizeX), make([]colorArray, sizeX),
} }