Image-Tileset

 view release on metacpan or  search on metacpan

META.yml  view on Meta::CPAN

--- #YAML:1.0
name:                Image-Tileset
version:             0.01
abstract:            A tileset loader.
license:             ~
author:              
    - Noah Petherbridge <root@kirsle.net>
generated_by:        ExtUtils::MakeMaker version 6.42
distribution_type:   module
requires:     
    Image::Magick:                 1
    XML::Simple:                   1
meta-spec:
    url:     http://module-build.sourceforge.net/META-spec-v1.3.html
    version: 1.3

Makefile.PL  view on Meta::CPAN

use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
    NAME              => 'Image::Tileset',
    VERSION_FROM      => 'lib/Image/Tileset.pm', # finds $VERSION
    PREREQ_PM         => {
	'Image::Magick' => 1,
	'XML::Simple'   => 1,
	}, # e.g., Module::Name => 1.1
    ($] >= 5.005 ?     ## Add these new keywords supported since 5.005
      (ABSTRACT_FROM  => 'lib/Image/Tileset.pm', # retrieve abstract from module
       AUTHOR         => 'Noah Petherbridge <root@kirsle.net>') : ()),
);

README  view on Meta::CPAN


   perl Makefile.PL
   make
   make test
   make install

DEPENDENCIES

This module requires these other modules and libraries:

  Image::Magick
  XML::Simple

COPYRIGHT AND LICENCE

Copyright (C) 2010 by Noah Petherbridge

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.

Tileset.html  view on Meta::CPAN

image will be loaded immediately.</p>
<p>
</p>
<h2><a name="void_error___">void error ()</a></h2>
<p>Print the last error message given. Example:</p>
<pre>
  $tileset-&gt;image(&quot;myfile.png&quot;) or die $tileset-&gt;error;</pre>
<p>
</p>
<h2><a name="bool_image__string_filepath_">bool image (string filepath)</a></h2>
<p>Load an image file with <code>Image::Magick</code>. The object is just kept in memory for
when you actually want to get a tile from it.</p>
<p>Returns 1 on success, undef on error.</p>
<p>
</p>
<h2><a name="bool_data__bin_data_">bool data (bin data)</a></h2>
<p>If your program already has the image's binary data in memory, it can send it
directly to this function. It will create an <code>Image::Magick</code> object based off
the binary data directly, instead of needing to read a file from disk.</p>
<p>Returns 1 on success, undef on error.</p>
<p>
</p>
<h2><a name="void_clear___">void clear ()</a></h2>
<p>Clear the internal <code>Image::Magick</code> object, unloading the tileset.</p>
<p>
</p>
<h2><a name="bool_xml__string_xmldata___string_specfile_">bool xml (string xmldata | string specfile)</a></h2>
<p>Load a specification file from XML. Pass either XML data or the path to a
file name.</p>
<p>If the data sent to this command begins with a left chevron, &lt;, or contains
newlines, it is assumed to be XML data; otherwise the filesystem is queried.</p>
<p>Returns 1 on success, undef on error.</p>
<p>
</p>

Tileset.html  view on Meta::CPAN

<p>Get the binary data of one of the tiles, named <code>id</code>, from the original
tileset.</p>
<p>You can optionally pass in a hash of named options. The following options are
supported:</p>
<pre>
  int scale:   Scale the tile before returning its data. This is a number to
               scale it by, for example '2' returns it at 200% its original size,
               while '0.5' returns it at 50% its original size.
  str size:    Resize the tile to this exact size before returning it, for
               example '64x64'.
  bool magick: If true, returns the Image::Magick object instead of the binary
               data. If you want to make additional modifications to the image
               (i.e. edit its colors, apply special effects), use the 'magick'
               option and then apply the effects yourself.</pre>
<p>The options <code>scale</code> and <code>size</code> are mutually exclusive.</p>
<p>Examples:</p>
<pre>
  # The tiles are 32x32, but lets scale it 2X so we get back a 64x64 tile
  my $tile = $ts-&gt;tile(&quot;grass&quot;, scale =&gt; 2);</pre>
<pre>
  # Get it at 1/2 its original size, or 16x16

Tileset.html  view on Meta::CPAN

<p>Returns data in the format:</p>
<pre>
  {
    speed =&gt; '...',
    tiles =&gt; [ ... ],
  };</pre>
<p>Returns undef on error.</p>
<p>
</p>
<h2><a name="imagemagick_slice__string_id_">ImageMagick slice (string id)</a></h2>
<p>Returns an <code>Image::Magick</code> object that contains the sliced tile from the
original tileset. This is mostly for internal use only.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="/Image/Magick.html">Image::Magick</a>, which powers this module's graphics handling.</p>
<p><a href="/XML/Simple.html">XML::Simple</a>, which powers this module's XML parsing.</p>
<p>
</p>
<hr />
<h1><a name="changes">CHANGES</a></h1>
<pre>
  0.01  Fri Jan 15 2010
  - Initial release.</pre>
