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