Skip to content

Instantly share code, notes, and snippets.

@glongman
Last active August 29, 2015 14:06
Show Gist options
  • Save glongman/c3ccaee88d56421689d0 to your computer and use it in GitHub Desktop.
Save glongman/c3ccaee88d56421689d0 to your computer and use it in GitHub Desktop.
Bash Version of Game Frame script
#!/usr/bin/env bash
# gfx2gf.bat v1.5 (20140823)
# by Jeremy Williams
#
# This batch file converts graphics for Game Frame compatibility (www.ledseq.com).
# *** You must have ImageMagick installed for it to run (www.imagemagick.org) ***
# Passed graphic files will be stretched, resized, & resampled if larger than 16x16. If
# the file is an animated GIF, a filmstrip graphic will be generated along with
# a CONFIG.INI using a "hold" value (framerate) equal to the first frame of the
# animation. Usually this works well, but you may still need to adjust the hold
# value if the animation plays too slow/fast.
#
# Notes by GWL - (20140912)
#
# To install ImageMagick on OSX, first get Homebrew (http://brew.sh)
# then:
# > brew install imagemagick
# > brew install ffmpeg
# > brew install gs
#
# To run this script it must be given executable permission
#
# > chmod 755 gfx2gf
#
# Then invoke with path to input image or video
#
# > ./gfx2gf /home/me/Downloads/my_cool_animated.gif
#
set -e
# set -x # debugging
function cleanup {
if [ -z "$TMP_FOLDER" ] ; then
if [ -d "${TMP_FOLDER}" ] ; then
rm -rf "${TMP_FOLDER}"
fi
fi
}
function error_exit {
cleanup
exit 1
}
if [[ $# -eq 0 ]] ; then
echo
echo Need input! Drag a graphic onto the .sh file,
echo or run it from a command prompt as:
echo
echo gfx2gf yourfile.gif
error_exit
fi
# simple check for ImageMagick installation
function check_installation {
hash "$1" &> /dev/null ;
}
if ! check_installation convert ; then
echo
echo "This program requires ImageMagick (www.imagemagick.org)"
echo "to be installed on the system. Make it so."
error_exit
fi ;
if ! check_installation ffmpeg ; then
echo
echo "This program requires ffmpeg (https://www.ffmpeg.org/)"
echo "to be installed on the system. Make it so."
error_exit
fi ;
# make folders
GRAPHIC=$1
FOLDER=${GRAPHIC%.*}
PREVIEW_NAME="$(basename $FOLDER)_preview.gif"
TMP_FOLDER=${FOLDER}/tmp
mkdir -p "$TMP_FOLDER"
echo $FOLDER
echo $TMP_FOLDER
# Helper Functions
function make_preview {
echo Generating preview...
if (( "$SCENES" <= 1 )) ; then
convert -filter box -resize 128x128 "$TMP_FOLDER\*.bmp" "$FOLDER/$PREVIEW_NAME"
fi
let "DELAY = HOLD>0?(HOLD/10):HOLD"
if (( "$SCENES" >= 2 )) ; then
if (( "$SCENES" <= 250 )) ; then
convert -delay $DELAY -filter box -resize 128x128 "$TMP_FOLDER/*.bmp" "$FOLDER/$PREVIEW_NAME"
else
echo Long animation, only previewing the first 250 frames...
convert -delay $DELAY -filter box -resize 128x128 "$TMP_FOLDER/%06d.bmp[1-250]" "$FOLDER/$PREVIEW_NAME"
fi
fi
}
# combine multiple files into one long filmstrip
function filmstrip {
echo Creating filmstrip...
montage "$TMP_FOLDER/*.bmp" -mode concatenate -tile 1x -type truecolor "$FOLDER/0.bmp"
echo Filmstrip conversion finished!
}
function fetch_hold {
# store the delay used for the first frame of animation in the animated GIF
HOLD=$(identify -format "%T" "$GRAPHIC[0]")
# GF frame rate bottoms out around 40
if (( "$HOLD" < 4 )) ; then
HOLD=4
fi
let "HOLD *=10"
}
function make_ini {
cd "$FOLDER"
# write config.ini using stored delay value
echo "# All of these settings are optional." >> config.ini
echo "# If this file doesnít exist, or if" >> config.ini
echo "# settings donít appear in this file," >> config.ini
echo "# Game Frame will use default values." >> config.ini
echo >> config.ini
echo "[animation]" >> config.ini
echo >> config.ini
echo "# milliseconds to hold each frame" >> config.ini
echo "# (1000 = 1 sec; lower is faster)" >> config.ini
echo "hold = $HOLD" >> config.ini
echo >> config.ini
echo "# should the animation loop? If false," >> config.ini
echo "# system will progress to next folder" >> config.ini
echo "# after the last frame is displayed." >> config.ini
echo "loop = true" >> config.ini
echo >> config.ini
echo "[translate]" >> config.ini
echo >> config.ini
echo "# move the animation across the screen" >> config.ini
echo "# this many pixels per frame. Experiment" >> config.ini
echo "# with positive and negative numbers for" >> config.ini
echo "# different directions." >> config.ini
echo "moveX = 0" >> config.ini
echo "moveY = 16" >> config.ini
echo >> config.ini
echo "# should the movement loop?" >> config.ini
if (( NESTED == -1 )) ; then
echo "loop = true" >> config.ini
else
echo "loop = false" >> config.ini
fi
echo >> config.ini
echo "# begin/end scroll off screen?" >> config.ini
echo "panoff = false" >> config.ini
echo >> config.ini
echo "# optionally dictate the next animation" >> config.ini
echo "# EXAMPLE: nextFolder = mspacman" >> config.ini
echo "# nextFolder = defend1" >> config.ini
cd - > /dev/null
echo CONFIG.INI Written!
}
function build_nest {
convert -crop 16x32000x0x0 "$FOLDER/0.bmp" "$FOLDER/%01d.bmp"
STRIPS=$(ls "$FOLDER/*.bmp" | wc -l)
echo $STRIPS filmstrips created.
CURRENTNEST=0
while (( "$CURRENTNEST" < "$STIPS" )) ; do
echo Making nested folder $CURRENTNEST
mkdir "$FOLDER/$CURRENT_NEST"
mv "$FOLDER/$CURRENT_NEST.bmp" "$FOLDER/$CURRENT_NEST"
cp "$FOLDER/config.ini" "$FOLDER/$CURRENT_NEST"
done
rm "$FOLDER/config.ini"
echo Nesting folders complete.
}
# End of Helper Functions
# skipping source resolution
# convert to BMP and extract animated GIF into seperate files
echo Converting $(basename ${GRAPHIC}) to Game Frame format...
echo -----
ffmpeg -r 1 -i ${GRAPHIC} -r 1 -f image2 -pix_fmt bgr24 -s 16x16 "$TMP_FOLDER/%06d.bmp"
echo -----
# count the number of frames
SCENES=$(ls "$TMP_FOLDER" | wc -l)
echo $SCENES 16x16 frames created.
if (( "$SCENES" > 196000 )) ; then
echo File has more than 196,000 frames. This might not work.
fi
HOLD=0
if (( "$SCENES" == 1 )) ; then
montage "$TMP_FOLDER/*.bmp" -mode concatenate -tile 1x -type truecolor "$FOLDER/0.bmp"
echo Graphic conversion finished!
echo File is not animated. Bypassing CONFIG.INI.
make_preview
else
# combine multiple files into one long filmstrip
echo Creating filmstrip...
montage "$FOLDER/tmp/*.bmp" -mode concatenate -tile 1x -type truecolor "$FOLDER/0.bmp"
echo Filmstrip conversion finished!
fetch_hold
echo hold value: $HOLD
let "NESTED = SCENES<1999?-1:1" # can't evaluate to 0 or script exits!
make_preview
make_ini
cleanup
if (( NESTED == 1 )) ; then
echo Long video detected.. nesting folders
build_nest
echo Nesting folders complete
fi
fi
echo Process finished!
@glongman
Copy link
Author

This is sorta working but far from fully debugged. better off using the script described here: http://ledseq.com/forums/topic/graphic-conversion-tool-gfx2gf/page/2/#post-1429

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment