Skip to content

Commit

Permalink
crop/trim the sprites before term conversion
Browse files Browse the repository at this point in the history
this greatly improves the colours! 🎉
  • Loading branch information
tmck-code committed Dec 4, 2024
1 parent e272c69 commit 0ae2a99
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
7 changes: 2 additions & 5 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand All @@ -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 . .
44 changes: 34 additions & 10 deletions src/pokedex/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 0ae2a99

Please sign in to comment.