2012-08-02 21:59:40 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2012, Jan Schlicht <jan.schlicht@gmail.com>
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any purpose
|
|
|
|
with or without fee is hereby granted, provided that the above copyright notice
|
|
|
|
and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
|
|
THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package resize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/color"
|
2012-09-14 23:12:05 +02:00
|
|
|
"math"
|
2012-08-02 21:59:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// color.RGBA64 as array
|
2012-09-15 19:30:32 +02:00
|
|
|
type rgba16 [4]uint16
|
2012-08-02 21:59:40 +02:00
|
|
|
|
|
|
|
// build RGBA from an arbitrary color
|
2012-09-15 19:30:32 +02:00
|
|
|
func toRGBA(c color.Color) rgba16 {
|
2012-09-01 00:21:10 +02:00
|
|
|
r, g, b, a := c.RGBA()
|
2012-09-15 19:30:32 +02:00
|
|
|
return rgba16{uint16(r), uint16(g), uint16(b), uint16(a)}
|
2012-08-02 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func clampToUint16(x float32) (y uint16) {
|
|
|
|
y = uint16(x)
|
|
|
|
if x < 0 {
|
|
|
|
y = 0
|
2012-09-14 23:12:05 +02:00
|
|
|
} else if x > float32(0xfffe) {
|
2012-08-02 21:59:40 +02:00
|
|
|
y = 0xffff
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-09-15 20:24:14 +02:00
|
|
|
type filterModel struct {
|
|
|
|
src image.Image
|
|
|
|
size int
|
|
|
|
kernel func(float32) float32
|
|
|
|
tempRow []rgba16
|
|
|
|
tempCol []rgba16
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *filterModel) convolution1d(x float32, p []rgba16) (c rgba16) {
|
2012-09-04 22:51:19 +02:00
|
|
|
x -= float32(int(x))
|
2012-09-15 20:24:14 +02:00
|
|
|
m := float32(f.size/2 - 1)
|
2012-09-04 18:49:04 +02:00
|
|
|
|
2012-09-14 23:12:05 +02:00
|
|
|
var k float32
|
|
|
|
var sum float32 = 0
|
2012-09-04 18:49:04 +02:00
|
|
|
l := [4]float32{0.0, 0.0, 0.0, 0.0}
|
|
|
|
|
|
|
|
for j := range p {
|
2012-09-15 20:24:14 +02:00
|
|
|
k = f.kernel(x + m - float32(j))
|
2012-09-14 23:12:05 +02:00
|
|
|
sum += k
|
2012-09-04 18:49:04 +02:00
|
|
|
for i := range c {
|
2012-09-14 23:12:05 +02:00
|
|
|
l[i] += float32(p[j][i]) * k
|
2012-09-04 18:49:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := range c {
|
|
|
|
c[i] = clampToUint16(l[i] / sum)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-09-15 20:24:14 +02:00
|
|
|
func (f *filterModel) Interpolate(x, y float32) color.RGBA64 {
|
|
|
|
xf, yf := int(x)-f.size/2+1, int(y)-f.size/2+1
|
2012-09-04 18:49:04 +02:00
|
|
|
|
2012-09-15 20:24:14 +02:00
|
|
|
for i := 0; i < f.size; i++ {
|
|
|
|
for j := 0; j < f.size; j++ {
|
|
|
|
f.tempRow[j] = toRGBA(f.src.At(xf+j, yf+i))
|
2012-09-14 23:12:05 +02:00
|
|
|
}
|
2012-09-15 20:24:14 +02:00
|
|
|
f.tempCol[i] = f.convolution1d(x, f.tempRow)
|
2012-09-04 18:49:04 +02:00
|
|
|
}
|
|
|
|
|
2012-09-15 20:24:14 +02:00
|
|
|
c := f.convolution1d(y, f.tempCol)
|
2012-09-04 18:49:04 +02:00
|
|
|
return color.RGBA64{c[0], c[1], c[2], c[3]}
|
|
|
|
}
|
|
|
|
|
2012-09-14 23:12:05 +02:00
|
|
|
// Nearest-neighbor interpolation.
|
|
|
|
// Approximates a value by returning the value of the nearest point.
|
2012-09-15 20:24:14 +02:00
|
|
|
func NearestNeighbor(img image.Image) Filter {
|
|
|
|
return &filterModel{img, 2, func(x float32) (y float32) {
|
2012-09-15 19:30:32 +02:00
|
|
|
if x >= -0.5 && x < 0.5 {
|
2012-09-14 23:12:05 +02:00
|
|
|
y = 1
|
|
|
|
} else {
|
|
|
|
y = 0
|
|
|
|
}
|
|
|
|
return
|
2012-09-15 20:24:14 +02:00
|
|
|
}, make([]rgba16, 2), make([]rgba16, 2)}
|
2012-09-14 23:12:05 +02:00
|
|
|
}
|
2012-09-04 18:49:04 +02:00
|
|
|
|
2012-09-14 23:12:05 +02:00
|
|
|
// Bicubic interpolation
|
2012-09-15 20:24:14 +02:00
|
|
|
func Bilinear(img image.Image) Filter {
|
|
|
|
return &filterModel{img, 2, func(x float32) float32 {
|
2012-09-15 19:30:32 +02:00
|
|
|
return 1 - float32(math.Abs(float64(x)))
|
2012-09-15 20:24:14 +02:00
|
|
|
}, make([]rgba16, 2), make([]rgba16, 2)}
|
2012-09-14 23:12:05 +02:00
|
|
|
}
|
2012-09-04 18:49:04 +02:00
|
|
|
|
2012-09-14 23:12:05 +02:00
|
|
|
// Bicubic interpolation
|
2012-09-15 20:24:14 +02:00
|
|
|
func Bicubic(img image.Image) Filter {
|
|
|
|
return &filterModel{img, 4, func(x float32) (y float32) {
|
2012-09-15 19:30:32 +02:00
|
|
|
absX := float32(math.Abs(float64(x)))
|
|
|
|
if absX <= 1 {
|
|
|
|
y = absX*absX*(1.5*absX-2.5) + 1
|
2012-09-14 23:12:05 +02:00
|
|
|
} else {
|
2012-09-15 19:30:32 +02:00
|
|
|
y = absX*(absX*(2.5-0.5*absX)-4) + 2
|
2012-08-02 21:59:40 +02:00
|
|
|
}
|
2012-09-14 23:12:05 +02:00
|
|
|
return
|
2012-09-15 20:24:14 +02:00
|
|
|
}, make([]rgba16, 4), make([]rgba16, 4)}
|
2012-09-14 23:12:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lanczos interpolation (a=2).
|
2012-09-15 20:24:14 +02:00
|
|
|
func Lanczos2(img image.Image) Filter {
|
|
|
|
return &filterModel{img, 4, func(x float32) float32 {
|
2012-09-15 19:30:32 +02:00
|
|
|
return float32(Sinc(float64(x))) * float32(Sinc(float64((x)/float32(2))))
|
2012-09-15 20:24:14 +02:00
|
|
|
}, make([]rgba16, 4), make([]rgba16, 4)}
|
2012-08-02 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lanczos interpolation (a=3).
|
2012-09-15 20:24:14 +02:00
|
|
|
func Lanczos3(img image.Image) Filter {
|
|
|
|
return &filterModel{img, 6, func(x float32) float32 {
|
2012-09-15 19:30:32 +02:00
|
|
|
return float32(Sinc(float64(x))) * float32(Sinc(float64((x)/float32(3))))
|
2012-09-15 20:24:14 +02:00
|
|
|
}, make([]rgba16, 6), make([]rgba16, 6)}
|
2012-08-02 21:59:40 +02:00
|
|
|
}
|