Piet-Interpreter
view release on metacpan or search on metacpan
As much as I love Class::Struct, I'm pulling stuff out into
methods. Sigh. Refactored and remodularized the PVM (Piet
Virtual Machine), and added basic stepping functionality.
Added a ton of documentation, and cleaned up the code
considerably. I think this may be ready for public consumption -
consider this the "beta" release.
0.03 Wed Nov 21 05:11:23 2001
Added Image::Magick to PREREQ_PM list in Makefile.PL for testing.
Reformatted the perldoc to avoid making eyes bleed.
Interpreter.pm view on Meta::CPAN
package Piet::Interpreter;
use 5.6.0; # or so.
use strict;
use Carp;
use Image::Magick;
our $VERSION = '0.03';
=head1 NAME
Piet::Interpreter - Interpreter for the Piet programming language
=head1 SYNOPSIS
use Piet::Interpreter;
Interpreter.pm view on Meta::CPAN
paintings. The language is named after Piet Mondrian, who pioneered
the field of geometric abstract art. The language is fully described
at http://www.physics.usyd.edu.au/~mar/esoteric/piet.html. A Piet
program is an image file, usually a gif, which uses a set of 20 colors
and the transitions between blocks of those colors to define a series
of instructions and program flow. See the above URL for more details.
(Note: some sample programs there may not work, as they were
constructed before a working interpreter was available.)
Since Piet is a visual language, an image parsing mechanism is
required. This module uses Image::Magick, so it would be to your
advantage to download, install, and test that module and its
related stuff before trying to use this one.
=cut
# Initialize variables and lookup hashes
$| = 1; # buffer bad.
my $HEX_BLACK = '000000';
Interpreter.pm view on Meta::CPAN
$self->{_current_block} = undef;
$self->{_block_value} = undef;
$self->{_last_color} = $self->matrix($self->{_cx},$self->{_cy});
}
=item $piet->image('myprog.gif');
Loads in a program image from the specified file. The interpreter was
designed and tested using gif images, but any format that is supported
by Image::Magick should work just fine. Once the file has been
loaded, it is inspected and processed, creating a run-matrix and
determining some useful properties from the image.
Note: Be sure to set the codel size, if needed, before loading the
image. Otherwise, a size of 1 will be assumed, and the codel columns
and rows will not be calculated correctly, causing pain and
irritation.
=cut
Interpreter.pm view on Meta::CPAN
return;
}
unless (-e $file) {
carp "File $file does not exist in Piet::Interpreter::image()";
return;
}
# Read file into object and process
$self->{_filename} = $file;
$self->{_image} = Image::Magick->new;
$self->{_image}->Read($file);
$self->_process_image;
}
=item $piet->run;
Starts the Piet interpreter running from the upper-left codel.
Program execution is described under "Language Concepts", below.
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' => 'Piet::Interpreter',
'VERSION_FROM' => 'Interpreter.pm', # finds $VERSION
'PREREQ_PM' => {Image::Magick => 5.39,
POSIX => 1.03},
($] >= 5.005 ? ## Add these new keywords supported since 5.005
(ABSTRACT_FROM => 'Interpreter.pm', # retrieve abstract from module
AUTHOR => 'Marc Majcher <piet-interpreter@majcher.com>') : ()),
);
paintings. The language is named after Piet Mondrian, who pioneered
the field of geometric abstract art. The language is fully described
at http://www.physics.usyd.edu.au/~mar/esoteric/piet.html. A Piet
program is an image file, usually a gif, which uses a set of 20 colors
and the transitions between blocks of those colors to define a series
of instructions and program flow. See the above URL for more details.
INSTALLATION
Since Piet is a visual language, an image parsing mechanism is
required. This module uses Image::Magick, so it would be to your
advantage to download, install, and test that module and its
related stuff before trying to use this one. I know, it's sort
of a pain in the ass, but then again, so is Piet.
To install this module type the following:
perl Makefile.PL
make
make test
make install
See the perldoc for complete usage instructions.
DEPENDENCIES
This module requires these other modules and libraries:
Image::Magick
POSIX
COPYRIGHT AND LICENCE
This code may be modified and distributed under the same license that
comes with Perl. Use at your own risk.
Copyright (C) 2001 Marc Majcher
"The only difference between me and a madman is that I am not mad."
bin/gif2txt.pl view on Meta::CPAN
#!/usr/local/bin/perl -w
#
# Piet utility - converts images to human-readable text files
#
use strict;
use Image::Magick;
use Getopt::Std;
getopts('s:');
our($opt_s);
my $codel_size = $opt_s || 1;
my $infile = shift || die "must specify image file";
die "can't find file $infile" unless (-f $infile);
my $img = Image::Magick->new;
$img->Read(filename=>$infile);
my $cols = $img->Get('columns');
my $rows = $img->Get('rows');
print "# Image $infile: ($cols x $rows)\n";
my %hex2a = ( 'FFC0C0' => 'lR', 'FFFFC0' => 'lY', 'C0FFC0' => 'lG',
'C0FFFF' => 'lC', 'C0C0FF' => 'lB', 'FFC0FF' => 'lM',
'FF0000' => ' R', 'FFFF00' => ' Y', '00FF00' => ' G',
bin/txt2gif.pl view on Meta::CPAN
#!/usr/local/bin/perl -w
#
# Piet utility - converts human-readable text files to images
#
use strict;
use Image::Magick;
use Getopt::Std;
getopts('s:o:');
our($opt_s, $opt_o);
my $codel_size = $opt_s || 1; # not yet (todo)
my $outfile = $opt_o || die "must specify output file";
my $infile = shift || die "must specify text file";
open(IN,$infile) || die "can't open text file: $!";
bin/txt2gif.pl view on Meta::CPAN
my @tmp = split;
push(@txtarr,\@tmp);
$rows++;
$cols = ($cols>@tmp)?$cols:@tmp;
}
$cols *= $codel_size;
$rows *= $codel_size;
print "Converting $infile: ($cols x $rows)\n";
my $img = Image::Magick->new;
$img->Set(size=>"$cols"."x$rows");
$img->ReadImage('xc:white');
my $j = 0;
for my $arref (@txtarr) {
my $i = 0;
for my $cabbr (@$arref) {
# $img->Draw doesn't seem to work - why?
#!/usr/local/bin/perl -w
use Test;
BEGIN { plan tests => 39, onfail => sub { print "\n*** Tests failed! ***\n" } };
END { print "not ok 1\n" unless $loaded }
use Image::Magick;
ok(1);
use Piet::Interpreter;
$loaded = 1;
my $h = Piet::Interpreter->new(codel_size => 10,
image => 'testimg/hello2big.gif', );
print "\nRunning hello:\n";
print "Codel size = $h->{_codel_size}, ";
ok($h->codel_size,10);
( run in 0.903 second using v1.01-cache-2.11-cpan-beeb90c9504 )