diff --git a/bin/muxsa-pv b/bin/muxsa-pv new file mode 100755 index 0000000..e9d1aa0 --- /dev/null +++ b/bin/muxsa-pv @@ -0,0 +1,138 @@ +#!/bin/bash + +# muxsa-pv +# +# part of muxsa, https://git-nks-public.tik.uni-stuttgart.de/edu/muxsa +# +# this script reads a series of png files (preferably created with +# muxsa-kvm2png) and creates new png files, with the current file +# large and the next file somewhat smaller. The result looks similar +# to the "presenter view" in slide presentation software. + +# MIT License +# +# Copyright (c) 2023 Sebastian Kiesel +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +# OTHER DEALINGS IN THE SOFTWARE. + +############################################################################ + +echo "This is muxsa-pv." + +# various parameters can be set as an environment variable (export XX="y z") +# or can be set in our RC file. Otherwise we will assign default values below. + +if [ -r "${HOME}/.muxsarc" ] ; then + echo "Found ${HOME}/.muxsarc ... will source it." + . "${HOME}/.muxsarc" +fi + +echo "Will work with these settings:" +echo "MUXSA_KVM2PNG_SLIDE_PREFIX=${MUXSA_KVM2PNG_SLIDE_PREFIX:=""}" +echo "MUXSA_PV_OUT_DIR=${MUXSA_PV_OUT_DIR:="presenter-view"}" +echo "MUXSA_PV_RM_OUT_DIR=${MUXSA_PV_RM_OUT_DIR:="0"}" +echo "MUXSA_PV_SIZE_X=${MUXSA_PV_SIZE_X:="1680"}" +echo "MUXSA_PV_MARGIN_Y=${MUXSA_PV_MARGIN_Y:="4"}" +if [ ! "${MUXSA_PV_SIZE_Y}" ] ; then + # the left image will be scaled to a width of 64% + # of the width of the output image. assuming an aspect ratio of 16:9, + # its height will be 9/16 * 64% = 36% of that. + MUXSA_PV_SIZE_Y=$(( ${MUXSA_PV_SIZE_X} * 36/100 + 2*${MUXSA_PV_MARGIN_Y} )) +fi +echo "MUXSA_PV_SIZE_Y=${MUXSA_PV_SIZE_Y}" +echo + +# left image is 64 % of target width, starting at an offset of 1% +# right image is 32 % of target width, starting at 1+64+2%, leaving 1% right +X01=$(( ${MUXSA_PV_SIZE_X} * 1 / 100 )) +X32=$(( ${MUXSA_PV_SIZE_X} * 32 / 100 )) +X64=$(( ${MUXSA_PV_SIZE_X} * 64 / 100 )) +X67=$(( ${MUXSA_PV_SIZE_X} * 67 / 100 )) + +MAX_Y=$(( ${MUXSA_PV_SIZE_Y} - 2 * ${MUXSA_PV_MARGIN_Y} )) + +############################################################################ + +if [ -e "${MUXSA_PV_OUT_DIR}" ] ; then + echo "Output dir ${MUXSA_PV_OUT_DIR} exists." + if [ "${MUXSA_PV_RM_OUT_DIR}" = "1" ] ; then + echo "Removal requested by MUXSA_PV_RM_OUT_DIR. rm -rf ..." + rm -rf "${MUXSA_PV_OUT_DIR}" + else + echo "Removal NOT requested by MUXSA_PV_RM_OUT_DIR. abort." + exit 1 + fi +fi +mkdir -p "${MUXSA_PV_OUT_DIR}" +if [ ! -d "${MUXSA_PV_OUT_DIR}" ] ; then + echo "Could not create output dir ${MUXSA_PV_OUT_DIR}. abort." + exit 1 +fi + +############################################################################ + +TMP_FILE_LEFT="$(mktemp -t muxsa-pv-XXXXXXXXXX.ppm)" +if [ ! -w "${TMP_FILE_LEFT}" ] ; then + echo "Error: cannot create tmp_file. abort." + exit 1 +fi +TMP_FILE_RIGHT="$(mktemp -t muxsa-pv-XXXXXXXXXX.ppm)" +if [ ! -w "${TMP_FILE_RIGHT}" ] ; then + rm -f -- "${TMP_FILE_LEFT}" + echo "Error: cannot create tmp_file. abort." + exit 1 +fi +trap 'rm -f -- "${TMP_FILE_LEFT}" "${TMP_FILE_RIGHT}"' EXIT + +############################################################################ + +# find . -maxdepth 1 -name "${MUXSA_KVM2PNG_SLIDE_PREFIX}????.png" | sort | \ +# while read RIGHT_FN ; do + +for RIGHT_FN in ${MUXSA_KVM2PNG_SLIDE_PREFIX}????.png "" ; do + if [ ! -f "${LEFT_FN}" ] ; then + LEFT_FN="${RIGHT_FN}" + continue + fi + + OUTFILE="${MUXSA_PV_OUT_DIR}/${LEFT_FN}" + echo "muxsa-pv: processing ${LEFT_FN} + ${RIGHT_FN:-(null)} to ${OUTFILE}" + + pngtopnm "${LEFT_FN}" | \ + pnmscale -xysize ${X64} ${MAX_Y} | \ + pnmpad -width ${X64} -height ${MAX_Y} > "${TMP_FILE_LEFT}" + + if [ -f "${RIGHT_FN}" ] ; then + pngtopnm "${RIGHT_FN}" | \ + pnmscale -xysize ${X32} ${MAX_Y} | \ + pnmpad -width ${X32} -height ${MAX_Y} > "${TMP_FILE_RIGHT}" + else + pbmmake -black ${X32} ${MAX_Y} > "${TMP_FILE_RIGHT}" + fi + + pbmmake -black ${MUXSA_PV_SIZE_X} ${MUXSA_PV_SIZE_Y} | \ + pnmpaste -replace "${TMP_FILE_LEFT}" ${X01} ${MUXSA_PV_MARGIN_Y} | \ + pnmpaste -replace "${TMP_FILE_RIGHT}" ${X67} ${MUXSA_PV_MARGIN_Y} | \ + pnmtopng -force > "${OUTFILE}" + + LEFT_FN="${RIGHT_FN}" +done + +echo "muxsa-pv finished successfully!" +############################################################################