jpegtruncate

#!/bin/sh
# truncates a progressive JPEG image, outputs degraded copy on standard output
# first parameter: input filename; second parameter: number of scans in output
SCANS=$2;
SIZE_TARGET=$(
  (
    hexdump -v -e '"" 1/1 "%02X" ""' <$1 | grep -ob FFDA |\
    head -n$(echo $SCANS 1 + p | dc) | tail -n1 | cut -d: -f1;
    echo 2 / p
  ) | dc );
head -c"$SIZE_TARGET" <"$1";
env printf '\xff\xd9';