Image-Caa

 view release on metacpan or  search on metacpan

lib/Image/Caa.pm  view on Meta::CPAN

		# Generate *hue between 0 and 0x5fff

		if ($r == $max){
			$hue = 0x1000 + 0x1000 * ($g - $b) / $delta;
		}elsif ($g == $max){
			$hue = 0x3000 + 0x1000 * ($b - $r) / $delta;
		}else{
			$hue = 0x5000 + 0x1000 * ($r - $g) / $delta;
		}
	}else{
		$sat = 0;
		$hue = 0;
	}

	return ($hue, $sat, $val);
}


sub HSV_DISTANCE{
	my ($self, $h, $s, $v, $index) = @_;

	my $v1 = $v - $self->{hsv_palette}->[$index * 4 + 3];
	my $s1 = $s - $self->{hsv_palette}->[$index * 4 + 2];
	my $h1 = $h - $self->{hsv_palette}->[$index * 4 + 1];

	my $s2 = $self->{hsv_palette}->[$index * 4 + 3] ? CAA_HSV_YRATIO * $s1 * $s1 : 0;
	my $h2 = $self->{hsv_palette}->[$index * 4 + 2] ? CAA_HSV_HRATIO * $h1 * $h1 : 0;

	return $self->{hsv_palette}->[$index * 4] * ((CAA_HSV_XRATIO * $v1 * $v1) + $s2 + $h2);
}

sub load_submodule {
	my ($self, $module, $args) = @_;

	eval "require Image::Caa::$module";
	warn $@ if $@;

	my $obj = undef;
	eval "\$obj = new Image::Caa::$module(\$args)";
	warn $@ if $@;

	if (!$@ && defined $obj){

		return $obj;
	}

	die "Image::Caa - Couldn't load 'Image::Caa::$module'";
}

1;

__END__

=head1 NAME

Image::Caa - Colored ASCII Art

=head1 SYNOPSIS

  use Image::Caa;
  use Image::Magick;


  # load an image

  my $image = Image::Magick->new;
  $image->Read('sunset.jpg');


  # display it as ASCII Art

  my $caa = new Image::Caa();
  $caa->draw_bitmap(0, 0, 40, 20, $image);


  # some fancy options

  my $caa = new Image::Caa(
    driver => 'DriverANSI',
    dither => 'DitherOrdered8',
    black_bg => 1,
  );
  $caa->draw_bitmap(0, 0, 40, 20, $image);

=head1 DESCRIPTION

This module outputs C<Image::Magick> image objects as ASCII Art, using a variety of output
dithering modes and output drivers (currently supported is a plain old ANSI termical
output driver and a curses driver).

=head1 METHODS

=over

=item C<new( opt =E<gt> 'value', ... )>

Returns a new C<Image::Caa> object. The options are as follows:

=over

=item * C<driver>

Output driver. Valid values are:

=over

=item * C<DriverANSI> (default)

=item * C<DriverCurses>

=back

=item * C<dither>

Dithering mode. Valid values are:

=over

=item * C<DitherNone> (default)

=item * C<DitherOrdered2>

=item * C<DitherOrdered4>

=item * C<DitherOrdered8>

=item * C<DitherRandom>

=back

=item * C<black_bg>

Set to 1 to enable black background mode.
By default, we use colored backgrounds to allow 256 colors (16 foreground x 16 background)

=item * C<window>

Used only by the Curses output driver. Indicates the Curses window to write output into.

=back

=item C<draw_bitmap($x1, $y1, $x2, $y2, $image)>

Draws the image C<$image> within the box bounded by C<($x1,$y1)-($x2,$y2)>.
Note that the default (ANSI) output driver ignores the origin position as uses
only the absolute box size.

=back

=head1 EXTENDING

Both the dithering and driver backends are plugable and fairly easy to create - just create 
modules in the Image::Caa::* namespace. Dither modules need to implement the C<new()>, 
C<init($line)>, C<get()> and C<increment()> methods. Driver modules need to implement the 
C<new()>, C<init()>, C<set_color($fg, $bg)>, C<putchar($x, $y, $char)> and C<fini()> methods.
Look at the existing modules for guidance.

=head1 AUTHORS

Copyright (C) 2006, Cal Henderson <cal@iamcal.com>

This library is based on libcaca's bitmap.c

libcaca is Copyright (C) 2004 Sam Hocevar <sam@zoy.org>

libcaca is licensed under the GNU Lesser General Publice License

=head1 SEE ALSO

L<Image::Magick>, L<http://sam.zoy.org/libcaca/>

=cut



( run in 1.738 second using v1.01-cache-2.11-cpan-39bf76dae61 )