diff --git a/build/Dockerfile b/build/Dockerfile index 02e6fc8c..800bedaf 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -4,7 +4,7 @@ WORKDIR /usr/local/src RUN BUILD_DEPS="make gcc" RUN apt-get update \ - && apt-get install -y --no-install-recommends "${BUILD_DEPS}" libmagickwand-dev ncurses-dev jq + && apt-get install -y --no-install-recommends "${BUILD_DEPS}" libmagickwand-dev ncurses-dev jq imagemagick RUN git clone --depth 1 https://github.com/denilsonsa/img2xterm \ && (cd img2xterm && make && make install) \ @@ -31,8 +31,5 @@ RUN go run /usr/local/src/src/bin/convert/png_convert.go \ && mv -v /tmp/cows/pokemon-gen8 /tmp/cows/gen8 \ && mv -v /tmp/cows/pokemon-gen7x /tmp/cows/gen7x \ && cat /tmp/original/pokesprite/data/pokemon.json | jq -c .[] > /tmp/cows/pokemon.json \ - && rm -rf /tmp/original/pokesprite \ - && find . ! -empty -type f -exec md5sum {} + | sort -r | uniq -w32 -dD > dup \ - && for i in $(cut -d' ' -f1 build/cows/dup | uniq); do grep $i dup | tail -n +2 | cut -d ' ' -f3 ; done | parallel echo rm -v - + && rm -rf /tmp/original/pokesprite ADD . . diff --git a/src/pokedex/convert.go b/src/pokedex/convert.go index 45b439d3..41d2b798 100644 --- a/src/pokedex/convert.go +++ b/src/pokedex/convert.go @@ -31,10 +31,22 @@ func FindFiles(dirpath string, ext string, skip []string) []string { return fpaths } +// img2xterm converts an image to a cowfile, returning the result as a byte slice func img2xterm(sourceFpath string) ([]byte, error) { return exec.Command("bash", "-c", fmt.Sprintf("/usr/local/bin/img2xterm %s", sourceFpath)).Output() } +// autoCrop trims the whitespace from the edges of an image, in place +func autoCrop(sourceFpath string) { + destFpath := fmt.Sprintf("/tmp/%s", filepath.Base(sourceFpath)) + _, err := exec.Command( + "bash", "-c", fmt.Sprintf("/usr/bin/convert %s -trim +repage %s", sourceFpath, destFpath), + ).Output() + Check(err) + + os.Rename(destFpath, sourceFpath) +} + func countLineLeftPadding(line string) int { count := 0 for _, ch := range line { @@ -64,27 +76,37 @@ func countCowfileLeftPadding(cowfile []byte) int { return minPadding } -func stripPadding(cowfile []byte, n int) []string { +func stripEmptyLines(cowfile []string) []string { converted := make([]string, 0) - lines := strings.Split(string(cowfile), "\n") - for _, line := range lines { + for _, line := range cowfile { if len(line) == 0 { continue } - convertedLine := "" - for i, ch := range line { - if i >= n { - convertedLine += string(ch) + onlySpaces := true + for _, ch := range line { + if ch != ' ' { + onlySpaces = false + break } } - if len(convertedLine) > 0 { - converted = append(converted, convertedLine) + if !onlySpaces { + converted = append(converted, line) } } return converted } +func padLeft(cowfile []byte, n int) []string { + converted := make([]string, 0) + lines := strings.Split(string(cowfile), "\n") + + for _, line := range lines { + converted = append(converted, strings.Repeat(" ", n)+line) + } + return converted +} + func ConvertPngToCow(sourceDirpath string, sourceFpath string, destDirpath string, extraPadding int) { destDir := filepath.Join( destDirpath, @@ -95,6 +117,8 @@ func ConvertPngToCow(sourceDirpath string, sourceFpath string, destDirpath strin // Ensure that the destination dir exists os.MkdirAll(destDir, 0755) + // Trim the whitespace from the edges of the images. This helps with the conversion + autoCrop(sourceFpath) // Some conversions are failing with something about colour channels // Can't be bothered resolving atm, so just skip past any failed conversions converted, _ := img2xterm(sourceFpath) @@ -110,7 +134,7 @@ func ConvertPngToCow(sourceDirpath string, sourceFpath string, destDirpath strin defer ostream.Close() writer := bufio.NewWriter(ostream) - final := stripPadding(converted, countCowfileLeftPadding(converted)-extraPadding) + 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)