-
Notifications
You must be signed in to change notification settings - Fork 3
/
jpgcrush
executable file
·60 lines (56 loc) · 1.83 KB
/
jpgcrush
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/perl -s
# jpgcrush by Loren Merritt
# Last updated: 2008-11-29
# This code is public domain.
@ARGV or die <<EOT;
usage: jpgcrush [opts] foo.jpg [...]
-f fast (constant scan order rather than search)
-g convert to grayscale (implies -f)
-r restart markers
EOT
$n=0;
$opts = "-optimize";
$g and $opts .= " -grayscale";
$r and $opts .= " -restart 1";
$f ||= $g;
$iss = $css = .001;
foreach $if (@ARGV) {
# `file` isn't always correct
my $type = `file -b "$if"`;
if($if !~ /\.jpe?g$/i or $type =~ /PNG|MNG|PPM|PGM|bitmap|ASCII/) {
warn "\n$if isn't a jpeg\n";
next;
} elsif($type !~ /JPEG/) {
warn "\n$if might not be a jpeg: $type\n";
}
($cf = $if) =~ s/^.*\///;
$cf = "_$cf.c.jpg";
printf "\r[%d/%d %.2f%%] %s ", $n, $#ARGV+1, 100*($css/$iss-1), $if;
unlink $cf;
if($f) {
$err = `jpegtran $opts -scans ~/src/perl/jpeg_scan_rgb.txt "$if" 2>&1 >"$cf"`;
# I don't know a fast way to distinguish color from grayscale input
# (short of invoking a whole extra copy of jpegtran or identify),
# so just let the error message tell me.
# jpegtran will also error out if I use the grayscale scan on a color
# image, so there's no risk of accidental conversion.
if($err =~ /Invalid scan script at entry 2/) {
$err = `jpegtran $opts -scans ~/src/perl/jpeg_scan_bw.txt "$if" 2>&1 >"$cf"`;
}
$err and warn "\njpegtran failed:\n$err\n" and next;
} else {
system("jpegrescan", "-q", ("-r")x$r, $if, $cf) and next;
}
$cs = -s $cf;
$is = -s $if;
if($cs and ($cs < $is or $r)) {
system "touch", "-r", $if, $cf;
rename $cf, $if;
} else {
unlink $cf;
}
$css += $cs;
$iss += $is;
$n++;
}
printf "\r[%d/%d %.2f%%]\n", $n, $#ARGV+1, 100*($css/$iss-1);