Function signature changed again, no need for multiple return value
This commit is contained in:
parent
a2154d46c5
commit
9041d4db18
11
README.md
11
README.md
|
@ -21,10 +21,11 @@ Import package with
|
||||||
import "github.com/nfnt/resize"
|
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
|
```go
|
||||||
resize.Resize(width, height int, img image.Image, interp resize.InterpolationFunction) image.Image, error
|
resize.Resize(width, height uint, img image.Image, interp resize.InterpolationFunction) image.Image, error
|
||||||
```
|
```
|
||||||
|
|
||||||
The provided interpolation functions are
|
The provided interpolation functions are
|
||||||
|
@ -60,10 +61,8 @@ func main() {
|
||||||
file.Close()
|
file.Close()
|
||||||
|
|
||||||
// resize to width 1000 using Lanczos resampling
|
// resize to width 1000 using Lanczos resampling
|
||||||
m, err := resize.Resize(1000, -1, img, resize.Lanczos3)
|
// and preserve aspect ration
|
||||||
if err != nil {
|
m := resize.Resize(1000, 0, img, resize.Lanczos3)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
out, err := os.Create("test_resized.jpg")
|
out, err := os.Create("test_resized.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
20
resize.go
20
resize.go
|
@ -25,7 +25,6 @@ THIS SOFTWARE.
|
||||||
package resize
|
package resize
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
@ -47,9 +46,9 @@ func (t *Trans2) Eval(x, y float32) (u, v float32) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate scaling factors using old and new image dimensions.
|
// Calculate scaling factors using old and new image dimensions.
|
||||||
func calcFactors(width, height int, oldWidth, oldHeight float32) (scaleX, scaleY float32) {
|
func calcFactors(width, height uint, oldWidth, oldHeight float32) (scaleX, scaleY float32) {
|
||||||
if width == -1 {
|
if width == 0 {
|
||||||
if height == -1 {
|
if height == 0 {
|
||||||
scaleX = 1.0
|
scaleX = 1.0
|
||||||
scaleY = 1.0
|
scaleY = 1.0
|
||||||
} else {
|
} else {
|
||||||
|
@ -58,7 +57,7 @@ func calcFactors(width, height int, oldWidth, oldHeight float32) (scaleX, scaleY
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
scaleX = oldWidth / float32(width)
|
scaleX = oldWidth / float32(width)
|
||||||
if height == -1 {
|
if height == 0 {
|
||||||
scaleY = scaleX
|
scaleY = scaleX
|
||||||
} else {
|
} else {
|
||||||
scaleY = oldHeight / float32(height)
|
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.
|
// 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.
|
// 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 aspect ratio is that of the originating image.
|
||||||
// The resizing algorithm uses channels for parallel computation.
|
// The resizing algorithm uses channels for parallel computation.
|
||||||
func Resize(width, height int, img image.Image, interp InterpolationFunction) (out image.Image, err error) {
|
func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
|
||||||
if width < -1 || height < -1 {
|
|
||||||
err = errors.New("Wrong width/height argument")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
oldBounds := img.Bounds()
|
oldBounds := img.Bounds()
|
||||||
oldWidth := float32(oldBounds.Dx())
|
oldWidth := float32(oldBounds.Dx())
|
||||||
oldHeight := float32(oldBounds.Dy())
|
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++ {
|
for i := 0; i < NCPU; i++ {
|
||||||
<-c
|
<-c
|
||||||
}
|
}
|
||||||
out = m
|
|
||||||
|
|
||||||
return
|
return m
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,30 +11,23 @@ var img = image.NewGray16(image.Rect(0, 0, 3, 3))
|
||||||
func Test_Nearest(t *testing.T) {
|
func Test_Nearest(t *testing.T) {
|
||||||
img.Set(1, 1, color.White)
|
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()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Param1(t *testing.T) {
|
func Test_Param1(t *testing.T) {
|
||||||
m, err := Resize(-1, -1, img, NearestNeighbor)
|
m := Resize(0, 0, img, NearestNeighbor)
|
||||||
if err != nil || m.Bounds() != img.Bounds() {
|
if m.Bounds() != img.Bounds() {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Param2(t *testing.T) {
|
func Test_Param2(t *testing.T) {
|
||||||
_, err := Resize(-100, -1, img, NearestNeighbor)
|
m := Resize(100, 0, img, NearestNeighbor)
|
||||||
if err == nil {
|
if m.Bounds() != image.Rect(0, 0, 100, 100) {
|
||||||
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) {
|
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,8 +35,8 @@ func Test_Param3(t *testing.T) {
|
||||||
func Test_ZeroImg(t *testing.T) {
|
func Test_ZeroImg(t *testing.T) {
|
||||||
zeroImg := image.NewGray16(image.Rect(0, 0, 0, 0))
|
zeroImg := image.NewGray16(image.Rect(0, 0, 0, 0))
|
||||||
|
|
||||||
m, err := Resize(-1, -1, zeroImg, NearestNeighbor)
|
m := Resize(0, 0, zeroImg, NearestNeighbor)
|
||||||
if err != nil || m.Bounds() != zeroImg.Bounds() {
|
if m.Bounds() != zeroImg.Bounds() {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user