Skip to content

Instantly share code, notes, and snippets.

@mbikovitsky
Last active December 14, 2015 12:38
Show Gist options
  • Save mbikovitsky/5087902 to your computer and use it in GitHub Desktop.
Save mbikovitsky/5087902 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Copyright (c) 2013 Michael Bikovitsky
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
WIDTH=$1
HEIGHT=$2
IN_FILE=$3
OUT_SCRIPT=$4
draw=""
declare -i skip=0
echo
if test $IN_FILE != "-"
then
if test `wc -c < "$IN_FILE"` -lt `echo "$WIDTH*$HEIGHT*3" | bc`
then
echo "There is not enough data in the input file to generate the image."
echo "Either reduce the output resolution or choose another data source."
echo "For the chosen resolution you'll need an input file with at least $(echo "$WIDTH*$HEIGHT*3" | bc) byte(s) of data."
exit
else
echo "Filesize test passed."
echo
fi
fi
echo '#!/bin/bash' > $OUT_SCRIPT
echo >> $OUT_SCRIPT
echo 'OUT_IMAGE=$1' >> $OUT_SCRIPT
echo >> $OUT_SCRIPT
echo 'echo' >> $OUT_SCRIPT
echo >> $OUT_SCRIPT
echo 'echo Creating initial image...' >> $OUT_SCRIPT
echo convert -size "$WIDTH"x"$HEIGHT" xc:white '$OUT_IMAGE' >> $OUT_SCRIPT
echo >> $OUT_SCRIPT
for ((row=0;row<$HEIGHT;++row))
do
echo $row
for ((col=0;col<$WIDTH;++col))
do
echo -n $col" "
color="#$(od -vAn -N3 -tx1 -j"$skip" "$IN_FILE")"
color="${color//[[:space:]]}"
draw+="fill $color color $col,$row point "
if test $IN_FILE != "-"; then
skip+=3
fi
done
echo 'echo "Dumping row #'$row\" >> $OUT_SCRIPT
echo convert '$OUT_IMAGE' -draw \"$draw\" '$OUT_IMAGE' >> $OUT_SCRIPT
echo >> $OUT_SCRIPT
draw=""
echo
done
sed -i '$ d' $OUT_SCRIPT
chmod u+x $OUT_SCRIPT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment