diff --git a/decode.go b/decode.go index 4b170d0..2bdbca9 100644 --- a/decode.go +++ b/decode.go @@ -9,20 +9,23 @@ import ( "os" ) -func (config Config) Decode(path string) error { +func (config Config) Decode(path string) ([]*image.RGBA, error) { // Open the GIF file, err := os.Open(path) if err != nil { - return fmt.Errorf("error while opening file: %s", err) + return nil, fmt.Errorf("error while opening file: %s", err) } - split(file, config.Width, config.Height, config.Output) + imgs, err := split(file, config.Width, config.Height) + if err != nil { + return nil, fmt.Errorf("error while decoding file: %s", err) + } - return nil + return imgs, nil } // Split the GIF into images -func split(file io.Reader, width int, height int, output Output) (imgs []*image.RGBA, err error) { +func split(file io.Reader, width int, height int) (imgs []*image.RGBA, err error) { defer func() { if recv := recover(); recv != nil { err = fmt.Errorf("error while decoding file: %s", recv) @@ -42,14 +45,13 @@ func split(file io.Reader, width int, height int, output Output) (imgs []*image. height = y } - dst := image.NewRGBA(image.Rect(0, 0, width, height)) - draw.Draw(dst, dst.Bounds(), gif.Image[0], image.Point{}, draw.Src) - var images []*image.RGBA for _, img := range gif.Image { + dst := image.NewRGBA(image.Rect(0, 0, width, height)) draw.Draw(dst, dst.Bounds(), img, image.Point{}, draw.Over) - images = append(images, dst) + new := dst + images = append(images, new) } return images, nil diff --git a/encode.go b/encode.go index 560adf4..15e1386 100644 --- a/encode.go +++ b/encode.go @@ -10,10 +10,10 @@ import ( "os" ) -func (config Config) Encode(path string) error { +func (config Config) Encode(path string) (*gif.GIF, error) { files, err := os.ReadDir(path) if err != nil { - return fmt.Errorf("error while opening dir: %s", err) + return nil, fmt.Errorf("error while opening dir: %s", err) } var allFiles []string @@ -22,19 +22,19 @@ func (config Config) Encode(path string) error { } animated := gif.GIF{ - LoopCount: len(allFiles), + LoopCount: 0, } for _, file := range allFiles { reader, err := os.Open(path + file) if err != nil { - return fmt.Errorf("error while opening file: %s", err) + return nil, fmt.Errorf("error while opening file: %s", err) } defer reader.Close() img, err := png.Decode(reader) if err != nil { - return fmt.Errorf("error while decoding image: %s", err) + return nil, fmt.Errorf("error while decoding image: %s", err) } bounds := img.Bounds() drawer := draw.FloydSteinberg @@ -46,20 +46,5 @@ func (config Config) Encode(path string) error { animated.Delay = append(animated.Delay, config.Delay) } - if config.Output.Name == "" { - config.Output.Name = "final" - } - - file, err := os.Create(fmt.Sprintf("%s%s%s", config.Output.Path, config.Output.Name, ".gif")) - if err != nil { - return fmt.Errorf("error while creating file: %s", err) - } - defer file.Close() - - encodeErr := gif.EncodeAll(file, &animated) - if encodeErr != nil { - return fmt.Errorf("error while encoding file: %s", err) - } - - return nil + return &animated, nil } diff --git a/test/sword_test.gif b/test/sword_test.gif new file mode 100644 index 0000000..81a3ae2 Binary files /dev/null and b/test/sword_test.gif differ diff --git a/test/test.go b/test/test.go index 1ea8d30..760d413 100644 --- a/test/test.go +++ b/test/test.go @@ -2,26 +2,53 @@ package main import ( "fmt" + "image/gif" + "image/png" + "os" "github.com/Nota30/gifenc" ) func main() { init := gifenc.Config{ - Output: gifenc.Output{ - Name: "sword1", - Path: "test/output/", - }, - Delay: 20, + Delay: 30, } - err := init.Decode("test/input/sword.gif") + // DECODE + imgs, err := init.Decode("test/input/sword.gif") if err != nil { fmt.Print(err) } - // err := init.Encode("test/output/") - // if err != nil { - // fmt.Print(err) - // } + for i, img := range imgs { + file, err := os.Create(fmt.Sprintf("%s%s%d%s", "test/output/", "sword", i, ".png")) + if err != nil { + fmt.Print(err) + } + + err = png.Encode(file, img) + if err != nil { + fmt.Print(err) + } + + file.Close() + } + + // ENCODE + encoded, err := init.Encode("test/output/") + if err != nil { + fmt.Print(err) + } + + newfile, err := os.Create(fmt.Sprintf("%s%s", "test/", "sword_test.gif")) + if err != nil { + fmt.Print(err) + } + defer newfile.Close() + + encodeErr := gif.EncodeAll(newfile, encoded) + + if encodeErr != nil { + fmt.Print(err) + } } diff --git a/types.go b/types.go index 8df41ff..373dd9c 100644 --- a/types.go +++ b/types.go @@ -1,12 +1,6 @@ package gifenc -type Output struct { - Name string - Path string -} - type Config struct { - Output Output Delay int // encode delay Width int // decode width Height int // decode height