summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/xypic/support/dvitogif89a
blob: c2d4385b798419828ba39f76f9a624307e2f2f37 (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
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
215
#!/bin/sh
# $Id: dvitogif89a,v 3.8 2011/03/14 20:14:00 krisrose Exp $
#
# Pack consecutive pages of TeX DVI file as GIF89a animation.
# Copyright (c) 1996-1997  Kristoffer H. Rose  <krisrose@tug.org>
#
# This file is part of the Xy-pic macro package.
# Copyright (c) 1991-2011  Kristoffer H. Rose  <krisrose@tug.org>
# See the README and INSTALL files for further information.
#
# The Xy-pic package 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.
#
# The Xy-pic package 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 package; if not, see http://www.gnu.org/licenses/.

NAME=`basename $0`
SYNOPSIS="Usage: $NAME file[.dvi]"
DESCRIPTION="Pack consecutive pages of TeX DVI file as GIF89a animation."

# Crop efficiently if possible.
#
if [ -n "`which pnmrawtopcropwhite`" ]
then
  cropwhite="pnmrawtopcropwhite |pnmcrop -white"
elif [ -x ./pnmrawtopcropwhite ]
then
  cropwhite="./pnmrawtopcropwhite |pnmcrop -white"
else
  cropwhite="pnmcrop -white"
fi

# Check files.
#
ok=yes
#
if [ $# -ne 1 ]; then ok=no; fi
#
dvi="$1.dvi"
if [ ! -f "$dvi" ]
then
  dvi="$1"
  if [ ! -f "$dvi" ]; then ok=no; fi
fi
#
base=`basename "$dvi" .dvi`
size="$base.size"
#
if [ ! -f "$size" ]
then
  echo $NAME": could not find $size file!" >&2
  ok=no
else
  eval `cat "$size"`
  if [ -z "$dpi" -o -z "$delay" ]
  then
    echo $NAME": could not interpret $size file!" >&2
    ok=no
  fi
fi
#
if [ $ok = no ]
then
  echo $SYNOPSIS >&2; exit 2
fi

# Initialise.
#
echo "${NAME}: Animating $base."
rm -f $base.ps $base.[0-9][0-9][0-9].*
#
exec 2>"$base.log"
set -x
#
date "+${NAME} Animating $dvi (log started %c)" >&2

# Produce PostScript file!
#
echo -n "Producing PostScript..."
echo "Producing PostScript:" >&2
dvips -D $dpi "$base.dvi" -o "$base.ps"
if [ $? -ne 0 ]; then echo "failed, aborting"; exit 1; else echo "done"; fi
echo '' >&2

# Produce PPM files!
#
echo -n "Producing PPMs"
echo "Producing PPMs:" >&2
echo '' \
| gs -sOutputFile="|$cropwhite >$base.%03d.ppm" \
     -sDEVICE=ppmraw -r$dpi - "$base.ps" 2>&1 \
| while read ln; do
    echo "$ln" >&2
    case "$ln" in
      *cols*off*the*left) echo -n . ;;
    esac
  done
if [ $? -ne 0 ]; then echo "failed, aborting"; exit 1; else echo "done"; fi
echo '' >&2

# Measure first image and alert user.
#
top=0 bottom=0 left=0 right=0 height=0 width=0
eval `pnmcrop -black "$base.001.ppm" |pnmfile |\
      sed -n -e 's/.*[ \t]\([0-9]*\) by \([0-9]*\).*$/ width=\1 height=\2/p'`
length=`echo $base.[0-9][0-9][0-9].ppm | wc -w | tr -d " \t"`
#
echo "Animation has $length frames of ${width}x${height}."
echo "Length: $length" >&2
echo "Image size: ${width}x${height}" >&2

# Convert to incremental GIFs!
#
echo -n "Producing GIFs"
lastppm=
for ppm in $base.[0-9][0-9][0-9].ppm
do

  echo -n "."
  echo -e "\nImage: $ppm" >&2

  # filenames
  gif="`basename $ppm .ppm`.gif"

  # crop black border [sic]
  mv $ppm $base.000.ppm
  pnmcrop -black $base.000.ppm >$ppm

  # produce smallest GIF image and add to gifpack command
  if [ -z "$lastppm" ]
  then

    # first bitmap is used directly
    ppmtogif $ppm > $gif
    gifpack="gifpack -delay $delay $loop -output $base.gif $gif"

  else
  
    # measure unchanged border
    top=0 bottom=0 left=0 right=0 height=0 width=0
    measures="\
     `( pnmarith -difference $lastppm $ppm | pnmcrop | pnmfile )2>&1 \
        | sed -n \
              -e 's/.*cropping \([0-9]*\) .* \([a-z]*\)$/\2=\1/p' \
              -e 's/.* \([0-9]*\) by \([0-9]*\).*$/width=\1 height=\2/p'`"
    eval "$measures"

    # if no change just pass top left pixel
    if [ $width -lt 1 -o $height -lt 1 ]
    then
      width=1; height=1; left=1; top=1
    fi

    # add GIF with changed rectangle to animation
    pnmcut $left $top $width $height $ppm | ppmtogif >$gif
    gifpack="$gifpack +$left,+$top $gif"

  fi

  # prepare next image
  lastppm=$ppm

done
echo "done"
echo '' >&2

echo -n "Packing GIFs..."
echo -e "\nPacking animation:" >&2
eval $gifpack >&2
if [ $? -ne 0 ]; then echo "failed, aborting"; exit 1; else echo "done"; fi
echo '' >&2

# Clean up.
#
rm -f $base.[0-9][0-9][0-9].*

# Done.
#
echo Done. >&2
exit

# $Log: dvitogif89a,v $
# Revision 3.8  2011/03/14 20:14:00  krisrose
# Preparing for release 3.8.6.
#
# Revision 3.7  2010/06/10 18:45:49  krisrose
# Reference to GPL by URL.
#
# Revision 3.6  2010/04/16 06:06:51  krisrose
# Preparing for a new release...
#
# Revision 3.5  1997/05/28 13:05:01  krisrose
# Fixed missing breaks bug.
#
# Revision 3.4  1997/05/18 01:14:25  krisrose
# Essential bugfixes.
#
# Revision 1.3  1997/05/01 21:32:32  krisrose
# Changed to exploit pnmrawtopcropwhite hack if available...
# Output cleaned up; uses log file for the rest.
#
# Revision 1.2  1997/05/01 19:23:16  krisrose
# Doesn't quite work...
#
# Revision 1.1  1997/02/11 04:19:23  krisrose
# Initial revision
#
# Based on MakeTeXmovie...