| 1 |
#!/usr/bin/perl -w |
| 2 |
# Scale pictures to be included into a LaTeX generated PDF file. |
| 3 |
# written by peter willadt |
| 4 |
# contact: willadt at t-online.de |
| 5 |
# this software is subject to the LaTeX project public license. |
| 6 |
# changes: |
| 7 |
# 2016-07-27 first public release |
| 8 |
# 2016-08-02 changed regex to prevent problem with long filenames |
| 9 |
# 2016-08-02 changed > to gt (shame on me) |
| 10 |
# 2016-10-20 corrected behaviour when program gets called without arguments |
| 11 |
# (shame on me, again) |
| 12 |
# 2016-10-20 added undocumented switch --help to show usage. |
| 13 |
# 2017-01-23 Updated documentation (see pdflatexpicscale.tex), |
| 14 |
# and corrected version number |
| 15 |
# Please note: version numbers will be updated to keep sync |
| 16 |
# with the documentation, even when the perl code does not change. |
| 17 |
# 2017-02-01 pdflatexpicscale did not work correct when log lines were |
| 18 |
# wrapped twice. This is fixed. |
| 19 |
# 2024-10-18 Replaced dependency of external ImageMagick software in favor |
| 20 |
# of GD library. |
| 21 |
# |
| 22 |
use strict; |
| 23 |
|
| 24 |
use GD; |
| 25 |
use Image::ExifTool; |
| 26 |
|
| 27 |
use File::Basename; |
| 28 |
use File::Spec; |
| 29 |
use File::Copy; |
| 30 |
|
| 31 |
use Getopt::Long; |
| 32 |
|
| 33 |
my $version = '0.50'; |
| 34 |
my $versiondate = '2024-10-18'; #version %version |
| 35 |
my $showversion; |
| 36 |
|
| 37 |
# command line inouts |
| 38 |
my $verbose = 0; |
| 39 |
my $help = 0; |
| 40 |
my $draftoption = 0; |
| 41 |
my $finaloption = 0; |
| 42 |
my $TeXproject; |
| 43 |
|
| 44 |
# folders for scaled graphics, relative names |
| 45 |
my $outputfolderprefix = undef; |
| 46 |
my $srcfolder = ''; |
| 47 |
|
| 48 |
my %customsettings = ( |
| 49 |
'name' => 'from command line', |
| 50 |
'outputdpi' => 300, |
| 51 |
'outputfolder' => 'printimg', |
| 52 |
'jpegquality' => 80, |
| 53 |
'outputbwdpi' => 1200 |
| 54 |
); |
| 55 |
|
| 56 |
my %finalsettings = ( |
| 57 |
'name' => 'final', |
| 58 |
'outputdpi' => 300, |
| 59 |
'jpegquality' => 80, |
| 60 |
'outputfolder' => 'finalimg', |
| 61 |
'outputbwdpi' => 1200 |
| 62 |
); |
| 63 |
|
| 64 |
my %draftsettings = ( |
| 65 |
'name' => 'draft', |
| 66 |
'outputdpi' => 96, |
| 67 |
'jpegquality' => 50, |
| 68 |
'outputfolder' => 'webimg', |
| 69 |
'outputbwdpi' => 300 |
| 70 |
); |
| 71 |
|
| 72 |
my $outputdpi = undef; # final resolution. |
| 73 |
my $outputbwdpi = undef; # final resolution for pure b/w art |
| 74 |
my $tolerance = 20; # avoid resampling of almost fitting images |
| 75 |
my $jpegquality = undef; # 50 is mostly acceptable, 80 is fine. |
| 76 |
|
| 77 |
# supported file formats (by extension) |
| 78 |
my %canhandle = |
| 79 |
( |
| 80 |
'JPEG' => '1', |
| 81 |
'JPG' => '1', |
| 82 |
'PNG' => '1', |
| 83 |
); |
| 84 |
|
| 85 |
# bookkeeping |
| 86 |
my %processedimages; |
| 87 |
my $copied = 0; |
| 88 |
my $scaled = 0; |
| 89 |
my $skipped = 0; |
| 90 |
|
| 91 |
# the following is required by Image::ExifTool |
| 92 |
our $AUTOLOAD; |
| 93 |
sub AUTOLOAD |
| 94 |
{ |
| 95 |
Image::ExifTool::DoAutoLoad($AUTOLOAD, @_); |
| 96 |
} |
| 97 |
|
| 98 |
sub printError |
| 99 |
{ |
| 100 |
my $filename = shift; |
| 101 |
my $kind = shift; |
| 102 |
if ($kind eq 'i') { |
| 103 |
print "I will not do inplace modification of graphics:\n" . |
| 104 |
"($filename) will not be processed. (Missing path?)\n"; |
| 105 |
} elsif ($kind eq 'm') { |
| 106 |
print "I can't find ($filename). (What have you done?)\n"; |
| 107 |
} elsif ($kind eq 'f') { |
| 108 |
print "File $filename has an unexpected or wrong format.\n"; |
| 109 |
} elsif ($kind eq 's') { |
| 110 |
print "Trying to copy '$filename' leads to system error # $_.\n"; |
| 111 |
} elsif ($kind eq 'd') { |
| 112 |
print "Directory '$filename' does not exist\n" |
| 113 |
. "in actual folder and I can't create it (sys error # $_).\n" |
| 114 |
. "Please check permissions. Aborting.\n"; |
| 115 |
} else { |
| 116 |
print "I got trouble with '$filename'.\n"; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
sub getDestFilename |
| 121 |
{ |
| 122 |
my @dstdirs; |
| 123 |
my $filename = shift; |
| 124 |
my $dstfilename = undef;; |
| 125 |
$dstdirs[0] = '.'; |
| 126 |
$dstdirs[1] = $outputfolderprefix; |
| 127 |
$dstfilename = File::Spec->catfile(@dstdirs, basename($filename)); |
| 128 |
return $dstfilename; |
| 129 |
} |
| 130 |
|
| 131 |
sub getSourceFilename |
| 132 |
{ |
| 133 |
my @srcdirs; |
| 134 |
my $filename = shift; |
| 135 |
my $srcfilename = undef; |
| 136 |
if (defined($srcfolder) and $srcfolder gt '') { |
| 137 |
$srcdirs[0] = '$srcfolder'; |
| 138 |
$srcfilename = File::Spec->catfile(@srcdirs, basename($filename)); |
| 139 |
$filename = $srcfilename; |
| 140 |
} |
| 141 |
unless (-e $filename){ |
| 142 |
printError($filename, 'm'); |
| 143 |
return undef; |
| 144 |
} |
| 145 |
return $filename; |
| 146 |
} |
| 147 |
|
| 148 |
sub fileYounger |
| 149 |
{ |
| 150 |
my @filename = @_; |
| 151 |
my ($mtime1, $mtime2, $size1, $size2, $idstring2, $existwidth); |
| 152 |
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, |
| 153 |
$atime, $ctime, $blksize, $blocks); |
| 154 |
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, |
| 155 |
$atime, $mtime1, $ctime, $blksize, $blocks) |
| 156 |
= stat($filename[0]); |
| 157 |
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, |
| 158 |
$atime, $mtime2, $ctime, $blksize, $blocks) |
| 159 |
= stat($filename[1]); |
| 160 |
if($mtime1 >= $mtime2) { |
| 161 |
return 1; |
| 162 |
} |
| 163 |
return 0; |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
sub numberInRange |
| 168 |
{ |
| 169 |
my $realwidth = shift; |
| 170 |
my $wantedwidth = shift; |
| 171 |
if (($realwidth >= $wantedwidth * 100 / (100 + $tolerance)) and |
| 172 |
($realwidth <= $wantedwidth * (100 + $tolerance) / 100)) { |
| 173 |
return 1; |
| 174 |
} else { |
| 175 |
return 0; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
sub loadImage |
| 180 |
{ |
| 181 |
my %result = ( |
| 182 |
'filename' => undef, |
| 183 |
'image' => undef, 'canhandle' => 0, |
| 184 |
'format' => undef, 'width' => 0, |
| 185 |
'height' => 0, 'monochrome' => 0, |
| 186 |
'quality' => 50 |
| 187 |
); |
| 188 |
$result{'filename'} = shift; |
| 189 |
my $maketruecolor = shift || 0; |
| 190 |
my $exifTool = Image::ExifTool->new; |
| 191 |
my $imageinfo = $exifTool->ImageInfo($result{'filename'}); |
| 192 |
# foreach (sort keys %$imageinfo) { |
| 193 |
# print "$_ => $$imageinfo{$_}\n"; |
| 194 |
# } |
| 195 |
$result{'image'} = GD::Image->new($result{'filename'}); |
| 196 |
$result{'width'} = $$imageinfo{'ImageWidth'}; |
| 197 |
$result{'height'} = $$imageinfo{'ImageHeight'}; |
| 198 |
if ( ($$imageinfo{'FileType'} eq 'PNG') or |
| 199 |
($$imageinfo{'FileType'} eq 'JPEG')) { |
| 200 |
$result{'format'} = $$imageinfo{'FileType'}; |
| 201 |
$result{'canhandle'} = 1; |
| 202 |
} |
| 203 |
if (defined($$imageinfo{'BitDepth'}) and $$imageinfo{'BitDepth'} == 1) { |
| 204 |
$result{'monochrome'} = 1; |
| 205 |
} elsif (defined($result{'image'})) { |
| 206 |
$result{'canhandle'} = 1; |
| 207 |
if (defined ($result{'image'}->colorsTotal) and |
| 208 |
$result{'image'}->colorsTotal < 3) { |
| 209 |
$result{'monochrome'} = 1; |
| 210 |
} |
| 211 |
unless (defined($result{'width'})) { |
| 212 |
$result{'width'} = $result{'image'}->width; |
| 213 |
} |
| 214 |
unless (defined($result{'height'})) { |
| 215 |
$result{'height'} = $result{'image'}->height; |
| 216 |
} |
| 217 |
} |
| 218 |
return %result; |
| 219 |
} |
| 220 |
|
| 221 |
sub writeImage |
| 222 |
{ |
| 223 |
my %dst = @_; |
| 224 |
my $imagedata = undef; |
| 225 |
my $realfilename = $dst{'filename'}; |
| 226 |
open(OUT, '>', $realfilename) or die; |
| 227 |
binmode OUT; |
| 228 |
if ($dst{'format'} eq 'PNG') { |
| 229 |
$dst{'image'}->interlaced(undef); # else PFDTEX has to do it. |
| 230 |
$imagedata = $dst{'image'}->png(9); |
| 231 |
} elsif ($dst{'format'} eq 'JPEG') { |
| 232 |
$imagedata = $dst{'image'}->jpeg($jpegquality); |
| 233 |
} |
| 234 |
print OUT $imagedata; |
| 235 |
close(OUT); |
| 236 |
return 1; |
| 237 |
} |
| 238 |
|
| 239 |
sub GDscale |
| 240 |
{ |
| 241 |
my $srcfilename = shift; |
| 242 |
my $dstfilename = shift; |
| 243 |
my $dstwidth = shift; |
| 244 |
my $dstheight = shift; |
| 245 |
# my $interpolationmethod = GD_BICUBIC; |
| 246 |
# Unfortunately not exported by GD and item of an enum. |
| 247 |
# So I guess: |
| 248 |
my $interpolationmethod = 4; |
| 249 |
my %src = loadImage($srcfilename, 0); |
| 250 |
$src{'destwidth'} = $dstwidth; |
| 251 |
$src{'destheight'} = $dstheight; |
| 252 |
$src{'image'}->interpolationMethod($interpolationmethod); |
| 253 |
# what I will not copy: image, width, height, filename |
| 254 |
my @properties = qw ( |
| 255 |
canhandle format monochrome |
| 256 |
); |
| 257 |
my %dstimage = (); |
| 258 |
foreach (@properties) { |
| 259 |
$dstimage{$_} = $src{$_}; |
| 260 |
} |
| 261 |
$dstimage{'width'} = $src{'destwidth'}; |
| 262 |
$dstimage{'height'} = $src{'destheight'}; |
| 263 |
$dstimage{'filename'} = $dstfilename; |
| 264 |
$dstimage{'image'} = $src{'image'}->copyScaleInterpolated( |
| 265 |
$dstimage{'width'}, $dstimage{'height'}); |
| 266 |
writeImage(%dstimage); |
| 267 |
return 1; |
| 268 |
} |
| 269 |
|
| 270 |
sub handleImage |
| 271 |
{ |
| 272 |
my $filename = shift; |
| 273 |
my $dstwidth = shift; |
| 274 |
my $dstheight = shift; |
| 275 |
my ($srcwidth, $srcheight, $srcdepth, $monochrome, $existwidth); |
| 276 |
my ($dstfilename, $xfilename); |
| 277 |
my @convertargs; |
| 278 |
my %srcimage = (); |
| 279 |
my %dstimage = (); |
| 280 |
print "$filename, " if $verbose; |
| 281 |
$dstfilename = getDestFilename($filename); |
| 282 |
$xfilename = getSourceFilename($filename); |
| 283 |
if (defined($xfilename)) { |
| 284 |
$filename = $xfilename; |
| 285 |
} else { |
| 286 |
printError($filename, 'm'); |
| 287 |
$skipped++; |
| 288 |
return 0; |
| 289 |
} |
| 290 |
if($filename eq $dstfilename) { |
| 291 |
printError($filename, 'i'); |
| 292 |
$skipped++; |
| 293 |
return 0; |
| 294 |
} |
| 295 |
# check for image width and height |
| 296 |
%srcimage = loadImage($filename); |
| 297 |
if ($srcimage{'canhandle'} == 0 or ! defined($srcimage{'image'})) { |
| 298 |
# copy from source to dest. |
| 299 |
print "I Can't deal with $filename, so I will just\n" |
| 300 |
. "copy it to $dstfilename\n" if $verbose; |
| 301 |
copy($filename, $dstfilename); |
| 302 |
$copied++; |
| 303 |
return 1; |
| 304 |
} |
| 305 |
$srcwidth = $srcimage{'width'}; |
| 306 |
$srcheight = $srcimage{'height'}; |
| 307 |
$monochrome = $srcimage{'monochrome'}; |
| 308 |
if($monochrome == 1) { # hires for bw pix |
| 309 |
$dstwidth = int($dstwidth * $outputbwdpi / 72.27 + .5); |
| 310 |
$dstheight = int($dstheight * $outputbwdpi / 72.27 + .5); |
| 311 |
}else { # moderate res for any other |
| 312 |
$dstwidth = int($dstwidth * $outputdpi / 72.27 + .5); |
| 313 |
$dstheight = int($dstheight * $outputdpi / 72.27 + .5); |
| 314 |
} |
| 315 |
if(-e $dstfilename and fileYounger($dstfilename, $filename)) { |
| 316 |
# if requested size already processed: skip |
| 317 |
if((defined $processedimages{$filename}) && |
| 318 |
($processedimages{$filename} >= $dstwidth)) { |
| 319 |
# if known, already processed and large enough: skip |
| 320 |
$skipped++; |
| 321 |
return 1; |
| 322 |
} |
| 323 |
%dstimage = loadImage($dstfilename); |
| 324 |
$existwidth = $dstimage{'width'}; |
| 325 |
# if requested size already there: make known and skip |
| 326 |
if(numberInRange($existwidth, $dstwidth)) { |
| 327 |
$processedimages{$filename} = $existwidth; |
| 328 |
$skipped++; |
| 329 |
return 1; |
| 330 |
} |
| 331 |
} |
| 332 |
if (numberInRange($dstwidth, $srcwidth) or $dstwidth > $srcwidth) { |
| 333 |
# we do no upscaling, but will probably copy the file. |
| 334 |
$processedimages{$filename} = $srcwidth; |
| 335 |
print "will copy $filename to $dstfilename\n" if $verbose; |
| 336 |
copy($filename, $dstfilename); |
| 337 |
$copied++; |
| 338 |
return 1; |
| 339 |
}else { |
| 340 |
GDscale($xfilename, $dstfilename, $dstwidth, $dstheight); |
| 341 |
$scaled++; |
| 342 |
} |
| 343 |
return 1; |
| 344 |
} |
| 345 |
|
| 346 |
sub createOutputFolder |
| 347 |
{ |
| 348 |
my @dstdirs; |
| 349 |
$dstdirs[0] = '.'; |
| 350 |
my $dstdirname = File::Spec->catfile(@dstdirs, $outputfolderprefix); |
| 351 |
if (mkdir($dstdirname) == 0) { |
| 352 |
printError($dstdirname, 'd'); |
| 353 |
return 0; |
| 354 |
} |
| 355 |
if ($verbose) { |
| 356 |
print "Created directory $outputfolderprefix\n"; |
| 357 |
} |
| 358 |
return 1; |
| 359 |
} |
| 360 |
|
| 361 |
sub readlog |
| 362 |
{ |
| 363 |
my $logfilename = shift; |
| 364 |
$logfilename .= '.log'; |
| 365 |
my ($picname, $picext, $picwidth, $picheight); |
| 366 |
my ($picextu, $state, $buffer); |
| 367 |
open LOGFILE, "<$logfilename" or die "Cannot read $logfilename.\n" |
| 368 |
. 'Please run pdfLaTeX before pdflatexpicscale.'; |
| 369 |
$state = 0; |
| 370 |
unless (-d $outputfolderprefix) { |
| 371 |
if (createOutputFolder() == 0) { |
| 372 |
close LOGFILE; |
| 373 |
return; |
| 374 |
} |
| 375 |
} |
| 376 |
print "Processing " if $verbose; |
| 377 |
while (<LOGFILE>){ |
| 378 |
if (/^Package\spdftex\.def\sInfo\:\s/){ |
| 379 |
$buffer = $_; |
| 380 |
unless ($buffer =~ /\sused/){ |
| 381 |
chomp $buffer; |
| 382 |
$buffer .= <LOGFILE>; |
| 383 |
} # twice ought to be enough |
| 384 |
unless ($buffer =~ /\sused/){ |
| 385 |
chomp $buffer; |
| 386 |
$buffer .= <LOGFILE>; |
| 387 |
} |
| 388 |
if($buffer =~ /Info:\s(\S*)\.(\w+)\s+used/){ |
| 389 |
$picname = $1; |
| 390 |
$picext = $2; |
| 391 |
($picextu = $picext) =~ tr/a-z/A-Z/; |
| 392 |
if(defined $canhandle{$picextu}){ |
| 393 |
$state = 1; |
| 394 |
next; # width/height information will follow |
| 395 |
} |
| 396 |
} else { |
| 397 |
print "Log file apparently damaged at line $.\n"; |
| 398 |
} |
| 399 |
} |
| 400 |
next unless ($state == 1); |
| 401 |
next unless /Requested size: (\d+\.\d+)pt x (\d+\.\d+)pt/; |
| 402 |
$picwidth = $1; |
| 403 |
$picheight = $2; |
| 404 |
handleImage("$picname.$picext", $picwidth, $picheight); |
| 405 |
$state = 0; |
| 406 |
} |
| 407 |
close LOGFILE; |
| 408 |
} |
| 409 |
|
| 410 |
sub initializeGlobals |
| 411 |
{ |
| 412 |
my $settinghash = undef; |
| 413 |
if($draftoption == 1) { |
| 414 |
$settinghash = \%draftsettings; |
| 415 |
} elsif ($finaloption == 1) { |
| 416 |
$settinghash = \%finalsettings; |
| 417 |
} else { |
| 418 |
$settinghash = \%customsettings; |
| 419 |
} |
| 420 |
if ($verbose != 0) { |
| 421 |
print 'Using settings: ' . $$settinghash{'name'} . "\n"; |
| 422 |
} |
| 423 |
unless (defined ($outputdpi)) { |
| 424 |
$outputdpi = $$settinghash{'outputdpi'} || 300; |
| 425 |
} |
| 426 |
unless (defined ($outputbwdpi)) { |
| 427 |
$outputbwdpi = 4 * $outputdpi; |
| 428 |
} |
| 429 |
unless (defined ($outputfolderprefix)) { |
| 430 |
$outputfolderprefix = $$settinghash{'outputfolder'} || 'printimg'; |
| 431 |
} |
| 432 |
unless (defined ($jpegquality)) { |
| 433 |
$jpegquality = $$settinghash{'jpegquality'} || 80; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
sub usage |
| 438 |
{ |
| 439 |
print <<'_endusagehead'; |
| 440 |
pdflatexpicscale will downscale large raster images for your TeX project |
| 441 |
to reasonable size. |
| 442 |
Basic usage: |
| 443 |
pdflatexpicscale myarticle |
| 444 |
where your main LaTeX file is called 'myarticle.tex' and a log file exists. |
| 445 |
General options: |
| 446 |
--help (show this message) |
| 447 |
--usage (show this message) |
| 448 |
--verbose (let the script tell, what it attempts to do) |
| 449 |
--version (show software version) |
| 450 |
If you are comfortable with defaults (see manual), you may use one of the |
| 451 |
following options: |
| 452 |
_endusagehead |
| 453 |
my %optiondumper = ( |
| 454 |
'name' => 'Option name', |
| 455 |
'outputfolder' => 'Output folder', |
| 456 |
'outputdpi' => 'Output density', |
| 457 |
'outputbwdpi' => 'same for B/W', |
| 458 |
'jpegquality' => 'JPEG quality' |
| 459 |
); |
| 460 |
print <<'_endusagefoot'; |
| 461 |
Option name --final --draft |
| 462 |
Output folder finalimg webimg |
| 463 |
JPEG quality 80% 50% |
| 464 |
output density 300 dpi 96 dpi |
| 465 |
same for B/W 1200 dpi 300 dpi |
| 466 |
tolerance 20% 20% |
| 467 |
If not, you may do some fine tuning: |
| 468 |
--destdir=folder (relative path, must exist, |
| 469 |
defaults to 'printimg' subfolder of current folder) |
| 470 |
--srcdir=folder (folder to find original images, if this software |
| 471 |
can't figure it out on it's own, because you already |
| 472 |
named the target directory inside your LaTex document.) |
| 473 |
--printdpi=XXX (default 300) |
| 474 |
--tolerance=XX (default 20 percent over printdpi to ignore scaling) |
| 475 |
_endusagefoot |
| 476 |
} |
| 477 |
|
| 478 |
GetOptions('verbose' => \$verbose, |
| 479 |
'printdpi=i' => \$outputdpi, |
| 480 |
'destdir=s' => \$outputfolderprefix, |
| 481 |
'tolerance=i' => \$tolerance, |
| 482 |
'srcdir=s' => \$srcfolder, |
| 483 |
'draft' => \$draftoption, |
| 484 |
'final' => \$finaloption, |
| 485 |
'version' => \$showversion, |
| 486 |
'help' => \$help, |
| 487 |
'usage' => \$help, |
| 488 |
); |
| 489 |
|
| 490 |
|
| 491 |
if($showversion){ |
| 492 |
print "pdflatexpicscale Version $version $versiondate\n"; |
| 493 |
} |
| 494 |
|
| 495 |
if($help){ |
| 496 |
usage(); |
| 497 |
exit(0); |
| 498 |
} |
| 499 |
|
| 500 |
my $settinghash = \%customsettings; |
| 501 |
|
| 502 |
initializeGlobals(); |
| 503 |
|
| 504 |
$TeXproject = shift; |
| 505 |
|
| 506 |
if((defined $TeXproject) && ($TeXproject gt ' ')){ |
| 507 |
readlog($TeXproject); |
| 508 |
if($verbose) { |
| 509 |
print "\npdflatexpicscale Version $version:\n" |
| 510 |
. "$copied file(s) copied, $scaled file(s) converted " . |
| 511 |
"and $skipped occurence(s) skipped for:\n${TeXproject}.log.\n"; |
| 512 |
} |
| 513 |
}else { |
| 514 |
usage(); |
| 515 |
exit(1); |
| 516 |
} |