File Coverage

blib/lib/Image/Random.pm
Criterion Covered Total %
statement 67 74 90.5
branch 13 18 72.2
condition 6 6 100.0
subroutine 15 15 100.0
pod 4 4 100.0
total 105 117 89.7


line stmt bran cond sub pod time code
1             package Image::Random;
2              
3             # Pragmas.
4 7     7   91926 use strict;
  7         16  
  7         252  
5 7     7   29 use warnings;
  7         10  
  7         245  
6              
7             # Modules.
8 7     7   3965 use Class::Utils qw(set_params);
  7         73113  
  7         147  
9 7     7   499 use Error::Pure qw(err);
  7         14  
  7         321  
10 7     7   41 use File::Basename qw(fileparse);
  7         14  
  7         508  
11 7     7   6852 use Imager;
  7         321306  
  7         56  
12 7     7   470 use Imager::Color;
  7         11  
  7         171  
13 7     7   36 use List::MoreUtils qw(none);
  7         11  
  7         4780  
14              
15             # Version.
16             our $VERSION = 0.06;
17              
18             # Constructor.
19             sub new {
20 10     10 1 8851 my ($class, @params) = @_;
21              
22             # Create object.
23 10         68 my $self = bless {}, $class;
24              
25             # Background color.
26 10         41 $self->{'color'} = undef;
27              
28             # Image type.
29 10         22 $self->{'type'} = 'bmp';
30              
31             # Sizes.
32 10         22 $self->{'height'} = 1080;
33 10         16 $self->{'width'} = 1920;
34              
35             # Process params.
36 10         45 set_params($self, @params);
37              
38             # Image magick object.
39 8         87 $self->_imager;
40              
41             # Check color.
42 8 100 100     111 if (defined $self->{'color'}
43             && ! $self->{'color'}->isa('Imager::Color')) {
44              
45 1         9 err 'Bad background color definition. Use Imager::Color '.
46             'object.';
47             }
48              
49             # Check type.
50 7 100       47 if (defined $self->{'type'}) {
51 6         39 $self->_check_type($self->{'type'});
52             }
53              
54             # Object.
55 6         37 return $self;
56             }
57              
58             # Create image.
59             sub create {
60 3     3 1 2375 my ($self, $path) = @_;
61              
62             # Background color.
63 3         7 my $background;
64 3 50       11 if ($self->{'color'}) {
65 0         0 $background = $self->{'color'};
66             } else {
67 3         38 $background = Imager::Color->new(int rand 256, int rand 256,
68             int rand 256);
69             }
70              
71             # Create image.
72 3         106 $self->{'i'}->box(
73             'color' => $background,
74             'filled' => 1,
75             );
76              
77             # Get type.
78 3         25326 my $suffix;
79 3 50       26 if (! defined $self->{'type'}) {
80              
81             # Get suffix.
82 0         0 (my $name, undef, $suffix) = fileparse($path, qr/\.[^.]*/ms);
83 0         0 $suffix =~ s/^\.//ms;
84              
85             # Jpeg.
86 0 0       0 if ($suffix eq 'jpg') {
87 0         0 $suffix = 'jpeg';
88             }
89              
90             # Check type.
91 0         0 $self->_check_type($suffix);
92             } else {
93 3         10 $suffix = $self->{'type'};
94             }
95              
96             # Save.
97 3         26 my $ret = $self->{'i'}->write(
98             'file' => $path,
99             'type' => $suffix,
100             );
101 3 50       55642 if (! $ret) {
102 0         0 err "Cannot write file to '$path'.",
103             'Error', Imager->errstr;
104             }
105              
106 3         59 return $suffix;
107             }
108              
109             # Set/Get image sizes.
110             sub sizes {
111 3     3 1 116560 my ($self, $width, $height) = @_;
112 3 100 100     21 if ($width && $height) {
113 1         4 $self->{'width'} = $width;
114 1         2 $self->{'height'} = $height;
115 1         4 $self->_imager;
116             }
117 3         658 return ($self->{'width'}, $self->{'height'});
118             }
119              
120             # Set/Get image type.
121             sub type {
122 3     3 1 1494 my ($self, $type) = @_;
123 3 100       10 if ($type) {
124 2         7 $self->_check_type($type);
125 1         2 $self->{'type'} = $type;
126             }
127 2         6 return $self->{'type'};
128             }
129              
130             # Check supported image type.
131             sub _check_type {
132 8     8   21 my ($self, $type) = @_;
133              
134             # Check type.
135 8 100   26   116 if (none { $type eq $_ } ('bmp', 'gif', 'jpeg', 'png',
  26         55  
136             'pnm', 'raw', 'sgi', 'tga', 'tiff')) {
137              
138 2         21 err "Image type '$type' doesn't supported.";
139             }
140              
141 6         30 return;
142             }
143              
144             # Create imager object.
145             sub _imager {
146 9     9   18 my $self = shift;
147 9         67 $self->{'i'} = Imager->new(
148             'xsize' => $self->{'width'},
149             'ysize' => $self->{'height'},
150             );
151 9         65852 return;
152             }
153              
154             1;
155              
156             __END__