From 58fdcfdc2b3164a6b87680b04b5251599db306d2 Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Thu, 1 Feb 2024 04:43:14 -0800 Subject: [PATCH] shellcheck fixes in 2020/kurdyukov2 try.sh now runs makegif.sh. Fix makegif.sh to first compile the program, allow one to specify CC and convert and check that the output file was created. In the case convert(1) is not found or it fails to generate the output file it suggests the user look at the FAQ entry about ImageMagick. --- 2020/kurdyukov2/Makefile | 2 +- 2020/kurdyukov2/README.md | 9 ++++--- 2020/kurdyukov2/makegif.sh | 55 +++++++++++++++++++++++++++++++------- 2020/kurdyukov2/try.sh | 16 +++++++++-- thanks-for-help.md | 6 +++++ 5 files changed, 72 insertions(+), 16 deletions(-) diff --git a/2020/kurdyukov2/Makefile b/2020/kurdyukov2/Makefile index 355d9fc65..14d28cc56 100644 --- a/2020/kurdyukov2/Makefile +++ b/2020/kurdyukov2/Makefile @@ -212,7 +212,7 @@ clean: clobber: clean ${RM} -f ${TARGET} ${ALT_TARGET} - ${RM} -rf *.dSYM *.gif *.ppm out*.jpg + ${RM} -rf *.dSYM *.gif *.ppm out*.jpg gif/ @-if [ -e sandwich ]; then \ ${RM} -f sandwich; \ echo 'ate sandwich'; \ diff --git a/2020/kurdyukov2/README.md b/2020/kurdyukov2/README.md index 1b99efbab..a1b31a1f3 100644 --- a/2020/kurdyukov2/README.md +++ b/2020/kurdyukov2/README.md @@ -19,6 +19,7 @@ make ./prog 1000 selfie.jpg output.jpg # Admire your portrait in cubist style +# Also try: ./try.sh ``` @@ -98,7 +99,7 @@ After that’s installed you need to run make with these options: make LDFLAGS="-I/opt/local/include -L/opt/local/lib" ``` -### Building on macOS with HomeBrew +### Building on macOS with Homebrew First, make sure you have the compiler tools installed e.g. by: @@ -106,7 +107,7 @@ First, make sure you have the compiler tools installed e.g. by: sudo xcode-select --install ``` -Make sure you install [HomeBrew](https://brew.sh). +Make sure you install [Homebrew](https://brew.sh). Then: @@ -139,9 +140,11 @@ You can use this command to make a GIF from output images (uses ImageMagick). For instance after running the command suggested by the judges: ```sh -convert -delay 10 -dither none -loop 0 $(ls out*.jpg | sort -V) $(ls out*.jpg | sort -rV) +map out.gif +convert -delay 10 -dither none -loop 0 $(find . -maxdepth 1 -type f -name '*jpg' | sort -V) $(find . -maxdepth 1 -type f -name '*jpg' | sort -rV) +map out.gif ``` +NOTE: the above is done in the [try.sh](try.sh) script. + Also provided `makegif.sh` to aid with GIF creation. diff --git a/2020/kurdyukov2/makegif.sh b/2020/kurdyukov2/makegif.sh index 906f29fd9..8fcaf7885 100755 --- a/2020/kurdyukov2/makegif.sh +++ b/2020/kurdyukov2/makegif.sh @@ -4,28 +4,63 @@ output=${2:-"output.gif"} prog=${3:-"./prog"} ext="${image##*.}" dir="gif" -[ $# -lt 3 ] && [ "$ext" = "png" -o "$ext" = "ppm" ] && prog="./prog_$ext" -[ -f "$image" ] || { +[[ $# -lt 3 ]] && [[ "$ext" = "png" || "$ext" = "ppm" ]] && prog="./prog_$ext" +[[ -f "$image" ]] || { echo "Usage: $0 image.jpg output.gif ./prog" echo "Dependencies: ImageMagick, bc (basic calculator)" echo "Note: don't use large images (dimensions above 1000), ImageMagick may fail." echo "Uses \"gif\" directory for temporary files." exit 1 } +# make sure CC is set so that when we do make CC="$CC" it isn't empty. Doing it +# this way allows us to have the user specify a different compiler in an easy +# way. +[[ -z "$CC" || ! -f "$CC" || ! -x "$CC" ]] && CC="cc" +make CC="$CC" all >/dev/null || exit 1 + mkdir -p "$dir" + i=8 ; x=$i ; j=0 -while [ $x -le 16000 ]; do +while [[ $x -le 16000 ]]; do x="$(echo "a=$i*e(l(2)/4*$j)+0.5;scale=0;a/1" | bc -l)" echo "### $j : $x" list[j]="$dir/out$j.$ext" "$prog" "$x" "$image" "$dir/out$j.$ext" ; j=$((j+1)) done i=$j ; j=$((j-2)) -while [ $j -ge 1 ]; do -### Uncomment the following line if you want to make a GIF in an image editor such as GIMP -# cp "$dir/out$j.$ext" "$dir/out$i.$ext" - list[i]="$dir/out$j.$ext" - i=$((i+1)) ; j=$((j-1)) -done -convert -delay 10 -dither none -loop 0 "${list[@]}" +map "$output" +while [[ $j -ge 1 ]]; do + ### Uncomment the following line if you want to make a GIF in an image editor such as GIMP + # cp "$dir/out$j.$ext" "$dir/out$i.$ext" + list[i]="$dir/out$j.$ext" + i=$((i+1)) ; j=$((j-1)) +done + +# try finding convert(1) from ImageMagick if CONVERT not passed to script or is +# not an executable file +convert_error() +{ + echo 1>&2 + echo "Warning: $CONVERT failed to generate out.gif. Is $CONVERT convert(1) from ImageMagick?" 1>&2 + echo "See: https://github.com/ioccc-src/temp-test-ioccc/blob/master/faq.md#imagemagick." 1>&2 + echo 1>&2 + echo "Tip: if you have it in a different path, try: CONVERT=path ./try.sh" 1>&2 + echo 1>&2 +} +[[ -z "$CONVERT" || ! -f "$CONVERT" || ! -x "$CONVERT" ]] && CONVERT="$(type -P convert)" +if [[ -z "$CONVERT" || ! -f "$CONVERT" || ! -x "$CONVERT" ]]; then + echo 1>&2 + echo "Could not fine convert(1) from ImageMagick." 1>&2 + echo "See: https://github.com/ioccc-src/temp-test-ioccc/blob/master/faq.md#imagemagick." 1>&2 + echo 1>&2 +else + "$CONVERT" -delay 10 -dither none -loop 0 "${list[@]}" +map "$output" + if [[ ! -f "$output" ]]; then + convert_error + else + echo 1>&2 + echo "Now look at $output with a graphics viewer that can show animated GIFs." 1>&2 + echo "Warning: includes flashing colours." 1>&2 + echo 1>&2 + fi +fi diff --git a/2020/kurdyukov2/try.sh b/2020/kurdyukov2/try.sh index 3af7f101a..9301244f0 100755 --- a/2020/kurdyukov2/try.sh +++ b/2020/kurdyukov2/try.sh @@ -27,6 +27,8 @@ convert_error() echo 1>&2 } +# remove ppm files that we create +rm -f random.ppm output.ppm echo "$ (echo P6 1024 1024 255; dd if=/dev/urandom bs=3M count=1) > random.ppm" 1>&2 (echo P6 1024 1024 255; dd if=/dev/urandom bs=3M count=1) > random.ppm echo "Look at random.ppm. Do you see any patterns?" 1>&2 @@ -54,8 +56,13 @@ if [[ -n "$CONVERT" ]]; then echo 1>&2 read -r -n 1 -p "Press any key to create GIF: " echo 1>&2 - echo "$ $CONVERT -delay 10 -dither none -loop 0 $(ls out*.jpg | sort -V) $(ls out*.jpg | sort -rV) +map out.gif" 1>&2 - "$CONVERT" -delay 10 -dither none -loop 0 $(ls out*.jpg | sort -V) $(ls out*.jpg | sort -rV) +map out.gif + echo "$ $CONVERT -delay 10 -dither none -loop 0 $(find . -maxdepth 1 -type f -name '*jpg' | sort -V) $(find . -maxdepth 1 -type f -name '*jpg' | sort -rV) +map out.gif" 1>&2 + # We want word splitting here so disable shellcheck warning SC2046. + # + # SC2046 (warning): Quote this to prevent word splitting. + # https://www.shellcheck.net/wiki/SC2046 + # shellcheck disable=SC2046 + "$CONVERT" -delay 10 -dither none -loop 0 $(find . -maxdepth 1 -type f -name '*jpg' | sort -V) $(find . -maxdepth 1 -type f -name '*jpg' | sort -rV) +map out.gif if [[ ! -f out.gif ]]; then convert_error else @@ -63,6 +70,11 @@ if [[ -n "$CONVERT" ]]; then echo "Now look at out.gif with a graphics viewer that can show animated GIFs." 1>&2 echo "Warning: includes flashing colours." 1>&2 fi + + read -r -n 1 -p "Press any key to run: ./makegif.sh sample.jpg kurdyukov2.gif ./prog: " + echo 1>&2 + ./makegif.sh sample.jpg kurdyukov2.gif ./prog + echo 1>&2 else echo 1>&2 echo "Could not fine convert(1) from ImageMagick." 1>&2 diff --git a/thanks-for-help.md b/thanks-for-help.md index 2c96a47fd..cf856a060 100644 --- a/thanks-for-help.md +++ b/thanks-for-help.md @@ -4833,6 +4833,12 @@ the user has installed the appropriate library with [MacPorts](https://www.macports.org) (with the default MacPorts prefix `/opt/local`). +Cody also fixed the script [makegif.sh](2020/kurdyukov2/makegif.sh) in a variety +of ways: shellcheck(1), make sure the program is compiled first (allowing one to +specify which compiler to use with `CC=foo ./makegif.sh ...`), checking that +`convert(1)` is found and that it worked properly (linking to the proper FAQ +entry if not installed or it fails). + ## [2020/kurdyukov3](/2020/kurdyukov3/prog.c) ([README.md](/2020/kurdyukov3/README.md))