blib/lib/Image/Checkerboard.pm

filter: Statementstmt 67.7% 42 / 62 Branchbran 22.2% 4 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 10 / 12 Podpod 100% 3 / 3 totaltotal 62.1% 59 / 95 CC 10 SCAR 30.0
CC
10
Cov
58%
create
56.0
type
6.0
new
2.0
CRAP 20.0
↑ Summary
1 package Image::Checkerboard;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Imager::Fill;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use List::MoreUtils qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = 0.04;
13  
14 # Constructor.
15 sub new {
16 5 ▶ my ($class, @params) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
17  
18 # Create object.
19 5 my $self = bless {}, $class;
20  
21 # Background color.
22 5 $self->{'bg'} = 'black';
23  
24 # Flip flag.
25 5 $self->{'flip'} = 1;
26  
27 # Foreground color.
28 5 $self->{'fg'} = 'white';
29  
30 # Sizes.
31 5 $self->{'width'} = 1920;
32 5 $self->{'height'} = 1080;
33  
34 # Image type.
35 5 $self->{'type'} = 'bmp';
36  
37 # Process params.
38 5 set_params($self, @params);
39  
40 # Flip stay.
41 3 $self->{'_flip_stay'} = 0;
42  
43 # Imager object.
44 $self->{'_imager'} = Imager->new(
45 'xsize' => $self->{'width'},
46 3 'ysize' => $self->{'height'},
47 );
48  
49 # Check type.
50 3 ▶ if (defined $self->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"type"}) 2 1 3
51 2 $self->_check_type($self->{'type'});
52 }
53  
54 # Object.
55 2 return $self;
56 }
57  
58 # Create image.
59 sub create {
60 0 ▶ my ($self, $path) = @_;
Subroutine create: not called CC=7 SCAR=40.3
Pod documented
61  
62 # Get type.
63 0 my $suffix;
64 0 ▶ if (! defined $self->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'type'}) { } 0 0 0
65  
66 # Get suffix.
67 0 (my $name, undef, $suffix) = fileparse($path, qr/\.[^.]*/ms);
68 0 $suffix =~ s/^\.//ms;
69  
70 # Jpeg.
71 0 ▶ if ($suffix eq 'jpg') {
Branch: 1 branch
BranchTrueFalseTotal
if ($suffix eq "jpg") 0 0 0
72 0 $suffix = 'jpeg';
73 }
74 } else {
75 0 $suffix = $self->{'type'};
76 }
77  
78 # Fill.
79 my $fill = Imager::Fill->new(
80 'hatch' => 'check4x4',
81 'fg' => $self->{'_flip_stay'} ? $self->{'fg'} : $self->{'bg'},
82 0 ▶ 'bg' => $self->{'_flip_stay'} ? $self->{'bg'} : $self->{'fg'},
Branch: 2 branches
BranchTrueFalseTotal
$self->{'_flip_stay'} ? : 0 0 0
$self->{'_flip_stay'} ? : 0 0 0
83 );
84 0 ▶ $self->{'_flip_stay'} = $self->{'_flip_stay'} == 1 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_flip_stay'} == 1 ? : 0 0 0
85  
86 # Add checkboard.
87 0 $self->{'_imager'}->box('fill' => $fill);
88  
89 # Save file.
90 0 my $ret = $self->{'_imager'}->write(
91 'file' => $path,
92 'type' => $suffix,
93 );
94 0 ▶ if (! $ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
95 err "Cannot write file to '$path'.",
96 0 'Error', $self->{'_imager'}->errstr;
97 }
98
99 0 return $suffix;
100 }
101  
102 # Set/Get image type.
103 sub type {
104 0 ▶ my ($self, $type) = @_;
Subroutine type: not called CC=2 SCAR=17.9
Pod documented
105 0 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 0 0 0
106 0 $self->_check_type($type);
107 0 $self->{'type'} = $type;
108 }
109 0 return $self->{'type'};
110 }
111  
112 # Check supported image type.
113 sub _check_type {
114 2 ▶ my ($self, $type) = @_;
Subroutine _check_type: called CC=2 SCAR=6.9
115  
116 # Check type.
117 2 ▶ if (none { $type eq $_ } ('bmp', 'gif', 'jpeg', 'png',
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $type eq $_; } , "bmp", "gif", "jpeg", "png", "pnm", "raw", "sgi", "tga", "tiff")) 1 1 2
118 'pnm', 'raw', 'sgi', 'tga', 'tiff')) {
119  
120 1 err "Image type '$type' doesn't supported."
121 }
122  
123 1 return;
124 }
125  
126 3 1;
127  
128 __END__
↑ Summary