#! /bin/bash
# Last edited on 2011-05-10 21:09:50 by stolfilocal

# Converts a black-on-white diagram to a green-on-black 
# drawing to be mixed with a povray image:

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

tmp=/tmp/$$

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

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"

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

# Create an all-green image with the size of the ref mask:
# (the 'canvas:' function does not work in debian version):
  
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}

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

cat ${refm_png}

rm ${tmp}-*.png

exit 0
