| 1 |
#!/bin/sh |
| 2 |
# $Id: dvitogif89a,v 3.8 2011/03/14 20:14:00 krisrose Exp $ |
| 3 |
# |
| 4 |
# Pack consecutive pages of TeX DVI file as GIF89a animation. |
| 5 |
# Copyright (c) 1996-1997 Kristoffer H. Rose <[email protected]> |
| 6 |
# |
| 7 |
# This file is part of the Xy-pic macro package. |
| 8 |
# Copyright (c) 1991-2011 Kristoffer H. Rose <[email protected]> |
| 9 |
# See the README and INSTALL files for further information. |
| 10 |
# |
| 11 |
# The Xy-pic package is free software; you can redistribute it and/or modify |
| 12 |
# it under the terms of the GNU General Public License as published by the |
| 13 |
# Free Software Foundation; either version 2 of the License, or (at your |
| 14 |
# option) any later version. |
| 15 |
# |
| 16 |
# The Xy-pic package is distributed in the hope that it will be useful, but |
| 17 |
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 18 |
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 19 |
# for more details. |
| 20 |
# |
| 21 |
# You should have received a copy of the GNU General Public License along |
| 22 |
# with this package; if not, see http://www.gnu.org/licenses/. |
| 23 |
|
| 24 |
NAME=`basename $0` |
| 25 |
SYNOPSIS="Usage: $NAME file[.dvi]" |
| 26 |
DESCRIPTION="Pack consecutive pages of TeX DVI file as GIF89a animation." |
| 27 |
|
| 28 |
# Crop efficiently if possible. |
| 29 |
# |
| 30 |
if [ -n "`which pnmrawtopcropwhite`" ] |
| 31 |
then |
| 32 |
cropwhite="pnmrawtopcropwhite |pnmcrop -white" |
| 33 |
elif [ -x ./pnmrawtopcropwhite ] |
| 34 |
then |
| 35 |
cropwhite="./pnmrawtopcropwhite |pnmcrop -white" |
| 36 |
else |
| 37 |
cropwhite="pnmcrop -white" |
| 38 |
fi |
| 39 |
|
| 40 |
# Check files. |
| 41 |
# |
| 42 |
ok=yes |
| 43 |
# |
| 44 |
if [ $# -ne 1 ]; then ok=no; fi |
| 45 |
# |
| 46 |
dvi="$1.dvi" |
| 47 |
if [ ! -f "$dvi" ] |
| 48 |
then |
| 49 |
dvi="$1" |
| 50 |
if [ ! -f "$dvi" ]; then ok=no; fi |
| 51 |
fi |
| 52 |
# |
| 53 |
base=`basename "$dvi" .dvi` |
| 54 |
size="$base.size" |
| 55 |
# |
| 56 |
if [ ! -f "$size" ] |
| 57 |
then |
| 58 |
echo $NAME": could not find $size file!" >&2 |
| 59 |
ok=no |
| 60 |
else |
| 61 |
eval `cat "$size"` |
| 62 |
if [ -z "$dpi" -o -z "$delay" ] |
| 63 |
then |
| 64 |
echo $NAME": could not interpret $size file!" >&2 |
| 65 |
ok=no |
| 66 |
fi |
| 67 |
fi |
| 68 |
# |
| 69 |
if [ $ok = no ] |
| 70 |
then |
| 71 |
echo $SYNOPSIS >&2; exit 2 |
| 72 |
fi |
| 73 |
|
| 74 |
# Initialise. |
| 75 |
# |
| 76 |
echo "${NAME}: Animating $base." |
| 77 |
rm -f $base.ps $base.[0-9][0-9][0-9].* |
| 78 |
# |
| 79 |
exec 2>"$base.log" |
| 80 |
set -x |
| 81 |
# |
| 82 |
date "+${NAME} Animating $dvi (log started %c)" >&2 |
| 83 |
|
| 84 |
# Produce PostScript file! |
| 85 |
# |
| 86 |
echo -n "Producing PostScript..." |
| 87 |
echo "Producing PostScript:" >&2 |
| 88 |
dvips -D $dpi "$base.dvi" -o "$base.ps" |
| 89 |
if [ $? -ne 0 ]; then echo "failed, aborting"; exit 1; else echo "done"; fi |
| 90 |
echo '' >&2 |
| 91 |
|
| 92 |
# Produce PPM files! |
| 93 |
# |
| 94 |
echo -n "Producing PPMs" |
| 95 |
echo "Producing PPMs:" >&2 |
| 96 |
echo '' \ |
| 97 |
| gs -sOutputFile="|$cropwhite >$base.%03d.ppm" \ |
| 98 |
-sDEVICE=ppmraw -r$dpi - "$base.ps" 2>&1 \ |
| 99 |
| while read ln; do |
| 100 |
echo "$ln" >&2 |
| 101 |
case "$ln" in |
| 102 |
*cols*off*the*left) echo -n . ;; |
| 103 |
esac |
| 104 |
done |
| 105 |
if [ $? -ne 0 ]; then echo "failed, aborting"; exit 1; else echo "done"; fi |
| 106 |
echo '' >&2 |
| 107 |
|
| 108 |
# Measure first image and alert user. |
| 109 |
# |
| 110 |
top=0 bottom=0 left=0 right=0 height=0 width=0 |
| 111 |
eval `pnmcrop -black "$base.001.ppm" |pnmfile |\ |
| 112 |
sed -n -e 's/.*[ \t]\([0-9]*\) by \([0-9]*\).*$/ width=\1 height=\2/p'` |
| 113 |
length=`echo $base.[0-9][0-9][0-9].ppm | wc -w | tr -d " \t"` |
| 114 |
# |
| 115 |
echo "Animation has $length frames of ${width}x${height}." |
| 116 |
echo "Length: $length" >&2 |
| 117 |
echo "Image size: ${width}x${height}" >&2 |
| 118 |
|
| 119 |
# Convert to incremental GIFs! |
| 120 |
# |
| 121 |
echo -n "Producing GIFs" |
| 122 |
lastppm= |
| 123 |
for ppm in $base.[0-9][0-9][0-9].ppm |
| 124 |
do |
| 125 |
|
| 126 |
echo -n "." |
| 127 |
echo -e "\nImage: $ppm" >&2 |
| 128 |
|
| 129 |
# filenames |
| 130 |
gif="`basename $ppm .ppm`.gif" |
| 131 |
|
| 132 |
# crop black border [sic] |
| 133 |
mv $ppm $base.000.ppm |
| 134 |
pnmcrop -black $base.000.ppm >$ppm |
| 135 |
|
| 136 |
# produce smallest GIF image and add to gifpack command |
| 137 |
if [ -z "$lastppm" ] |
| 138 |
then |
| 139 |
|
| 140 |
# first bitmap is used directly |
| 141 |
ppmtogif $ppm > $gif |
| 142 |
gifpack="gifpack -delay $delay $loop -output $base.gif $gif" |
| 143 |
|
| 144 |
else |
| 145 |
|
| 146 |
# measure unchanged border |
| 147 |
top=0 bottom=0 left=0 right=0 height=0 width=0 |
| 148 |
measures="\ |
| 149 |
`( pnmarith -difference $lastppm $ppm | pnmcrop | pnmfile )2>&1 \ |
| 150 |
| sed -n \ |
| 151 |
-e 's/.*cropping \([0-9]*\) .* \([a-z]*\)$/\2=\1/p' \ |
| 152 |
-e 's/.* \([0-9]*\) by \([0-9]*\).*$/width=\1 height=\2/p'`" |
| 153 |
eval "$measures" |
| 154 |
|
| 155 |
# if no change just pass top left pixel |
| 156 |
if [ $width -lt 1 -o $height -lt 1 ] |
| 157 |
then |
| 158 |
width=1; height=1; left=1; top=1 |
| 159 |
fi |
| 160 |
|
| 161 |
# add GIF with changed rectangle to animation |
| 162 |
pnmcut $left $top $width $height $ppm | ppmtogif >$gif |
| 163 |
gifpack="$gifpack +$left,+$top $gif" |
| 164 |
|
| 165 |
fi |
| 166 |
|
| 167 |
# prepare next image |
| 168 |
lastppm=$ppm |
| 169 |
|
| 170 |
done |
| 171 |
echo "done" |
| 172 |
echo '' >&2 |
| 173 |
|
| 174 |
echo -n "Packing GIFs..." |
| 175 |
echo -e "\nPacking animation:" >&2 |
| 176 |
eval $gifpack >&2 |
| 177 |
if [ $? -ne 0 ]; then echo "failed, aborting"; exit 1; else echo "done"; fi |
| 178 |
echo '' >&2 |
| 179 |
|
| 180 |
# Clean up. |
| 181 |
# |
| 182 |
rm -f $base.[0-9][0-9][0-9].* |
| 183 |
|
| 184 |
# Done. |
| 185 |
# |
| 186 |
echo Done. >&2 |
| 187 |
exit |
| 188 |
|
| 189 |
# $Log: dvitogif89a,v $ |
| 190 |
# Revision 3.8 2011/03/14 20:14:00 krisrose |
| 191 |
# Preparing for release 3.8.6. |
| 192 |
# |
| 193 |
# Revision 3.7 2010/06/10 18:45:49 krisrose |
| 194 |
# Reference to GPL by URL. |
| 195 |
# |
| 196 |
# Revision 3.6 2010/04/16 06:06:51 krisrose |
| 197 |
# Preparing for a new release... |
| 198 |
# |
| 199 |
# Revision 3.5 1997/05/28 13:05:01 krisrose |
| 200 |
# Fixed missing breaks bug. |
| 201 |
# |
| 202 |
# Revision 3.4 1997/05/18 01:14:25 krisrose |
| 203 |
# Essential bugfixes. |
| 204 |
# |
| 205 |
# Revision 1.3 1997/05/01 21:32:32 krisrose |
| 206 |
# Changed to exploit pnmrawtopcropwhite hack if available... |
| 207 |
# Output cleaned up; uses log file for the rest. |
| 208 |
# |
| 209 |
# Revision 1.2 1997/05/01 19:23:16 krisrose |
| 210 |
# Doesn't quite work... |
| 211 |
# |
| 212 |
# Revision 1.1 1997/02/11 04:19:23 krisrose |
| 213 |
# Initial revision |
| 214 |
# |
| 215 |
# Based on MakeTeXmovie... |