Skip to content

Instantly share code, notes, and snippets.

@thewhodidthis
Last active June 13, 2024 11:26
Show Gist options
  • Save thewhodidthis/ef522400f81ad5cf0631e95ffd7e5d28 to your computer and use it in GitHub Desktop.
Save thewhodidthis/ef522400f81ad5cf0631e95ffd7e5d28 to your computer and use it in GitHub Desktop.
PPM byte beat maker
/* Call like so: `echo 'clamp(256, 0, 255)' | bc -l helper.bc` */
define clamp(v, lo, hi) {
if (v < lo) {
return (lo)
}
if (v > hi) {
return (hi)
}
return (v)
}
define remap(v, x1, x2, y1, y2) {
return (v - x1) * (y2 - y1) / (x2 - x1) + y1
}
# The filenaming timestamp
D?=$$(date +"%Y%m%d%s")
# Step
S?=2
# Pixmap dimensions
W?=192
H?=192
# How many pixels minus one
Q?=$$(($W * $H - 1))
# How many steps minus one
N?=$$((($W * $H / ($S * 2)) - 1))
# Out image dimensions
w?=200
h?=200
# Formula params
t?=12
k?=9
svg:
@for i in `seq 0 $N`; do x=$$(($$i % ($W / $S))); y=$$(($$i / ($W / $S)));\
f=$$(bc -l -S0 -O16 <<< "16777215 * !(bxor($$x, $$y) % $k)");\
printf '<rect x="%d" y="%d" fill="#%s" width="%d" height="%d"/>' $$(($$x * $S)) $$(($$y * $S)) $$f $S $S;\
done | awk '{printf("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"%d\" height=\"%d\">%s</svg>", $W, $H, $$0)}' > $D.svg
ppm:
@for i in `seq 0 $Q`; do d=(); x=$$(($$i % $W)); y=$$(($$i / $W));\
d+=($$(bc -l -S0 <<< "255 * !(bxor($$x, $$y) % $k)"));\
c=`printf "%d\n" "$${d[@]}"`;\
echo $$c $$c $$c;\
done | tr "\n" " " | awk '{print "P3\n$W $H\n255\n"$$0}' > $D.ppm
jpg: ppm
sips -s formatOptions best -s format jpeg $$(ls -tr | tail -n 1) -o $D.jpg && sips -z $h $w $D.jpg
ppm/2:
@for i in `seq 0 $Q`; do d=(); x=$$(($$i % $W)); y=$$(($$i / $W));\
d+=($$(bc -l -S0 <<< "255 * !(bxor($$x * $$y, $t) % $k)"));\
c=`printf "%d\n" "$${d[@]}"`;\
echo $$c $$c $$c;\
done | tr "\n" " " | awk '{print "P3\n$W $H\n255\n"$$0}' > $D.ppm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment