]> git.sur5r.net Git - bacula/docs/blob - docs/images/image_convert.pl
Tweak update image files
[bacula/docs] / docs / images / image_convert.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Long;
5
6 # Converts the image given in the first argument into 
7 #  the format of the second argument. Decides how to do it
8 #  based on the filenames of the source and destination
9 #
10
11 my $usage =
12 "Usage: image_convert.pl [--scale scale_filename] [--res resolution] source_file destination_file\n ";
13
14 # A global hash to hold command-line arguments.
15 my $cliArgs = {};
16 my $DEFAULT_SCALE = 0.75;
17 my $DEFAULT_MAXX = 5;
18 my $DEFAULT_MAXY = 8;
19 my $DEFAULT_RESOLUTION = 72;
20
21 sub max { return $_[0] > $_[1] ? $_[0] : $_[1]; }
22 sub min { return $_[0] < $_[1] ? $_[0] : $_[1]; }
23
24 sub getIMGdetails {
25         # Takes the image filename as the only argument, returns the image
26         #  size in inches.
27         # If the identify program is available:
28         #  Determine the current geometry and resolution of the image.
29         #  Return the image size in inches.
30         # Otherwize return 0,0.
31         my ($img) = shift;
32
33         # # If the identify program is not available, bail.
34         (`which identify 2>\&1` =~ /^which:/) and 
35                 die "identify program (part of Imagemagick) not available -- aborting\n";
36
37         my $attrib = {};
38
39         # Determine the size of the existing image.
40         my $response = `identify -verbose $img`;
41         if ($response =~ /^identify: no decode delegate/ or
42                 !$response) {
43                 return 0;
44         }
45         ($attrib->{format}) = 
46                 $response =~ /^\s*Format:\s+(\S*)\s+/m;
47         ($attrib->{hdots},$attrib->{vdots}) = 
48                 $response =~ /^\s*Geometry:\s*([\d\.]*)x([\d\.]*)/m;
49         ($attrib->{hres},$attrib->{vres}) = 
50                 $response =~ /^\s*Resolution:\s*([\d\.]*)x([\d\.]*)/m;
51         $attrib->{hsize} = $attrib->{hdots} / $attrib->{hres};
52         $attrib->{vsize} = $attrib->{vdots} / $attrib->{vres};
53         return $attrib;
54 }
55
56 sub doConversion {
57         my ($cli,$scales) = @_;
58         my ($attrib,$ourScale,$newsizex,$newsizey);
59
60         # Get the input file details.
61         $attrib = getIMGdetails($cli->{infile});
62         if (ref($attrib) ne 'HASH') {
63                 # Identify failed.
64                 die "Failed to Identify Input Image File $cli->{infile}.  Aborting\n";
65         }
66         
67         # If the scale factor is defined in the file of scale factors,
68         #  use that.  Otherwise use the default scale provided on the command line, 
69         #  If none was provided, use 0.75.
70         if (defined($scales->{$cli->{infile}})) {
71                 $ourScale = $scales->{$cli->{infile}};
72         } else {
73                 if (defined $cli->{defaultscale}) {
74                         $ourScale = $cli->{defaultscale};
75                 } else { $ourScale = $DEFAULT_SCALE; }
76
77                 # If after scaling the image is still too big, scale it to the max
78                 #  size. Otherwise, scale it according to the scale factor.
79                 if ($attrib->{hsize} * $ourScale > $cli->{maxx} or
80                                 $attrib->{vsize} * $ourScale > $cli->{maxy}) {
81                         $ourScale /= max($attrib->{hsize} * $ourScale / $cli->{maxx},
82                                 $attrib->{vsize} * $ourScale / $cli->{maxy});
83                 } 
84         }
85
86         $newsizex = $attrib->{hsize} * $ourScale;
87         $newsizey = $attrib->{vsize} * $ourScale;
88
89         # Get strings ready for the convert command.
90         my $cdots = sprintf "%dx%d",$newsizex * $cli->{resolution},
91                 $newsizey * $cli->{resolution};
92         my $cres = sprintf "%dx%d",$cli->{resolution},$cli->{resolution};
93         
94         # Do the conversion...
95         printf "Scaling by %.5f : %s -> %s\n",$ourScale,$cli->{infile},$cli->{outfile};
96         print STDERR `convert $cli->{infile} -resize $cdots -density $cres $cli->{outfile}`;
97
98 #       if ($cli->{infile} =~ /.*\.png$/ and $cli->{outfile} =~ /.*\.pdf$/) {
99 #               printf "Scaling %s to %s with scale %.3f\n",$cli->{infile},$cli->{outfile},$ourScale;
100 #               print STDERR `pngtopnm $cli->{infile} | pnmtops -scale=$ourScale -noturn -nosetpage | epstopdf --filter > $cli->{outfile}`;
101 #               pngtopnm $${i}.png | pnmtops -scale=0.85 --noturn -nosetpage | epstopdf --filter >$${i}.pdf ;   
102 #       } elsif ($cli->{infile} =~ /.*\.png$/ and $cli->{outfile} =~ /.*\.eps$/) {
103 #               printf "Scaling %s to %s with scale %.3f\n",$cli->{infile},$cli->{outfile},$ourScale;
104 #               print STDERR `pngtopnm $cli->{infile} | pnmtops -scale=$ourScale -noturn -nosetpage > $cli->{outfile}`;
105 #               pngtopnm $${i}.png | pnmtops -scale=0.65 --noturn -nosetpage >$${i}.eps;
106 #       } elsif ($cli->{infile} =~ /.*\.jpg$/ and $cli->{outfile} =~ /.*\.png$/) {
107 #               printf "Converting %s to %s\n",$cli->{infile},$cli->{outfile};
108 #               print STDERR `jpegtopnm $cli->{infile} | pnmtopng > $cli->{outfile}`;
109 #               jpegtopnm $${i}.jpg | pnmtopng >$${i}.png ;
110 #       } elsif ($cli->{infile} =~ /.*\.gif$/ and $cli->{outfile} =~ /.*\.png$/) {
111 #               printf "Converting %s to %s\n",$cli->{infile},$cli->{outfile};
112 #               print STDERR `giftopnm $cli->{infile} | pnmtopng > $cli->{outfile}`;
113 #               giftopnm $${i}.gif | pnmtopng >$${i}.png ;
114 #       } else {
115 #               die "Unsupported Conversion: $cli->{infile} to $cli->{outfile}\n";
116 #       }
117
118         # Save the image scale for putting into the file of scales.
119         $scales->{$cli->{infile}} = $ourScale;
120 }
121
122 sub loadImgScales {
123         # Loads the image scales from the specified file into the
124         #  global hash %imgScales
125         my $scaleFile = shift;
126         my ($imagename,$scale,$cnt);
127         my %imgScales;
128
129         if (!open (IF,"<$scaleFile")) {
130                 warn "File of Image Scales not Found.  Will create one when needed.\n";
131         } else {
132                 $cnt = 0;
133                 while (<IF>) {
134                         $cnt++;
135                         chomp;
136                         my ($imagename,$scale) = split /\s+/;
137                         if (!defined $imagename or !defined $scale or
138                                 $scale !~ /^[\-\+\d\.]+$/ or $scale < 0.01 or $scale > 100) {
139                                 print STDERR "Invalid Image Scale Entry at line $cnt in $scaleFile\n";
140                         } else {
141                                 $imgScales{$imagename} = $scale;
142                         }
143                 }
144                 close IF;
145         }
146         return { %imgScales };
147 }
148
149 sub writeImgScales {
150         my ($fileName,$scales) = @_;
151
152         open OF,">$fileName" or die "Cannot open scale file $fileName for output\n";
153         foreach (sort(keys(%{$scales}))) { printf OF "%s %.5f\n",$_,$scales->{$_}; }
154         close OF;
155 }
156
157
158 sub getCliArgs {
159         # Sets up the parameters and gets the command-line
160         #  options. $args is a pointer to a hash.
161         my $args = shift;
162         my ($infile,$outfile,@filenames);
163
164         # Establish defaults:
165         $args->{defaultscale} = $DEFAULT_SCALE;
166         $args->{maxx} = $DEFAULT_MAXX;
167         $args->{maxy} = $DEFAULT_MAXY;
168         $args->{resolution} = $DEFAULT_RESOLUTION;
169         GetOptions( "scalefile=s" => \$args->{scalefile},
170                                 "defaultscale=f" => \$args->{defaultscale},
171                                 "resolution=i" => \$args->{resolution},
172                                 "maxx=f" => \$args->{maxx},
173                                 "maxy=f" => \$args->{maxy}, 
174                         ) or die $usage;
175         # There should be just the input and output filenames and
176         #  nothing more in @filenames
177         $args->{infile} = shift @ARGV;
178         $args->{outfile} = shift @ARGV;
179         if (!defined $args->{outfile}) { die $usage; }
180 }
181
182 ###############################################
183 ############## MAIN ###########################
184 ###############################################
185
186 my ($scales);
187
188 # Get the command-line arguments into the global hash.
189 getCliArgs($cliArgs);
190
191 #foreach (sort(keys(%$cliArgs))) {
192 #       print "$_ $cliArgs->{$_}\n";
193 #}
194
195 if (! -e $cliArgs->{infile}) {die "Input File not Found: $cliArgs->{infile}\n";}
196
197 # Load in the image conversion scales from the file of scale factors.
198 if (defined $cliArgs->{scalefile}) {
199         if (-e $cliArgs->{scalefile}) {
200                 $scales = loadImgScales($cliArgs->{scalefile});
201         } else {
202                 warn "File of Image Scales not Found.  Will create one when needed.\n";
203         }
204 }
205
206
207 # Process the file.
208 doConversion($cliArgs,$scales);
209
210 # Write the file of scale factors.
211 writeImgScales($cliArgs->{scalefile},$scales);