Ticket #406: fuzzyocr.diffs.txt

File fuzzyocr.diffs.txt, 3.6 kB (added by keithr, 1 year ago)

Patch to add support for netpbm images

Line 
1 diff -pU10 -rU10 ./FuzzyOcr.pm /etc/mail/spamassassin/FuzzyOcr.pm
2 --- ./FuzzyOcr.pm       2007-07-07 21:45:20.000000000 -0700
3 +++ /etc/mail/spamassassin/FuzzyOcr.pm  2007-07-11 13:07:55.000000000 -0700
4 @@ -148,21 +148,34 @@ sub fuzzyocr_do {
5              $fname =~ s/[<>]//g;
6              $fname =~ tr/\@\$\%\&/_/s;
7          }
8  
9          my $filename = $fname; $filename =~ tr{a-zA-Z0-9\-.}{_}cs;
10          debuglog("fname: \"$fname\" => \"$filename\"");
11          my $pdata = $p->decode();
12          my $pdatalen = length($pdata);
13          my $w = 0; my $h = 0;
14  
15 -        if ( substr($pdata,0,3) eq "\x47\x49\x46" ) {
16 +        if ($pdata =~ /^P([1-7])\n/o) {
17 +           ## NETPBM File
18 +           if ($pdata =~ /^P[1-7]\n([0-9]+) ([0-9]+)\n/o) {
19 +               $w = $1;
20 +               $h = $2;
21 +           } else {
22 +               errorlog("Cannot find image dimensions");
23 +           }
24 +
25 +           $imgfiles{$filename}{ftype} = 0;
26 +           $imgfiles{$filename}{width}  = $w;
27 +           $imgfiles{$filename}{height} = $h;
28 +           infolog("NETPBM: [${h}x${w}] $filename ($pdatalen)");
29 +       } elsif ( substr($pdata,0,3) eq "\x47\x49\x46" ) {
30              ## GIF File
31              $imgfiles{$filename}{ftype} = 1;
32              ($w,$h) = unpack("vv",substr($pdata,6,4));
33              infolog("GIF: [${h}x${w}] $filename ($pdatalen)");
34              $imgfiles{$filename}{width}  = $w;
35              $imgfiles{$filename}{height} = $h;
36          } elsif ( substr($pdata,0,2) eq "\xff\xd8" ) {
37              ## JPEG File
38              my @Markers = (0xC0,0xC1,0xC2,0xC3,0xC5,0xC6,0xC7,0xC9,0xCA,0xCB,0xCD,0xCE,0xCF);
39              my $pos = 2;
40 @@ -369,21 +382,58 @@ sub fuzzyocr_do {
41  
42          if($$pic{fname} =~ /\.([\w-]+)$/) {
43              $suffix = $1;
44          }
45          if ($suffix) {
46              debuglog("File has Content-Type \"$mimetype\" and File Extension \"$suffix\"");
47          } else {
48              debuglog("File has Content-Type \"$mimetype\" and no File Extension");
49          }
50  
51 -        if ( $$pic{ftype} == 1 ) {
52 +        if ( $$pic{ftype} == 0 ) {
53 +            infolog("Found NETPBM header name=\"$$pic{fname}\"");
54 +
55 +           if ($conf->{focr_skip_ppm}) {
56 +               infolog("Skipping image check");
57 +               next;
58 +           }
59 +
60 +           my $max_size;
61 +
62 +           if (defined($conf->{focr_max_size_ppm}) and ($$pic{fsize} > $conf->{focr_max_size_ppm})) {
63 +               infolog("PPM file size ($$pic{fsize}) exceeds maximum file size for this format, skipping...");
64 +               next;
65 +           }
66 +
67 +           if ( ($$pic{ctype} !~ /(pbm|pgm|ppm|pnm|pam)/i) and not $generic_ctype) {
68 +               wrong_ctype( "PPM", $$pic{ctype} );
69 +               $internal_score += $conf->{'focr_wrongctype_score'};
70 +           }
71 +
72 +            if ( $suffix and $suffix !~ /ppm/i) {
73 +                wrong_extension( "PPM", $suffix);
74 +                $internal_score += $conf->{'focr_wrongext_score'};
75 +            }
76 +
77 +           unless (defined $conf->{'focr_bin_ppmtopgm'}) {
78 +               errorlog("Cannot exec ppmtopgm, skipping image");
79 +               next;
80 +           }
81 +
82 +            printf RAWERR qq(## link($file, $pfile)\n) if ($haserr>0);
83 +           unless (link($file, $pfile)) {
84 +                printf RAWERR "?? link failed: $!\n" if ($haserr>0);
85 +                errorlog("link($file, $pfile) failed, skipping...");
86 +                ++$imgerr if $conf->{focr_keep_bad_images}>0; next;
87 +           }
88 +       }
89 +       elsif ( $$pic{ftype} == 1 ) {
90              infolog("Found GIF header name=\"$$pic{fname}\"");
91              if ($conf->{focr_skip_gif}) {
92                  infolog("Skipping image check");
93                  next;
94              }
95              if (defined($conf->{focr_max_size_gif}) and ($$pic{fsize} > $conf->{focr_max_size_gif})) {
96                  infolog("GIF file size ($$pic{fsize}) exceeds maximum file size for this format, skipping...");
97                  next;
98              }
99