############################################################ # ConvertTiffsToPS # # Convert a list of tiff files to PS files # # RETURNS: # A list of the paths of the postscript files ############################################################ use threads; use threads::shared; sub ConvertTiffsToPS { my (@tiffpaths) = @_; my @pspaths; # to be filled in. my @threads; foreach my $tiffpath (@tiffpaths){ (my $pspath = $tiffpath) =~ s/\.tiff?$/\.PS/i; push @pspaths, $pspath; print "Converting $tiffpath to $pspath\n"; my $thread = async { Tiff2PS($tiffpath, $pspath) }; push @threads,$thread; } print("Waiting for child processes..."); print join(",\n",map {$_->join()} @threads); print("child processes complete..."); confess "Somethings wrong".Dumper(\@tiffpaths,\@pspaths) unless (scalar(@tiffpaths) == scalar(@pspaths)); # return the list of PS files return @pspaths; } # Take a tifffile, and produce a PS file next to it. sub Tiff2PS { my ($tiffpath,$pspath) = @_; #local $| = 1; # has no visible effect either way... print qq("$TIFF2PS_COMMAND" "$tiffpath" |\n); open TIFF2PS, qq("$TIFF2PS_COMMAND" "$tiffpath"|) or confess "Can't run $TIFF2PS_COMMAND"; open PSOUT, ">$pspath" or confess "Can't open $pspath for writing!\n"; my $flag = 0; foreach my $line () { print PSOUT $line; # Add the following line only once... if (!$flag && $line =~ /^%%BoundingBox: (\d+) (\d+) (\d+) (\d+)/o) { my ($w,$h) = ($3-$1, $4-$2); # Fix the pagesize, since GS wants everything to be 8.5x11" portrait print PSOUT "<< /PageSize [$w $h] >> setpagedevice\n"; # Short-circuit prevents expensive regexp match afterwards $flag = 1; } } close TIFF2PS; close PSOUT; if (! -e $pspath) { warn("**TIFF2PS Problem: $!"); confess "Tiff2PS Error: $!"; } else { print "$pspath created...\n"; } return $pspath; }