File Coverage

blib/lib/Image/Checkerboard.pm
Criterion Covered Total %
statement 40 60 66.6
branch 4 18 22.2
condition n/a
subroutine 10 12 83.3
pod 3 3 100.0
total 57 93 61.2


line stmt bran cond sub pod time code
1             package Image::Checkerboard;
2              
3 3     3   83659 use strict;
  3         19  
  3         83  
4 3     3   16 use warnings;
  3         6  
  3         81  
5              
6 3     3   1331 use Class::Utils qw(set_params);
  3         73277  
  3         57  
7 3     3   215 use Error::Pure qw(err);
  3         6  
  3         112  
8 3     3   2823 use Imager;
  3         131116  
  3         23  
9 3     3   1806 use Imager::Fill;
  3         3572  
  3         104  
10 3     3   21 use List::MoreUtils qw(none);
  3         6  
  3         27  
11              
12             our $VERSION = 0.04;
13              
14             # Constructor.
15             sub new {
16 5     5 1 5995 my ($class, @params) = @_;
17              
18             # Create object.
19 5         13 my $self = bless {}, $class;
20              
21             # Background color.
22 5         19 $self->{'bg'} = 'black';
23              
24             # Flip flag.
25 5         14 $self->{'flip'} = 1;
26              
27             # Foreground color.
28 5         9 $self->{'fg'} = 'white';
29              
30             # Sizes.
31 5         10 $self->{'width'} = 1920;
32 5         9 $self->{'height'} = 1080;
33              
34             # Image type.
35 5         9 $self->{'type'} = 'bmp';
36              
37             # Process params.
38 5         21 set_params($self, @params);
39              
40             # Flip stay.
41 3         44 $self->{'_flip_stay'} = 0;
42              
43             # Imager object.
44             $self->{'_imager'} = Imager->new(
45             'xsize' => $self->{'width'},
46 3         16 'ysize' => $self->{'height'},
47             );
48              
49             # Check type.
50 3 100       17509 if (defined $self->{'type'}) {
51 2         9 $self->_check_type($self->{'type'});
52             }
53              
54             # Object.
55 2         15 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 0     0 1 0 my ($self, $type) = @_;
105 0 0       0 if ($type) {
106 0         0 $self->_check_type($type);
107 0         0 $self->{'type'} = $type;
108             }
109 0         0 return $self->{'type'};
110             }
111              
112             # Check supported image type.
113             sub _check_type {
114 2     2   7 my ($self, $type) = @_;
115              
116             # Check type.
117 2 100   10   31 if (none { $type eq $_ } ('bmp', 'gif', 'jpeg', 'png',
  10         21  
118             'pnm', 'raw', 'sgi', 'tga', 'tiff')) {
119              
120 1         8 err "Image type '$type' doesn't supported."
121             }
122              
123 1         5 return;
124             }
125              
126             1;
127              
128             __END__