#!/bin/sh - # # gif2anim [options] image_anim.gif... # # This script uses the ImageMagick 'convert' program to take a gif animation # (normally ending in '_anim.gif' and NOT just '.gif') and converts it into a # collection of X Pixmap files (or GIFs) as well as a "animation sequence # file" which lists all settings needed to re-build the original GIF # animation. # # The animation file uses the string "BASENAME" to represent the basenames for # the individual frame images that it loads when re-creating the original GIF # animation. This allows you to specify a different set of images without # changling any other settings. # # The resulting animation file can be used to re-build the animation either # using the IM options contained (ignoring comments), or by giving the file to # the "anim2gif" script which will also do the basename filename substitutions # specified. # # OPTIONS # -d 1 For debugging # It also outputs to the terminal. # -c 1 coalesce animation before parsing # -t 1 Add time synchronization comment before each frame # -l 1 just list the anim file to stdout, no images # -v 1 Be verbose in animation conversions # -n 1 no images, just create the '.anim' file # -g 1 use an GIF suffix for frame images (default) # -x 1 use an XPM suffix for frame images # None support # -s suffix use this suffix for the frame images # -i initframe number of the first frame image (def=1) # -b framename basename for the individual frames # -o file.anim output to this .anim file (- for stdout) # # NOTE: This script reqires both the ImageMagick 'convert' program and # 'identify' programs to extract the images and information needed. # #### # # WARNING: Input arguments are NOT tested for correctness. # This script represents a security risk if used ONLINE. # I accept no responsiblity for misuse. Use at own risk. # # Anthony Thyssen 25 April 1996 # # 1 November 2005 Expanded with options for basename handling. # # Hiroshi Kamifuji 2022-02-03 # Ported UNIX-like sh to PowerShell. # Changed the method of specifying run-time arguments. # # Param( [String]$in, [Int]$d = 0, [Int]$c = 0, [Int]$t = 0, [Int]$l = 0, [Int]$v = 0, [Int]$n = 0, [String]$s = "", [Int]$g = 0, [Int]$x = 0, [Int]$i = 0, [String]$b = "", [String]$o ="" ) if ( $in -eq "" ) { Write-Host "Usage : gif2anim [-c 1] [-t 1] [-l 1] [-v 1] [-n 1] [-s 1] [-g 1] [-x 1] [-i 1] [-b 1] [-o 1] in " Exit } $sfx="gif" # Default output suffix of images to produce $init=1 # the count for the first frame in sequence $fmt="%03d" # The format for frame count $frames="true" # output frame images by defult $basename="" # use this name for the individual frame images $animfile="" # no default animfile to output to. $all_gifs="" # forcefully convert any ".gif" file $coalesce="" # coalesce flag (off) $VERBOSE= "" # be verbose (off) $time_sync=0 # add time sync comments (off) $awk="awk" $dbg = 0 $basename, $insfx = $in.Split(".") if ( $c -gt 0 ) { $coalesce="-coalesce" # coalesce animation (on) } if ( $t -gt 0 ) { $time_sync = 1 # add time sync comments (on) } if ( $l -gt 0 ) { $frames= # List anim file to standard output, no images $animfile="-" } if ( $v -gt 0 ) { $VERBOSE="true" # Be verbose } if ( $n -gt 0 ) { $frames="" # don't output any individual frames } if ( $s ) { $sfx = $s # output frame images with this suffix } elseif ( $g -gt 0 ) { $sfx = "gif" # output frame images as GIF's (default) } elseif ( $x -gt 0 ) { $sfx = "xpm" # output frame images as XPM's Write-Host "Not supported then exit." Exit } if ( $i -gt 0 ) { $init=$i # the count for the first frame in sequence } if ( $b ) { $basename=$b # use this basename for frames } if ( $o ) { $animfile=$o # output .anim file here } if ( $d ) { $dbg = 1 } # echo "input : $in" # echo "coalesce : $coalesce" # echo "time_sync : $time_sync" # echo "frames : $frames" # echo "animfile : $animfile" # echo "VERBOSE : $VERBOSE" # echo "frames : $frames" # echo "sfx : $sfx" # echo "init : $init" # echo "basename : $basename" # echo "animfile : $animfile" $im6 = (gcm montage).Definition 2> -null $im7 = (gcm magick).Definition 2> -null # echo "im6 : $im6" # echo "im7 : $im7" if ( $im7 ) { $method = 1 $magick = "magick" } elseif ( $im6 ) { $method =2 $magick = "" } else { echo "ImageMagick cannot be found and cannot be executed." Exit } # echo "magick : $magick" # echo "method : $method" switch ( $method ) { # Execute by ImageMagick7.x 1 { $Inf = magick convert "$in" $coalesce -verbose info:- } # Execute by ImageMagick6.x 2 { $Inf = convert "$in" $coalesce -verbose info:- } } $anim_output = (pwd).Path + "\$basename" + ".anim" # echo "anim_output : $anim_output" # $enc_utf8_with_bom = new-object System.Text.UTF8Encoding $true $enc_utf8_without_bom = new-object System.Text.UTF8Encoding $false $file = New-Object System.IO.StreamWriter($anim_output, $false, $enc_utf8_without_bom ) # $file = New-Object System.IO.StreamWriter($anim_output, $false, [System.Text.Encoding]::GetEncoding("UTF-8")) $loop = 0 $canvas_set = 0 # BEGIN { print "#"; # print "# Animation Sequence File \"'"$i"'\""; # print "# using a BASENAME of \"'"$name"'\""; # print "# Extracted on '"$date"'"; # print "#"; # frame='"$init"' # last_offset="+0+0"; # time_sync='"$time_sync"'; # time=0; # } if ( $dbg ) { Write-Host "#" } $file.WriteLine( "#" ) if ( $dbg ) { Write-Host "# Animation Sequence File `"$in`"" } $file.WriteLine( "# Animation Sequence File `"$in`"" ) $name, $wk = $in.Split(".") if ( $dbg ) { Write-Host "# using a BASENAME of `"$name`"" } $file.WriteLine( "# using a BASENAME of `"$name`"" ) # $dt = date $dt = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") if ( $dbg ) { Write-Host "# Extracted on $dt" } $file.WriteLine( "# Extracted on $dt" ) if ( $dbg ){ Write-Host "#" } $file.WriteLine( "#" ) $frame = $init $last_offset = "+0+0" # $time_sync='"$time_sync"'; $time = 0 $wkk1 = "" foreach ($ln in $Inf) { $lnd = $ln.Split(" ") for ($i=0; $i -lt $lnd.Length; $i++) { if ( $lnd[$i] ) { break } } # /^ Iterations:/ { if ( ! loop ) { # print "-loop " $NF; # loop = 1; # } } if ( $lnd[$i] -eq "Iterations:" ) { if ( ! $loop ) { $wkk = $lnd[$i + 1] if ( $dbg ){ Write-Host "-loop $wkk" } $file.WriteLine( "-loop $wkk" ) $loop = 1 } } # /^ Delay:/ { delay = $NF; sub("x100$", "", delay); } if ( $lnd[$i] -eq "Delay:" ) { $delay, $wk = $lnd[$i + 1].Split("x") } # /^ Dispose:/ { dispose = $NF; } if ( $lnd[$i] -eq "Dispose:" ) { $dispose = $lnd[$i + 1] } # /^ Geometry:/ { size = substr($NF,0,match($NF, /\+/ )-1 ); } if ( $lnd[$i] -eq "Geometry:" ) { $size, $wk = $lnd[$i + 1].Split("+") } # /^ Page geometry:/ { canvas = substr($NF,0,match($NF, /\+/ )-1 ); # offset = substr($NF, match($NF, /\+/ ) ); # } if (( $lnd[$i] -eq "Page" ) -and ( $lnd[$i + 1] -eq "geometry:" )) { $canvas, $w1, $w2 = $lnd[$i + 2].Split("+") $offset = "+$w1+$w2" } # # End of Image Frame -- output collected image meta-data changes # /^ Tainted: / { # if ( ! canvas_set ) { # print "-page " canvas; # canvas_set = 1; # } if ( $lnd[$i] -eq "Tainted:" ) { if ( ! $canvas_set ) { if ( $dbg ) { Write-Host "-page $canvas" } $file.WriteLine( "-page $canvas" ) $canvas_set = 1; } # if ( time_sync ) printf "# TIME SYNC %d\n", time; if ( $time_sync ) { if ( $dbg ) { Write-Host "# TIME SYNC $time" } $file.WriteLine( "# TIME SYNC $time" ) } # if ( delay != last_delay ) { # print "-delay " delay; # last_delay = delay; # } if ( $delay -ne $last_delay ) { if ( $dbg ) { Write-Host "-delay $delay" } $file.WriteLine( "-delay $delay" ) $last_delay = $delay } # time+=delay; $time += $delay # delay=0; # no delay is given if it remains set to zero $delay = 0 # if ( dispose != last_dispose ) { # print "-dispose " dispose; # last_dispose = dispose; # } if ( $dispose -ne $last_dispose ) { if ( $dbg ) { Write-Host "-dispose $dispose" } $file.WriteLine( "-dispose $dispose" ) $last_dispose = $dispose } # if ( offset != last_offset ) { # printf "%-16s ", "-page " offset; # last_offset = offset; # } else { # printf "%-16s ", ""; # } # printf "BASENAME_'"$fmt.$sfx"' # %s%s\n", # frame++, size, offset; if ( $offset -ne $last_offset ) { $wkk1 = [String]::Format("-page {0,-6} ", $offset) # Write-Host -NoNewline $wkk $last_offset = $offset } else { $wkk1 = " " } $wkk = "{0:D3}" -f $frame++ if ( $dbg ) { Write-Host "$wkk1 BASENAME_$wkk.$sfx # $size$offset" } $file.WriteLine( "$wkk1 BASENAME_$wkk.$sfx # $size$offset" ) # } } } # END { if ( time_sync ) printf "# LOOP TIME %d\n", time; } if ( $time_sync ) { if ( $dbg ) { Write-Host "# LOOP TIME $time" } $file.WriteLine( "# LOOP TIME $time" ) } $file.Close() # if [ "$frames" ]; then # convert "$i" $coalesce \ # +repage -set delay 0 -set dispose none -loop 0 \ # -scene $init +adjoin "${name}_${fmt}.${sfx}" # switch ( $method ) { # Execute by ImageMagick7.x 1 { magick convert "$in" $coalesce ` +repage -set delay 0 -set dispose none -loop 0 ` -scene $init +adjoin "${name}_${fmt}.${sfx}" } # Execute by ImageMagick6.x 2 { convert "$in" $coalesce ` +repage -set delay 0 -set dispose none -loop 0 ` -scene $init +adjoin "${name}_${fmt}.${sfx}" } }