diff --git a/cmd/main.go b/cmd/main.go index a4d61ed..e585c1f 100755 --- a/cmd/main.go +++ b/cmd/main.go @@ -8,9 +8,7 @@ import ( "image/draw" "image/gif" "image/jpeg" - _ "image/jpeg" "image/png" - _ "image/png" "log" "os" "path" @@ -37,6 +35,9 @@ func main() { } img, err := os.Open(*source) + if err != nil { + log.Fatal("could not open source image:", err) + } defer img.Close() src, _, err := image.Decode(img) @@ -46,7 +47,7 @@ func main() { start := time.Now() if *outputGif { for i := 1; i <= *radius; i++ { - img, err := stackblur.Run(src, uint32(i)) + img, err := stackblur.Process(src, uint32(i)) if err != nil { log.Fatal(err) } @@ -67,7 +68,7 @@ func main() { log.Fatal(err) } } else { - img, err := stackblur.Run(src, uint32(*radius)) + img, err := stackblur.Process(src, uint32(*radius)) if err != nil { log.Fatal(err) } @@ -76,7 +77,7 @@ func main() { } } end := time.Since(start) - fmt.Printf("\nGenerated in: %.2fs\n", end.Seconds()) + fmt.Printf("Generated in: %.2fs\n", end.Seconds()) } // encodeGIF encodes the generated output into a gif file @@ -100,6 +101,9 @@ func encodeGIF(imgs []image.Image, path string) error { // generateImage generates the image type depending on the provided extension func generateImage(dst string, img image.Image) error { output, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR, 0755) + if err != nil { + return err + } defer output.Close() if err != nil { diff --git a/go.mod b/go.mod index 42d6a55..abfdbd1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,12 @@ module github.com/esimov/stackblur-go -go 1.13 +go 1.16 + +// Provides an unstable API +retract v1.0.0 + +// Introduces backwards incompatible changes +retract v1.0.1 + +// Also makes a backwards incompatible change +retract v1.0.2 diff --git a/stackblur.go b/stackblur.go index 92e0d03..aa759be 100755 --- a/stackblur.go +++ b/stackblur.go @@ -7,8 +7,6 @@ import ( "errors" "image" "image/color" - "io" - "os" ) // blurStack is a linked list containing the color value and a pointer to the next struct. @@ -55,27 +53,15 @@ var shgTable = []uint32{ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, } -// Run takes an image or pixel data as input and returns -// it's blurred version by applying the blur radius defined as parameter. -func Run(input interface{}, radius uint32) (image.Image, error) { +// Process takes the source image and returns it's blurred version by applying the blur radius defined as parameter. +func Process(src image.Image, radius uint32) (*image.NRGBA, error) { var ( stackEnd *blurStack stackIn *blurStack stackOut *blurStack - src interface{} - err error ) - switch input.(type) { - case *os.File: - src, _, err = image.Decode(input.(io.Reader)) - if err != nil { - return nil, err - } - default: - src = input - } - var width, height = uint32(src.(image.Image).Bounds().Dx()), uint32(src.(image.Image).Bounds().Dy()) + var width, height = uint32(src.Bounds().Dx()), uint32(src.Bounds().Dy()) var ( div, widthMinus1, heightMinus1, radiusPlus1, sumFactor uint32 x, y, i, p, yp, yi, yw, @@ -94,7 +80,7 @@ func Run(input interface{}, radius uint32) (image.Image, error) { return nil, errors.New("blur radius must be greater than 0") } - img := toNRGBA(src.(image.Image)) + img := toNRGBA(src) div = radius + radius + 1 widthMinus1 = width - 1