#!/usr/bin/perl

use strict;
use warnings;

use Image::TextMode::Loader;
use Image::TextMode::Renderer::GD;
use Getopt::Long ();
use Pod::Usage   ();
use Carp 'croak';

my %options;
my $result = Getopt::Long::GetOptions(
    \%options,           'output|o=s',
    'thumbnail|t',       'inputformat|if=s',
    'outputformat|of=s', 'font=s',
    'pal=s',             'readopt=s%',
    'renderopt=s%',      'help|h|?'
);
my @files = @ARGV;

if ( !@files || $options{ help } ) {
    Pod::Usage::pod2usage;
}

my $read_options   = $options{ readopt };
my $render_options = $options{ renderopt };
my $method         = $options{ thumbnail } ? 'thumbnail' : 'fullscale';
my $force_filename = ( @files > 1 || !$options{ output } ) ? 1 : 0;
my $renderer       = Image::TextMode::Renderer::GD->new;

my ( $font, $pal ) = @options{ qw( font pal ) };
if ( $font ) {
    my $class = "Image::TextMode::Font::${font}";
    my $return = eval "use $class;";

    if( !$return && $@ ) {
        croak "Unable to load font '$font'";
    }

    $font = $class->new;
}
if ( $pal ) {
    my $class = "Image::TextMode::Palette::${pal}";
    my $return = eval "use $class;";

    if( !$return && $@ ) {
        croak "Unable to load palette '$pal'";
    }

    $pal = $class->new;
}

for my $file ( @files ) {
    croak "file not found: '$file'"    if !-e $file;
    croak "file not readable: '$file'" if !-r $file;

    my $image;
    if ( my $format = $options{ inputformat } ) {
        my $package = "Image\::TextMode\::Format\::${format}";
        my $return = eval "use $package;";

        if( !$return && $@ ) {
            croak "Unable to load format '$format'";
        }

        $image = $package->new;
        $image->read( $file, $read_options );
    }
    else {
        $image = Image::TextMode::Loader->load( [ $file, $read_options ] );
    }

    my $output = $options{ output };
    if ( $force_filename ) {
        $output
            = $file
            . ( $options{ thumbnail } ? q{.t} : '' ) . q{.}
            . ( $options{ outputformat } || 'png' );
    }

    $output =~ s{[^.]+?$}{gif}s
        if $output ne q{-}
            && $image->isa( 'Image::TextMode::Format::ANSIMation' );

    $image->font( $font )   if $font;
    $image->palette( $pal ) if $pal;

    my $data = $renderer->$method( $image, $render_options );

    if ( $output eq q{-} ) {
        binmode STDOUT;
        print $data;
    }
    else {
        open( my $fh, '>', $output ) or croak "Unable to open file '$output': $!";
        binmode( $fh );
        print $fh $data;
        close( $fh ) or croak "Unable to close file '$output': $!";
    }
}

__END__

=head1 NAME 

textmode2png - Convert text mode files to png

=head1 SYNOPSIS

    % textmode2png [OPTIONS] file.ext [file2.ext, ...]
    
    Available options:
    -o, --output            output filename (- for STDOUT); ignored when
                            more than one file is specified)
    -if, --inputformat      force a textmode format
    -of, --outputformat     specify the output format (default: png)
    -t, --thumbnail         generate a thumbnail
    --font                  specify a particular font (e.g. 8x8)
    --pal                   specify a particular palette (e.g. ANSI)
    --readopt key=val       set read options
    --renderopt key=val     set rendering options
    -h, -?, --help          this message

=head1 DESCRIPTION

This is a simple command-line tool to help you convert text mode
(ansi, bin, etc) files to png images. Use the -t switch to create a
thumbnail.

=head1 AUTHOR

Brian Cassidy E<lt>bricas@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2008-2009 by Brian Cassidy

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. 

=cut

