blib/lib/Image/Random.pm

filter: Statementstmt 100% 76 / 76 Branchbran 100% 18 / 18 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 100% 123 / 123 CC 12 SCAR 9.6
CC
12
Cov
100%
create
5.0
new
4.0
sizes
3.0
CRAP 2.6
↑ Summary
1 package Image::Random;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Imager::Color;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use List::MoreUtils qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 our $VERSION = 0.09;
14  
15 # Constructor.
16 sub new {
17 13 ▶ my ($class, @params) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
18  
19 # Create object.
20 13 my $self = bless {}, $class;
21  
22 # Background color.
23 13 $self->{'color'} = undef;
24  
25 # Image type.
26 13 $self->{'type'} = 'bmp';
27  
28 # Sizes.
29 13 $self->{'height'} = 1080;
30 13 $self->{'width'} = 1920;
31  
32 # Process params.
33 13 set_params($self, @params);
34  
35 # Image magick object.
36 11 $self->_imager;
37  
38 # Check color.
39 11 ▶ if (defined $self->{'color'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"color"} and not $self->{"color"}->isa("Imager::Color")) 1 10 11
Condition100%3/3
defined $self->{"color"} and not $self->{"color"}->isa("Imager::Color")
!ll&&!rl&&r
821
MC/DC100%2/2
defined $self->{"color"} and not $self->{"color"}->isa("Imager::Color")
defined $self->{"color"} not $self->{"color"}->isa("Imager::Color")
Truth table100%3/3
A and not(B)
A= defined $self->{"color"}B= not $self->{"color"}->isa("Imager::Color")
ABresult
0X0
100
111
40 && ! $self->{'color'}->isa('Imager::Color')) {
41  
42 1 err 'Bad background color definition. Use Imager::Color '.
43 'object.';
44 }
45  
46 # Check type.
47 10 ▶ if (defined $self->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"type"}) 7 3 10
48 7 $self->_check_type($self->{'type'});
49 }
50  
51 # Object.
52 9 return $self;
53 }
54  
55 # Create image.
56 sub create {
57 6 ▶ my ($self, $path) = @_;
Subroutine create: called CC=5 SCAR=16.1
Pod documented
58  
59 # Background color.
60 6 my $background;
61 6 ▶ if ($self->{'color'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'color'}) { } 1 5 6
62 1 $background = $self->{'color'};
63 } else {
64 5 $background = Imager::Color->new(int rand 256, int rand 256,
65 int rand 256);
66 }
67  
68 # Create image.
69 6 $self->{'i'}->box(
70 'color' => $background,
71 'filled' => 1,
72 );
73  
74 # Get type.
75 6 my $suffix;
76 6 ▶ if (! defined $self->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'type'}) { } 2 4 6
77  
78 # Get suffix.
79 2 (my $name, undef, $suffix) = fileparse($path, qr/\.[^.]*/ms);
80 2 $suffix =~ s/^\.//ms;
81  
82 # Jpeg.
83 2 ▶ if ($suffix eq 'jpg') {
Branch: 1 branch
BranchTrueFalseTotal
if ($suffix eq "jpg") 1 1 2
84 1 $suffix = 'jpeg';
85 }
86  
87 # Check type.
88 2 $self->_check_type($suffix);
89 } else {
90 4 $suffix = $self->{'type'};
91 }
92  
93 # Save.
94 6 my $ret = $self->{'i'}->write(
95 'file' => $path,
96 'type' => $suffix,
97 );
98 6 ▶ if (! $ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 1 5 6
99 1 err "Cannot write file to '$path'.",
100 'Error', Imager->errstr;
101 }
102  
103 5 return $suffix;
104 }
105  
106 # Set/Get image sizes.
107 sub sizes {
108 3 ▶ my ($self, $width, $height) = @_;
Subroutine sizes: called CC=3 SCAR=11.0
Pod documented
109 3 ▶ if ($width && $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width and $height) 1 2 3
Condition100%3/3
$width and $height
!ll&&!rl&&r
111
MC/DC100%2/2
$width and $height
$width $height
Truth table100%3/3
A and B
A= $widthB= $height
ABresult
0X0
100
111
110 1 $self->{'width'} = $width;
111 1 $self->{'height'} = $height;
112 1 $self->_imager;
113 }
114 3 return ($self->{'width'}, $self->{'height'});
115 }
116  
117 # Set/Get image type.
118 sub type {
119 3 ▶ my ($self, $type) = @_;
Subroutine type: called CC=2 SCAR=6.9
Pod documented
120 3 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 2 1 3
121 2 $self->_check_type($type);
122 1 $self->{'type'} = $type;
123 }
124 2 return $self->{'type'};
125 }
126  
127 # Check supported image type.
128 sub _check_type {
129 11 ▶ my ($self, $type) = @_;
Subroutine _check_type: called CC=2 SCAR=6.9
130  
131 # Check type.
132 11 ▶ 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")) 2 9 11
133 'pnm', 'raw', 'sgi', 'tga', 'tiff')) {
134  
135 2 err "Image type '$type' doesn't supported.";
136 }
137  
138 9 return;
139 }
140  
141 # Create imager object.
142 sub _imager {
143 12 ▶ my $self = shift;
Subroutine _imager: called CC=1 SCAR=0.0
144 $self->{'i'} = Imager->new(
145 'xsize' => $self->{'width'},
146 12 'ysize' => $self->{'height'},
147 );
148 12 return;
149 }
150  
151 6 1;
152  
153 __END__
↑ Summary