about summary refs log tree commit diff
path: root/editor/pnmindex.sh
blob: dfc5b82aa3edea80ca140ad9c2504375472b7ad8 (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/sh
#
# pnmindex - build a visual index of a bunch of PNM images
#
# Copyright (C) 1991 by Jef Poskanzer.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation.  This software is provided "as is" without express or
# implied warranty.

size=100        # make the images about this big
across=6        # show this many images per row
colors=256      # quantize results to this many colors
back="-white"   # default background color
doquant=true    # quantize or not
title=""        # default title (none)

usage ()
{
  echo "usage: $0 [-size N] [-across N] [-colors N] [-black] pnmfile ..."
  exit 1
}

while :; do
    case "$1" in

    -s*)
        if [ $# -lt 2 ]; then usage; fi
        size="$2"
        shift
        shift
    ;;

    -a*)
        if [ $# -lt 2 ]; then usage; fi
        across="$2"
        shift
        shift
    ;;

    -t*)
        if [ $# -lt 2 ]; then usage; fi
        title="$2"
        shift
        shift
    ;;

    -c*)
        if [ $# -lt 2 ]; then usage; fi
        colors="$2"
        shift
        shift
    ;;

    -b*)
        back="-black"
        shift
    ;;

    -w*)
        back="-white"
        shift
    ;;

    -noq*)
        doquant=false
        shift
    ;;

    -q*)
        doquant=true
        shift
    ;;

    -*)
        usage
    ;;

    *)
        break
    ;;
    esac
done

if [ $# -eq 0 ]; then
    usage
fi

tempdir="${TMPDIR-/tmp}/pnmindex.$$"
mkdir -m 0700 $tempdir || \
  { echo "Could not create temporary file. Exiting."; exit 1;}
trap 'rm -rf $tempdir' 0 1 3 15

tmpfile=$tempdir/pi.tmp
maxformat=PBM

rowfiles=()
imagefiles=()
row=1
col=1

if [ "$title"x != ""x ] ; then
#    rowfile=`tempfile -p pirow -m 600`
    rowfile=$tempdir/pi.${row}
    pbmtext "$title" > $rowfile
    rowfiles=(${rowfiles[*]} $rowfile )
    row=$(($row + 1))
fi

for i in "$@"; do

    description=(`pnmfile $i`)

    format=${description[1]}
    width=${description[3]}
    height=${description[5]}

    if [ $? -ne 0 ]; then
        echo pnmfile returned an error
        exit $?
    fi

    if [ $width -le $size ] && \
       [ $height -le $size ]; then
        cat $i > $tmpfile
    else
        case $format in

        PBM) 
            pamscale -quiet -xysize $size $size $i | pgmtopbm > $tmpfile
        ;;

        PGM)
            pamscale -quiet -xysize $size $size $i > $tmpfile
            if [ $maxformat = PBM ]; then
                maxformat=PGM
            fi
        ;;

        *) 
            if [ "$doquant" = "true" ] ; then
                pamscale -quiet -xysize $size $size $i | \
                pnmquant -quiet $colors > $tmpfile
            else
                pamscale -quiet -xysize $size $size $i > $tmpfile
            fi
            maxformat=PPM
        ;;
        esac
    fi

    imagefile=$tempdir/pi.${row}.${col}
    rm -f $imagefile
    if [ "$back" = "-white" ]; then
        pbmtext "$i" | pnmcat $back -tb $tmpfile - > $imagefile
    else
        pbmtext "$i" | pnminvert | pnmcat $back -tb $tmpfile - > $imagefile
    fi
    imagefiles=( ${imagefiles[*]} $imagefile )

    if [ $col -ge $across ]; then
        rowfile=$tempdir/pi.${row}
        rm -f $rowfile

        if [ $maxformat != PPM -o "$doquant" = "false" ]; then
            pnmcat $back -lr -jbottom ${imagefiles[*]} > $rowfile
        else
            pnmcat $back -lr -jbottom ${imagefiles[*]} | \
            pnmquant -quiet $colors > $rowfile
        fi

        rm -f ${imagefiles[*]}
        unset imagefiles
        imagefiles=()
        rowfiles=( ${rowfiles[*]} $rowfile )
        col=1
        row=$(($row + 1))
    else
        col=$(($col + 1))
    fi
done

# All the full rows have been put in row files.  
# Now put the final partial row in its row file.

if [ ${#imagefiles[*]} -gt 0 ]; then
    rowfile=$tempdir/pi.${row}
    rm -f $rowfile
    if [ $maxformat != PPM -o "$doquant" = "false" ]; then
        pnmcat $back -lr -jbottom ${imagefiles[*]} > $rowfile
    else
        pnmcat $back -lr -jbottom ${imagefiles[*]} | \
        pnmquant -quiet $colors > $rowfile
    fi
    rm -f ${imagefiles[*]}
    rowfiles=( ${rowfiles[*]} $rowfile )
fi

if [ ${#rowfiles[*]} -eq 1 ]; then
    cat $rowfiles
else
    if [ $maxformat != PPM -o "$doquant" = "false" ]; then
        pnmcat $back -tb ${rowfiles[*]}
    else
        pnmcat $back -tb ${rowfiles[*]} | pnmquant -quiet $colors
    fi
fi
rm -f ${rowfiles[*]}

exit 0