#!/usr/bin/perl
#
# Name:
#	im-hax-composite-4.pl.
#
# Purpose:
#	Demonstrate transparency with composition.
#	Read the comments starting at lines 129 and
#	164 to confuse youself :-(.
#	The main problem is which program you use to
#	view the images. The ImageMagick source code
#	ships with the source for rpng2-win. Either
#	use it, or IrfanView (say), but you can
#	never trust Microsoft's Internet Explorer
#	to correctly display PNGs which have a
#	transparent channel. Unfortunately, my
#	original testing was done using IE.
#	IrfanView is at: http://www.irfanview.com/
#
# V 1.00 9-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-4-background.(gif|png)
#	im-hax-composite-4-foreground.(gif|png)
#	im-hax-composite-4-composite.(gif|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;

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

sub do_it
{
	my($prefix, $suffix) = @_;

	# Fabricate a red image with a cyan border.

	my($background_image)	=
		Image::Magick -> new(size => '100x100');
	my($result)				=
		$background_image -> Read('xc:red');
	die $result if $result;

	$result = $background_image -> Border
	(
		width	=> 2,
		height	=> 2,
		fill	=> 'cyan',
	);
	die $result if $result;

	$result = $background_image ->
		Write("$prefix-background.$suffix");
	die $result if $result;

	print
		"Wrote: $prefix-background.$suffix. \n";

	# Makee a yellow image with a cyan border.

	my($foreground_image)	=
		Image::Magick -> new(size => '50x50');
	$result					=
		$foreground_image -> Read('xc:yellow');
	die $result if $result;

	$result = $foreground_image -> Border
	(
		width	=> 2,
		height	=> 2,
		fill	=> 'cyan',
	);
	die $result if $result;

	# Draw a maroon square and a blue triangle
	# on the yellow square.

	$result = $foreground_image -> Draw
	(
		primitive	=> 'polygon',
		stroke		=> 'maroon',
		fill		=> 'none',
		strokewidth	=> 3,
		points		=> "10,10 40,10 40,40 10,40",
	);
	die $result if $result;

	$result = $foreground_image -> Draw
	(
		primitive	=> 'path',
		stroke		=> 'blue',
		fill		=> 'none',
		strokewidth	=> 3,
		points		=> "M10,10 L40,25 L10,40 z",
	);
	die $result if $result;

	#	The next line makes the blue triangle
	# transparent for GIFs but not for PNGs.
	# When viewing the foreground image, the
	# blue triangle is white.
	#	This is correct (for GIFs) since there
	# is no other background color.
	# When viewing the composite image, the blue
	# triangle is red.
	#	This is correct (for GIFs) since there
	# is a red background.
	# Note: Comment out lines 142 .. 143 or
	# comment out lines 178 .. 179.

	#$result = $foreground_image ->
	#	Transparent(color => 'blue');
	die $result if $result;

	$result = $foreground_image ->
		Write("$prefix-foreground.$suffix");
	die $result if $result;

	print
		"Wrote: $prefix-foreground.$suffix. \n";

	# Compose the yellow image on the red image.

	$result = $background_image -> Composite
	(
		compose	=> 'over',
		image	=> $foreground_image,
		x		=> 25,
		y		=> 25,
	);
	die $result if $result;

	#	If you comment out lines 142 .. 143 and
	# activate the next line, then the next line
	# works for GIFs but not for PNGs.
	# When viewing the foreground image, the
	# blue triangle is blue.
	#	This is correct (for GIFs) since there
	# is no transparent color.
	# When viewing the composite image, the blue
	# triangle is white.
	#	This is correct (for GIFs) since there is
	# no other background color.
	# Note: Comment out lines 142 .. 143 or
	# comment out lines 178 .. 179.

	$result = $background_image ->
		Transparent(color => 'blue');
	die $result if $result;

	$result = $background_image ->
		Write("$prefix-composite.$suffix");
	die $result if $result;

	print "Wrote: $prefix-composite.$suffix. \n";

}	# End of do_it.

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

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";

my($prefix)	= 'im-hax-composite-4';

for my $suffix ('gif', 'png')
{
	do_it($prefix, $suffix);
}
