summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaoran S. Diao (刁浩然) <0@hairydiode.xyz>2023-06-30 01:25:17 -0700
committerHaoran S. Diao (刁浩然) <0@hairydiode.xyz>2023-06-30 01:25:17 -0700
commit703ece1f7dc40975b69e1ef52367e86e6561474b (patch)
tree2873df789bcd6eccf2ebf79c33fbbc892c58e900
Initial Commit
-rw-r--r--INPUT22
-rw-r--r--INPUT28
-rw-r--r--README28
-rwxr-xr-xbbrll117
4 files changed, 175 insertions, 0 deletions
diff --git a/INPUT b/INPUT
new file mode 100644
index 0000000..e3c39c7
--- /dev/null
+++ b/INPUT
@@ -0,0 +1,22 @@
+LOAD_RAW
+.11.
+1..1
+1..1
+1..1
+..AA00
+......
+..zz11
+..xxAB
+END_RAW
+LOAD_POINT
+0
+0
+.
+LOAD_POINT
+2
+5
+X
+PRINT_RAW
+PRINT_BRAILLE
+END
+
diff --git a/INPUT2 b/INPUT2
new file mode 100644
index 0000000..7fe4f8c
--- /dev/null
+++ b/INPUT2
@@ -0,0 +1,8 @@
+LOAD_RAW
+1...1..1
+111...
+11.
+END_RAW
+PRINT_RAW
+PRINT_BRAILLE
+END
diff --git a/README b/README
new file mode 100644
index 0000000..96da413
--- /dev/null
+++ b/README
@@ -0,0 +1,28 @@
+Simple bash script for drawing graphics in the terminal, takes as input drawing
+commands, and will print either the raw image or the braille version of the
+image as an output
+
+begin:
+ CANVAS or
+ X then
+ Y
+ #makes a XxY canvas filled with ".", which is the character
+ #interpreted as a blank when converting to braille
+ LOAD_RAW or
+ FOOBAR repeat until
+ END_RAW
+ #loads the lines FOOBAR into the image buffer
+ LOAD_POINT or
+ X then
+ Y then
+ C
+ #loads the character C into position X,Y
+ PRINT_RAW or
+ #prints the raw image buffer
+ PRINT_BRAILLE or
+ #prints the output in braille, this uses only the width of the
+ #first row so make sure to call CANVAS or something before hand
+ END
+ #ends the script
+
+examples are in INPUT and INPUT2
diff --git a/bbrll b/bbrll
new file mode 100755
index 0000000..661fd14
--- /dev/null
+++ b/bbrll
@@ -0,0 +1,117 @@
+#!/bin/bash
+#NOTE, because bash arrays are just string delimited by space, we will not be
+#using space as our empty character but .
+rawbuff=()
+function load_raw () {
+ while read -r line; do
+ if [ "$line" = "END_RAW" ]; then
+ return
+ else
+ rawbuff=("${rawbuff[@]}" "$line")
+ fi
+ done
+}
+function load_point() {
+ read -r x
+ read -r y
+ read -r c
+ line=${rawbuff[$y]}
+ #construct a sed prompt that replaces the xth character
+ prompt="s/^\(.\{$x\}\).\(.*\)$/\1$c\2/"
+ rawbuff[$y]=$(echo "$line" | sed "$prompt")
+}
+function print_raw () {
+ for line in "${rawbuff[@]}"; do
+ echo "$line"
+ done
+}
+function canvas() {
+ read -r w
+ read -r h
+ ln=""
+ i=0
+ while [ $i -lt $w ];do
+ ln="$ln."
+ i=$(($i + 1))
+ done
+ j=0
+ while [ $j -lt $h ]; do
+ rawbuff=("${rawbuff[@]}" "$ln")
+ j=$(($j+1))
+ done
+}
+
+#prints a single braille at $1=x $2=y
+function print_sin_braille() {
+#bit to braille
+#
+#0 3
+#1 4
+#2 5
+#6 7
+#reverse
+#(1,3)(0,3)(1,2)(1,1)(1,0)(0,2)(0,1)(0,0)
+ #converts anything other than ., and empty string, or space into 1, else
+ #0
+ prompt='s/[^\. ]/1/;s/^[^1]*$/0/'
+
+ b0=$( echo ${rawbuff[((0+4*$2))]:((0+2*$1)):1} | sed "$prompt" )
+ b1=$( echo ${rawbuff[((1+4*$2))]:((0+2*$1)):1} | sed "$prompt" )
+ b2=$( echo ${rawbuff[((2+4*$2))]:((0+2*$1)):1} | sed "$prompt" )
+ b3=$( echo ${rawbuff[((0+4*$2))]:((1+2*$1)):1} | sed "$prompt" )
+ b4=$( echo ${rawbuff[((1+4*$2))]:((1+2*$1)):1} | sed "$prompt" )
+ b5=$( echo ${rawbuff[((2+4*$2))]:((1+2*$1)):1} | sed "$prompt" )
+ b6=$( echo ${rawbuff[((3+4*$2))]:((0+2*$1)):1} | sed "$prompt" )
+ b7=$( echo ${rawbuff[((3+4*$2))]:((1+2*$1)):1} | sed "$prompt" )
+ #echo "2#$b7$b600$b5$b4$b3$b2$b1$b0"
+ #the two spaces assume aht the encoding is utf-8, convoluted bitwise OR
+ #and coversion between character and number stuff
+ echo "obase=16;$(( 2#$b7${b6}00$b5$b4$b3$b2$b1$b0 | 0x$(echo -n ⠀ |xxd -p -u)))" |bc | xxd --reverse -ps
+}
+
+function print_braille() {
+ #I do the lazy thing and just test the first line
+ w=${#rawbuff[0]}
+ h=${#rawbuff[@]}
+ #conver to braille coords, since bash rounds down, account for when
+ #there a braille character that will not be completely filled(it can
+ #handle this fine because of how the sed pattern was constructed in
+ #print_sin_braille)
+ if [ $((w % 2)) -gt 0 ]; then
+ w=$((w/2+1))
+ else
+ w=$((w/2))
+ fi
+ if [ $((h % 4)) -gt 0 ]; then
+ h=$((h/4+1))
+ else
+ h=$((h/4))
+ fi
+ i=0
+ j=0
+ while [ $i -lt $h ]; do
+ j=0
+ while [ $j -lt $w ]; do
+ print_sin_braille $j $i
+ j=$(($j+1))
+ done
+ #just want a new line
+ echo
+ i=$(($i+1))
+ done
+}
+while read -r line; do
+ if [ "$line" = "LOAD_RAW" ]; then
+ load_raw
+ elif [ "$line" = "LOAD_POINT" ]; then
+ load_point
+ elif [ "$line" = "PRINT_RAW" ]; then
+ print_raw
+ elif [ "$line" = "CANVAS" ]; then
+ canvas
+ elif [ "$line" = "PRINT_BRAILLE" ]; then
+ print_braille
+ elif [ "$line" = "END" ]; then
+ exit
+ fi
+done