File Coverage

blib/lib/Image/TextMode/Canvas.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Image::TextMode::Canvas;
2              
3 2     2   9328 use Moose;
  0            
  0            
4              
5             use Image::TextMode::Pixel;
6              
7             =head1 NAME
8              
9             Image::TextMode::Canvas - A canvas of text mode pixels
10              
11             =head1 DESCRIPTION
12              
13             This module represents the graphical portion of an image, i.e. a grid
14             of pixels.
15              
16             =head1 ACCESSORS
17              
18             =over 4
19              
20             =item * width - the width of the canvas
21              
22             =item * height - the height of the canvas
23              
24             =item * pixeldata - an arrayref of arrayrefs of pixel data
25              
26             =back
27              
28             =cut
29              
30             has 'width' => ( is => 'rw', isa => 'Int', default => sub { 0 } );
31              
32             has 'height' => ( is => 'rw', isa => 'Int', default => sub { 0 } );
33              
34             has 'pixeldata' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
35              
36             =head1 METHODS
37              
38             =head2 new( %args )
39              
40             Creates a new canvas.
41              
42             =head2 getpixel( $x, $y )
43              
44             Get raw pixel data at C<$x>, C<$y>.
45              
46             =cut
47              
48             sub getpixel {
49             my ( $self, $x, $y ) = @_;
50             return unless exists $self->pixeldata->[ $y ]; # avoid autovivification
51             return $self->pixeldata->[ $y ]->[ $x ];
52             }
53              
54             =head2 getpixel_obj( $x, $y, \%options )
55              
56             Create a pixel object data at C<$x>, C<$y>. Available options include:
57              
58             =over 4
59              
60             =item * blink_mode - enabed or disable blink mode for the pixel object
61              
62             =back
63              
64             =cut
65              
66             sub getpixel_obj {
67             my ( $self, $x, $y, $options ) = @_;
68             my $pixel = $self->getpixel( $x, $y );
69             return unless $pixel;
70             return Image::TextMode::Pixel->new( %$pixel, $options );
71             }
72              
73             =head2 putpixel( \%pixel, $x, $y )
74              
75             Store pixel data at C<$x>, C<$y>.
76              
77             =cut
78              
79             sub putpixel {
80             my ( $self, $pixel, $x, $y ) = @_;
81             $self->pixeldata->[ $y ]->[ $x ] = $pixel;
82              
83             my ( $w, $h ) = ( $x + 1, $y + 1 );
84             $self->height( $h ) if $self->height < $h;
85             $self->width( $w ) if $self->width < $w;
86             }
87              
88             =head2 dimensions( )
89              
90             returns a list of the width and height of the image.
91              
92             =cut
93              
94             sub dimensions {
95             my $self = shift;
96             return $self->width, $self->height;
97             }
98              
99             =head2 clear_screen( )
100              
101             Clears the canvas pixel data.
102              
103             =cut
104              
105             sub clear_screen {
106             my $self = shift;
107             $self->width( 0 );
108             $self->height( 0 );
109             $self->pixeldata( [] );
110             }
111              
112             =head2 clear_line( $y, [ \@range ] )
113              
114             Clears the data at line C<$y>. Specify a range to clear only a portion of
115             line C<$y>.
116              
117             =cut
118              
119             sub clear_line {
120             my $self = shift;
121             my $y = shift;
122             my $range = shift;
123              
124             return unless defined $self->pixeldata->[ $y ];
125              
126             if ( !$range ) {
127             $self->pixeldata->[ $y ] = [];
128             }
129             else {
130             $range->[ 1 ] = @{ $self->pixeldata->[ $y ] } - 1 if $range->[ 1 ] == -1;
131             $self->pixeldata->[ $y ]->[ $_ ] = undef
132             for $range->[ 0 ] .. $range->[ 1 ];
133             }
134             }
135              
136             =head2 delete_line( $y )
137              
138             Removes the line from the canvas, moving all subsquent lines up.
139              
140             =cut
141              
142             sub delete_line {
143             my $self = shift;
144             my $y = shift;
145              
146             return unless exists $self->pixeldata->[ $y ];
147              
148             delete @{ $self->pixeldata }[ $y ];
149             $self->height( $self->height - 1 );
150             }
151              
152             =head2 as_ascii( )
153              
154             Returns only the character data stored in the canvas.
155              
156             =cut
157              
158             sub as_ascii {
159             my ( $self ) = @_;
160              
161             my $output = '';
162             for my $row ( @{ $self->pixeldata } ) {
163             for my $col ( @$row ) {
164             $output .= defined $col
165             && defined $col->{ char } ? $col->{ char } : ' ';
166             }
167             $output .= "\n";
168             }
169              
170             return $output;
171             }
172              
173             =head2 max_x( $line )
174              
175             Finds the last defined pixel on a given line. Useful for optimizing writes
176             in formats where width matters. Returns undef for a missing line.
177              
178             =cut
179              
180             sub max_x {
181             my ( $self, $y ) = @_;
182             my $line = $self->pixeldata->[ $y ];
183              
184             return unless $line;
185              
186             my $x;
187             for ( 0 .. @$line - 1 ) {
188             $x = $_ if defined $line->[ $_ ];
189             }
190              
191             return $x;
192             }
193              
194             =head2 ansiscale( $factor )
195              
196             Perform nearest neighbor scaling in text mode. Returns a new textmode
197             image.
198              
199             # scale down to 1/4 the original size
200             my $scaled = $image->ansiscale( 0.25 );
201              
202             =cut
203              
204             sub ansiscale {
205             my ( $self, $factor ) = @_;
206              
207             my $new = ( ref $self )->new;
208             my $width = $self->width * $factor;
209             my $height = $self->height * $factor;
210              
211             $width = int( $width + 1 ) if int( $width ) != $width;
212             $height = int( $height + 1 ) if int( $height ) != $height;
213              
214             my $oldpixels = $self->pixeldata;
215             my $newpixels = [];
216              
217             my $inv_ratio = ( 1 / $factor );
218              
219             for my $y ( 0 .. $height - 1 ) {
220             for my $x ( 0 .. $width - 1 ) {
221             my $px = int( $x * $inv_ratio );
222             my $py = int( $y * $inv_ratio );
223              
224             $newpixels->[ $y ]->[ $x ] = $oldpixels->[ $py ]->[ $px ];
225             }
226             }
227              
228             $new->width( $width );
229             $new->height( $height );
230             $new->pixeldata( $newpixels );
231             return $new;
232             }
233              
234             no Moose;
235              
236             __PACKAGE__->meta->make_immutable;
237              
238             =head1 AUTHOR
239              
240             Brian Cassidy E<lt>bricas@cpan.orgE<gt>
241              
242             =head1 COPYRIGHT AND LICENSE
243              
244             Copyright 2008-2013 by Brian Cassidy
245              
246             This library is free software; you can redistribute it and/or modify
247             it under the same terms as Perl itself.
248              
249             =cut
250              
251             1;