Skip to content

Commit

Permalink
remove file create & types
Browse files Browse the repository at this point in the history
  • Loading branch information
Nota30 committed Feb 11, 2024
1 parent 285922e commit bf0050f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 46 deletions.
20 changes: 11 additions & 9 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
27 changes: 6 additions & 21 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Binary file added test/sword_test.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 37 additions & 10 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
6 changes: 0 additions & 6 deletions types.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit bf0050f

Please sign in to comment.