diff --git a/encode.go b/encode.go index 15e1386..353e21d 100644 --- a/encode.go +++ b/encode.go @@ -1,41 +1,18 @@ package gifenc import ( - "fmt" "image" "image/color/palette" "image/draw" "image/gif" - "image/png" - "os" ) -func (config Config) Encode(path string) (*gif.GIF, error) { - files, err := os.ReadDir(path) - if err != nil { - return nil, fmt.Errorf("error while opening dir: %s", err) - } - - var allFiles []string - for _, file := range files { - allFiles = append(allFiles, file.Name()) - } - +func (config Config) Encode(images []image.Image) (*gif.GIF, error) { animated := gif.GIF{ LoopCount: 0, } - for _, file := range allFiles { - reader, err := os.Open(path + file) - if err != nil { - return nil, fmt.Errorf("error while opening file: %s", err) - } - defer reader.Close() - - img, err := png.Decode(reader) - if err != nil { - return nil, fmt.Errorf("error while decoding image: %s", err) - } + for _, img := range images { bounds := img.Bounds() drawer := draw.FloydSteinberg diff --git a/test/test.go b/test/test.go index 15da41c..23a8014 100644 --- a/test/test.go +++ b/test/test.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "image" "image/gif" "image/png" "os" @@ -19,7 +20,10 @@ func encode() { init := gifenc.Config{ Delay: 30, } - encoded, err := init.Encode("test/output/") + + images := getImages() + + encoded, err := init.Encode(images) if err != nil { fmt.Print(err) } @@ -58,6 +62,10 @@ func decode() { fmt.Print(err) } + saveImages(imgs) +} + +func saveImages(imgs []*image.RGBA) { for i, img := range imgs { file, err := os.Create(fmt.Sprintf("%s%s%d%s", "test/output/", "sword", i, ".png")) if err != nil { @@ -72,3 +80,34 @@ func decode() { file.Close() } } + +// Get all the images inside a directory +func getImages() []image.Image { + var images []image.Image + files, err := os.ReadDir("test/output/") + if err != nil { + fmt.Print(err) + } + + var allFiles []string + for _, file := range files { + allFiles = append(allFiles, file.Name()) + } + + for _, file := range allFiles { + reader, err := os.Open("test/output/" + file) + if err != nil { + fmt.Print(err) + } + defer reader.Close() + + img, err := png.Decode(reader) + if err != nil { + fmt.Print(err) + } + + images = append(images, img) + } + + return images +}