# Album Plugin: images/tile-annotate
# For info: 'album -plugin_info images/tile-annotate'
# For usage:  'album -plugin_usage images/tile-annotate'
use strict;		# Not required, but recommended

my $DESCRIPTION = << 'DESCRIPTION';
Annotates *medium* images with a tiled string of text such as a copyright tag.
Copyright idea credit:  Joe Botha
Implemented by:         Zoltan Csala

DESCRIPTION

sub start_plugin {
	my ($opt) = @_;

	my $ret = {
		author      => 'Zoltan Csala',
		href        => 'http://MarginalHacks.com/',
		version     => '1.0',
		description => $DESCRIPTION,
	};

	# Setup the hooks
 	album::hook($opt, 'do_album_top', \&sanity);
 	album::hook($opt, 'medium', \&medium);
	album::add_option(1,'text1', $album::OPTION_STR, usage=>"First text to annotate on image");
	album::add_option(1,'text2', $album::OPTION_STR, usage=>"Second text to annotate on image");
	album::add_option(1,'first_time',$album::OPTION_BOOL, default=>0, one_time=>1, usage=>"Force annotation of medium images even if they're already generated");
	album::add_option(1,'tilesize',$album::OPTION_STR, default=>'233x144', usage=>"Tile size (in points)");
	album::add_option(1,'fontsize',$album::OPTION_NUM, default=>'14', usage=>"Text size (in points)");
	album::add_option(1,'dissolve',$album::OPTION_NUM, default=>'12', usage=>"Dissolve factor (in percent)");
	
	return $ret;
}

my $PLUGNAME;
sub sanity {
	my ($opt, $hook) = @_;
	$PLUGNAME = album::curr_plugin($opt);
	album::fatal($opt,"Plugin $PLUGNAME requires album -medium option\n")
		unless $opt->{medium};
	album::perror($opt,"Plugin $PLUGNAME expects album -just_medium option\n\tOnly medium images will be annotated\n")
		unless $opt->{just_medium};
	album::fatal($opt,"Plugin $PLUGNAME requires -text1 option\n")
		unless album::option($opt,'text1');
	my $tileSize = album::option($opt, 'tilesize');
	album::fatal($opt,"Plugin $PLUGNAME requires -tilesize option to be in format 999x999 (supplied: $tileSize)\n")
		unless $tileSize =~ m/\d{2,4}x\d{2,4}/;
	0;
}

sub medium {
	my ($opt, $hook,  $dir, $obj, $file, $full_path) = @_;
	my $tmpMask  = "$opt->{topdir}/annotate.png";
	
	# Deal with changed options
	unlink($obj->{medium}{path})
		if album::option_changed($opt, 'text1') || album::option_changed($opt, 'fontsize')
		|| album::option_changed($opt, 'tilesize') || album::option_changed($opt, 'dissolve');

	# If the image already exists, don't bother,
	return undef if -f $obj->{medium}{path}
		&& !album::option($opt, 'first_time')
		&& !album::option_changed($opt, 'medium');
	## BUG!  What if medium is regenerated (because of full image change?)

	# Actually make the medium
	album::medium($opt, $dir, $obj);
	
	my $tmpResult = "$opt->{topdir}/result.jpg";
	my $tilesize = album::option($opt,'tilesize');
	my $fontsize = album::option($opt,'fontsize');
	my $dissolve = album::option($opt,'dissolve');
	my $text1    = album::option($opt,'text1');
	my $text2    = album::option($opt,'text2');
	$text2 = $text1 unless defined $text2;
	
	if (! -f $tmpMask) {
		album::option($opt,'tilesize') =~ m/\d{2,4}x(\d{2,4})/;
		my $annHeight = int($1 / 2);
		system('convert', '-size', $tilesize, 'xc:none', '-fill', 'grey50', '-colorize', '40',
		'-pointsize', $fontsize, '-gravity', 'NorthWest', '-annotate', '+0+0', $text1,
		'-gravity', 'SouthEast', '-annotate', "+0+$annHeight", $text2, $tmpMask );
		album::perror($opt,"[$PLUGNAME] Trouble with creating tile mask\n")
			if $?;
	}
	system( 'composite', '-dissolve', $dissolve, '-tile', $tmpMask, $obj->{medium}{path}, $tmpResult );
	album::perror($opt,"[$PLUGNAME] Trouble with composite [$obj->{medium}{path}]\n")
		if $?;

	if ( rindex(lc($obj->{medium}{path}), '.jpg') > -1) {
		# It is JPG, simply rename
		rename $tmpResult, $obj->{medium}{path};
	} else {
		# Type different from JPG, convert
		system( 'convert', $tmpResult, $obj->{medium}{path} );
	}
	album::perror($opt,"[$PLUGNAME] Trouble with convert/rename [$obj->{medium}{path}]\n")
		if $?;

	# Need to erase the cached size of the image
	undef $obj->{medium}{x};
	undef $obj->{medium}{y};

	# Still return undef, we haven't actually generated the thumbnail.
	return $obj->{medium}{path};
}


# Plugins always end with:
1;
