This is part of a script that I use for porting a TWRP Theme from one resolution to another. Respectively portrait to portrait and landscape to landscape. In this function entirely, it focuses on the images. What I am not showing is irrelevant since it focuses on porting the xml files.
I compiled GraphicsMagick for ARM and thus call on it when it is time to do the resizing of each image. GM can be a bit slow so I included a method which mimics the parallel concept to help speed up the process.
Is this sufficient or is there a better approach I can take given how this part of the script operates?
If you want to look at the whole script then you can snatch it up here.
GOTOTWRPIMAGE() {
NUM=0
QUEUE=""
MAX_NPROC=$(grep "processor :" < /proc/cpuinfo | wc -l) # default
MAX_NPROC=$(($MAX_NPROC+1))
queue() {
QUEUE="$QUEUE $1"
NUM=$(($NUM+1))
}
regeneratequeue() {
OLDREQUEUE=$QUEUE
QUEUE=""
NUM=0
for PID in $OLDREQUEUE; do
if [ -d /proc/$PID ]; then
QUEUE="$QUEUE $PID"
NUM=$(($NUM+1))
fi
done
}
checkqueue() {
OLDCHQUEUE=$QUEUE
for PID in $OLDCHQUEUE; do
if [ ! -d /proc/$PID ]; then
regeneratequeue # at least one PID has finished
break
fi
done
}
if [ ! -f "${P_IMAGES}"/image_dir.txt ]; then
busybox touch "${P_IMAGES}"/image_dir.txt
else
rm -f "${P_IMAGES}"/image_dir.txt
busybox touch "${P_IMAGES}"/image_dir.txt
fi
# Find the images to be ported.
if output=$(find "${P_IMAGES}" -type f -iname "*.png" -o -iname "*.jpg"); then
echo "${output}" > "${P_IMAGES}"/image_dir.txt
fi
filename="${P_IMAGES}"/image_dir.txt
filelines=$(cat "${filename}")
for line in $filelines; do
# Remember the directory and filename
local IMAGEFILE="$(basename "${line}")"
local IMAGEFILEDIR="$(dirname "${line}")"
# Remember width and height
SIZEW=$(gm identify -format "%w" "${IMAGEFILEDIR}"/"${IMAGEFILE}")
SIZEH=$(gm identify -format "%h" "${IMAGEFILEDIR}"/"${IMAGEFILE}")
# Calculate old size for new size of each image.
# Round fractions up and down for each image.
FINALW=$(awk 'BEGIN{print int("'$BASEW'"/("'$PORTW'"/"'$SIZEW'")+0.5)}')
FINALH=$(awk 'BEGIN{print int("'$BASEH'"/("'$PORTH'"/"'$SIZEH'")+0.5)}')
# Inform user of the change and conduct the change
echo " "
echo Porting "${IMAGEFILE}" from $SIZEW'x'$SIZEH to $FINALW'x'$FINALH
# Convert image to new sizes
gm convert -size $FINALW'x'$FINALH! "${IMAGEFILEDIR}"/"${IMAGEFILE}" -resize $FINALW'x'$FINALH! "${IMAGEFILEDIR}"/"${IMAGEFILE}" &
PID=$!
queue $PID
while [ $NUM -ge $MAX_NPROC ]; do
checkqueue
#sleep 0.4
done
done
wait # wait for all processes to finish before exit
if [ -f "${P_IMAGES}"/image_dir.txt ]; then
rm -rf "${P_IMAGES}"/image_dir.txt
fi
if [ $OPT == "1" ]; then
HEADSUP
fi
}