2012-08-02 19:59:40 +00: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 21:12:05 +00:00
|
|
|
"math"
|
2012-08-02 19:59:40 +00:00
|
|
|
)
|
|
|
|
|
2012-12-11 19:18:23 +00:00
|
|
|
// restrict an input float32 to the range of uint16 values
|
2012-08-02 19:59:40 +00:00
|
|
|
func clampToUint16(x float32) (y uint16) {
|
|
|
|
y = uint16(x)
|
|
|
|
if x < 0 {
|
|
|
|
y = 0
|
2012-09-14 21:12:05 +00:00
|
|
|
} else if x > float32(0xfffe) {
|
2012-12-10 17:56:53 +00:00
|
|
|
// "else if x > float32(0xffff)" will cause overflows!
|
2012-08-02 19:59:40 +00:00
|
|
|
y = 0xffff
|
|
|
|
}
|
2012-12-13 20:55:44 +00:00
|
|
|
|
2012-08-02 19:59:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-11 19:18:23 +00:00
|
|
|
// describe a resampling filter
|
2012-09-15 18:24:14 +00:00
|
|
|
type filterModel struct {
|
2012-12-11 19:24:30 +00:00
|
|
|
// resampling is done by convolution with a (scaled) kernel
|
|
|
|
kernel func(float32) float32
|
2012-12-11 19:18:23 +00:00
|
|
|
|
|
|
|
// instead of blurring an image before downscaling to avoid aliasing,
|
2012-12-13 21:13:37 +00:00
|
|
|
// the filter is scaled by a factor which leads to a similar effect
|
2012-12-11 19:18:23 +00:00
|
|
|
factor [2]float32
|
|
|
|
|
2012-12-11 19:24:30 +00:00
|
|
|
// for optimized access to image points
|
|
|
|
converter
|
2012-12-11 19:18:23 +00:00
|
|
|
|
|
|
|
// temporaries used by Interpolate
|
2012-09-21 18:02:25 +00:00
|
|
|
tempRow, tempCol []colorArray
|
2012-09-15 18:24:14 +00:00
|
|
|
}
|
|
|
|
|
2012-12-13 21:13:37 +00:00
|
|
|
func (f *filterModel) convolution1d(x float32, p []colorArray, factor float32) colorArray {
|
2012-09-14 21:12:05 +00:00
|
|
|
var k float32
|
|
|
|
var sum float32 = 0
|
2012-09-21 18:02:25 +00:00
|
|
|
c := colorArray{0.0, 0.0, 0.0, 0.0}
|
2012-09-04 16:49:04 +00:00
|
|
|
|
|
|
|
for j := range p {
|
2012-12-13 21:13:37 +00:00
|
|
|
k = f.kernel((x - float32(j)) / factor)
|
2012-09-14 21:12:05 +00:00
|
|
|
sum += k
|
2012-09-04 16:49:04 +00:00
|
|
|
for i := range c {
|
2012-09-21 18:02:25 +00:00
|
|
|
c[i] += p[j][i] * k
|
2012-09-04 16:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-21 18:02:25 +00:00
|
|
|
|
|
|
|
// normalize values
|
2012-09-04 16:49:04 +00:00
|
|
|
for i := range c {
|
2012-09-21 18:02:25 +00:00
|
|
|
c[i] = c[i] / sum
|
2012-09-04 16:49:04 +00:00
|
|
|
}
|
2012-12-13 20:55:44 +00:00
|
|
|
|
2012-09-21 18:02:25 +00:00
|
|
|
return c
|
2012-09-04 16:49:04 +00:00
|
|
|
}
|
|
|
|
|
2012-09-15 18:24:14 +00:00
|
|
|
func (f *filterModel) Interpolate(x, y float32) color.RGBA64 {
|
2012-09-19 19:03:56 +00:00
|
|
|
xf, yf := int(x)-len(f.tempRow)/2+1, int(y)-len(f.tempCol)/2+1
|
2012-09-16 07:20:11 +00:00
|
|
|
x -= float32(xf)
|
|
|
|
y -= float32(yf)
|
2012-09-04 16:49:04 +00:00
|
|
|
|
2012-12-13 20:55:44 +00:00
|
|
|
for i := range f.tempCol {
|
|
|
|
for j := range f.tempRow {
|
2012-09-21 18:02:25 +00:00
|
|
|
f.tempRow[j] = f.at(xf+j, yf+i)
|
2012-09-14 21:12:05 +00:00
|
|
|
}
|
2012-12-13 20:55:44 +00:00
|
|
|
|
2012-12-13 21:13:37 +00:00
|
|
|
f.tempCol[i] = f.convolution1d(x, f.tempRow, f.factor[0])
|
2012-09-04 16:49:04 +00:00
|
|
|
}
|
|
|
|
|
2012-12-13 21:13:37 +00:00
|
|
|
c := f.convolution1d(y, f.tempCol, f.factor[1])
|
2012-09-21 18:02:25 +00:00
|
|
|
return color.RGBA64{
|
|
|
|
clampToUint16(c[0]),
|
|
|
|
clampToUint16(c[1]),
|
|
|
|
clampToUint16(c[2]),
|
|
|
|
clampToUint16(c[3]),
|
|
|
|
}
|
2012-09-04 16:49:04 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 18:02:25 +00:00
|
|
|
// createFilter tries to find an optimized converter for the given input image
|
|
|
|
// and initializes all filterModel members to their defaults
|
|
|
|
func createFilter(img image.Image, factor [2]float32, size int, kernel func(float32) float32) (f Filter) {
|
2012-09-19 19:03:56 +00:00
|
|
|
sizeX := size * (int(math.Ceil(float64(factor[0]))))
|
|
|
|
sizeY := size * (int(math.Ceil(float64(factor[1]))))
|
2012-09-21 18:02:25 +00:00
|
|
|
|
|
|
|
switch img.(type) {
|
|
|
|
default:
|
|
|
|
f = &filterModel{
|
2012-12-11 19:24:30 +00:00
|
|
|
kernel, factor,
|
2012-09-21 18:02:25 +00:00
|
|
|
&genericConverter{img},
|
|
|
|
make([]colorArray, sizeX), make([]colorArray, sizeY),
|
|
|
|
}
|
|
|
|
case *image.RGBA:
|
|
|
|
f = &filterModel{
|
2012-12-11 19:24:30 +00:00
|
|
|
kernel, factor,
|
2012-09-21 18:02:25 +00:00
|
|
|
&rgbaConverter{img.(*image.RGBA)},
|
|
|
|
make([]colorArray, sizeX), make([]colorArray, sizeY),
|
|
|
|
}
|
|
|
|
case *image.RGBA64:
|
|
|
|
f = &filterModel{
|
2012-12-11 19:24:30 +00:00
|
|
|
kernel, factor,
|
2012-09-21 18:02:25 +00:00
|
|
|
&rgba64Converter{img.(*image.RGBA64)},
|
|
|
|
make([]colorArray, sizeX), make([]colorArray, sizeY),
|
|
|
|
}
|
|
|
|
case *image.Gray:
|
|
|
|
f = &filterModel{
|
2012-12-11 19:24:30 +00:00
|
|
|
kernel, factor,
|
2012-09-21 18:02:25 +00:00
|
|
|
&grayConverter{img.(*image.Gray)},
|
|
|
|
make([]colorArray, sizeX), make([]colorArray, sizeY),
|
|
|
|
}
|
|
|
|
case *image.Gray16:
|
|
|
|
f = &filterModel{
|
2012-12-11 19:24:30 +00:00
|
|
|
kernel, factor,
|
2012-09-21 18:02:25 +00:00
|
|
|
&gray16Converter{img.(*image.Gray16)},
|
|
|
|
make([]colorArray, sizeX), make([]colorArray, sizeY),
|
|
|
|
}
|
|
|
|
case *image.YCbCr:
|
|
|
|
f = &filterModel{
|
2012-12-11 19:24:30 +00:00
|
|
|
kernel, factor,
|
2012-09-21 18:02:25 +00:00
|
|
|
&ycbcrConverter{img.(*image.YCbCr)},
|
|
|
|
make([]colorArray, sizeX), make([]colorArray, sizeY),
|
|
|
|
}
|
|
|
|
}
|
2012-12-13 20:55:44 +00:00
|
|
|
|
2012-09-21 18:02:25 +00:00
|
|
|
return
|
2012-09-19 19:03:56 +00:00
|
|
|
}
|
|
|
|
|
2012-09-16 07:20:11 +00:00
|
|
|
// Nearest-neighbor interpolation
|
2012-09-19 19:03:56 +00:00
|
|
|
func NearestNeighbor(img image.Image, factor [2]float32) Filter {
|
|
|
|
return createFilter(img, factor, 2, func(x float32) (y float32) {
|
2012-09-15 17:30:32 +00:00
|
|
|
if x >= -0.5 && x < 0.5 {
|
2012-09-14 21:12:05 +00:00
|
|
|
y = 1
|
|
|
|
} else {
|
|
|
|
y = 0
|
|
|
|
}
|
2012-12-13 20:55:44 +00:00
|
|
|
|
2012-09-14 21:12:05 +00:00
|
|
|
return
|
2012-09-19 19:03:56 +00:00
|
|
|
})
|
2012-09-14 21:12:05 +00:00
|
|
|
}
|
2012-09-04 16:49:04 +00:00
|
|
|
|
2012-09-16 07:20:11 +00:00
|
|
|
// Bilinear interpolation
|
2012-09-19 19:03:56 +00:00
|
|
|
func Bilinear(img image.Image, factor [2]float32) Filter {
|
2012-12-10 17:56:53 +00:00
|
|
|
return createFilter(img, factor, 2, func(x float32) (y float32) {
|
|
|
|
absX := float32(math.Abs(float64(x)))
|
|
|
|
if absX <= 1 {
|
|
|
|
y = 1 - absX
|
|
|
|
} else {
|
|
|
|
y = 0
|
|
|
|
}
|
2012-12-13 20:55:44 +00:00
|
|
|
|
2012-12-10 17:56:53 +00:00
|
|
|
return
|
2012-09-19 19:03:56 +00:00
|
|
|
})
|
2012-09-14 21:12:05 +00:00
|
|
|
}
|
2012-09-04 16:49:04 +00:00
|
|
|
|
2012-09-16 07:20:11 +00:00
|
|
|
// Bicubic interpolation (with cubic hermite spline)
|
2012-09-19 19:03:56 +00:00
|
|
|
func Bicubic(img image.Image, factor [2]float32) Filter {
|
|
|
|
return createFilter(img, factor, 4, func(x float32) (y float32) {
|
2012-09-15 17:30:32 +00:00
|
|
|
absX := float32(math.Abs(float64(x)))
|
|
|
|
if absX <= 1 {
|
|
|
|
y = absX*absX*(1.5*absX-2.5) + 1
|
2012-12-10 17:56:53 +00:00
|
|
|
} else if absX <= 2 {
|
2012-09-15 17:30:32 +00:00
|
|
|
y = absX*(absX*(2.5-0.5*absX)-4) + 2
|
2012-12-10 17:56:53 +00:00
|
|
|
} else {
|
|
|
|
y = 0
|
2012-08-02 19:59:40 +00:00
|
|
|
}
|
2012-12-13 20:55:44 +00:00
|
|
|
|
2012-09-14 21:12:05 +00:00
|
|
|
return
|
2012-09-19 19:03:56 +00:00
|
|
|
})
|
2012-09-14 21:12:05 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 18:02:25 +00:00
|
|
|
// Mitchell-Netravali interpolation
|
2012-09-19 19:03:56 +00:00
|
|
|
func MitchellNetravali(img image.Image, factor [2]float32) Filter {
|
|
|
|
return createFilter(img, factor, 4, func(x float32) (y float32) {
|
2012-09-19 17:32:00 +00:00
|
|
|
absX := float32(math.Abs(float64(x)))
|
|
|
|
if absX <= 1 {
|
|
|
|
y = absX*absX*(7*absX-12) + 16.0/3
|
2012-12-10 17:56:53 +00:00
|
|
|
} else if absX <= 2 {
|
2012-09-19 17:32:00 +00:00
|
|
|
y = -(absX - 2) * (absX - 2) / 3 * (7*absX - 8)
|
2012-12-10 17:56:53 +00:00
|
|
|
} else {
|
|
|
|
y = 0
|
2012-09-19 17:32:00 +00:00
|
|
|
}
|
2012-12-13 20:55:44 +00:00
|
|
|
|
2012-09-19 17:32:00 +00:00
|
|
|
return
|
2012-09-19 19:03:56 +00:00
|
|
|
})
|
2012-09-19 17:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func lanczosKernel(a uint) func(float32) float32 {
|
2012-12-10 17:56:53 +00:00
|
|
|
return func(x float32) (y float32) {
|
|
|
|
if x > -float32(a) && x < float32(a) {
|
|
|
|
y = float32(Sinc(float64(x))) * float32(Sinc(float64(x/float32(a))))
|
|
|
|
} else {
|
|
|
|
y = 0
|
|
|
|
}
|
2012-12-13 20:55:44 +00:00
|
|
|
|
2012-12-10 17:56:53 +00:00
|
|
|
return
|
2012-09-19 17:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-21 18:02:25 +00:00
|
|
|
// Lanczos interpolation (a=2)
|
2012-09-19 19:03:56 +00:00
|
|
|
func Lanczos2(img image.Image, factor [2]float32) Filter {
|
|
|
|
return createFilter(img, factor, 4, lanczosKernel(2))
|
2012-08-02 19:59:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 18:02:25 +00:00
|
|
|
// Lanczos interpolation (a=3)
|
2012-09-19 19:03:56 +00:00
|
|
|
func Lanczos3(img image.Image, factor [2]float32) Filter {
|
|
|
|
return createFilter(img, factor, 6, lanczosKernel(3))
|
2012-08-02 19:59:40 +00:00
|
|
|
}
|