#!/bin/sh

PPZ=$1
BITS=$2
MASK=$3
COL1=$4
COL2=$5
OUT=$6

# create colored tile
gzip  -d -c $PPZ | ppmtorgb3

pnmarith -sub noname.red noname.blu \
 | pgmtoppm Black-${COL1} >tmp.red
pnmarith -sub noname.grn noname.blu \
 | pgmtoppm Black-${COL2} >tmp.grn
pnmarith -add tmp.red tmp.grn >tmp.blu
pnmarith -add tmp.blu noname.blu >tmp.ppm

# cut out mask
pnmarith -mul tmp.ppm $MASK >tmp1.ppm
pnmarith -sub $BITS $MASK >tmp2.ppm

# add images
pnmarith -add tmp1.ppm tmp2.ppm | gzip -v9 > ${OUT}

# garbage collection
rm tmp.* tmp?.* noname.*
