-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixelfloot.sh
executable file
·570 lines (480 loc) · 15.4 KB
/
pixelfloot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#!/bin/bash
# pixelfloot - a pixelflut client written in bash
# work on this script started at 37c3
# pixelflut.la10cy.net - 130.185.104.31
# my small pixelflut test server, feel free to have fun with it! :)
# You can watch the board by VNC at port 5900, max connections are 2
test -z "$IPFLOOT" && IPFLOOT="130.185.104.31"
test -z "$FLOOTPORT" && FLOOTPORT="1234"
########################################################################
IMGFILE="$2"
# remove file extension
FNAME="${2%.*}"
# remove everything before the slash, so we get the filename without
# path and extension
FNAME="${FNAME##*/}"
PIXLIST="/tmp/$FNAME.pixlist"
ANIFILE="/tmp/$FNAME"
SHUFMODE="$3"
FLOOTSRUNNING=0
test -z "$FLOOTFORKS" && FLOOTFORKS="1"
test -z $X_MAX && X_MAX=800
test -z $Y_MAX && Y_MAX=600
test -z $X && X=0
test -z $Y && Y=0
## bounce
XDIR=0
YDIR=0
test -z "$BOUNCESTEP" && BOUNCESTEP=2
## end bounce
OFFSET_SHM="/dev/shm/pxlflt-offset-${FNAME}"
## ANIMATION
# convert -coalesce animation.gif target.png -> produces target1.png, target2.png, ..
# /dev/shm/foo to store frame counter
#
# GifFileFormat - gif, jpg, png || detect which fileformat IMGFILE is
#test -z "$ANIMATION" && ANIMATION="false"
#
# loadLOL loads every single frame into an array field WHEN ANIMATION is true
# loadLOL sets No of frames in LOLFIELDS
#
# flootworker looks in while loop which value in /dev/shm/frametick is.
# /dev/shm/frametick contains which frame from LOL to draw.
# function frametick() gets started in background when ANIMATION is true
# and gets started before the flootworker get started
# frametick writes the no of frame to draw into /dev/shm/frametick
# to not draw too fast, there is a sleep in frametick which waits for
# FRAMETICKTIME seconds. Values are float, down to lowest value 0.001
# Only one of ANIMATION or LARGE can be true, not both.
FRAMETOPICK_SHM="/dev/shm/pxlflt-frametopick-${FNAME}"
## END ANIMATION
declare -a LOL
declare -a LOLPID
## TODOS
#
# DONE - Put OFFSET into /dev/shm/ so each worker do not have to count OFFSET
# for itself and we prevent worker from drifting apart when drawing.
# - get dimensions of pic with "identify" (imagemagick part)
# - get dimensions of the pixelflut board
# colors for colored output 8)
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
ENDCOLOR="\e[0m"
function message() {
case $1 in
warn)
MESSAGE_TYPE="${YELLOW}WARN${ENDCOLOR}"
;;
error)
MESSAGE_TYPE="${RED}ERROR${ENDCOLOR}"
;;
info|*)
MESSAGE_TYPE="${GREEN}INFO${ENDCOLOR}"
;;
esac
if [ "$1" == "info" ] || [ "$1" == "warn" ] || [ "$1" == "error" ]
then
MESSAGE=$2
else
MESSAGE=$1
fi
echo -e "[${MESSAGE_TYPE}] $MESSAGE"
}
error ()
{
message error "${RED}ERROR!!${ENDCOLOR}"
exit 1
}
gen_field() {
test -z $W && W=640
test -z $H && H=480
test -z $COLOR && COLOR="666999"
message "drawing ${YELLOW}$W x $H - $COLOR${ENDCOLOR}" >&2
for x in $(seq 0 $W)
do
for y in $(seq 0 $H)
do
echo "PX $x $y $COLOR"
done
done
}
convertimg() {
if [ -z "$1" ]
then
CONVERTFILE="$IMGFILE"
else
CONVERTFILE="$1"
fi
convert $CONVERTFILE $BORDER $RESIZE txt: | tail -n +2 | awk '{print $1 $3}' | sed -e 's/\,/ /' -e 's/\:/ /' -e 's/\#//' -e 's/^/PX /'
}
xymode() {
case $SHUFMODE in
chaos) OFFSET="OFFSET $(shuf -i 0-$X_MAX -n 1) $(shuf -i 0-$Y_MAX -n 1)"
;;
shake) OFFSET="OFFSET $(shuf -i $X-$(($X+10)) -n 1) $(shuf -i $Y-$(($Y+10)) -n 1)"
;;
cursor) OFFSET="OFFSET $(xdotool getmouselocation | tr ':' ' '|awk '{print $2 " " $4}')"
;;
bounce)
# every call is a run in a loop
# in every run we count x or y alternativ one up or down
# we decide with with var 'xory', 0 is x , 1 is y
# up or down ist set with 'xdir' and 'ydir'
# 0 means up, 1 means down
#
# handled outsite, in the flootworker()
if [ $XDIR == 0 ]
then
X=$(($X+$BOUNCESTEP))
test $X -ge $X_MAX && XDIR=1
else
X=$(($X-$BOUNCESTEP))
test $X -eq 0 && XDIR=0
fi
if [ $YDIR == 0 ]
then
Y=$(($Y+$BOUNCESTEP))
test $Y -ge $Y_MAX && YDIR=1
else
Y=$(($Y-$BOUNCESTEP))
test $Y -eq 0 && YDIR=0
fi
OFFSET="OFFSET $X $Y"
;;
static|*) test -z $X && X=0
test -z $Y && Y=0
OFFSET="OFFSET $X $Y"
;;
esac
}
frametick() {
test -z "$FRAMETICKTIME" && FRAMETICKTIME=0.1
#LOLFIELDS=12
i=0
while true
do
if [ "$i" -lt $LOLFIELDS ]
then
echo "$i" > $FRAMETOPICK_SHM
i=$(($i+1))
else
i=0
fi
sleep $FRAMETICKTIME
done
}
pipectrl() {
if [ $THROTTLE ]
then
pv $(test -z $PIPEVIEW && echo "-q") -c -N "[worker ${iFLOOTWORKER}]" -L "$THROTTLE" || return 1
else
if [ $PIPEVIEW ]
then
pv -c -N "[worker ${iFLOOTWORKER}]" || return 1
else
cat - || return 1
fi
fi
}
flootworker()
{
if [ $ANIMATION ] && [ $iFLOOTWORKER -gt 1 ]
then
message "[worker ${YELLOW}$iFLOOTWORKER${ENDCOLOR}] shuffle pixels again to maximize coverage" >&2
i=0
while [ $i -lt $LOLFIELDS ]
do
LOL[$i]="$(echo "${LOL[$i]}" | shuf)"
i=$(($i+1))
done
message "[worker ${YELLOW}$iFLOOTWORKER${ENDCOLOR}] shuffle ${GREEN}done${ENDCOLOR}" >&2
fi
while true
do
# set offset
if [ $SYNCFLOOTWORKER ]
then
if [ $iFLOOTWORKER == 1 ]
then
xymode
echo "$OFFSET" | tee $OFFSET_SHM
else
test -f $OFFSET_SHM && OFFSET="$(<${OFFSET_SHM})"
# little hack, otherwise on fast machines with small images we
# get an empty string back.
while [ "$(echo $OFFSET | wc -c )" -lt 10 ]
do
test $VERBOSE && message warn "[worker ${YELLOW}$iFLOOTWORKER${ENDCOLOR}] [${RED}VERBOSE${ENDCOLOR}] TOO FAST, cannot fetch OFFSET! I got '${YELLOW}${OFFSET}${ENDCOLOR}' from ${YELLOW}${OFFSET_SHM}${ENDCOLOR}" >&2
OFFSET="$(<${OFFSET_SHM})"
done
echo "$OFFSET"
fi
else
xymode
echo "$OFFSET"
fi
if [ $LARGE ]
then
for i in $(seq 0 $1 | shuf)
do
echo "${LOL[$i]}"
done
elif [ $ANIMATION ]
then
echo "${LOL[$(< ${FRAMETOPICK_SHM})]}"
else
echo "${LOL[$1]}"
fi
done | pipectrl > /dev/tcp/$IPFLOOT/$FLOOTPORT || message warn "[worker ${YELLOW}${iFLOOTWORKER}${ENDCOLOR}] transmission ${RED}failed${ENDCOLOR} - maybe you need to decrease ${YELLOW}FLOOTFORKS${ENDCOLOR} or expand/tune your uplink"
#~ echo "${LOL[$(< ${FRAMETOPICK_SHM})]}" > /dev/shm/lol123_${i}
#~ echo "${OFFSET}" > /dev/shm/lol123.offset_${i}
#~ read
}
checkfile() {
if [ ! -f $1 ] || [ -z $1 ] || [ -d $1 ]
then
message error "file ${YELLOW}$1${ENDCOLOR} is not a valid file or does not exist."
exit 1
fi
}
loadLOL() {
# when LARGE true, then we slize the large pixlist into smaller pieces
# max 64k each one
if [ $LARGE ]
then
if [ -z "$ALPHACOLOR" ]
then
LOL_org="$(echo "$LOL_org" | shuf)"
else
message "remove aplha color ${YELLOW}${ALPHACOLOR^^}${ENDCOLOR}"
LOL_org="$(echo "$LOL_org" | grep -v ${ALPHACOLOR^^} | shuf)"
fi
test -z "$LOLFIELDSIZE" && LOLFIELDSIZE=64000
# line counter
L=1
LINES="$(echo "$LOL_org" | wc -l )"
LOLFIELDS="$(( ( $LINES / $LOLFIELDSIZE ) ))"
message "LARGE mode: slicing ${YELLOW}${IMGFILE}${ENDCOLOR} - ${YELLOW}${LINES}${ENDCOLOR} into ${YELLOW}$((${LOLFIELDS}+1))${ENDCOLOR} fields"
i=0
while [ $i -le $LOLFIELDS ]
do
LN=$(($L+$LOLFIELDSIZE+1))
message "field ${YELLOW}${i}/${LOLFIELDS}${ENDCOLOR}, lines ${YELLOW}${L}${ENDCOLOR} - ${YELLOW}${LN}${ENDCOLOR}"
LOL[$i]="$(echo "$LOL_org" | sed -n "${L},$(($LN-1))p;${LN}q" )"
L=$LN
i=$(($i+1))
done
elif [ $ANIMATION ]
then
i=0
while [ $i -lt $LOLFIELDS ]
do
if [ -z "$ALPHACOLOR" ]
then
message "load and shuffle pixels for frame ${YELLOW}$((${i}+1))/${LOLFIELDS}${ENDCOLOR}"
LOL[$i]="$(convertimg ${ANIFILE}-${i}.png | shuf)"
else
message "load and shuffle pixels for frame ${YELLOW}$((${i}+1))/${LOLFIELDS}${ENDCOLOR}, remove aplha color ${YELLOW}${ALPHACOLOR^^}${ENDCOLOR}"
LOL[$i]="$(convertimg ${ANIFILE}-${i}.png | grep -v ${ALPHACOLOR^^} | shuf)"
fi
#echo "${LOL[$i]}" | head
i=$(($i+1))
done
# ani
#~ echo ani ani
#~ exit 1
else
for i in $(seq 1 $FLOOTFORKS)
do
if [ -z "$ALPHACOLOR" ]
then
message "shuffle pixels for [worker ${YELLOW}${i}${ENDCOLOR}]"
LOL[$i]="$(echo "$LOL_org" | shuf)"
else
message "remove aplha color ${YELLOW}${ALPHACOLOR^^}${ENDCOLOR} and shuffle pixels for [worker ${YELLOW}${i}${ENDCOLOR}]"
LOL[$i]="$(echo "$LOL_org" | grep -v ${ALPHACOLOR^^} | shuf)"
fi
done
fi
}
floot() {
if [ -n "$BORDERCOLOR" ]
then
BORDER="-bordercolor #${BORDERCOLOR} -border 2x2"
else
BORDER=""
fi
if [ -n "$RESIZE" ]
then
message "resizing to ${YELLOW}${RESIZE}px${ENDCOLOR}"
RESIZE="-resize $RESIZE"
fi
case $FNAME in
fill)
message "generating color field with ${YELLOW}${FLOOTFORKS}${ENDCOLOR} worker"
LOL_org="$(gen_field)"
loadLOL
;;
""|text)
test -z "$TEXT" && TEXT="$0"
test -z "$FONTSIZE" && FONTSIZE=42
test -z "$COLOR" && COLOR="000000"
test -z "$BGCOLOR" && BGCOLOR="FFFFFF"
if [ -n "$SIZE" ]
then
SIZE="-size $SIZE"
fi
#convert -fill lightgreen -background white -pointsize 40 caption:"KARTTUR" -quality 72 DstImage.png
message "generating text, size $FONTSIZE for ${YELLOW}$FLOOTFORKS${ENDCOLOR} worker"
message "TEXT: ${YELLOW}${TEXT}${ENDCOLOR}"
LOL_org="$(convert ${SIZE} ${BORDER} +antialias -depth 8 -fill \#${COLOR} -background \#${BGCOLOR} -pointsize ${FONTSIZE} caption:"${TEXT}" -quality 72 txt: | tail -n +2 | awk '{print $1 $3}' | sed -e 's/\,/ /' -e 's/\:/ /' -e 's/\#//' -e 's/^/PX /')"
loadLOL
;;
*)
if [ $ANIMATION ]
then
checkfile $IMGFILE
message "ANIMATION mode, checking if ${YELLOW}${IMGFILE}${ENDCOLOR} is an GIF"
if [ "$(file $IMGFILE |awk '{print $2}')" == "GIF" ]
then
LOLFIELDS="$(identify $IMGFILE | wc -l )"
message "splitting ${YELLOW}${IMGFILE}${ENDCOLOR} up into ${YELLOW}${LOLFIELDS}${ENDCOLOR} frame images"
convert -coalesce $IMGFILE ${ANIFILE}.png || error
else
message error "Other filetypes then ${YELLOW}GIF${ENDCOLOR} are not supported at the moment for ${YELLOW}ANIMATION${ENDCOLOR}"
exit 1
fi
else
if [ $USECACHE ]
then
checkfile $PIXLIST
message "using cache from ${YELLOW}$PIXLIST${ENDCOLOR}"
LOL_org="$(< $PIXLIST)"
else
checkfile $IMGFILE
message "convertimg image file ${YELLOW}${IMGFILE}${ENDCOLOR}"
LOL_org="$(convertimg)"
#convertimg > $PIXLIST
fi
fi
message "prepare worker .."
#set -x
loadLOL
#set +x
message "${GREEN}Done!${ENDCOLOR}"
;;
esac
if [ $ANIMATION ]
then
frametick &
fi
message "starting ${YELLOW}${FLOOTFORKS}${ENDCOLOR} worker"
if [ -n "$SYNCFLOOTWORKER" ] && [ $FLOOTFORKS -gt 1 ]
then
message "SYNCFLOOTWORKER is enabled, all worker use OFFSET from [worker ${YELLOW}1${ENDCOLOR}]"
fi
while true
do
for iFLOOTWORKER in $(seq $FLOOTFORKS)
do
if [ -z ${LOLPID[$iFLOOTWORKER]} ] || ! ps -p ${LOLPID[$iFLOOTWORKER]} > /dev/null
then
message "[worker ${YELLOW}$iFLOOTWORKER${ENDCOLOR}] not running, starting it"
if [ $LARGE ] || [ $ANIMATION ]
then
flootworker $LOLFIELDS &
LOLPID[$iFLOOTWORKER]=$!
else
flootworker $iFLOOTWORKER &
LOLPID[$iFLOOTWORKER]=$!
fi
message "[worker ${YELLOW}$iFLOOTWORKER${ENDCOLOR}] PID ${YELLOW}${LOLPID[$iFLOOTWORKER]}${ENDCOLOR} ${GREEN}started${ENDCOLOR}"
fi
done
done
}
case $1 in
convertimg)
checkfile $IMGFILE
message "generating pixlist cachefile from ${YELLOW}${IMGFILE}${ENDCOLOR}"
convertimg > $PIXLIST
message "file ${YELLOW}${PIXLIST}${ENDCOLOR} generated, you can use it with ${GREEN}USECACHE=true $0 floot ${IMGFILE}${YELLOW}"
;;
floot) message "flooting ${YELLOW}${IPFLOOT}:${FLOOTPORT}${ENDCOLOR}"
##~ WIP - get the size of the board from server
#~ message "request board size from ${YELLOW}${IPFLOOT}:${FLOOTPORT}${ENDCOLOR}"
#~ exec 5<>/dev/tcp/$IPFLOOT/$FLOOTPORT
#~ echo "SIZE" >&5
#~ #sleep 1
#~ BOARDSIZE="$(cat <&5)" &
#~ cat <&5 &
#~ sleep 1
#~ exec 5<&-
#~ message "$BOARSIZE"
##~ END WIP
case $SHUFMODE in
cursor)
if ! command -v xdotool > /dev/null
then
message error "command ${YELLOW}xdotool${ENDCOLOR} not found"
exit 1
fi
;;
*)
;;
esac
for imgmgckCMD in convert identify
do
if ! command -v $imgmgckCMD > /dev/null
then
message error "imagemagick command ${YELLOW}${imgmgckCMD}${ENDCOLOR} not found"
exit 1
fi
done
if [ $PIPEVIEW ] || [ $THROTTLE ]
then
if ! command -v pv > /dev/null
then
message error "command ${YELLOW}pv${ENDCOLOR} not found"
exit 1
fi
fi
if [ $LARGE ] && [ $ANIMATION ]
then
message error "${YELLOW}LARGE${ENDCOLOR} and ${YELLOW}ANIMATION${ENDCOLOR} cannot be used at the same time. Please use only one of them."
exit 1
fi
message "all requirements satisfied ${GREEN}:)${ENDCOLOR}"
floot
;;
*)
echo "$0 [floot|convertimg] [FILENAME|fill|text] ([MODE])"
echo ""
echo "floot: flooting the target specified with IPFLOOT"
echo "convertimg: converts an image to a command list file in /tmp"
echo " to use it, start 'USECACHE=true $0 floot [FILENAME]', where FILENAME"
echo " is the original image file."
echo ""
echo "FILENAME: path to any picture imagemagick can handle (env X, Y, RESIZE, "
echo " BORDERCOLOR, ALPHACOLOR)"
echo "fill: create a filled area with (env COLOR, W (width), H (height), X, Y)"
echo "text: create a textbox (env TEXT, FONTSIZE, SIZE, COLOR, BGCOLOR, BORDERCOLOR"
echo " ALPHACOLOR)"
echo ""
echo "MODE: static (env X and Y for position)"
echo " chaos (env X_MAX and Y_MAX for position range)"
echo " shake (env X and Y for position)"
echo " cursor"
echo " bounce (env Y_MAX and X_MAX for max bounce range, BOUNCESTEP for step size)"
echo ""
echo "available env vars to configure:"
echo "IPFLOOT(string), FLOOTPORT(int), USECACHE(bool), FLOOTFORKS(int)"
echo "SIZE(int), TEXT(string), FONTSIZE(int), BGCOLOR(hex), COLOR(hex)"
echo "BORDERCOLOR(hex), X(int), Y(int), X_MAX(int), Y_MAX(int), H(int), W(int)"
echo "RESIZE(int), ALPHACOLOR(hex), BOUNCESTEP(int), LARGE(bool), LOLFIELDSIZE(int)"
echo "ANIMATION(bool), FRAMETICKTIME(float), SYNCFLOOTWORKER(bool), THROTTLE(string)"
echo "PIPEVIEW(bool), VERBOSE(bool)"
exit 1
;;
esac