#!/bin/sh
# Usage: icon2xbm icon-file > <xbm-file>
# Converts a Sun (64x64) monochrome .icon file to a .xbm file
# 960321, Marcel Waldvogel
#
get() {
  # Extract the variable named $1 from the first line in $2
  sed -n -e "s/.* $1=\([0-9]*\).*/\1/p" -e q $2
}

Width=`get Width $1`
Height=`get Height $1`
echo "Converting ${Width}x${Height} icon file $1 to xbm format" >&2
echo "#define xbm_width $Width"
echo "#define xbm_height $Height"
echo "static char xbm_bits[] = { "
# Make shorts to chars and reverse the bits in the byte
# The bits in a nibble are swapped with the y command, the splitting
# and nibble-swapping is done in the s command
#           [ Nibble 1 ][ Nibble 2 ][ Nibble 3 ][ Nibble 4 ] -> 2 1    4 3
sed -e 's/0x\([0-9A-F]\)\([0-9A-F]\)\([0-9A-F]\)\([0-9A-F]\)/0x\2\1,0x\4\3/g' \
    -e '/0x/y/0123456789ABCDEF/084C2A6E195D3B7F/' $1
echo "}"
