fix problems with html output
[light-and-matter.git] / scripts / pdf_to_bitmap.pl
blob70036e10e49b825801b670ed29a0e1d1da346a64
1 #!/usr/bin/perl
3 use strict;
5 # usage:
6 # pdf_to_bitmap.pl foo.pdf
7 # pdf_to_bitmap.pl foo.pdf jpg
8 # pdf_to_bitmap.pl foo.pdf png
9 # pdf_to_bitmap.pl foo.pdf png 1
10 # default format is png
11 # quality for jpg is set to imagemagick's default of 92
12 # If the file foo.jpg or foo.png exists and is newer than foo.pdf, then nothing is done, and this is not an error.
13 # [currently I have the date checking commented out...why?]
14 # If the file foo.jpg or foo.png exists and foo.pdf doesn't exist, then nothing is done, and this is not an error.
15 # Adding the 1 as the third command-line arg forces rendering even if it seems up to date.
17 my $pdf = $ARGV[0];
18 my $dest_fmt = $ARGV[1];
19 if ($dest_fmt=='') {$dest_fmt='png'}
20 my $force = ($ARGV[2] eq '1');
22 my @temp_files = ();
24 unless ($pdf =~ /\.pdf$/) {finit("Error in pdf_to_bitmap.pl, input file doesn't have extension .pdf")}
26 my $rendered = $pdf;
27 $rendered =~ s/\.pdf$/.$dest_fmt/;
30 if ((!$force) && -e $rendered && (! (-e $pdf))) {exit(0)}
31 ###if (-e $rendered && -M $pdf > -M $rendered) {exit(0)} # -M finds age in days
33 print "rendering $pdf to $rendered\n";
35 # Don't use inkscape --export-png, because as of april 2013, it messes up on transparency.
36 # Can convert pdf directly to bitmap of the desired resolution using imagemagick, but it messes up on some files (e.g., huygens-1.pdf), so
37 # go through pdftoppm first.
38 my $ppm = 'z-1.ppm'; # only 1 page in pdf
39 push @temp_files,$ppm;
40 if (system("pdftoppm -r 300 $pdf z")!=0) {finit("Error in pdf_to_bitmap.pl, pdftoppm")}
42 # Work around misfeature in ImageMagick's convert. See notes in computer/apps. It's possible that ca. 2015
43 # ImageMagick's color handling will change, and I will need to get rid of this code.
44 # Detect whether or not it's color:
45 my $stats = `identify -colorspace HSL -verbose $ppm`;
46 $stats =~ s/\n//g; # strip newlines
47 my $is_color = !($stats=~quotemeta("Green: min: 0 (0) max: 0 (0)"));
48 # when ImageMagick's identify utility is given the -colorspace HSL, saturation is in the green channel
49 my $gamma_correction = 1.0;
50 if (!$is_color) {$gamma_correction=0.44} # grayscale images are much too dark otherwise
52 if (system("convert $ppm -density 300 -evaluate Pow $gamma_correction -units PixelsPerInch $rendered")!=0) {finit("Error in pdf_to_bitmap.pl, ImageMagick's convert")}
53 # ... uses default quality of 92
55 # It seems that some tool on my toolchain has had inconsistencies/regressions in its behavior with respect to
56 # how it handles gamma. I think this was probably inkscape, although imagemagick has also had some flaky changes
57 # in how it handles color management.
58 # Currently I have this:
59 # $ inkscape --version
60 # Inkscape 0.48.4 r9939 (Jan 22 2014)
61 # $ convert --version
62 # Version: ImageMagick 6.7.7-10 2014-03-06 Q16 http://www.imagemagick.org
63 # $ pdftoppm -v
64 # pdftoppm version 0.24.5
65 # With these versions, it seems OK if I set gamma_correction to 1.0. With some other versions, previously,
66 # I needed 0.44.
68 print "\n";
69 finit();
71 sub finit {
72 my $message = shift;
73 tidy();
74 if ($message eq '') {
75 exit(0);
77 else {
78 die $message;
82 sub tidy {
83 foreach my $f(@temp_files) {
84 unlink($f) or die "error deleting $f, $!";