# Album Plugin: images/exactimage
#
# For info:     'album -plugin_info images/exactimage'
# For usage:    'album -plugin_usage images/exactimage'
# For license:  'album -images/exactimage:show_license'
#
# ExactImage source http://www.exactcode.com/site/open_source/exactimage/

use strict;

my $LICENSE = << 'LICENSE';
Copyright (c) Jan 2013   F. Breitling <frank.breitling at gmx.de>

This plugin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This plugin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this plugin.  If not, see <http://www.gnu.org/licenses/>.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
LICENSE


my $DESCRIPTION = << 'DESCRIPTION';
Creates thumbnails via ExactImage or Image::ExactImage if available

The ExactImage library was developed for speed. As such it creates thumbnails
from JPEG images about 4x faster than ImageMagick's convert which is otherwise
used by album.

This plugin was created as an alternative to the images/epeg plugin since the
epeg library was difficult to find and install on Ubuntu 11.10. A solution was
provided by ExactImage lib which was available as distribution package.
Moreover the ExactImage lib supports a variety of other formats in addition to
JPEG. For the complete list of supported formats as well as further
information about ExactImage see

  http://www.exactcode.com/site/open_source/exactimage/ .

The author would like to acknowledge the 'images/epeg' plugin by Scott J.
Bertin which served as a template, the constructive communication with David
Ljung Madison which made him come up with this plugin and last but not least
the ExactImage library.
DESCRIPTION


my $IMAGE_EXACTIMAGE = album::attempt_require('Image::ExactImage');
my $EXACTIMAGE_COMMAND;

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

  # Setup the options
  album::add_option(1, 'show_license', \&show_license, one_time=>1,
                    usage=>'Show the license for this plugin.');
  album::add_option(2, 'econvert', album::OPTION_STR, default=>'econvert',
                    usage=>'Name or full path to the econvert command.');

  # Setup the hooks
  album::hook($opt,'scale', \&scale_image);

  return {
    author => 'Frank Breitling',
    href => 'mailto://frank.breitling at gmx.de',
    version => '1.0',
    description => $DESCRIPTION,
  };
}

sub scale_image {
  my ($opt, $hookname, $img, $scale_arg, $new, $medium) = @_;
    
  # Is ExactImage available
  find_exactimage($opt) unless $IMAGE_EXACTIMAGE || $EXACTIMAGE_COMMAND;
  return undef if !$IMAGE_EXACTIMAGE && $EXACTIMAGE_COMMAND eq 'NOTFOUND';
  
  # Determine the max final size
  return undef unless $scale_arg =~ /^(\d+)x(\d+)/;
  my $maxw = $1;
  my $maxh = $2;
  
  my $img_x;
  my $img_y;
  my $new_x;
  my $new_y;
  if ($IMAGE_EXACTIMAGE) {
    my $epg = new Image::Exactimage($img) || return undef;
    $img_x = $epg->get_width();
    $img_y = $epg->get_height();
    ($new_x, $new_y) = get_new_size($img_x, $img_y, $maxw, $maxh);
    if($new_x >= $img_x || $new_y >= $img_y) {
      ($new_x, $new_y) = ($img_x, $img_y);
      copy($img, $new);
    } else {
      $epg->resize($new_x, $new_y) || return undef;
      $epg->write_file($new) || return undef;
    }
  } else {
    ($img_x, $img_y) = album::get_size($opt, $img);
    ($new_x, $new_y) = get_new_size($img_x, $img_y, $maxw, $maxh);
    if($new_x >= $img_x || $new_y >= $img_y) {
      ($new_x, $new_y) = ($img_x, $img_y);
      copy($img, $new);
    } else {
      my @args = ( $EXACTIMAGE_COMMAND,
                   "-i", $img,
                   "--scale", $new_x/$img_x, 
                   "-o", $new );
      system(@args) == 0 || return undef;
    }
  }
  
  return ($img_x, $img_y, $new_x, $new_y);
}

sub get_new_size {
  my ($img_x, $img_y, $maxw, $maxh) = @_;
  
  my ($new_x, $new_y) = ($maxw, $maxh);
  if($img_x*$maxh < $maxw*$img_y) {
    $new_x = int($img_x * $maxh/$img_y + .5);
  } else {
    $new_y = int($img_y * $maxw/$img_x + .5);
  }
  
  return ($new_x, $new_y);
}

sub find_exactimage {
  my ($opt) = @_;
  
  $EXACTIMAGE_COMMAND = album::option($opt, 'econvert');
  
  if(defined($EXACTIMAGE_COMMAND) && ! -x $EXACTIMAGE_COMMAND) {
    my @path = album::get_path($opt);
    $EXACTIMAGE_COMMAND = album::search_path_exec($opt,$EXACTIMAGE_COMMAND,@path);
  }
  
  if(!$EXACTIMAGE_COMMAND || ! -x $EXACTIMAGE_COMMAND) {
		my $me = album::curr_plugin($opt);
    album::hash_warn($opt,"Unable to locate the econvert command. $me plugin disabled\n".
      "\tUse -${me}:exactimage option to specify where to find it.\n");
    $EXACTIMAGE_COMMAND = 'NOTFOUND';
  }
}

sub show_license {
  my ($opt) = @_;
  
	my $me = album::curr_plugin($opt);
  print "The following license applies ONLY to the ",
        "$me plugin, and should not be construed ",
        "to apply to album, or any other plugin.\n\n", $LICENSE;
  exit;
}

# Plugins always end with:
1;



