File Coverage

blib/lib/Image/Random.pm
Criterion Covered Total %
statement 24 77 31.1
branch 0 20 0.0
condition 0 9 0.0
subroutine 8 15 53.3
pod 4 4 100.0
total 36 125 28.8


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