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
12 "Usage: image_convert.pl [--scale scale_filename] [--res resolution] source_file destination_file\n ";
14 # A global hash to hold command-line arguments.
16 my $DEFAULT_SCALE = 0.75;
19 my $DEFAULT_RESOLUTION = 72;
21 sub max { return $_[0] > $_[1] ? $_[0] : $_[1]; }
22 sub min { return $_[0] < $_[1] ? $_[0] : $_[1]; }
25 # Takes the image filename as the only argument, returns the image
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.
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";
39 # Determine the size of the existing image.
40 my $response = `identify -verbose $img`;
41 if ($response =~ /^identify: no decode delegate/ or
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};
57 my ($cli,$scales) = @_;
58 my ($attrib,$ourScale,$newsizex,$newsizey);
60 # Get the input file details.
61 $attrib = getIMGdetails($cli->{infile});
62 if (ref($attrib) ne 'HASH') {
64 die "Failed to Identify Input Image File $cli->{infile}. Aborting\n";
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}};
73 if (defined $cli->{defaultscale}) {
74 $ourScale = $cli->{defaultscale};
75 } else { $ourScale = $DEFAULT_SCALE; }
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});
86 $newsizex = $attrib->{hsize} * $ourScale;
87 $newsizey = $attrib->{vsize} * $ourScale;
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};
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}`;
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 ;
115 # die "Unsupported Conversion: $cli->{infile} to $cli->{outfile}\n";
118 # Save the image scale for putting into the file of scales.
119 $scales->{$cli->{infile}} = $ourScale;
123 # Loads the image scales from the specified file into the
124 # global hash %imgScales
125 my $scaleFile = shift;
126 my ($imagename,$scale,$cnt);
129 if (!open (IF,"<$scaleFile")) {
130 warn "File of Image Scales not Found. Will create one when needed.\n";
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";
141 $imgScales{$imagename} = $scale;
146 return { %imgScales };
150 my ($fileName,$scales) = @_;
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->{$_}; }
159 # Sets up the parameters and gets the command-line
160 # options. $args is a pointer to a hash.
162 my ($infile,$outfile,@filenames);
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},
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; }
182 ###############################################
183 ############## MAIN ###########################
184 ###############################################
188 # Get the command-line arguments into the global hash.
189 getCliArgs($cliArgs);
191 #foreach (sort(keys(%$cliArgs))) {
192 # print "$_ $cliArgs->{$_}\n";
195 if (! -e $cliArgs->{infile}) {die "Input File not Found: $cliArgs->{infile}\n";}
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});
202 warn "File of Image Scales not Found. Will create one when needed.\n";
208 doConversion($cliArgs,$scales);
210 # Write the file of scale factors.
211 writeImgScales($cliArgs->{scalefile},$scales);