#!/usr/bin/perl
###########################################
# dreamify - Gimp Image Dreamifier
# Mike Schilli, 2003 (m@perlmeister.com)
###########################################
use warnings;
use strict;

use Gimp qw(:auto);
use Gimp::Fu;
use Pod::Usage;

use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init($DEBUG);

my $menu = "<Toolbox>/Xtns/Mike/Dreamify";
my $jpg_file;

if(grep /-gimp/, @ARGV) {
    # Call from within Gimp
  $menu = "<Image>/Perl-Fu/Dreamify";
} else {
    # Call from the command line
  $jpg_file = $ARGV[0];
  pod2usage("No file") 
      unless defined $jpg_file;
}

register(
  "perl_fu_dreamify",    # Name
  "Dreamify a Picture",  # Blurb
  "=pod(HELP)",          # Help 
  "=pod(AUTHOR)",        # Author
  "=pod(COPYRIGHT)",     # Copy
  "=pod(DATE)",          # Date
  $menu,                 # Menu
  "",                    # Images accepted
  [],                    # Parameters
  [],                    # Return values
  \&dreamify             # Function
);

###########################################
sub dreamify { 
###########################################
  my($img, $layer) = @_;

  Gimp->progress_init("Dreamifying ...");
     
  if(!$img) {
    DEBUG "Loading $jpg_file";
    $img = file_jpeg_load($jpg_file, "");
    die "Can't load $jpg_file" unless $img;
    $layer = image_get_active_layer($img);
  }

  DEBUG "Copying layer";
  my $new_layer = $layer->layer_copy(0);
  $new_layer->layer_set_mode(OVERLAY_MODE);
  $img->image_add_layer($new_layer, -1);
  Gimp->progress_update(.1);

  scale_image_down($img, 800, 600);
  Gimp->progress_update(.5);

  DEBUG "Blurring";
  $img->plug_in_gauss_iir($new_layer, 
                          20.0, 1, 1);
  Gimp->progress_update(.8);

  DEBUG "Adjusting Colors";
  $new_layer->gimp_levels(0, 0, 255, 1.5, 
                          0, 255);

  $img->flatten();
  $layer = $img->get_active_layer;
  Gimp->progress_update(1);

  if($jpg_file) {
      (my $png_file = $jpg_file) =~ 
                           s/\.jpg$/.png/g;
      DEBUG "Saving to $png_file";
      $layer->file_png_save($png_file, "", 
                            0, 6, (1) x 5);
  }
}

###########################################
sub scale_image_down { 
###########################################
  my($img, $x, $y) = @_;

  my $w = $img->image_width();
  my $h = $img->image_height();

    # Switch x,y if portrait
  ($x, $y) = ($y, $x) if $w < $h;
  
  DEBUG "Limits $x x $y";
  DEBUG "Size   $w x $h";

  if($w > $x and $h > $y) {
      my $new_h = int($h*$x/$w);
      DEBUG "Resizing to $x x $new_h";
      $img->image_scale($x, $new_h);
  }
}

exit main;

__END__

=head1 NAME

Dreamify - Gimp Plugin for dreamy pictures

=head1 SYNOPSIS

    dreamify file.jpg

=head1 HELP

Adds a second layer to a picture in
"Overlay" mode, lightens up the colors,
applies a Gaussian blur and rescales the
result to 800x600. Operates on the current
picture if called from within Gimp via
C<Rightclick/Perl-Fu/Dreamify>. If called
from the command line, it modifies the JPG
file specified, flattens the result and 
writes it back as PNG.

=head1 AUTHOR

Copyright 2003 by Mike Schilli, all rights
reserved. This program is free software,
you can redistribute it and/or modify it
under the same terms as Perl itself.
