aboutsummaryrefslogtreecommitdiff
path: root/image2ascii
blob: 729cb4db7743c87a5136f268d36cf7763c98aa62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#! /bin/sh
#
# $ Id: image2ascii,v 1.6 2002/12/01 12:36:56 roland Exp roland $
#
# Convert any image to an ASCII-graphic using ImageMagick
#
##########################################################################
#
#   Copyright (C) 1997-2002  Roland Rosenfeld <roland@spinnaker.de>
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License as
#   published by the Free Software Foundation; either version 2 of
#   the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
##########################################################################

CONVERT=convert		# The ImageMagick convert binary
PBMTOASCII=pbmtoascii	# The NetPBM pbmtoascii binary

umask 077

tmpdir=${TMPDIR-/tmp}/image2ascii.$$
mkdir $tmpdir || exit 1
trap "rm -rf $tmpdir; exit" 0 1 2 3 15

TMPFILE=$tmpdir/image

usage="Usage: $0 [option] [imagefile]

   -help                display this help text
   -geometry 132x50     define the size of the ascii image"


# set default geometry to display width:
geometry=`stty size </dev/tty | awk '{print $2 "x" $1}'`

# test if stty did not output a useful value:
case "$geometry" in
        0x0 ) geometry=80x24 ;;
        ""  ) geometry=80x24 ;;
esac

case $# in 
        0 ) cat > $TMPFILE ;;
        1 ) case "$1" in
                    -* ) echo "$usage"; exit 0 ;;
                    * )  cat "$1" > $TMPFILE ;;
            esac ;;
        2 ) case "$1" in
                    -geometry ) geometry=$2
                                cat > $TMPFILE ;;
                    * )         echo "$usage"; exit 0 ;;
            esac ;;
        3 ) case "$1" in
                    -geometry ) geometry=$2
                                cat $3 > $TMPFILE ;;
                    * )         echo "$usage"; exit 0 ;;
            esac ;;
        * ) echo "$usage"; exit 0 ;;
esac

# multiply x with 2 and y with 4 (pbmtoascii divides by 2x4)
geometry=`echo $geometry | awk -Fx '{print 2*$1 "x" 4*$2}'`

$CONVERT -geometry $geometry $TMPFILE $TMPFILE.pbm
$PBMTOASCII -2x4 < $TMPFILE.pbm