TextMode/Canvas.pm

filter: Statementstmt 77.5% 62 / 80 Branchbran 69.2% 18 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.8% 13 / 14 Podpod 100% 10 / 10 totaltotal 77.0% 104 / 135 CC 21 SCAR 19.7
CC
21
Cov
74%
ansiscale
30.0
as_ascii
5.3
clear_line
5.0
CRAP 7.2
« TextMode/Reader/Bin.pm ↑ Summary TextMode/Writer/ANSIMation.pm »
1 package Image::TextMode::Canvas;
2  
3 29 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 29 ▶use Types::Standard qw( Int ArrayRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use Image::TextMode::Pixel;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 29 has 'width' => ( is => 'rw', lazy => 1, isa => Int, default => 0 );
31  
32 29 has 'height' => ( is => 'rw', lazy => 1, isa => Int, default => 0 );
33  
34 42 ▶has 'pixeldata' => ( is => 'rw', lazy => 1, isa => ArrayRef, default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
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 255 ▶ my ( $self, $x, $y ) = @_;
Subroutine getpixel: called CC=2 SCAR=6.9
Pod documented
50 255 ▶ return unless exists $self->pixeldata->[ $y ]; # avoid autovivification
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->pixeldata->[$y] 1 254 255
51 254 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 2 ▶ my ( $self, $x, $y, $options ) = @_;
Subroutine getpixel_obj: called CC=2 SCAR=6.9
Pod documented
68 2 my $pixel = $self->getpixel( $x, $y );
69 2 ▶ return unless $pixel;
Branch: 1 branch
BranchTrueFalseTotal
unless $pixel 1 1 2
70 1 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 1062 ▶ my ( $self, $pixel, $x, $y ) = @_;
Subroutine putpixel: called CC=3 SCAR=11.0
Pod documented
81 1062 $self->pixeldata->[ $y ]->[ $x ] = $pixel;
82  
83 1062 my ( $w, $h ) = ( $x + 1, $y + 1 );
84 1062 ▶ $self->height( $h ) if $self->height < $h;
Branch: 1 branch
BranchTrueFalseTotal
if $self->height < $h 51 1011 1062
85 1062 ▶ $self->width( $w ) if $self->width < $w;
Branch: 1 branch
BranchTrueFalseTotal
if $self->width < $w 1041 21 1062
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 6 ▶ my $self = shift;
Subroutine dimensions: called CC=1 SCAR=0.0
Pod documented
96 6 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 5 ▶ my $self = shift;
Subroutine clear_screen: called CC=1 SCAR=0.0
Pod documented
107 5 $self->width( 0 );
108 5 $self->height( 0 );
109 5 $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 6 ▶ my $self = shift;
Subroutine clear_line: called CC=5 SCAR=16.1
Pod documented
121 6 my $y = shift;
122 6 my $range = shift;
123  
124 6 ▶ return unless defined $self->pixeldata->[ $y ];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->pixeldata->[$y] 0 6 6
125  
126 6 ▶ if ( !$range ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $range) { } 2 4 6
127 2 $self->pixeldata->[ $y ] = [];
128 }
129 else {
130 4 ▶ $range->[ 1 ] = @{ $self->pixeldata->[ $y ] } - 1 if $range->[ 1 ] == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $range->[1] == -1 2 2 4
131 $self->pixeldata->[ $y ]->[ $_ ] = undef
132 4 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 2 ▶ my $self = shift;
Subroutine delete_line: called CC=2 SCAR=6.9
Pod documented
144 2 my $y = shift;
145  
146 2 ▶ return unless exists $self->pixeldata->[ $y ];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->pixeldata->[$y] 1 1 2
147  
148 1 delete @{ $self->pixeldata }[ $y ];
149 1 $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 1 ▶ my ( $self ) = @_;
Subroutine as_ascii: called CC=5 SCAR=16.7
Pod documented
160  
161 1 my $output = '';
162 1 for my $row ( @{ $self->pixeldata } ) {
163 1 for my $col ( @$row ) {
164 $output .= defined $col
165 1 ▶ && defined $col->{ char } ? $col->{ char } : ' ';
Branch: 1 branch
BranchTrueFalseTotal
defined $col && defined $col->{'char'} ? : 1 0 1
Condition33%1/3
defined $col and defined $col->{"char"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $col and defined $col->{"char"}
defined $col defined $col->{"char"}
Truth table33%1/3
A and B
A= defined $colB= defined $col->{"char"}
ABresult
0X0
100
111
166 }
167 1 $output .= "\n";
168 }
169  
170 1 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 3 ▶ my ( $self, $y ) = @_;
Subroutine max_x: called CC=4 SCAR=14.1
Pod documented
182 3 my $line = $self->pixeldata->[ $y ];
183  
184 3 ▶ return unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 3 3
185  
186 3 my $x;
187 3 for ( 0 .. @$line - 1 ) {
188 12 ▶ $x = $_ if defined $line->[ $_ ];
Branch: 1 branch
BranchTrueFalseTotal
if defined $line->[$_] 12 0 12
189 }
190  
191 3 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 0 ▶ my ( $self, $factor ) = @_;
Subroutine ansiscale: not called CC=5 SCAR=34.0
Pod documented
206  
207 0 my $new = ( ref $self )->new;
208 0 my $width = $self->width * $factor;
209 0 my $height = $self->height * $factor;
210  
211 0 ▶ $width = int( $width + 1 ) if int( $width ) != $width;
Branch: 1 branch
BranchTrueFalseTotal
if int $width != $width 0 0 0
212 0 ▶ $height = int( $height + 1 ) if int( $height ) != $height;
Branch: 1 branch
BranchTrueFalseTotal
if int $height != $height 0 0 0
213  
214 0 my $oldpixels = $self->pixeldata;
215 0 my $newpixels = [];
216  
217 0 my $inv_ratio = ( 1 / $factor );
218  
219 0 for my $y ( 0 .. $height - 1 ) {
220 0 for my $x ( 0 .. $width - 1 ) {
221 0 my $px = int( $x * $inv_ratio );
222 0 my $py = int( $y * $inv_ratio );
223  
224 0 $newpixels->[ $y ]->[ $x ] = $oldpixels->[ $py ]->[ $px ];
225 }
226 }
227  
228 0 $new->width( $width );
229 0 $new->height( $height );
230 0 $new->pixeldata( $newpixels );
231 0 return $new;
232 }
233  
234 =head1 AUTHOR
235  
236 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
237  
238 =head1 COPYRIGHT AND LICENSE
239  
240 Copyright 2008-2014 by Brian Cassidy
241  
242 This library is free software; you can redistribute it and/or modify
243 it under the same terms as Perl itself.
244  
245 =cut
246  
247 29 1;
« TextMode/Reader/Bin.pm ↑ Summary TextMode/Writer/ANSIMation.pm »