#!/bin/sh # interlace-gif.sh --- make a gif be interlaced, or not. # Copyright © 1996, 2000 Jamie Zawinski # # Permission to use, copy, modify, distribute, and sell this software and its # documentation for any purpose is hereby granted without fee, provided that # the above copyright notice appear in all copies and that both that # copyright notice and this permission notice appear in supporting # documentation. No representations are made about the suitability of this # software for any purpose. It is provided "as is" without express or # implied warranty. # # Requires the following programs: # # giftrans (1.11.1) # giftoppm or giftopnm (any version) # ppmtogif (netpbm 1) # If you have giftopnm but not giftoppm, change this line: giftoppm=giftopnm #giftoppm=giftoppm usage() { echo "usage: $0 [-interlace|-deinterlace] GIFs ..." exit 1 } set -e #set -x interlacep=True case $1 in -deinterlace ) interlacep=False shift ;; -interlace ) interlacep=True shift ;; -* ) usage ;; esac TMP=/tmp/IG$$ TMP2=/tmp/IG$$2 find_transparent_color() { file="$1" giftrans -L $file > $TMP 2>&1 index=`sed -n "s@.*Transparent Color Index: \(.*\)@\1@p" < $TMP` color=`sed -n "s@.*Color ${index}: .*#\([0-9a-fA-F][0-9a-fA-F]*\).*@\1@p" \ < $TMP` rm -f $TMP if [ "$color" = "" ]; then echo "$0: couldn't determine transparent color of $file" >&2 else echo $color fi } for file in $* ; do intp=`giftrans -L $file 2>&1 | sed -n 's@.*Interlace Flag: \(.*\)@\1@p'` if [ "$interlacep" = True -a x"$intp" = xTrue ]; then echo "$file: gif is already interlaced." >&2 elif [ "$interlacep" = False -a x"$intp" = xFalse ]; then echo "$file: gif is already non-interlaced." >&2 else transp=`giftrans -L $file 2>&1 | sed -n 's@.*Transparent Color Flag: \(.*\)@\1@p'` color="" trans_args="" if [ x"$transp" = xTrue ]; then if ( ppmtogif -help 2>&1 | grep transparent >&- ) then color=`find_transparent_color $file` trans_args="-transparent #$color" else echo "$0: ppmtogif does not support the -transparent flag." exit 1 fi fi if [ x"$transp" = xTrue -a "$color" = "" ]; then echo -n #noop else if [ "$interlacep" = True ]; then if ( ppmtogif -help 2>&1 | grep interlace >&- ) then echo "$0: converting $file to an interlaced gif..." >&2 else echo "$0: ppmtogif does not support the -interlace flag." exit 1 fi else echo "$0: converting $file to a non-interlaced gif..." >&2 fi rm -f $TMP cp $file $TMP if [ "$interlacep" = True ]; then $giftoppm < $TMP | ( ppmtogif $trans_args -interlace > $TMP2 2>&- ) else $giftoppm < $TMP | ( ppmtogif $trans_args > $TMP2 2>&- ) fi ls -lFi $TMP $TMP2 rm -f $TMP cp -p $TMP2 $file ls -lFi $TMP2 $file rm -f $TMP2 ls -lF $file fi fi done