Skip to content

Commit

Permalink
remove the file path for encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Nota30 committed Feb 11, 2024
1 parent fdb3930 commit 8107447
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
27 changes: 2 additions & 25 deletions encode.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
41 changes: 40 additions & 1 deletion test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"image"
"image/gif"
"image/png"
"os"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 8107447

Please sign in to comment.