#! /bin/bash
# Last edited on 2011-04-29 01:31:15 by stolfi

# !!! The main picture and the blueprint must have the same aspect.
# !!! Fix it so that it works with any aspect.

synt_png="$1"; shift  # Synthetic image.
spct="$1"; shift      # Reduction percentage (without '%').

refd_png="$1"; shift  # Reference drawing (eg. blueprint)
rpct="$1"; shift      # Reduction percentage (without '%').

tmp=/tmp/$$

dull_png="${tmp}-d.png"
mask_png="${tmp}-m.png"
nero_png="${tmp}-z.png"
albo_png="${tmp}-a.png"
vert_png="${tmp}-v.png"
refm_png="${tmp}-r.png"
over_png="${tmp}-o.png"

# Convert synthetic image to dim gray:

convert ${synt_png} \
  -colorspace Gray \
  -resize "${spct}%" \
  -brightness-contrast '-35%' \
  ${dull_png}
  
# Get the image dimensions:

ssize=( `identify ${dull_png} | cut -d' ' -f3 | tr 'x' ' '` )
echo "input image reduced size = ${ssize[@]}"

# Convert the reference image to an alpha mask for itself:

convert ${refd_png} \
  -colorspace Gray \
  -resize "${rpct}%" \
  -negate \
  -brightness-contrast '+30%' \
  -alpha Off \
  ${mask_png}

rsize=( `identify ${dull_png} | cut -d' ' -f3 | tr 'x' ' '` )
echo "reference image reduced size = ${rsize[@]}"

# Create an all-green image with the size of the ref mask:
# (the 'canvas:' function does not work in debian):
  
convert ${mask_png} \
  -brightness-contrast '-100%' \
  ${nero_png}
  
convert ${mask_png} \
  -brightness-contrast '+100%' \
  ${albo_png}
 
convert ${nero_png} ${albo_png} ${nero_png} \
  -channel RGB -combine \
  ${vert_png}
 
# Recreate the ref drawing as a green-transparent image:

convert \
  ${vert_png} ${mask_png} -alpha Off \
  -compose Copy_Opacity  \
  -composite ${refm_png}

# Draw the reference image over the dull image:

convert \
  -gravity Center \
  ${dull_png} ${refm_png} \
  -compose Over \
  -composite ${over_png}

osize=( `identify ${over_png} | cut -d' ' -f3 | tr 'x' ' '` )
echo "result image size = ${osize[@]}"

display ${over_png} ${tmp}*.png

cat ${over_png}

rm ${tmp}-*.png

exit 0

