File Coverage

blib/lib/Image/Checkerboard.pm
Criterion Covered Total %
statement 45 60 75.0
branch 6 18 33.3
condition n/a
subroutine 11 12 91.6
pod 3 3 100.0
total 65 93 69.8


line stmt bran cond sub pod time code
1             package Image::Checkerboard;
2              
3 4     4   99681 use strict;
  4         27  
  4         117  
4 4     4   23 use warnings;
  4         7  
  4         113  
5              
6 4     4   2054 use Class::Utils qw(set_params);
  4         96248  
  4         71  
7 4     4   283 use Error::Pure qw(err);
  4         10  
  4         154  
8 4     4   3719 use Imager;
  4         185581  
  4         33  
9 4     4   2518 use Imager::Fill;
  4         4969  
  4         205  
10 4     4   33 use List::MoreUtils qw(none);
  4         10  
  4         38  
11              
12             our $VERSION = 0.05;
13              
14             # Constructor.
15             sub new {
16 6     6 1 6449 my ($class, @params) = @_;
17              
18             # Create object.
19 6         19 my $self = bless {}, $class;
20              
21             # Background color.
22 6         25 $self->{'bg'} = 'black';
23              
24             # Flip flag.
25 6         14 $self->{'flip'} = 1;
26              
27             # Foreground color.
28 6         15 $self->{'fg'} = 'white';
29              
30             # Sizes.
31 6         11 $self->{'width'} = 1920;
32 6         13 $self->{'height'} = 1080;
33              
34             # Image type.
35 6         12 $self->{'type'} = 'bmp';
36              
37             # Process params.
38 6         32 set_params($self, @params);
39              
40             # Flip stay.
41 4         55 $self->{'_flip_stay'} = 0;
42              
43             # Imager object.
44             $self->{'_imager'} = Imager->new(
45             'xsize' => $self->{'width'},
46 4         35 'ysize' => $self->{'height'},
47             );
48              
49             # Check type.
50 4 100       26353 if (defined $self->{'type'}) {
51 3         18 $self->_check_type($self->{'type'});
52             }
53              
54             # Object.
55 3         21 return $self;
56             }
57              
58             # Create image.
59             sub create {
60 0     0 1 0 my ($self, $path) = @_;
61              
62             # Get type.
63 0         0 my $suffix;
64 0 0       0 if (! defined $self->{'type'}) {
65              
66             # Get suffix.
67 0         0 (my $name, undef, $suffix) = fileparse($path, qr/\.[^.]*/ms);
68 0         0 $suffix =~ s/^\.//ms;
69              
70             # Jpeg.
71 0 0       0 if ($suffix eq 'jpg') {
72 0         0 $suffix = 'jpeg';
73             }
74             } else {
75 0         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 0       0 'bg' => $self->{'_flip_stay'} ? $self->{'bg'} : $self->{'fg'},
    0          
83             );
84 0 0       0 $self->{'_flip_stay'} = $self->{'_flip_stay'} == 1 ? 0 : 1;
85              
86             # Add checkboard.
87 0         0 $self->{'_imager'}->box('fill' => $fill);
88              
89             # Save file.
90 0         0 my $ret = $self->{'_imager'}->write(
91             'file' => $path,
92             'type' => $suffix,
93             );
94 0 0       0 if (! $ret) {
95             err "Cannot write file to '$path'.",
96 0         0 'Error', $self->{'_imager'}->errstr;
97             }
98            
99 0         0 return $suffix;
100             }
101              
102             # Set/Get image type.
103             sub type {
104 3     3 1 4416 my ($self, $type) = @_;
105 3 100       10 if ($type) {
106 2         8 $self->_check_type($type);
107 1         5 $self->{'type'} = $type;
108             }
109 2         7 return $self->{'type'};
110             }
111              
112             # Check supported image type.
113             sub _check_type {
114 5     5   18 my ($self, $type) = @_;
115              
116             # Check type.
117 5 100   24   70 if (none { $type eq $_ } ('bmp', 'gif', 'jpeg', 'png',
  24         52  
118             'pnm', 'raw', 'sgi', 'tga', 'tiff')) {
119              
120 2         15 err "Image type '$type' doesn't supported."
121             }
122              
123 3         15 return;
124             }
125              
126             1;
127              
128             __END__