pixelfloot/pixelfloot_bash.sh

339 lines
7.5 KiB
Bash
Raw Normal View History

2023-12-30 10:22:50 +01:00
#!/bin/bash
2024-01-01 05:44:55 +01:00
#IPFLOOT="151.217.15.90"
IPFLOOT="192.168.254.7"
2024-01-01 07:42:04 +01:00
FLOOTPORT="1337"
########################################################################
FNAME="$(echo $2 | sed -e 's/\..*$//' -e 's/^images\///')"
IMGFILE="$2"
PPMFILE="$FNAME.ppm"
HEXPPM="images/$FNAME.hexppm"
PIXLIST="/tmp/$FNAME.pixlist"
SHUFMODE="$3"
2023-12-30 10:22:50 +01:00
2024-01-02 02:43:54 +01:00
FLOOTSRUNNING=0
test -z "$FLOOTFORKS" && FLOOTFORKS="2"
2024-01-01 07:42:04 +01:00
2023-12-30 10:22:50 +01:00
declare -a PIXMAP
declare -a LOL
declare -a LOLPID
# https://gist.github.com/nberlette/e3e303a81f2c41927bf4fe90fb89d97f
function hex() {
printf "%02X%02X%02X" ${*//','/' '}
}
2024-01-02 02:43:54 +01:00
###### OLDFOOO ######
2023-12-30 10:22:50 +01:00
gen_pixmap() {
y=0
while read -r LINE
do
if [ "$y" -gt 2 ]
then
for x in $(seq 1 $x_ppm)
do
REDindex=$((x*3-2))
GREENindex=$((x*3+1-2))
BLUEindex=$((x*3+2-2))
REDvalue="$(echo $LINE| cut -d ' ' -f${REDindex})"
GREENvalue="$(echo $LINE| cut -d ' ' -f${GREENindex})"
BLUEvalue="$(echo $LINE| cut -d ' ' -f${BLUEindex})"
PIXELcolor="$(hex ${REDvalue} $GREENvalue $BLUEvalue)"
PIXMAP[$y]="${PIXMAP[$y]} $PIXELcolor"
done
echo "please wait"
echo ${PIXMAP[$y]} >> $HEXPPM
elif [ "$y" -eq 1 ]
then
x_ppm="$(echo "$LINE"|cut -d ' ' -f1)"
fi
y=$((y+1))
#~ for col in $LINE
#~ do
#~ case $count in
#~ 0) RED="$(hex $col)"
#~ ;;
#~ 1) GREEN="$(hex $col)"
#~ ;;
#~ 2) BLUE="$(hex $col)"
#~ ;;
#~ esac
#~ count=$((count+1))
#~ test $count -ge 2 && count=0
#~ done
done < "$PPMFILE"
}
draw_pixmap() {
y=1
2024-01-01 05:44:55 +01:00
test -z $x_ppm && echo "x_ppm missing" && exit 1
2023-12-30 10:22:50 +01:00
while read -r LINE
do
2024-01-01 05:44:55 +01:00
for x in $(seq 1 $x_ppm)
2023-12-30 10:22:50 +01:00
do
2024-01-01 05:44:55 +01:00
# when Color is FF00FE draw rainbow background
2023-12-30 11:37:49 +01:00
if [[ "$(echo $LINE | cut -d ' ' -f$x)" != "FF00FE" ]]
then
echo "please wait"
2024-01-01 05:44:55 +01:00
echo "PX $x $y $(echo $LINE | cut -d ' ' -f$x)" >> $PIXLIST
# magnify double size
#echo "PX $((x*2)) $((y*2)) $(echo $LINE | cut -d ' ' -f$x)" >> $PIXLIST
#echo "PX $((x*2+1)) $((y*2)) $(echo $LINE | cut -d ' ' -f$x)" >> $PIXLIST
#echo "PX $((x*2)) $((y*2+1)) $(echo $LINE | cut -d ' ' -f$x)" >> $PIXLIST
#echo "PX $((x*2+1)) $((y*2+1)) $(echo $LINE | cut -d ' ' -f$x)" >> $PIXLIST
2023-12-30 11:37:49 +01:00
else
2023-12-30 13:11:33 +01:00
2023-12-30 13:27:55 +01:00
if [ "$y" -lt 32 ]
2023-12-30 13:11:33 +01:00
then
RAINBOWCOLOR="$(hex 0 0 255)"
2023-12-30 13:27:55 +01:00
elif [ "$y" -lt 64 ]
2023-12-30 13:11:33 +01:00
then
RAINBOWCOLOR="$(hex 0 255 255)"
2023-12-30 13:27:55 +01:00
elif [ "$y" -lt 94 ]
2023-12-30 13:11:33 +01:00
then
RAINBOWCOLOR="$(hex 0 255 0)"
2023-12-30 13:27:55 +01:00
elif [ "$y" -lt 126 ]
2023-12-30 13:11:33 +01:00
then
RAINBOWCOLOR="$(hex 255 255 0)"
else
RAINBOWCOLOR="$(hex 255 0 0)"
fi
#~ if [ "$y" -lt 16 ]
#~ then
#~ RAINBOWCOLOR="$(hex 0 0 $(((y+1)*16)))"
#~ elif [ "$y" -lt 32 ]
#~ then
#~ RAINBOWCOLOR="$(hex 0 $(((y-16)*16)) 255)"
#~ elif [ "$y" -lt 48 ]
#~ then
#~ RAINBOWCOLOR="$(hex 0 $((y*16)) $((16-(y-32)*16)))"
#~ elif [ "$y" -lt 64 ]
#~ then
#~ RAINBOWCOLOR="$(hex $((y*16)) $((y*16)) 0)"
#~ else
#~ RAINBOWCOLOR="$(hex $((y*16)) 0 0)"
#~ fi
2023-12-30 11:37:49 +01:00
echo "please wait for rainbow"
echo "PX $((x*2)) $((y*2)) $RAINBOWCOLOR" >> $PIXLIST
echo "PX $((x*2+1)) $((y*2)) $RAINBOWCOLOR" >> $PIXLIST
echo "PX $((x*2)) $((y*2+1)) $RAINBOWCOLOR" >> $PIXLIST
echo "PX $((x*2+1)) $((y*2+1)) $RAINBOWCOLOR" >> $PIXLIST
fi
2023-12-30 10:22:50 +01:00
done
y=$((y+1))
done < "$HEXPPM"
}
2024-01-02 02:43:54 +01:00
###### END OLDFOOO ######
gen_field() {
test -z $W && W=640
test -z $H && H=480
test -z $COLOR && COLOR="666999"
echo "drawing $W x $H - $COLOR" >&2
2024-01-02 02:49:27 +01:00
for x in $(seq 0 $W)
2024-01-02 02:43:54 +01:00
do
2024-01-02 02:49:27 +01:00
for y in $(seq 0 $H)
2024-01-02 02:43:54 +01:00
do
echo "PX $x $y $COLOR"
done
done
}
convertimg() {
if [ -n "$RESIZE" ]
then
RESIZE="-resize $RESIZE"
fi
while read -r LINE
do
echo "PX $LINE"
done < <(convert $IMGFILE $RESIZE txt: | tail -n +2 | awk '{print $1 $3}' | sed -e 's/\,/ /' -e 's/\:/ /' -e 's/\#//')
}
2023-12-30 10:22:50 +01:00
sx=0
sy=0
shuf_xy() {
2024-01-01 07:42:04 +01:00
2023-12-30 21:55:03 +01:00
case $SHUFMODE in
2024-01-01 07:42:04 +01:00
chaos) test -z $H && H=640
test -z $W && W=480
echo "OFFSET $(shuf -i 0-$W -n 1) $(shuf -i 0-$H -n 1)"
2023-12-30 21:55:03 +01:00
;;
2024-01-01 07:42:04 +01:00
shake) test -z $H && H=1
test -z $W && W=1
echo "OFFSET $(shuf -i $W-$(($W+10)) -n 1) $(shuf -i $H-$(($H+10)) -n 1)"
2023-12-30 21:55:03 +01:00
;;
2024-01-01 07:42:04 +01:00
static) echo "OFFSET $W $H"
2023-12-30 21:55:03 +01:00
;;
2024-01-01 04:32:22 +01:00
cursor) echo "OFFSET $(xdotool getmouselocation | tr ':' ' '|awk '{print $2 " " $4}')"
;;
2023-12-30 21:55:03 +01:00
2024-01-01 05:44:55 +01:00
*) echo "OFFSET 0 0"
2023-12-30 21:55:03 +01:00
;;
esac
#
#
2023-12-30 10:22:50 +01:00
#echo "OFFSET $(shuf -i 0-1760 -n 1) 919"
2023-12-30 21:55:03 +01:00
2023-12-30 10:22:50 +01:00
#echo > /dev/null
#~ echo "OFFSET $sx $sy"
#~ sx=$((sx+1))
#~ sy=$((sy+1))
#~ test $sx -gt 1760 && sx=0
#~ test $sy -gt 920 && sy=0
}
flootworker()
{
while true
do
2024-01-02 02:43:54 +01:00
#FLOOTSRUNNING=$((FLOOTSRUNNING+1))
#test $FLOOTSRUNNING -le $FLOOTFORKS &&
echo "$(shuf_xy)
${LOL[$i]}" > /dev/tcp/$IPFLOOT/1337
2024-01-02 02:43:54 +01:00
#FLOOTSRUNNING=$((FLOOTSRUNNING-1))
#echo "${LOL[$i]}" > /dev/tcp/127.0.0.1/1337 &
#echo "worker $i PID ${LOLPID[$i]}"
done
}
2023-12-30 10:22:50 +01:00
floot() {
2024-01-01 07:42:04 +01:00
# small stupid animation, two alternating images
2023-12-30 17:21:30 +01:00
if [ "$FNAME" == "winketuxS" ]
then
echo "drawing winketuxS animation"
LOL[1]="$(cat pixlists/${FNAME}1.pixlist | shuf)"
LOL[2]="$(cat pixlists/${FNAME}2.pixlist | shuf )"
LOL[3]="$(cat pixlists/${FNAME}2.pixlist | shuf )"
2023-12-30 17:21:30 +01:00
#LOL[3]="$(cat $FNAME-mc.pixlist.2 | shuf)"
2024-01-01 05:44:55 +01:00
elif [ "$FNAME" == "fill" ]
then
echo "generating color field with $FLOOTFORKS workers"
2024-01-01 07:42:04 +01:00
LOL_org="$(gen_field)"
2024-01-01 05:44:55 +01:00
for i in $(seq 1 $FLOOTFORKS)
do
2024-01-01 07:42:04 +01:00
LOL[$i]="$LOL_org"
2024-01-01 05:44:55 +01:00
done
2023-12-30 17:21:30 +01:00
else
# generate a tmp file, as i have trouble atm to figure out
# why free space get lost when i generate the pixlist directly
# in ram
2024-01-02 02:43:54 +01:00
echo "generating tmp file $PIXLIST"
convertimg > $PIXLIST
2024-01-02 02:43:54 +01:00
echo "shuffle pixels in $PIXLIST for $FLOOTFORKS workers"
2024-01-01 05:44:55 +01:00
for i in $(seq 1 $FLOOTFORKS)
2023-12-30 17:21:30 +01:00
do
#LOL[$i]="OFFSET 1 200"
#LOL[$i]="OFFSET $(shuf -i 0-1760 -n 1) $(shuf -i 0-920 -n 1)"
# LOL[$i]="$(shuf_xy)"
#LOL[$i]="$(cat $PIXLIST | shuf)"
2024-01-02 02:43:54 +01:00
echo -n "prepare worker $i .."
2023-12-30 17:21:30 +01:00
if [ -z "$ALPHACOLOR" ]
then
LOL[$i]="$(cat $PIXLIST | shuf)"
#LOL[$i]="$(convertimg | shuf)"
else
LOL[$i]="$(grep -v $ALPHACOLOR $PIXLIST | shuf)"
#LOL[$i]="$(convertimg | grep -v $ALPHACOLOR | shuf)"
fi
echo " DONE!"
2023-12-30 17:21:30 +01:00
done
fi
2023-12-30 11:37:49 +01:00
2024-01-02 02:43:54 +01:00
echo "starting $FLOOTFORKS workers"
while true
do
for i in $(seq $FLOOTFORKS)
do
#echo "check worker $i PID ${LOLPID[$i]} if running "
if [ -z ${LOLPID[$i]} ] || ! ps -p ${LOLPID[$i]} > /dev/null
then
2024-01-02 02:43:54 +01:00
echo "worker $i is not running, starting it"
#if [ "$FLOOTSRUNNING" -le "$FLOOTFORKS" ]
#then
flootworker &
LOLPID[$i]=$!
#fi
fi
done
done
2023-12-30 10:22:50 +01:00
}
case $1 in
2024-01-02 03:24:52 +01:00
### DEPRECATED - JUST FOR TESTING
2023-12-30 10:22:50 +01:00
draw_pixmap) draw_pixmap
;;
gen_pixmap) gen_pixmap
;;
2024-01-02 03:24:52 +01:00
### END DEPRECATED - JUST FOR TESTING
2024-01-01 05:44:55 +01:00
convertimg)
2024-01-02 03:24:52 +01:00
# old way
##gen_pixmap
##draw_pixmap
2024-01-02 03:24:52 +01:00
# convert arbeitsplatz.jpg txt: | tail -n +2 | awk '{print $1 $3}'
# this is probably better
convertimg > $PIXLIST
echo "file $PIXLIST" generated
2024-01-01 05:44:55 +01:00
;;
2024-01-01 07:42:04 +01:00
floot) if [ "$SHUFMODE" == "static" ] && ([ -z "$W" ] && [ -z "$H" ])
then
echo "please specify coords with e.g. 'W=420 H=420 SHUFMODE=static $0 floot $FNAME" >&2
exit 1
fi
floot
2023-12-30 10:22:50 +01:00
;;
*)
2024-01-02 03:24:52 +01:00
echo "$0 [floot|convertimg] [FILENAME|fill] ([MODE])"
echo "MODE: static (env \$H and \$W for position)"
echo " chaos (env \$H and \$W for position range)"
echo " shake (env \$H and \$W for position range)"
echo " cursor"
echo ""
echo "available env vars to configure:"
2024-01-02 03:22:42 +01:00
echo "RESIZE(int), ALPHACOLOR(hex), FLOOTFORKS(int), H(int), W(int)"
exit 1
2023-12-30 10:22:50 +01:00
;;
esac