Image::ANSIMation - Load, create, manipulate and save ANSI animation (ANSIMation) files


Image-ANSI documentation Contained in the Image-ANSI distribution.

Index


Code Index:

NAME

Top

Image::ANSIMation - Load, create, manipulate and save ANSI animation (ANSIMation) files

SYNOPSIS

Top

	use Image::ANSI::ANSIMation;

	# Read in a file...
	my $anim = Image::ANSIMation->new( file => 'file.ans' );

	# Image width and height
	my $w = $anim->width;
	my $h = $anim->height;

	# export it as a gif animation
	my $gif = $anim->as_gif;

DESCRIPTION

Top

This module allows you to load, create and manipulate files made up of ANSI escape codes, much like Image::ANSI, except that it can be composed of many frames creating an animation.

METHODS

Top

new( %options )

Creates a new ANSIMation. Currently only reads in data.

	# filename
	$anim = Image::ANSIMation->new( file => 'file.ans' );

	# file handle
	$anim = Image::ANSIMation->new( handle => $handle );

	# string
	$anim = Image::ANSIMation->new( string => $string );

read( %options )

Reads in ANSI data.

write( %options )

Writes the ANSI data to a file, filehandle or string.

as_string( %options )

Returns the ANSI output as a scalar.

add_frame( $frame )

Adds another frame to the animation

frames( )

Returns an array ref of frames.

current_frame( )

Returns an integer of the current position in the array of frames.

next_frame( )

Return the next frame in the sequence and add 1 to the current_frame.

width( )

Returns the width of the animation.

height( )

Returns the height of the animation.

as_gif( )

Return the animation as an animated gif.

AUTHOR

Top

* Brian Cassidy <bricas@cpan.org>

COPYRIGHT AND LICENSE

Top


Image-ANSI documentation Contained in the Image-ANSI distribution.
package Image::ANSIMation;

use base qw( Class::Accessor );

use strict;
use warnings;

use GD;
use Image::ANSI::Utils;

our $VERSION = '0.10';

__PACKAGE__->mk_accessors( qw( frames current_frame ) );

sub new {
	my $class = shift;

	my $self  = {};
	bless $self, $class;

	$self->frames( [ ] );
	$self->current_frame( 0 );

	my %options = @_;
	if(
		exists $options{ file } or
		exists $options{ string } or
		exists $options{ handle }
	) {
		return $self->read( @_ );
	}

	return $self;
}

sub read {
	my $self = shift;

	require Image::ANSIMation::Parser;

	$self = Image::ANSIMation::Parser->new( @_ );

	return $self;
}

sub write {
	my $self    = shift;
	my %options = @_;
	my $file    = $self->create_io_object( \%options, '<' );
	
	$file->print( $self->as_string( @_ ) );
}

sub as_string {
	my $self    = shift;
	my %options = @_;
	
	return join ( "\x1b[1;1H", map { $_->as_string } @{ $self->frames } );
}

sub add_frame {
	my $self   = shift;
	my $frame  = shift;
	my $frames = $self->frames;

	push @$frames, $frame;
	$self->current_frame( scalar( @$frames ) - 1 );
}

sub next_frame {
	my $self   = shift;
	my $frames = $self->frames;
	my $next   = $self->current_frame + 1;

	if( $next > @$frames - 1 ) {
		$next = 0;
	}

	$self->current_frame( $next );
	return $self->frames->[ $next ];
}

sub width {
	my $self = shift;

	return $self->frames->[ 0 ]->width;
}

sub height {
	my $self = shift;
	my $max  = 0;

	for my $frame ( @{ $self->frames } ) {
		my $height = $frame->height;
		$max = $height if $height > $max;
	}

	return $max;
}

sub as_gif {
	my $self   = shift;
	my $frames = $self->frames;

	my $first     = GD::Image->new( $frames->[ 0 ]->as_png_full );
	my $animation = GD::Image->new( $self->width * 8, $self->height * 16  );

	for( 0..255 ) {
		$animation->colorAllocate( $first->rgb( $_ ) );
	}
	
	my $gif = $animation->gifanimbegin( 1 );

	for( @$frames ) {
		my $frame = GD::Image->new( $animation->getBounds );

		for( 0..255 ) {
			$frame->colorAllocate( $animation->rgb( $_ ) );
		}

		my $image = GD::Image->newFromPngData( $_->as_png_full );

		$frame->copy( $image, 0, 0, 0, 0, $animation->getBounds );
		$gif     .= $frame->gifanimadd( 0, 0, 0, 15, 1 );
	}

	$gif .= $animation->gifanimend;

	return $gif;
}

1;