Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified build/cows.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/tmck-code/pokesay

go 1.20
go 1.23

require (
github.com/fatih/color v1.18.0
Expand Down
41 changes: 39 additions & 2 deletions src/bin/convert/png_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/tmck-code/pokesay/src/bin"
"github.com/tmck-code/pokesay/src/pokedex"
Expand Down Expand Up @@ -52,9 +54,44 @@ func main() {

fmt.Println("Converting PNGs -> cowfiles")
pbar := bin.NewProgressBar(len(fpaths))

allData := make([]string, 0, len(fpaths))
nDuplicates, nFailures := 0, 0

for _, f := range fpaths {
pokedex.ConvertPngToCow(args.FromDir, f, args.ToDir, args.Padding)
data, err := pokedex.ConvertPngToCow(args.FromDir, f, args.ToDir, args.Padding)
if err != nil {
nFailures++
continue
}

// check if this cawfile is a duplicate of one that has already been written
found := false
for _, existingData := range allData {
if existingData == data {
found = true
break
}
}
if found {
fmt.Println("Skipping duplicate data for", f)
nDuplicates++
pbar.Add(1)
continue
}
allData = append(allData, data)

destDirpath := filepath.Join(
args.ToDir,
// strip the root "source dirpath" from the source path
// e.g. fpath: /a/b/c.txt sourceDir: /a/ -> b/c.txt
filepath.Dir(strings.ReplaceAll(f, args.FromDir, "")),
)
destFpath := filepath.Join(destDirpath, strings.ReplaceAll(filepath.Base(f), ".png", ".cow"))

pokedex.WriteToCowfile(data, destDirpath, destFpath)
pbar.Add(1)
}
fmt.Println("Finished converting", len(fpaths), "pokesprite PNGs", "-> cowfiles")
fmt.Println("Finished converting", len(fpaths), "pokesprite PNGs -> cowfiles")
fmt.Println("(skipped", nDuplicates, "duplicates and", nFailures, "failures)")
}
28 changes: 12 additions & 16 deletions src/pokedex/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,7 @@ func padLeft(cowfile []byte, n int) []string {
return converted
}

func ConvertPngToCow(sourceDirpath string, sourceFpath string, destDirpath string, extraPadding int) {
destDir := filepath.Join(
destDirpath,
// strip the root "source dirpath" from the source path
// e.g. fpath: /a/b/c.txt sourceDir: /a/ -> b/c.txt
filepath.Dir(strings.ReplaceAll(sourceFpath, sourceDirpath, "")),
)
// Ensure that the destination dir exists
os.MkdirAll(destDir, 0755)
func ConvertPngToCow(sourceDirpath string, sourceFpath string, destDirpath string, extraPadding int) (string, error) {

// Trim the whitespace from the edges of the images. This helps with the conversion
autoCrop(sourceFpath)
Expand All @@ -125,20 +117,24 @@ func ConvertPngToCow(sourceDirpath string, sourceFpath string, destDirpath strin

if len(converted) == 0 {
failures = append(failures, sourceFpath)
return
return "", fmt.Errorf("failed to convert %s", sourceFpath)
}
final := stripEmptyLines(padLeft(converted, extraPadding))
return strings.Join(final, "\n") + COLOUR_RESET, nil
}

func WriteToCowfile(data string, destDirpath string, destFpath string) {
// Ensure that the destination dir exists
os.MkdirAll(destDirpath, 0755)

destFpath := filepath.Join(destDir, strings.ReplaceAll(filepath.Base(sourceFpath), ".png", ".cow"))
ostream, err := os.Create(destFpath)
Check(err)
defer ostream.Close()
writer := bufio.NewWriter(ostream)

final := stripEmptyLines(padLeft(converted, extraPadding))

// Join all of the lines back together, add colour reset sequence at the end
_, err = writer.WriteString(strings.Join(final, "\n") + COLOUR_RESET)
_, err = writer.WriteString(data)
Check(err)

writer.Flush()
err = writer.Flush()
Check(err)
}
Loading