#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package EmbedImage;
use strict;


# This adds the label's logo to the top right corner of the first page.
#
sub labelLogo
{
    my ($pdf, $page, $clientNameClean, $topMargin, $rightMargin) = @_;
    
    my $photo = $page->gfx;

    my $imagePath = "/app/tools/common/production/images/logos/";  
		
	my $gifImage = $imagePath . $clientNameClean . ".gif";	
	my $pngImage = $imagePath . $clientNameClean . ".png";
	
	# Let's check for a pdf-only version of the logo first
	#	
	if (-e $pngImage)
	{
			# We have trouble with some GIFs getting corrupted when they are embedded.
			# For those images, we have created PNG versions to use here instead.
			#

			my ($imageWidth, $imageHeight) = pngDimensions($pngImage);
					
			my $y = $topMargin - $imageHeight;
			my $x = $rightMargin - $imageWidth;
	 		my $imageFile = $pdf->image_png($pngImage);
	 		$photo->image($imageFile, $x, $y, $imageWidth, $imageHeight);			
		
	}			
	# Otherwise, just use the standard GIF
	#
	elsif (-e $gifImage)
	{
			my ($imageWidth, $imageHeight) = gifDimensions($gifImage);
	
			my $y = $topMargin - $imageHeight;
			my $x = $rightMargin - $imageWidth;
	 		my $imageFile = $pdf->image_gif($gifImage);
	 		$photo->image($imageFile, $x, $y, $imageWidth, $imageHeight);
	}

    return ($pdf, $page);
}


sub gifDimensions
{
    my ($filename) = @_;

    open(GIF, $filename) || return (undef, undef);
    my $buf = '';
    my $n = read GIF, $buf, 10;
    close GIF;

    return (undef, undef) if $n < 10;
    my ($head, $width, $height) = unpack("A6vv", $buf);
    return (undef, undef) unless $head =~ /^GIF8[79]a/;
    return ($width, $height);
}


sub pngDimensions
{
  my ($filename) = @_;
  my $dimensions;
	
  open(IMAGEFILE, $filename) || return(undef, undef);
  binmode IMAGEFILE;

  seek(IMAGEFILE,16,0);
  if (read(IMAGEFILE, $dimensions,8) != 8) {
    close IMAGEFILE;
    return(-1,-1);
  }
  close IMAGEFILE;
  my ($width, $height) = unpack("NN", $dimensions);
  return ($width, $height);
}


sub addIconGIF
{
    my %args = @_;
    
	my $page        = $args{page};
	my $pdf         = $args{pdf};
    my $source      = $args{source};
    my $link        = $args{link};  
    my $width       = $args{width};       
    my $height      = $args{height};      
    my $x           = $args{x};
    my $y           = $args{y};     
    
    # Let's make sure the image exists first.
    #
    if (-e $source)
    {
        my $images = $page->gfx;
        my $icon = $pdf->image_gif($source);
        
        if (!$width || !$height)
        {
            ($width, $height) = gifDimensions($source);
        }
        
    	$images->image($icon, $x, $y, $width, $height);    
    	
        if ($link)
        {
            my $left = $x;
            my $right = $x + $width;
            my $bottom = $y;
            my $top = $y + $height;
            
            my $clientNameClean = Common::Client::Current()->WebAlias();
            $clientNameClean = Common::Client::Current()->ClientNameClean() unless $clientNameClean;
            my $url = "https://" . $clientNameClean . ".royaltyshare.com" . $link;
            # And now we'll create the actual link    
            my $annot = $page->annotation();
            $annot->url($url, -rect => [$left, $bottom, $right, $top]);          
        }
        
    }
	   
}


1;
