###########################################################################
#
# imageconvert.pm -- utility to help convert image file 
#                    used by ImagePlug, PagedImgPlug ...
#
# Copyright (C) 1999 DigiLib Systems Limited, NZ
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################


package imageconvert;

use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa


use baseconvert;

sub BEGIN {
    @imageconvert::ISA = ('baseconvert');
}



# Discover the characteristics of an image file with the ImageMagick
# "identify" command.

sub identify { 
    my ($image, $outhandle, $verbosity) = @_;

    # Use the ImageMagick "identify" command to get the file specs
    my $command = "identify \"$image\" 2>&1";
    print $outhandle "$command\n" if ($verbosity > 2);
    my $result = '';
    $result = `$command`;
    print $outhandle "$result\n" if ($verbosity > 3);

    # Read the type, width, and height
    my $type =   'unknown';
    my $width =  'unknown';
    my $height = 'unknown';

    my $image_safe = quotemeta $image;
    if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
	$type = $1;
	$width = $2;
	$height = $3;
    }
	
    # Read the size
    my $size = "unknown";
    if ($result =~ m/^.* ([0-9]+)b/) {
	$size = $1;
    } elsif ($result =~ m/^.* ([0-9]+)kb/) {
	$size = 1024 * $1;
    }

    print $outhandle "file: $image:\t $type, $width, $height, $size\n" 
	if ($verbosity > 3);

    # Return the specs
    return ($type, $width, $height, $size);
}




sub new {
    my ($class) = shift @_;

    my ($base_dir,$image_filename,$verbosity,$outhandle) = @_;

    my $self = new baseconvert($base_dir,$image_filename,$verbosity,$outhandle);
    
    return bless $self, $class;
}


sub stream_cmd_XXXX
{
    my $self = shift (@_);
    my ($ivideo_filename,$video_width,$video_height,
	$streaming_bitrate,$streaming_size, 
	$opt_streaming_achan, $opt_streaming_arate) = @_;

    my $streaming_achan 
	= (defined $opt_streaming_achan) ? $opt_streaming_achan : 2;

    my $streaming_arate 
	= (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050;

    my $output_dir = $self->{'cached_dir'};
    my $ivideo_root = $self->{'file_root'};

    my $oflash_file = "${ivideo_root}_stream.flv";
    my $oflash_filename = &util::filename_cat($output_dir,$oflash_file);

    my $s_opt = $self->optional_frame_scale($streaming_size,$video_width,$video_height);

    my $exp_duration = $self->{'exp_duration'};
    my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";

    my $main_opts = "-y $t_opt";

    my $stream_opts = "-b $streaming_bitrate $s_opt";
    ### my $stream_opts = "-r 25 $s_opt";
    $stream_opts .= " -ac $streaming_achan -ar $streaming_arate";
    $stream_opts .= " -pass 1/2";

    # -flags +ilme+ildct' and maybe '-flags +alt' for interlaced material, and try '-top 0/1'

    my $all_opts = "$main_opts $stream_opts"; 
    my $ffmpeg_cmd = "ffmpeg  -i \"$ivideo_filename\" $all_opts -y \"$oflash_filename\"";

    return ($ffmpeg_cmd,$oflash_filename,$oflash_file);
}









1;