<p>
</p>

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

package Image::Tileset;

use strict;
use warnings;
use Image::Magick;
use XML::Simple;
use Data::Dumper;

our $VERSION = '0.01';

=head1 NAME

Image::Tileset - A tileset loader.

=head1 SYNOPSIS

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

=cut

sub new {
	my $class = shift;

	my $self = {
		debug  => 0,      # Debug mode
		xml    => '',     # XML file
		spec   => [],     # Spec data (XML data in Perl form)
		image  => '',     # Image file
		magick => undef,  # Image::Magick object
		error  => '',     # Last error state
		tiles  => {},     # Tile positions in tileset
		animations => {}, # Animation information
		@_,
	};
	bless ($self,$class);

	$self->{magick} = Image::Magick->new;

	# If given an image, load it.
	if (length $self->{image}) {
		$self->image ($self->{image});
		$self->{image} = '';
	}

	# If given an XML file, load it.
	if (length $self->{xml}) {
		$self->xml ($self->{xml});

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

sub error {
	my ($self,$error) = @_;
	if (defined $error) {
		$self->{error} = $error;
	}
	return $self->{error};
}

=head2 bool image (string filepath)

Load an image file with C<Image::Magick>. The object is just kept in memory for
when you actually want to get a tile from it.

Returns 1 on success, undef on error.

=cut

sub image {
	my ($self,$image) = @_;
	$self->debug("Attempting to load image file from $image");

	# Exists?
	if (!-e $image) {
		$self->error("Can't load image file $image: file not found!");
		return undef;
	}

	# Load it with Image::Magick.
	my $x = $self->{magick}->Read($image);
	if ($x) {
		warn $x;
		return undef;
	}

	return 1;
}

=head2 bool data (bin data)

If your program already has the image's binary data in memory, it can send it
directly to this function. It will create an C<Image::Magick> object based off
the binary data directly, instead of needing to read a file from disk.

Returns 1 on success, undef on error.

=cut

sub data {
	my ($self,$data) = @_;

	# Load it with Image::Magick.
	my $x = $self->{magick}->BlobToImage($data);
	if ($x) {
		warn $x;
		return undef;
	}

	return 1;
}

=head2 void clear ()

Clear the internal C<Image::Magick> object, unloading the tileset.

=cut

sub clear {
	my $self = shift;

	undef $self->{magick};
	$self->{magick} = new Image::Magick();
}

=head2 bool xml (string xmldata | string specfile)

Load a specification file from XML. Pass either XML data or the path to a
file name.

If the data sent to this command begins with a left chevron, E<lt>, or contains
newlines, it is assumed to be XML data; otherwise the filesystem is queried.

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

tileset.

You can optionally pass in a hash of named options. The following options are
supported:

  int scale:   Scale the tile before returning its data. This is a number to
               scale it by, for example '2' returns it at 200% its original size,
               while '0.5' returns it at 50% its original size.
  str size:    Resize the tile to this exact size before returning it, for
               example '64x64'.
  bool magick: If true, returns the Image::Magick object instead of the binary
               data. If you want to make additional modifications to the image
               (i.e. edit its colors, apply special effects), use the 'magick'
               option and then apply the effects yourself.

The options C<scale> and C<size> are mutually exclusive.

Examples:

  # The tiles are 32x32, but lets scale it 2X so we get back a 64x64 tile
  my $tile = $ts->tile("grass", scale => 2);

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

	if (!exists $self->{animations}->{$id}) {
		$self->error("No animation named '$id' in tileset!");
		return undef;
	}

	return $self->{animations}->{$id};
}

=head2 ImageMagick slice (string id)

Returns an C<Image::Magick> object that contains the sliced tile from the
original tileset. This is mostly for internal use only.

=cut

sub slice {
	my ($self,$id) = @_;

	# Tile exists?
	if (!exists $self->{tiles}->{$id}) {
		$self->error("No tile named '$id' in tileset!");

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

	my $y = $self->{tiles}->{$id}->[1];
	my $crop = $dims . "+$x+$y";
	$self->debug("Cropping image clone to $crop for tile $id");
	$slice->Crop($crop);

	return $slice;
}

=head1 SEE ALSO

L<Image::Magick|Image::Magick>, which powers this module's graphics handling.

L<XML::Simple|XML::Simple>, which powers this module's XML parsing.

=head1 CHANGES

  0.01  Fri Jan 15 2010
  - Initial release.

=head1 COPYRIGHT



( run in 1.101 second using v1.01-cache-2.11-cpan-beeb90c9504 )