#!/bin/sh -
#
# xpm-mask [-e] [-b] bitmap/pixmap...
#
#   Given a X bitmap or Xpixmap file look for a mask file or if none
# is found create a temporary one, and use it to create a X Pixmap with
# approprate transparency. By default this program assumes the background is
# white in the original image which works with already transparent X pixmaps.
#
# This is not easy to do under PbmPlus but via a kludge and selected a
# specific color as the transparency this can be done.
#
# The options below are used to create a mask, only if no mask is
# already present. The same method as xbm-mask is used internaly for
# this mask creation, but the mask file is not created in the current
# directory.
#
# OPTIONS  
#    -b     Background is black - not white
#    -e     Expand mask by one pixel to form a border
#
# Anthony Thyssen    2 September 1995    <anthony@cit.gu.edu.au>
#
b="                                                                        
"
TRANS="#BFBFBF"   # Color used to represent the tranparent color in pbmplus
white=true

# ---- Process options ----
Usage() {
  echo >&2 "Usage: xpm-mask [-e] [-b] bitmap/pixmap..."
  exit 10
}

loop=true
while [ "$loop" ]
do
  case "$1" in
  --) loop= ;;
  -b) white=; shift ;;          # Black background
  -e) expand=-expand; shift ;;  # Expand mask border
  -*) Usage ;;
  *)  loop= ;;
  esac
done

# ---- Process Files ----
TMP1=/tmp/xpm-mask$$.1
TMP2=/tmp/xpm-mask$$.2
TMP3=/tmp/xpm-mask$$.3
trap "rm -f $TMP1 $TMP2 $TMP3; exit 1" 1 2 3 15

for i in "$@"; do
  echo -n "${b}  masking \"$i\"
"
  # --- check out this file ---
  if [ ! -r "$i" ]; then
    echo -n "${b}"
    echo >&2 "Unable to read icon \"$i\""
    continue;
  fi

  # --- convert Original to PbmPlus ---
  name="`expr "//$i" : '.*/\([^/]*\)'`"               # remove path
  suffix="`expr "$name" : '.*\.\([^.]*\)'`"
  name="`expr "$name" : '\([^.]*\)'`"
  name="`basename $name _masked`"
  name="`basename $name _recol`"
  name="`basename $name _recol_bad`"

  case "$suffix" in
    xbm)       sed 's/unsigned //' "$i" | xbmtopbm > $TMP1 ;;
    xpm|recol) x2p.sed "$i" | xpmtoppm  > $TMP1 ;;
    *)   echo -n "${b}"
         echo >&2 "Unknown suffix for \"$i\""
         continue ;;
  esac
  if [ ! -f $TMP1 -o ! -s $TMP1 ]; then
    echo -n "${b}"
    echo >&2 "Unable to Convert \"$i\" to Pbmplus"
    continue
  fi

  # --- convert/create mask ---
  if [ -f "${name}_mask.xbm" ]; then
    sed 's/unsigned //' "${name}_mask.xbm" | xbmtopbm > $TMP2
  else
    if [ "$white" ]
    then  pnminvert $TMP1
    else  cat $TMP1
    fi | ppmtopgm | pnmnoraw | sed '4,$s/[1-9][0-9]*/255/g' |
      pgmtopbm -threshold | pbmmask $expand > $TMP2
  fi 
  if [ ! -f $TMP2 -o ! -s $TMP2 ]; then
    echo -n "${b}"
    echo >&2 "Unable to Create/Find Mask for \"$i\""
    continue
  fi

  # --- Mask the original file ---
  pnminvert $TMP2 | pgmtoppm "#FFF" | pnmarith -multiply $TMP1 - > $TMP3
  
  # --- change mask into transparent mask ---
  pnmdepth 255 $TMP2 2>/dev/null | pgmtoppm "$TRANS" > $TMP1

  # --- Merge transparent mask and image ---
  pnmarith -add $TMP1 $TMP3 | ppmtoxpm 2>/dev/null |
      sed "/colors/,/pixels/s/c $TRANS/c None/" | # substitute
      xpm-fix -o "${name}_masked.xpm"

done
echo "${b}Done"

rm -f $TMP1 $TMP2 $TMP3

