#!/bin/bash
# Copyright 2009-2011 Chad Lemmen http://www.lemmen.com
# 
# Program: img2mac
# Purpose: Create a PCL macro file from an image file. 
# Author : iiug@lemmen.com
# History:
# v1.00 - 07/31/2009
#       * Created
# v1.10 - 02/15/2011
#       Resizing image didn't work because of wrong variable name, changed
#       variable name from size to s.
#
esc=`echo -e "\033"`
ff=`echo -e "\014"`
v=0              # Vertical image position 
h=0              # Horizontal image position 
i=0              # Default macro ID
eval file=\${$#} # Let last argument equal $file
s=100            # Default image size (percent of original size)


showhelp()
{
cat <<eod

Create a PCL macro file to program a macro into a HP Laserjet printer
Any image file that ImageMagick's 'convert' program can convert into postscript
can be used.

The resulting macro file should be sent to the printer each time you need to
print the image to ensure that the macro is there since it will be lost
during a power cycle or if another macro with the same ID is sent to the 
printer.

In an Informix 4GL program you would send the macro file to the printer using
the 4GL 'run' command anywhere in the 4GL program before the report prints:

  run "lp -o raw macro.pcl"

Then to print the image you must call the macro in the 4GL report function

  print ascii 27, "&f14Y", # Specify printer macro ID number 14
        ascii 27, "&f4X"   # Overlay last specified macro ID
    
See the PCL reference manual, macros chapter, for help:
  http://h20000.www2.hp.com/bc/docs/support/SupportManual/bpl13210/bpl13210.pdf


Usage: img2mac [options ...] [-o output filename] file

Options
    -i      Macro ID number, range (0 - 32767)
    -v      Vertical position in PCL decipoints     
    -h      Horizontal position in PCL decipoints
    -s      Resize image, value is a percent of original size
    -o      Output filename

EXAMPLE
    img2mac -i 5 -v 6400 -h 3500 -s 75 -o macro.pcl signature.png

eod
}


while getopts i:v:h:s:o: opt
do
    case "$opt" in
    i)    i="$OPTARG";;
    v)    v="$OPTARG";;
    h)    h="$OPTARG";;
    s)    s="$OPTARG";;
    o)    out="$OPTARG";;
    [?])  printf "Usage: [options ...] [-o output filename] file\n"
          exit 1;;
    esac
done

if [ $OPTIND = 1 ]; then
  showhelp
  exit 0
fi

if [ $1 = -help ]; then
  showhelp
  exit 0
fi

# Test to see if they gave the -o option
#
if [ -z $out ]; then
  echo "-o [option] is required"
  exit
fi


# Convert image file into postscript
convert -resize $s% $file /tmp/image.ps

# Convert postscript file into PCL escape codes
gs -sDEVICE=ljet4 -sOutputFile=/tmp/image.pcl - < /tmp/image.ps >/dev/null 


# We need to extract just the image from the PCL file.
# Remove everything up until <ESC>*r1A (start graphics)
# and remove the form feed ^L and printer reset <ESC>E at the end
sed -e "s/.*\*r1A/$esc\*r1A/; s/$ff$esc\E\$//" /tmp/image.pcl > /tmp/image.new 
 
# Add macro start sequence, resolution, and position commands to top of file
# <ESC>&f#Y <ESC>&f0X <ESC>*t600R <ESC>&a#h#V
echo $esc'&f'$i'Y'$esc'&f0X'$esc'*t600R'$esc'&a'$h'h'$v'V' | 
cat - /tmp/image.new > /tmp/image.pcl

# Add macro stop definition to bottom of file and make it a permanent type
# <ESC>&f1X <ESC>&f10X
echo $esc'&f1X'$esc'&f10X' | cat /tmp/image.pcl - > $out 

# Cleanup
rm -f /tmp/image.pcl
rm -f /tmp/image.ps
rm -f /tmp/image.new

