# Album Plugin: images/epeg
# For info:     'album -plugin_info images/epeg'
# For usage:    'album -plugin_usage images/epeg'
# For license:  'album -images/epeg:show_license'
#
# epeg can be found at http://enlightenment.freedesktop.org/
use strict;

my $LICENSE = << 'LICENSE';
Copyright (c) 2005 Scott J. Bertin

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

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';
Uses enlightenment 'epeg' (or Image::Epeg if available) to scale jpegs.

Only works if scaling geometry is in max size format (i.e., 640x640).

This is dramatically faster than using convert, probably around 3x.

Requires epeg version 0.9.0.004 or higher.
DESCRIPTION

my $IMAGE_EPEG = album::attempt_require('Image::Epeg');
my $EPEG_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, 'epeg', album::OPTION_STR, default=>'epeg',
                    usage=>'Name or full path to the epeg command.');

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

  return {
    author => 'Scott J. Bertin',
    href => 'mailto://scottbertin@yahoo.com',
    version => '1.0',
    description => $DESCRIPTION,
  };
}

sub scale_image {
  my ($opt, $hookname, $img, $scale_arg, $new, $medium) = @_;
  
  # epeg can only handle JPEG
  return undef unless $img =~ /\.(jpg|jpeg)$/i;
  return undef unless $new =~ /\.(jpg|jpeg)$/i;
  
  # Is epeg available
  find_epeg($opt) unless $IMAGE_EPEG || $EPEG_COMMAND;
  return undef if !$IMAGE_EPEG && $EPEG_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_EPEG) {
    my $epg = new Image::Epeg($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 = ( $EPEG_COMMAND,
                   "-c", "", # Avoid obnoxious epeg comment
                   "-w", $new_x,
                   "-h", $new_y,
                   $img,
                   $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_epeg {
  my ($opt) = @_;
  
  $EPEG_COMMAND = album::option($opt, 'epeg');
  
  if(defined($EPEG_COMMAND) && ! -x $EPEG_COMMAND) {
    my @path = album::get_path($opt);
    $EPEG_COMMAND = album::search_path_exec($opt,$EPEG_COMMAND,@path);
  }
  
  if(!$EPEG_COMMAND || ! -x $EPEG_COMMAND) {
		my $me = album::curr_plugin($opt);
    album::hash_warn($opt,"Unable to locate the epeg command. $me plugin disabled\n".
      "\tUse -${me}:epeg option to specify where to find it.\n");
    $EPEG_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;
