#!/usr/bin/perl
#
# Name:
#	im-hax-clip-2-bmp-1.pl.
#
# Purpose:
#	Determine what's in the clipboard.
#	If it's an image, write it to disk.
#
# V 1.00 9-Jun-2002
# -----------------
# o Initial version
#
# Test environment:
#	OS: WinNT 4 SP 6a
#	Web server: Apache V 1.3.26
#	IM: V 5.4.7
#
# Input files:
#	None.
#
# Output files:
#	im-hax-clip-2-bmp-1.bmp.
#
# Note:
#	o This program has nothing to do with the new
#		(as of ImageMagick V 5.4.6)
#		clipboard image format
#	o If you use Alt-PrintScreen, the image is
#		correctly inserted into the clipboard.
#		Hence it can be retrieved reliably.
#		However, I can't get this program to work
#		with graphics programs which offer
#		their own Select and Copy commands,
#		for reasons I cannot imagine.
#		IMDisplay falls into this latter
#		category :-(.
#		So does convert. For instance:
#		shell>convert smile.gif clipboard:
#		puts something in the clipboard, but
#		neither this program, not convert
#		can recover that image, eg with:
#		shell>convert clipboard: smile.png
#	o tab = 4 spaces || die.
#	o Text reformatted to max 50 chars wide
#		so source can be converted to PDF
#
# Author:
#	Ron Savage <ron@savage.net.au>
#	http://savage.net.au/ImageMagick.html
#
# Licence:
#	Copyright (c) 1999-2002 Ron Savage
#
#	All Programs of mine are
#	'OSI Certified Open Source Software';
#	you can redistribute them and/or modify them
#	under the terms of the Artistic License,
#	a copy of which is available at:
#	http://www.opensource.org/licenses/index.html

use Win32::Clipboard;

# -----------------------------------------------

sub report
{
	my($clipboard)					= @_;
	$content{'Is Text Available'}	=
		$clipboard -> IsFormatAvailable(CF_TEXT);
	$content{'Is DIB Available'}	=
		$clipboard -> IsFormatAvailable(CF_DIB);
	$content{'Is HDROP Available'}	=
		$clipboard ->IsFormatAvailable(CF_HDROP);
	$content{'Is Bitmap'}			=
		$clipboard -> IsBitmap();
	$content{'Is Files'}			=
		$clipboard -> IsFiles();
	$content{'Is Text'}				=
		$clipboard -> IsText();
	$content{'Text'}				=
		$clipboard -> IsText()
		? ('<' . $clipboard -> GetText() . '>')
		: 'Clipboard does not contain text';

	print "Clipboard status: \n";
	print map
		{
			"$_ => $content{$_}. \n"
		} sort keys %content;
	print "\n";

}	# End of report.

# -----------------------------------------------

my($dir_name)		= '/temp';
my($im_version)		= $Image::Magick::VERSION;
my($clip_version)	= $Win32::Clipboard::VERSION;

chdir($dir_name)
|| die("Can't chdir($dir_name): $!");

print "Win32::Clipboard V $clip_version. \n";
print "\n";

my($clipboard)	= Win32::Clipboard();
my($debug)		= 0;

report($clipboard) if ($debug);

if ($clipboard -> IsFormatAvailable(CF_DIB))
{
	my($output_file_name)	=
		'im-hax-clip-2-bmp-1.bmp';
	my($image)				=
		Win32::Clipboard::GetBitmap()
	|| die("Can't read image from clipboard");
	open(BITMAP, "> $output_file_name");
	binmode BITMAP;
	print BITMAP $image;
	close BITMAP;
	print "Wrote: $output_file_name. \n";
}
else
{
	print "Clipboard deviod of image. \n";
}
