1#!/bin/sh
2#
3# xbm-resize  [ -# | -#x# ]  bitmap...
4#
5#    Using the pbmplus package resize all the given bitmaps or pixmaps
6# to the size given on the command line. The default size is 64x54 pixels
7#
8#  Anthony Thyssen         29 Oct 1993       anthony@cit.gu.edu.au
9#
10
11Width=64    # default width and height of icon to produce
12Height=54
13Margin=35   # margin to add to the icon ( must be >= 1/2 * max(Width,Height) )
14
15b="
16"
17
18Usage() {
19  echo >&2 "Usage: xbm-resize [ -# | -#x# ] bitmap/pixmap..."
20  exit 10
21}
22
23loop=true
24while [ "$loop" ]
25do
26  case "$1" in
27  --) loop= ;;
28  -*x*)           #  resize to fully specified size
29      Width=`expr "$1" : '-\([0-9]*\)'`
30      Height=`expr "$1" : '-[0-9]*x\([0-9]*\)'`
31      size=$Width
32      [ "$size" -lt "$Height" ] && size=$Height
33      Margin=`expr $size / 2 + 1`
34      shift ;;
35  -*)             #  resize to a square of this size
36      if  size=`expr "$1" : '-\([0-9]*\)'`;  then
37        Width=$size
38        Height=$size
39        Margin=`expr $size / 2 + 1`
40        shift
41      else
42        Usage;
43      fi ;;
44  *)  loop= ;;
45  esac
46done
47
48# ----- MAIN LOOP -----
49
50TMP1=/tmp/xbmresize$$.1
51TMP2=/tmp/xbmresize$$.2
52trap "rm -f $TMP1 $TMP2; exit 0" 0
53
54for i in "$@"; do
55  echo -n "${b}  resizing \"$i\"
56"
57  # --- check out this file ---
58  if [ ! -r "$i" ]; then
59    echo -n "${b}"
60    echo >&2 "Unable to read icon \"$i\""
61    continue;
62  fi
63  if [ ! -w "$i" ]; then
64    echo -n "${b}"
65    echo >&2 "xbm-cmd: Unable to write \"$i\""
66    continue;
67  fi
68
69  # --- convert it to PbmPlus ---
70  name="`basename $i`"
71  suffix="`expr "$name" : '.*\.\([^.]*\)'`"
72  name="`expr "$name" : '\([^.]*\)'`"
73
74  case "$suffix" in
75    xbm)   xbmtopbm  > $TMP1 ;;
76    xpm)   xpmtoppm  > $TMP1 ;;
77    *)   echo -n "${b}"
78         echo >&2 "Unknown suffix for \"$i\""
79         continue
80  esac
81
82  # --- find out if we need to make a backup first (on cropped size) ---
83  # NOTE: this does not always work!
84  set - `pnmcrop $TMP1 2>/dev/null | pnmfile | sed 's/.*,//'`
85  [ $? -ne 0 ] && continue
86  if  expr  $1 \> $Width \| $3 \> $Height >/dev/null;  then
87    echo "${b}"
88    echo >&2 "Icon \"$i\" shrinks in size ... making copy"
89    echo -n "  resizing \"$i\"
90"
91    merge -c $i $i
92  fi
93
94  # --- add margins and cut out to correct size ---
95  set - `pnmfile $TMP1 | sed 's/.*,//'`
96  X=`expr \( $1 - $Width \) / 2 + $Margin`
97  Y=`expr \( $3 - $Height - 1 \) / 2 + $Margin`  # -1 to lower picture
98  pnmmargin $Margin $TMP1 | pnmcut $X $Y $Width $Height > $TMP2
99
100  # --- convert back ---
101  case "$suffix" in
102    xbm) pbmtoxbm < $TMP2 > "$i" ;;
103    xpm) ppmtoxpm < $TMP2 2>/dev/null > "$i" ;;
104  esac
105done
106echo "${b}done"
107
108