#!/bin/bash

declare -i height
declare -i width
declare -i noimgs
declare -i rown
width=1920
height=1200
noimgs=6
ratio=$(bc <<< "scale=2;100/$noimgs")
indir="/mnt/flashblade/zimdata/zd_rolling_turning__20180131T182607/0000-east_wide_left__15384270/"
#indir="/mnt/flashblade/zimdata/zd_rolling_turning__20180131T182607/0000-north_wide_left__16363469/"
outdir="/tmp/imgsline/"

declare -i nogframes
nogframes=height*noimgs

rm "/tmp/imgs/*"
rm "/tmp/imgsline/*"
echo "nogframes : {$nogframes} "


# Cut single lines per frame
for i in `seq 0 $nogframes`;
do
  rown=($i-1)%height
  infile=$(printf "%s%09d.ppm\n" $indir $i)
  outfile=$(printf "%sfrow%09d.png\n" $outdir $i)
  echo "Cropping $infile to $outfile"
  convert -crop ${width}x1+0+${rown} $infile $outfile
done  

# Glue to single image
montage -mode concatenate -tile 1x ${outdir}frow*.png ${outdir}fullout.png

# Cut into independent frames
convert -crop 100%x${ratio}% ${outdir}fullout.png ${outdir}rframe.jpg

convert -delay 50 -loop 0 ${outdir}rframe-*.jpg ${outdir}out.gif
