Permalink
Browse files

サンプルスクリプト追加

  • Loading branch information...
kentaro committed Apr 10, 2011
1 parent 1c258d7 commit 5369fc815166f124d22fd696e2b7c1a79dd0db1f
Showing with 132 additions and 2 deletions.
  1. +11 −0 eg/marquee.pl
  2. +11 −0 eg/rmarquee.pl
  3. +11 −0 eg/shake.pl
  4. +99 −2 lib/Imager/Marquee.pm
View
@@ -0,0 +1,11 @@
+package main;
+use strict;
+use warnings;
+use Imager::Marquee;
+
+my $image = Imager::Marquee->new(file => shift);
+ $image->marquee(3)->write(
+ file => 'marquee.gif',
+ gif_loop => 0,
+ delay => 1,
+ );
View
@@ -0,0 +1,11 @@
+package main;
+use strict;
+use warnings;
+use Imager::Marquee;
+
+my $image = Imager::Marquee->new(file => shift);
+ $image->rmarquee(3)->write(
+ file => 'rmarquee.gif',
+ gif_loop => 0,
+ delay => 1,
+ );
View
@@ -0,0 +1,11 @@
+package main;
+use strict;
+use warnings;
+use Imager::Marquee;
+
+my $image = Imager::Marquee->new(file => shift);
+ $image->shake(1)->write(
+ file => 'shake.gif',
+ gif_loop => 0,
+ delay => 1,
+ );
View
@@ -1,12 +1,109 @@
package Imager::Marquee;
+use 5.008001;
use strict;
use warnings;
-use 5.008001;
-our $VERSION = '0.01';
+use Imager;
+use List::Rubyish::Circular;
+our $VERSION = '0.01';
+
+sub new {
+ my ($class, %args) = @_;
+ my $image = Imager->new(%args)
+ or dir Imager->errstr;
+
+ bless {
+ image => $image,
+ matrix => $class->matrix_for($image),
+ images => [],
+ }, $class;
+}
+
+sub marquee {
+ my ($self, $count, $direction) = @_;
+ $count ||= 1;
+
+ my @images;
+ for (my $i = 0; $i < $self->{image}->getwidth; $i += $count) {
+ $self->cycle($count, $direction);
+ }
+
+ $self;
+}
+
+sub rmarquee {
+ my ($self, $count) = @_;
+ $count ||= 1;
+ $self->marquee($count, 'right');
+}
+
+sub shake {
+ my ($self, $count) = @_;
+ $count ||= int($self->{image}->getwidth/100);
+ $self->cycle($_) for (1 .. $count);
+ $self->rcycle($_) for (- $count .. $count);
+ $self;
+}
+
+sub write {
+ my ($self, %args) = @_;
+ Imager->write_multi({ %args, type => 'gif' }, @{$self->{images}});
+}
+
+sub matrix_for {
+ my ($class, $image) = @_;
+ my $height = $image->getheight;
+
+ my @matrix;
+ for (my $i = 0; $i < $height; $i++) {
+ my @colors = $image->getpixel(
+ x => [ 0 .. ($image->getwidth - 1) ],
+ y => [ map { $i } (0 .. ($image->getwidth - 1)) ],
+ );
+ my $row = List::Rubyish::Circular->new(@colors);
+ push @matrix, $row;
+ }
+
+ \@matrix;
+}
+
+sub cycle {
+ my ($self, $count, $direction) = @_;
+ $count ||= 1;
+
+ my $image = Imager->new(
+ xsize => $self->{image}->getwidth,
+ ysize => $self->{image}->getheight,
+ channels => $self->{image}->getchannels,
+ );
+
+ for (my $j = 0; $j < $self->{image}->getwidth; $j++) {
+ if (($direction || '') eq 'right') {
+ $self->{matrix}->[$j] = $self->{matrix}->[$j]->rcycle($count);
+ } else {
+ $self->{matrix}->[$j] = $self->{matrix}->[$j]->cycle($count);
+ }
+
+ $image->setpixel(
+ x => $_,
+ y => $j,
+ color => $self->{matrix}->[$j]->[$_],
+ ) for (0 .. ($self->{image}->getwidth - 1));
+ }
+
+ push @{$self->{images}}, $image;
+ $self;
+}
+
+sub rcycle {
+ my ($self, $count) = @_;
+ $count ||= 1;
+ $self->cycle($count, 'right');
+}
1;
+
__END__
=encoding utf8

0 comments on commit 5369fc8

Please sign in to comment.