Function signature changed again, no need for multiple return value

This commit is contained in:
jst 2012-08-09 18:56:42 +02:00
parent a2154d46c5
commit 6b67b636b8
3 changed files with 19 additions and 33 deletions

View File

@ -21,7 +21,8 @@ Import package with
import "github.com/nfnt/resize"
```
Resize creates a scaled image with new dimensions (width, height) using the interpolation function interp.
Resize creates a scaled image with new dimensions (`width`, `height`) using the interpolation function interp.
If either `width` or `height` is set to 0, it will be set to an aspect ratio preserving value.
```go
resize.Resize(width, height int, img image.Image, interp resize.InterpolationFunction) image.Image, error
@ -60,10 +61,8 @@ func main() {
file.Close()
// resize to width 1000 using Lanczos resampling
m, err := resize.Resize(1000, -1, img, resize.Lanczos3)
if err != nil {
return
}
// and preserve aspect ration
m := resize.Resize(1000, 0, img, resize.Lanczos3)
out, err := os.Create("test_resized.jpg")
if err != nil {

View File

@ -25,7 +25,6 @@ THIS SOFTWARE.
package resize
import (
"errors"
"image"
"image/color"
"runtime"
@ -47,9 +46,9 @@ func (t *Trans2) Eval(x, y float32) (u, v float32) {
}
// Calculate scaling factors using old and new image dimensions.
func calcFactors(width, height int, oldWidth, oldHeight float32) (scaleX, scaleY float32) {
if width == -1 {
if height == -1 {
func calcFactors(width, height uint, oldWidth, oldHeight float32) (scaleX, scaleY float32) {
if width == 0 {
if height == 0 {
scaleX = 1.0
scaleY = 1.0
} else {
@ -58,7 +57,7 @@ func calcFactors(width, height int, oldWidth, oldHeight float32) (scaleX, scaleY
}
} else {
scaleX = oldWidth / float32(width)
if height == -1 {
if height == 0 {
scaleY = scaleX
} else {
scaleY = oldHeight / float32(height)
@ -73,14 +72,10 @@ type InterpolationFunction func(float32, float32, image.Image) color.RGBA64
// Resize an image to new width w and height h using the interpolation function interp.
// A new image with the given dimensions will be returned.
// If one of the parameters w or h is set to -1, its size will be calculated so that
// If one of the parameters w or h is set to 0, its size will be calculated so that
// the aspect ratio is that of the originating image.
// The resizing algorithm uses channels for parallel computation.
func Resize(width, height int, img image.Image, interp InterpolationFunction) (out image.Image, err error) {
if width < -1 || height < -1 {
err = errors.New("Wrong width/height argument")
return
}
func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
oldBounds := img.Bounds()
oldWidth := float32(oldBounds.Dx())
oldHeight := float32(oldBounds.Dy())
@ -108,7 +103,6 @@ func Resize(width, height int, img image.Image, interp InterpolationFunction) (o
for i := 0; i < NCPU; i++ {
<-c
}
out = m
return
return m
}

View File

@ -11,30 +11,23 @@ var img = image.NewGray16(image.Rect(0, 0, 3, 3))
func Test_Nearest(t *testing.T) {
img.Set(1, 1, color.White)
m, err := Resize(6, -1, img, NearestNeighbor)
m := Resize(6, 0, img, NearestNeighbor)
if err != nil || m.At(2, 2) != m.At(3, 3) {
if m.At(2, 2) != m.At(3, 3) {
t.Fail()
}
}
func Test_Param1(t *testing.T) {
m, err := Resize(-1, -1, img, NearestNeighbor)
if err != nil || m.Bounds() != img.Bounds() {
m := Resize(0, 0, img, NearestNeighbor)
if m.Bounds() != img.Bounds() {
t.Fail()
}
}
func Test_Param2(t *testing.T) {
_, err := Resize(-100, -1, img, NearestNeighbor)
if err == nil {
t.Fail()
}
}
func Test_Param3(t *testing.T) {
m, err := Resize(0, -1, img, NearestNeighbor)
if err != nil || m.Bounds() != image.Rect(0, 0, 0, 0) {
m := Resize(100, 0, img, NearestNeighbor)
if m.Bounds() != image.Rect(0, 0, 100, 100) {
t.Fail()
}
}
@ -42,8 +35,8 @@ func Test_Param3(t *testing.T) {
func Test_ZeroImg(t *testing.T) {
zeroImg := image.NewGray16(image.Rect(0, 0, 0, 0))
m, err := Resize(-1, -1, zeroImg, NearestNeighbor)
if err != nil || m.Bounds() != zeroImg.Bounds() {
m := Resize(0, 0, zeroImg, NearestNeighbor)
if m.Bounds() != zeroImg.Bounds() {
t.Fail()
}
}