diff --git a/bin/blurborder b/bin/blurborder deleted file mode 100755 index 47d4dd3..0000000 --- a/bin/blurborder +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env sh -set -e # exit on failure -IFS=$(printf '\n\t') # smarter ifs - -test "$RATIOW" || RATIOW=16 -test "$RATIOH" || RATIOH=9 -imageh=$(identify -format %h "$1") -des_ratio=$(printf '3 k %u %u / p' "$RATIOW" "$RATIOH" | dc) -des_width=$(printf '1 k %f %u * p' "$des_ratio" "$imageh" | dc | cut -d. -f1) - -convert "$1" -blur 0x7 -resize "$des_width"x tmp1.ppm -cp "$1" tmp2.ppm -[ "$BORDER" != "0" ] && convert "$1" -bordercolor black -border 10x0 tmp2.ppm -composite -gravity center tmp2.ppm tmp1.ppm tmp3.ppm -mogrify -gravity center -crop "x$imageh+0" tmp3.ppm -outfile="$(dirname "$1")/tran_$(basename "$1" | tuc -d. -f:-2).jxl" -cjxl -e 8 tmp3.ppm "$outfile" -rm tmp*.ppm diff --git a/bin/blurborder b/bin/blurborder new file mode 120000 index 0000000..eda2734 --- /dev/null +++ b/bin/blurborder @@ -0,0 +1 @@ +wallmanip \ No newline at end of file diff --git a/bin/croptop b/bin/croptop deleted file mode 100755 index a5cd1d3..0000000 --- a/bin/croptop +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env sh -set -e # exit on failure -IFS=$(printf '\n\t') # smarter ifs - -jxl=0 -if file "$1" | grep 'JPEG XL'; then - jxl="$1" - out=$(mktemp --suffix=.jpg) - djxl "$1" "$out" - shift - set -- "$out" "$@" -fi - -test "$RATIOW" || RATIOW=16 -test "$RATIOH" || RATIOH=9 -imagew=$(identify -format %w "$1") -des_ratio=$(printf '3 k %u %u / p' "$RATIOW" "$RATIOH" | dc) - -des_height=$(printf '1 k %u %f / p' "$imagew" "$des_ratio" | dc | cut -d. -f1) -outfile="$(dirname "$1")/tran_$(basename "$1")" - -jpegtran -crop "$imagew"x"$des_height" -outfile "$outfile" "$1" - -if [ "$jxl" != 0 ]; then - cjxl -e 8 "$outfile" "tran_$jxl" - rm "$outfile" "$1" -fi diff --git a/bin/croptop b/bin/croptop new file mode 120000 index 0000000..eda2734 --- /dev/null +++ b/bin/croptop @@ -0,0 +1 @@ +wallmanip \ No newline at end of file diff --git a/bin/padjpeg b/bin/padjpeg deleted file mode 100755 index c843502..0000000 --- a/bin/padjpeg +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env sh -set -e # exit on failure -IFS=$(printf '\n\t') # smarter ifs - -jxl=0 -if file "$1" | grep 'JPEG XL'; then - jxl="$1" - out=$(mktemp --suffix=.jpg) - djxl "$1" "$out" - shift - set -- "$out" "$@" -fi - -test "$RATIOW" || RATIOW=16 -test "$RATIOH" || RATIOH=9 -imageh=$(identify -format %h "$1") -imagew=$(identify -format %w "$1") -des_ratio=$(printf '3 k %u %u / p' "$RATIOW" "$RATIOH" | dc) -img_ratio=$(printf '3 k %u %u / p' "$imagew" "$imageh" | dc) - -wide="$(echo "$img_ratio > $des_ratio" | bc )" -if [ "$wide" -gt 0 ] ; then - backup=$(mktemp) - mv "$1" "$backup" - jpegtran -rotate 90 -outfile "$1" "$backup" - imageh=$(identify -format %h "$1") - imagew=$(identify -format %w "$1") - des_ratio=$(printf '3 k %u %u / p' "$RATIOH" "$RATIOW" | dc) -fi - -des_width=$(printf '1 k %f %u * p' "$des_ratio" "$imageh" | dc | cut -d. -f1) -offset=$(printf '%u %u - 2 / p' "$des_width" "$imagew" | dc) -outfile="$(dirname "$1")/tran_$(basename "$1")" - -test "$REFLECT" && mode=r || mode=f -jpegtran -crop "$des_width$mode+$offset" -outfile "$outfile" "$1" - -if [ "$wide" -gt 0 ] ; then - mv "$backup" "$1" - jpegtran -rotate 270 -outfile "$outfile" "$outfile" -fi - -if [ "$jxl" != 0 ]; then - cjxl -e 8 "$outfile" "tran_$jxl" - rm "$outfile" "$1" -fi diff --git a/bin/padjpeg b/bin/padjpeg new file mode 120000 index 0000000..eda2734 --- /dev/null +++ b/bin/padjpeg @@ -0,0 +1 @@ +wallmanip \ No newline at end of file diff --git a/bin/wallmanip b/bin/wallmanip new file mode 100755 index 0000000..6406750 --- /dev/null +++ b/bin/wallmanip @@ -0,0 +1,210 @@ +#!/bin/sh +set -e # exit on failure +IFS=$(printf '\n\t') # smarter ifs + +error() { + echo "$@" >&2 + exit 1 +} + +help() { + echo "Usage: wallmanip [options...]" + echo '*defaults*: -a PAD -p CENTER -r 16:9' + echo " -a, --action One of {blurpad, crop, pad, reflect}" + echo " -f, --filename Specify file. Switch can be omitted." + echo " -p, --position One of {top, bottom, left, right}" + echo " -r, --ratio Specify aspect ratio, as W:H (e.g. 16:9)" + echo " -s, --scale Specify output resolution, as WxH (e.g. 1920x1080)" + + echo " -h, --help Print this help text" + echo " -v, --version Print the wallmanip version" + exit +} + +{ # block to initially set config vars + ACTION=PAD + FILE='' + POS=CENTER + + RATIOW=16 + RATIOH=9 + + RESIZE=0 + SCALEW=1920 + SCALEH=1080 +} + +# handle symlink names +case "$(basename "$0")" in + blurborder ) + ACTION=BLURPAD + ;; + croptop ) + ACTION=CROP + POS=TOP + ;; + padjpeg ) ;; +esac + +while [ $# -gt 0 ]; do + case "$1" in + -a | --action ) + ACTION=$(echo "$2" | tr "[:lower:]" "[:upper:]") + shift 2 ;; + -p | --position ) + POS=$(echo "$2" | tr "[:lower:]" "[:upper:]") + shift 2 ;; + -r | --ratio ) + RATIOW=$(echo "$2" | cut -d: -f1) + RATIOH=$(echo "$2" | cut -d: -f2) + shift 2 ;; + -s | --scale ) + RESIZE=1 + SCALEW=$(echo "$2" | cut -dx -f1) + SCALEH=$(echo "$2" | cut -dx -f2) + shift 2 ;; + --resize ) + RESIZE=1 + shift ;; + --version | -v ) + printf '\e[32m' + echo 'wallmanip 2024.05.11' + printf '\e[36m' + echo Built by July 🏳️‍🌈 + printf '\e[0m' + exit ;; + -h | --help) + help ;; + -f | --filename) + shift;; + *) + [ "$FILE" != '' ] && help # give helptext if already set + FILE=$1 + shift 1 ;; + esac +done + +{ # initial error/sanity checking blocking + test -f "$FILE" || error Invalid/missing filename + identify -format '' "$FILE" || error File is not an image + + case "$ACTION" in + BLURPAD | CROP | SCALE | PAD | REFLECT) ;; + *) error Invalid action ;; + esac + + case "$POS" in + BOTTOM | CENTER | LEFT | RIGHT | TOP ) ;; + *) error Invalid position ;; + esac + + for i in "$RATIOW" "$RATIOH" "$SCALEW" "$SCALEH"; do + if [ -n "$i" ] && [ "$i" -eq "$i" ]; then + continue + fi + error Invalid numerical argument + done +} + +divide() { # helper for precise division + printf '3k %s %s /p' "$1" "$2" | dc +} +truncate() { # specify truncation of floats where desired + cut -d. -f1 +} + +{ # build image information + img_height=$(identify -format %h "$FILE") + img_width=$(identify -format %w "$FILE") + + des_ratio=$(divide "$RATIOW" "$RATIOH") + img_ratio=$(divide "$img_width" "$img_height") + wide="$(echo "$img_ratio > $des_ratio" | bc )" + + jpg="$(file -bi "$FILE" | grep -q '^image/jpeg' && echo 1 || echo 0)" + jxl="$(file -bi "$FILE" | grep -q '^image/jxl' && echo 1 || echo 0)" + + out="$(dirname "$FILE")/tran_$(basename "$FILE")" + work=$(mktemp) +} + +{ # build working jpeg file, for as needed + if [ "$jxl" -eq 1 ]; then + djxl "$FILE" "$work" --output_format jpg + elif [ "$jpg" -eq 1 ]; then + cp "$FILE" "$work" + else + cjpegli "$FILE" "$work" + fi + + if [ "$wide" -eq 1 ] && [ "$ACTION" != CROP ]; then + jpegtran -rotate 90 -outfile "$work" "$work" + img_height=$(identify -format %h "$work") + img_width=$(identify -format %w "$work") + des_ratio=$(divide 1 "$des_ratio") + fi +} + +border() { + border=$(mktemp) + backdrop=$(mktemp) + des_width=$(echo "$des_ratio * $img_height" | bc | truncate) + gravity=center + convert "$work" -bordercolor black -border 10x0 PPM:"$border" + convert "$work" -blur 0x7 -resize "$des_width"x PPM:"$backdrop" + composite -gravity $gravity "$border" "$backdrop" PPM:"$backdrop" + convert -gravity center -crop "x$img_height+0" "$backdrop" JPEG:"$work" + rm -f "$border" "$backdrop" +} + +crop() { + des_height=$(divide "$img_width" "$des_ratio" | truncate) + offset=$(echo "$img_height - $des_height" | bc) + case $POS in + LEFT | TOP ) offset=0 ;; + CENTER ) offset=$(echo "$offset / 2" | bc) ;; + RIGHT | BOTTOM ) ;; + esac + jpegtran -crop "$img_width"x"$des_height"+0+"$offset" -outfile "$work" "$work" +} + +pad() { + des_width=$(echo "$des_ratio * $img_height" | bc | truncate) + offset=$(echo "$des_width - $img_width" | bc) + case $POS in + LEFT | BOTTOM ) offset=0 ;; + CENTER ) offset=$(echo "$offset / 2" | bc) ;; + RIGHT | TOP ) ;; + esac + jpegtran -crop "$des_width$1+$offset" -outfile "$work" "$work" +} + +case "$ACTION" in + BLURPAD ) + border ;; + CROP ) + crop ;; + REFLECT ) + pad r;; + PAD ) + pad f;; +esac + +{ # transfer back to output file + if [ "$wide" -eq 1 ] && [ "$ACTION" != CROP ]; then + jpegtran -rotate 270 -outfile "$work" "$work" + fi + if [ "$RESIZE" -eq 1 ]; then + ffmpeg -i "$work" -vf zscale="$SCALEW":"$SCALEH":filter=spline36 -vcodec ppm -f image2pipe - | convert - JPEG:"$work" + fi + + if [ "$jxl" -eq 1 ]; then + cjxl "$work" "$out" + elif [ "$jpg" -eq 1 ]; then + cp "$work" "$out" + else + djpegli "$work" "$out" + fi +} + +rm "$work" -f