Ticket #420: diff

File diff, 3.1 kB (added by on, 1 year ago)
  • FuzzyOcr/Deanimate.pm

    old new  
    4747                ." [$retcode], image not reduced!"); 
    4848        } 
    4949    } 
    50     my $retcode = save_execute( 
    51         "$conf->{focr_bin_gifsicle} -o $tfile2 --unoptimize $tfile #$index", 
    52         undef, 
    53         undef, 
    54         ">>$efile"); 
    55     return $tfile2 if ($retcode == 0); 
     50    if ($index >=0) { 
     51        my $retcode =  
     52            save_execute( 
     53                         "$conf->{focr_bin_gifsicle} -o $tfile2 --unoptimize $tfile #$index", 
     54                         undef, 
     55                         undef, 
     56                         ">>$efile"); 
     57        return $tfile2 if ($retcode == 0); 
     58    } 
     59    else { 
     60        # Explode the animation into GIFs 
     61        my $retcode =  
     62            save_execute( 
     63                         "$conf->{focr_bin_gifsicle} -e -o $tfile2 $tfile", 
     64                         undef, 
     65                         undef, 
     66                         ">>$efile");   
     67        if ($retcode != 0) { 
     68            warnlog("$conf->{focr_bin_gifsicle}: cannot explode $tfile"); 
     69            return $tfile; 
     70        } 
     71        my $delays=$info->{'delays'}; 
     72        my $imagecount=@$delays; 
     73 
     74        for (my $i=0; $i<$imagecount; $i++) { 
     75            # change all GIF frames to pnm 
     76            my $ffile=sprintf "%s.%03d", $tfile2, $i; 
     77            my $retcode =  
     78                save_execute( 
     79                             "$conf->{focr_bin_giftopnm} $ffile", 
     80                             undef, 
     81                             ">$ffile.pnm", 
     82                             ">>$efile");   
     83            if ($retcode != 0) { 
     84                warnlog("$conf->{focr_bin_gifsicle}: cannot explode $tfile"); 
     85                return $tfile; 
     86            } 
     87        } 
     88        my $cmd="$conf->{focr_bin_pnmcat} -topbottom "; 
     89        my $cfile="$imgdir/pnmcat"; 
     90        for (my $i=0; $i<$imagecount; $i++) { 
     91            # concat pnm frames 
     92            my $ffile=sprintf "%s.%03d.pnm", $tfile2, $i; 
     93            $cmd.=" $ffile"; 
     94        } 
     95        $retcode =  
     96            save_execute( 
     97                         $cmd, 
     98                         undef, 
     99                         ">$cfile.pnm", 
     100                         ">>$efile");   
     101        if ($retcode != 0) { 
     102            warnlog("$conf->{focr_bin_pnmcat}: cannot contact frames of $tfile"); 
     103            return $tfile; 
     104        } 
     105        $retcode =  
     106            save_execute( 
     107                         "$conf->{focr_bin_ppmtogif} $cfile.pnm", 
     108                         undef, 
     109                         ">$cfile.gif", 
     110                         ">>$efile");   
     111        if ($retcode != 0) { 
     112            warnlog("$conf->{focr_bin_ppmtogif}: cannot convert to GIF $cfile.pnm"); 
     113            return $tfile; 
     114        } 
     115        return "$cfile.gif"; 
     116    } 
    56117    warnlog("$conf->{focr_bin_gifsicle}: cannot extract image#${index}"); 
    57118    return $tfile; 
    58119} 
     
    125186    # Pick out the frame with the longest delay. 
    126187    my $maxdelay = -1e6; 
    127188    my $maxn = @$delays - 1; 
     189    my $totdelay=0; 
    128190    for (my $n = 0; $n < @$delays; $n++) { 
     191        $totdelay+=$delays->[$n]; 
    129192        $delays->[$n] > $maxdelay 
    130193            and ($maxn, $maxdelay) = ($n, $delays->[$n]); 
    131194    } 
    132195 
    133     if ($maxdelay < 15.0 && !$loop) { 
     196    if (!$loop || $loop_count>0) { 
    134197        # In non-looped (or finitely-looped) images, the last frame 
    135198        # gets displayed forever at the end of the animation. 
    136199        # Therefore the last frame is the dominant frame. 
    137200        return @$delays - 1; 
    138201    } 
    139     return $maxn; 
     202    elsif ($maxdelay>$totdelay/2) { 
     203        # If infinite loop with a dominant frame 
     204        # Dominant frame could be: 
     205        #   delay > 50% total duration (default) 
     206        #   delay > x * average delay 
     207        #   etc. 
     208        return $maxn; 
     209    } 
     210    else { 
     211        # If infinite loop with equal, or near equal delays 
     212        return -1; 
     213    } 
    140214} 
    141215