#! /usr/bin/perl

=head1 NAME

clip2png.pl, clip2gif.pl - Convert bitmap image from clipboard to png or gif

=cut

# Full POD documentation at the end

# Get image from clipboard,
# convert to .png or .gif and save to a temp file
# set clipboard to temp file name
# optionally open image in graphics editor

my $VERSION = 0.35;

# -------------------     Edit these variables    --------------------

# Full path to ImageMagick's convert.exe
# (unless it's in your path, in which case the executable name is enough)
my $IMconvert = "d:\\Graphics\\ImageMagick\\convert.exe";

# Your favorite image editor
my $editor = "mspaint.exe"; # "C:\\util\\IrfanView\\i_view32.exe"

# Template for filename of resulting image
my $template = "clip-image-XXXX";

# --------------------------------------------------------------------

use Win32::Clipboard;
use File::Temp qw(tempfile);
use File::Spec;
use Getopt::Std;

my %options;
getopts('hef:', \%options);

if ($options{h}) {
	print <<END_USAGE;
Usage: $0 [options]

Options:
	-h
		show this help

	-e
		edit resulting image in $editor

	-f filename
		name resulting file "filename". Default is creating a file like $template

For more info: perldoc $0

END_USAGE
	exit 0;
}

# the script name determines the wanted output format
my ($ext) = $0 =~ /\bclip2(\w+)/i;
$ext = lc($ext);
unless ($ext eq "png" or $ext eq "gif") {
	die "Unsupported output format '$ext'\n",
		"Rename the script to clip2png or clip2gif\n";
}

my ($fh, $filename);

if ($options{f}) {
	$filename = $options{f};
	if ($filename =~ /\.(png|gif)$/i) {
		if ($ext ne lc($1)) {
			$ext = lc($1);
			warn "filename extension '$ext' overriding script name for output format\n";
		}
	}
	else {
		$filename .= ".$ext";
	}
	open($fh, ">$filename") or die "Error creating output file $filename: $!\n";
} else {
	($fh, $filename) = tempfile($template, SUFFIX=>".$ext") or die "Error creating output file $filename: $!\n";
}
close $fh;

system($IMconvert, "clipboard:", $filename) == 0
	or die "Error calling '$IMconvert clipboard: $filename'\n";

my $size = -s $filename;

print STDERR "created $filename ($size bytes). Clipboard reset to contain the file name.\n";

Win32::Clipboard::Set(File::Spec->rel2abs($filename));

if ($options{e}) {
	# exec() doesn't work with mspaint.exe and spaces in file names. Win32 is really annoying sometimes!
	# this silly hack sort of gets around it:
	exit system($editor, $filename);
}

__END__

=head1 SYNOPSIS

  clip2png
  clip2png -f outputfile.png -e
  clip2gif
  etc.

=head1 DESCRIPTION

If you have ImageMagick installed (and Perl of course), you can use this script
to quickly convert bitmaps in the clipboard to png or gif files on disk.
I generally use this to send small attached files from screen captures
(made with PrtSC or Alt-PrtSc).

=head1 INSTALLATION

=over

=item 1

Edit these three variables at the top of the script:

=over

=item $IMconvert

Full path to your ImageMagick's convert.exe.

=item $editor

Path to your favorite image editor. This will be called if you used the -e
option to edit the image after the conversion.
The default is mspaint.exe, a lame editor but which is present on every Win32 system.

=item $template

Template for file name of resulting image
Default: "clip-image-XXXX"

=back

=item 2

Save the script under both clip2png.pl and clip2gif.pl

=item 3

Do C<pl2bat clip2png.pl> and C<pl2bat clip2gif.pl> to create Windows batch files.

=back

=over

Note: You can use the following for the 2 steps above. Adapt the final directory to one in your path.

C<copy clip2gif.pl clip2png.pl & pl2bat clip2png.pl & pl2bat clip2gif.pl & copy clip2png.bat c:\bin & copy clip2gif.bat c:\bin>

=back

=head1 OPTIONS

=over

=item -h

show brief usage help

=item -e

after the conversion, open the resulting image in the configured editor

=item -f filename

name resulting file "filename" instead of using the configured template

=back

=head1 AUTHOR

perl -e "print q(Milivoj Ivkovic <mi).qq(\x40alma.ch>\n)"

=head1 SEE ALSO

L<perl(1)>, convert, L<http://www.imagemagick.org>

=cut
