#!/usr/bin/perl
#
# Name:
#	im-hax-composite-3.pl.
#
# Purpose:
#	Demonstrate the Composite method.
#	4 images are created, and then
#	composed into 1.
#
# V 1.00 01-May-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-composite-3-one.png
#	im-hax-composite-3-two.png
#	im-hax-composite-3-three.png
#	im-hax-composite-3-four.png
#	im-hax-composite-3.png
#
# Note:
#	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 strict;
use warnings;

use Image::Magick;

# -----------------------------------------------
# Input parameters:
#	$output_name	A file name
#	$bkg_color		A color
#	$param			An array ref:
#					 0: A stroke and fill color
#					 1: A string of coordinates

sub make_image
{
	my($output_name, $bkg_color, $param) = @_;
	my($image)	=
		Image::Magick -> new(size => '56x56');
	my($result)	=
		$image -> Read("xc:$bkg_color");
	die $result if $result;

	for my $p (@$param)
	{
		$result = $image -> Draw
		(
			primitive	=> 'polygon',
			stroke		=> $$p[0],
			fill		=> $$p[0],
			strokewidth	=> 1,
			points		=> $$p[1]
		);
		die $result if $result;
	}

	$result = $image -> Write($output_name);
	die $result if $result;
	print "Wrote: $output_name. \n";

}	# End of make_image.

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

my($work_dir_name)	= '/temp';
my($im_version)		= $Image::Magick::VERSION;

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

print "Image::Magick V $im_version. \n";
print "\n";

# Any old images will do.

make_image('im-hax-composite-3-one.png',
	'maroon', [ ['white',  '1,6 54,54 6, 1'] ]);
make_image('im-hax-composite-3-two.png',
	'white',  [ ['green',  '1,6 54, 9 3, 4'],
	['blue',  ' 6,1  4,3 9,54'] ]);
make_image('im-hax-composite-3-three.png',
	'red',    [ ['green',  '9,1 54, 6 1,54'] ]);
make_image('im-hax-composite-3-four.png',
	'silver', [ ['maroon', '9,1 51, 3 1, 9'],
	['olive', '52,3 54,5 5,52'] ]);

print "\n";

my($output_file_name)	=
	'im-hax-composite-3.png';
my(%image)				=
(
	upper_left =>
	{
		file_name	=>
			'im-hax-composite-3-one.png',
		image		=> '',
		width		=> 0,
		height		=> 0,
		x			=> 0,
		y			=> 0,
	},
	upper_right =>
	{
		file_name	=>
			'im-hax-composite-3-two.png',
		image		=> '',
		width		=> 0,
		height		=> 0,
		x			=> 0,
		y			=> 0,
	},
	lower_left =>
	{
		file_name	=>
			'im-hax-composite-3-three.png',
		image		=> '',
		width		=> 0,
		height		=> 0,
		x			=> 0,
		y			=> 0,
	},
	lower_right =>
	{
		file_name	=>
			'im-hax-composite-3-four.png',
		image		=> '',
		width		=> 0,
		height		=> 0,
		x			=> 0,
		y			=> 0,
	},
);

my($image, $file_name, $result);

for $image (keys %image)
{
	$file_name = $image{$image}{'file_name'};

	print "Reading: $file_name. \n";

	$image{$image}{'image'}	=
		Image::Magick -> new();
	$result					=
	$image{$image}{'image'} -> Read($file_name);

	die $result if $result;

	($image{$image}{'width'},
	$image{$image}{'height'}) =
	$image{$image}{'image'} ->
	Get('width', 'height');

	print "Size: $image{$image}{'width'} x ",
		"$image{$image}{'height'}. \n";
	print "\n";
}

$image{'upper_right'}{'x'}	=
	$image{'upper_left'}{'width'};
$image{'lower_left'}{'y'}	=
	$image{'upper_left'}{'height'};
$image{'lower_right'}{'x'}	=
	$image{'lower_left'}{'width'};
$image{'lower_right'}{'y'}	=
	$image{'upper_left'}{'height'};
my($width, $height)			=
	($image{'upper_left'}{'width'} +
	$image{'upper_right'}{'width'},
	$image{'upper_left'}{'height'} +
	$image{'lower_left'}{'height'});
my($i)						=
	Image::Magick -> new(size => "${width}x$height");
$result						=
	$i -> Read('xc:white');

die $result if $result;

for $image (keys %image)
{
	$file_name = $image{$image}{'file_name'};

	print "Composing: $file_name. \n";

	$result = $i -> Composite
	(
		compose	=> 'Over',
		image	=> $image{$image}{'image'},
		x		=> $image{$image}{'x'},
		y		=> $image{$image}{'y'},
	);

	die $result if $result;
}

print "\n";

$result = $i -> Write($output_file_name);

die $result if $result;

print "Wrote: $output_file_name. \n";
print "Size:  $width x $height. \n";
