#!/usr/local/bin/perl use Getopt::Long; use MLDBM qw(DB_File Storable); my %Files = ( db_hash => '/etc/mail/spamassassin/FuzzyOcr.db', db_safe => '/etc/mail/spamassassin/FuzzyOcr.safe.db', ); my %App = ( identify => '/usr/bin/identify', ppmhist => '/usr/local/netpbm/bin/ppmhist', jpegtopnm => '/usr/local/netpbm/bin/jpegtopnm', giftopnm => '/usr/local/netpbm/bin/giftopnm', pngtopnm => '/usr/local/netpbm/bin/pngtopnm', bmptopnm => '/usr/local/netpbm/bin/bmptopnm', ); my $delete = 0; my $verbose = 0; GetOptions( 'verbose' => \$verbose, 'delete' => \$delete, ); while (@ARGV) { my $file = shift @ARGV; my @data = (); if ($file =~ m/(\d+):(\d+):(\d+):(\d+)/) { push @data, $1,$2,$3,$4; } else { next unless -r $file; } my $key = ''; unless (@data) { my $res = `/usr/bin/identify $file`; my ($h,$w) = (0,0); if ($res =~ /(\d+)x(\d+)/) { $h=$1;$w=$2; printf "Found ($h,$w)\n" if $verbose } my $app; if ($res =~ m/JPEG/) { $app = $App{jpegtopnm}; } elsif ($res =~ m/PNG/) { $app = $App{pngtopnm}; } elsif ($res =~ m/BMP/) { $app = $App{bmptopnm}; } elsif ($res =~ m/PNM/) { $app = '/bin/cat'; } else { $app = $App{giftopnm}; } my @hist = `$app $file |$App{ppmhist} -noheader -`; my $c = scalar(@hist); my $cnt = 0; printf "Colors: %d\n",$c if $verbose; push @data, (stat($file))[7],$h,$w,$c; foreach (@hist) { $_ =~ s/ +/ /g; my @d = split(' ',$_); $hash .= sprintf("::%d:%d:%d:%d:%d",@d); last if ($cnt++ ge 5); } $key = substr($hash,2); } printf "Img = %9d %dx%dx%d\n",@data; printf "key = <$key>\n" if ($key); foreach my $ff (keys %Files) { my %DB; tie %DB, 'MLDBM', $Files{$ff} or next; printf "Searching $Files{$ff}...\n"; foreach my $kk (keys %DB) { my $db = $DB{$kk}; my @dd = split('::',$kk); shift @dd if ($dd[0] !~ m/:/); my $dd = join('::',@dd); if ($key eq '') { next unless ($db->{basic} eq join(':',@data)); } else { next unless ($dd eq $key); } printf "%s HASH\n",($delete)?'Removing':'Found'; if ($delete) { delete $DB{$kk}; } else { printf "ImageInfo : %9d:%d:%d:%d\n",split(':',$db->{basic}); printf "Matched : %4d Time(s)\n",$db->{match}; printf "Calc.Score : %9.3f\n",$db->{score}; printf "in DB since: %s\n",scalar(localtime($db->{input})); printf "Last Match : %s\n",scalar(localtime($db->{check})); } } untie %DB; } }