Created
February 29, 2020 21:26
-
-
Save chigozienri/5024c98541ea37513fda6224972294e0 to your computer and use it in GitHub Desktop.
Bash 1 bit image viewer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# bash 1 bit image viewer | |
# Usage: ./readbinary.sh filename linelength | |
# Try echo '\x07\xe0\x1c\x38\x32\x4c\x30\x0c\x33\xcc\x18\x18\x07\xe0' > onebitimage; ./readbinary.sh onebitimage 16 | |
# `xxd -b` gets binary dump of file, | |
# first `awk` matches part after line number, | |
# second `awk` strips ascii interpretation, | |
# `tr` strips space, period, linebreak | |
binary=$(xxd -b $1 | awk 'match($0, /.+:/) {print substr($0, RLENGTH+1)}' | awk 'match($0, / /) {print substr($0, 0, RSTART)}'| tr -d ' .\n') | |
i=0 | |
# `grep` splits each character on separate line, | |
# `read` iterates over lines | |
grep -o . <<< $binary | while read -r character; do | |
# write '.' for 1 | |
if [ "$character" == '1' ]; then | |
echo -ne "m" | |
# write ' ' for 0 | |
else | |
echo -ne " " | |
fi | |
# increment counter | |
i=$((i+1)) | |
# linebreak every nth character | |
if [ $((i%$2)) == '0' ]; then | |
echo '' | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment