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